fix(terminal): preserve RTL DOM cell widths and detect astral RTL

This commit is contained in:
陈大猫 committed 2026-11-21 15:49:45 +08:00
1 parent 8e862798e4
commit 7bbff7ce6e
7 files changed
+163 -8

No files matched your search

@@ -1,7 +1,7 @@
import assert from "node:assert/strict";
import test from "node:test";
import { createPaneBidiJoiner, findRightToLeftRuns } from "./terminalBidiJoiner";
import { createPaneBidiJoiner, findRightToLeftRuns, mightContainRightToLeft } from "./terminalBidiJoiner";
const slice = (text: string, ranges: [number, number][]) =>
ranges.map(([start, end]) => text.slice(start, end));
@@ -12,6 +12,19 @@ test("pure left-to-right text is left alone", () => {
assert.deepEqual(findRightToLeftRuns(""), []);
});
test("the cheap detection gate rejects the ASCII and unrelated astral characters", () => {
assert.equal(mightContainRightToLeft(String.raw`update DEPLOY 0 2 3 \uD802`), false);
assert.equal(mightContainRightToLeft("shell 🛠️ 👍"), false);
assert.equal(mightContainRightToLeft("𞤀𞤤"), true);
});
test("supplementary RTL stays whole and returns xterm UTF-16 offsets", () => {
const adlam = "𞤀𞤤";
const text = `🛠 ${adlam} x`;
assert.deepEqual(findRightToLeftRuns(text), [[3, 7]]);
assert.deepEqual(findRightToLeftRuns(`${adlam} 123`), [[0, 8]]);
});
test("Persian prose is one run so the browser can order and join it", () => {
const text = "سلام دنیا";
assert.deepEqual(findRightToLeftRuns(text), [[0, text.length]]);
@@ -1,4 +1,5 @@
import bidiFactory from "bidi-js";
import rtlCharacterPattern from "../../../lib/terminalRtlCharacter.json";
/**
* Display-only right-to-left support for xterm.js (issue #4261).
@@ -24,18 +25,36 @@ import bidiFactory from "bidi-js";
* not right-aligned.
*/
// Hebrew, Arabic, Syriac, Thaana, NKo, Samaritan, Mandaic and the Arabic
// presentation forms, plus the RTL scripts outside the BMP. A cheap check that
// keeps the full algorithm off the hot path for left-to-right lines.
const RTL_CHARACTER = /[֐-ࣿיִ-﷿ﹰ-ﻼ]|[\uD802\uD803\uD83A\uD83B]/;
// RTL scripts and directional controls in the BMP and supplementary planes.
// The Unicode flag makes the supplementary bounds code points, not UTF-16
// lead surrogates (which would also match unrelated non-RTL characters).
const RTL_CHARACTER = new RegExp(rtlCharacterPattern, "u");
const SURROGATE_PAIR = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
// bidi-js 1.0.3 looks up string[i] rather than a full code point. Project
// astral characters to BMP characters of the same bidi class for this
// calculation only. Two copies retain the original UTF-16 offsets expected
// by xterm; the actual glyphs (and terminal buffer) are never replaced.
const BIDI_CLASS_PROXY: Readonly<Record<string, string>> = {
L: "a", R: "א", AL: "ا", EN: "0", AN: "٠", NSM: "\u0301",
ON: "!", BN: "\u00ad", ET: "$", CS: ",", ES: "+",
};
let bidi: ReturnType<typeof bidiFactory> | null = null;
export function mightContainRightToLeft(text: string): boolean {
return RTL_CHARACTER.test(text);
}
export function findRightToLeftRuns(text: string): [number, number][] {
if (!RTL_CHARACTER.test(text)) return [];
if (!mightContainRightToLeft(text)) return [];
bidi ??= bidiFactory();
const { levels } = bidi.getEmbeddingLevels(text, "ltr");
const classifier = bidi;
const projected = text.replace(SURROGATE_PAIR, (character) =>
(BIDI_CLASS_PROXY[classifier.getBidiCharTypeName(character)] ?? "!").repeat(2),
);
const { levels } = classifier.getEmbeddingLevels(projected, "ltr");
const ranges: [number, number][] = [];
let start = -1;
+1
View File
@@ -0,0 +1 @@
"[\\u0590-\\u08ff\\u200f\\u202b\\u202e\\u2067\\ufb1d-\\ufdff\\ufe70-\\ufefc\\u{10800}-\\u{10fff}\\u{1e800}-\\u{1eeff}]"
+1 -1
View File
@@ -42,7 +42,7 @@
"pack:linux": "npm run build && cross-env NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --publish=never",
"pack:linux-x64": "npm run build && cross-env npm_config_arch=x64 NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --x64 --publish=never",
"pack:linux-arm64": "npm run build && cross-env npm_config_arch=arm64 NODE_OPTIONS=--disable-warning=DEP0190 electron-builder --config electron-builder.config.cjs --linux --arm64 --publish=never",
"postinstall": "patch-package && node scripts/patch-xterm-macos-column-selection.cjs && node scripts/patch-xterm-foreground-intense.cjs && node scripts/patch-xterm-ligatures-font-cache.cjs && node scripts/patch-xterm-image-tile-stride.cjs && node scripts/patch-xterm-image-iip-type.cjs && electron-builder install-app-deps && node scripts/rebuildPatchedNodePty.cjs && node scripts/patch-xterm-webgl-atlas.cjs",
"postinstall": "patch-package && node scripts/patch-xterm-macos-column-selection.cjs && node scripts/patch-xterm-foreground-intense.cjs && node scripts/patch-xterm-joined-width.cjs && node scripts/patch-xterm-ligatures-font-cache.cjs && node scripts/patch-xterm-image-tile-stride.cjs && node scripts/patch-xterm-image-iip-type.cjs && electron-builder install-app-deps && node scripts/rebuildPatchedNodePty.cjs && node scripts/patch-xterm-webgl-atlas.cjs",
"rebuild": "electron-builder install-app-deps",
"tool:cli": "node electron/cli/netcatty-tool-cli.cjs",
"generate:capability-tools": "node scripts/generate-capability-tools.cjs",
+50
View File
@@ -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 };
+71
View File
@@ -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); });
}
+1
View File
@@ -11,6 +11,7 @@ declare module "bidi-js" {
}
interface Bidi {
getBidiCharTypeName(char: string): string;
getEmbeddingLevels(text: string, baseDirection?: "ltr" | "rtl"): BidiEmbeddingLevels;
}