From a8d52fe3d61d03f94221273ef49f95d84d17e71e Mon Sep 17 00:00:00 2001 From: Yasser Lahbibi Date: Mon, 23 Dec 2024 12:13:17 +0100 Subject: [PATCH] fix(wrapText): trime lines --- src/graphic/helper/parseText.ts | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/graphic/helper/parseText.ts b/src/graphic/helper/parseText.ts index 87a0bd1f..c56c8487 100644 --- a/src/graphic/helper/parseText.ts +++ b/src/graphic/helper/parseText.ts @@ -703,7 +703,9 @@ function wrapText( continue; } - const chWidth = getWidth(ch, font); + // Check if first char of line is empty + const firstCharIsEmpty = !line && ch === ' '; + const chWidth = firstCharIsEmpty ? 0 : getWidth(ch, font); const inWord = isBreakAll ? false : !isWordBreakChar(ch); if (!lines.length @@ -805,6 +807,15 @@ function wrapText( accumWidth += lastAccumWidth; } + // Remove last empty chars from each line. + const emptySpaceWidth = getWidth(' ', font); + for (var index = 0; index < lines.length; index++) { + lines[index] = lines[index].replace(/( )+$/, () => { + linesWidths[index] -= emptySpaceWidth; + return ''; + }) + } + return { // Accum width of last line accumWidth,