Merge remote-tracking branch 'origin/master' into danieljgeiger-mathjax

This commit is contained in:
Daniel J. Geiger 2023-01-26 17:38:48 -06:00
commit 512e506798
2 changed files with 85 additions and 73 deletions

View File

@ -12,6 +12,20 @@ describe("Test wrapText", () => {
expect(res).toBe("Hello whats up ");
});
it("should work with emojis", () => {
const text = "😀";
const maxWidth = 1;
const res = wrapText(text, font, maxWidth);
expect(res).toBe("😀");
});
it("should show the text correctly when min width reached", () => {
const text = "Hello😀";
const maxWidth = 10;
const res = wrapText(text, font, maxWidth);
expect(res).toBe("H\ne\nl\nl\no\n😀");
});
describe("When text doesn't contain new lines", () => {
const text = "Hello whats up";
[

View File

@ -390,7 +390,8 @@ 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 {
return; // continue
}
let currentLine = "";
let currentLineWidthTillNow = 0;
@ -406,10 +407,12 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
currentLine = "";
currentLineWidthTillNow = 0;
while (words[index].length > 0) {
const currentChar = words[index][0];
const currentChar = String.fromCodePoint(
words[index].codePointAt(0)!,
);
const width = charWidth.calculate(currentChar, font);
currentLineWidthTillNow += width;
words[index] = words[index].slice(1);
words[index] = words[index].slice(currentChar.length);
if (currentLineWidthTillNow >= maxWidth) {
// only remove last trailing space which we have added when joining words
@ -419,10 +422,6 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
push(currentLine);
currentLine = currentChar;
currentLineWidthTillNow = width;
if (currentLineWidthTillNow === maxWidth) {
currentLine = "";
currentLineWidthTillNow = 0;
}
} else {
currentLine += currentChar;
}
@ -479,7 +478,6 @@ export const wrapText = (text: string, font: FontString, maxWidth: number) => {
}
push(currentLine);
}
}
});
return lines.join("\n");
};