Skip to content

Commit ca96b7a

Browse files
RusinosuryaKommana2662
authored andcommitted
[web_paragraph] Fix placeholder positioning and BoxHeightStyle.tight boxes (flutter#193734)
Part of flutter#193731. Split out of flutter#193403 (second of four independent PRs; the others are flutter#193733 and flutter#193736, and flutter#193403 itself will be rebased on top once these land). ### Problem - **Placeholder vertical position was computed too early.** `PlaceholderBlock.calculatePlaceholderTop` set `block.advance` from the line ascent *as it was at that moment* before folding `block.ascent` into the line. A placeholder taller than the preceding text (or a later, taller placeholder on the same line) grew `line.fontBoundingBoxAscent` afterwards in `line.updateBoundingBox(block)`, leaving placeholder `advance.top` positioned against a stale ascent. `getBoxesForPlaceholders` returned those stale rects, so a placeholder box could sit above the line's top (negative `top`) and disagree with `getBoxesForRange` for the same range. - **`BoxHeightStyle.tight` returned the height-scaled box for text.** `getBoxesForRange` used `multipliedFontBoundingBoxAscent` / `multipliedHeight`, i.e. the box included the extra leading added by `TextStyle.height`. SkParagraph's `TextLine::getRectsForRange` returns the unscaled glyph bounds for `tight` (`Rect::MakeXYWH(..., -run.ascent(), ..., run.descent())` relative to the baseline). The same code path was also applied to `PlaceholderBlock`, whose `advance` is already line-relative, producing a double-shifted box. ### Fix - `PlaceholderBlock` & `addLine`: split placeholder layout into two passes: - Pass 1 calls `block.calculatePlaceholderMetrics(line.fontBoundingBoxAscent, line.fontBoundingBoxDescent)` to compute each placeholder's `ascent`/`descent` and folds them into the line metrics via `line.updateBoundingBox(block)`. - Pass 2, after `line.fontBoundingBoxAscent` is final, calls `block.calculatePlaceholderTop(line.fontBoundingBoxAscent)` to compute `top = lineAscent - ascent` and set `block.advance = Rect.fromLTWH(spanShiftFromLineStart, top, span.width, span.height)`. - `getBoxesForRange`, `BoxHeightStyle.tight`: - `TextBlock`: `top = line.advance.top + line.fontBoundingBoxAscent - block.rawFontBoundingBoxAscent`, `height = rawFontBoundingBoxAscent + rawFontBoundingBoxDescent` (unscaled glyph bounds positioned at the baseline, matching SkParagraph). - `PlaceholderBlock`: `top = firstRect.top + line.advance.top`, `height = span.height` — `block.advance` is already relative to the line, so no extra ascent shift. - Removed the unused vertical `.translate(...)` on `firstRect` for `TextBlock`, since `firstRect` only provides `left` and `width` while `top` and `bottom` are computed per `BoxHeightStyle`. ### Tests - `paragraph_get_boxes_test.dart`: - `getBoxesForRange and getBoxesForPlaceholders return matching boxes for placeholders` — two placeholders on one line where the second is taller; checks both APIs agree, `top >= 0`, and the box sizes equal the placeholder sizes. On `master`: `getBoxesForPlaceholders` returns `(0, -12, 50, 18)` for the first placeholder while `getBoxesForRange(0, 1)` returns `(0, 14, 50, 44)`. - `BoxHeightStyle.tight uses unscaled glyph height and respects TextHeightBehavior` — `height: 3.0` with `even` leading; `tight` box height equals the unscaled line height and is centered in the 60px line. On `master` the `tight` box is 60px tall instead of 22px. - Existing `paragraph_get_boxes_test.dart` and `paragraph_placeholders_test.dart` pass. - **Full WebParagraph corpus (`--full`, `preferWebParagraph` + `forceTestFonts` vs `origin/master` `b7315d76579e`)**: - Part 1 (`web_ui`): `16/16` `webparagraph/*` (`+198 ~5 -1`, **+2** new tests passing) + `60/60` `test/ui/*` (`+1354 ~5 -41`). - Part 2 (`packages/flutter`, `894/894` suites across `8/8` shards): `+16712 ~1237 -459` vs baseline `+16710 ~1237 -461` (**+3** existing tests fixed; **1** apparent golden diff in `test/cupertino/text_field_test.dart: Overflow clipBehavior none golden` where on `origin/master` two bugs cancelled out — missing `StrutStyle(forceStrutHeight: true)` from flutter#193774 inflated the line ascent by 83px while the old single-pass `calculatePlaceholderTop` shifted the placeholder by `-83px` detached from its `'Hi'` text; resolved once flutter#193774 lands). - Jointly with flutter#193736 + flutter#193773 also unlocks `test/painting/text_painter_test.dart: TextPainter line-height half-leading with leading trim`. <details> <summary>Fixed corpus tests vs <code>origin/master</code> (3)</summary> - `packages/flutter` :: `test/painting/text_painter_test.dart: TextPainter line-height TextLeadingDistribution falls back to paragraph style` - `packages/flutter` :: `test/painting/text_painter_test.dart: TextPainter line-height half-leading` - `packages/flutter` :: `test/painting/text_painter_test.dart: TextPainter widget span` </details> ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [ ] 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
1 parent c60cd6c commit ca96b7a

2 files changed

Lines changed: 101 additions & 12 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/web_paragraph/layout.dart‎

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,17 @@ class TextLayout {
477477
if (block is! PlaceholderBlock) {
478478
continue;
479479
}
480-
block.calculatePlaceholderTop(line.fontBoundingBoxAscent, line.fontBoundingBoxDescent);
480+
block.calculatePlaceholderMetrics(line.fontBoundingBoxAscent, line.fontBoundingBoxDescent);
481481
line.updateBoundingBox(block);
482482
}
483483

484+
// Position each placeholder only after all placeholders have folded their ascent into the line
485+
for (final LineBlock block in line.visualBlocks) {
486+
if (block is PlaceholderBlock) {
487+
block.calculatePlaceholderTop(line.fontBoundingBoxAscent);
488+
}
489+
}
490+
484491
line.advance = ui.Rect.fromLTWH(
485492
0,
486493
top,
@@ -601,21 +608,25 @@ class TextLayout {
601608
ui.Rect firstRect = block.advance;
602609
if (block is! PlaceholderBlock) {
603610
// We need to calculate the intersect rectangle
604-
firstRect = (block.span as TextSpan)
605-
.getTextRangeSelectionInBlock(block, intersect)
606-
.translate(0, block.multipliedFontBoundingBoxAscent);
611+
firstRect = (block.span as TextSpan).getTextRangeSelectionInBlock(block, intersect);
607612
}
608613
// Now we need to recalculate the rects
609614
double left, right, top, bottom;
610615
switch (boxHeightStyle) {
611616
case ui.BoxHeightStyle.tight:
612-
top =
613-
firstRect.top +
614-
line.advance.top +
615-
line.fontBoundingBoxAscent -
616-
block.multipliedFontBoundingBoxAscent;
617-
bottom = top + block.multipliedHeight;
618-
assert((block.multipliedHeight - (bottom - top).abs() < epsilon));
617+
if (block is PlaceholderBlock) {
618+
// For PlaceholderBlock, `firstRect` is `block.advance`, which already has
619+
// `top = line.fontBoundingBoxAscent - block.ascent` relative to the line
620+
top = firstRect.top + line.advance.top;
621+
bottom = top + block.span.height;
622+
} else {
623+
// For TextBlock, BoxHeightStyle.tight returns the unscaled glyph bounds
624+
// positioned relative to the line's baseline
625+
// (`line.advance.top + line.fontBoundingBoxAscent`), matching SkParagraph's
626+
// TextLine::getRectsForRange
627+
top = line.advance.top + line.fontBoundingBoxAscent - block.rawFontBoundingBoxAscent;
628+
bottom = top + block.rawFontBoundingBoxAscent + block.rawFontBoundingBoxDescent;
629+
}
619630
case ui.BoxHeightStyle.max:
620631
top = line.advance.top;
621632
bottom = line.advance.bottom;
@@ -1345,7 +1356,7 @@ class PlaceholderBlock extends LineBlock {
13451356
@override
13461357
final double spanShiftFromLineStart;
13471358

1348-
void calculatePlaceholderTop(double lineAscent, double lineDescent) {
1359+
void calculatePlaceholderMetrics(double lineAscent, double lineDescent) {
13491360
double baselineAdjustment = 0;
13501361
if (span.baseline == ui.TextBaseline.ideographic) {
13511362
baselineAdjustment = lineDescent / 2;
@@ -1386,6 +1397,9 @@ class PlaceholderBlock extends LineBlock {
13861397
ascent = lineAscent - diff;
13871398
descent = lineDescent - diff;
13881399
}
1400+
}
1401+
1402+
void calculatePlaceholderTop(double lineAscent) {
13891403
final double top = lineAscent - ascent;
13901404
// The advance needs to be calculated relative to the line. In order to do that, we need to start
13911405
// from the span's own advance within the line.

‎engine/src/flutter/lib/web_ui/test/webparagraph/paragraph_get_boxes_test.dart‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,81 @@ Future<void> testMain() async {
302302
expect(boxes1.first.toRect().bottom >= boxes2.first.toRect().top, true);
303303
});
304304

305+
test('getBoxesForRange and getBoxesForPlaceholders return matching boxes for placeholders', () {
306+
final paragraphStyle = ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20);
307+
final builder = ui.ParagraphBuilder(paragraphStyle);
308+
builder.addPlaceholder(
309+
50,
310+
30,
311+
ui.PlaceholderAlignment.baseline,
312+
baseline: ui.TextBaseline.alphabetic,
313+
);
314+
builder.addText('Hello ');
315+
// A second taller placeholder on the same line increases `line.fontBoundingBoxAscent`
316+
// after the first placeholder's initial pass
317+
builder.addPlaceholder(
318+
40,
319+
60,
320+
ui.PlaceholderAlignment.bottom,
321+
baseline: ui.TextBaseline.alphabetic,
322+
);
323+
final ui.Paragraph paragraph = builder.build();
324+
paragraph.layout(const ui.ParagraphConstraints(width: 500));
325+
326+
final List<ui.TextBox> placeholderBoxes = paragraph.getBoxesForPlaceholders();
327+
expect(placeholderBoxes, hasLength(2));
328+
329+
// Placeholder 1 is at text index 0..1; Placeholder 2 is at text index 7..8
330+
final List<ui.TextBox> rangeBoxes1 = paragraph.getBoxesForRange(0, 1);
331+
final List<ui.TextBox> rangeBoxes2 = paragraph.getBoxesForRange(7, 8);
332+
expect(rangeBoxes1, hasLength(1));
333+
expect(rangeBoxes2, hasLength(1));
334+
335+
expect(rangeBoxes1.first.toRect(), placeholderBoxes[0].toRect());
336+
expect(rangeBoxes2.first.toRect(), placeholderBoxes[1].toRect());
337+
// Placeholder 1 sits on the baseline, placeholder 2 on the line bottom.
338+
expect(rangeBoxes1.first.bottom, closeTo(paragraph.alphabeticBaseline, 1e-3));
339+
expect(rangeBoxes1.first.toRect().width, 50.0);
340+
expect(rangeBoxes1.first.toRect().height, 30.0);
341+
expect(rangeBoxes2.first.bottom, closeTo(paragraph.height, 1e-3));
342+
expect(rangeBoxes2.first.toRect().width, 40.0);
343+
expect(rangeBoxes2.first.toRect().height, 60.0);
344+
});
345+
346+
test('BoxHeightStyle.tight uses unscaled glyph height and respects TextHeightBehavior', () {
347+
final builderUnscaled = ui.ParagraphBuilder(
348+
ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20),
349+
)..addText('Hello');
350+
final ui.Paragraph paragraphUnscaled = builderUnscaled.build()
351+
..layout(const ui.ParagraphConstraints(width: 500));
352+
final ui.LineMetrics unscaledMetrics = paragraphUnscaled.computeLineMetrics().single;
353+
final double rawGlyphHeight = unscaledMetrics.ascent + unscaledMetrics.descent;
354+
355+
final builderEven =
356+
ui.ParagraphBuilder(
357+
ui.ParagraphStyle(
358+
fontFamily: 'Arial',
359+
fontSize: 20,
360+
height: 3.0,
361+
textHeightBehavior: const ui.TextHeightBehavior(
362+
leadingDistribution: ui.TextLeadingDistribution.even,
363+
),
364+
),
365+
)
366+
..pushStyle(ui.TextStyle())
367+
..addText('Hello');
368+
final ui.Paragraph paragraphEven = builderEven.build()
369+
..layout(const ui.ParagraphConstraints(width: 500));
370+
371+
// Line height is scaled to 3.0 * 20 = 60.0, with extra leading split evenly above and below.
372+
// BoxHeightStyle.tight should return the unscaled glyph height centered vertically in the line
373+
expect(paragraphEven.height, 60.0);
374+
final List<ui.TextBox> tightBoxes = paragraphEven.getBoxesForRange(0, 5);
375+
expect(tightBoxes, hasLength(1));
376+
expect(tightBoxes.single.toRect().height, closeTo(rawGlyphHeight, 1e-3));
377+
expect(tightBoxes.single.top, closeTo((60.0 - rawGlyphHeight) / 2, 1e-3));
378+
});
379+
305380
test('layout with kTextHeightNone disables parent height multiplier', () {
306381
// Pushing a child TextStyle with kTextHeightNone overrides the parent ParagraphStyle's
307382
// height multiplier (3.0) and lays out the line at its unscaled font height.

0 commit comments

Comments
 (0)