未验证 提交 2db8af2d 编写于 作者: H Harry Terkelsen 提交者: GitHub

[canvaskit] Implement TextHeightBehavior (#25734)

* Add TextHeightBehavior

* Remove TODO

* Update goldens lock

* Update goldens_lock
上级 939df110
repository: https://github.com/flutter/goldens.git repository: https://github.com/flutter/goldens.git
revision: 449cb88e9ffed446e4e7df9beb73814e5fb11ead revision: 8d866a84b7ceb67412ce6767ea0c1eec2eb3fa3f
...@@ -42,6 +42,7 @@ class CanvasKit { ...@@ -42,6 +42,7 @@ class CanvasKit {
external SkRectWidthStyleEnum get RectWidthStyle; external SkRectWidthStyleEnum get RectWidthStyle;
external SkAffinityEnum get Affinity; external SkAffinityEnum get Affinity;
external SkTextAlignEnum get TextAlign; external SkTextAlignEnum get TextAlign;
external SkTextHeightBehaviorEnum get TextHeightBehavior;
external SkTextDirectionEnum get TextDirection; external SkTextDirectionEnum get TextDirection;
external SkFontWeightEnum get FontWeight; external SkFontWeightEnum get FontWeight;
external SkFontSlantEnum get FontSlant; external SkFontSlantEnum get FontSlant;
...@@ -290,6 +291,33 @@ SkTextAlign toSkTextAlign(ui.TextAlign align) { ...@@ -290,6 +291,33 @@ SkTextAlign toSkTextAlign(ui.TextAlign align) {
return _skTextAligns[align.index]; return _skTextAligns[align.index];
} }
@JS()
class SkTextHeightBehaviorEnum {
external SkTextHeightBehavior get All;
external SkTextHeightBehavior get DisableFirstAscent;
external SkTextHeightBehavior get DisableLastDescent;
external SkTextHeightBehavior get DisableAll;
}
@JS()
class SkTextHeightBehavior {
external int get value;
}
final List<SkTextHeightBehavior> _skTextHeightBehaviors =
<SkTextHeightBehavior>[
canvasKit.TextHeightBehavior.All,
canvasKit.TextHeightBehavior.DisableFirstAscent,
canvasKit.TextHeightBehavior.DisableLastDescent,
canvasKit.TextHeightBehavior.DisableAll,
];
SkTextHeightBehavior toSkTextHeightBehavior(ui.TextHeightBehavior behavior) {
int index = (behavior.applyHeightToFirstAscent ? 0 : 1 << 0) |
(behavior.applyHeightToLastDescent ? 0 : 1 << 1);
return _skTextHeightBehaviors[index];
}
@JS() @JS()
class SkRectHeightStyleEnum { class SkRectHeightStyleEnum {
// TODO(yjbanov): support all styles // TODO(yjbanov): support all styles
...@@ -1531,7 +1559,7 @@ class SkParagraphStyleProperties { ...@@ -1531,7 +1559,7 @@ class SkParagraphStyleProperties {
external set textAlign(SkTextAlign? value); external set textAlign(SkTextAlign? value);
external set textDirection(SkTextDirection? value); external set textDirection(SkTextDirection? value);
external set heightMultiplier(double? value); external set heightMultiplier(double? value);
external set textHeightBehavior(int? value); external set textHeightBehavior(SkTextHeightBehavior? value);
external set maxLines(int? value); external set maxLines(int? value);
external set ellipsis(String? value); external set ellipsis(String? value);
external set textStyle(SkTextStyleProperties? value); external set textStyle(SkTextStyleProperties? value);
......
...@@ -139,8 +139,7 @@ class CkParagraphStyle implements ui.ParagraphStyle { ...@@ -139,8 +139,7 @@ class CkParagraphStyle implements ui.ParagraphStyle {
if (textHeightBehavior != null) { if (textHeightBehavior != null) {
properties.textHeightBehavior = properties.textHeightBehavior =
(textHeightBehavior.applyHeightToFirstAscent ? 0 : 1 << 0) | toSkTextHeightBehavior(textHeightBehavior);
(textHeightBehavior.applyHeightToLastDescent ? 0 : 1 << 1);
} }
if (ellipsis != null) { if (ellipsis != null) {
...@@ -151,8 +150,8 @@ class CkParagraphStyle implements ui.ParagraphStyle { ...@@ -151,8 +150,8 @@ class CkParagraphStyle implements ui.ParagraphStyle {
properties.strutStyle = toSkStrutStyleProperties(strutStyle); properties.strutStyle = toSkStrutStyleProperties(strutStyle);
} }
properties.textStyle = properties.textStyle = toSkTextStyleProperties(
toSkTextStyleProperties(fontFamily, fontSize, height, fontWeight, fontStyle); fontFamily, fontSize, height, fontWeight, fontStyle);
return canvasKit.ParagraphStyle(properties); return canvasKit.ParagraphStyle(properties);
} }
......
...@@ -262,8 +262,6 @@ void testMain() { ...@@ -262,8 +262,6 @@ void testMain() {
await testTextStyle('paragraph height', layoutWidth: 50, paragraphHeight: 1.5); await testTextStyle('paragraph height', layoutWidth: 50, paragraphHeight: 1.5);
}); });
// TODO(yjbanov): paragraphTextHeightBehavior seems to have no effect. Unsure how to use it.
// https://github.com/flutter/flutter/issues/74337
test('text styles - paragraph text height behavior', () async { test('text styles - paragraph text height behavior', () async {
await testTextStyle('paragraph text height behavior', layoutWidth: 50, paragraphHeight: 1.5, paragraphTextHeightBehavior: ui.TextHeightBehavior( await testTextStyle('paragraph text height behavior', layoutWidth: 50, paragraphHeight: 1.5, paragraphTextHeightBehavior: ui.TextHeightBehavior(
applyHeightToFirstAscent: false, applyHeightToFirstAscent: false,
......
...@@ -996,7 +996,7 @@ void _canvasTests() { ...@@ -996,7 +996,7 @@ void _canvasTests() {
test('drawImageCubic', () { test('drawImageCubic', () {
final SkAnimatedImage image = final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage); canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageCubic( canvas.drawImageCubic(
image.getCurrentFrame(), image.getCurrentFrame(),
10, 10,
...@@ -1009,7 +1009,7 @@ void _canvasTests() { ...@@ -1009,7 +1009,7 @@ void _canvasTests() {
test('drawImageRectOptions', () { test('drawImageRectOptions', () {
final SkAnimatedImage image = final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage); canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageRectOptions( canvas.drawImageRectOptions(
image.getCurrentFrame(), image.getCurrentFrame(),
Float32List.fromList([0, 0, 1, 1]), Float32List.fromList([0, 0, 1, 1]),
...@@ -1022,7 +1022,7 @@ void _canvasTests() { ...@@ -1022,7 +1022,7 @@ void _canvasTests() {
test('drawImageRectCubic', () { test('drawImageRectCubic', () {
final SkAnimatedImage image = final SkAnimatedImage image =
canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage); canvasKit.MakeAnimatedImageFromEncoded(kTransparentImage);
canvas.drawImageRectCubic( canvas.drawImageRectCubic(
image.getCurrentFrame(), image.getCurrentFrame(),
Float32List.fromList([0, 0, 1, 1]), Float32List.fromList([0, 0, 1, 1]),
...@@ -1273,7 +1273,7 @@ void _paragraphTests() { ...@@ -1273,7 +1273,7 @@ void _paragraphTests() {
props.textAlign = canvasKit.TextAlign.Center; props.textAlign = canvasKit.TextAlign.Center;
props.textDirection = canvasKit.TextDirection.RTL; props.textDirection = canvasKit.TextDirection.RTL;
props.heightMultiplier = 3; props.heightMultiplier = 3;
props.textHeightBehavior = 0; props.textHeightBehavior = canvasKit.TextHeightBehavior.All;
props.maxLines = 4; props.maxLines = 4;
props.ellipsis = '___'; props.ellipsis = '___';
props.textStyle = SkTextStyleProperties() props.textStyle = SkTextStyleProperties()
...@@ -1389,4 +1389,35 @@ void _paragraphTests() { ...@@ -1389,4 +1389,35 @@ void _paragraphTests() {
paragraph.delete(); paragraph.delete();
}); });
test('TextHeightBehavior', () {
expect(
toSkTextHeightBehavior(ui.TextHeightBehavior(
applyHeightToFirstAscent: true,
applyHeightToLastDescent: true,
)),
canvasKit.TextHeightBehavior.All,
);
expect(
toSkTextHeightBehavior(ui.TextHeightBehavior(
applyHeightToFirstAscent: false,
applyHeightToLastDescent: true,
)),
canvasKit.TextHeightBehavior.DisableFirstAscent,
);
expect(
toSkTextHeightBehavior(ui.TextHeightBehavior(
applyHeightToFirstAscent: true,
applyHeightToLastDescent: false,
)),
canvasKit.TextHeightBehavior.DisableLastDescent,
);
expect(
toSkTextHeightBehavior(ui.TextHeightBehavior(
applyHeightToFirstAscent: false,
applyHeightToLastDescent: false,
)),
canvasKit.TextHeightBehavior.DisableAll,
);
});
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册