Skip to content

Commit 24e828b

Browse files
committed
Add ParagraphStyle.hyphenCharacter to dart:ui
Lets apps choose the string drawn at a soft hyphen (U+00AD) line break, instead of always drawing "-". Native and Skwasm pass it to Skia's ParagraphStyle::setSoftHyphenString. CanvasKit and WebParagraph store it for now: CanvasKit gets it once its softHyphenString binding rolls in, and WebParagraph does not draw soft hyphens yet (flutter#193506). Part of flutter#189617.
1 parent 130546f commit 24e828b

17 files changed

Lines changed: 172 additions & 18 deletions

File tree

‎engine/src/flutter/lib/ui/text.dart‎

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,6 +1967,7 @@ Int32List _encodeParagraphStyle(
19671967
String? ellipsis,
19681968
Locale? locale,
19691969
Hyphens? hyphens,
1970+
String? hyphenCharacter,
19701971
) {
19711972
final result = Int32List(8); // also update paragraph_builder.cc
19721973
if (textAlign != null) {
@@ -2023,6 +2024,10 @@ Int32List _encodeParagraphStyle(
20232024
result[0] |= 1 << 13;
20242025
result[7] = hyphens.index;
20252026
}
2027+
if (hyphenCharacter != null) {
2028+
result[0] |= 1 << 14;
2029+
// Passed separately to native.
2030+
}
20262031
return result;
20272032
}
20282033

@@ -2092,6 +2097,15 @@ class ParagraphStyle {
20922097
///
20932098
/// * `hyphens`: Whether to render a hyphen glyph for soft hyphens (U+00AD)
20942099
/// that fall at a line break. Defaults to [Hyphens.manual].
2100+
///
2101+
/// * `hyphenCharacter`: The string drawn at the end of a line that breaks
2102+
/// at a soft hyphen (U+00AD). If null, a hyphen-minus ("-") is drawn. Line
2103+
/// breaking leaves room for the string when it can, so a longer string can
2104+
/// move a break earlier. The empty string draws nothing, but the line
2105+
/// still breaks there. Has no effect when `hyphens` is [Hyphens.hidden].
2106+
/// The string is shaped as a single run in the style of the text before
2107+
/// it, so it should be short, typically one character, and supported by
2108+
/// that text's font.
20952109
ParagraphStyle({
20962110
TextAlign? textAlign,
20972111
TextDirection? textDirection,
@@ -2106,6 +2120,7 @@ class ParagraphStyle {
21062120
String? ellipsis,
21072121
Locale? locale,
21082122
Hyphens? hyphens,
2123+
String? hyphenCharacter,
21092124
}) : _encoded = _encodeParagraphStyle(
21102125
textAlign,
21112126
textDirection,
@@ -2120,13 +2135,15 @@ class ParagraphStyle {
21202135
ellipsis,
21212136
locale,
21222137
hyphens,
2138+
hyphenCharacter,
21232139
),
21242140
_fontFamily = fontFamily,
21252141
_fontSize = fontSize,
21262142
_height = height,
21272143
_strutStyle = strutStyle,
21282144
_ellipsis = ellipsis,
21292145
_locale = locale,
2146+
_hyphenCharacter = hyphenCharacter,
21302147
_leadingDistribution =
21312148
textHeightBehavior?.leadingDistribution ?? TextLeadingDistribution.proportional;
21322149

@@ -2137,6 +2154,7 @@ class ParagraphStyle {
21372154
final StrutStyle? _strutStyle;
21382155
final String? _ellipsis;
21392156
final Locale? _locale;
2157+
final String? _hyphenCharacter;
21402158
final TextLeadingDistribution _leadingDistribution;
21412159

21422160
@override
@@ -2154,6 +2172,7 @@ class ParagraphStyle {
21542172
other._strutStyle == _strutStyle &&
21552173
other._ellipsis == _ellipsis &&
21562174
other._locale == _locale &&
2175+
other._hyphenCharacter == _hyphenCharacter &&
21572176
other._leadingDistribution == _leadingDistribution &&
21582177
_listEquals<int>(other._encoded, _encoded);
21592178
}
@@ -2166,6 +2185,7 @@ class ParagraphStyle {
21662185
_height,
21672186
_ellipsis,
21682187
_locale,
2188+
_hyphenCharacter,
21692189
_leadingDistribution,
21702190
);
21712191

@@ -2184,7 +2204,8 @@ class ParagraphStyle {
21842204
'strutStyle: ${_encoded[0] & 0x400 == 0x400 ? _strutStyle : "unspecified"}, '
21852205
'ellipsis: ${_encoded[0] & 0x800 == 0x800 ? '"$_ellipsis"' : "unspecified"}, '
21862206
'locale: ${_encoded[0] & 0x1000 == 0x1000 ? _locale : "unspecified"}, '
2187-
'hyphens: ${_encoded[0] & 0x2000 == 0x2000 ? Hyphens.values[_encoded[7]] : "unspecified"}'
2207+
'hyphens: ${_encoded[0] & 0x2000 == 0x2000 ? Hyphens.values[_encoded[7]] : "unspecified"}, '
2208+
'hyphenCharacter: ${_encoded[0] & 0x4000 == 0x4000 ? '"$_hyphenCharacter"' : "unspecified"}'
21882209
')';
21892210
}
21902211
}
@@ -3609,12 +3630,13 @@ base class _NativeParagraphBuilder extends NativeFieldWrapperClass1 implements P
36093630
style._height ?? 0,
36103631
style._ellipsis ?? '',
36113632
_encodeLocale(style._locale),
3633+
style._hyphenCharacter ?? '',
36123634
);
36133635
}
36143636

3615-
@Native<Void Function(Handle, Handle, Handle, Handle, Handle, Double, Double, Handle, Handle)>(
3616-
symbol: 'ParagraphBuilder::Create',
3617-
)
3637+
@Native<
3638+
Void Function(Handle, Handle, Handle, Handle, Handle, Double, Double, Handle, Handle, Handle)
3639+
>(symbol: 'ParagraphBuilder::Create')
36183640
external void _constructor(
36193641
Int32List encoded,
36203642
ByteData? strutData,
@@ -3624,6 +3646,7 @@ base class _NativeParagraphBuilder extends NativeFieldWrapperClass1 implements P
36243646
double height,
36253647
String ellipsis,
36263648
String locale,
3649+
String hyphenCharacter,
36273650
);
36283651

36293652
@override

‎engine/src/flutter/lib/ui/text/paragraph_builder.cc‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ const int kPSLocaleIndex = 12;
9595
// encode slot (7), since slots 7-12 are reused only as mask-bit positions.
9696
const int kPSRenderSoftHyphensIndex = 13;
9797
const int kPSRenderSoftHyphensEncodeIndex = 7;
98+
// Like ellipsis, soft_hyphen_string is passed separately and only claims a
99+
// mask bit.
100+
const int kPSSoftHyphenStringIndex = 14;
98101

99102
const int kPSTextAlignMask = 1 << kPSTextAlignIndex;
100103
const int kPSTextDirectionMask = 1 << kPSTextDirectionIndex;
@@ -109,6 +112,7 @@ const int kPSStrutStyleMask = 1 << kPSStrutStyleIndex;
109112
const int kPSEllipsisMask = 1 << kPSEllipsisIndex;
110113
const int kPSLocaleMask = 1 << kPSLocaleIndex;
111114
const int kPSRenderSoftHyphensMask = 1 << kPSRenderSoftHyphensIndex;
115+
const int kPSSoftHyphenStringMask = 1 << kPSSoftHyphenStringIndex;
112116

113117
// TextShadows decoding
114118

@@ -159,11 +163,12 @@ void ParagraphBuilder::Create(Dart_Handle wrapper,
159163
double fontSize,
160164
double height,
161165
const std::u16string& ellipsis,
162-
const std::string& locale) {
166+
const std::string& locale,
167+
const std::u16string& softHyphenString) {
163168
UIDartState::ThrowIfUIOperationsProhibited();
164169
auto res = fml::MakeRefCounted<ParagraphBuilder>(
165170
encoded_handle, strutData, fontFamily, strutFontFamilies, fontSize,
166-
height, ellipsis, locale);
171+
height, ellipsis, locale, softHyphenString);
167172
res->AssociateWithDartWrapper(wrapper);
168173
}
169174

@@ -232,7 +237,8 @@ ParagraphBuilder::ParagraphBuilder(
232237
double fontSize,
233238
double height,
234239
const std::u16string& ellipsis,
235-
const std::string& locale) {
240+
const std::string& locale,
241+
const std::u16string& softHyphenString) {
236242
int32_t mask = 0;
237243
txt::ParagraphStyle style;
238244
{
@@ -299,6 +305,10 @@ ParagraphBuilder::ParagraphBuilder(
299305
style.locale = locale;
300306
}
301307

308+
if (mask & kPSSoftHyphenStringMask) {
309+
style.soft_hyphen_string = softHyphenString;
310+
}
311+
302312
FontCollection& font_collection = UIDartState::Current()
303313
->platform_configuration()
304314
->client()

‎engine/src/flutter/lib/ui/text/paragraph_builder.h‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class ParagraphBuilder : public RefCountedDartWrappable<ParagraphBuilder> {
3030
double fontSize,
3131
double height,
3232
const std::u16string& ellipsis,
33-
const std::string& locale);
33+
const std::string& locale,
34+
const std::u16string& softHyphenString);
3435

3536
~ParagraphBuilder() override;
3637

@@ -76,7 +77,8 @@ class ParagraphBuilder : public RefCountedDartWrappable<ParagraphBuilder> {
7677
double fontSize,
7778
double height,
7879
const std::u16string& ellipsis,
79-
const std::string& locale);
80+
const std::string& locale,
81+
const std::u16string& softHyphenString);
8082

8183
std::unique_ptr<txt::ParagraphBuilder> m_paragraph_builder_;
8284
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ SkParagraphStyle _toSkParagraphStyle(EngineParagraphStyle style) {
8383
properties.applyRoundingHack = false;
8484
// Like the native engine, render soft hyphens unless they are hidden.
8585
properties.renderSoftHyphens = style.hyphens != ui.Hyphens.hidden;
86+
// TODO(dbebawy): Pass style.hyphenCharacter once CanvasKit's softHyphenString
87+
// rolls in. https://github.com/flutter/flutter/issues/189617
8688

8789
return canvasKit.ParagraphStyle(properties);
8890
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
238238
this.ellipsis,
239239
this.locale,
240240
this.hyphens,
241+
this.hyphenCharacter,
241242
}) : height = height == ui.kTextHeightNone ? null : height,
242243
strutStyle = strutStyle as EngineStrutStyle?;
243244

@@ -254,6 +255,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
254255
final String? ellipsis;
255256
final ui.Locale? locale;
256257
final ui.Hyphens? hyphens;
258+
final String? hyphenCharacter;
257259

258260
EngineStrutStyle? get effectiveStrutStyle {
259261
final EngineStrutStyle? strutStyle = this.strutStyle;
@@ -311,7 +313,8 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
311313
other.strutStyle == strutStyle &&
312314
other.ellipsis == ellipsis &&
313315
other.locale == locale &&
314-
other.hyphens == hyphens;
316+
other.hyphens == hyphens &&
317+
other.hyphenCharacter == hyphenCharacter;
315318
}
316319

317320
@override
@@ -330,6 +333,7 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
330333
ellipsis,
331334
locale,
332335
hyphens,
336+
hyphenCharacter,
333337
);
334338
}
335339

@@ -353,7 +357,8 @@ class EngineParagraphStyle implements ui.ParagraphStyle {
353357
'strutStyle: ${strutStyle ?? "unspecified"}, '
354358
'ellipsis: ${ellipsis != null ? '"$ellipsis"' : "unspecified"}, '
355359
'locale: ${locale ?? "unspecified"}, '
356-
'hyphens: ${hyphens ?? "unspecified"}'
360+
'hyphens: ${hyphens ?? "unspecified"}, '
361+
'hyphenCharacter: ${hyphenCharacter != null ? '"$hyphenCharacter"' : "unspecified"}'
357362
')';
358363
return true;
359364
}());

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,11 @@ ParagraphStyleHandle _createNativeParagraphStyle(
426426
paragraphStyleSetApplyRoundingHack(handle, false);
427427
// Like the native engine, render soft hyphens unless they are hidden.
428428
paragraphStyleSetRenderSoftHyphens(handle, style.hyphens != ui.Hyphens.hidden);
429+
if (style.hyphenCharacter != null) {
430+
final SkStringHandle hyphenCharacterHandle = skStringFromDartString(style.hyphenCharacter!);
431+
paragraphStyleSetSoftHyphenString(handle, hyphenCharacterHandle);
432+
skStringFree(hyphenCharacterHandle);
433+
}
429434
return handle;
430435
}
431436

‎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
@@ -88,3 +88,12 @@ external void paragraphStyleSetRenderSoftHyphens(
8888
ParagraphStyleHandle handle,
8989
bool renderSoftHyphens,
9090
);
91+
92+
@Native<Void Function(ParagraphStyleHandle, SkStringHandle)>(
93+
symbol: 'paragraphStyle_setSoftHyphenString',
94+
isLeaf: true,
95+
)
96+
external void paragraphStyleSetSoftHyphenString(
97+
ParagraphStyleHandle handle,
98+
SkStringHandle softHyphenString,
99+
);

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class WebParagraphStyle implements ui.ParagraphStyle {
3737
ui.StrutStyle? strutStyle,
3838
this.textHeightBehavior,
3939
this.hyphens,
40+
this.hyphenCharacter,
4041
}) : _strutStyle = strutStyle as WebStrutStyle?,
4142
_textStyle = WebTextStyle(
4243
fontFamily: fontFamily,
@@ -69,6 +70,10 @@ class WebParagraphStyle implements ui.ParagraphStyle {
6970
// https://github.com/flutter/flutter/issues/193506
7071
final ui.Hyphens? hyphens;
7172

73+
// TODO(dbebawy): draw hyphenCharacter along with hyphens.
74+
// https://github.com/flutter/flutter/issues/193506
75+
final String? hyphenCharacter;
76+
7277
WebStrutStyle? get strutStyle => _strutStyle;
7378
final WebStrutStyle? _strutStyle;
7479

@@ -88,7 +93,8 @@ class WebParagraphStyle implements ui.ParagraphStyle {
8893
textHeightBehavior == other.textHeightBehavior &&
8994
_strutStyle == other._strutStyle &&
9095
_textStyle == other._textStyle &&
91-
hyphens == other.hyphens;
96+
hyphens == other.hyphens &&
97+
hyphenCharacter == other.hyphenCharacter;
9298
}
9399

94100
@override
@@ -102,6 +108,7 @@ class WebParagraphStyle implements ui.ParagraphStyle {
102108
_strutStyle,
103109
_textStyle,
104110
hyphens,
111+
hyphenCharacter,
105112
);
106113
}
107114

@@ -117,6 +124,7 @@ class WebParagraphStyle implements ui.ParagraphStyle {
117124
'ellipsis: $ellipsis, '
118125
'textHeightBehavior: $textHeightBehavior, '
119126
'hyphens: $hyphens, '
127+
'hyphenCharacter: $hyphenCharacter, '
120128
'strutStyle: $_strutStyle, '
121129
'textAlign: $textAlign'
122130
'textStyle: $_textStyle'

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,7 @@ abstract class ParagraphStyle {
406406
String? ellipsis,
407407
Locale? locale,
408408
Hyphens? hyphens,
409+
String? hyphenCharacter,
409410
}) => engine.isWebParagraphEnabled
410411
? engine.WebParagraphStyle(
411412
textAlign: textAlign,
@@ -421,6 +422,7 @@ abstract class ParagraphStyle {
421422
ellipsis: ellipsis,
422423
locale: locale,
423424
hyphens: hyphens,
425+
hyphenCharacter: hyphenCharacter,
424426
)
425427
: engine.EngineParagraphStyle(
426428
textAlign: textAlign,
@@ -436,6 +438,7 @@ abstract class ParagraphStyle {
436438
ellipsis: ellipsis,
437439
locale: locale,
438440
hyphens: hyphens,
441+
hyphenCharacter: hyphenCharacter,
439442
);
440443
}
441444

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ 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';
1112

1213
void main() {
1314
internalBootstrapBrowserTest(() => testMain);
@@ -224,4 +225,27 @@ Future<void> testMain() async {
224225
expect(unspecified.longestLine, manual.longestLine);
225226
});
226227
});
228+
229+
group('hyphenCharacter', () {
230+
double longestLine(String? hyphenCharacter) {
231+
final builder = ParagraphBuilder(
232+
ParagraphStyle(fontSize: 10, fontFamily: 'FlutterTest', hyphenCharacter: hyphenCharacter),
233+
);
234+
// Breaks at the soft hyphen into 'abcd' and 'efg'.
235+
builder.addText('abcd\u00ADefg');
236+
return (builder.build()..layout(const ParagraphConstraints(width: 65))).longestLine;
237+
}
238+
239+
test(
240+
'is drawn at a soft hyphen line break',
241+
() {
242+
expect(longestLine(null), 50);
243+
expect(longestLine('--'), 60);
244+
expect(longestLine(''), 40);
245+
},
246+
// TODO(dbebawy): Enable on CanvasKit once its softHyphenString rolls in.
247+
// https://github.com/flutter/flutter/issues/189617
248+
skip: isCanvasKit,
249+
);
250+
});
227251
}

0 commit comments

Comments
 (0)