From 04c82165b1c7bb1c4a15484aa423f6dbcf5b646f Mon Sep 17 00:00:00 2001 From: weiyuhuang Date: Tue, 20 Apr 2021 10:55:49 -0700 Subject: [PATCH] [canvaskit] Add halfLeading to TextStyle and StrutStyle - Add halfLeading (introduced in https://skia-review.googlesource.com/c/skia/+/394969) to canvaskit bindings - Fix wrong initial value of fDescent Change-Id: I01d522cd5a3761f32426038dce2ab82f24c9c529 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/398276 Reviewed-by: Kevin Lubick Commit-Queue: Kevin Lubick --- modules/canvaskit/CHANGELOG.md | 1 + modules/canvaskit/npm_build/types/index.d.ts | 2 + modules/canvaskit/paragraph.js | 2 + modules/canvaskit/paragraph_bindings.cpp | 8 ++++ modules/canvaskit/tests/paragraph.spec.js | 41 ++++++++++++++++++++ modules/skparagraph/include/Metrics.h | 2 +- 6 files changed, 55 insertions(+), 1 deletion(-) diff --git a/modules/canvaskit/CHANGELOG.md b/modules/canvaskit/CHANGELOG.md index dd446cc420..9305265ffe 100644 --- a/modules/canvaskit/CHANGELOG.md +++ b/modules/canvaskit/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `CanvasKit.RuntimeEffect` now supports integer uniforms in the SkSL. These are still passed to `RuntimeEffect.makeShader` as floats (like all other uniforms), and will be converted to integers internally, to match the expectations of the shader. + - Add 'halfLeading' to `TextStyle` and `StrutStyle`. ### Removed - `Picture.saveAsFile()`, in favor of `Picture.serialize()` where clients can control how to diff --git a/modules/canvaskit/npm_build/types/index.d.ts b/modules/canvaskit/npm_build/types/index.d.ts index 161ab644c8..ddbe562818 100644 --- a/modules/canvaskit/npm_build/types/index.d.ts +++ b/modules/canvaskit/npm_build/types/index.d.ts @@ -2666,6 +2666,7 @@ export interface StrutStyle { fontStyle?: FontStyle; fontSize?: number; heightMultiplier?: number; + halfLeading?: boolean; leading?: number; forceStrutHeight?: boolean; } @@ -2697,6 +2698,7 @@ export interface TextStyle { fontStyle?: FontStyle; foregroundColor?: InputColor; heightMultiplier?: number; + halfLeading?: boolean; letterSpacing?: number; locale?: string; shadows?: TextShadow[]; diff --git a/modules/canvaskit/paragraph.js b/modules/canvaskit/paragraph.js index ec94746c81..0e3c183327 100644 --- a/modules/canvaskit/paragraph.js +++ b/modules/canvaskit/paragraph.js @@ -101,6 +101,7 @@ s['fontStyle'] = fontStyle(s['fontStyle']); s['fontSize'] = s['fontSize'] || 0; s['heightMultiplier'] = s['heightMultiplier'] || 0; + s['halfLeading'] = s['halfLeading'] || false; s['leading'] = s['leading'] || 0; s['forceStrutHeight'] = s['forceStrutHeight'] || false; return s; @@ -120,6 +121,7 @@ s['letterSpacing'] = s['letterSpacing'] || 0; s['wordSpacing'] = s['wordSpacing'] || 0; s['heightMultiplier'] = s['heightMultiplier'] || 0; + s['halfLeading'] = s['halfLeading'] || false; if (s['locale']) { var str = s['locale']; s['_localePtr'] = cacheOrCopyString(str); diff --git a/modules/canvaskit/paragraph_bindings.cpp b/modules/canvaskit/paragraph_bindings.cpp index 863b6ed294..3b19cb733e 100644 --- a/modules/canvaskit/paragraph_bindings.cpp +++ b/modules/canvaskit/paragraph_bindings.cpp @@ -52,6 +52,7 @@ struct SimpleTextStyle { SkScalar letterSpacing; SkScalar wordSpacing; SkScalar heightMultiplier; + bool halfLeading; uintptr_t /* const char* */ localePtr; int localeLen; SimpleFontStyle fontStyle; @@ -75,6 +76,7 @@ struct SimpleStrutStyle { SimpleFontStyle fontStyle; SkScalar fontSize; SkScalar heightMultiplier; + bool halfLeading; SkScalar leading; bool strutEnabled; bool forceStrutHeight; @@ -102,6 +104,8 @@ para::StrutStyle toStrutStyle(const SimpleStrutStyle& s) { ss.setHeight(s.heightMultiplier); ss.setHeightOverride(true); } + ss.setHalfLeading(s.halfLeading); + if (s.leading != 0) { ss.setLeading(s.leading); } @@ -147,6 +151,8 @@ para::TextStyle toTextStyle(const SimpleTextStyle& s) { ts.setHeightOverride(true); } + ts.setHalfLeading(s.halfLeading); + ts.setDecoration(para::TextDecoration(s.decoration)); ts.setDecorationStyle(s.decorationStyle); if (s.decorationThickness != 0) { @@ -475,6 +481,7 @@ EMSCRIPTEN_BINDINGS(Paragraph) { .field("fontSize", &SimpleStrutStyle::fontSize) .field("fontStyle", &SimpleStrutStyle::fontStyle) .field("heightMultiplier", &SimpleStrutStyle::heightMultiplier) + .field("halfLeading", &SimpleStrutStyle::halfLeading) .field("leading", &SimpleStrutStyle::leading) .field("forceStrutHeight", &SimpleStrutStyle::forceStrutHeight); @@ -492,6 +499,7 @@ EMSCRIPTEN_BINDINGS(Paragraph) { .field("letterSpacing", &SimpleTextStyle::letterSpacing) .field("wordSpacing", &SimpleTextStyle::wordSpacing) .field("heightMultiplier", &SimpleTextStyle::heightMultiplier) + .field("halfLeading", &SimpleTextStyle::halfLeading) .field("_localePtr", &SimpleTextStyle::localePtr) .field("_localeLen", &SimpleTextStyle::localeLen) .field("fontStyle", &SimpleTextStyle::fontStyle) diff --git a/modules/canvaskit/tests/paragraph.spec.js b/modules/canvaskit/tests/paragraph.spec.js index 85360d5070..66a53191c9 100644 --- a/modules/canvaskit/tests/paragraph.spec.js +++ b/modules/canvaskit/tests/paragraph.spec.js @@ -805,6 +805,47 @@ describe('Paragraph Behavior', function() { builder.delete(); }); + gm('paragraph_text_styles_mixed_leading_distribution', (canvas) => { + const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer); + expect(fontMgr.countFamilies()).toEqual(1); + expect(fontMgr.getFamilyName(0)).toEqual('Noto Serif'); + + const wrapTo = 200; + + const paraStyle = new CanvasKit.ParagraphStyle({ + textStyle: { + color: CanvasKit.BLACK, + backgroundColor: CanvasKit.Color(234, 208, 232), // light pink + fontFamilies: ['Noto Serif'], + fontSize: 10, + heightMultiplier: 10, + }, + }); + + const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr); + builder.addText('Not half leading'); + + const halfLeadingText = new CanvasKit.TextStyle({ + color: CanvasKit.Color(48, 37, 199), + backgroundColor: CanvasKit.Color(234, 208, 232), // light pink + fontFamilies: ['Noto Serif'], + fontSize: 10, + heightMultiplier: 10, + halfLeading: true, + }); + builder.pushStyle(halfLeadingText); + builder.addText('Half Leading Text'); + const paragraph = builder.build(); + + paragraph.layout(wrapTo); + canvas.clear(CanvasKit.WHITE); + canvas.drawParagraph(paragraph, 0, 0); + + fontMgr.delete(); + paragraph.delete(); + builder.delete(); + }); + it('should not crash if we omit font family on pushed textStyle', () => { const surface = CanvasKit.MakeCanvasSurface('test'); expect(surface).toBeTruthy('Could not make surface'); diff --git a/modules/skparagraph/include/Metrics.h b/modules/skparagraph/include/Metrics.h index f754d5b90c..4b8581ef7f 100644 --- a/modules/skparagraph/include/Metrics.h +++ b/modules/skparagraph/include/Metrics.h @@ -70,7 +70,7 @@ public: // for specific runs of text can be obtained in run_metrics_map. These values // are the cumulative metrics for the entire line. double fAscent = SK_ScalarMax; - double fDescent = SK_ScalarMax; + double fDescent = SK_ScalarMin; double fUnscaledAscent = SK_ScalarMax; // Total height of the paragraph including the current line. //