Skip to content

Commit 988d755

Browse files
RusinosuryaKommana2662
authored andcommitted
[web_paragraph] Honor kTextHeightNone in TextStyle and ParagraphStyle (flutter#193733)
Part of flutter#193731. Split out of flutter#193403 (first of four independent PRs; the others are flutter#193734 and flutter#193736, and flutter#193403 itself will be rebased on top once these land). ### Problem - `ChildStyleNode._height` converted `style.height == ui.kTextHeightNone` to `null` *before* falling back to `parent._height`, so a child `TextStyle(height: ui.kTextHeightNone)` could never override a non-null parent height (`ParagraphStyle(height: 3.0)` + `pushStyle(TextStyle(height: kTextHeightNone))` still laid out the line at 3x). - `LineBlock` only checked `span.style.height == null`, so `ParagraphStyle(height: ui.kTextHeightNone)` (`0.0`) multiplied the line ascent/descent by `0.0` instead of disabling the height multiplier. ### Fix - `ChildStyleNode._height` keeps `style.height` as-is: `style.height ?? parent._height`. `kTextHeightNone` now reaches `LineBlock`, which is the one place that interprets it. - `LineBlock`: `height == null || height == ui.kTextHeightNone` → raw font ascent/descent, no scaling (same as the native path, where `paragraph_builder.cc` sets `has_height_override = style.height != kTextHeightNone`). - `LineBlock` uses `StyleManager.defaultFontSize` instead of a hardcoded `14.0`. - `WebStrutStyle` already normalizes `kTextHeightNone` to `null` in its constructor, so struts need no change. ### Tests - `paragraph_get_boxes_test.dart`: `layout with kTextHeightNone disables parent height multiplier`, `layout with ParagraphStyle(kTextHeightNone)`. - **WebParagraph corpus (`preferWebParagraph` + `forceTestFonts` vs `origin/master` `b7315d76579e`)**: 20,012 tests (18,765 executed, 1,247 skipped) — **503 -> 503 failures (0 fixed standalone, +2 new tests passing, 0 regressions)**. - *Note:* Existing corpus tests for `kTextHeightNone` also depend on other issues (`test/ui/paragraph_builder_test.dart: kTextHeightNone StrutStyle` is unlocked in flutter#193774 once `StrutStyle` is applied; `test/painting/text_painter_test.dart: kTextHeightNone unsets the text height multiplier` still fails due to Chromium's integer-rounded `FlutterTest` font metrics at `10px` where `ascent + descent = 11 != 10`). ## 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 8ee3e3d commit 988d755

3 files changed

Lines changed: 52 additions & 5 deletions

File tree

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:ui/ui.dart' as ui;
99

1010
import '../canvaskit/canvaskit_api.dart';
1111
import '../dom.dart';
12+
import '../view_embedder/style_manager.dart';
1213
import 'bidi.dart';
1314
import 'code_unit_flags.dart';
1415
import 'debug.dart';
@@ -1193,12 +1194,14 @@ abstract class LineBlock {
11931194
this.textRange,
11941195
this.shiftFromLineStart,
11951196
) {
1196-
if (span.style.height == null) {
1197+
// When height is null or kTextHeightNone, disable the height multiplier
1198+
// and use the font's raw ascent and descent directly.
1199+
if (span.style.height == null || span.style.height == ui.kTextHeightNone) {
11971200
_multipliedFontBoundingBoxAscent = span.fontBoundingBoxAscent;
11981201
_multipliedFontBoundingBoxDescent = span.fontBoundingBoxDescent;
11991202
return;
12001203
}
1201-
final double fontSize = span.style.fontSize ?? 14.0;
1204+
final double fontSize = span.style.fontSize ?? StyleManager.defaultFontSize;
12021205
final double runHeight = span.style.height! * fontSize;
12031206
final double fontHeight = span.fontBoundingBoxAscent + span.fontBoundingBoxDescent;
12041207
switch (span.style.leadingDistribution) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,10 +1504,11 @@ class ChildStyleNode extends StyleNode {
15041504
@override
15051505
double? get _wordSpacing => style.wordSpacing ?? parent._wordSpacing;
15061506

1507+
// Preserve `style.height` as-is (including `ui.kTextHeightNone`) so a child
1508+
// span can explicitly override a non-null parent height and disable the
1509+
// height multiplier during layout.
15071510
@override
1508-
double? get _height {
1509-
return style.height == ui.kTextHeightNone ? null : (style.height ?? parent._height);
1510-
}
1511+
double? get _height => style.height ?? parent._height;
15111512

15121513
@override
15131514
ui.TextLeadingDistribution? get _leadingDistribution =>

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,4 +301,47 @@ Future<void> testMain() async {
301301
expect(boxes2.isNotEmpty && boxes2.length == 1, true);
302302
expect(boxes1.first.toRect().bottom >= boxes2.first.toRect().top, true);
303303
});
304+
305+
test('layout with kTextHeightNone disables parent height multiplier', () {
306+
// Pushing a child TextStyle with kTextHeightNone overrides the parent ParagraphStyle's
307+
// height multiplier (3.0) and lays out the line at its unscaled font height.
308+
final builderWithNone = ui.ParagraphBuilder(
309+
ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20, height: 3.0),
310+
);
311+
builderWithNone.pushStyle(ui.TextStyle(height: ui.kTextHeightNone));
312+
builderWithNone.addText('Hello');
313+
final ui.Paragraph paragraphWithNone = builderWithNone.build()
314+
..layout(const ui.ParagraphConstraints(width: 500));
315+
316+
final builderDefault = ui.ParagraphBuilder(
317+
ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20),
318+
);
319+
builderDefault.addText('Hello');
320+
final ui.Paragraph paragraphDefault = builderDefault.build()
321+
..layout(const ui.ParagraphConstraints(width: 500));
322+
323+
expect(paragraphWithNone.height, greaterThan(0));
324+
expect(paragraphWithNone.height, paragraphDefault.height);
325+
});
326+
327+
test('layout with ParagraphStyle(kTextHeightNone)', () {
328+
// Passing kTextHeightNone directly on ParagraphStyle produces the same unscaled
329+
// line height as omitting height (null), rather than multiplying by 0.0.
330+
final builderWithNone = ui.ParagraphBuilder(
331+
ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20, height: ui.kTextHeightNone),
332+
);
333+
builderWithNone.addText('Hello');
334+
final ui.Paragraph paragraphWithNone = builderWithNone.build()
335+
..layout(const ui.ParagraphConstraints(width: 500));
336+
337+
final builderDefault = ui.ParagraphBuilder(
338+
ui.ParagraphStyle(fontFamily: 'Arial', fontSize: 20),
339+
);
340+
builderDefault.addText('Hello');
341+
final ui.Paragraph paragraphDefault = builderDefault.build()
342+
..layout(const ui.ParagraphConstraints(width: 500));
343+
344+
expect(paragraphWithNone.height, greaterThan(0));
345+
expect(paragraphWithNone.height, paragraphDefault.height);
346+
});
304347
}

0 commit comments

Comments
 (0)