Skip to content

Commit

Permalink
Cherry-pick 1be1a3f. rdar://123854414
Browse files Browse the repository at this point in the history
    text-wrap: balance, prefering longer lines at the beginning of block
    https://bugs.webkit.org/show_bug.cgi?id=268780
    rdar://122338948

    Reviewed by Alan Baradlay.

    When balancing, there are different results that produce the same score,
    i.e.: the overall deviation to the ideal line is the same for different
    layout configurations. The current implementation prefers to keep longer
    lines at the end of the block, rather than at the beginning. Although
    this is not a bug, since specification doesn't dictates the shape of
    balancing, this might look a bit weird for some authors, as we are used
    to the Greedy behavior of "wrap", which will have a shorter line at the
    end.

    Also, other UAs prefers to have longer lines at the beginning, so, although
    it is not a bug, it would make the feature less confusing for authors if we
    would match the same behavior.

    We are introducing a new test to track the behavior of WebKit shape, but since
    this is not dictate by specification, this should not be WPT test.

    * LayoutTests/fast/text/whitespace/text-wrap-balance-shape-expected.html: Added.
    * LayoutTests/fast/text/whitespace/text-wrap-balance-shape.html: Added.
    * Source/WebCore/layout/formattingContexts/inline/InlineContentBalancer.cpp:
    (WebCore::Layout::InlineContentBalancer::balanceRangeWithLineRequirement):

    Canonical link: https://commits.webkit.org/274125@main

Canonical link: https://commits.webkit.org/272448.663@safari-7618-branch
  • Loading branch information
vitorroriz authored and Dan Robson committed Mar 1, 2024
1 parent a7d6999 commit b36c04b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-text-4/#valdef-text-wrap-balance">
<!-- This tests that we prefer to accumulate longer lines at the beginning of the block, rather than at the end.
Since specification (see href) doesn't dictates the shape itself, this should not be a general web-platform test, since other UAs might prefer to shape it on a different way. -->
<style>
.container {
font-family: monospace;
font-size: 20px;
width: 20ch;
}
</style>
<div class="container">
123 567 901 345 789
123 567 901 345 789
123 123 567 901<br>
345 789 123 567<br>
901 345 789 123
</div>
19 changes: 19 additions & 0 deletions LayoutTests/fast/text/whitespace/text-wrap-balance-shape.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<link rel="help" href="https://drafts.csswg.org/css-text-4/#valdef-text-wrap-balance">
<!-- This tests that we prefer to accumulate longer lines at the beginning of the block, rather than at the end.
Since specification (see href) doesn't dictates the shape itself, this should not be a general web-platform test, since other UAs might prefer to shape it on a different way. -->
<style>
.container {
font-family: monospace;
font-size: 20px;
width: 20ch;
text-wrap: balance;
}
</style>
<div class="container">
123 567 901 345 789
123 567 901 345 789 123
123 567 901 345 789
123 567 901 345 789
123
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "InlineLineBuilder.h"
#include "RenderStyleInlines.h"
#include <limits>
#include <wtf/MathExtras.h>

namespace WebCore {
namespace Layout {
Expand Down Expand Up @@ -292,7 +293,8 @@ std::optional<Vector<LayoutUnit>> InlineContentBalancer::balanceRangeWithLineReq
// Compute the cost of this line based on the line index
for (size_t lineIndex = 1; lineIndex <= numberOfLines; lineIndex++) {
auto accumulatedCost = candidateLineCost + state[startIndex][lineIndex - 1].accumulatedCost;
if (accumulatedCost < state[breakIndex][lineIndex].accumulatedCost) {
auto currentAccumulatedCost = state[breakIndex][lineIndex].accumulatedCost;
if (accumulatedCost < currentAccumulatedCost || WTF::areEssentiallyEqual(accumulatedCost, currentAccumulatedCost)) {
state[breakIndex][lineIndex].accumulatedCost = accumulatedCost;
state[breakIndex][lineIndex].previousBreakIndex = startIndex;
}
Expand Down

0 comments on commit b36c04b

Please sign in to comment.