Skip to content

Commit 327c15e

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[canvaskit] Add renderSoftHyphens to ParagraphStyle
Exposes skparagraph's ParagraphStyle::setRenderSoftHyphens so CanvasKit clients can draw a hyphen at line breaks that fall on a soft hyphen (U+00AD). Defaults to false, matching skparagraph. Flutter web needs this to support its Hyphens API on the CanvasKit renderer. Bug: flutter/flutter#193506 Change-Id: I7b8f780cbff27498c607a9a58610451f819ccd1d Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1378836 Reviewed-by: Florin Malita <[email protected]> Auto-Submit: David H Bebawy <[email protected]> Commit-Queue: Julia Lavrova <[email protected]> Reviewed-by: Julia Lavrova <[email protected]>
1 parent 5290584 commit 327c15e

5 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎modules/canvaskit/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- `CanvasKit.MakeCanvasFromSurface` wraps an existing `Surface` in the Canvas2D
1111
emulation layer, allowing Canvas2D to render on a GPU-backed surface.
12+
- `ParagraphStyle.renderSoftHyphens` draws a hyphen at line breaks that fall on a
13+
soft hyphen (U+00AD). Defaults to false.
1214

1315
## [0.42.0] - 2026-08-18
1416

‎modules/canvaskit/npm_build/types/index.d.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,7 @@ export interface ParagraphStyle {
13101310
textHeightBehavior?: TextHeightBehavior;
13111311
textStyle?: TextStyle;
13121312
applyRoundingHack?: boolean;
1313+
renderSoftHyphens?: boolean;
13131314
}
13141315

13151316
export interface PositionWithAffinity {

‎modules/canvaskit/paragraph.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
s['textHeightBehavior'] = s['textHeightBehavior'] || CanvasKit.TextHeightBehavior.All;
9696
s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
9797
s['applyRoundingHack'] = s['applyRoundingHack'] !== false;
98+
s['renderSoftHyphens'] = s['renderSoftHyphens'] || false;
9899
return s;
99100
};
100101

‎modules/canvaskit/paragraph_bindings.cpp‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ struct SimpleParagraphStyle {
267267
SimpleTextStyle textStyle;
268268
SimpleStrutStyle strutStyle;
269269
bool applyRoundingHack;
270+
bool renderSoftHyphens;
270271
};
271272

272273
para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
@@ -295,6 +296,7 @@ para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
295296
ps.setApplyRoundingHack(s.applyRoundingHack);
296297
ps.setTextHeightBehavior(s.textHeightBehavior);
297298
ps.setReplaceTabCharacters(s.replaceTabCharacters);
299+
ps.setRenderSoftHyphens(s.renderSoftHyphens);
298300
return ps;
299301
}
300302

@@ -803,7 +805,8 @@ EMSCRIPTEN_BINDINGS(Paragraph) {
803805
.field("textHeightBehavior", &SimpleParagraphStyle::textHeightBehavior)
804806
.field("textStyle", &SimpleParagraphStyle::textStyle)
805807
.field("strutStyle", &SimpleParagraphStyle::strutStyle)
806-
.field("applyRoundingHack", &SimpleParagraphStyle::applyRoundingHack);
808+
.field("applyRoundingHack", &SimpleParagraphStyle::applyRoundingHack)
809+
.field("renderSoftHyphens", &SimpleParagraphStyle::renderSoftHyphens);
807810

808811
value_object<SimpleStrutStyle>("StrutStyle")
809812
.field("_fontFamiliesPtr", &SimpleStrutStyle::fontFamiliesPtr)

‎modules/canvaskit/tests/paragraph_test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,43 @@ describe('Paragraph Behavior', function() {
823823
expect(paraStyleOverride.applyRoundingHack).toEqual(false);
824824
});
825825

826+
it('paragraph_render_soft_hyphens', () => {
827+
if (!CanvasKit.Paragraph) {
828+
console.warn('Skipping test because not compiled with paragraph');
829+
return;
830+
}
831+
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
832+
const textStyle = {
833+
fontFamilies: ['Noto Serif'],
834+
fontSize: 20,
835+
};
836+
expect(new CanvasKit.ParagraphStyle({textStyle}).renderSoftHyphens).toEqual(false);
837+
838+
// Returns [line count, longest line] for 'abcd\u00ADe' laid out at the given width.
839+
const layout = (renderSoftHyphens, width) => {
840+
const paraStyle = new CanvasKit.ParagraphStyle({textStyle, renderSoftHyphens});
841+
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
842+
builder.addText('abcd\u00ADe');
843+
const paragraph = builder.build();
844+
paragraph.layout(width);
845+
const result = [paragraph.getLineMetrics().length, paragraph.getLongestLine()];
846+
paragraph.delete();
847+
builder.delete();
848+
return result;
849+
};
850+
851+
// Just narrower than the unbroken text, so the line breaks at the soft hyphen.
852+
const width = layout(false, 1000)[1] - 1;
853+
const [hiddenLines, hiddenWidth] = layout(false, width);
854+
const [renderedLines, renderedWidth] = layout(true, width);
855+
856+
expect(hiddenLines).toEqual(2);
857+
expect(renderedLines).toEqual(2);
858+
expect(renderedWidth).toBeGreaterThan(hiddenWidth);
859+
860+
fontMgr.delete();
861+
});
862+
826863
gm('paragraph_font_provider', (canvas) => {
827864
if (!CanvasKit.Paragraph) {
828865
console.warn('Skipping test because not compiled with paragraph');

0 commit comments

Comments
 (0)