Skip to content

Commit

Permalink
fix(svg): fix loss of - in texts (#363)
Browse files Browse the repository at this point in the history
  • Loading branch information
schoero authored Jan 11, 2022
1 parent cfeb89a commit b46d83b
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/svg/svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -594,7 +594,7 @@ export class SVG_ {

private _fitTextToWidth(text: string, lengthInPixel: number, maxLines: number, size: "8pt" | "10pt"): Array<string> {

const remainder = text.split(/ |-/g);
const remainder = text.split(/([ |-])/g);
let lines: Array<string> = [];
let currentLine = "";

Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -645,15 +648,15 @@ 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;

}


private _elipsis(text: string, lengthInPixel: number, size: "8pt" | "10pt"): string {
private _ellipsis(text: string, lengthInPixel: number, size: "8pt" | "10pt"): string {

let result = "";

Expand Down

0 comments on commit b46d83b

Please sign in to comment.