${text}
`, + }); + return { + editor, + [Symbol.dispose]() { + editor.destroy(); + element.remove(); + }, + }; +} + +/** Text offsets of the first paragraph (0 = start of its text). */ +const select = (editor: Editor, from: number, to = from) => + editor.commands.setTextSelection({ from: 1 + from, to: 1 + to }); + +/** Each word with its size, read from the document. */ +const sizes = (editor: Editor) => { + const runs: Array<[string, unknown]> = []; + editor.state.doc.descendants((node) => { + if (node.isText) + runs.push([ + node.text!, + node.marks.find((m) => m.type.name === "latexFontSize")?.attrs.points, + ]); + }); + return runs; +}; + +describe("LaTeX font commands in a real editor", () => { + it("shows the common size of words sized one by one", () => { + using fixture = open("alpha beta gamma"); + const { editor } = fixture; + select(editor, 2); + setLatexFontSize(editor, 14); + select(editor, 8); + setLatexFontSize(editor, 14); + select(editor, 0, 10); + expect(latexFontState(editor)).toMatchObject({ size: 14, sizeMixed: false }); + }); + + it("sizes a whole word with an apostrophe or a combining accent at the caret", () => { + using fixture = open("I don't know café now"); + const { editor } = fixture; + select(editor, 3); + setLatexFontSize(editor, 14); + select(editor, 14); + setLatexFontSize(editor, 16); + expect(sizes(editor)).toEqual([ + ["I ", undefined], + ["don't", 14], + [" know ", undefined], + ["café", 16], + [" now", undefined], + ]); + }); + + it("+ and − move each word of a mixed selection by one point and keep the field empty", () => { + using fixture = open("alpha beta gamma"); + const { editor } = fixture; + const drawn = drawnPointsAt(editor.view, 13)!; + select(editor, 2); + setLatexFontSize(editor, 11); + select(editor, 8); + setLatexFontSize(editor, 20); + select(editor, 0, 16); + stepLatexFontSize(editor, 1, 10); + expect(sizes(editor)).toEqual([ + ["alpha", 12], + [" ", undefined], + ["beta", 21], + [" ", undefined], + ["gamma", drawn + 1], + ]); + expect(latexFontState(editor)).toMatchObject({ size: null, sizeMixed: true }); + stepLatexFontSize(editor, -1, 10); + expect(sizes(editor).map(([, points]) => points)).toEqual([ + 11, + undefined, + 20, + undefined, + drawn, + ]); + }); + + it("+ at a caret moves the word under it, from a named size", () => { + using fixture = open("one two"); + const { editor } = fixture; + editor.commands.setTextSelection({ from: 1, to: 4 }); + editor.commands.setMark("latexSize_large"); + select(editor, 1); + stepLatexFontSize(editor, 1, 10); + expect(sizes(editor)).toEqual([ + ["one", 13], + [" two", undefined], + ]); + }); + + it("Mod+Shift+. and Mod+Shift+, reach the LaTeX size commands", () => { + using fixture = open("alpha beta"); + const { editor } = fixture; + const run: string[] = []; + const detach = attachShortcutHost(editor.view.dom, ["latex"], { + accepts: () => true, + execute: (id) => { + run.push(id); + return true; + }, + }); + editor.commands.focus(); + select(editor, 0, 5); + // macOS reports the unshifted character while Cmd and Shift are held. + const mac = isMacKeyboard(); + for (const [code, key] of [ + ["Period", "."], + ["Comma", ","], + ] as const) { + editor.view.dom.dispatchEvent( + new KeyboardEvent("keydown", { + key, + code, + metaKey: mac, + ctrlKey: !mac, + shiftKey: true, + bubbles: true, + cancelable: true, + }), + ); + } + detach(); + expect(run).toEqual(["latex.fontSizeLarger", "latex.fontSizeSmaller"]); + }); + + it("text without a size steps from the size it is drawn at, wherever the selection starts", () => { + using fixture = open("ab cd"); + const { editor } = fixture; + select(editor, 3, 5); + editor.commands.setMark("latexSmallCaps"); + const small = drawnPointsAt(editor.view, 1 + 4)!; + const body = drawnPointsAt(editor.view, 1 + 1)!; + expect(small).toBeLessThan(body); + select(editor, 0, 5); + stepLatexFontSize(editor, 1, 10); + expect(sizes(editor).map(([, points]) => points)).toEqual([body + 1, undefined, small + 1]); + }); +}); diff --git a/apps/web/src/scient/latex/latexFontCommands.ts b/apps/web/src/scient/latex/latexFontCommands.ts new file mode 100644 index 0000000000..2cf7770606 --- /dev/null +++ b/apps/web/src/scient/latex/latexFontCommands.ts @@ -0,0 +1,220 @@ +import type { ChainedCommands, Editor } from "@tiptap/core"; +import type { Mark } from "@tiptap/pm/model"; +import { latexVisualFontMetrics } from "./latexVisualLayout"; +import { drawnPointsAt, normalizeFontSize } from "../writing/fontChoices"; +import { + type FontRun, + holdsFont, + sharedValue, + steppedFontSize, + steppedRuns, + wordAround, +} from "../writing/fontTarget"; +import { declaredLatexCompiler } from "./latexCompiler"; +import { latexFontFamilyDeclaration, type LatexFontEngine } from "./latexTextFormatting"; + +const NAMED_SIZES = [ + "tiny", + "scriptsize", + "footnotesize", + "small", + "normalsize", + "large", + "Large", + "LARGE", + "huge", + "Huge", +] as const; + +/** Baseline written with a new size: 1.2 times the size, as the class sizes mostly are. */ +const LEADING_RATIO = 1.2; + +/** The size of a run in points: its numeric size, or its named size for the class size. */ +function latexRunSize(marks: ReadonlyArray, baseFontPt: 10 | 11 | 12): number | null { + const numeric = marks.find((mark) => mark.type.name === "latexFontSize")?.attrs.points; + if (typeof numeric === "number") return numeric; + const named = NAMED_SIZES.find((name) => + marks.some((mark) => mark.type.name === `latexSize_${name}`), + ); + return named === undefined ? null : latexVisualFontMetrics(baseFontPt)[named][0]; +} + +/** + * The size and font shared by the whole selection. A value is null when the text has none, and + * the matching `Mixed` flag is true when the selection holds several. A named size (`\large`) + * reports its number for the document's class size. Whitespace and code are ignored (`holdsFont`). + */ +export function latexFontState( + editor: Editor | null, + baseFontPt: 10 | 11 | 12 = 10, +): { + readonly size: number | null; + readonly family: string | null; + readonly sizeMixed: boolean; + readonly familyMixed: boolean; +} { + if (!editor) return { size: null, family: null, sizeMixed: false, familyMixed: false }; + const { selection, doc } = editor.state; + const sizes = new Set