Skip to content

Commit 48f7762

Browse files
dbebawysuryaKommana2662
authored andcommitted
[web] Render soft hyphens on CanvasKit (flutter#194339)
Makes `Hyphens` work on the CanvasKit renderer, following flutter#193850 for Skwasm. CanvasKit's `ParagraphStyle` gained `renderSoftHyphens` in Skia (https://skia-review.googlesource.com/c/skia/+/1378836), which rolled into Flutter with flutter#193884. `CkParagraphStyle` now sets it the same way the native engine and Skwasm do: soft hyphens render at a line break unless `Hyphens.hidden` is set. Because CanvasKit is the default renderer for `flutter build web`, text containing U+00AD in most web apps now shows a hyphen at soft-hyphen line breaks, matching native and Skwasm. This also covers a `--wasm` app that falls back to CanvasKit. With both web renderers supported, the hyphen tests in `paragraph_builder_test.dart` and the `TextPainter` hyphen tests no longer skip on the web, and the `TextPainter.hyphens` docs drop the web caveat. WebParagraph, which is experimental and off by default, still ignores `hyphens`; its TODO stays on flutter#193506. Part of flutter#193506. ## 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. - [ ] 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/CONTRIBUTING.ai.md [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 6dabff3 commit 48f7762

5 files changed

Lines changed: 8 additions & 15 deletions

File tree

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/canvaskit_api.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,7 @@ extension type SkParagraphStyleProperties._(JSObject _) implements JSObject {
19291929
external set strutStyle(SkStrutStyleProperties? strutStyle);
19301930
external set replaceTabCharacters(bool? bool);
19311931
external set applyRoundingHack(bool applyRoundingHack);
1932+
external set renderSoftHyphens(bool renderSoftHyphens);
19321933
}
19331934

19341935
extension type SkTextStyle(JSObject _) implements JSObject {}

‎engine/src/flutter/lib/web_ui/lib/src/engine/canvaskit/text.dart‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
4747
strutStyle,
4848
ellipsis,
4949
locale,
50+
hyphens,
5051
),
5152
_textAlign = textAlign,
5253
_textDirection = textDirection,
@@ -78,13 +79,6 @@ class CkParagraphStyle implements ui.ParagraphStyle {
7879
final ui.StrutStyle? _strutStyle;
7980
final String? _ellipsis;
8081
final ui.Locale? _locale;
81-
// TODO(dbebawy): hyphens is accepted and stored but not honored here yet.
82-
// Neither Hyphens.manual nor Hyphens.hidden takes effect on CanvasKit until a
83-
// renderSoftHyphens setter is exposed on the CanvasKit ParagraphStyle binding
84-
// (the native engine wires this via SkParagraph::setRenderSoftHyphens).
85-
// https://github.com/flutter/flutter/issues/193506
86-
// Customizing the hyphen string is separate future work:
87-
// https://github.com/flutter/flutter/issues/189617
8882
final ui.Hyphens? _hyphens;
8983

9084
static SkTextStyleProperties toSkTextStyleProperties(
@@ -178,6 +172,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
178172
ui.StrutStyle? strutStyle,
179173
String? ellipsis,
180174
ui.Locale? locale,
175+
ui.Hyphens? hyphens,
181176
) {
182177
final properties = SkParagraphStyleProperties();
183178

@@ -218,6 +213,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
218213
fontStyle,
219214
);
220215
properties.applyRoundingHack = false;
216+
// Like the native engine, render soft hyphens unless they are hidden.
217+
properties.renderSoftHyphens = hyphens != ui.Hyphens.hidden;
221218

222219
return canvasKit.ParagraphStyle(properties);
223220
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:ui/ui.dart';
88
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
99

1010
import '../common/test_initialization.dart';
11-
import 'utils.dart';
1211

1312
void main() {
1413
internalBootstrapBrowserTest(() => testMain);
@@ -116,7 +115,6 @@ Future<void> testMain() async {
116115
expect(paragraph.height, lessThan(2 * fontSize));
117116
});
118117

119-
// TODO(dbebawy): Enable on CanvasKit, https://github.com/flutter/flutter/issues/193506.
120118
group('hyphens', () {
121119
const text = 'abcd\u00ADe';
122120

@@ -147,5 +145,5 @@ Future<void> testMain() async {
147145

148146
expect(unspecified.longestLine, manual.longestLine);
149147
});
150-
}, skip: isCanvasKit);
148+
});
151149
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,9 +1066,6 @@ class TextPainter {
10661066
/// Defaults to [Hyphens.manual], which renders a hyphen glyph at a line break
10671067
/// that falls on a soft hyphen. [Hyphens.hidden] suppresses the glyph; the line
10681068
/// still breaks at U+00AD regardless.
1069-
///
1070-
/// On the web, only the Skwasm renderer supports this so far. With CanvasKit,
1071-
/// the hyphen glyph is never rendered.
10721069
/// {@endtemplate}
10731070
Hyphens get hyphens => _hyphens;
10741071
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 && !kIsWasm); // CanvasKit: https://github.com/flutter/flutter/issues/193506
1298+
});
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 && !kIsWasm); // CanvasKit: https://github.com/flutter/flutter/issues/193506
1308+
});
13091309

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

0 commit comments

Comments
 (0)