Skip to content

[css-text-4] Should text-wrap: balance apply separate logic before and after forced breaks? #9112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
RWDavid opened this issue Jul 25, 2023 · 8 comments
Assignees
Labels

Comments

@RWDavid
Copy link

RWDavid commented Jul 25, 2023

Relevant spec: https://drafts.csswg.org/css-text-4/#valdef-text-wrap-balance

Currently, text-wrap: balance applies to the line boxes of an inline formatting context.

Line boxes are balanced when the standard deviation from the average inline-size of the remaining space in each line box is reduced over the block (including lines that end in a forced break).

However, it is unclear whether inline content broken apart by a <br> should cause text-wrap: balance to treat them as two separate 'units' to be balanced. For example, given the following code snippet:

<style>
div {
    width: 20em;
    text-wrap: balance;
}
</style>

<div>
    Paragraph A: Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
  <br>
    Paragraph B: Feugiat in fermentum posuere urna nec tincidunt praesent. Porttitor lacus luctus accumsan tortor posuere ac ut. Aliquet porttitor lacus luctus accumsan tortor. Nibh sed pulvinar.
</div>

Should the browser attempt to balance Paragraphs A and B individually (with the goal of reducing variance of line lengths within each paragraph) or together (with the goal of reducing overall line length variance)?

I did some investigation into some text-balancing algorithms, and it's worth noting that some algorithms will produce virtually identical line-breaking decisions regardless of balancing them together or separately. For example, if you define a cost function to be $f(w) = (w - \overline{w})^2$, where $w$ is a candidate line width and $\overline{w}$ is the ideal/average line width, then minimizing this cost function across all lines will result in identical line-breaking decisions. This cost function also models variance pretty well.

However, for other text-balancing algorithms, applying them on individual paragraphs delimited by forced breaks can result in different line breaking decisions, so I think it's worth trying to clarify this now.

@frivoal
Copy link
Collaborator

frivoal commented Jul 25, 2023

@astearns @fantasai @litherum @jfkthame what do you think?

@frivoal
Copy link
Collaborator

frivoal commented Jul 25, 2023

Since "lines" is plural in this part of the spec,

(including lines that end in a forced break)

I think the intended meaning is that you balance the whole block as one thing, across forced line breaks. Otherwise, for each sub-block, you'd only be taking into account one "line that ends in a forced break" at a time.

But I don't think it would hurt to be explicit if that is what we mean.

On the other hand, if the difference in layout is minimal in most cases, maybe it's worth defining it the other way around, or at least allowing UAs to treat each sub-block separately, as that should have better performance characteristics.

@jfkthame
Copy link
Contributor

My gut feeling is that a forced break should "reset" such a balancing algorithm, so that it applies independently to the lines before the break and those after it.

(Consider content such as:

    <div style="text-wrap:balance">
        Hello!<br><br>
        Lorem ipsum .... etc, for a couple lines' worth of text.<br><br>
        That's all, folks!
    </div>

If we're expected to "globally balance" the line widths, the presence of the forced short lines may have an undesired effect on the lines in the middle section.)

@kojiishi
Copy link
Contributor

+1 to @jfkthame.

@nigelmegitt
Copy link

Also +1 to @jfkthame

@frivoal
Copy link
Collaborator

frivoal commented Oct 4, 2023

I'm not sure it would make that much of a difference, or maybe none at all. Since balance is not allowed to change the number of lines, only where they wrap. Even in the presence of forcibly short lines, this will not result in a bunch of short lines.

How much it actually matters might depend on the exact definition we give to what it means to balance (see #9111), but I'm starting to think it's probably not that relevant, ant that we might want to go with whatever is simplest to do.

@kojiishi
Copy link
Contributor

kojiishi commented Oct 4, 2023

From the performance point of view, though the algorithm is not defined, the algorithm is likely O(n), O(n!), or O(n^2) for the n lines of the "paragraph". It'll perform better if the "paragraph" has less lines.

@css-meeting-bot
Copy link
Member

css-meeting-bot commented Jan 10, 2024

The CSS Working Group just discussed [css-text-4] Should `text-wrap: balance` apply separate logic before and after forced breaks?.

RESOLVED: Forced break resets the balancing algorithm for text-wrap:balance

The full IRC log of that discussion <fantasai> proposal: https://github.com//issues/9112#issuecomment-1649890262
<frances> Alan: next issue is on text-wrap: balance, what happens before and after they break?
<frances> fantasai: Johnathan proposed, applies to before and after the lines the lines
<fantasai> s/lines/lines independently/
<frances> Alan: discussed it before, should the balancing algorithm stop or continue? Does it continue and try to balance before or after the break? One or more balancing sections?
<frances> Florian: Proposal is good, separate sets for performance could be good
<frances> Alan: Any other opinions?
<florian> s/could be good/is be good, and no downside identified
<miriam> q+
<frances> Eric: Can think of possibility where to balance and center and force break at a certain point, maybe one word that is centered in the middle of it all. Feel like as an author, wrapping spans anyway
<frances> Alan: Because of the way we are measuring better balance, we are already accounting for different line lengths for fragmentation and line breaks, hard pressed to find result that is substantially different. Balance better to improve according to the spec.
<frances> David: The other thing is that if we want to group across breaks, it would be good for blocking across elements, if there were such a thing, it make sense to be a separate mechanism for both
<astearns> ack miriam
<frances> Miriam: Might be misunderstanding this as being fragmentation breaks possibly
<frances> fantasai: Still line breaking across the fragmentation breaks, if not after the fragmentation and two long lines from wrapping, need to balance across both
<frances> Miriam: more about hard breaks
<frances> David: where you break the lines can influence where you fragment
<frances> Alan: I was incorrect. should be only forced breaks
<frances> Alan; any other comments?
<frances> PROPOSAL: Forced break resets the balancing algorithm for text-wrap:balance

@frivoal frivoal self-assigned this Mar 27, 2024
frivoal added a commit to frivoal/wpt that referenced this issue Mar 27, 2024
frivoal added a commit to web-platform-tests/wpt that referenced this issue Mar 27, 2024
@frivoal frivoal added Tested Memory aid - issue has WPT tests and removed Needs Edits Needs Testcase (WPT) labels Mar 27, 2024
@frivoal frivoal closed this as completed Mar 27, 2024
jackyzy823 pushed a commit to jackyzy823/gecko-dev that referenced this issue Apr 1, 2024
… a=testonly

Automatic update from web-platform-tests
Add tests for text-wrap: balance and br

Related to w3c/csswg-drafts#9112

--

wpt-commits: dad6f5e038ec13e3506e9a869d759b0138131ee3
wpt-pr: 45369
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants