Skip to content

fix(svg): keep the page's line breaks under the layout zoom - #565

Merged
tinchox5 merged 2 commits into
zumerlab:devfrom
Hema2-official:fix/firefox-zoom-line-breaks
Nov 17, 2026
Merged

tinchox5 merged 2 commits into
zumerlab:devfrom
Hema2-official:fix/firefox-zoom-line-breaks

Conversation

@Hema2-official

@Hema2-official Hema2-official commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

At a devicePixelRatio other than 1, the clone is laid out under the layout zoom (#508), and its text doesn't wrap quite like the page's. In Firefox at fractional scales, a line comes out up to 0.33px wider or narrower. In Chrome, box edges land on the zoomed 1/64px grid. A nearly full line then breaks a word early, and fixed-height boxes overflow. In Firefox, a short word can also move up a line.

This reads where each text node's lines break on the page and pins the lines at risk: each one goes into a nowrap span, in a copy of the foreignObject that the SVG engine serializes. The clone stays intact for burst. An inline-block or image that shares a line gets a span of its own, so the line can't break beside it either.

A line is at risk when its room is within the drift the engine can add. In Firefox, it's also at risk when the next line's first word would fit within that drift. The bound grows with the characters on the line in Firefox and with the box edges in Chrome. A context it can't judge that way, such as bidi, justified text, columns, floats or a CSS zoom, is pinned whole. The pass runs only when the layout zoom isn't 1, so Safari, 1× screens and Firefox at 2× and 3× skip it.

Firefox at 1.25×: page, 3.7.6, this PR

Chrome at 2×: page, 3.7.6, this PR

On my test pages, lines that break differently from the page went from 1–76 per page to 0 in Firefox at 1.25× and 1.5×, and from up to 26 per page to 0 in Chrome at 1.25× and 2×.

snapdom() plus toCanvas() takes longer:

  • A viewport shot: 1–3ms in Chrome at 2×, 2–6.5ms in Firefox at 1.25×.
  • A whole text-heavy page: 1.4–18ms (4–14%) in Chrome, 6–44ms (8–17%) in Firefox.

npm run check passes, and the new module.lineBreaks tests pass. The Chromium suite shows no new failures. I haven't run the Firefox and WebKit suites.

@tinchox5

Copy link
Copy Markdown
Member

Thanks a lot for this. The write-up is clear and the fix does what it says. However, I can't merge it as it is because of speed, which is the first rule of the project, but I think a much cheaper version is within reach and I'd love for you to take it there.

Give me some time to provide you and idea to get the same but cheaper.

@tinchox5

Copy link
Copy Markdown
Member

Could you look into a way to detect only the risky cases, instead of applying it to every line? From what I saw, the only lines that break early are the ones that almost fill their box on the page; a line with room to spare never moves.

If the pass can find those cheaply and leave the rest alone, most captures would pay close to nothing. freezeOneLine in src/modules/styles.js does something similar for one-line boxes and is a good place to start.

If that turns out not to be possible, the last resort would be to run it only under the existing reconcile: true option, which already means "exact layout, slower". I'd rather have it work for everyone by default, though.

@Hema2-official

Hema2-official commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor Author

That's valid. I'm looking into it. Thanks!

Regarding performance: on the side, I'm working on verifying some optimizations related to listing nodes and styles. If everything goes well, expect -20% in initial capture time. I'm watching out not to replicate changes on dev.

@tinchox5

Copy link
Copy Markdown
Member

Great, thanks! I'll wait for the update.

And the optimization sounds very welcome. Besides speed, keep two limits in mind:

  • Fidelity: the capture has to stay identical to what it is today.
  • Cross-browser: it has to hold on all three engines (Chromium, Firefox and WebKit), since several changes here behaved differently on each one.

npm run test:full covers both.

Another option is to base it on dev. That branch is changing a lot right now, so if you can wait a few days until it settles, your work would land on the new structure and you wouldn't have to redo it.

@tinchox5

Copy link
Copy Markdown
Member

Small update: dev now has a change that touches the same area as this PR. Firefox no longer applies the layout zoom on 2x and 3x screens, so the early line breaks are gone there without any extra work per capture.

What's left for your pass is Firefox at fractional scales (like your 1.25x example) and Chrome at 2x. Could you rebase onto dev and work from there? It will save you a conflict in svg.js, and with fewer cases to cover the cheaper version should be easier to reach.

@Hema2-official

Copy link
Copy Markdown
Contributor Author

As for your older comment: ofc, identical results are a must, I'm paying attention there.

Otherwise wow, that's amazing! I'm gonna rebase to dev as soon as I'm free.

@tinchox5

Copy link
Copy Markdown
Member

Alright, the dev branch is "stable", so please work with it. First, we should solve the zoom issue you detected. And, then your speed improvements. So you can propose several PRs.

Thank you!

Hema2-official and others added 2 commits November 17, 2026 00:59
Under the layout zoom (zumerlab#508), Firefox lays a line of text out up to
0.33px wider or narrower than the page, and Chrome puts box edges on
the zoomed 1/64px grid. A nearly full line broke a word early.

The SVG engine now serializes a copy of the tree with each live line in
a `nowrap` span, and each inline-block or image that shares a line in
one of its own, so the shot wraps only where the page did. The clone
stays intact for burst. Lines breaking differently from the page went
from up to 76 per page to 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Pinning every line cost Chrome 5-12% of a capture. The pass now
compares each line's room, and in Firefox the next line's first word,
with how far the engine can drift, and pins only the inline formatting
contexts at risk or the ones it cannot read that way.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Hema2-official
Hema2-official changed the base branch from main to dev November 17, 2026 00:08
@Hema2-official
Hema2-official force-pushed the fix/firefox-zoom-line-breaks branch from aefea55 to 849a2a5 Compare November 17, 2026 00:08
@Hema2-official

Copy link
Copy Markdown
Contributor Author

I updated the base branch and the code. Now it takes up to 34ms less than the original version.
It still takes some time (see the bullet-point numbers at the bottom of the PR description), but I hope this can be acceptable in return for more accuracy by default.

WebKit measurements are missing due to my development setup being weird asl. I'd appreciate it much if you or anyone else could provide those...

Optimization PR incoming soon(TM).

@tinchox5
tinchox5 merged commit 849a2a5 into zumerlab:dev Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants