mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-21 23:50:45 +00:00
fix(terminal): fit wide RTL runs without clipping glyphs
This commit is contained in:
1 parent
7bbff7ce6e
commit
0fbf1cdc14
2 files changed
+40
-8
No files matched your search
@@ -13,12 +13,30 @@ const VERSION = "6.1.0-beta.292";
|
||||
// Share the terminalBidiJoiner gate. Scope this fix to the new RTL
|
||||
// shaping path; leave the existing ASCII font-ligature renderer unchanged.
|
||||
const rtl = new RegExp(require("../lib/terminalRtlCharacter.json"), "u");
|
||||
// Keep the outer span exactly on the grid. When wider fallback glyphs would
|
||||
// extend into the next cell, compress the complete shaped text horizontally
|
||||
// in an inner span. Keep the untouched text together for browser bidi/shaping,
|
||||
// and avoid measuring DOM layout during rendering: xterm's existing spacing
|
||||
// calculation already supplies the difference from the cached natural width.
|
||||
const patches = [
|
||||
["xterm.js", "k!==this.defaultSpacing&&(w.style.letterSpacing=`\${k}px`)",
|
||||
`H&&${rtl}.test($)?(w.style.width=\`\${y*_}px\`,w.style.letterSpacing="0px",w.style.overflow="clip"):k!==this.defaultSpacing&&(w.style.letterSpacing=\`\${k}px\`)`],
|
||||
["xterm.mjs", "Ot!==this.defaultSpacing&&(I.style.letterSpacing=`\${Ot}px`)",
|
||||
`fi&&${rtl}.test(_i)?(I.style.width=\`\${Rr*h}px\`,I.style.letterSpacing="0px",I.style.overflow="clip"):Ot!==this.defaultSpacing&&(I.style.letterSpacing=\`\${Ot}px\`)`],
|
||||
];
|
||||
{ file: "xterm.js", joined: "H", chars: "$", span: "w", width: "y*_", spacing: "k", rows: "m" },
|
||||
{ file: "xterm.mjs", joined: "fi", chars: "_i", span: "I", width: "Rr*h", spacing: "Ot", rows: "p" },
|
||||
].map(({ file, joined, chars, span, width, spacing, rows }) => {
|
||||
const defaultSpacing = `${spacing}!==this.defaultSpacing&&(${span}.style.letterSpacing=\`\${${spacing}}px\`)`;
|
||||
const original = `,${defaultSpacing},${rows}.push(${span}),`;
|
||||
const replacement = [
|
||||
`;if(${joined}&&${rtl}.test(${chars})){`,
|
||||
`const allocated=${width};${span}.style.width=allocated+"px";${span}.style.letterSpacing="0px";`,
|
||||
`if(${spacing}<0&&allocated>0){`,
|
||||
'const run=this._document.createElement("span");',
|
||||
`run.textContent=${span}.textContent;`,
|
||||
'run.style.transformOrigin="left center";',
|
||||
`run.style.transform="scaleX("+allocated/(allocated-${spacing})+")";`,
|
||||
`${span}.replaceChildren(run);`,
|
||||
`}}else{${defaultSpacing}}${rows}.push(${span}),`,
|
||||
].join("");
|
||||
return [file, original, replacement];
|
||||
});
|
||||
|
||||
function patchJoinedWidth(source, original, replacement) {
|
||||
if (source.split(replacement).length === 2 && !source.replace(replacement, "").includes(original)) return source;
|
||||
@@ -29,7 +47,7 @@ function patchJoinedWidth(source, original, replacement) {
|
||||
function patchInstalledXterm(root = process.cwd()) {
|
||||
const base = path.resolve(root, "node_modules/@xterm/xterm");
|
||||
const version = JSON.parse(fs.readFileSync(path.join(base, "package.json"), "utf8")).version;
|
||||
if (version !== VERSION) throw new Error(`unsupported @xterm/xterm \${version}; expected \${VERSION}`);
|
||||
if (version !== VERSION) throw new Error(`unsupported @xterm/xterm ${version}; expected ${VERSION}`);
|
||||
const files = patches.map(([file, original, replacement]) => {
|
||||
const filename = path.join(base, "lib", file);
|
||||
const source = fs.readFileSync(filename, "utf8");
|
||||
|
||||
@@ -30,19 +30,28 @@ if (!process.versions.electron) {
|
||||
await win.webContents.insertCSS(fs.readFileSync(require.resolve("@xterm/xterm/css/xterm.css"), "utf8"));
|
||||
const measurement = await win.webContents.executeJavaScript(`(async () => {
|
||||
${bundle}
|
||||
const term = new rtlTest.Terminal({cols: 30, rows: 8, fontSize: 24, fontFamily: "serif", allowProposedApi: true});
|
||||
const fontFamily = '"Noto Sans Mono", "DejaVu Sans", monospace';
|
||||
const term = new rtlTest.Terminal({cols: 30, rows: 8, fontSize: 24, fontFamily, allowProposedApi: true});
|
||||
term.open(document.querySelector("#terminal"));
|
||||
term.registerCharacterJoiner(text => text.startsWith("ffi") ? [[0, 3]] : rtlTest.findRightToLeftRuns(text));
|
||||
const a = "יייייייי", b = "שששששששש";
|
||||
const ctx = document.createElement("canvas").getContext("2d");
|
||||
ctx.font = "24px serif";
|
||||
ctx.font = "24px " + fontFamily;
|
||||
await new Promise(resolve => term.write(a + " Z\\r\\n" + b + " Z\\r\\n01234567 Z\\r\\nffi Z\\r\\n", resolve));
|
||||
await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
|
||||
const rows = [...document.querySelectorAll(".xterm-rows > div")].slice(0, 4);
|
||||
const geometry = rows.map(row => {
|
||||
const spans = [...row.children];
|
||||
const suffix = spans.find(span => span.textContent.includes("Z"));
|
||||
const content = spans[0].firstElementChild ?? spans[0];
|
||||
const range = document.createRange();
|
||||
range.selectNodeContents(content);
|
||||
const glyphs = range.getBoundingClientRect();
|
||||
const cellBox = spans[0].getBoundingClientRect();
|
||||
return {
|
||||
glyphWidth: glyphs.width,
|
||||
glyphRight: glyphs.right - cellBox.left,
|
||||
transform: content.style.transform,
|
||||
marker: suffix.getBoundingClientRect().right - row.getBoundingClientRect().left,
|
||||
joinedWidth: spans[0].getBoundingClientRect().width,
|
||||
text: row.textContent,
|
||||
@@ -61,6 +70,11 @@ if (!process.versions.electron) {
|
||||
// Ensure the fixture actually exercises unequal font metrics rather than
|
||||
// passing accidentally on a monospace/fallback-only test environment.
|
||||
assert.ok(Math.abs(measurement.natural[0] - measurement.natural[1]) > 8);
|
||||
assert.ok(measurement.natural[1] > measurement.cell * 8 + 8, "wide fallback must exceed its cell allocation");
|
||||
assert.notEqual(wide.transform, "", "nominally oversized RTL glyphs must actually fit, not just clip");
|
||||
assert.equal(wide.overflow, "", "RTL text must not be hidden by overflow clipping");
|
||||
assert.ok(wide.glyphRight <= wide.joinedWidth + 1, JSON.stringify(measurement));
|
||||
assert.equal(thin.transform, "", "narrow RTL text must not be unnecessarily rescaled");
|
||||
for (const row of [thin, wide]) {
|
||||
assert.match(row.text, / Z/);
|
||||
assert.ok(Math.abs(row.joinedWidth - measurement.cell * 8) <= 1, JSON.stringify(measurement));
|
||||
|
||||
Reference in new issue
Block a user