mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-03 23:36:43 +02:00
Fix
This commit is contained in:
+21
-39
@@ -207,48 +207,30 @@ module.exports = class CanvasUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static wrapText(ctx, text, maxWidth) {
|
static wrapText(ctx, text, maxWidth) {
|
||||||
return new Promise(resolve => {
|
const lines = [];
|
||||||
if (ctx.measureText(text).width < maxWidth) return resolve([text]);
|
const wordsAndBreaks = text.split('\n');
|
||||||
if (ctx.measureText('W').width > maxWidth) return resolve(null);
|
for (let i = 0; i < wordsAndBreaks.length; i++) {
|
||||||
const words = text.split(' ');
|
const segment = wordsAndBreaks[i];
|
||||||
const lines = [];
|
if (segment === '') {
|
||||||
let line = '';
|
lines.push('');
|
||||||
for (let i = 0; i < words.length; i++) {
|
continue;
|
||||||
let split = false;
|
}
|
||||||
while (ctx.measureText(words[i]).width >= maxWidth) {
|
const words = segment.split(' ');
|
||||||
const temp = words[i];
|
let currentLine = '';
|
||||||
words[i] = temp.slice(i, -1);
|
for (let j = 0; j < words.length; j++) {
|
||||||
if (split) {
|
const word = words[j];
|
||||||
words[i + 1] = `${temp.slice(-1)}${words[i + 1]}`;
|
if (ctx.measureText(`${currentLine} ${word}`).width <= maxWidth) {
|
||||||
} else {
|
currentLine += `${currentLine === '' ? '' : ' '}${word}`;
|
||||||
split = true;
|
|
||||||
words.splice(i + 1, i, temp.slice(-1));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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} `;
|
|
||||||
}
|
|
||||||
lines.push(line.trim());
|
|
||||||
line = '';
|
|
||||||
}
|
|
||||||
} else if (ctx.measureText(`${line}${word}`).width <= maxWidth) {
|
|
||||||
line += `${word} `;
|
|
||||||
} else {
|
} else {
|
||||||
lines.push(line.trim());
|
lines.push(currentLine.trim());
|
||||||
line = `${word} `;
|
currentLine = word;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lines.push(line.trim());
|
if (currentLine !== '') {
|
||||||
return resolve(lines);
|
lines.push(currentLine.trim());
|
||||||
});
|
}
|
||||||
|
}
|
||||||
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
static centerImage(base, data) {
|
static centerImage(base, data) {
|
||||||
|
|||||||
Reference in New Issue
Block a user