| 1 | /* |
| 2 | * Copyright (C) 2019 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "InlineFormattingContext.h" |
| 28 | |
| 29 | #if ENABLE(LAYOUT_FORMATTING_CONTEXT) |
| 30 | |
| 31 | #include "FloatingContext.h" |
| 32 | #include "FloatingState.h" |
| 33 | #include "InlineFormattingState.h" |
| 34 | #include "InlineLineBreaker.h" |
| 35 | #include "InlineRunProvider.h" |
| 36 | #include "LayoutBox.h" |
| 37 | #include "LayoutContainer.h" |
| 38 | #include "LayoutState.h" |
| 39 | #include "TextUtil.h" |
| 40 | |
| 41 | namespace WebCore { |
| 42 | namespace Layout { |
| 43 | |
| 44 | class Line { |
| 45 | public: |
| 46 | void init(const LayoutPoint& topLeft, LayoutUnit availableWidth, LayoutUnit minimalHeight); |
| 47 | void close(); |
| 48 | |
| 49 | void appendContent(const InlineRunProvider::Run&, const LayoutSize&); |
| 50 | |
| 51 | void adjustLogicalLeft(LayoutUnit delta); |
| 52 | void adjustLogicalRight(LayoutUnit delta); |
| 53 | |
| 54 | bool hasContent() const { return !m_inlineRuns.isEmpty(); } |
| 55 | bool isClosed() const { return m_closed; } |
| 56 | bool isFirstLine() const { return m_isFirstLine; } |
| 57 | Vector<InlineRun>& runs() { return m_inlineRuns; } |
| 58 | |
| 59 | LayoutUnit contentLogicalRight() const; |
| 60 | LayoutUnit contentLogicalLeft() const { return m_logicalRect.left(); } |
| 61 | LayoutUnit availableWidth() const { return m_availableWidth; } |
| 62 | Optional<InlineRunProvider::Run::Type> lastRunType() const { return m_lastRunType; } |
| 63 | |
| 64 | LayoutUnit logicalTop() const { return m_logicalRect.top(); } |
| 65 | LayoutUnit logicalBottom() const { return m_logicalRect.bottom(); } |
| 66 | LayoutUnit logicalHeight() const { return logicalBottom() - logicalTop(); } |
| 67 | |
| 68 | private: |
| 69 | struct TrailingTrimmableContent { |
| 70 | LayoutUnit width; |
| 71 | unsigned length; |
| 72 | }; |
| 73 | Optional<TrailingTrimmableContent> m_trailingTrimmableContent; |
| 74 | Optional<InlineRunProvider::Run::Type> m_lastRunType; |
| 75 | bool m_lastRunCanExpand { false }; |
| 76 | |
| 77 | Display::Box::Rect m_logicalRect; |
| 78 | LayoutUnit m_availableWidth; |
| 79 | |
| 80 | Vector<InlineRun> m_inlineRuns; |
| 81 | bool m_isFirstLine { true }; |
| 82 | bool m_closed { true }; |
| 83 | }; |
| 84 | |
| 85 | void Line::init(const LayoutPoint& topLeft, LayoutUnit availableWidth, LayoutUnit minimalHeight) |
| 86 | { |
| 87 | m_logicalRect.setTopLeft(topLeft); |
| 88 | m_logicalRect.setWidth(availableWidth); |
| 89 | m_logicalRect.setHeight(minimalHeight); |
| 90 | m_availableWidth = availableWidth; |
| 91 | |
| 92 | m_inlineRuns.clear(); |
| 93 | m_lastRunType = { }; |
| 94 | m_lastRunCanExpand = false; |
| 95 | m_trailingTrimmableContent = { }; |
| 96 | m_closed = false; |
| 97 | } |
| 98 | |
| 99 | void Line::adjustLogicalLeft(LayoutUnit delta) |
| 100 | { |
| 101 | ASSERT(delta > 0); |
| 102 | |
| 103 | m_availableWidth -= delta; |
| 104 | m_logicalRect.shiftLeftTo(m_logicalRect.left() + delta); |
| 105 | |
| 106 | for (auto& inlineRun : m_inlineRuns) |
| 107 | inlineRun.moveHorizontally(delta); |
| 108 | } |
| 109 | |
| 110 | void Line::adjustLogicalRight(LayoutUnit delta) |
| 111 | { |
| 112 | ASSERT(delta > 0); |
| 113 | |
| 114 | m_availableWidth -= delta; |
| 115 | m_logicalRect.shiftRightTo(m_logicalRect.right() - delta); |
| 116 | } |
| 117 | |
| 118 | static bool isTrimmableContent(const InlineRunProvider::Run& inlineRun) |
| 119 | { |
| 120 | return inlineRun.isWhitespace() && inlineRun.style().collapseWhiteSpace(); |
| 121 | } |
| 122 | |
| 123 | LayoutUnit Line::contentLogicalRight() const |
| 124 | { |
| 125 | if (m_inlineRuns.isEmpty()) |
| 126 | return m_logicalRect.left(); |
| 127 | |
| 128 | return m_inlineRuns.last().logicalRight(); |
| 129 | } |
| 130 | |
| 131 | void Line::appendContent(const InlineRunProvider::Run& run, const LayoutSize& runSize) |
| 132 | { |
| 133 | ASSERT(!isClosed()); |
| 134 | |
| 135 | // Append this text run to the end of the last text run, if the last run is continuous. |
| 136 | Optional<InlineRun::TextContext> textRun; |
| 137 | if (run.isText()) { |
| 138 | auto textContext = run.textContext(); |
| 139 | auto runLength = textContext->isCollapsed() ? 1 : textContext->length(); |
| 140 | textRun = InlineRun::TextContext { textContext->start(), runLength }; |
| 141 | } |
| 142 | |
| 143 | auto requiresNewInlineRun = !hasContent() || !run.isText() || !m_lastRunCanExpand; |
| 144 | if (requiresNewInlineRun) { |
| 145 | // FIXME: This needs proper baseline handling |
| 146 | auto inlineRun = InlineRun { { logicalTop(), contentLogicalRight(), runSize.width(), runSize.height() }, run.inlineItem() }; |
| 147 | if (textRun) |
| 148 | inlineRun.setTextContext({ textRun->start(), textRun->length() }); |
| 149 | m_inlineRuns.append(inlineRun); |
| 150 | m_logicalRect.setHeight(std::max(runSize.height(), m_logicalRect.height())); |
| 151 | } else { |
| 152 | // Non-text runs always require new inline run. |
| 153 | ASSERT(textRun); |
| 154 | auto& inlineRun = m_inlineRuns.last(); |
| 155 | ASSERT(runSize.height() == inlineRun.logicalHeight()); |
| 156 | inlineRun.setLogicalWidth(inlineRun.logicalWidth() + runSize.width()); |
| 157 | inlineRun.textContext()->setLength(inlineRun.textContext()->length() + textRun->length()); |
| 158 | } |
| 159 | |
| 160 | m_availableWidth -= runSize.width(); |
| 161 | m_lastRunType = run.type(); |
| 162 | m_lastRunCanExpand = run.isText() && !run.textContext()->isCollapsed(); |
| 163 | m_trailingTrimmableContent = { }; |
| 164 | if (isTrimmableContent(run)) |
| 165 | m_trailingTrimmableContent = TrailingTrimmableContent { runSize.width(), textRun->length() }; |
| 166 | } |
| 167 | |
| 168 | void Line::close() |
| 169 | { |
| 170 | auto trimTrailingContent = [&] { |
| 171 | if (!m_trailingTrimmableContent) |
| 172 | return; |
| 173 | auto& lastInlineRun = m_inlineRuns.last(); |
| 174 | lastInlineRun.setLogicalWidth(lastInlineRun.logicalWidth() - m_trailingTrimmableContent->width); |
| 175 | lastInlineRun.textContext()->setLength(lastInlineRun.textContext()->length() - m_trailingTrimmableContent->length); |
| 176 | |
| 177 | if (!lastInlineRun.textContext()->length()) |
| 178 | m_inlineRuns.removeLast(); |
| 179 | m_availableWidth += m_trailingTrimmableContent->width; |
| 180 | m_trailingTrimmableContent = { }; |
| 181 | }; |
| 182 | |
| 183 | if (!hasContent()) |
| 184 | return; |
| 185 | |
| 186 | trimTrailingContent(); |
| 187 | m_isFirstLine = false; |
| 188 | m_closed = true; |
| 189 | } |
| 190 | |
| 191 | InlineFormattingContext::LineLayout::LineLayout(const InlineFormattingContext& inlineFormattingContext) |
| 192 | : m_formattingContext(inlineFormattingContext) |
| 193 | , m_formattingState(m_formattingContext.formattingState()) |
| 194 | , m_floatingState(m_formattingState.floatingState()) |
| 195 | , m_formattingRoot(downcast<Container>(m_formattingContext.root())) |
| 196 | { |
| 197 | } |
| 198 | |
| 199 | static bool isTrimmableContent(const InlineLineBreaker::Run& run) |
| 200 | { |
| 201 | return run.content.isWhitespace() && run.content.style().collapseWhiteSpace(); |
| 202 | } |
| 203 | |
| 204 | void InlineFormattingContext::LineLayout::layout(const InlineRunProvider& inlineRunProvider) const |
| 205 | { |
| 206 | auto& layoutState = m_formattingContext.layoutState(); |
| 207 | auto floatingContext = FloatingContext { m_floatingState }; |
| 208 | |
| 209 | Line line; |
| 210 | initializeNewLine(line); |
| 211 | |
| 212 | InlineLineBreaker lineBreaker(layoutState, m_formattingState.inlineContent(), inlineRunProvider.runs()); |
| 213 | while (auto run = lineBreaker.nextRun(line.contentLogicalRight(), line.availableWidth(), !line.hasContent())) { |
| 214 | auto isFirstRun = run->position == InlineLineBreaker::Run::Position::LineBegin; |
| 215 | auto isLastRun = run->position == InlineLineBreaker::Run::Position::LineEnd; |
| 216 | auto generatesInlineRun = true; |
| 217 | |
| 218 | // Position float and adjust the runs on line. |
| 219 | if (run->content.isFloat()) { |
| 220 | auto& floatBox = run->content.inlineItem().layoutBox(); |
| 221 | computeFloatPosition(floatingContext, line, floatBox); |
| 222 | m_floatingState.append(floatBox); |
| 223 | |
| 224 | auto floatBoxWidth = layoutState.displayBoxForLayoutBox(floatBox).marginBox().width(); |
| 225 | // Shrink availble space for current line and move existing inline runs. |
| 226 | floatBox.isLeftFloatingPositioned() ? line.adjustLogicalLeft(floatBoxWidth) : line.adjustLogicalRight(floatBoxWidth); |
| 227 | |
| 228 | generatesInlineRun = false; |
| 229 | } |
| 230 | |
| 231 | // 1. Initialize new line if needed. |
| 232 | // 2. Append inline run unless it is skipped. |
| 233 | // 3. Close current line if needed. |
| 234 | if (isFirstRun) { |
| 235 | // When the first run does not generate an actual inline run, the next run comes in first-run as well. |
| 236 | // No need to spend time on closing/initializing. |
| 237 | // Skip leading whitespace. |
| 238 | if (!generatesInlineRun || isTrimmableContent(*run)) |
| 239 | continue; |
| 240 | |
| 241 | if (line.hasContent()) { |
| 242 | // Previous run ended up being at the line end. Adjust the line accordingly. |
| 243 | if (!line.isClosed()) |
| 244 | closeLine(line, IsLastLine::No); |
| 245 | initializeNewLine(line); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | if (generatesInlineRun) { |
| 250 | auto width = run->width; |
| 251 | auto height = run->content.isText() ? LayoutUnit(m_formattingRoot.style().computedLineHeight()) : layoutState.displayBoxForLayoutBox(run->content.inlineItem().layoutBox()).height(); |
| 252 | appendContentToLine(line, run->content, { width, height }); |
| 253 | } |
| 254 | |
| 255 | if (isLastRun) |
| 256 | closeLine(line, IsLastLine::No); |
| 257 | } |
| 258 | |
| 259 | closeLine(line, IsLastLine::Yes); |
| 260 | } |
| 261 | |
| 262 | void InlineFormattingContext::LineLayout::initializeNewLine(Line& line) const |
| 263 | { |
| 264 | auto& formattingRootDisplayBox = m_formattingContext.layoutState().displayBoxForLayoutBox(m_formattingRoot); |
| 265 | |
| 266 | auto lineLogicalLeft = formattingRootDisplayBox.contentBoxLeft(); |
| 267 | auto lineLogicalTop = line.isFirstLine() ? formattingRootDisplayBox.contentBoxTop() : line.logicalBottom(); |
| 268 | auto availableWidth = formattingRootDisplayBox.contentBoxWidth(); |
| 269 | |
| 270 | // Check for intruding floats and adjust logical left/available width for this line accordingly. |
| 271 | if (!m_floatingState.isEmpty()) { |
| 272 | auto floatConstraints = m_floatingState.constraints({ lineLogicalTop }, m_formattingRoot); |
| 273 | // Check if these constraints actually put limitation on the line. |
| 274 | if (floatConstraints.left && *floatConstraints.left <= formattingRootDisplayBox.contentBoxLeft()) |
| 275 | floatConstraints.left = { }; |
| 276 | |
| 277 | if (floatConstraints.right && *floatConstraints.right >= formattingRootDisplayBox.contentBoxRight()) |
| 278 | floatConstraints.right = { }; |
| 279 | |
| 280 | if (floatConstraints.left && floatConstraints.right) { |
| 281 | ASSERT(*floatConstraints.left < *floatConstraints.right); |
| 282 | availableWidth = *floatConstraints.right - *floatConstraints.left; |
| 283 | lineLogicalLeft = *floatConstraints.left; |
| 284 | } else if (floatConstraints.left) { |
| 285 | ASSERT(*floatConstraints.left > lineLogicalLeft); |
| 286 | availableWidth -= (*floatConstraints.left - lineLogicalLeft); |
| 287 | lineLogicalLeft = *floatConstraints.left; |
| 288 | } else if (floatConstraints.right) { |
| 289 | ASSERT(*floatConstraints.right > lineLogicalLeft); |
| 290 | availableWidth = *floatConstraints.right - lineLogicalLeft; |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | line.init({ lineLogicalLeft, lineLogicalTop }, availableWidth, m_formattingRoot.style().computedLineHeight()); |
| 295 | } |
| 296 | |
| 297 | void InlineFormattingContext::LineLayout::splitInlineRunIfNeeded(const InlineRun& inlineRun, InlineRuns& splitRuns) const |
| 298 | { |
| 299 | ASSERT(inlineRun.textContext()); |
| 300 | ASSERT(inlineRun.overlapsMultipleInlineItems()); |
| 301 | // In certain cases, a run can overlap multiple inline elements like this: |
| 302 | // <span>normal text content</span><span style="position: relative; left: 10px;">but this one needs a dedicated run</span><span>end of text</span> |
| 303 | // The content above generates one long run <normal text contentbut this one needs dedicated runend of text> |
| 304 | // However, since the middle run is positioned, it needs to be moved independently from the rest of the content, hence it needs a dedicated inline run. |
| 305 | |
| 306 | // 1. Start with the first inline item (element) and travers the list until |
| 307 | // 2. either find an inline item that needs a dedicated run or we reach the end of the run |
| 308 | // 3. Create dedicate inline runs. |
| 309 | auto& inlineContent = m_formattingState.inlineContent(); |
| 310 | auto contentStart = inlineRun.logicalLeft(); |
| 311 | auto startPosition = inlineRun.textContext()->start(); |
| 312 | auto remaningLength = inlineRun.textContext()->length(); |
| 313 | |
| 314 | struct Uncommitted { |
| 315 | const InlineItem* firstInlineItem { nullptr }; |
| 316 | const InlineItem* lastInlineItem { nullptr }; |
| 317 | unsigned length { 0 }; |
| 318 | }; |
| 319 | Optional<Uncommitted> uncommitted; |
| 320 | |
| 321 | auto commit = [&] { |
| 322 | if (!uncommitted) |
| 323 | return; |
| 324 | |
| 325 | contentStart += uncommitted->firstInlineItem->nonBreakableStart(); |
| 326 | |
| 327 | auto runWidth = this->runWidth(inlineContent, *uncommitted->firstInlineItem, startPosition, uncommitted->length, contentStart); |
| 328 | auto run = InlineRun { { inlineRun.logicalTop(), contentStart, runWidth, inlineRun.logicalHeight() }, *uncommitted->firstInlineItem }; |
| 329 | run.setTextContext({ startPosition, uncommitted->length }); |
| 330 | splitRuns.append(run); |
| 331 | |
| 332 | contentStart += runWidth + uncommitted->lastInlineItem->nonBreakableEnd(); |
| 333 | |
| 334 | startPosition = 0; |
| 335 | uncommitted = { }; |
| 336 | }; |
| 337 | |
| 338 | for (auto iterator = inlineContent.find(const_cast<InlineItem*>(&inlineRun.inlineItem())); iterator != inlineContent.end() && remaningLength > 0; ++iterator) { |
| 339 | auto& inlineItem = **iterator; |
| 340 | |
| 341 | // Skip all non-inflow boxes (floats, out-of-flow positioned elements). They don't participate in the inline run context. |
| 342 | if (!inlineItem.layoutBox().isInFlow()) |
| 343 | continue; |
| 344 | |
| 345 | auto currentLength = [&] { |
| 346 | return std::min(remaningLength, inlineItem.textContent().length() - startPosition); |
| 347 | }; |
| 348 | |
| 349 | // 1. Break before/after -> requires dedicated run -> commit what we've got so far and also commit the current inline element as a separate inline run. |
| 350 | // 2. Break at the beginning of the inline element -> commit what we've got so far. Current element becomes the first uncommitted. |
| 351 | // 3. Break at the end of the inline element -> commit what we've got so far including the current element. |
| 352 | // 4. Inline element does not require run breaking -> add current inline element to uncommitted. Jump to the next element. |
| 353 | auto detachingRules = inlineItem.detachingRules(); |
| 354 | |
| 355 | // #1 |
| 356 | if (detachingRules.containsAll({ InlineItem::DetachingRule::BreakAtStart, InlineItem::DetachingRule::BreakAtEnd })) { |
| 357 | commit(); |
| 358 | auto contentLength = currentLength(); |
| 359 | uncommitted = Uncommitted { &inlineItem, &inlineItem, contentLength }; |
| 360 | remaningLength -= contentLength; |
| 361 | commit(); |
| 362 | continue; |
| 363 | } |
| 364 | |
| 365 | // #2 |
| 366 | if (detachingRules.contains(InlineItem::DetachingRule::BreakAtStart)) |
| 367 | commit(); |
| 368 | |
| 369 | // Add current inline item to uncommitted. |
| 370 | // #3 and #4 |
| 371 | auto contentLength = currentLength(); |
| 372 | if (!uncommitted) |
| 373 | uncommitted = Uncommitted { &inlineItem, &inlineItem, 0 }; |
| 374 | uncommitted->length += contentLength; |
| 375 | uncommitted->lastInlineItem = &inlineItem; |
| 376 | remaningLength -= contentLength; |
| 377 | |
| 378 | // #3 |
| 379 | if (detachingRules.contains(InlineItem::DetachingRule::BreakAtEnd)) |
| 380 | commit(); |
| 381 | } |
| 382 | // Either all inline elements needed dedicated runs or neither of them. |
| 383 | if (!remaningLength || remaningLength == inlineRun.textContext()->length()) |
| 384 | return; |
| 385 | |
| 386 | commit(); |
| 387 | } |
| 388 | |
| 389 | void InlineFormattingContext::LineLayout::createFinalRuns(Line& line) const |
| 390 | { |
| 391 | for (auto& inlineRun : line.runs()) { |
| 392 | if (inlineRun.overlapsMultipleInlineItems()) { |
| 393 | InlineRuns splitRuns; |
| 394 | splitInlineRunIfNeeded(inlineRun, splitRuns); |
| 395 | for (auto& splitRun : splitRuns) |
| 396 | m_formattingState.appendInlineRun(splitRun); |
| 397 | |
| 398 | if (!splitRuns.isEmpty()) |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | auto finalRun = [&] { |
| 403 | auto& inlineItem = inlineRun.inlineItem(); |
| 404 | if (inlineItem.detachingRules().isEmpty()) |
| 405 | return inlineRun; |
| 406 | |
| 407 | InlineRun adjustedRun = inlineRun; |
| 408 | auto width = inlineRun.logicalWidth() - inlineItem.nonBreakableStart() - inlineItem.nonBreakableEnd(); |
| 409 | adjustedRun.setLogicalLeft(inlineRun.logicalLeft() + inlineItem.nonBreakableStart()); |
| 410 | adjustedRun.setLogicalWidth(width); |
| 411 | return adjustedRun; |
| 412 | }; |
| 413 | |
| 414 | m_formattingState.appendInlineRun(finalRun()); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void InlineFormattingContext::LineLayout::postProcessInlineRuns(Line& line, IsLastLine isLastLine) const |
| 419 | { |
| 420 | alignRuns(m_formattingRoot.style().textAlign(), line, isLastLine); |
| 421 | auto firstRunIndex = m_formattingState.inlineRuns().size(); |
| 422 | createFinalRuns(line); |
| 423 | |
| 424 | placeInFlowPositionedChildren(firstRunIndex); |
| 425 | } |
| 426 | |
| 427 | void InlineFormattingContext::LineLayout::closeLine(Line& line, IsLastLine isLastLine) const |
| 428 | { |
| 429 | line.close(); |
| 430 | if (!line.hasContent()) |
| 431 | return; |
| 432 | |
| 433 | postProcessInlineRuns(line, isLastLine); |
| 434 | } |
| 435 | |
| 436 | void InlineFormattingContext::LineLayout::appendContentToLine(Line& line, const InlineRunProvider::Run& run, const LayoutSize& runSize) const |
| 437 | { |
| 438 | auto lastRunType = line.lastRunType(); |
| 439 | line.appendContent(run, runSize); |
| 440 | |
| 441 | if (m_formattingRoot.style().textAlign() == TextAlignMode::Justify) |
| 442 | computeExpansionOpportunities(line, run, lastRunType.valueOr(InlineRunProvider::Run::Type::NonWhitespace)); |
| 443 | } |
| 444 | |
| 445 | void InlineFormattingContext::LineLayout::computeFloatPosition(const FloatingContext& floatingContext, Line& line, const Box& floatBox) const |
| 446 | { |
| 447 | auto& layoutState = m_formattingContext.layoutState(); |
| 448 | ASSERT(layoutState.hasDisplayBox(floatBox)); |
| 449 | auto& displayBox = layoutState.displayBoxForLayoutBox(floatBox); |
| 450 | |
| 451 | // Set static position first. |
| 452 | displayBox.setTopLeft({ line.contentLogicalRight(), line.logicalTop() }); |
| 453 | // Float it. |
| 454 | displayBox.setTopLeft(floatingContext.positionForFloat(floatBox)); |
| 455 | } |
| 456 | |
| 457 | void InlineFormattingContext::LineLayout::placeInFlowPositionedChildren(unsigned fistRunIndex) const |
| 458 | { |
| 459 | auto& layoutState = m_formattingContext.layoutState(); |
| 460 | auto& inlineRuns = m_formattingState.inlineRuns(); |
| 461 | for (auto runIndex = fistRunIndex; runIndex < inlineRuns.size(); ++runIndex) { |
| 462 | auto& inlineRun = inlineRuns[runIndex]; |
| 463 | |
| 464 | auto positionOffset = [&](auto& layoutBox) { |
| 465 | // FIXME: Need to figure out whether in-flow offset should stick. This might very well be temporary. |
| 466 | Optional<LayoutSize> offset; |
| 467 | for (auto* box = &layoutBox; box != &m_formattingRoot; box = box->parent()) { |
| 468 | if (!box->isInFlowPositioned()) |
| 469 | continue; |
| 470 | offset = offset.valueOr(LayoutSize()) + Geometry::inFlowPositionedPositionOffset(layoutState, *box); |
| 471 | } |
| 472 | return offset; |
| 473 | }; |
| 474 | |
| 475 | if (auto offset = positionOffset(inlineRun.inlineItem().layoutBox())) { |
| 476 | inlineRun.moveVertically(offset->height()); |
| 477 | inlineRun.moveHorizontally(offset->width()); |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static LayoutUnit adjustedLineLogicalLeft(TextAlignMode align, LayoutUnit lineLogicalLeft, LayoutUnit remainingWidth) |
| 483 | { |
| 484 | switch (align) { |
| 485 | case TextAlignMode::Left: |
| 486 | case TextAlignMode::WebKitLeft: |
| 487 | case TextAlignMode::Start: |
| 488 | return lineLogicalLeft; |
| 489 | case TextAlignMode::Right: |
| 490 | case TextAlignMode::WebKitRight: |
| 491 | case TextAlignMode::End: |
| 492 | return lineLogicalLeft + std::max(remainingWidth, 0_lu); |
| 493 | case TextAlignMode::Center: |
| 494 | case TextAlignMode::WebKitCenter: |
| 495 | return lineLogicalLeft + std::max(remainingWidth / 2, 0_lu); |
| 496 | case TextAlignMode::Justify: |
| 497 | ASSERT_NOT_REACHED(); |
| 498 | break; |
| 499 | } |
| 500 | ASSERT_NOT_REACHED(); |
| 501 | return lineLogicalLeft; |
| 502 | } |
| 503 | |
| 504 | void InlineFormattingContext::LineLayout::justifyRuns(Line& line) |
| 505 | { |
| 506 | auto& inlineRuns = line.runs(); |
| 507 | auto& lastInlineRun = inlineRuns.last(); |
| 508 | |
| 509 | // Adjust (forbid) trailing expansion for the last text run on line. |
| 510 | auto expansionBehavior = lastInlineRun.expansionOpportunity().behavior; |
| 511 | // Remove allow and add forbid. |
| 512 | expansionBehavior ^= AllowTrailingExpansion; |
| 513 | expansionBehavior |= ForbidTrailingExpansion; |
| 514 | lastInlineRun.expansionOpportunity().behavior = expansionBehavior; |
| 515 | |
| 516 | // Collect expansion opportunities and justify the runs. |
| 517 | auto widthToDistribute = line.availableWidth(); |
| 518 | if (widthToDistribute <= 0) |
| 519 | return; |
| 520 | |
| 521 | auto expansionOpportunities = 0; |
| 522 | for (auto& inlineRun : inlineRuns) |
| 523 | expansionOpportunities += inlineRun.expansionOpportunity().count; |
| 524 | |
| 525 | if (!expansionOpportunities) |
| 526 | return; |
| 527 | |
| 528 | float expansion = widthToDistribute.toFloat() / expansionOpportunities; |
| 529 | LayoutUnit accumulatedExpansion; |
| 530 | for (auto& inlineRun : inlineRuns) { |
| 531 | auto expansionForRun = inlineRun.expansionOpportunity().count * expansion; |
| 532 | |
| 533 | inlineRun.expansionOpportunity().expansion = expansionForRun; |
| 534 | inlineRun.setLogicalLeft(inlineRun.logicalLeft() + accumulatedExpansion); |
| 535 | inlineRun.setLogicalWidth(inlineRun.logicalWidth() + expansionForRun); |
| 536 | accumulatedExpansion += expansionForRun; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | void InlineFormattingContext::LineLayout::computeExpansionOpportunities(Line& line, const InlineRunProvider::Run& run, InlineRunProvider::Run::Type lastRunType) const |
| 541 | { |
| 542 | auto isExpansionOpportunity = [](auto currentRunIsWhitespace, auto lastRunIsWhitespace) { |
| 543 | return currentRunIsWhitespace || (!currentRunIsWhitespace && !lastRunIsWhitespace); |
| 544 | }; |
| 545 | |
| 546 | auto expansionBehavior = [](auto isAtExpansionOpportunity) { |
| 547 | ExpansionBehavior expansionBehavior = AllowTrailingExpansion; |
| 548 | expansionBehavior |= isAtExpansionOpportunity ? ForbidLeadingExpansion : AllowLeadingExpansion; |
| 549 | return expansionBehavior; |
| 550 | }; |
| 551 | |
| 552 | auto isAtExpansionOpportunity = isExpansionOpportunity(run.isWhitespace(), lastRunType == InlineRunProvider::Run::Type::Whitespace); |
| 553 | |
| 554 | auto& currentInlineRun = line.runs().last(); |
| 555 | auto& expansionOpportunity = currentInlineRun.expansionOpportunity(); |
| 556 | if (isAtExpansionOpportunity) |
| 557 | ++expansionOpportunity.count; |
| 558 | |
| 559 | expansionOpportunity.behavior = expansionBehavior(isAtExpansionOpportunity); |
| 560 | } |
| 561 | |
| 562 | void InlineFormattingContext::LineLayout::alignRuns(TextAlignMode textAlign, Line& line, IsLastLine isLastLine) const |
| 563 | { |
| 564 | auto adjutedTextAlignment = textAlign != TextAlignMode::Justify ? textAlign : isLastLine == IsLastLine::No ? TextAlignMode::Justify : TextAlignMode::Left; |
| 565 | if (adjutedTextAlignment == TextAlignMode::Justify) { |
| 566 | justifyRuns(line); |
| 567 | return; |
| 568 | } |
| 569 | |
| 570 | auto lineLogicalLeft = line.contentLogicalLeft(); |
| 571 | auto adjustedLogicalLeft = adjustedLineLogicalLeft(adjutedTextAlignment, lineLogicalLeft, line.availableWidth()); |
| 572 | if (adjustedLogicalLeft == lineLogicalLeft) |
| 573 | return; |
| 574 | |
| 575 | auto delta = adjustedLogicalLeft - lineLogicalLeft; |
| 576 | for (auto& inlineRun : line.runs()) |
| 577 | inlineRun.setLogicalLeft(inlineRun.logicalLeft() + delta); |
| 578 | } |
| 579 | |
| 580 | LayoutUnit InlineFormattingContext::LineLayout::runWidth(const InlineContent& inlineContent, const InlineItem& inlineItem, ItemPosition from, unsigned length, LayoutUnit contentLogicalLeft) const |
| 581 | { |
| 582 | LayoutUnit width; |
| 583 | auto startPosition = from; |
| 584 | auto iterator = inlineContent.find(const_cast<InlineItem*>(&inlineItem)); |
| 585 | #if !ASSERT_DISABLED |
| 586 | auto inlineItemEnd = inlineContent.end(); |
| 587 | #endif |
| 588 | while (length) { |
| 589 | ASSERT(iterator != inlineItemEnd); |
| 590 | auto& currentInlineItem = **iterator; |
| 591 | auto endPosition = std::min<ItemPosition>(startPosition + length, currentInlineItem.textContent().length()); |
| 592 | auto textWidth = TextUtil::width(currentInlineItem, startPosition, endPosition, contentLogicalLeft); |
| 593 | |
| 594 | contentLogicalLeft += textWidth; |
| 595 | width += textWidth; |
| 596 | length -= (endPosition - startPosition); |
| 597 | |
| 598 | startPosition = 0; |
| 599 | ++iterator; |
| 600 | } |
| 601 | return width; |
| 602 | } |
| 603 | |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | #endif |
| 608 | |