Skip to content

Commit 05c25ae

Browse files
dbebawyLongCatIsLooong
authored andcommitted
[web] Render soft hyphens on Skwasm (flutter#193850)
Makes `Hyphens` work on the Skwasm renderer. flutter#185152 added the `Hyphens` API, but the web renderers only store the value. Skwasm gets a `paragraphStyle_setRenderSoftHyphens` binding, and `SkwasmParagraphStyle` sets it the same way the native engine does: soft hyphens render at a line break unless `Hyphens.hidden` is set. So text containing U+00AD now shows a hyphen at soft-hyphen line breaks on Skwasm, matching native since flutter#185152. It also updates the `TextPainter.hyphens` docs, which said the web doesn't support this yet, and runs the `TextPainter` hyphen tests on Skwasm. They still skip on CanvasKit. CanvasKit isn't covered here. Its `ParagraphStyle` binding lives in Skia (`modules/canvaskit/paragraph_bindings.cpp`) and doesn't expose `renderSoftHyphens` yet, so that needs a Skia change first: https://skia-review.googlesource.com/c/skia/+/1378836. Once that rolls, the Flutter side is a few lines in `CkParagraphStyle`. Until then, a `--wasm` app that falls back to CanvasKit won't show the hyphen. WebParagraph also still ignores `hyphens`. Part of flutter#193506. ## Pre-launch Checklist - [ ] 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. - [ ] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [ ] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [ ] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [ ] I updated/added relevant in-code documentation (doc comments with `///`). - [ ] 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). - [ ] I added new tests to check the change I am making, or this PR is [test-exempt]. - [ ] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [ ] 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/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: LongCatIsLooong <[email protected]>
1 parent b8d0aad commit 05c25ae

6 files changed

Lines changed: 61 additions & 11 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/paragraph.dart‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,8 @@ class SkwasmParagraphStyle implements ui.ParagraphStyle {
842842
}
843843
paragraphStyleSetTextStyle(handle, textStyleHandle);
844844
paragraphStyleSetApplyRoundingHack(handle, false);
845+
// Like the native engine, render soft hyphens unless they are hidden.
846+
paragraphStyleSetRenderSoftHyphens(handle, _hyphens != ui.Hyphens.hidden);
845847
return (handle, textStyle);
846848
}
847849

@@ -857,13 +859,6 @@ class SkwasmParagraphStyle implements ui.ParagraphStyle {
857859
final ui.StrutStyle? _strutStyle;
858860
final String? _ellipsis;
859861
final ui.Locale? _locale;
860-
// TODO(dbebawy): hyphens is accepted and stored but not honored here yet.
861-
// Neither Hyphens.manual nor Hyphens.hidden takes effect on Skwasm until a
862-
// renderSoftHyphens setter is exposed on the Skwasm ParagraphStyle binding
863-
// (the native engine wires this via SkParagraph::setRenderSoftHyphens).
864-
// https://github.com/flutter/flutter/issues/193506
865-
// Customizing the hyphen string is separate future work:
866-
// https://github.com/flutter/flutter/issues/189617
867862
final ui.Hyphens? _hyphens;
868863

869864
@override

‎engine/src/flutter/lib/web_ui/lib/src/engine/skwasm/skwasm_impl/raw/text/raw_paragraph_style.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,12 @@ external void paragraphStyleSetApplyRoundingHack(
7979
ParagraphStyleHandle handle,
8080
bool applyRoundingHack,
8181
);
82+
83+
@Native<Void Function(ParagraphStyleHandle, Bool)>(
84+
symbol: 'paragraphStyle_setRenderSoftHyphens',
85+
isLeaf: true,
86+
)
87+
external void paragraphStyleSetRenderSoftHyphens(
88+
ParagraphStyleHandle handle,
89+
bool renderSoftHyphens,
90+
);

‎engine/src/flutter/lib/web_ui/test/ui/paragraph_builder_test.dart‎

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@
55
import 'package:test/bootstrap/browser.dart';
66
import 'package:test/test.dart';
77
import 'package:ui/ui.dart';
8+
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
89

910
import '../common/test_initialization.dart';
11+
import 'utils.dart';
1012

1113
void main() {
1214
internalBootstrapBrowserTest(() => testMain);
1315
}
1416

1517
Future<void> testMain() async {
16-
setUpUnitTests(withImplicitView: true, setUpTestViewDimensions: false);
18+
setUpUnitTests(
19+
withImplicitView: true,
20+
setUpTestViewDimensions: false,
21+
testEnvironment: const ui_web.TestEnvironment(forceTestFonts: true),
22+
);
1723

1824
test('Should be able to build and layout a paragraph', () {
1925
final builder = ParagraphBuilder(ParagraphStyle());
@@ -109,4 +115,37 @@ Future<void> testMain() async {
109115
// The height should be much smaller than fontSize * 10.
110116
expect(paragraph.height, lessThan(2 * fontSize));
111117
});
118+
119+
// TODO(dbebawy): Enable on CanvasKit, https://github.com/flutter/flutter/issues/193506.
120+
group('hyphens', () {
121+
const text = 'abcd\u00ADe';
122+
123+
Paragraph layoutParagraph(Hyphens? hyphens, double width) {
124+
final builder = ParagraphBuilder(
125+
ParagraphStyle(fontSize: 10, fontFamily: 'FlutterTest', hyphens: hyphens),
126+
);
127+
builder.addText(text);
128+
return builder.build()..layout(ParagraphConstraints(width: width));
129+
}
130+
131+
// Just narrower than the unbroken text, so the line breaks at the soft hyphen.
132+
double breakingWidth() => layoutParagraph(null, double.infinity).longestLine - 1;
133+
134+
test('manual renders a hyphen at a soft hyphen line break', () {
135+
final Paragraph manual = layoutParagraph(Hyphens.manual, breakingWidth());
136+
final Paragraph hidden = layoutParagraph(Hyphens.hidden, breakingWidth());
137+
138+
expect(manual.numberOfLines, 2);
139+
expect(hidden.numberOfLines, 2);
140+
expect(manual.longestLine, 50);
141+
expect(hidden.longestLine, 40);
142+
});
143+
144+
test('unspecified behaves like manual', () {
145+
final Paragraph unspecified = layoutParagraph(null, breakingWidth());
146+
final Paragraph manual = layoutParagraph(Hyphens.manual, breakingWidth());
147+
148+
expect(unspecified.longestLine, manual.longestLine);
149+
});
150+
}, skip: isCanvasKit);
112151
}

‎engine/src/flutter/skwasm/text/paragraph_style.cc‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,9 @@ SKWASM_EXPORT void paragraphStyle_setApplyRoundingHack(
8383
bool apply_rounding_hack) {
8484
style->skia_paragraph_style.setApplyRoundingHack(apply_rounding_hack);
8585
}
86+
87+
SKWASM_EXPORT void paragraphStyle_setRenderSoftHyphens(
88+
Skwasm::ParagraphStyle* style,
89+
bool render_soft_hyphens) {
90+
style->skia_paragraph_style.setRenderSoftHyphens(render_soft_hyphens);
91+
}

‎packages/flutter/lib/src/painting/text_painter.dart‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,8 @@ class TextPainter {
10671067
/// that falls on a soft hyphen. [Hyphens.hidden] suppresses the glyph; the line
10681068
/// still breaks at U+00AD regardless.
10691069
///
1070-
/// Not yet supported on the web, where the hyphen glyph is never rendered.
1070+
/// On the web, only the Skwasm renderer supports this so far. With CanvasKit,
1071+
/// the hyphen glyph is never rendered.
10711072
/// {@endtemplate}
10721073
Hyphens get hyphens => _hyphens;
10731074
Hyphens _hyphens;

‎packages/flutter/test/painting/text_painter_test.dart‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ void main() {
12951295
expect(hidden.width, 30);
12961296
manual.dispose();
12971297
hidden.dispose();
1298-
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/193506
1298+
}, skip: kIsWeb && !kIsWasm); // CanvasKit: https://github.com/flutter/flutter/issues/193506
12991299

13001300
test('Changing TextPainter.hyphens after layout changes the width', () {
13011301
final TextPainter painter = softHyphenPainter(Hyphens.hidden)..layout(maxWidth: 49);
@@ -1305,7 +1305,7 @@ void main() {
13051305
painter.layout(maxWidth: 49);
13061306
expect(painter.width, 40);
13071307
painter.dispose();
1308-
}, skip: kIsWeb); // https://github.com/flutter/flutter/issues/193506
1308+
}, skip: kIsWeb && !kIsWasm); // CanvasKit: https://github.com/flutter/flutter/issues/193506
13091309

13101310
test('TextPainter line metrics', () {
13111311
final painter = TextPainter()..textDirection = TextDirection.ltr;

0 commit comments

Comments
 (0)