Skip to content

Commit 76786c1

Browse files
committed
[web] Render soft hyphens on Skwasm
Adds a paragraphStyle_setRenderSoftHyphens binding to Skwasm and sets it from SkwasmParagraphStyle, so Hyphens behaves the same as on native: soft hyphens render at a line break unless Hyphens.hidden is set. CanvasKit still ignores hyphens until its ParagraphStyle binding in Skia exposes renderSoftHyphens. Part of flutter#193506
1 parent eaee7db commit 76786c1

4 files changed

Lines changed: 50 additions & 7 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
@@ -822,6 +822,8 @@ class SkwasmParagraphStyle implements ui.ParagraphStyle {
822822
}
823823
paragraphStyleSetTextStyle(handle, textStyleHandle);
824824
paragraphStyleSetApplyRoundingHack(handle, false);
825+
// Like the native engine, render soft hyphens unless they are hidden.
826+
paragraphStyleSetRenderSoftHyphens(handle, _hyphens != ui.Hyphens.hidden);
825827
return (handle, textStyle);
826828
}
827829

@@ -837,13 +839,6 @@ class SkwasmParagraphStyle implements ui.ParagraphStyle {
837839
final ui.StrutStyle? _strutStyle;
838840
final String? _ellipsis;
839841
final ui.Locale? _locale;
840-
// TODO(dbebawy): hyphens is accepted and stored but not honored here yet.
841-
// Neither Hyphens.manual nor Hyphens.hidden takes effect on Skwasm until a
842-
// renderSoftHyphens setter is exposed on the Skwasm ParagraphStyle binding
843-
// (the native engine wires this via SkParagraph::setRenderSoftHyphens).
844-
// https://github.com/flutter/flutter/issues/193506
845-
// Customizing the hyphen string is separate future work:
846-
// https://github.com/flutter/flutter/issues/189617
847842
final ui.Hyphens? _hyphens;
848843

849844
@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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:test/test.dart';
77
import 'package:ui/ui.dart';
88

99
import '../common/test_initialization.dart';
10+
import 'utils.dart';
1011

1112
void main() {
1213
internalBootstrapBrowserTest(() => testMain);
@@ -109,4 +110,36 @@ Future<void> testMain() async {
109110
// The height should be much smaller than fontSize * 10.
110111
expect(paragraph.height, lessThan(2 * fontSize));
111112
});
113+
114+
// TODO(dbebawy): Enable on CanvasKit, https://github.com/flutter/flutter/issues/193506.
115+
group('hyphens', () {
116+
const text = 'abcd\u00ADe';
117+
118+
Paragraph layoutParagraph(Hyphens? hyphens, double width) {
119+
final builder = ParagraphBuilder(
120+
ParagraphStyle(fontSize: 10, fontFamily: 'FlutterTest', hyphens: hyphens),
121+
);
122+
builder.addText(text);
123+
return builder.build()..layout(ParagraphConstraints(width: width));
124+
}
125+
126+
// Just narrower than the unbroken text, so the line breaks at the soft hyphen.
127+
double breakingWidth() => layoutParagraph(null, double.infinity).longestLine - 1;
128+
129+
test('manual renders a hyphen at a soft hyphen line break', () {
130+
final Paragraph manual = layoutParagraph(Hyphens.manual, breakingWidth());
131+
final Paragraph hidden = layoutParagraph(Hyphens.hidden, breakingWidth());
132+
133+
expect(manual.numberOfLines, 2);
134+
expect(hidden.numberOfLines, 2);
135+
expect(manual.longestLine, greaterThan(hidden.longestLine));
136+
});
137+
138+
test('unspecified behaves like manual', () {
139+
final Paragraph unspecified = layoutParagraph(null, breakingWidth());
140+
final Paragraph manual = layoutParagraph(Hyphens.manual, breakingWidth());
141+
142+
expect(unspecified.longestLine, manual.longestLine);
143+
});
144+
}, skip: isCanvasKit);
112145
}

‎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+
}

0 commit comments

Comments
 (0)