Skip to content

Commit 5c7e45a

Browse files
Rusinomdebbar
andauthored
[web_paragraph] Honor TextHeightBehavior.leadingDistribution and default to proportional (flutter#193736)
Part of flutter#193731. Split out of flutter#193403 (third of four independent PRs; the others are flutter#193733 and flutter#193734, and flutter#193403 is stacked on top of this PR). ### Problem - **`TextHeightBehavior.leadingDistribution` was ignored.** `WebParagraphStyle` never passed `textHeightBehavior.leadingDistribution` to the root `WebTextStyle`, and `RootStyleNode._leadingDistribution` always returned `null`, so the value could not reach `LineBlock`. Only an explicit `TextStyle(leadingDistribution: …)` had any effect. - **The default leading distribution was `even`.** `LineBlock` treated `null` the same as `TextLeadingDistribution.even` (half-leading). SkParagraph (and therefore the native engine and CanvasKit) default to `proportional`: `TextStyle::fHalfLeading` is `false` unless `leadingDistribution == even` is set, and `dart:ui`'s `TextHeightBehavior.leadingDistribution` defaults to `TextLeadingDistribution.proportional`. - **Negative half-leading clamped `fontBoundingBoxDescent` to `0.0`.** `TextLine` initialized `fontBoundingBoxAscent` and `fontBoundingBoxDescent` to `0.0` and accumulated every block with `math.max(0.0, ...)`. With `TextLeadingDistribution.even` and a small `height` multiplier (where `height * fontSize < rawAscent + rawDescent`), `extraLeading / 2` is negative and `multipliedFontBoundingBoxDescent` can become negative; `math.max(0.0, ...)` clamped it to `0.0`, inflating the line height above `height * fontSize`. ### Fix - `WebParagraphStyle` propagates `textHeightBehavior?.leadingDistribution` into the root `WebTextStyle`; `RootStyleNode._leadingDistribution` returns `style.leadingDistribution`. Child `TextStyle`s still override it through `ChildStyleNode` as before. - `LineBlock`: `null` now falls through to the `proportional` branch (ascent and descent are scaled by `height * fontSize / (ascent + descent)`), `even` keeps the half-leading behavior. - `TextLine.updateBoundingBox`: track `_hasLineMetrics` so the first block on the line initializes `fontBoundingBoxAscent` and `fontBoundingBoxDescent` directly (preserving a negative `descent`), while subsequent blocks take `math.max`. ### Behavior change For any paragraph with a `height` multiplier and no explicit `leadingDistribution`, the **line height is unchanged** but the **baseline moves**: the extra leading is now split in proportion to the font's ascent/descent instead of 50/50. This brings WebParagraph in line with CanvasKit/Skwasm output for the same `ParagraphStyle`; WebParagraph is not the default renderer yet, so no app-facing change on stable. ### Tests - New `paragraph_line_height_test.dart`: - `Height multiplier scales ascent and descent proportionally by default` — checks the scaled line keeps `height * fontSize`, that ascent/descent scale by the same factor by default, that `TextHeightBehavior(leadingDistribution: even)` adds equal leading on both sides, and that the two differ. Fails on `master` (default was `even` and the THB value was ignored). - `Half-leading with a small height multiplier preserves negative descent` — checks that `height: 0.1` with `TextLeadingDistribution.even` keeps a negative `descent` and total height `0.1 * fontSize`. - Existing `webparagraph/*` and `ui/paragraph_builder_test.dart` suites pass. - **WebParagraph corpus (`preferWebParagraph` + `forceTestFonts` vs `origin/master` `b7315d76579e`)**: **0 fixed standalone, +2 new tests passing, 0 regressions** (base of the flutter#193736 -> flutter#193773 -> flutter#193774 -> flutter#193403 stack that fixes **34** corpus tests; framework `TextPainter line-height` tests in `test/painting/text_painter_test.dart` also call `getBoxesForSelection` with `BoxHeightStyle.tight` and are unlocked when combined with flutter#193734). ## 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 --------- Co-authored-by: Mouad Debbar <[email protected]>
1 parent 4d8c334 commit 5c7e45a

3 files changed

Lines changed: 145 additions & 12 deletions

File tree

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,12 +1205,14 @@ abstract class LineBlock {
12051205
final double runHeight = span.style.height! * fontSize;
12061206
final double fontHeight = span.fontBoundingBoxAscent + span.fontBoundingBoxDescent;
12071207
switch (span.style.leadingDistribution) {
1208-
case null:
12091208
case ui.TextLeadingDistribution.even:
1209+
// Split extra leading equally above ascent and below descent (half-leading).
12101210
final double extraLeading = (runHeight - fontHeight) / 2;
12111211
_multipliedFontBoundingBoxAscent = span.fontBoundingBoxAscent + extraLeading;
12121212
_multipliedFontBoundingBoxDescent = span.fontBoundingBoxDescent + extraLeading;
1213+
case null:
12131214
case ui.TextLeadingDistribution.proportional:
1215+
// Default to proportional scaling of ascent and descent, matching SkParagraph.
12141216
final double multiplier = fontHeight == 0 ? 1.0 : runHeight / fontHeight;
12151217
_multipliedFontBoundingBoxAscent = span.fontBoundingBoxAscent * multiplier;
12161218
_multipliedFontBoundingBoxDescent = span.fontBoundingBoxDescent * multiplier;
@@ -1480,6 +1482,7 @@ class TextLine {
14801482
ui.Rect advance = ui.Rect.zero;
14811483
double fontBoundingBoxAscent = 0.0;
14821484
double fontBoundingBoxDescent = 0.0;
1485+
bool _hasLineMetrics = false;
14831486

14841487
double paintBoundsAscent = 0.0;
14851488
double paintBoundsDescent = 0.0;
@@ -1494,28 +1497,33 @@ class TextLine {
14941497
List<LineBlock> visualBlocks = <LineBlock>[];
14951498

14961499
void updateBoundingBox(LineBlock block) {
1500+
final double blockAscent;
1501+
final double blockDescent;
14971502
if (block is TextBlock) {
14981503
// Line always counts multiplied metrics.
1499-
fontBoundingBoxAscent = math.max(
1500-
fontBoundingBoxAscent,
1501-
block.multipliedFontBoundingBoxAscent,
1502-
);
1503-
fontBoundingBoxDescent = math.max(
1504-
fontBoundingBoxDescent,
1505-
block.multipliedFontBoundingBoxDescent,
1506-
);
1504+
blockAscent = block.multipliedFontBoundingBoxAscent;
1505+
blockDescent = block.multipliedFontBoundingBoxDescent;
15071506
paintBoundsAscent = math.max(paintBoundsAscent, block.paintBoundsAscent);
15081507
paintBoundsDescent = math.max(paintBoundsDescent, block.paintBoundsDescent);
15091508
paintBoundsLeft = math.min(paintBoundsLeft, block.paintBounds.left);
15101509
paintBoundsRight = math.max(paintBoundsRight, block.paintBounds.right);
15111510
} else if (block is PlaceholderBlock) {
1512-
fontBoundingBoxAscent = math.max(fontBoundingBoxAscent, block.ascent);
1513-
fontBoundingBoxDescent = math.max(fontBoundingBoxDescent, block.descent);
1511+
blockAscent = block.ascent;
1512+
blockDescent = block.descent;
15141513
// There's no need to update paint bounds because placeholders aren't painted by the
15151514
// paragraph.
15161515
} else {
15171516
throw UnsupportedError('Unknown block type: $block');
15181517
}
1518+
1519+
if (!_hasLineMetrics) {
1520+
fontBoundingBoxAscent = blockAscent;
1521+
fontBoundingBoxDescent = blockDescent;
1522+
_hasLineMetrics = true;
1523+
} else {
1524+
fontBoundingBoxAscent = math.max(fontBoundingBoxAscent, blockAscent);
1525+
fontBoundingBoxDescent = math.max(fontBoundingBoxDescent, blockDescent);
1526+
}
15191527
}
15201528
}
15211529

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ class WebParagraphStyle implements ui.ParagraphStyle {
4444
fontStyle: fontStyle,
4545
fontWeight: fontWeight,
4646
height: height,
47+
// Propagate the paragraph's default leadingDistribution to the root text style
48+
// so spans and lines inherit it unless overridden by a child TextStyle.
49+
leadingDistribution: textHeightBehavior?.leadingDistribution,
4750
locale: locale,
4851
color: color,
4952
),
@@ -1591,8 +1594,9 @@ class RootStyleNode extends StyleNode {
15911594
@override
15921595
double? get _height => style.height;
15931596

1597+
// Inherit the default leadingDistribution from ParagraphStyle.textHeightBehavior.
15941598
@override
1595-
ui.TextLeadingDistribution? get _leadingDistribution => null;
1599+
ui.TextLeadingDistribution? get _leadingDistribution => style.leadingDistribution;
15961600

15971601
@override
15981602
ui.Locale? get _locale => style.locale;
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:test/bootstrap/browser.dart';
6+
import 'package:test/test.dart';
7+
import 'package:ui/ui.dart' as ui;
8+
9+
import '../common/test_initialization.dart';
10+
11+
void main() {
12+
internalBootstrapBrowserTest(() => testMain);
13+
}
14+
15+
const double _fontSize = 20.0;
16+
const double _heightMultiplier = 3.0;
17+
// The line height produced by `_heightMultiplier` at `_fontSize`.
18+
const double _scaledHeight = _heightMultiplier * _fontSize;
19+
20+
ui.ParagraphStyle _style({
21+
double fontSize = _fontSize,
22+
double? height,
23+
ui.TextHeightBehavior? textHeightBehavior,
24+
ui.StrutStyle? strutStyle,
25+
}) {
26+
return ui.ParagraphStyle(
27+
fontFamily: 'Arial',
28+
fontSize: fontSize,
29+
height: height,
30+
textHeightBehavior: textHeightBehavior,
31+
strutStyle: strutStyle,
32+
);
33+
}
34+
35+
/// Lays out [text] (or an empty paragraph when [text] is null) with [style].
36+
ui.Paragraph _layout(ui.ParagraphStyle style, [String? text]) {
37+
final builder = ui.ParagraphBuilder(style);
38+
if (text != null) {
39+
builder.addText(text);
40+
}
41+
return builder.build()..layout(const ui.ParagraphConstraints(width: 500));
42+
}
43+
44+
ui.LineMetrics _singleLineMetrics(ui.ParagraphStyle style, String text) {
45+
return _layout(style, text).computeLineMetrics().single;
46+
}
47+
48+
Future<void> testMain() async {
49+
setUpUnitTests();
50+
51+
test('Height multiplier scales ascent and descent proportionally by default', () {
52+
final ui.LineMetrics raw = _singleLineMetrics(_style(), 'Hello');
53+
final ui.LineMetrics scaled = _singleLineMetrics(_style(height: _heightMultiplier), 'Hello');
54+
expect(scaled.height, closeTo(_scaledHeight, 1e-3));
55+
56+
// `proportional` (the default, as in SkParagraph) scales both sides by the same factor...
57+
final double factor = _scaledHeight / raw.height;
58+
expect(scaled.ascent, closeTo(raw.ascent * factor, 1e-3));
59+
expect(scaled.descent, closeTo(raw.descent * factor, 1e-3));
60+
61+
// ...whereas `even` adds the same amount of extra leading on each side.
62+
final ui.LineMetrics even = _singleLineMetrics(
63+
_style(
64+
height: _heightMultiplier,
65+
textHeightBehavior: const ui.TextHeightBehavior(
66+
leadingDistribution: ui.TextLeadingDistribution.even,
67+
),
68+
),
69+
'Hello',
70+
);
71+
expect(even.height, closeTo(_scaledHeight, 1e-3));
72+
final double extraLeading = (_scaledHeight - raw.height) / 2;
73+
expect(even.ascent, closeTo(raw.ascent + extraLeading, 1e-3));
74+
expect(even.descent, closeTo(raw.descent + extraLeading, 1e-3));
75+
76+
// The two distributions differ for any font whose ascent and descent are not equal.
77+
expect(scaled.ascent, isNot(closeTo(even.ascent, 1e-3)));
78+
});
79+
80+
test('TextStyle.leadingDistribution overrides TextHeightBehavior.leadingDistribution', () {
81+
final ui.LineMetrics proportional = _singleLineMetrics(
82+
_style(height: _heightMultiplier),
83+
'Hello',
84+
);
85+
final builder = ui.ParagraphBuilder(
86+
_style(
87+
height: _heightMultiplier,
88+
textHeightBehavior: const ui.TextHeightBehavior(
89+
leadingDistribution: ui.TextLeadingDistribution.even,
90+
),
91+
),
92+
);
93+
builder.pushStyle(ui.TextStyle(leadingDistribution: ui.TextLeadingDistribution.proportional));
94+
builder.addText('Hello');
95+
final ui.Paragraph paragraph = builder.build()
96+
..layout(const ui.ParagraphConstraints(width: 500));
97+
final ui.LineMetrics overridden = paragraph.computeLineMetrics().single;
98+
expect(overridden.ascent, closeTo(proportional.ascent, 1e-3));
99+
expect(overridden.descent, closeTo(proportional.descent, 1e-3));
100+
});
101+
102+
test('Half-leading with a small height multiplier preserves negative descent', () {
103+
final ui.LineMetrics raw = _singleLineMetrics(_style(), 'Hello');
104+
const smallMultiplier = 0.1;
105+
const double expectedHeight = smallMultiplier * _fontSize;
106+
final ui.LineMetrics even = _singleLineMetrics(
107+
_style(
108+
height: smallMultiplier,
109+
textHeightBehavior: const ui.TextHeightBehavior(
110+
leadingDistribution: ui.TextLeadingDistribution.even,
111+
),
112+
),
113+
'Hello',
114+
);
115+
final double extraLeading = (expectedHeight - raw.height) / 2;
116+
expect(even.ascent, closeTo(raw.ascent + extraLeading, 1e-3));
117+
expect(even.descent, closeTo(raw.descent + extraLeading, 1e-3));
118+
expect(even.descent, isNegative);
119+
expect(even.height, closeTo(expectedHeight, 1e-3));
120+
});
121+
}

0 commit comments

Comments
 (0)