add TextLeadingDistribution to webui TextStyle (flutter/engine#25777)

This commit is contained in:
LongCatIsLooong
2021-05-13 15:29:02 -07:00
committed by GitHub
parent 2043d0839a
commit 177d73babf
7 changed files with 64 additions and 4 deletions

View File

@@ -1,2 +1,2 @@
repository: https://github.com/flutter/goldens.git repository: https://github.com/flutter/goldens.git
revision: 4a8da9f65353bda26a73e12838797f20cfdedc40 revision: 4d00d1a0f1c0bc123814919a45ef2c2f57ed21ec

View File

@@ -1674,6 +1674,7 @@ class SkTextStyleProperties {
external set letterSpacing(double? value); external set letterSpacing(double? value);
external set wordSpacing(double? value); external set wordSpacing(double? value);
external set heightMultiplier(double? value); external set heightMultiplier(double? value);
external set halfLeading(bool? value);
external set locale(String? value); external set locale(String? value);
external set fontFamilies(List<String>? value); external set fontFamilies(List<String>? value);
external set fontStyle(SkFontStyle? value); external set fontStyle(SkFontStyle? value);
@@ -1688,6 +1689,7 @@ class SkStrutStyleProperties {
external set fontStyle(SkFontStyle? value); external set fontStyle(SkFontStyle? value);
external set fontSize(double? value); external set fontSize(double? value);
external set heightMultiplier(double? value); external set heightMultiplier(double? value);
external set halfLeading(bool? value);
external set leading(double? value); external set leading(double? value);
external set strutEnabled(bool? value); external set strutEnabled(bool? value);
external set forceStrutHeight(bool? value); external set forceStrutHeight(bool? value);

View File

@@ -37,6 +37,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
_fontFamily = fontFamily, _fontFamily = fontFamily,
_fontSize = fontSize, _fontSize = fontSize,
_height = height, _height = height,
_leadingDistribution = textHeightBehavior?.leadingDistribution,
_fontWeight = fontWeight, _fontWeight = fontWeight,
_fontStyle = fontStyle; _fontStyle = fontStyle;
@@ -47,6 +48,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
final double? _height; final double? _height;
final ui.FontWeight? _fontWeight; final ui.FontWeight? _fontWeight;
final ui.FontStyle? _fontStyle; final ui.FontStyle? _fontStyle;
final ui.TextLeadingDistribution? _leadingDistribution;
static SkTextStyleProperties toSkTextStyleProperties( static SkTextStyleProperties toSkTextStyleProperties(
String? fontFamily, String? fontFamily,
@@ -73,7 +75,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
return skTextStyle; return skTextStyle;
} }
static SkStrutStyleProperties toSkStrutStyleProperties(ui.StrutStyle value) { static SkStrutStyleProperties toSkStrutStyleProperties(ui.StrutStyle value, ui.TextHeightBehavior? paragraphHeightBehavior) {
EngineStrutStyle style = value as EngineStrutStyle; EngineStrutStyle style = value as EngineStrutStyle;
final SkStrutStyleProperties skStrutStyle = SkStrutStyleProperties(); final SkStrutStyleProperties skStrutStyle = SkStrutStyleProperties();
skStrutStyle.fontFamilies = skStrutStyle.fontFamilies =
@@ -87,6 +89,18 @@ class CkParagraphStyle implements ui.ParagraphStyle {
skStrutStyle.heightMultiplier = style._height; skStrutStyle.heightMultiplier = style._height;
} }
final ui.TextLeadingDistribution? effectiveLeadingDistribution = style._leadingDistribution ?? paragraphHeightBehavior?.leadingDistribution;
switch (effectiveLeadingDistribution) {
case null:
break;
case ui.TextLeadingDistribution.even:
skStrutStyle.halfLeading = true;
break;
case ui.TextLeadingDistribution.proportional:
skStrutStyle.halfLeading = false;
break;
}
if (style._leading != null) { if (style._leading != null) {
skStrutStyle.leading = style._leading; skStrutStyle.leading = style._leading;
} }
@@ -147,7 +161,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
} }
if (strutStyle != null) { if (strutStyle != null) {
properties.strutStyle = toSkStrutStyleProperties(strutStyle); properties.strutStyle = toSkStrutStyleProperties(strutStyle, textHeightBehavior);
} }
properties.textStyle = toSkTextStyleProperties( properties.textStyle = toSkTextStyleProperties(
@@ -161,6 +175,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
fontFamily: _fontFamily, fontFamily: _fontFamily,
fontSize: _fontSize, fontSize: _fontSize,
height: _height, height: _height,
leadingDistribution: _leadingDistribution,
fontWeight: _fontWeight, fontWeight: _fontWeight,
fontStyle: _fontStyle, fontStyle: _fontStyle,
); );
@@ -184,6 +199,7 @@ class CkTextStyle implements ui.TextStyle {
double? letterSpacing, double? letterSpacing,
double? wordSpacing, double? wordSpacing,
double? height, double? height,
ui.TextLeadingDistribution? leadingDistribution,
ui.Locale? locale, ui.Locale? locale,
CkPaint? background, CkPaint? background,
CkPaint? foreground, CkPaint? foreground,
@@ -205,6 +221,7 @@ class CkTextStyle implements ui.TextStyle {
letterSpacing, letterSpacing,
wordSpacing, wordSpacing,
height, height,
leadingDistribution,
locale, locale,
background, background,
foreground, foreground,
@@ -228,6 +245,7 @@ class CkTextStyle implements ui.TextStyle {
this.letterSpacing, this.letterSpacing,
this.wordSpacing, this.wordSpacing,
this.height, this.height,
this.leadingDistribution,
this.locale, this.locale,
this.background, this.background,
this.foreground, this.foreground,
@@ -249,6 +267,7 @@ class CkTextStyle implements ui.TextStyle {
final double? letterSpacing; final double? letterSpacing;
final double? wordSpacing; final double? wordSpacing;
final double? height; final double? height;
final ui.TextLeadingDistribution? leadingDistribution;
final ui.Locale? locale; final ui.Locale? locale;
final CkPaint? background; final CkPaint? background;
final CkPaint? foreground; final CkPaint? foreground;
@@ -275,6 +294,7 @@ class CkTextStyle implements ui.TextStyle {
letterSpacing: other.letterSpacing ?? letterSpacing, letterSpacing: other.letterSpacing ?? letterSpacing,
wordSpacing: other.wordSpacing ?? wordSpacing, wordSpacing: other.wordSpacing ?? wordSpacing,
height: other.height ?? height, height: other.height ?? height,
leadingDistribution: other.leadingDistribution ?? leadingDistribution,
locale: other.locale ?? locale, locale: other.locale ?? locale,
background: other.background ?? background, background: other.background ?? background,
foreground: other.foreground ?? foreground, foreground: other.foreground ?? foreground,
@@ -367,6 +387,17 @@ class CkTextStyle implements ui.TextStyle {
properties.heightMultiplier = height; properties.heightMultiplier = height;
} }
switch (leadingDistribution) {
case null:
break;
case ui.TextLeadingDistribution.even:
properties.halfLeading = true;
break;
case ui.TextLeadingDistribution.proportional:
properties.halfLeading = false;
break;
}
if (locale != null) { if (locale != null) {
properties.locale = locale.toLanguageTag(); properties.locale = locale.toLanguageTag();
} }

View File

@@ -1210,7 +1210,6 @@ class EngineStrutStyle implements ui.StrutStyle {
List<String>? fontFamilyFallback, List<String>? fontFamilyFallback,
double? fontSize, double? fontSize,
double? height, double? height,
//TODO(LongCatIsLooong): implement leadingDistribution.
ui.TextLeadingDistribution? leadingDistribution, ui.TextLeadingDistribution? leadingDistribution,
double? leading, double? leading,
ui.FontWeight? fontWeight, ui.FontWeight? fontWeight,

View File

@@ -285,6 +285,7 @@ abstract class TextStyle {
letterSpacing: letterSpacing, letterSpacing: letterSpacing,
wordSpacing: wordSpacing, wordSpacing: wordSpacing,
height: height, height: height,
leadingDistribution: leadingDistribution,
locale: locale, locale: locale,
background: background as engine.CkPaint?, background: background as engine.CkPaint?,
foreground: foreground as engine.CkPaint?, foreground: foreground as engine.CkPaint?,

View File

@@ -357,6 +357,27 @@ void testMain() {
await testTextStyle('height', height: 2); await testTextStyle('height', height: 2);
}); });
test('text styles - leading distribution', () async {
await testTextStyle('half leading', height: 20, fontSize: 10, leadingDistribution: ui.TextLeadingDistribution.even);
await testTextStyle(
'half leading inherited from paragraph',
height: 20,
fontSize: 10,
paragraphTextHeightBehavior: ui.TextHeightBehavior(
leadingDistribution: ui.TextLeadingDistribution.even,
),
);
await testTextStyle(
'text style half leading overrides paragraph style half leading',
height: 20,
fontSize: 10,
leadingDistribution: ui.TextLeadingDistribution.proportional,
paragraphTextHeightBehavior: ui.TextHeightBehavior(
leadingDistribution: ui.TextLeadingDistribution.even,
),
);
});
// TODO(yjbanov): locales specified in text styles don't work: // TODO(yjbanov): locales specified in text styles don't work:
// https://github.com/flutter/flutter/issues/74687 // https://github.com/flutter/flutter/issues/74687
// TODO(yjbanov): spaces are not rendered correctly: // TODO(yjbanov): spaces are not rendered correctly:
@@ -1118,6 +1139,7 @@ Future<void> testTextStyle(
double? letterSpacing, double? letterSpacing,
double? wordSpacing, double? wordSpacing,
double? height, double? height,
ui.TextLeadingDistribution? leadingDistribution,
ui.Locale? locale, ui.Locale? locale,
CkPaint? background, CkPaint? background,
CkPaint? foreground, CkPaint? foreground,
@@ -1169,6 +1191,7 @@ Future<void> testTextStyle(
letterSpacing: letterSpacing, letterSpacing: letterSpacing,
wordSpacing: wordSpacing, wordSpacing: wordSpacing,
height: height, height: height,
leadingDistribution: leadingDistribution,
locale: locale, locale: locale,
background: background, background: background,
foreground: foreground, foreground: foreground,

View File

@@ -1289,6 +1289,7 @@ void _paragraphTests() {
..letterSpacing = 5 ..letterSpacing = 5
..wordSpacing = 10 ..wordSpacing = 10
..heightMultiplier = 2.5 ..heightMultiplier = 2.5
..halfLeading = true
..locale = 'en_CA' ..locale = 'en_CA'
..fontFamilies = <String>['Roboto', 'serif'] ..fontFamilies = <String>['Roboto', 'serif']
..fontStyle = (SkFontStyle() ..fontStyle = (SkFontStyle()
@@ -1310,6 +1311,7 @@ void _paragraphTests() {
..weight = canvasKit.FontWeight.Bold) ..weight = canvasKit.FontWeight.Bold)
..fontSize = 23 ..fontSize = 23
..heightMultiplier = 5 ..heightMultiplier = 5
..halfLeading = true
..leading = 6 ..leading = 6
..strutEnabled = true ..strutEnabled = true
..forceStrutHeight = false; ..forceStrutHeight = false;
@@ -1338,6 +1340,8 @@ void _paragraphTests() {
SkPaint()); SkPaint());
builder.addText('!'); builder.addText('!');
builder.pop(); builder.pop();
builder.pushStyle(canvasKit.TextStyle(SkTextStyleProperties()..halfLeading = true));
builder.pop();
final SkParagraph paragraph = builder.build(); final SkParagraph paragraph = builder.build();
paragraph.layout(55); paragraph.layout(55);
expect(paragraph.getAlphabeticBaseline(), expect(paragraph.getAlphabeticBaseline(),