mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 08:03:40 +00:00
fix(terminal): preserve RTL DOM cell widths and detect astral RTL
This commit is contained in:
1 parent
8e862798e4
commit
7bbff7ce6e
7 files changed
+163
-8
No files matched your search
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env node
|
||||
/* global console, process */
|
||||
"use strict";
|
||||
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
// xterm 6.1 beta applies a joined run's *total* width correction as
|
||||
// letter-spacing, which Chromium can apply once per glyph. Joined cells are
|
||||
// already sized in terminal columns; use that width instead of changing the
|
||||
// text spacing (which can also interfere with contextual shaping).
|
||||
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");
|
||||
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\`)`],
|
||||
];
|
||||
|
||||
function patchJoinedWidth(source, original, replacement) {
|
||||
if (source.split(replacement).length === 2 && !source.replace(replacement, "").includes(original)) return source;
|
||||
if (source.split(original).length !== 2) throw new Error("unexpected xterm joined-width patch anchor");
|
||||
return source.replace(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}`);
|
||||
const files = patches.map(([file, original, replacement]) => {
|
||||
const filename = path.join(base, "lib", file);
|
||||
const source = fs.readFileSync(filename, "utf8");
|
||||
return [filename, source, patchJoinedWidth(source, original, replacement)];
|
||||
});
|
||||
for (const [filename, source, patched] of files) {
|
||||
if (source !== patched) fs.writeFileSync(filename, patched);
|
||||
}
|
||||
if (files.some(([, source, patched]) => source !== patched)) {
|
||||
fs.rmSync(path.resolve(root, "node_modules/.vite"), { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
try { patchInstalledXterm(); }
|
||||
catch (error) { console.error(error.message); process.exitCode = 1; }
|
||||
}
|
||||
module.exports = { patchInstalledXterm };
|
||||
@@ -0,0 +1,71 @@
|
||||
"use strict";
|
||||
/* global process, __dirname, console */
|
||||
if (!process.versions.electron) {
|
||||
require("node:test")("RTL DOM cell geometry", { skip: "run with Electron" }, () => {});
|
||||
} else {
|
||||
const { app, BrowserWindow } = require("electron");
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const { buildSync } = require("esbuild");
|
||||
const profile = fs.mkdtempSync(path.join(os.tmpdir(), "netcatty-rtl-width-"));
|
||||
app.setPath("userData", profile);
|
||||
app.on("window-all-closed", () => {});
|
||||
const bundle = buildSync({
|
||||
stdin: {
|
||||
contents: 'export { Terminal } from "@xterm/xterm"; export { findRightToLeftRuns } from "./components/terminal/runtime/terminalBidiJoiner";',
|
||||
resolveDir: path.resolve(__dirname, ".."),
|
||||
},
|
||||
bundle: true, format: "iife", globalName: "rtlTest", platform: "browser", write: false,
|
||||
}).outputFiles[0].text;
|
||||
let win;
|
||||
function finish(code) {
|
||||
win?.destroy();
|
||||
app.exit(code);
|
||||
}
|
||||
void app.whenReady().then(async () => {
|
||||
win = new BrowserWindow({ show: true, width: 600, height: 320 });
|
||||
await win.loadURL("data:text/html,<div id='terminal' style='width:500px;height:260px'></div>");
|
||||
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});
|
||||
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";
|
||||
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"));
|
||||
return {
|
||||
marker: suffix.getBoundingClientRect().right - row.getBoundingClientRect().left,
|
||||
joinedWidth: spans[0].getBoundingClientRect().width,
|
||||
text: row.textContent,
|
||||
first: spans[0].textContent,
|
||||
widthStyle: spans[0].style.width,
|
||||
overflow: spans[0].style.overflow,
|
||||
};
|
||||
});
|
||||
return { geometry, cell: term._core._renderService.dimensions.css.cell.width, natural: [ctx.measureText(a).width, ctx.measureText(b).width] };
|
||||
})()`);
|
||||
console.log(JSON.stringify(measurement));
|
||||
const [thin, wide, ascii, ligature] = measurement.geometry;
|
||||
assert.equal(ligature.first, "ffi");
|
||||
assert.equal(ligature.widthStyle, "", "ASCII ligatures must keep their existing layout");
|
||||
assert.equal(ligature.overflow, "", "ASCII ligature overhang must not be clipped");
|
||||
// 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);
|
||||
for (const row of [thin, wide]) {
|
||||
assert.match(row.text, / Z/);
|
||||
assert.ok(Math.abs(row.joinedWidth - measurement.cell * 8) <= 1, JSON.stringify(measurement));
|
||||
assert.ok(Math.abs(row.marker - ascii.marker) <= 1, JSON.stringify(measurement));
|
||||
}
|
||||
finish(0);
|
||||
}).catch(error => { console.error(error); finish(1); });
|
||||
}
|
||||
Reference in new issue
Block a user