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,