Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 322d5b6

Browse files
add TextLeadingDistribution to webui TextStyle
1 parent afd9c39 commit 322d5b6

File tree

6 files changed

+62
-3
lines changed

6 files changed

+62
-3
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,7 @@ class SkTextStyleProperties {
16611661
external set letterSpacing(double? value);
16621662
external set wordSpacing(double? value);
16631663
external set heightMultiplier(double? value);
1664+
external set halfLeading(bool? value);
16641665
external set locale(String? value);
16651666
external set fontFamilies(List<String>? value);
16661667
external set fontStyle(SkFontStyle? value);
@@ -1675,6 +1676,7 @@ class SkStrutStyleProperties {
16751676
external set fontStyle(SkFontStyle? value);
16761677
external set fontSize(double? value);
16771678
external set heightMultiplier(double? value);
1679+
external set halfLeading(bool? value);
16781680
external set leading(double? value);
16791681
external set strutEnabled(bool? value);
16801682
external set forceStrutHeight(bool? value);

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
3737
_fontFamily = fontFamily,
3838
_fontSize = fontSize,
3939
_height = height,
40+
_leadingDistribution = textHeightBehavior?.leadingDistribution,
4041
_fontWeight = fontWeight,
4142
_fontStyle = fontStyle;
4243

@@ -47,6 +48,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
4748
final double? _height;
4849
final ui.FontWeight? _fontWeight;
4950
final ui.FontStyle? _fontStyle;
51+
final ui.TextLeadingDistribution? _leadingDistribution;
5052

5153
static SkTextStyleProperties toSkTextStyleProperties(
5254
String? fontFamily,
@@ -73,7 +75,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
7375
return skTextStyle;
7476
}
7577

76-
static SkStrutStyleProperties toSkStrutStyleProperties(ui.StrutStyle value) {
78+
static SkStrutStyleProperties toSkStrutStyleProperties(ui.StrutStyle value, ui.TextHeightBehavior? paragraphHeightBehavior) {
7779
EngineStrutStyle style = value as EngineStrutStyle;
7880
final SkStrutStyleProperties skStrutStyle = SkStrutStyleProperties();
7981
skStrutStyle.fontFamilies =
@@ -87,6 +89,17 @@ class CkParagraphStyle implements ui.ParagraphStyle {
8789
skStrutStyle.heightMultiplier = style._height;
8890
}
8991

92+
switch (style._leadingDistribution ?? paragraphHeightBehavior?.leadingDistribution) {
93+
case null:
94+
break;
95+
case ui.TextLeadingDistribution.even:
96+
skStrutStyle.halfLeading = true;
97+
break;
98+
case ui.TextLeadingDistribution.proportional:
99+
skStrutStyle.halfLeading = false;
100+
break;
101+
}
102+
90103
if (style._leading != null) {
91104
skStrutStyle.leading = style._leading;
92105
}
@@ -147,7 +160,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
147160
}
148161

149162
if (strutStyle != null) {
150-
properties.strutStyle = toSkStrutStyleProperties(strutStyle);
163+
properties.strutStyle = toSkStrutStyleProperties(strutStyle, textHeightBehavior);
151164
}
152165

153166
properties.textStyle = toSkTextStyleProperties(
@@ -161,6 +174,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
161174
fontFamily: _fontFamily,
162175
fontSize: _fontSize,
163176
height: _height,
177+
leadingDistribution: _leadingDistribution,
164178
fontWeight: _fontWeight,
165179
fontStyle: _fontStyle,
166180
);
@@ -184,6 +198,7 @@ class CkTextStyle implements ui.TextStyle {
184198
double? letterSpacing,
185199
double? wordSpacing,
186200
double? height,
201+
ui.TextLeadingDistribution? leadingDistribution,
187202
ui.Locale? locale,
188203
CkPaint? background,
189204
CkPaint? foreground,
@@ -205,6 +220,7 @@ class CkTextStyle implements ui.TextStyle {
205220
letterSpacing,
206221
wordSpacing,
207222
height,
223+
leadingDistribution,
208224
locale,
209225
background,
210226
foreground,
@@ -228,6 +244,7 @@ class CkTextStyle implements ui.TextStyle {
228244
this.letterSpacing,
229245
this.wordSpacing,
230246
this.height,
247+
this.leadingDistribution,
231248
this.locale,
232249
this.background,
233250
this.foreground,
@@ -249,6 +266,7 @@ class CkTextStyle implements ui.TextStyle {
249266
final double? letterSpacing;
250267
final double? wordSpacing;
251268
final double? height;
269+
final ui.TextLeadingDistribution? leadingDistribution;
252270
final ui.Locale? locale;
253271
final CkPaint? background;
254272
final CkPaint? foreground;
@@ -275,6 +293,7 @@ class CkTextStyle implements ui.TextStyle {
275293
letterSpacing: other.letterSpacing ?? letterSpacing,
276294
wordSpacing: other.wordSpacing ?? wordSpacing,
277295
height: other.height ?? height,
296+
leadingDistribution: other.leadingDistribution ?? leadingDistribution,
278297
locale: other.locale ?? locale,
279298
background: other.background ?? background,
280299
foreground: other.foreground ?? foreground,
@@ -367,6 +386,17 @@ class CkTextStyle implements ui.TextStyle {
367386
properties.heightMultiplier = height;
368387
}
369388

389+
switch (leadingDistribution) {
390+
case null:
391+
break;
392+
case ui.TextLeadingDistribution.even:
393+
properties.halfLeading = true;
394+
break;
395+
case ui.TextLeadingDistribution.proportional:
396+
properties.halfLeading = false;
397+
break;
398+
}
399+
370400
if (locale != null) {
371401
properties.locale = locale.toLanguageTag();
372402
}

lib/web_ui/lib/src/engine/text/paragraph.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,6 @@ class EngineStrutStyle implements ui.StrutStyle {
12101210
List<String>? fontFamilyFallback,
12111211
double? fontSize,
12121212
double? height,
1213-
//TODO(LongCatIsLooong): implement leadingDistribution.
12141213
ui.TextLeadingDistribution? leadingDistribution,
12151214
double? leading,
12161215
ui.FontWeight? fontWeight,

lib/web_ui/lib/src/ui/text.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ abstract class TextStyle {
285285
letterSpacing: letterSpacing,
286286
wordSpacing: wordSpacing,
287287
height: height,
288+
leadingDistribution: leadingDistribution,
288289
locale: locale,
289290
background: background as engine.CkPaint?,
290291
foreground: foreground as engine.CkPaint?,

lib/web_ui/test/canvaskit/canvas_golden_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,27 @@ void testMain() {
357357
await testTextStyle('height', height: 2);
358358
});
359359

360+
test('text styles - leading distribution', () async {
361+
await testTextStyle('half leading', height: 20, fontSize: 10, leadingDistribution: ui.TextLeadingDistribution.even);
362+
await testTextStyle(
363+
'half leading inherited from paragraph',
364+
height: 20,
365+
fontSize: 10,
366+
paragraphTextHeightBehavior: ui.TextHeightBehavior(
367+
leadingDistribution: ui.TextLeadingDistribution.even,
368+
),
369+
);
370+
await testTextStyle(
371+
'half leading',
372+
height: 20,
373+
fontSize: 10,
374+
leadingDistribution: ui.TextLeadingDistribution.proportional,
375+
paragraphTextHeightBehavior: ui.TextHeightBehavior(
376+
leadingDistribution: ui.TextLeadingDistribution.even,
377+
),
378+
);
379+
});
380+
360381
// TODO(yjbanov): locales specified in text styles don't work:
361382
// https://github.com/flutter/flutter/issues/74687
362383
// TODO(yjbanov): spaces are not rendered correctly:
@@ -1118,6 +1139,7 @@ Future<void> testTextStyle(
11181139
double? letterSpacing,
11191140
double? wordSpacing,
11201141
double? height,
1142+
ui.TextLeadingDistribution? leadingDistribution,
11211143
ui.Locale? locale,
11221144
CkPaint? background,
11231145
CkPaint? foreground,
@@ -1169,6 +1191,7 @@ Future<void> testTextStyle(
11691191
letterSpacing: letterSpacing,
11701192
wordSpacing: wordSpacing,
11711193
height: height,
1194+
leadingDistribution: leadingDistribution,
11721195
locale: locale,
11731196
background: background,
11741197
foreground: foreground,

lib/web_ui/test/canvaskit/canvaskit_api_test.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,7 @@ void _paragraphTests() {
12891289
..letterSpacing = 5
12901290
..wordSpacing = 10
12911291
..heightMultiplier = 2.5
1292+
..halfLeading = true
12921293
..locale = 'en_CA'
12931294
..fontFamilies = <String>['Roboto', 'serif']
12941295
..fontStyle = (SkFontStyle()
@@ -1310,6 +1311,7 @@ void _paragraphTests() {
13101311
..weight = canvasKit.FontWeight.Bold)
13111312
..fontSize = 23
13121313
..heightMultiplier = 5
1314+
..halfLeading = true
13131315
..leading = 6
13141316
..strutEnabled = true
13151317
..forceStrutHeight = false;
@@ -1338,6 +1340,8 @@ void _paragraphTests() {
13381340
SkPaint());
13391341
builder.addText('!');
13401342
builder.pop();
1343+
builder.pushStyle(canvasKit.TextStyle(SkTextStyleProperties()..halfLeading = true));
1344+
builder.pop();
13411345
final SkParagraph paragraph = builder.build();
13421346
paragraph.layout(55);
13431347
expect(paragraph.getAlphabeticBaseline(),

0 commit comments

Comments
 (0)