Skip to content

Commit 919cb53

Browse files
authored
Correct width of text ending with space (#491)
2 parents a74b092 + cb655ea commit 919cb53

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

examples/tests/text-dimensions.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ export default async function test(settings: ExampleSettings) {
102102
text1.text = 'SDF 2nd';
103103
text1.textRendererOverride = 'sdf';
104104
},
105+
() => {
106+
// Test when text ends with space for correct width
107+
text1.text = 'Canvas ';
108+
text1.textRendererOverride = 'canvas';
109+
},
110+
() => {
111+
// Test when text ends with space for correct width
112+
text1.text = 'SDF ';
113+
text1.textRendererOverride = 'sdf';
114+
},
105115
];
106116
/**
107117
* Run the next mutation in the list

src/core/text-rendering/renderers/SdfTextRenderer/internal/layoutText.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,18 @@ export function layoutText(
121121
xStart: -1,
122122
};
123123

124-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
125124
const shaper = trFontFace.shaper;
126125

127126
const shaperProps: FontShaperProps = {
128127
letterSpacing: vertexLSpacing,
129128
};
130129

130+
// HACK: The space is used as a word boundary. When a text ends with a space, we need to
131+
// add an extra space to ensure the space is included in the line width calculation.
132+
if (text.endsWith(' ')) {
133+
text += ' ';
134+
}
135+
131136
// Get glyphs
132137
let glyphs = shaper.shapeText(
133138
shaperProps,
2.14 KB
Loading
1.35 KB
Loading

0 commit comments

Comments
 (0)