fix: 默认不出现在行首行尾的字符

This commit is contained in:
unanmed 2024-12-10 11:13:10 +08:00
parent 39baab94ae
commit 1243574e32

View File

@ -63,6 +63,7 @@ Mota.require('var', 'loading').once('coreInit', () => {
/** /**
* *
* @param data * @param data
* @returns
*/ */
function splitLines(data: TextContentData) { function splitLines(data: TextContentData) {
const words = breakWords(data); const words = breakWords(data);
@ -80,19 +81,11 @@ function splitLines(data: TextContentData) {
const ctx = testCanvas.ctx; const ctx = testCanvas.ctx;
ctx.font = data.font; ctx.font = data.font;
console.time();
while (1) { while (1) {
const diff = end - start; const diff = end - start;
if (diff === 1) { if (diff === 1) {
const data1 = ctx.measureText( res.push(words[start]);
text.slice(words[resolved], words[end])
);
if (data1.width <= data.width) {
res.push(words[end - 1]);
} else {
res.push(words[start]);
}
if (end === words.length) break; if (end === words.length) break;
resolved = start; resolved = start;
end = words.length; end = words.length;
@ -109,13 +102,17 @@ function splitLines(data: TextContentData) {
} }
} }
} }
console.timeEnd();
return res; return res;
} }
const defaultsBreak = ' -,.)]}?!;:,。)】?!;:'; const defaultsBreak = ' -,.)]}?!;:,。)】?!;:';
const defaultsIgnoreStart =
')】》>﹞>)]»›〕〉}]」}〗』,。?!:;·…,.?!:;、……~&@#';
const defaultsIgnoreEnd = '(【《<﹝<([«‹〔〈{[「{〖『';
const breakSet = new Set(defaultsBreak); const breakSet = new Set(defaultsBreak);
const ignoreStart = new Set(defaultsIgnoreStart);
const ignoreEnd = new Set(defaultsIgnoreEnd);
/** /**
* CJK * CJK
@ -139,6 +136,7 @@ function isCJK(char: number) {
/** /**
* *
* @param data * @param data
* @returns
*/ */
function breakWords(data: TextContentData) { function breakWords(data: TextContentData) {
let allBreak = false; let allBreak = false;
@ -157,10 +155,10 @@ function breakWords(data: TextContentData) {
} }
} }
console.time();
const res: number[] = [0]; const res: number[] = [0];
const text = data.text; const text = data.text;
const { ignoreLineStart, ignoreLineEnd } = data; const ignoreLineStart = data.ignoreLineStart.union(ignoreStart);
const ignoreLineEnd = data.ignoreLineEnd.union(ignoreEnd);
for (let pointer = 0; pointer < text.length; pointer++) { for (let pointer = 0; pointer < text.length; pointer++) {
const char = text[pointer]; const char = text[pointer];
const next = text[pointer + 1]; const next = text[pointer + 1];
@ -186,6 +184,5 @@ function breakWords(data: TextContentData) {
} }
} }
res.push(text.length); res.push(text.length);
console.timeEnd();
return res; return res;
} }