Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/core/text-rendering/TextLayoutEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ export const wrapLine = (
): WrappedLinesStruct => {
const words = line.split(spaceRegex);
const spaces = line.match(spaceRegex) || [];
// Leading whitespace splits into an empty token; fold it into the next
// word so it isn't dropped and re-applied after the first word instead.
if (words[0] === '' && words.length > 1) {
const leadingSpace = spaces.shift() ?? '';
words.shift();
words[0] = leadingSpace + (words[0] ?? '');
}
const wrappedLines: TextLineStruct[] = [];
let currentLine = '';
let currentLineWidth = 0;
Expand Down
22 changes: 22 additions & 0 deletions src/core/text-rendering/tests/TextLayoutEngine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ describe('SDF Text Utils', () => {
expect(result[0][0]?.[2]).toBe(true);
expect(result[1]).toBe(0);
});

it('should keep leading whitespace attached to the first word, not the word after it', () => {
// Regression test: a line starting with padding spaces (used e.g. to
// reserve room for an inline badge before the first word) used to have
// those spaces silently reassigned as the gap AFTER the first word.
const result = wrapLine(
testMeasureText,
' hello world',
'Arial',
1000,
0,
10, // spaceWidth
'',
0,
'break-word',
10,
);

const [lines] = result;
expect(lines).toHaveLength(1);
expect(lines[0]?.[0]).toBe(' hello world');
});
});

describe('wrapText', () => {
Expand Down
Loading