Skip to content

Commit 49f7e61

Browse files
committed
resolved max lines issues and propely suffix last line if there's text remaining
1 parent 7a801cd commit 49f7e61

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

src/core/text-rendering/TextLayoutEngine.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ export const wrapText = (
179179
let hasMaxLines = maxLines > 0;
180180

181181
for (let i = 0; i < lines.length; i++) {
182-
const line = lines[i]!;
183-
182+
const line = lines[i];
183+
if (line === undefined || line.length === 0) {
184+
continue;
185+
}
184186
[wrappedLine, remainingLines, hasRemainingText] = wrapLine(
185187
measureText,
186188
line,
@@ -194,7 +196,25 @@ export const wrapText = (
194196
hasMaxLines,
195197
);
196198

199+
remainingLines--;
197200
wrappedLines.push(...wrappedLine);
201+
202+
if (hasMaxLines === true && remainingLines <= 0) {
203+
const lastLine = wrappedLines[wrappedLines.length - 1]!;
204+
if (i < lines.length - 1) {
205+
if (lastLine[0].endsWith(overflowSuffix) === false) {
206+
lastLine[0] = truncateLineWithSuffix(
207+
measureText,
208+
lastLine[0],
209+
fontFamily,
210+
maxWidth,
211+
letterSpacing,
212+
overflowSuffix,
213+
);
214+
}
215+
}
216+
break;
217+
}
198218
}
199219

200220
return [wrappedLines, remainingLines, hasRemainingText];
@@ -266,12 +286,10 @@ export const wrapLine = (
266286

267287
if (wordBreak !== 'break-all' && currentLine.length > 0) {
268288
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
269-
currentLine = '';
270-
currentLineWidth = 0;
271-
remainingLines--;
272289
}
273290

274291
if (wordBreak !== 'break-all') {
292+
remainingLines--;
275293
currentLine = word;
276294
currentLineWidth = wordWidth;
277295
}
@@ -343,6 +361,8 @@ export const wrapLine = (
343361
}
344362
}
345363

364+
console.log('remaining lines', remainingLines);
365+
346366
// Add the last line if it has content
347367
if (currentLine.length > 0 && hasMaxLines === true && remainingLines === 0) {
348368
currentLine = truncateLineWithSuffix(
@@ -357,8 +377,6 @@ export const wrapLine = (
357377

358378
if (currentLine.length > 0) {
359379
wrappedLines.push([currentLine, currentLineWidth, 0, 0]);
360-
} else {
361-
wrappedLines.push(['', 0, 0, 0]);
362380
}
363381
return [wrappedLines, remainingLines, hasRemainingText];
364382
};

0 commit comments

Comments
 (0)