mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-17 16:19:12 +02:00
Handle linebreaks in wrapText
This commit is contained in:
@@ -213,7 +213,7 @@ module.exports = class TweetCommand extends Command {
|
|||||||
ctx.fillText(line, x, y + (23 * currentLine) + (9 * currentLine));
|
ctx.fillText(line, x, y + (23 * currentLine) + (9 * currentLine));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let currentX = x + 0;
|
let currentX = x;
|
||||||
for (let i = 0; i < lineNoEmoji.length; i++) {
|
for (let i = 0; i < lineNoEmoji.length; i++) {
|
||||||
const linePart = lineNoEmoji[i];
|
const linePart = lineNoEmoji[i];
|
||||||
ctx.fillText(linePart, currentX, y + (23 * currentLine) + (9 * currentLine));
|
ctx.fillText(linePart, currentX, y + (23 * currentLine) + (9 * currentLine));
|
||||||
|
|||||||
+23
-16
@@ -213,26 +213,33 @@ module.exports = class CanvasUtil {
|
|||||||
const words = text.split(' ');
|
const words = text.split(' ');
|
||||||
const lines = [];
|
const lines = [];
|
||||||
let line = '';
|
let line = '';
|
||||||
while (words.length > 0) {
|
for (let i = 0; i < words.length; i++) {
|
||||||
let split = false;
|
const word = words[i];
|
||||||
while (ctx.measureText(words[0]).width >= maxWidth) {
|
if (word.includes('\n')) {
|
||||||
const temp = words[0];
|
const parts = word.split('\n');
|
||||||
words[0] = temp.slice(0, -1);
|
for (let j = 0; j < parts.length; j++) {
|
||||||
if (split) {
|
const part = parts[j];
|
||||||
words[1] = `${temp.slice(-1)}${words[1]}`;
|
if (ctx.measureText(`${line}${part}`).width <= maxWidth) {
|
||||||
|
line += `${part} `;
|
||||||
|
} else {
|
||||||
|
lines.push(line.trim());
|
||||||
|
line = `${part} `;
|
||||||
|
}
|
||||||
|
if (j < parts.length - 1) {
|
||||||
|
lines.push(line.trim());
|
||||||
|
line = '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (ctx.measureText(`${line}${word}`).width <= maxWidth) {
|
||||||
|
line += `${word} `;
|
||||||
} else {
|
} else {
|
||||||
split = true;
|
lines.push(line.trim());
|
||||||
words.splice(1, 0, temp.slice(-1));
|
line = `${word} `;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (ctx.measureText(`${line}${words[0]}`).width < maxWidth) {
|
|
||||||
line += `${words.shift()} `;
|
|
||||||
} else {
|
|
||||||
lines.push(line.trim());
|
|
||||||
line = '';
|
|
||||||
}
|
|
||||||
if (words.length === 0) lines.push(line.trim());
|
|
||||||
}
|
}
|
||||||
|
lines.push(line.trim());
|
||||||
return resolve(lines);
|
return resolve(lines);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user