mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +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));
|
||||
continue;
|
||||
}
|
||||
let currentX = x + 0;
|
||||
let currentX = x;
|
||||
for (let i = 0; i < lineNoEmoji.length; i++) {
|
||||
const linePart = lineNoEmoji[i];
|
||||
ctx.fillText(linePart, currentX, y + (23 * currentLine) + (9 * currentLine));
|
||||
|
||||
+23
-16
@@ -213,26 +213,33 @@ module.exports = class CanvasUtil {
|
||||
const words = text.split(' ');
|
||||
const lines = [];
|
||||
let line = '';
|
||||
while (words.length > 0) {
|
||||
let split = false;
|
||||
while (ctx.measureText(words[0]).width >= maxWidth) {
|
||||
const temp = words[0];
|
||||
words[0] = temp.slice(0, -1);
|
||||
if (split) {
|
||||
words[1] = `${temp.slice(-1)}${words[1]}`;
|
||||
for (let i = 0; i < words.length; i++) {
|
||||
const word = words[i];
|
||||
if (word.includes('\n')) {
|
||||
const parts = word.split('\n');
|
||||
for (let j = 0; j < parts.length; j++) {
|
||||
const part = parts[j];
|
||||
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 {
|
||||
split = true;
|
||||
words.splice(1, 0, temp.slice(-1));
|
||||
lines.push(line.trim());
|
||||
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);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user