Skip to content

Commit 1bd2b06

Browse files
dbebawyskcq-be@skia-corp.google.com.iam.gserviceaccount.com
authored andcommitted
[canvaskit] Add softHyphenString to ParagraphStyle
Exposes skparagraph's ParagraphStyle::setSoftHyphenString so CanvasKit clients can choose the string drawn at line breaks that fall on a soft hyphen (U+00AD) when renderSoftHyphens is true. Leaving it unset keeps skparagraph's default "-"; an empty string draws nothing. Flutter web needs this to support customizing the soft hyphen on the CanvasKit renderer. [email protected], [email protected] Bug: flutter/flutter#189617 Change-Id: I570bf61584e05de8b7ff0cbeba766ff51a5409eb Reviewed-on: https://skia-review.googlesource.com/c/skia/+/1404496 Commit-Queue: Florin Malita <[email protected]> Reviewed-by: Julia Lavrova <[email protected]> Reviewed-by: Florin Malita <[email protected]>
1 parent 6961f82 commit 1bd2b06

5 files changed

Lines changed: 73 additions & 1 deletion

File tree

‎modules/canvaskit/CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
emulation layer, allowing Canvas2D to render on a GPU-backed surface.
1212
- `ParagraphStyle.renderSoftHyphens` draws a hyphen at line breaks that fall on a
1313
soft hyphen (U+00AD). Defaults to false.
14+
- `ParagraphStyle.softHyphenString` sets the string drawn at those line breaks.
15+
Defaults to "-". An empty string draws nothing.
1416

1517
## [0.42.0] - 2026-08-18
1618

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,11 @@ export interface ParagraphStyle {
13111311
textStyle?: TextStyle;
13121312
applyRoundingHack?: boolean;
13131313
renderSoftHyphens?: boolean;
1314+
/**
1315+
* The string drawn at line breaks that fall on a soft hyphen, when renderSoftHyphens is
1316+
* true. Defaults to "-"; an empty string draws nothing.
1317+
*/
1318+
softHyphenString?: string;
13141319
}
13151320

13161321
export interface PositionWithAffinity {

‎modules/canvaskit/paragraph.js‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@
9696
s['textStyle'] = CanvasKit.TextStyle(s['textStyle']);
9797
s['applyRoundingHack'] = s['applyRoundingHack'] !== false;
9898
s['renderSoftHyphens'] = s['renderSoftHyphens'] || false;
99+
// Unlike the ellipsis, an empty string is meaningful here (draw no hyphen), so only a
100+
// missing value keeps skparagraph's default.
101+
if (typeof s['softHyphenString'] === 'string') {
102+
var hyphen = s['softHyphenString'];
103+
s['_softHyphenStringPtr'] = cacheOrCopyString(hyphen);
104+
s['_softHyphenStringLen'] = lengthBytesUTF8(hyphen);
105+
} else {
106+
s['_softHyphenStringPtr'] = nullptr;
107+
s['_softHyphenStringLen'] = 0;
108+
}
99109
return s;
100110
};
101111

‎modules/canvaskit/paragraph_bindings.cpp‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,9 @@ struct SimpleParagraphStyle {
268268
SimpleStrutStyle strutStyle;
269269
bool applyRoundingHack;
270270
bool renderSoftHyphens;
271+
// Null unless softHyphenString was given; an empty string is not null.
272+
WASMPointerU8 softHyphenStringPtr;
273+
size_t softHyphenStringLen;
271274
};
272275

273276
para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
@@ -297,6 +300,10 @@ para::ParagraphStyle toParagraphStyle(const SimpleParagraphStyle& s) {
297300
ps.setTextHeightBehavior(s.textHeightBehavior);
298301
ps.setReplaceTabCharacters(s.replaceTabCharacters);
299302
ps.setRenderSoftHyphens(s.renderSoftHyphens);
303+
if (s.softHyphenStringPtr) {
304+
const char* softHyphenPtr = reinterpret_cast<const char*>(s.softHyphenStringPtr);
305+
ps.setSoftHyphenString(SkString(softHyphenPtr, s.softHyphenStringLen));
306+
}
300307
return ps;
301308
}
302309

@@ -806,7 +813,9 @@ EMSCRIPTEN_BINDINGS(Paragraph) {
806813
.field("textStyle", &SimpleParagraphStyle::textStyle)
807814
.field("strutStyle", &SimpleParagraphStyle::strutStyle)
808815
.field("applyRoundingHack", &SimpleParagraphStyle::applyRoundingHack)
809-
.field("renderSoftHyphens", &SimpleParagraphStyle::renderSoftHyphens);
816+
.field("renderSoftHyphens", &SimpleParagraphStyle::renderSoftHyphens)
817+
.field("_softHyphenStringPtr", &SimpleParagraphStyle::softHyphenStringPtr)
818+
.field("_softHyphenStringLen", &SimpleParagraphStyle::softHyphenStringLen);
810819

811820
value_object<SimpleStrutStyle>("StrutStyle")
812821
.field("_fontFamiliesPtr", &SimpleStrutStyle::fontFamiliesPtr)

‎modules/canvaskit/tests/paragraph_test.js‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -860,6 +860,52 @@ describe('Paragraph Behavior', function() {
860860
fontMgr.delete();
861861
});
862862

863+
it('paragraph_soft_hyphen_string', () => {
864+
if (!CanvasKit.Paragraph) {
865+
console.warn('Skipping test because not compiled with paragraph');
866+
return;
867+
}
868+
const fontMgr = CanvasKit.FontMgr.FromData(notoSerifFontBuffer);
869+
const textStyle = {
870+
fontFamilies: ['Noto Serif'],
871+
fontSize: 20,
872+
};
873+
874+
// Returns [line count, longest line] for 'abcd\u00ADe' laid out at the given width.
875+
const layout = (paraStyleOverrides, width) => {
876+
const paraStyle = new CanvasKit.ParagraphStyle(
877+
Object.assign({textStyle, renderSoftHyphens: true}, paraStyleOverrides));
878+
const builder = CanvasKit.ParagraphBuilder.Make(paraStyle, fontMgr);
879+
builder.addText('abcd\u00ADe');
880+
const paragraph = builder.build();
881+
paragraph.layout(width);
882+
const result = [paragraph.getLineMetrics().length, paragraph.getLongestLine()];
883+
paragraph.delete();
884+
builder.delete();
885+
return result;
886+
};
887+
888+
// Just narrower than the unbroken text, so the line breaks at the soft hyphen.
889+
const width = layout({renderSoftHyphens: false}, 1000)[1] - 1;
890+
const [, hiddenWidth] = layout({renderSoftHyphens: false}, width);
891+
const [defaultLines, defaultWidth] = layout({}, width);
892+
const [dashLines, dashWidth] = layout({softHyphenString: '-'}, width);
893+
const [doubleLines, doubleWidth] = layout({softHyphenString: '--'}, width);
894+
const [emptyLines, emptyWidth] = layout({softHyphenString: ''}, width);
895+
896+
expect(defaultLines).toEqual(2);
897+
expect(dashLines).toEqual(2);
898+
expect(doubleLines).toEqual(2);
899+
expect(emptyLines).toEqual(2);
900+
// Without softHyphenString, the hyphen is the default '-'.
901+
expect(defaultWidth).toBeCloseTo(dashWidth, 3);
902+
expect(doubleWidth).toBeGreaterThan(dashWidth);
903+
// An empty string draws no hyphen.
904+
expect(emptyWidth).toBeCloseTo(hiddenWidth, 3);
905+
906+
fontMgr.delete();
907+
});
908+
863909
gm('paragraph_font_provider', (canvas) => {
864910
if (!CanvasKit.Paragraph) {
865911
console.warn('Skipping test because not compiled with paragraph');

0 commit comments

Comments
 (0)