|
|
|
@ -359,96 +359,94 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
|
|
|
|
|
// This means its newline so push it
|
|
|
|
|
if (words.length === 1 && words[0] === "") {
|
|
|
|
|
lines.push(words[0]);
|
|
|
|
|
} else {
|
|
|
|
|
let currentLine = "";
|
|
|
|
|
let currentLineWidthTillNow = 0;
|
|
|
|
|
return; // continue
|
|
|
|
|
}
|
|
|
|
|
let currentLine = "";
|
|
|
|
|
let currentLineWidthTillNow = 0;
|
|
|
|
|
|
|
|
|
|
let index = 0;
|
|
|
|
|
while (index < words.length) {
|
|
|
|
|
const currentWordWidth = getLineWidth(words[index], font);
|
|
|
|
|
let index = 0;
|
|
|
|
|
while (index < words.length) {
|
|
|
|
|
const currentWordWidth = getLineWidth(words[index], font);
|
|
|
|
|
|
|
|
|
|
// Start breaking longer words exceeding max width
|
|
|
|
|
if (currentWordWidth >= maxWidth) {
|
|
|
|
|
// push current line since the current word exceeds the max width
|
|
|
|
|
// so will be appended in next line
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
while (words[index].length > 0) {
|
|
|
|
|
const currentChar = words[index][0];
|
|
|
|
|
const width = charWidth.calculate(currentChar, font);
|
|
|
|
|
currentLineWidthTillNow += width;
|
|
|
|
|
words[index] = words[index].slice(1);
|
|
|
|
|
|
|
|
|
|
if (currentLineWidthTillNow >= maxWidth) {
|
|
|
|
|
// only remove last trailing space which we have added when joining words
|
|
|
|
|
if (currentLine.slice(-1) === " ") {
|
|
|
|
|
currentLine = currentLine.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLine = currentChar;
|
|
|
|
|
currentLineWidthTillNow = width;
|
|
|
|
|
if (currentLineWidthTillNow === maxWidth) {
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
currentLine += currentChar;
|
|
|
|
|
// Start breaking longer words exceeding max width
|
|
|
|
|
if (currentWordWidth >= maxWidth) {
|
|
|
|
|
// push current line since the current word exceeds the max width
|
|
|
|
|
// so will be appended in next line
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
while (words[index].length > 0) {
|
|
|
|
|
const currentChar = String.fromCodePoint(
|
|
|
|
|
words[index].codePointAt(0)!,
|
|
|
|
|
);
|
|
|
|
|
const width = charWidth.calculate(currentChar, font);
|
|
|
|
|
currentLineWidthTillNow += width;
|
|
|
|
|
words[index] = words[index].slice(currentChar.length);
|
|
|
|
|
|
|
|
|
|
if (currentLineWidthTillNow >= maxWidth) {
|
|
|
|
|
// only remove last trailing space which we have added when joining words
|
|
|
|
|
if (currentLine.slice(-1) === " ") {
|
|
|
|
|
currentLine = currentLine.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// push current line if appending space exceeds max width
|
|
|
|
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
currentLine = currentChar;
|
|
|
|
|
currentLineWidthTillNow = width;
|
|
|
|
|
} else {
|
|
|
|
|
// space needs to be appended before next word
|
|
|
|
|
// as currentLine contains chars which couldn't be appended
|
|
|
|
|
// to previous line
|
|
|
|
|
currentLine += " ";
|
|
|
|
|
currentLineWidthTillNow += spaceWidth;
|
|
|
|
|
currentLine += currentChar;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
// push current line if appending space exceeds max width
|
|
|
|
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
} else {
|
|
|
|
|
// Start appending words in a line till max width reached
|
|
|
|
|
while (currentLineWidthTillNow < maxWidth && index < words.length) {
|
|
|
|
|
const word = words[index];
|
|
|
|
|
currentLineWidthTillNow = getLineWidth(currentLine + word, font);
|
|
|
|
|
// space needs to be appended before next word
|
|
|
|
|
// as currentLine contains chars which couldn't be appended
|
|
|
|
|
// to previous line
|
|
|
|
|
currentLine += " ";
|
|
|
|
|
currentLineWidthTillNow += spaceWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (currentLineWidthTillNow >= maxWidth) {
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
currentLine = "";
|
|
|
|
|
index++;
|
|
|
|
|
} else {
|
|
|
|
|
// Start appending words in a line till max width reached
|
|
|
|
|
while (currentLineWidthTillNow < maxWidth && index < words.length) {
|
|
|
|
|
const word = words[index];
|
|
|
|
|
currentLineWidthTillNow = getLineWidth(currentLine + word, font);
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
index++;
|
|
|
|
|
currentLine += `${word} `;
|
|
|
|
|
|
|
|
|
|
// Push the word if appending space exceeds max width
|
|
|
|
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
|
|
|
const word = currentLine.slice(0, -1);
|
|
|
|
|
push(word);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (currentLineWidthTillNow >= maxWidth) {
|
|
|
|
|
push(currentLine);
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
currentLine = "";
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (currentLineWidthTillNow === maxWidth) {
|
|
|
|
|
index++;
|
|
|
|
|
currentLine += `${word} `;
|
|
|
|
|
|
|
|
|
|
// Push the word if appending space exceeds max width
|
|
|
|
|
if (currentLineWidthTillNow + spaceWidth >= maxWidth) {
|
|
|
|
|
const word = currentLine.slice(0, -1);
|
|
|
|
|
push(word);
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (currentLine) {
|
|
|
|
|
// only remove last trailing space which we have added when joining words
|
|
|
|
|
if (currentLine.slice(-1) === " ") {
|
|
|
|
|
currentLine = currentLine.slice(0, -1);
|
|
|
|
|
if (currentLineWidthTillNow === maxWidth) {
|
|
|
|
|
currentLine = "";
|
|
|
|
|
currentLineWidthTillNow = 0;
|
|
|
|
|
}
|
|
|
|
|
push(currentLine);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (currentLine) {
|
|
|
|
|
// only remove last trailing space which we have added when joining words
|
|
|
|
|
if (currentLine.slice(-1) === " ") {
|
|
|
|
|
currentLine = currentLine.slice(0, -1);
|
|
|
|
|
}
|
|
|
|
|
push(currentLine);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return lines.join("\n");
|
|
|
|
|
};
|
|
|
|
|