From b46d83b83698655d921162ec7b28f49f9933b89b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roger=20Sch=C3=B6nb=C3=A4chler?= <42278642+schoero@users.noreply.github.com> Date: Tue, 11 Jan 2022 22:14:48 +0100 Subject: [PATCH] fix(svg): fix loss of `-` in texts (#363) --- src/svg/svg.ts | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/svg/svg.ts b/src/svg/svg.ts index 12805d0..0ae0c78 100644 --- a/src/svg/svg.ts +++ b/src/svg/svg.ts @@ -430,7 +430,7 @@ export class SVG_ { // QRR and SCOR have 1 line for the message and 2 lines for the additional information if(this._data.message !== undefined){ - paymentPartRightTextContainer.addTSpan(this._elipsis(this._data.message, lengthInPixel, "10pt")) + paymentPartRightTextContainer.addTSpan(this._ellipsis(this._data.message, lengthInPixel, "10pt")) .x(0) .dy("11pt") .fontFamily("Arial") @@ -594,7 +594,7 @@ export class SVG_ { private _fitTextToWidth(text: string, lengthInPixel: number, maxLines: number, size: "8pt" | "10pt"): Array { - const remainder = text.split(/ |-/g); + const remainder = text.split(/([ |-])/g); let lines: Array = []; let currentLine = ""; @@ -622,16 +622,19 @@ export class SVG_ { }; while(remainder.length > 0){ - const nextWord = remainder.shift() + " "; - if(calculateTextWidth(currentLine + nextWord, size) <= lengthInPixel){ - currentLine += nextWord; + + const nextWord = remainder.shift()!; + const separator = remainder.shift() ?? ""; + + if(calculateTextWidth(currentLine + nextWord + separator, size) <= lengthInPixel){ + currentLine += nextWord + separator; } else { if(currentLine !== ""){ const { lines: newLines, leftover } = checkCurrentLine(currentLine); lines.push(...newLines); - currentLine = leftover + nextWord; + currentLine = leftover + nextWord + separator; } else { - currentLine = nextWord; + currentLine = nextWord + separator; } } } @@ -645,7 +648,7 @@ export class SVG_ { if(lines.length > maxLines){ lines = lines.slice(0, maxLines); - lines[lines.length - 1] = this._elipsis(lines[lines.length - 1], lengthInPixel, size); + lines[lines.length - 1] = this._ellipsis(lines[lines.length - 1], lengthInPixel, size); } return lines; @@ -653,7 +656,7 @@ export class SVG_ { } - private _elipsis(text: string, lengthInPixel: number, size: "8pt" | "10pt"): string { + private _ellipsis(text: string, lengthInPixel: number, size: "8pt" | "10pt"): string { let result = "";