Text after the CJK merge: plain ASCII is measured without segmenting … · nicoloboschi/giotto@db7444b · GitHub
Skip to content

Commit db7444b

Browse files
committed
Text after the CJK merge: plain ASCII is measured without segmenting (text layout was ~40x slower), spaces keep their old width so boxes don't re-wrap, rich text blocks rejoin lines broken mid-sentence again, a Latin word longer than its line stays whole (its box grows) while code and unspaced CJK still break, and runs of spaces read as one; the site copies all of lib/ so no module 404s (closes #1)
1 parent fa456f6 commit db7444b

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

‎lib/blocks.js‎

Lines changed: 9 additions & 5 deletions

‎lib/text.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const opening = /[(「『【〔〈《\uff08([]$/u;
88
const space = (t) => /^[ \t]+$/.test(t);
99

1010
export function measureText(text, size, charWidth, bold = false) {
11+
text = String(text);
12+
if (/^[\x20-\x7e]*$/.test(text)) return text.length * size * charWidth * (bold ? 1.07 : 1); // plain ASCII: no segmenting needed
1113
return graphemes(text).reduce((w, g) => w + (wide.test(g) ? Math.max(1, charWidth * 2) : /^\p{Mark}+$/u.test(g) ? 0 : charWidth) * size, 0) * (bold ? 1.07 : 1);
1214
}
1315

@@ -51,6 +53,7 @@ export function wrapRuns(runs, room, widthOf, { preserveSpace = false } = {}) {
5153
}
5254
if (end < atoms.length) {
5355
if (lastBreak > start) end = lastBreak;
56+
else if (!preserveSpace && !wide.test(atoms[start].t)) while (end < atoms.length && !preferred(end)) end++; // a Latin word longer than the line stays whole (its box grows)
5457
else {
5558
while (end > start && !allowed(end)) end--;
5659
if (end === start) {

‎site/build.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const out = path.join(root, '_site');
99

1010
fs.rmSync(out, { recursive: true, force: true });
1111
fs.mkdirSync(path.join(out, 'lib'), { recursive: true });
12-
// The browser-side modules the player needs.
13-
for (const f of ['edit.js', 'render.js', 'styles.js', 'blocks.js', 'scenes.js']) fs.copyFileSync(path.join(root, 'lib', f), path.join(out, 'lib', f));
12+
// All of lib/, so a module the player starts importing can't be missing from the site (it was: chart.js, issue #1).
13+
for (const f of fs.readdirSync(path.join(root, 'lib')).filter((f) => f.endsWith('.js'))) fs.copyFileSync(path.join(root, 'lib', f), path.join(out, 'lib', f));
1414
for (const f of ['index.html', 'giotto-docs.json', 'styles.js', 'card.png']) fs.copyFileSync(path.join(root, 'site', f), path.join(out, f));
1515
fs.writeFileSync(path.join(out, '.nojekyll'), '');
1616
console.log(`Built ${out}`);

‎test/text.test.js‎

Lines changed: 3 additions & 2 deletions

0 commit comments

Comments
 (0)