|
@@ -84,93 +84,81 @@ function removeStorage(key) {
|
|
|
function drawtext(text, maxWidth) {
|
|
|
let textArr = text.split("");
|
|
|
let len = textArr.length;
|
|
|
- // 上个节点
|
|
|
- let previousNode = 0;
|
|
|
- // 记录节点宽度
|
|
|
- let nodeWidth = 0;
|
|
|
- // 文本换行数组
|
|
|
let rowText = [];
|
|
|
- // 如果是字母,侧保存长度
|
|
|
- let letterWidth = 0;
|
|
|
- // 汉字宽度
|
|
|
- let chineseWidth = 16;
|
|
|
- // otherFont宽度
|
|
|
- let otherWidth = 8;
|
|
|
+ let currentLine = "";
|
|
|
+ let currentWidth = 0;
|
|
|
+ let lastChineseIndex = -1;
|
|
|
+
|
|
|
for (let i = 0; i < len; i++) {
|
|
|
- if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(textArr[i])) {
|
|
|
- if (letterWidth > 0) {
|
|
|
- if (nodeWidth + chineseWidth + letterWidth * otherWidth > maxWidth) {
|
|
|
- rowText.push({
|
|
|
- type: "text",
|
|
|
- content: text.substring(previousNode, i)
|
|
|
- });
|
|
|
- previousNode = i;
|
|
|
- nodeWidth = chineseWidth;
|
|
|
- letterWidth = 0;
|
|
|
- } else {
|
|
|
- nodeWidth += chineseWidth + letterWidth * otherWidth;
|
|
|
- letterWidth = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (nodeWidth + chineseWidth > maxWidth) {
|
|
|
- rowText.push({
|
|
|
- type: "text",
|
|
|
- content: text.substring(previousNode, i)
|
|
|
- });
|
|
|
- previousNode = i;
|
|
|
- nodeWidth = chineseWidth;
|
|
|
- } else {
|
|
|
- nodeWidth += chineseWidth;
|
|
|
- }
|
|
|
- }
|
|
|
+ let char = textArr[i];
|
|
|
+ let charWidth = 0;
|
|
|
+
|
|
|
+ // 计算字符宽度
|
|
|
+ if (/[\u4e00-\u9fa5]|[\uFE30-\uFFA0]/g.test(char)) {
|
|
|
+ charWidth = 16; // 汉字宽度
|
|
|
+ lastChineseIndex = i;
|
|
|
+ } else if (/[a-zA-Z0-9]/g.test(char)) {
|
|
|
+ charWidth = 8; // 字母和数字宽度
|
|
|
} else {
|
|
|
- if (/\n/g.test(textArr[i])) {
|
|
|
- rowText.push({
|
|
|
- type: "break",
|
|
|
- content: text.substring(previousNode, i)
|
|
|
- });
|
|
|
- previousNode = i + 1;
|
|
|
- nodeWidth = 0;
|
|
|
- letterWidth = 0;
|
|
|
- } else if (textArr[i] == "\\" && textArr[i + 1] == "n") {
|
|
|
+ charWidth = 16; // 其他字符宽度
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理换行符
|
|
|
+ if (char === "\n" || (char === "\\" && textArr[i + 1] === "n")) {
|
|
|
+ if (currentLine) {
|
|
|
rowText.push({
|
|
|
- type: "break",
|
|
|
- content: text.substring(previousNode, i)
|
|
|
+ type: "text",
|
|
|
+ content: currentLine
|
|
|
});
|
|
|
- previousNode = i + 2;
|
|
|
- nodeWidth = 0;
|
|
|
- letterWidth = 0;
|
|
|
- } else if (/[a-zA-Z0-9]/g.test(textArr[i])) {
|
|
|
- letterWidth += 1;
|
|
|
- if (nodeWidth + letterWidth * otherWidth > maxWidth) {
|
|
|
+ }
|
|
|
+ rowText.push({
|
|
|
+ type: "break",
|
|
|
+ content: ""
|
|
|
+ });
|
|
|
+ currentLine = "";
|
|
|
+ currentWidth = 0;
|
|
|
+ lastChineseIndex = -1;
|
|
|
+ if (char === "\\") i++; // 跳过\n中的n
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否需要换行
|
|
|
+ if (currentWidth + charWidth > maxWidth) {
|
|
|
+ if (currentLine) {
|
|
|
+ // 如果当前字符是汉字,且不是第一个字符,则在上一个汉字处换行
|
|
|
+ if (lastChineseIndex > 0 && lastChineseIndex < i) {
|
|
|
rowText.push({
|
|
|
type: "text",
|
|
|
- content: text.substring(previousNode, i + 1 - letterWidth)
|
|
|
+ content: currentLine.substring(0, lastChineseIndex + 1)
|
|
|
});
|
|
|
- previousNode = i + 1 - letterWidth;
|
|
|
- nodeWidth = letterWidth * otherWidth;
|
|
|
- letterWidth = 0;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (nodeWidth + otherWidth > maxWidth) {
|
|
|
+ currentLine = currentLine.substring(lastChineseIndex + 1) + char;
|
|
|
+ currentWidth = currentLine.length * charWidth;
|
|
|
+ } else {
|
|
|
rowText.push({
|
|
|
type: "text",
|
|
|
- content: text.substring(previousNode, i)
|
|
|
+ content: currentLine
|
|
|
});
|
|
|
- previousNode = i;
|
|
|
- nodeWidth = otherWidth;
|
|
|
- } else {
|
|
|
- nodeWidth += otherWidth;
|
|
|
+ currentLine = char;
|
|
|
+ currentWidth = charWidth;
|
|
|
}
|
|
|
+ } else {
|
|
|
+ currentLine = char;
|
|
|
+ currentWidth = charWidth;
|
|
|
}
|
|
|
+ } else {
|
|
|
+ currentLine += char;
|
|
|
+ currentWidth += charWidth;
|
|
|
}
|
|
|
}
|
|
|
- if (previousNode < len) {
|
|
|
+
|
|
|
+ // 添加最后一行
|
|
|
+ if (currentLine) {
|
|
|
rowText.push({
|
|
|
type: "text",
|
|
|
- content: text.substring(previousNode, len)
|
|
|
+ content: currentLine
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
return rowText;
|
|
|
}
|
|
|
// 重写app弹窗
|