docxkeun 0.1.3 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +74 -0
- package/README.md +166 -16
- package/dist/api/editor.d.ts +231 -7
- package/dist/api/editor.js +1125 -26
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +6 -1
- package/dist/api/index.js +4 -1
- package/dist/api/lists.d.ts +37 -0
- package/dist/api/lists.js +132 -0
- package/dist/api/parts.js +1 -0
- package/dist/api/positions.js +1 -0
- package/dist/api/revisions.d.ts +61 -0
- package/dist/api/revisions.js +401 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -0
- package/dist/api/tables.d.ts +60 -0
- package/dist/api/tables.js +138 -0
- package/dist/charts/color.d.ts +45 -0
- package/dist/charts/color.js +322 -0
- package/dist/charts/format.d.ts +12 -0
- package/dist/charts/format.js +459 -0
- package/dist/charts/index.d.ts +11 -0
- package/dist/charts/index.js +8 -0
- package/dist/charts/model.d.ts +225 -0
- package/dist/charts/model.js +758 -0
- package/dist/charts/outline.d.ts +17 -0
- package/dist/charts/outline.js +214 -0
- package/dist/charts/render.d.ts +20 -0
- package/dist/charts/render.js +1772 -0
- package/dist/drawing/arrows.d.ts +37 -0
- package/dist/drawing/arrows.js +99 -0
- package/dist/drawing/formula.d.ts +40 -0
- package/dist/drawing/formula.js +159 -0
- package/dist/drawing/geometry.d.ts +61 -0
- package/dist/drawing/geometry.js +286 -0
- package/dist/drawing/index.d.ts +12 -0
- package/dist/drawing/index.js +8 -0
- package/dist/drawing/presets.d.ts +72 -0
- package/dist/drawing/presets.js +2874 -0
- package/dist/fonts/shaping/apply.d.ts +94 -0
- package/dist/fonts/shaping/apply.js +349 -0
- package/dist/fonts/shaping/arabic.d.ts +28 -0
- package/dist/fonts/shaping/arabic.js +235 -0
- package/dist/fonts/shaping/buffer.d.ts +53 -0
- package/dist/fonts/shaping/buffer.js +95 -0
- package/dist/fonts/shaping/gdef.d.ts +30 -0
- package/dist/fonts/shaping/gdef.js +66 -0
- package/dist/fonts/shaping/gpos.d.ts +86 -0
- package/dist/fonts/shaping/gpos.js +428 -0
- package/dist/fonts/shaping/gsub.d.ts +65 -0
- package/dist/fonts/shaping/gsub.js +313 -0
- package/dist/fonts/shaping/index.d.ts +32 -0
- package/dist/fonts/shaping/index.js +402 -0
- package/dist/fonts/shaping/indic.d.ts +6 -0
- package/dist/fonts/shaping/indic.js +805 -0
- package/dist/fonts/shaping/layout-common.d.ts +124 -0
- package/dist/fonts/shaping/layout-common.js +349 -0
- package/dist/fonts/shaping/plan.d.ts +87 -0
- package/dist/fonts/shaping/plan.js +152 -0
- package/dist/fonts/shaping/scripts.d.ts +26 -0
- package/dist/fonts/shaping/scripts.js +160 -0
- package/dist/fonts/shaping/shaper.d.ts +23 -0
- package/dist/fonts/shaping/shaper.js +5 -0
- package/dist/fonts/shaping/unicode.d.ts +34 -0
- package/dist/fonts/shaping/unicode.js +138 -0
- package/dist/images/bmp.d.ts +9 -0
- package/dist/images/bmp.js +234 -0
- package/dist/images/gif.d.ts +9 -0
- package/dist/images/gif.js +215 -0
- package/dist/images/index.d.ts +18 -0
- package/dist/images/index.js +63 -0
- package/dist/images/png-encode.d.ts +4 -0
- package/dist/images/png-encode.js +73 -0
- package/dist/images/size.d.ts +10 -0
- package/dist/images/size.js +83 -0
- package/dist/images/tiff.d.ts +14 -0
- package/dist/images/tiff.js +407 -0
- package/dist/index.d.ts +7 -2
- package/dist/index.js +5 -0
- package/dist/layout/bidi.d.ts +18 -0
- package/dist/layout/bidi.js +71 -0
- package/dist/layout/blocks.d.ts +20 -3
- package/dist/layout/blocks.js +88 -58
- package/dist/layout/cache.d.ts +31 -0
- package/dist/layout/cache.js +38 -0
- package/dist/layout/drawing.d.ts +30 -0
- package/dist/layout/drawing.js +120 -0
- package/dist/layout/engine.d.ts +3 -0
- package/dist/layout/engine.js +218 -11
- package/dist/layout/index.d.ts +1 -0
- package/dist/layout/index.js +1 -0
- package/dist/layout/lines.d.ts +63 -5
- package/dist/layout/lines.js +245 -20
- package/dist/layout/paint.d.ts +25 -3
- package/dist/layout/paint.js +26 -10
- package/dist/layout/shapes.d.ts +7 -0
- package/dist/layout/shapes.js +338 -0
- package/dist/layout/style.d.ts +4 -0
- package/dist/layout/style.js +2 -0
- package/dist/layout/types.d.ts +89 -2
- package/dist/math/boxes.d.ts +88 -0
- package/dist/math/boxes.js +166 -0
- package/dist/math/index.d.ts +29 -0
- package/dist/math/index.js +44 -0
- package/dist/math/layout.d.ts +13 -0
- package/dist/math/layout.js +771 -0
- package/dist/math/linear.d.ts +3 -0
- package/dist/math/linear.js +152 -0
- package/dist/math/omml.d.ts +15 -0
- package/dist/math/omml.js +57 -0
- package/dist/math/paths.d.ts +61 -0
- package/dist/math/paths.js +450 -0
- package/dist/math/symbols.d.ts +49 -0
- package/dist/math/symbols.js +269 -0
- package/dist/math/text.d.ts +53 -0
- package/dist/math/text.js +316 -0
- package/dist/metafile/canvas.d.ts +80 -0
- package/dist/metafile/canvas.js +515 -0
- package/dist/metafile/dib.d.ts +27 -0
- package/dist/metafile/dib.js +300 -0
- package/dist/metafile/emf.d.ts +31 -0
- package/dist/metafile/emf.js +591 -0
- package/dist/metafile/geometry.d.ts +49 -0
- package/dist/metafile/geometry.js +85 -0
- package/dist/metafile/index.d.ts +27 -0
- package/dist/metafile/index.js +34 -0
- package/dist/metafile/reader.d.ts +28 -0
- package/dist/metafile/reader.js +59 -0
- package/dist/metafile/state.d.ts +118 -0
- package/dist/metafile/state.js +172 -0
- package/dist/metafile/text.d.ts +17 -0
- package/dist/metafile/text.js +130 -0
- package/dist/metafile/wmf.d.ts +33 -0
- package/dist/metafile/wmf.js +395 -0
- package/dist/model/content.d.ts +92 -7
- package/dist/model/content.js +251 -17
- package/dist/model/document.d.ts +34 -1
- package/dist/model/document.js +80 -1
- package/dist/model/fields.d.ts +4 -0
- package/dist/model/fields.js +10 -0
- package/dist/model/numbering.d.ts +2 -0
- package/dist/model/numbering.js +22 -0
- package/dist/model/props.d.ts +2 -0
- package/dist/model/props.js +3 -0
- package/dist/opc/index.d.ts +1 -1
- package/dist/opc/package.d.ts +34 -0
- package/dist/opc/package.js +58 -4
- package/dist/pdf/export.js +97 -6
- package/dist/pdf/images.d.ts +7 -0
- package/dist/pdf/images.js +39 -2
- package/dist/render/activity.d.ts +179 -0
- package/dist/render/activity.js +1003 -0
- package/dist/render/clipboard.d.ts +29 -0
- package/dist/render/clipboard.js +401 -0
- package/dist/render/comments.d.ts +166 -0
- package/dist/render/comments.js +779 -0
- package/dist/render/context-menu.d.ts +124 -0
- package/dist/render/context-menu.js +525 -0
- package/dist/render/editing.d.ts +146 -5
- package/dist/render/editing.js +678 -53
- package/dist/render/index.d.ts +12 -0
- package/dist/render/index.js +11 -0
- package/dist/render/navigation-panel.d.ts +76 -0
- package/dist/render/navigation-panel.js +431 -0
- package/dist/render/selection.js +5 -0
- package/dist/render/svg.d.ts +4 -6
- package/dist/render/svg.js +69 -7
- package/dist/render/toolbar.d.ts +83 -0
- package/dist/render/toolbar.js +505 -0
- package/dist/render/ui/comment-layout.d.ts +39 -0
- package/dist/render/ui/comment-layout.js +79 -0
- package/dist/render/ui/icons.d.ts +6 -0
- package/dist/render/ui/icons.js +38 -0
- package/dist/render/ui/menu.d.ts +38 -0
- package/dist/render/ui/menu.js +274 -0
- package/dist/render/ui/nav-model.d.ts +11 -0
- package/dist/render/ui/nav-model.js +34 -0
- package/dist/render/ui/overflow.d.ts +5 -0
- package/dist/render/ui/overflow.js +18 -0
- package/dist/render/ui/popover.d.ts +18 -0
- package/dist/render/ui/popover.js +109 -0
- package/dist/render/ui/theme.d.ts +2 -0
- package/dist/render/ui/theme.js +29 -0
- package/dist/render/undo.d.ts +76 -0
- package/dist/render/undo.js +144 -0
- package/dist/render/viewer.d.ts +100 -7
- package/dist/render/viewer.js +304 -92
- package/dist/security/policy.d.ts +3 -0
- package/dist/security/policy.js +6 -2
- package/dist/security/protection.d.ts +1 -0
- package/dist/security/protection.js +1 -1
- package/dist/text/bidi-data.d.ts +53 -0
- package/dist/text/bidi-data.js +318 -0
- package/dist/text/bidi.d.ts +37 -0
- package/dist/text/bidi.js +508 -0
- package/dist/xml/ns.d.ts +5 -0
- package/dist/xml/ns.js +7 -0
- package/package.json +9 -2
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
const REL = new Set([
|
|
2
|
+
...'=<>≠≤≥≦≧≨≩≪≫≮≯≰≱≈≉≊≋≡≢≣≅≆≇≃≄≂∼∽≍≐≑≒≓≔≕≜≝≞≟∝∈∉∊∋∌∍⊂⊃⊄⊅⊆⊇⊈⊉⊊⊋⊏⊐⊑⊒≺≻≼≽⪯⪰∣∤∥∦⊢⊣⊨⊩⊪⊫⊲⊳⊴⊵',
|
|
3
|
+
...'→←↔↑↓↕⇒⇐⇔⇑⇓⇕↦↤⟶⟵⟷⟹⟸⟺⟼↪↩⇀⇁↼↽⇌⇋⇄⇆↗↘↙↖:∶∷',
|
|
4
|
+
]);
|
|
5
|
+
const BIN = new Set([...'+-−±∓×÷·⋅∗∘∙∪∩∧∨⊕⊖⊗⊘⊙⊚⊛⊎⊓⊔⋄⋆⋉⋊⋋⋌⋎⋏⋒⋓△▽◁▷∖≀⊞⊟⊠⊡*']);
|
|
6
|
+
const OPEN = new Set([...'([{⟨⌈⌊⟦⟪⦃〈❲⁅']);
|
|
7
|
+
const CLOSE = new Set([...')]}⟩⌉⌋⟧⟫⦄〉❳⁆!']);
|
|
8
|
+
const PUNCT = new Set([...',;']);
|
|
9
|
+
const LARGE = new Set([...'∑∏∐∫∬∭∮∯∰∱∲∳⋀⋁⋂⋃⨀⨁⨂⨃⨄⨅⨆⨉⨌']);
|
|
10
|
+
export function charClass(ch) {
|
|
11
|
+
if (REL.has(ch))
|
|
12
|
+
return 'rel';
|
|
13
|
+
if (BIN.has(ch))
|
|
14
|
+
return 'bin';
|
|
15
|
+
if (OPEN.has(ch))
|
|
16
|
+
return 'open';
|
|
17
|
+
if (CLOSE.has(ch))
|
|
18
|
+
return 'close';
|
|
19
|
+
if (PUNCT.has(ch))
|
|
20
|
+
return 'punct';
|
|
21
|
+
if (LARGE.has(ch))
|
|
22
|
+
return 'op';
|
|
23
|
+
return 'ord';
|
|
24
|
+
}
|
|
25
|
+
/** Word shows these typed characters as their mathematical forms. */
|
|
26
|
+
const SHOWN = { '-': '−', '*': '∗', "'": '′' };
|
|
27
|
+
export function displayChar(ch) {
|
|
28
|
+
return SHOWN[ch] ?? ch;
|
|
29
|
+
}
|
|
30
|
+
/** Look-alikes for symbols no registered font has (the bundled fonts lack many math symbols). */
|
|
31
|
+
export const GLYPH_FALLBACK = {
|
|
32
|
+
'⋅': '·',
|
|
33
|
+
'∙': '·',
|
|
34
|
+
'∗': '*',
|
|
35
|
+
'∼': '~',
|
|
36
|
+
'∣': '|',
|
|
37
|
+
'∥': '‖',
|
|
38
|
+
'−': '-',
|
|
39
|
+
'′': "'",
|
|
40
|
+
'″': '"',
|
|
41
|
+
'∶': ':',
|
|
42
|
+
'⋯': '…',
|
|
43
|
+
'⟶': '→',
|
|
44
|
+
'⟵': '←',
|
|
45
|
+
'⟷': '↔',
|
|
46
|
+
'⟹': '⇒',
|
|
47
|
+
'〈': '⟨',
|
|
48
|
+
'〉': '⟩',
|
|
49
|
+
};
|
|
50
|
+
/** Function names Word sets upright when they appear as a whole word. */
|
|
51
|
+
export const FUNCTION_NAMES = new Set([
|
|
52
|
+
'sin', 'cos', 'tan', 'cot', 'sec', 'csc', 'sinh', 'cosh', 'tanh', 'coth', 'sech', 'csch',
|
|
53
|
+
'arcsin', 'arccos', 'arctan', 'arccot', 'arcsec', 'arccsc', 'arsinh', 'arcosh', 'artanh',
|
|
54
|
+
'log', 'ln', 'lg', 'exp', 'lim', 'liminf', 'limsup', 'max', 'min', 'sup', 'inf', 'det', 'dim',
|
|
55
|
+
'ker', 'deg', 'gcd', 'lcm', 'arg', 'hom', 'Pr', 'mod', 'sgn', 'tr', 'rank', 'Re', 'Im', 'erf',
|
|
56
|
+
]);
|
|
57
|
+
export function isDigit(ch) {
|
|
58
|
+
return ch >= '0' && ch <= '9';
|
|
59
|
+
}
|
|
60
|
+
/** Latin and Greek letters: the characters Word's math italic applies to. */
|
|
61
|
+
export function isLetter(cp) {
|
|
62
|
+
return ((cp >= 0x41 && cp <= 0x5a) ||
|
|
63
|
+
(cp >= 0x61 && cp <= 0x7a) ||
|
|
64
|
+
(cp >= 0xc0 && cp <= 0x24f && cp !== 0xd7 && cp !== 0xf7) ||
|
|
65
|
+
(cp >= 0x391 && cp <= 0x3a9) ||
|
|
66
|
+
(cp >= 0x3b1 && cp <= 0x3c9) ||
|
|
67
|
+
(cp >= 0x3d0 && cp <= 0x3d6) ||
|
|
68
|
+
(cp >= 0x3f0 && cp <= 0x3f5));
|
|
69
|
+
}
|
|
70
|
+
export function isSpace(ch) {
|
|
71
|
+
return ch === ' ' || ch === ' ' || ch === ' ' || ch === ' ' || ch === ' ' || ch === ' ' || ch === ' ' || ch === ' ';
|
|
72
|
+
}
|
|
73
|
+
/** Latin block order: [script, bold, italic] for each 52-letter run from U+1D400. */
|
|
74
|
+
const LATIN_STYLES = [
|
|
75
|
+
['roman', true, false],
|
|
76
|
+
['roman', false, true],
|
|
77
|
+
['roman', true, true],
|
|
78
|
+
['script', false, false],
|
|
79
|
+
['script', true, false],
|
|
80
|
+
['fraktur', false, false],
|
|
81
|
+
['double-struck', false, false],
|
|
82
|
+
['fraktur', true, false],
|
|
83
|
+
['sans-serif', false, false],
|
|
84
|
+
['sans-serif', true, false],
|
|
85
|
+
['sans-serif', false, true],
|
|
86
|
+
['sans-serif', true, true],
|
|
87
|
+
['monospace', false, false],
|
|
88
|
+
];
|
|
89
|
+
const GREEK_STYLES = [
|
|
90
|
+
['roman', true, false],
|
|
91
|
+
['roman', false, true],
|
|
92
|
+
['roman', true, true],
|
|
93
|
+
['sans-serif', true, false],
|
|
94
|
+
['sans-serif', true, true],
|
|
95
|
+
];
|
|
96
|
+
const GREEK_ORDER = [...'ΑΒΓΔΕΖΗΘϴΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ∇αβγδεζηθικλμνξοπρςστυφχψω∂ϵϑϰϕϱϖ'];
|
|
97
|
+
const DIGIT_STYLES = [
|
|
98
|
+
['roman', true],
|
|
99
|
+
['double-struck', false],
|
|
100
|
+
['sans-serif', false],
|
|
101
|
+
['sans-serif', true],
|
|
102
|
+
['monospace', false],
|
|
103
|
+
];
|
|
104
|
+
/** Letters the block leaves out because they were encoded earlier (ℎ, ℬ, ℂ…). */
|
|
105
|
+
const HOLES = {
|
|
106
|
+
'1:h': 0x210e,
|
|
107
|
+
'3:B': 0x212c, '3:E': 0x2130, '3:F': 0x2131, '3:H': 0x210b, '3:I': 0x2110, '3:L': 0x2112, '3:M': 0x2133, '3:R': 0x211b,
|
|
108
|
+
'3:e': 0x212f, '3:g': 0x210a, '3:o': 0x2134,
|
|
109
|
+
'5:C': 0x212d, '5:H': 0x210c, '5:I': 0x2111, '5:R': 0x211c, '5:Z': 0x2128,
|
|
110
|
+
'6:C': 0x2102, '6:H': 0x210d, '6:N': 0x2115, '6:P': 0x2119, '6:Q': 0x211a, '6:R': 0x211d, '6:Z': 0x2124,
|
|
111
|
+
};
|
|
112
|
+
const HOLE_DECODE = new Map(Object.entries(HOLES).map(([k, cp]) => [cp, k]));
|
|
113
|
+
export function decodeMathAlpha(cp) {
|
|
114
|
+
const hole = HOLE_DECODE.get(cp);
|
|
115
|
+
if (hole) {
|
|
116
|
+
const [index, base] = hole.split(':');
|
|
117
|
+
const [script, bold, italic] = LATIN_STYLES[Number(index)];
|
|
118
|
+
return { base, script, bold, italic };
|
|
119
|
+
}
|
|
120
|
+
if (cp >= 0x1d400 && cp < 0x1d400 + 13 * 52) {
|
|
121
|
+
const i = cp - 0x1d400;
|
|
122
|
+
const [script, bold, italic] = LATIN_STYLES[Math.floor(i / 52)];
|
|
123
|
+
const k = i % 52;
|
|
124
|
+
return { base: String.fromCharCode(k < 26 ? 65 + k : 97 + k - 26), script, bold, italic };
|
|
125
|
+
}
|
|
126
|
+
if (cp === 0x1d6a4 || cp === 0x1d6a5)
|
|
127
|
+
return { base: cp === 0x1d6a4 ? 'ı' : 'ȷ', script: 'roman', bold: false, italic: true };
|
|
128
|
+
if (cp >= 0x1d6a8 && cp < 0x1d6a8 + 5 * 58) {
|
|
129
|
+
const i = cp - 0x1d6a8;
|
|
130
|
+
const [script, bold, italic] = GREEK_STYLES[Math.floor(i / 58)];
|
|
131
|
+
return { base: GREEK_ORDER[i % 58], script, bold, italic };
|
|
132
|
+
}
|
|
133
|
+
if (cp >= 0x1d7ce && cp <= 0x1d7ff) {
|
|
134
|
+
const i = cp - 0x1d7ce;
|
|
135
|
+
const [script, bold] = DIGIT_STYLES[Math.floor(i / 10)];
|
|
136
|
+
return { base: String(i % 10), script, bold, italic: false };
|
|
137
|
+
}
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
/** The mathematical alphanumeric for a letter or digit in a style, when Unicode has one. */
|
|
141
|
+
export function encodeMathAlpha(base, script, bold, italic) {
|
|
142
|
+
const cp = base.codePointAt(0);
|
|
143
|
+
if (isDigitCp(cp)) {
|
|
144
|
+
const i = DIGIT_STYLES.findIndex(([s, b]) => s === script && b === bold);
|
|
145
|
+
return i < 0 || (script === 'roman' && !bold) ? undefined : 0x1d7ce + i * 10 + (cp - 48);
|
|
146
|
+
}
|
|
147
|
+
const latin = (cp >= 65 && cp <= 90) || (cp >= 97 && cp <= 122);
|
|
148
|
+
if (latin) {
|
|
149
|
+
// Styles without a bold or italic variant fall back to the plain one.
|
|
150
|
+
let i = LATIN_STYLES.findIndex(([s, b, it]) => s === script && b === bold && it === italic);
|
|
151
|
+
if (i < 0)
|
|
152
|
+
i = LATIN_STYLES.findIndex(([s, b]) => s === script && b === bold);
|
|
153
|
+
if (i < 0)
|
|
154
|
+
i = LATIN_STYLES.findIndex(([s]) => s === script);
|
|
155
|
+
if (i < 0)
|
|
156
|
+
return undefined;
|
|
157
|
+
const hole = HOLES[`${i}:${base}`];
|
|
158
|
+
if (hole)
|
|
159
|
+
return hole;
|
|
160
|
+
return 0x1d400 + i * 52 + (cp <= 90 ? cp - 65 : 26 + cp - 97);
|
|
161
|
+
}
|
|
162
|
+
const g = GREEK_ORDER.indexOf(base);
|
|
163
|
+
if (g >= 0) {
|
|
164
|
+
const i = GREEK_STYLES.findIndex(([s, b, it]) => s === script && b === bold && it === italic);
|
|
165
|
+
return i < 0 ? undefined : 0x1d6a8 + i * 58 + g;
|
|
166
|
+
}
|
|
167
|
+
return undefined;
|
|
168
|
+
}
|
|
169
|
+
function isDigitCp(cp) {
|
|
170
|
+
return cp >= 48 && cp <= 57;
|
|
171
|
+
}
|
|
172
|
+
/** Stretchy delimiters. `side` is the side the character opens from (its outer edge). */
|
|
173
|
+
const DELIMITERS = {
|
|
174
|
+
'(': { kind: 'paren', side: 'left' },
|
|
175
|
+
')': { kind: 'paren', side: 'right' },
|
|
176
|
+
'[': { kind: 'bracket', side: 'left' },
|
|
177
|
+
']': { kind: 'bracket', side: 'right' },
|
|
178
|
+
'{': { kind: 'brace', side: 'left' },
|
|
179
|
+
'}': { kind: 'brace', side: 'right' },
|
|
180
|
+
'⌈': { kind: 'ceil', side: 'left' },
|
|
181
|
+
'⌉': { kind: 'ceil', side: 'right' },
|
|
182
|
+
'⌊': { kind: 'floor', side: 'left' },
|
|
183
|
+
'⌋': { kind: 'floor', side: 'right' },
|
|
184
|
+
'⟨': { kind: 'angle', side: 'left' },
|
|
185
|
+
'⟩': { kind: 'angle', side: 'right' },
|
|
186
|
+
'〈': { kind: 'angle', side: 'left' },
|
|
187
|
+
'〉': { kind: 'angle', side: 'right' },
|
|
188
|
+
'|': { kind: 'bar', side: 'both' },
|
|
189
|
+
'∣': { kind: 'bar', side: 'both' },
|
|
190
|
+
'│': { kind: 'bar', side: 'both' },
|
|
191
|
+
'‖': { kind: 'dbar', side: 'both' },
|
|
192
|
+
'∥': { kind: 'dbar', side: 'both' },
|
|
193
|
+
};
|
|
194
|
+
export function delimiterOf(ch) {
|
|
195
|
+
return DELIMITERS[ch];
|
|
196
|
+
}
|
|
197
|
+
/** Horizontal stretchy characters of m:groupChr: the shape and the side its outer edge faces. */
|
|
198
|
+
const GROUP = {
|
|
199
|
+
'⏞': { kind: 'brace', outer: 'top' },
|
|
200
|
+
'⏟': { kind: 'brace', outer: 'bottom' },
|
|
201
|
+
'⏜': { kind: 'paren', outer: 'top' },
|
|
202
|
+
'⏝': { kind: 'paren', outer: 'bottom' },
|
|
203
|
+
'⎴': { kind: 'bracket', outer: 'top' },
|
|
204
|
+
'⎵': { kind: 'bracket', outer: 'bottom' },
|
|
205
|
+
'︷': { kind: 'brace', outer: 'top' },
|
|
206
|
+
'︸': { kind: 'brace', outer: 'bottom' },
|
|
207
|
+
};
|
|
208
|
+
export function groupShapeOf(ch) {
|
|
209
|
+
return GROUP[ch];
|
|
210
|
+
}
|
|
211
|
+
/** Arrows that stretch (group characters, accents): which ends get heads, and whether doubled. */
|
|
212
|
+
const ARROWS = {
|
|
213
|
+
'→': { left: false, right: true, double: false, harpoon: false },
|
|
214
|
+
'←': { left: true, right: false, double: false, harpoon: false },
|
|
215
|
+
'↔': { left: true, right: true, double: false, harpoon: false },
|
|
216
|
+
'⟶': { left: false, right: true, double: false, harpoon: false },
|
|
217
|
+
'⟵': { left: true, right: false, double: false, harpoon: false },
|
|
218
|
+
'⟷': { left: true, right: true, double: false, harpoon: false },
|
|
219
|
+
'⇒': { left: false, right: true, double: true, harpoon: false },
|
|
220
|
+
'⇐': { left: true, right: false, double: true, harpoon: false },
|
|
221
|
+
'⇔': { left: true, right: true, double: true, harpoon: false },
|
|
222
|
+
'⟹': { left: false, right: true, double: true, harpoon: false },
|
|
223
|
+
'⟸': { left: true, right: false, double: true, harpoon: false },
|
|
224
|
+
'⟺': { left: true, right: true, double: true, harpoon: false },
|
|
225
|
+
'⃗': { left: false, right: true, double: false, harpoon: false },
|
|
226
|
+
'⃖': { left: true, right: false, double: false, harpoon: false },
|
|
227
|
+
'⃡': { left: true, right: true, double: false, harpoon: false },
|
|
228
|
+
'⃑': { left: false, right: true, double: false, harpoon: true },
|
|
229
|
+
'⃐': { left: true, right: false, double: false, harpoon: true },
|
|
230
|
+
'⇀': { left: false, right: true, double: false, harpoon: true },
|
|
231
|
+
'↼': { left: true, right: false, double: false, harpoon: true },
|
|
232
|
+
};
|
|
233
|
+
export function arrowOf(ch) {
|
|
234
|
+
return ARROWS[ch];
|
|
235
|
+
}
|
|
236
|
+
const ACCENTS = {
|
|
237
|
+
'̂': 'hat', '^': 'hat', 'ˆ': 'hat',
|
|
238
|
+
'̌': 'check', 'ˇ': 'check',
|
|
239
|
+
'̃': 'tilde', '~': 'tilde', '˜': 'tilde',
|
|
240
|
+
'̄': 'bar', '̅': 'bar', '¯': 'bar', '‾': 'bar',
|
|
241
|
+
'̇': 'dot', '˙': 'dot',
|
|
242
|
+
'̈': 'ddot', '¨': 'ddot',
|
|
243
|
+
'⃛': 'dddot',
|
|
244
|
+
'́': 'acute', '´': 'acute',
|
|
245
|
+
'̀': 'grave', '`': 'grave',
|
|
246
|
+
'̆': 'breve', '˘': 'breve',
|
|
247
|
+
'̊': 'ring', '˚': 'ring',
|
|
248
|
+
};
|
|
249
|
+
export function accentOf(ch) {
|
|
250
|
+
return ACCENTS[ch] ?? (ARROWS[ch] ? 'arrow' : undefined);
|
|
251
|
+
}
|
|
252
|
+
/** Integral signs: how many strokes, and whether a contour circle crosses them. */
|
|
253
|
+
const INTEGRALS = {
|
|
254
|
+
'∫': { count: 1, contour: false },
|
|
255
|
+
'∬': { count: 2, contour: false },
|
|
256
|
+
'∭': { count: 3, contour: false },
|
|
257
|
+
'⨌': { count: 4, contour: false },
|
|
258
|
+
'∮': { count: 1, contour: true },
|
|
259
|
+
'∯': { count: 2, contour: true },
|
|
260
|
+
'∰': { count: 3, contour: true },
|
|
261
|
+
'∱': { count: 1, contour: true },
|
|
262
|
+
'∲': { count: 1, contour: true },
|
|
263
|
+
'∳': { count: 1, contour: true },
|
|
264
|
+
};
|
|
265
|
+
export function integralOf(ch) {
|
|
266
|
+
return INTEGRALS[ch];
|
|
267
|
+
}
|
|
268
|
+
/** Big operators layout always draws (the bundled fonts lack them, or their glyphs would not scale well). */
|
|
269
|
+
export const DRAWN_OPERATORS = new Set([...'∐⋃⋂⋁⋀⨀⨁⨂⨄⨆⨅']);
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Text in equations: picking fonts and code points for math runs (Word's math italic, m:sty,
|
|
3
|
+
* m:scr, m:nor and run formatting), splitting runs into atoms for operator spacing, and reading
|
|
4
|
+
* glyph ink bounds, which place scripts, limits and fraction parts far more tightly than the
|
|
5
|
+
* font's line ascent and descent would.
|
|
6
|
+
*/
|
|
7
|
+
import type { FontProvider, LayoutFont } from '../layout/types.js';
|
|
8
|
+
import type { XmlElement } from '../xml/dom.js';
|
|
9
|
+
import type { Atom, Box, Ctx } from './boxes.js';
|
|
10
|
+
export declare class MathFonts {
|
|
11
|
+
readonly provider: FontProvider;
|
|
12
|
+
private readonly cache;
|
|
13
|
+
constructor(provider: FontProvider);
|
|
14
|
+
family(name: string, bold: boolean, italic: boolean): LayoutFont;
|
|
15
|
+
/** The first font of the math chain (after `family`, when given) that has the character. */
|
|
16
|
+
pick(cp: number, bold: boolean, italic: boolean, family?: string): LayoutFont;
|
|
17
|
+
/** Whether some registered font can show the character. */
|
|
18
|
+
canShow(cp: number): boolean;
|
|
19
|
+
}
|
|
20
|
+
/** Ink bounds of a glyph in em, y up. */
|
|
21
|
+
export interface Ink {
|
|
22
|
+
xMin: number;
|
|
23
|
+
yMin: number;
|
|
24
|
+
xMax: number;
|
|
25
|
+
yMax: number;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Exact bounds from the font's glyf table (null for a glyph without ink, undefined when the
|
|
29
|
+
* font file is not available, as with canvas-measured fonts, or has CFF outlines).
|
|
30
|
+
*/
|
|
31
|
+
export declare function fontInk(font: LayoutFont, cp: number): Ink | null | undefined;
|
|
32
|
+
export declare function inkOf(font: LayoutFont, cp: number): Ink | null;
|
|
33
|
+
/** Height of the math axis (em): the centre of the minus sign, which fraction bars line up with. */
|
|
34
|
+
export declare function axisHeight(fonts: MathFonts): number;
|
|
35
|
+
export interface Glyph {
|
|
36
|
+
/** The character as drawn (one code point). */
|
|
37
|
+
ch: string;
|
|
38
|
+
font: LayoutFont;
|
|
39
|
+
}
|
|
40
|
+
/** A box of glyphs on the baseline; neighbouring glyphs of one font share a text item. */
|
|
41
|
+
export declare function glyphsBox(glyphs: Glyph[], size: number, color: string, simple?: boolean): Box;
|
|
42
|
+
/** A symbol in the math fonts, with a look-alike when no font has it. */
|
|
43
|
+
export declare function symbolGlyph(ch: string, fonts: MathFonts, bold: boolean): Glyph;
|
|
44
|
+
/** A string of symbols in the math fonts (delimiter glyphs, the lin fraction slash, fallbacks). */
|
|
45
|
+
export declare function symbolBox(text: string, size: number, color: string, fonts: MathFonts, bold?: boolean): Box;
|
|
46
|
+
/** A w:r inside an equation is plain text with its run formatting. */
|
|
47
|
+
export declare function textRunAtoms(run: XmlElement, ctx: Ctx): Atom[];
|
|
48
|
+
/**
|
|
49
|
+
* The atoms of a math run. Letters are italic unless styled or inside a function name, digits
|
|
50
|
+
* and operators upright; numbers and known function names stay whole; every operator is its own
|
|
51
|
+
* atom so the row can space it by class.
|
|
52
|
+
*/
|
|
53
|
+
export declare function runAtoms(run: XmlElement, ctx: Ctx): Atom[];
|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
import { measureChars, textItem } from '../layout/paint.js';
|
|
2
|
+
import { attr, firstChild } from '../xml/dom.js';
|
|
3
|
+
import { SCALE } from './boxes.js';
|
|
4
|
+
import { onOff, runText, wOnOff } from './omml.js';
|
|
5
|
+
import { FUNCTION_NAMES, GLYPH_FALLBACK, charClass, decodeMathAlpha, displayChar, encodeMathAlpha, isDigit, isLetter, isSpace, } from './symbols.js';
|
|
6
|
+
/**
|
|
7
|
+
* Word typesets equations in Cambria Math. Registries without it map it (and Cambria) to the
|
|
8
|
+
* metric-compatible Caladea; a serif with Greek comes next, then whatever font has the character.
|
|
9
|
+
*/
|
|
10
|
+
const MATH_FAMILIES = ['Cambria Math', 'Cambria', 'Times New Roman'];
|
|
11
|
+
export class MathFonts {
|
|
12
|
+
provider;
|
|
13
|
+
cache = new Map();
|
|
14
|
+
constructor(provider) {
|
|
15
|
+
this.provider = provider;
|
|
16
|
+
}
|
|
17
|
+
family(name, bold, italic) {
|
|
18
|
+
const key = `${name}|${bold}|${italic}`;
|
|
19
|
+
let font = this.cache.get(key);
|
|
20
|
+
if (!font) {
|
|
21
|
+
font = this.provider.get(name, bold, italic);
|
|
22
|
+
this.cache.set(key, font);
|
|
23
|
+
}
|
|
24
|
+
return font;
|
|
25
|
+
}
|
|
26
|
+
/** The first font of the math chain (after `family`, when given) that has the character. */
|
|
27
|
+
pick(cp, bold, italic, family) {
|
|
28
|
+
const chain = family ? [family, ...MATH_FAMILIES] : MATH_FAMILIES;
|
|
29
|
+
for (const name of chain) {
|
|
30
|
+
const font = this.family(name, bold, italic);
|
|
31
|
+
if (font.has(cp))
|
|
32
|
+
return font;
|
|
33
|
+
}
|
|
34
|
+
return this.provider.forChar(this.family(chain[0], bold, italic), cp);
|
|
35
|
+
}
|
|
36
|
+
/** Whether some registered font can show the character. */
|
|
37
|
+
canShow(cp) {
|
|
38
|
+
return this.pick(cp, false, false).has(cp);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
const inkCache = new WeakMap();
|
|
42
|
+
/**
|
|
43
|
+
* Exact bounds from the font's glyf table (null for a glyph without ink, undefined when the
|
|
44
|
+
* font file is not available, as with canvas-measured fonts, or has CFF outlines).
|
|
45
|
+
*/
|
|
46
|
+
export function fontInk(font, cp) {
|
|
47
|
+
const sfnt = font.face?.font;
|
|
48
|
+
if (!sfnt)
|
|
49
|
+
return undefined;
|
|
50
|
+
let cache = inkCache.get(font);
|
|
51
|
+
if (!cache) {
|
|
52
|
+
cache = new Map();
|
|
53
|
+
inkCache.set(font, cache);
|
|
54
|
+
}
|
|
55
|
+
if (cache.has(cp))
|
|
56
|
+
return cache.get(cp);
|
|
57
|
+
const range = sfnt.glyphRange(font.glyphId(cp));
|
|
58
|
+
if (!range)
|
|
59
|
+
return undefined;
|
|
60
|
+
let ink = null;
|
|
61
|
+
if (range.length >= 10) {
|
|
62
|
+
const view = new DataView(sfnt.data.buffer, sfnt.data.byteOffset + range.offset, 10);
|
|
63
|
+
const upem = sfnt.unitsPerEm;
|
|
64
|
+
ink = { xMin: view.getInt16(2) / upem, yMin: view.getInt16(4) / upem, xMax: view.getInt16(6) / upem, yMax: view.getInt16(8) / upem };
|
|
65
|
+
// Renderers slant a synthetic italic by about 12°.
|
|
66
|
+
if (font.syntheticItalic)
|
|
67
|
+
ink = { ...ink, xMin: ink.xMin + Math.min(0, ink.yMin) * 0.21, xMax: ink.xMax + Math.max(0, ink.yMax) * 0.21 };
|
|
68
|
+
}
|
|
69
|
+
cache.set(cp, ink);
|
|
70
|
+
return ink;
|
|
71
|
+
}
|
|
72
|
+
const ASCENDERS = new Set([...'bdfhklt']);
|
|
73
|
+
const DESCENDERS = new Set([...'gjpqyQ']);
|
|
74
|
+
/** Typical bounds when the font file is not available. */
|
|
75
|
+
function approximateInk(font, cp) {
|
|
76
|
+
const ch = String.fromCodePoint(cp);
|
|
77
|
+
if (isSpace(ch))
|
|
78
|
+
return null;
|
|
79
|
+
const xMax = font.advance(cp);
|
|
80
|
+
let yMax = 0.7;
|
|
81
|
+
let yMin = 0;
|
|
82
|
+
if (ch >= 'a' && ch <= 'z' && !ASCENDERS.has(ch))
|
|
83
|
+
yMax = 0.47;
|
|
84
|
+
if (DESCENDERS.has(ch) || '()[]{}|'.includes(ch))
|
|
85
|
+
yMin = -0.2;
|
|
86
|
+
if ('()[]{}|'.includes(ch))
|
|
87
|
+
yMax = 0.75;
|
|
88
|
+
if ('+−-=±×÷<>≤≥≠≈·∙'.includes(ch)) {
|
|
89
|
+
yMax = 0.5;
|
|
90
|
+
yMin = 0.05;
|
|
91
|
+
}
|
|
92
|
+
return { xMin: 0, yMin, xMax, yMax };
|
|
93
|
+
}
|
|
94
|
+
export function inkOf(font, cp) {
|
|
95
|
+
const exact = fontInk(font, cp);
|
|
96
|
+
return exact === undefined ? approximateInk(font, cp) : exact;
|
|
97
|
+
}
|
|
98
|
+
/** Height of the math axis (em): the centre of the minus sign, which fraction bars line up with. */
|
|
99
|
+
export function axisHeight(fonts) {
|
|
100
|
+
const minus = 0x2212;
|
|
101
|
+
const font = fonts.pick(minus, false, false);
|
|
102
|
+
const ink = font.has(minus) ? fontInk(font, minus) : undefined;
|
|
103
|
+
if (ink && ink.yMax > ink.yMin && ink.yMax < 0.6 && ink.yMin > 0.1)
|
|
104
|
+
return (ink.yMin + ink.yMax) / 2;
|
|
105
|
+
return 0.25;
|
|
106
|
+
}
|
|
107
|
+
/** A box of glyphs on the baseline; neighbouring glyphs of one font share a text item. */
|
|
108
|
+
export function glyphsBox(glyphs, size, color, simple = true) {
|
|
109
|
+
const items = [];
|
|
110
|
+
let x = 0;
|
|
111
|
+
let ascent = 0;
|
|
112
|
+
let descent = 0;
|
|
113
|
+
let italic = 0;
|
|
114
|
+
let i = 0;
|
|
115
|
+
while (i < glyphs.length) {
|
|
116
|
+
const font = glyphs[i].font;
|
|
117
|
+
let j = i + 1;
|
|
118
|
+
while (j < glyphs.length && glyphs[j].font === font)
|
|
119
|
+
j++;
|
|
120
|
+
const text = glyphs
|
|
121
|
+
.slice(i, j)
|
|
122
|
+
.map((g) => g.ch)
|
|
123
|
+
.join('');
|
|
124
|
+
const m = measureChars(text, font, size, x);
|
|
125
|
+
items.push(textItem(x, 0, m.chars, m.xs, m.widths, font, size, color));
|
|
126
|
+
m.chars.forEach((ch, k) => {
|
|
127
|
+
const cp = ch.codePointAt(0);
|
|
128
|
+
const ink = inkOf(font, cp);
|
|
129
|
+
if (!ink) {
|
|
130
|
+
italic = 0;
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
ascent = Math.max(ascent, ink.yMax * size);
|
|
134
|
+
descent = Math.max(descent, -ink.yMin * size);
|
|
135
|
+
italic = font.italic ? Math.max(0, ink.xMax * size - m.widths[k]) : 0;
|
|
136
|
+
});
|
|
137
|
+
x += m.width;
|
|
138
|
+
i = j;
|
|
139
|
+
}
|
|
140
|
+
return { width: x, ascent, descent, items, italic, simple };
|
|
141
|
+
}
|
|
142
|
+
/** A symbol in the math fonts, with a look-alike when no font has it. */
|
|
143
|
+
export function symbolGlyph(ch, fonts, bold) {
|
|
144
|
+
const cp = ch.codePointAt(0);
|
|
145
|
+
const font = fonts.pick(cp, bold, false);
|
|
146
|
+
if (font.has(cp))
|
|
147
|
+
return { ch, font };
|
|
148
|
+
const alt = GLYPH_FALLBACK[ch];
|
|
149
|
+
if (alt) {
|
|
150
|
+
const altFont = fonts.pick(alt.codePointAt(0), bold, false);
|
|
151
|
+
if (altFont.has(alt.codePointAt(0)))
|
|
152
|
+
return { ch: alt, font: altFont };
|
|
153
|
+
}
|
|
154
|
+
return { ch, font };
|
|
155
|
+
}
|
|
156
|
+
/** A string of symbols in the math fonts (delimiter glyphs, the lin fraction slash, fallbacks). */
|
|
157
|
+
export function symbolBox(text, size, color, fonts, bold = false) {
|
|
158
|
+
return glyphsBox([...text].map((ch) => symbolGlyph(ch, fonts, bold)), size, color);
|
|
159
|
+
}
|
|
160
|
+
const STY = new Set(['p', 'b', 'i', 'bi']);
|
|
161
|
+
const SCRIPTS = new Set(['roman', 'script', 'fraktur', 'double-struck', 'sans-serif', 'monospace']);
|
|
162
|
+
function readFormat(run) {
|
|
163
|
+
const m = firstChild(run, 'm:rPr');
|
|
164
|
+
const w = firstChild(run, 'w:rPr');
|
|
165
|
+
const sty = attr(firstChild(m, 'm:sty'), 'm:val');
|
|
166
|
+
const scr = attr(firstChild(m, 'm:scr'), 'm:val');
|
|
167
|
+
const color = attr(firstChild(w, 'w:color'), 'w:val');
|
|
168
|
+
const sz = Number(attr(firstChild(w, 'w:sz'), 'w:val'));
|
|
169
|
+
const fonts = firstChild(w, 'w:rFonts');
|
|
170
|
+
return {
|
|
171
|
+
sty: sty && STY.has(sty) ? sty : undefined,
|
|
172
|
+
scr: scr && SCRIPTS.has(scr) ? scr : undefined,
|
|
173
|
+
nor: onOff(m, 'm:nor', false),
|
|
174
|
+
color: color && /^[0-9a-fA-F]{6}$/.test(color) ? color.toUpperCase() : undefined,
|
|
175
|
+
size: Number.isFinite(sz) && sz > 0 ? sz / 2 : undefined,
|
|
176
|
+
family: attr(fonts, 'w:ascii') ?? attr(fonts, 'w:hAnsi'),
|
|
177
|
+
bold: wOnOff(w, 'w:b'),
|
|
178
|
+
italic: wOnOff(w, 'w:i'),
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/** Size of a run at the current level (w:sz replaces the base size). */
|
|
182
|
+
function runSize(f, ctx) {
|
|
183
|
+
return (f.size ?? ctx.base) * SCALE[ctx.level];
|
|
184
|
+
}
|
|
185
|
+
/** A letter in a math style: real math alphanumerics when a font has them, else an approximation. */
|
|
186
|
+
function letterGlyph(base, script, bold, italic, fonts) {
|
|
187
|
+
const cp = base.codePointAt(0);
|
|
188
|
+
if (script !== 'roman') {
|
|
189
|
+
const mapped = encodeMathAlpha(base, script, bold, italic);
|
|
190
|
+
if (mapped !== undefined) {
|
|
191
|
+
const font = fonts.pick(mapped, false, false);
|
|
192
|
+
if (font.has(mapped))
|
|
193
|
+
return { ch: String.fromCodePoint(mapped), font };
|
|
194
|
+
}
|
|
195
|
+
switch (script) {
|
|
196
|
+
case 'sans-serif':
|
|
197
|
+
return { ch: base, font: fonts.pick(cp, bold, italic, 'Calibri') };
|
|
198
|
+
case 'monospace':
|
|
199
|
+
return { ch: base, font: fonts.pick(cp, bold, italic, 'Courier New') };
|
|
200
|
+
case 'double-struck':
|
|
201
|
+
return { ch: base, font: fonts.pick(cp, true, false) };
|
|
202
|
+
case 'script':
|
|
203
|
+
return { ch: base, font: fonts.pick(cp, bold, true) };
|
|
204
|
+
default:
|
|
205
|
+
return { ch: base, font: fonts.pick(cp, bold, false) };
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (italic) {
|
|
209
|
+
// An upright math font with the math italic letters (the real Cambria Math has no italic face).
|
|
210
|
+
const mapped = encodeMathAlpha(base, 'roman', bold, true);
|
|
211
|
+
const math = fonts.family('Cambria Math', false, false);
|
|
212
|
+
if (mapped !== undefined && math.face && math.has(mapped))
|
|
213
|
+
return { ch: String.fromCodePoint(mapped), font: math };
|
|
214
|
+
}
|
|
215
|
+
return { ch: base, font: fonts.pick(cp, bold, italic) };
|
|
216
|
+
}
|
|
217
|
+
function atomOf(box, cls) {
|
|
218
|
+
return { box, left: cls, right: cls };
|
|
219
|
+
}
|
|
220
|
+
/** Normal text (m:nor, or a w:r inside an equation): upright, its own font, no operator spacing. */
|
|
221
|
+
function normalTextAtoms(text, f, ctx) {
|
|
222
|
+
if (!text)
|
|
223
|
+
return [];
|
|
224
|
+
const bold = f.bold ?? false;
|
|
225
|
+
const italic = f.italic ?? false;
|
|
226
|
+
const glyphs = [...text].map((ch) => ({ ch, font: ctx.fonts.pick(ch.codePointAt(0), bold, italic, f.family) }));
|
|
227
|
+
return [atomOf(glyphsBox(glyphs, runSize(f, ctx), f.color ?? ctx.color, false), 'ord')];
|
|
228
|
+
}
|
|
229
|
+
/** A w:r inside an equation is plain text with its run formatting. */
|
|
230
|
+
export function textRunAtoms(run, ctx) {
|
|
231
|
+
return normalTextAtoms(runText(run), readFormat(run), ctx);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* The atoms of a math run. Letters are italic unless styled or inside a function name, digits
|
|
235
|
+
* and operators upright; numbers and known function names stay whole; every operator is its own
|
|
236
|
+
* atom so the row can space it by class.
|
|
237
|
+
*/
|
|
238
|
+
export function runAtoms(run, ctx) {
|
|
239
|
+
const f = readFormat(run);
|
|
240
|
+
const text = runText(run);
|
|
241
|
+
if (!text)
|
|
242
|
+
return [];
|
|
243
|
+
if (f.nor)
|
|
244
|
+
return normalTextAtoms(text, f, ctx);
|
|
245
|
+
const { fonts } = ctx;
|
|
246
|
+
const size = runSize(f, ctx);
|
|
247
|
+
const color = f.color ?? ctx.color;
|
|
248
|
+
const bold = f.sty === 'b' || f.sty === 'bi';
|
|
249
|
+
const styItalic = f.sty === undefined ? undefined : f.sty === 'i' || f.sty === 'bi';
|
|
250
|
+
const script = f.scr ?? 'roman';
|
|
251
|
+
const chars = [...text];
|
|
252
|
+
const atoms = [];
|
|
253
|
+
const push = (glyphs, cls) => atoms.push(atomOf(glyphsBox(glyphs, size, color), cls));
|
|
254
|
+
let i = 0;
|
|
255
|
+
while (i < chars.length) {
|
|
256
|
+
const ch = chars[i];
|
|
257
|
+
const cp = ch.codePointAt(0);
|
|
258
|
+
const alpha = decodeMathAlpha(cp);
|
|
259
|
+
if (alpha) {
|
|
260
|
+
// A mathematical alphanumeric typed directly: keep it when a real font has it.
|
|
261
|
+
const font = fonts.pick(cp, false, false);
|
|
262
|
+
const glyph = font.has(cp) ? { ch, font } : letterGlyph(alpha.base, alpha.script, alpha.bold || bold, alpha.italic, fonts);
|
|
263
|
+
push([glyph], 'ord');
|
|
264
|
+
i++;
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (isDigit(ch)) {
|
|
268
|
+
let j = i + 1;
|
|
269
|
+
while (j < chars.length && (isDigit(chars[j]) || (chars[j] === '.' && j + 1 < chars.length && isDigit(chars[j + 1]))))
|
|
270
|
+
j++;
|
|
271
|
+
const glyphs = chars.slice(i, j).map((c) => letterGlyph(c, script, bold, styItalic === true, fonts));
|
|
272
|
+
push(glyphs, 'ord');
|
|
273
|
+
i = j;
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (isLetter(cp)) {
|
|
277
|
+
let j = i + 1;
|
|
278
|
+
while (j < chars.length && isLetter(chars[j].codePointAt(0)))
|
|
279
|
+
j++;
|
|
280
|
+
const word = chars.slice(i, j).join('');
|
|
281
|
+
if (styItalic !== true && j - i > 1 && FUNCTION_NAMES.has(word)) {
|
|
282
|
+
push(chars.slice(i, j).map((c) => letterGlyph(c, script, bold, false, fonts)), 'op');
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
const italic = styItalic ?? !ctx.fn;
|
|
286
|
+
for (let k = i; k < j; k++)
|
|
287
|
+
push([letterGlyph(chars[k], script, bold, italic, fonts)], 'ord');
|
|
288
|
+
}
|
|
289
|
+
i = j;
|
|
290
|
+
continue;
|
|
291
|
+
}
|
|
292
|
+
i++;
|
|
293
|
+
if (isSpace(ch)) {
|
|
294
|
+
const font = fonts.pick(32, bold, false);
|
|
295
|
+
atoms.push({ box: { width: font.advance(32) * size, ascent: 0, descent: 0, items: [], italic: 0 }, left: 'ord', right: 'ord', kind: 'glue' });
|
|
296
|
+
continue;
|
|
297
|
+
}
|
|
298
|
+
if (ch === '&' && ctx.align) {
|
|
299
|
+
atoms.push({ box: { width: 0, ascent: 0, descent: 0, items: [], italic: 0 }, left: 'ord', right: 'ord', kind: 'mark' });
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
// A run of primes is one atom so it reads ′′ rather than spaced symbols.
|
|
303
|
+
if (ch === "'" || ch === '′') {
|
|
304
|
+
let primes = '′';
|
|
305
|
+
while (i < chars.length && (chars[i] === "'" || chars[i] === '′')) {
|
|
306
|
+
primes += '′';
|
|
307
|
+
i++;
|
|
308
|
+
}
|
|
309
|
+
push([...primes].map((p) => symbolGlyph(p, fonts, bold)), 'ord');
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
const cls = charClass(ch);
|
|
313
|
+
push([symbolGlyph(displayChar(ch), fonts, bold)], cls);
|
|
314
|
+
}
|
|
315
|
+
return atoms;
|
|
316
|
+
}
|