diff --git a/commands/edit-image-text/tweet.js b/commands/edit-image-text/tweet.js index e017f410..b0e9ecf5 100644 --- a/commands/edit-image-text/tweet.js +++ b/commands/edit-image-text/tweet.js @@ -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)); diff --git a/util/Canvas.js b/util/Canvas.js index 871c54a4..f7e75c3c 100644 --- a/util/Canvas.js +++ b/util/Canvas.js @@ -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); }); }