Repository navigation
Commit f1586fc
[web_paragraph] Honor TextHeightBehavior.applyHeightToFirstAscent and applyHeightToLastDescent (flutter#193773)
Honors `TextHeightBehavior.applyHeightToFirstAscent` and
`applyHeightToLastDescent` when computing `WebParagraph` line metrics
(for non-empty paragraphs). Part of flutter#193731.
> [!NOTE]
> Split out from flutter#193403 to keep each change small and focused.
### Problem
Both `applyHeightToFirstAscent` and `applyHeightToLastDescent` were
ignored: the first line always kept its height-scaled ascent and the
last line always kept its height-scaled descent.
### Fix
- `TextLine.rawFontBoundingBoxAscent` / `rawFontBoundingBoxDescent`:
accumulate unscaled font metrics alongside height-scaled metrics in
`TextLine.updateBoundingBox` (and copy them in
`TextLine.inheritMetricsFrom`).
- **Identifying the first and last lines**:
- `isFirstLine` is determined in `TextLayout._finishLine` via
`lines.isEmpty` before the line is appended.
- `isLastLine` is passed from `_LineBuilder.build` (`wrapper.dart`) and
stored on `line.lastLine` in `_finishLine`: a content line is the last
line when it hits the `maxLines` limit or reaches the artificial EOF
cluster without a trailing newline; when a trailing `\n` is present (and
`maxLines` is not yet reached), the synthetic empty line added after
`\n` is marked `isLastLine: true`.
- **Single-pass metric adjustment (`TextLayout._finishLine` ->
`TextLine.applyTextHeightBehavior`)**: before computing `line.advance`,
`isFirstLine && !applyHeightToFirstAscent` sets `usesRawAscent = true`
and reverts `fontBoundingBoxAscent` to `rawFontBoundingBoxAscent`, and
`isLastLine && !applyHeightToLastDescent` sets `usesRawDescent = true`
and reverts `fontBoundingBoxDescent` to `rawFontBoundingBoxDescent`.
Because this happens before `line.advance` is returned to the wrapper,
line tops and paragraph height are accumulated in a single pass.
- **Placeholders & backgrounds**: `PlaceholderBlock.positionInLine`
positions placeholders after `applyTextHeightBehavior` so first-line
placeholders align against the reverted ascent, and
`WebParagraphPainter._paintAllBlocks` bounds background rects by raw
ascent/descent when `usesRawAscent`/`usesRawDescent` are set.
- **Trailing newline**: the synthetic empty line created after a
trailing `\n` inherits the preceding line's scaled and raw metrics via
`inheritMetricsFrom`, so the synthetic last line drops its scaled
descent while the preceding text line keeps its scaled descent —
matching `SkParagraph`.
### Tests
- `paragraph_line_height_test.dart` (+6 tests):
- `TextHeightBehavior reverts a single line to the unscaled font
metrics`
- `TextHeightBehavior affects only the first line ascent and the last
line descent`
- `TextHeightBehavior treats the empty line after a trailing newline as
the last line`
- `TextHeightBehavior applies applyHeightToLastDescent to the maxLines
limit line`
- `TextHeightBehavior positions first-line placeholders against the
reverted ascent`
- `TextHeightBehavior bounds background rects by reverted first ascent
and last descent`
- **WebParagraph corpus (`preferWebParagraph` + `forceTestFonts` vs
`origin/master` `2e53c3af41a`)**:
- Part 1 (`web_ui` `test/webparagraph/*` + `test/ui/*`): **+6 new tests
passing (`207` vs `201`), 0 regressions**.
- Part 2 (`packages/flutter`): **+2 tests fixed
(`test/painting/text_painter_test.dart: TextHeightBehavior with strut on
empty paragraph` and `test/painting/text_painter_test.dart: TextPainter
widget span`), 0 regressions**.
## Pre-launch Checklist
- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [AI contribution guidelines] and understand my
responsibilities, or I am not using AI tools.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant in-code documentation (doc comments with
`///`).
- [x] If this PR introduces a new feature or capability, I created and
linked a website documentation issue or PR in [flutter/website] (or
verified none is needed).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.
<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[AI contribution guidelines]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[flutter/website]: https://github.com/flutter/website
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
---------
Co-authored-by: Mouad Debbar <[email protected]>1 parent 4c6f72f commit f1586fc
4 files changed
Lines changed: 326 additions & 44 deletions
File tree
- engine/src/flutter/lib/web_ui
- lib/src/engine/web_paragraph
- test/webparagraph
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
202 | 202 | | |
203 | 203 | | |
204 | 204 | | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | 205 | | |
210 | 206 | | |
211 | 207 | | |
| |||
214 | 210 | | |
215 | 211 | | |
216 | 212 | | |
| 213 | + | |
217 | 214 | | |
218 | 215 | | |
219 | 216 | | |
| |||
277 | 274 | | |
278 | 275 | | |
279 | 276 | | |
| 277 | + | |
| 278 | + | |
280 | 279 | | |
281 | | - | |
282 | | - | |
283 | | - | |
284 | | - | |
| 280 | + | |
285 | 281 | | |
286 | | - | |
287 | | - | |
288 | | - | |
289 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
290 | 289 | | |
291 | 290 | | |
292 | 291 | | |
| |||
481 | 480 | | |
482 | 481 | | |
483 | 482 | | |
484 | | - | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
485 | 513 | | |
486 | 514 | | |
487 | 515 | | |
488 | 516 | | |
489 | 517 | | |
490 | | - | |
491 | | - | |
492 | | - | |
493 | | - | |
494 | | - | |
495 | | - | |
496 | | - | |
497 | | - | |
498 | | - | |
| 518 | + | |
499 | 519 | | |
| 520 | + | |
500 | 521 | | |
501 | | - | |
502 | 522 | | |
503 | 523 | | |
504 | 524 | | |
| |||
1399 | 1419 | | |
1400 | 1420 | | |
1401 | 1421 | | |
| 1422 | + | |
| 1423 | + | |
1402 | 1424 | | |
1403 | 1425 | | |
1404 | 1426 | | |
| |||
1496 | 1518 | | |
1497 | 1519 | | |
1498 | 1520 | | |
| 1521 | + | |
| 1522 | + | |
| 1523 | + | |
| 1524 | + | |
| 1525 | + | |
| 1526 | + | |
| 1527 | + | |
| 1528 | + | |
| 1529 | + | |
| 1530 | + | |
| 1531 | + | |
| 1532 | + | |
| 1533 | + | |
| 1534 | + | |
| 1535 | + | |
| 1536 | + | |
1499 | 1537 | | |
1500 | 1538 | | |
1501 | 1539 | | |
| |||
1510 | 1548 | | |
1511 | 1549 | | |
1512 | 1550 | | |
| 1551 | + | |
| 1552 | + | |
| 1553 | + | |
| 1554 | + | |
| 1555 | + | |
| 1556 | + | |
| 1557 | + | |
| 1558 | + | |
| 1559 | + | |
| 1560 | + | |
| 1561 | + | |
| 1562 | + | |
| 1563 | + | |
| 1564 | + | |
| 1565 | + | |
| 1566 | + | |
| 1567 | + | |
| 1568 | + | |
| 1569 | + | |
| 1570 | + | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
| 1575 | + | |
| 1576 | + | |
| 1577 | + | |
| 1578 | + | |
| 1579 | + | |
| 1580 | + | |
| 1581 | + | |
| 1582 | + | |
| 1583 | + | |
| 1584 | + | |
| 1585 | + | |
| 1586 | + | |
| 1587 | + | |
1513 | 1588 | | |
1514 | | - | |
1515 | | - | |
| 1589 | + | |
1516 | 1590 | | |
1517 | | - | |
1518 | | - | |
1519 | | - | |
1520 | 1591 | | |
1521 | 1592 | | |
1522 | 1593 | | |
1523 | 1594 | | |
1524 | | - | |
1525 | | - | |
1526 | | - | |
1527 | | - | |
1528 | | - | |
1529 | | - | |
1530 | | - | |
1531 | 1595 | | |
1532 | 1596 | | |
| 1597 | + | |
| 1598 | + | |
| 1599 | + | |
| 1600 | + | |
| 1601 | + | |
| 1602 | + | |
| 1603 | + | |
| 1604 | + | |
| 1605 | + | |
| 1606 | + | |
| 1607 | + | |
| 1608 | + | |
| 1609 | + | |
1533 | 1610 | | |
1534 | | - | |
1535 | | - | |
| 1611 | + | |
| 1612 | + | |
| 1613 | + | |
| 1614 | + | |
1536 | 1615 | | |
1537 | 1616 | | |
1538 | | - | |
1539 | | - | |
| 1617 | + | |
| 1618 | + | |
| 1619 | + | |
| 1620 | + | |
1540 | 1621 | | |
1541 | 1622 | | |
1542 | 1623 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
231 | 231 | | |
232 | 232 | | |
233 | 233 | | |
234 | | - | |
| 234 | + | |
| 235 | + | |
235 | 236 | | |
236 | | - | |
237 | | - | |
238 | | - | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
239 | 246 | | |
240 | 247 | | |
241 | 248 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
365 | 365 | | |
366 | 366 | | |
367 | 367 | | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
368 | 375 | | |
369 | 376 | | |
370 | 377 | | |
371 | 378 | | |
372 | 379 | | |
373 | 380 | | |
| 381 | + | |
374 | 382 | | |
375 | 383 | | |
376 | 384 | | |
| |||
385 | 393 | | |
386 | 394 | | |
387 | 395 | | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
388 | 399 | | |
389 | 400 | | |
390 | 401 | | |
| |||
0 commit comments