docxkeun 0.1.3 → 0.2.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 +48 -0
- package/README.md +137 -16
- package/dist/api/editor.d.ts +172 -5
- package/dist/api/editor.js +814 -24
- package/dist/api/headers.d.ts +51 -0
- package/dist/api/headers.js +133 -0
- package/dist/api/index.d.ts +5 -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 +390 -0
- package/dist/api/selection.d.ts +57 -0
- package/dist/api/selection.js +91 -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 +138 -0
- package/dist/render/activity.js +794 -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 +302 -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,771 @@
|
|
|
1
|
+
import { measureChars, textItem } from '../layout/paint.js';
|
|
2
|
+
import { elements, firstChild } from '../xml/dom.js';
|
|
3
|
+
import { Builder, RULE, X_HEIGHT, em, emptyBox, fractionStyle, hpack, resolveBinaries, scriptStyle, shiftItems, spaceBetween } from './boxes.js';
|
|
4
|
+
import { charProp, ctrlColor, isPropertyElement, numProp, onOff, val } from './omml.js';
|
|
5
|
+
import { accentMetrics, accentPaths, arrowPaths, bigOperatorPaths, delimiterShape, delimiterWidth, ellipse, filled, horizontalFrame, integralShape, mapShape, polygons, radicalShape, strokeOutline, stroked, verticalFrame, } from './paths.js';
|
|
6
|
+
import { DRAWN_OPERATORS, accentOf, arrowOf, delimiterOf, groupShapeOf, integralOf } from './symbols.js';
|
|
7
|
+
import { inkOf, runAtoms, symbolBox, textRunAtoms } from './text.js';
|
|
8
|
+
/** Wrappers whose content belongs to the equation (tracked insertions, smart tags…). */
|
|
9
|
+
const WRAPPERS = new Set(['w:ins', 'w:moveTo', 'w:smartTag', 'w:customXml', 'w:hyperlink', 'w:fldSimple']);
|
|
10
|
+
function ord(box) {
|
|
11
|
+
return { box, left: 'ord', right: 'ord' };
|
|
12
|
+
}
|
|
13
|
+
function rect(x, y, w, h, color) {
|
|
14
|
+
return { type: 'rect', x, y, w, h, fill: color };
|
|
15
|
+
}
|
|
16
|
+
/** Whether an argument element holds anything besides properties. */
|
|
17
|
+
function hasContent(el) {
|
|
18
|
+
return !!el && elements(el).some((c) => !isPropertyElement(c.name));
|
|
19
|
+
}
|
|
20
|
+
export function rowAtoms(container, ctx) {
|
|
21
|
+
if (!container)
|
|
22
|
+
return [];
|
|
23
|
+
const out = [];
|
|
24
|
+
for (const child of elements(container))
|
|
25
|
+
for (const a of elementAtoms(child, ctx))
|
|
26
|
+
out.push(a);
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
/** Lay out an argument (m:e, m:num, m:sub, m:oMath…) as a spaced row. */
|
|
30
|
+
export function row(container, ctx, before) {
|
|
31
|
+
const atoms = rowAtoms(container, ctx);
|
|
32
|
+
resolveBinaries(atoms);
|
|
33
|
+
return hpack(atoms, ctx, before);
|
|
34
|
+
}
|
|
35
|
+
function elementAtoms(el, ctx) {
|
|
36
|
+
switch (el.name) {
|
|
37
|
+
case 'm:r':
|
|
38
|
+
return runAtoms(el, ctx);
|
|
39
|
+
case 'w:r':
|
|
40
|
+
return textRunAtoms(el, ctx);
|
|
41
|
+
case 'm:f':
|
|
42
|
+
return [fraction(el, ctx)];
|
|
43
|
+
case 'm:sSup':
|
|
44
|
+
case 'm:sSub':
|
|
45
|
+
case 'm:sSubSup':
|
|
46
|
+
return [scripted(el, ctx)];
|
|
47
|
+
case 'm:sPre':
|
|
48
|
+
return [prescripted(el, ctx)];
|
|
49
|
+
case 'm:rad':
|
|
50
|
+
return [radical(el, ctx)];
|
|
51
|
+
case 'm:nary':
|
|
52
|
+
return [nary(el, ctx)];
|
|
53
|
+
case 'm:d':
|
|
54
|
+
return [delimited(el, ctx)];
|
|
55
|
+
case 'm:m':
|
|
56
|
+
return [matrix(el, ctx)];
|
|
57
|
+
case 'm:acc':
|
|
58
|
+
return [accent(el, ctx)];
|
|
59
|
+
case 'm:bar':
|
|
60
|
+
return [overUnderBar(el, ctx)];
|
|
61
|
+
case 'm:func':
|
|
62
|
+
return [func(el, ctx)];
|
|
63
|
+
case 'm:limLow':
|
|
64
|
+
case 'm:limUpp':
|
|
65
|
+
return [limit(el, ctx)];
|
|
66
|
+
case 'm:groupChr':
|
|
67
|
+
return [groupChr(el, ctx)];
|
|
68
|
+
case 'm:box':
|
|
69
|
+
return [boxed(el, ctx)];
|
|
70
|
+
case 'm:borderBox':
|
|
71
|
+
return [borderBox(el, ctx)];
|
|
72
|
+
case 'm:eqArr':
|
|
73
|
+
return [eqArray(el, ctx)];
|
|
74
|
+
case 'm:phant':
|
|
75
|
+
return [phantom(el, ctx)];
|
|
76
|
+
case 'w:sdt':
|
|
77
|
+
return rowAtoms(firstChild(el, 'w:sdtContent'), ctx);
|
|
78
|
+
default:
|
|
79
|
+
if (isPropertyElement(el.name))
|
|
80
|
+
return [];
|
|
81
|
+
// Unknown math elements still show their content; other markup (bookmarks, deletions) does not.
|
|
82
|
+
if (el.name.startsWith('m:') || WRAPPERS.has(el.name))
|
|
83
|
+
return rowAtoms(el, ctx);
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
// ---------------------------------------------------------------------------------------------
|
|
88
|
+
// Fractions.
|
|
89
|
+
function fraction(el, ctx) {
|
|
90
|
+
const pr = firstChild(el, 'm:fPr');
|
|
91
|
+
const type = val(pr, 'm:type') ?? 'bar';
|
|
92
|
+
const numEl = firstChild(el, 'm:num');
|
|
93
|
+
const denEl = firstChild(el, 'm:den');
|
|
94
|
+
const s = em(ctx);
|
|
95
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
96
|
+
const axis = ctx.axis * s;
|
|
97
|
+
if (type === 'lin') {
|
|
98
|
+
// Linear: num/den on the baseline at full size.
|
|
99
|
+
const num = row(numEl, ctx).box;
|
|
100
|
+
const den = row(denEl, ctx).box;
|
|
101
|
+
const slash = symbolBox('/', s, color, ctx.fonts);
|
|
102
|
+
const b = new Builder();
|
|
103
|
+
b.put(num, 0, 0);
|
|
104
|
+
b.put(slash, num.width, 0);
|
|
105
|
+
b.put(den, num.width + slash.width, 0);
|
|
106
|
+
return ord(b.done());
|
|
107
|
+
}
|
|
108
|
+
if (type === 'skw') {
|
|
109
|
+
// Skewed: small numerator up left, denominator down right, a slash between them.
|
|
110
|
+
const num = row(numEl, scriptStyle(ctx)).box;
|
|
111
|
+
const den = row(denEl, scriptStyle(ctx, true)).box;
|
|
112
|
+
const up = axis + num.descent + 0.04 * s;
|
|
113
|
+
const down = den.ascent - axis + 0.04 * s;
|
|
114
|
+
const top = up + num.ascent;
|
|
115
|
+
const bottom = -(down + den.descent);
|
|
116
|
+
const slant = 0.32 * (top - bottom);
|
|
117
|
+
const gap = 0.04 * s;
|
|
118
|
+
const mid = num.width + gap + slant / 2;
|
|
119
|
+
const b = new Builder();
|
|
120
|
+
b.put(num, 0, -up);
|
|
121
|
+
const t = RULE * s * 0.9;
|
|
122
|
+
const outline = strokeOutline([[mid - slant / 2, bottom], [mid + slant / 2, top]], [t]);
|
|
123
|
+
b.draw(filled(polygons([outline]), color), -top, -bottom);
|
|
124
|
+
b.put(den, mid + slant / 2 + gap, down);
|
|
125
|
+
return ord(b.done());
|
|
126
|
+
}
|
|
127
|
+
const display = ctx.display && ctx.level === 0;
|
|
128
|
+
const num = row(numEl, fractionStyle(ctx, ctx.cramped)).box;
|
|
129
|
+
const den = row(denEl, fractionStyle(ctx, true)).box;
|
|
130
|
+
const th = RULE * s;
|
|
131
|
+
const gap = (display ? 0.12 : 0.07) * s;
|
|
132
|
+
// Numerator and denominator clear the bar by `gap`, with minimum shifts so that fractions
|
|
133
|
+
// side by side line up (TeX's num/denom parameters, a little tighter, as in Word).
|
|
134
|
+
const u = Math.max(axis + th / 2 + gap + num.descent, (display ? 0.5 : 0.36) * s);
|
|
135
|
+
const v = Math.max(den.ascent + gap + th / 2 - axis, (display ? 0.45 : 0.3) * s);
|
|
136
|
+
const over = 0.07 * s;
|
|
137
|
+
const margin = 0.03 * s;
|
|
138
|
+
const inner = Math.max(num.width, den.width) + 2 * over;
|
|
139
|
+
const b = new Builder();
|
|
140
|
+
b.put(num, margin + (inner - num.width) / 2, -u);
|
|
141
|
+
b.put(den, margin + (inner - den.width) / 2, v);
|
|
142
|
+
if (type !== 'noBar')
|
|
143
|
+
b.draw(rect(margin, -(axis + th / 2), inner, th, color), -(axis + th / 2), -(axis - th / 2));
|
|
144
|
+
return ord(b.done(inner + 2 * margin));
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Attach scripts to a base (TeX rules 18a–f): a character base uses fixed shifts, a structure
|
|
148
|
+
* shifts scripts from its own top and bottom; superscript and subscript keep a minimum gap.
|
|
149
|
+
*/
|
|
150
|
+
function attachScripts(base, sub, sup, ctx, o = {}) {
|
|
151
|
+
const s = em(ctx);
|
|
152
|
+
const simple = base.simple === true;
|
|
153
|
+
const u0 = simple ? 0 : base.ascent - 0.28 * s;
|
|
154
|
+
const v0 = simple ? 0 : base.descent + 0.05 * s;
|
|
155
|
+
let u = 0;
|
|
156
|
+
let v = 0;
|
|
157
|
+
if (sup)
|
|
158
|
+
u = Math.max(u0, (ctx.cramped ? 0.29 : 0.35) * s, sup.descent + 0.12 * s);
|
|
159
|
+
if (sub && !sup)
|
|
160
|
+
v = Math.max(v0, 0.2 * s, sub.ascent - 0.8 * X_HEIGHT * s);
|
|
161
|
+
if (sub && sup) {
|
|
162
|
+
v = Math.max(v0, 0.25 * s);
|
|
163
|
+
const minGap = 0.16 * s;
|
|
164
|
+
const gap = u - sup.descent - (sub.ascent - v);
|
|
165
|
+
if (gap < minGap) {
|
|
166
|
+
v += minGap - gap;
|
|
167
|
+
const psi = 0.8 * X_HEIGHT * s - (u - sup.descent);
|
|
168
|
+
if (psi > 0) {
|
|
169
|
+
u += psi;
|
|
170
|
+
v -= psi;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
const space = 0.04 * s;
|
|
175
|
+
const b = new Builder();
|
|
176
|
+
if (o.pre) {
|
|
177
|
+
const w = Math.max(sup?.width ?? 0, sub?.width ?? 0);
|
|
178
|
+
if (sup)
|
|
179
|
+
b.put(sup, w - sup.width, -u);
|
|
180
|
+
if (sub)
|
|
181
|
+
b.put(sub, w - sub.width, v);
|
|
182
|
+
b.put(base, w + space, 0);
|
|
183
|
+
return b.done(w + space + base.width, base.italic);
|
|
184
|
+
}
|
|
185
|
+
const baseWidth = base.width;
|
|
186
|
+
const italic = base.italic;
|
|
187
|
+
b.put(base, 0, 0);
|
|
188
|
+
const supX = baseWidth + (o.aligned ? 0 : italic);
|
|
189
|
+
const subX = baseWidth - (o.subKern ?? 0);
|
|
190
|
+
if (sup)
|
|
191
|
+
b.put(sup, supX, -u);
|
|
192
|
+
if (sub)
|
|
193
|
+
b.put(sub, subX, v);
|
|
194
|
+
const right = Math.max(baseWidth, sup ? supX + sup.width : 0, sub ? subX + sub.width : 0);
|
|
195
|
+
return b.done(right + (sub || sup ? space : 0));
|
|
196
|
+
}
|
|
197
|
+
function scripted(el, ctx) {
|
|
198
|
+
const base = row(firstChild(el, 'm:e'), ctx);
|
|
199
|
+
const supEl = el.name === 'm:sSub' ? undefined : firstChild(el, 'm:sup');
|
|
200
|
+
const subEl = el.name === 'm:sSup' ? undefined : firstChild(el, 'm:sub');
|
|
201
|
+
const sup = supEl ? row(supEl, scriptStyle(ctx)).box : undefined;
|
|
202
|
+
const sub = subEl ? row(subEl, scriptStyle(ctx, true)).box : undefined;
|
|
203
|
+
const aligned = onOff(firstChild(el, 'm:sSubSupPr'), 'm:alnScr', false);
|
|
204
|
+
const box = attachScripts(base.box, sub, sup, ctx, { aligned });
|
|
205
|
+
// A script keeps its base's class: sin² is still an operator, (x)² still closes.
|
|
206
|
+
return { box, left: base.first ?? 'ord', right: base.last ?? 'ord' };
|
|
207
|
+
}
|
|
208
|
+
function prescripted(el, ctx) {
|
|
209
|
+
const base = row(firstChild(el, 'm:e'), ctx);
|
|
210
|
+
const subEl = firstChild(el, 'm:sub');
|
|
211
|
+
const supEl = firstChild(el, 'm:sup');
|
|
212
|
+
const sub = hasContent(subEl) ? row(subEl, scriptStyle(ctx, true)).box : undefined;
|
|
213
|
+
const sup = hasContent(supEl) ? row(supEl, scriptStyle(ctx)).box : undefined;
|
|
214
|
+
return { box: attachScripts(base.box, sub, sup, ctx, { pre: true }), left: 'ord', right: base.last ?? 'ord' };
|
|
215
|
+
}
|
|
216
|
+
/** Limits centred over and under a base (n-ary limits in display, m:limLow, m:limUpp). */
|
|
217
|
+
function stackLimits(base, under, over, overGap, underGap) {
|
|
218
|
+
const w = Math.max(base.width, under?.width ?? 0, over?.width ?? 0);
|
|
219
|
+
const b = new Builder();
|
|
220
|
+
const baseAscent = base.ascent;
|
|
221
|
+
const baseDescent = base.descent;
|
|
222
|
+
b.put(base, (w - base.width) / 2, 0);
|
|
223
|
+
if (over)
|
|
224
|
+
b.put(over, (w - over.width) / 2, -(baseAscent + overGap + over.descent));
|
|
225
|
+
if (under)
|
|
226
|
+
b.put(under, (w - under.width) / 2, baseDescent + underGap + under.ascent);
|
|
227
|
+
return b.done(w);
|
|
228
|
+
}
|
|
229
|
+
// ---------------------------------------------------------------------------------------------
|
|
230
|
+
// Radicals.
|
|
231
|
+
function radical(el, ctx) {
|
|
232
|
+
const pr = firstChild(el, 'm:radPr');
|
|
233
|
+
const s = em(ctx);
|
|
234
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
235
|
+
const body = row(firstChild(el, 'm:e'), { ...ctx, cramped: true }).box;
|
|
236
|
+
const degEl = firstChild(el, 'm:deg');
|
|
237
|
+
const deg = !onOff(pr, 'm:degHide', false) && hasContent(degEl) ? row(degEl, { ...ctx, level: 2, display: false, cramped: true, align: false }).box : undefined;
|
|
238
|
+
const display = ctx.display && ctx.level === 0;
|
|
239
|
+
const rule = RULE * s;
|
|
240
|
+
const gap = (display ? 0.12 : 0.08) * s;
|
|
241
|
+
const top = Math.max(body.ascent, X_HEIGHT * s) + gap + rule;
|
|
242
|
+
// The sign reaches a little below the content and is never shorter than a text radical.
|
|
243
|
+
const depth = Math.max(body.descent + 0.06 * s, 0.9 * s - top);
|
|
244
|
+
const height = top + depth;
|
|
245
|
+
const signWidth = Math.min(0.56 + 0.1 * Math.max(0, height / s - 1.2), 0.95) * s;
|
|
246
|
+
const b = new Builder();
|
|
247
|
+
let x0 = 0;
|
|
248
|
+
if (deg) {
|
|
249
|
+
// The degree sits over the hook, its right edge above the downstroke, raised to 60% of the
|
|
250
|
+
// sign; a wide degree pushes the sign right.
|
|
251
|
+
const anchor = 0.3 * s;
|
|
252
|
+
x0 = Math.max(0, deg.width - anchor + 0.03 * s);
|
|
253
|
+
const hook = Math.min(0.48 * s, height * 0.5);
|
|
254
|
+
const baseline = Math.max(-depth + 0.6 * height, -depth + hook + 0.08 * s) + deg.descent;
|
|
255
|
+
b.put(deg, x0 + anchor - deg.width, -baseline);
|
|
256
|
+
}
|
|
257
|
+
const bodyX = x0 + signWidth + 0.04 * s;
|
|
258
|
+
const barEnd = bodyX + body.width + 0.06 * s;
|
|
259
|
+
b.put(body, bodyX, 0);
|
|
260
|
+
b.draw(filled(polygons([radicalShape(x0, -depth, signWidth, top, barEnd, rule, s)]), color), -top, depth + 0.05 * s);
|
|
261
|
+
return ord(b.done(barEnd + 0.03 * s));
|
|
262
|
+
}
|
|
263
|
+
function bigOperator(chr, ctx, display, growTo, color) {
|
|
264
|
+
const s = em(ctx);
|
|
265
|
+
const axis = ctx.axis * s;
|
|
266
|
+
// m:grow: the operator grows to cover its argument, centred on the axis.
|
|
267
|
+
const grown = (h) => (growTo ? Math.max(h, 2 * Math.max(growTo.ascent - axis, growTo.descent + axis) + 0.1 * s) : h);
|
|
268
|
+
const box = (width, height, items) => ({ width, ascent: axis + height / 2, descent: Math.max(0, height / 2 - axis), items, italic: 0 });
|
|
269
|
+
const integral = integralOf(chr);
|
|
270
|
+
if (integral) {
|
|
271
|
+
const natural = (display ? 2.2 : 1.25) * s;
|
|
272
|
+
const H = grown(natural);
|
|
273
|
+
const W = (display ? 0.8 : 0.52) * s * Math.pow(H / natural, 0.3);
|
|
274
|
+
const step = 0.62 * W;
|
|
275
|
+
const bearing = 0.04 * s;
|
|
276
|
+
const bottom = axis - H / 2;
|
|
277
|
+
const commands = [];
|
|
278
|
+
for (let k = 0; k < integral.count; k++) {
|
|
279
|
+
const shape = integralShape(bearing + k * step, bottom, W, H, s);
|
|
280
|
+
commands.push(...polygons([shape.outline]));
|
|
281
|
+
for (const [cx, cy, r] of shape.balls)
|
|
282
|
+
commands.push(...ellipse(cx, cy, r, r));
|
|
283
|
+
}
|
|
284
|
+
const items = [filled(commands, color)];
|
|
285
|
+
if (integral.contour) {
|
|
286
|
+
const span = (integral.count - 1) * step;
|
|
287
|
+
const cx = bearing + (span + W) / 2;
|
|
288
|
+
items.push(stroked(ellipse(cx, axis, span / 2 + (display ? 0.2 : 0.14) * s, (display ? 0.2 : 0.14) * s), color, 0.05 * s));
|
|
289
|
+
}
|
|
290
|
+
return { box: box(2 * bearing + W + (integral.count - 1) * step, H, items), subKern: W * (display ? 0.35 : 0.3) };
|
|
291
|
+
}
|
|
292
|
+
const target = grown((display ? 1.4 : 1.0) * s);
|
|
293
|
+
const cp = chr.codePointAt(0);
|
|
294
|
+
const bearing = 0.06 * s;
|
|
295
|
+
if (DRAWN_OPERATORS.has(chr) || !ctx.fonts.canShow(cp)) {
|
|
296
|
+
const ratio = '⋃⋂'.includes(chr) ? 0.72 : '⋁⋀'.includes(chr) ? 0.8 : '⨀⨁⨂'.includes(chr) ? 1 : 0.85;
|
|
297
|
+
const W = target * ratio;
|
|
298
|
+
const drawn = bigOperatorPaths(chr, bearing, axis - target / 2, W, target, s, color);
|
|
299
|
+
if (drawn)
|
|
300
|
+
return { box: box(W + 2 * bearing, target, drawn), subKern: 0 };
|
|
301
|
+
}
|
|
302
|
+
// A glyph from the math fonts, scaled so its ink has the target height, centred on the axis.
|
|
303
|
+
const font = ctx.fonts.pick(cp, false, false);
|
|
304
|
+
const ink = inkOf(font, cp) ?? { xMin: 0, yMin: 0, xMax: font.advance(cp), yMax: 0.7 };
|
|
305
|
+
const inkHeight = Math.max(ink.yMax - ink.yMin, 0.1);
|
|
306
|
+
const size = target / inkHeight;
|
|
307
|
+
const x = bearing - ink.xMin * size;
|
|
308
|
+
const baseline = axis - ((ink.yMin + ink.yMax) / 2) * size;
|
|
309
|
+
const m = measureChars(chr, font, size, x);
|
|
310
|
+
const item = textItem(x, -baseline, m.chars, m.xs, m.widths, font, size, color);
|
|
311
|
+
return { box: box((ink.xMax - ink.xMin) * size + 2 * bearing, target, [item]), subKern: 0 };
|
|
312
|
+
}
|
|
313
|
+
function nary(el, ctx) {
|
|
314
|
+
const pr = firstChild(el, 'm:naryPr');
|
|
315
|
+
const chr = charProp(pr, 'm:chr', '∫') || '∫';
|
|
316
|
+
const integral = integralOf(chr);
|
|
317
|
+
const limLoc = val(pr, 'm:limLoc') ?? (integral ? 'subSup' : 'undOvr');
|
|
318
|
+
const display = ctx.display && ctx.level === 0;
|
|
319
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
320
|
+
const body = row(firstChild(el, 'm:e'), ctx);
|
|
321
|
+
const subEl = firstChild(el, 'm:sub');
|
|
322
|
+
const supEl = firstChild(el, 'm:sup');
|
|
323
|
+
const sub = !onOff(pr, 'm:subHide', false) && hasContent(subEl) ? row(subEl, scriptStyle(ctx, true)).box : undefined;
|
|
324
|
+
const sup = !onOff(pr, 'm:supHide', false) && hasContent(supEl) ? row(supEl, scriptStyle(ctx)).box : undefined;
|
|
325
|
+
const op = bigOperator(chr, ctx, display, onOff(pr, 'm:grow', false) ? body.box : undefined, color);
|
|
326
|
+
const s = em(ctx);
|
|
327
|
+
// Limits go under and over in display style; inline equations put them at the side, as Word does.
|
|
328
|
+
const opBox = limLoc === 'undOvr' && display ? stackLimits(op.box, sub, sup, 0.12 * s, 0.1 * s) : attachScripts(op.box, sub, sup, ctx, { subKern: op.subKern });
|
|
329
|
+
const space = body.first ? spaceBetween('op', body.first, ctx) : 0;
|
|
330
|
+
const b = new Builder();
|
|
331
|
+
const opWidth = opBox.width;
|
|
332
|
+
b.put(opBox, 0, 0);
|
|
333
|
+
b.put(body.box, opWidth + space, 0);
|
|
334
|
+
return { box: b.done(), left: 'op', right: body.last ?? 'op' };
|
|
335
|
+
}
|
|
336
|
+
// ---------------------------------------------------------------------------------------------
|
|
337
|
+
// Delimiters.
|
|
338
|
+
/**
|
|
339
|
+
* A delimiter covering content of the given ascent and descent: the font's glyph when it is
|
|
340
|
+
* tall enough, otherwise a shape drawn at the needed height (centred on the axis unless `match`).
|
|
341
|
+
*/
|
|
342
|
+
function delimiterBox(ch, ascent, descent, ctx, grow, match, color) {
|
|
343
|
+
if (!ch)
|
|
344
|
+
return emptyBox();
|
|
345
|
+
const s = em(ctx);
|
|
346
|
+
const axis = ctx.axis * s;
|
|
347
|
+
const cp = ch.codePointAt(0);
|
|
348
|
+
const shape = delimiterOf(ch);
|
|
349
|
+
const font = ctx.fonts.pick(cp, false, false);
|
|
350
|
+
const half = Math.max(ascent - axis, descent + axis);
|
|
351
|
+
let top = match ? ascent : axis + half;
|
|
352
|
+
let bottom = match ? descent : half - axis;
|
|
353
|
+
if (font.has(cp)) {
|
|
354
|
+
const ink = inkOf(font, cp);
|
|
355
|
+
const tolerance = 0.15 * s;
|
|
356
|
+
const fits = !ink || (top <= ink.yMax * s + tolerance && bottom <= -ink.yMin * s + tolerance);
|
|
357
|
+
if (!shape || !grow || fits)
|
|
358
|
+
return symbolBox(ch, s, color, ctx.fonts);
|
|
359
|
+
}
|
|
360
|
+
else if (!shape)
|
|
361
|
+
return symbolBox(ch, s, color, ctx.fonts);
|
|
362
|
+
if (!shape)
|
|
363
|
+
return emptyBox();
|
|
364
|
+
const pad = 0.06 * s;
|
|
365
|
+
top = Math.max(top + pad, axis + 0.5 * s);
|
|
366
|
+
bottom = Math.max(bottom + pad, 0.5 * s - axis);
|
|
367
|
+
if (!grow) {
|
|
368
|
+
top = axis + 0.5 * s;
|
|
369
|
+
bottom = 0.5 * s - axis;
|
|
370
|
+
}
|
|
371
|
+
const L = top + bottom;
|
|
372
|
+
const W = delimiterWidth(shape.kind, L, s);
|
|
373
|
+
const bearing = (shape.kind === 'bar' || shape.kind === 'dbar' ? 0.08 : 0.05) * s;
|
|
374
|
+
const frame = verticalFrame(bearing, W, -bottom, shape.side === 'right' ? 'right' : 'left');
|
|
375
|
+
const item = filled(polygons(mapShape(delimiterShape(shape.kind, L, W, s), frame)), color);
|
|
376
|
+
return { width: W + 2 * bearing, ascent: top, descent: Math.max(0, bottom), items: [item], italic: 0 };
|
|
377
|
+
}
|
|
378
|
+
function delimited(el, ctx) {
|
|
379
|
+
const pr = firstChild(el, 'm:dPr');
|
|
380
|
+
const beg = charProp(pr, 'm:begChr', '(');
|
|
381
|
+
const end = charProp(pr, 'm:endChr', ')');
|
|
382
|
+
const sep = charProp(pr, 'm:sepChr', '|');
|
|
383
|
+
const grow = onOff(pr, 'm:grow', true);
|
|
384
|
+
const match = val(pr, 'm:shp') === 'match';
|
|
385
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
386
|
+
const s = em(ctx);
|
|
387
|
+
const parts = elements(el, 'm:e').map((e) => row(e, ctx));
|
|
388
|
+
const ascent = Math.max(0, ...parts.map((p) => p.box.ascent));
|
|
389
|
+
const descent = Math.max(0, ...parts.map((p) => p.box.descent));
|
|
390
|
+
const b = new Builder();
|
|
391
|
+
let x = 0;
|
|
392
|
+
const open = delimiterBox(beg, ascent, descent, ctx, grow, match, color);
|
|
393
|
+
b.put(open, 0, 0);
|
|
394
|
+
x += open.width;
|
|
395
|
+
parts.forEach((p, i) => {
|
|
396
|
+
if (i > 0 && sep) {
|
|
397
|
+
const bar = delimiterBox(sep, ascent, descent, ctx, grow, match, color);
|
|
398
|
+
x += 0.05 * s;
|
|
399
|
+
b.put(bar, x, 0);
|
|
400
|
+
x += bar.width + 0.05 * s;
|
|
401
|
+
}
|
|
402
|
+
const w = p.box.width;
|
|
403
|
+
b.put(p.box, x, 0);
|
|
404
|
+
x += w;
|
|
405
|
+
});
|
|
406
|
+
const close = delimiterBox(end, ascent, descent, ctx, grow, match, color);
|
|
407
|
+
b.put(close, x, 0);
|
|
408
|
+
x += close.width;
|
|
409
|
+
return { box: b.done(x), left: beg ? 'open' : (parts[0]?.first ?? 'ord'), right: end ? 'close' : (parts[parts.length - 1]?.last ?? 'ord') };
|
|
410
|
+
}
|
|
411
|
+
// ---------------------------------------------------------------------------------------------
|
|
412
|
+
// Matrices and equation arrays.
|
|
413
|
+
/** Gap between rows (pt) from m:rSpRule / m:rSp: single, 1.5, double, exactly (twips), multiple (half lines). */
|
|
414
|
+
function rowGap(rule, value, s) {
|
|
415
|
+
switch (rule) {
|
|
416
|
+
case 1:
|
|
417
|
+
return 0.2 * s + 0.6 * s;
|
|
418
|
+
case 2:
|
|
419
|
+
return 0.2 * s + 1.2 * s;
|
|
420
|
+
case 3:
|
|
421
|
+
return value / 20;
|
|
422
|
+
case 4:
|
|
423
|
+
return Math.max(0, 0.2 * s + (value / 2 - 1) * 1.2 * s);
|
|
424
|
+
default:
|
|
425
|
+
return 0.2 * s;
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
/** Gap between matrix columns (pt) from m:cGapRule / m:cGap. */
|
|
429
|
+
function columnGap(rule, value, s) {
|
|
430
|
+
switch (rule) {
|
|
431
|
+
case 1:
|
|
432
|
+
return 1.5 * s;
|
|
433
|
+
case 2:
|
|
434
|
+
return 2 * s;
|
|
435
|
+
case 3:
|
|
436
|
+
return value / 20;
|
|
437
|
+
case 4:
|
|
438
|
+
return (value / 2) * s;
|
|
439
|
+
default:
|
|
440
|
+
return s;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Stack rows of cells. Rows get at least a strut's height so their spacing is even; the block is
|
|
445
|
+
* centred on the axis (baseJc center), or its top or bottom row sits on the baseline.
|
|
446
|
+
*/
|
|
447
|
+
function stackRows(rows, ctx, gap, baseJc, width) {
|
|
448
|
+
const s = em(ctx);
|
|
449
|
+
const metrics = rows.map((cells) => ({
|
|
450
|
+
ascent: Math.max(0.7 * s, ...cells.map((c) => c.box.ascent)),
|
|
451
|
+
descent: Math.max(0.25 * s, ...cells.map((c) => c.box.descent)),
|
|
452
|
+
}));
|
|
453
|
+
const baselines = [];
|
|
454
|
+
let y = 0;
|
|
455
|
+
metrics.forEach((m, i) => {
|
|
456
|
+
if (i > 0)
|
|
457
|
+
y += gap;
|
|
458
|
+
y += m.ascent;
|
|
459
|
+
baselines.push(y);
|
|
460
|
+
y += m.descent;
|
|
461
|
+
});
|
|
462
|
+
const height = y;
|
|
463
|
+
const shift = baseJc === 'top' ? -(baselines[0] ?? 0) : baseJc === 'bot' ? -(baselines[baselines.length - 1] ?? 0) : -(ctx.axis * s + height / 2);
|
|
464
|
+
const b = new Builder();
|
|
465
|
+
rows.forEach((cells, i) => {
|
|
466
|
+
for (const c of cells)
|
|
467
|
+
b.put(c.box, c.x, baselines[i] + shift);
|
|
468
|
+
});
|
|
469
|
+
if (rows.length) {
|
|
470
|
+
b.ascent = Math.max(b.ascent, -shift);
|
|
471
|
+
b.descent = Math.max(b.descent, height + shift);
|
|
472
|
+
}
|
|
473
|
+
return b.done(width);
|
|
474
|
+
}
|
|
475
|
+
function matrix(el, ctx) {
|
|
476
|
+
const pr = firstChild(el, 'm:mPr');
|
|
477
|
+
const s = em(ctx);
|
|
478
|
+
const cellCtx = { ...ctx, display: false, align: false };
|
|
479
|
+
const cells = elements(el, 'm:mr').map((mr) => elements(mr, 'm:e').map((e) => row(e, cellCtx).box));
|
|
480
|
+
const jcs = [];
|
|
481
|
+
const mcs = firstChild(pr, 'm:mcs');
|
|
482
|
+
for (const mc of mcs ? elements(mcs, 'm:mc') : []) {
|
|
483
|
+
const mcPr = firstChild(mc, 'm:mcPr');
|
|
484
|
+
const count = Math.max(1, Math.min(64, numProp(mcPr, 'm:count') ?? 1));
|
|
485
|
+
const jc = val(mcPr, 'm:mcJc') ?? 'center';
|
|
486
|
+
for (let k = 0; k < count; k++)
|
|
487
|
+
jcs.push(jc);
|
|
488
|
+
}
|
|
489
|
+
const columns = Math.max(0, ...cells.map((r) => r.length));
|
|
490
|
+
const minWidth = (numProp(pr, 'm:cSp') ?? 0) / 20;
|
|
491
|
+
const widths = [];
|
|
492
|
+
for (let j = 0; j < columns; j++)
|
|
493
|
+
widths.push(Math.max(minWidth, ...cells.map((r) => r[j]?.width ?? 0)));
|
|
494
|
+
const gap = columnGap(numProp(pr, 'm:cGapRule') ?? 0, numProp(pr, 'm:cGap') ?? 0, s);
|
|
495
|
+
const side = 0.08 * s;
|
|
496
|
+
const xs = [];
|
|
497
|
+
let x = side;
|
|
498
|
+
for (let j = 0; j < columns; j++) {
|
|
499
|
+
xs.push(x);
|
|
500
|
+
x += widths[j] + (j < columns - 1 ? gap : 0);
|
|
501
|
+
}
|
|
502
|
+
const placed = cells.map((r) => r.map((box, j) => {
|
|
503
|
+
const jc = jcs[j] ?? 'center';
|
|
504
|
+
const free = widths[j] - box.width;
|
|
505
|
+
return { box, x: xs[j] + (jc === 'left' ? 0 : jc === 'right' ? free : free / 2) };
|
|
506
|
+
}));
|
|
507
|
+
const box = stackRows(placed, ctx, rowGap(numProp(pr, 'm:rSpRule') ?? 0, numProp(pr, 'm:rSp') ?? 0, s), val(pr, 'm:baseJc') ?? 'center', x + side);
|
|
508
|
+
return ord(box);
|
|
509
|
+
}
|
|
510
|
+
/**
|
|
511
|
+
* An equation array: rows aligned at `&` marks. Segments alternate right- and left-aligned
|
|
512
|
+
* columns (like LaTeX's align), so "x&=1" lines up the equals signs.
|
|
513
|
+
*/
|
|
514
|
+
function eqArray(el, ctx) {
|
|
515
|
+
const pr = firstChild(el, 'm:eqArrPr');
|
|
516
|
+
const s = em(ctx);
|
|
517
|
+
const rows = elements(el, 'm:e').map((e) => {
|
|
518
|
+
const atoms = rowAtoms(e, { ...ctx, align: true });
|
|
519
|
+
resolveBinaries(atoms);
|
|
520
|
+
const segments = [[]];
|
|
521
|
+
for (const a of atoms) {
|
|
522
|
+
if (a.kind === 'mark')
|
|
523
|
+
segments.push([]);
|
|
524
|
+
else
|
|
525
|
+
segments[segments.length - 1].push(a);
|
|
526
|
+
}
|
|
527
|
+
// Each segment is packed knowing what precedes it, so the space before "=" survives the split.
|
|
528
|
+
let before;
|
|
529
|
+
return segments.map((seg) => {
|
|
530
|
+
const r = hpack(seg, ctx, before);
|
|
531
|
+
before = r.last ?? before;
|
|
532
|
+
return r.box;
|
|
533
|
+
});
|
|
534
|
+
});
|
|
535
|
+
const columns = Math.max(1, ...rows.map((r) => r.length));
|
|
536
|
+
const aligned = columns > 1;
|
|
537
|
+
const widths = [];
|
|
538
|
+
for (let j = 0; j < columns; j++)
|
|
539
|
+
widths.push(Math.max(0, ...rows.map((r) => r[j]?.width ?? 0)));
|
|
540
|
+
const xs = [];
|
|
541
|
+
let x = 0;
|
|
542
|
+
for (const w of widths) {
|
|
543
|
+
xs.push(x);
|
|
544
|
+
x += w;
|
|
545
|
+
}
|
|
546
|
+
const placed = rows.map((r) => r.map((box, j) => {
|
|
547
|
+
const free = widths[j] - box.width;
|
|
548
|
+
return { box, x: xs[j] + (aligned ? (j % 2 === 0 ? free : 0) : free / 2) };
|
|
549
|
+
}));
|
|
550
|
+
const box = stackRows(placed, ctx, rowGap(numProp(pr, 'm:rSpRule') ?? 0, numProp(pr, 'm:rSp') ?? 0, s), val(pr, 'm:baseJc') ?? 'center', x);
|
|
551
|
+
return ord(box);
|
|
552
|
+
}
|
|
553
|
+
// ---------------------------------------------------------------------------------------------
|
|
554
|
+
// Accents, bars, group characters, limits.
|
|
555
|
+
function isItalicGlyph(box) {
|
|
556
|
+
const first = box.items[0];
|
|
557
|
+
return box.simple === true && first?.type === 'text' && first.font.italic;
|
|
558
|
+
}
|
|
559
|
+
function accent(el, ctx) {
|
|
560
|
+
const pr = firstChild(el, 'm:accPr');
|
|
561
|
+
const chr = charProp(pr, 'm:chr', '̂') || '̂';
|
|
562
|
+
const s = em(ctx);
|
|
563
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
564
|
+
const base = row(firstChild(el, 'm:e'), { ...ctx, cramped: true });
|
|
565
|
+
const bb = base.box;
|
|
566
|
+
const baseWidth = bb.width;
|
|
567
|
+
// Over an italic letter the accent follows the slant to the letter's top.
|
|
568
|
+
const skew = isItalicGlyph(bb) ? 0.21 * bb.ascent * 0.5 : 0;
|
|
569
|
+
const lift = Math.max(bb.ascent, X_HEIGHT * s) + 0.05 * s;
|
|
570
|
+
const kind = accentOf(chr);
|
|
571
|
+
let items;
|
|
572
|
+
let x0;
|
|
573
|
+
let w;
|
|
574
|
+
let height;
|
|
575
|
+
if (kind) {
|
|
576
|
+
const metrics = accentMetrics(kind);
|
|
577
|
+
const natural = metrics.width * s;
|
|
578
|
+
w = natural;
|
|
579
|
+
if (kind === 'bar' || kind === 'arrow')
|
|
580
|
+
w = Math.max(natural, baseWidth);
|
|
581
|
+
else if (metrics.stretch && baseWidth > 1.3 * natural)
|
|
582
|
+
w = baseWidth * 0.92;
|
|
583
|
+
x0 = baseWidth / 2 + skew - w / 2;
|
|
584
|
+
const drawn = accentPaths(kind, x0, w, lift, s, color, arrowOf(chr));
|
|
585
|
+
items = drawn.items;
|
|
586
|
+
height = drawn.height;
|
|
587
|
+
}
|
|
588
|
+
else {
|
|
589
|
+
// An accent we do not draw: its glyph, centred above the base.
|
|
590
|
+
const glyph = symbolBox(chr, s, color, ctx.fonts);
|
|
591
|
+
w = glyph.width;
|
|
592
|
+
x0 = baseWidth / 2 + skew - w / 2;
|
|
593
|
+
height = glyph.ascent + glyph.descent;
|
|
594
|
+
shiftItems(glyph.items, x0, -(lift + glyph.descent));
|
|
595
|
+
items = glyph.items;
|
|
596
|
+
}
|
|
597
|
+
const dx = Math.max(0, -x0);
|
|
598
|
+
const b = new Builder();
|
|
599
|
+
b.put(bb, dx, 0);
|
|
600
|
+
shiftItems(items, dx, 0);
|
|
601
|
+
for (const it of items)
|
|
602
|
+
b.draw(it, -(lift + height), -lift);
|
|
603
|
+
return { box: b.done(Math.max(dx + baseWidth, dx + x0 + w)), left: base.first ?? 'ord', right: base.last ?? 'ord' };
|
|
604
|
+
}
|
|
605
|
+
function overUnderBar(el, ctx) {
|
|
606
|
+
const pr = firstChild(el, 'm:barPr');
|
|
607
|
+
const top = (val(pr, 'm:pos') ?? 'bot') === 'top';
|
|
608
|
+
const s = em(ctx);
|
|
609
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
610
|
+
const base = row(firstChild(el, 'm:e'), top ? { ...ctx, cramped: true } : ctx);
|
|
611
|
+
const bb = base.box;
|
|
612
|
+
const th = RULE * s;
|
|
613
|
+
const gap = 0.1 * s;
|
|
614
|
+
const b = new Builder();
|
|
615
|
+
const width = bb.width;
|
|
616
|
+
if (top) {
|
|
617
|
+
const y = -(Math.max(bb.ascent, X_HEIGHT * s) + gap + th);
|
|
618
|
+
b.draw(rect(0, y, width, th, color), y, y + th);
|
|
619
|
+
}
|
|
620
|
+
else {
|
|
621
|
+
const y = bb.descent + gap;
|
|
622
|
+
b.draw(rect(0, y, width, th, color), y, y + th);
|
|
623
|
+
}
|
|
624
|
+
b.put(bb, 0, 0);
|
|
625
|
+
return { box: b.done(width), left: base.first ?? 'ord', right: base.last ?? 'ord' };
|
|
626
|
+
}
|
|
627
|
+
function func(el, ctx) {
|
|
628
|
+
const name = row(firstChild(el, 'm:fName'), { ...ctx, fn: true });
|
|
629
|
+
const arg = row(firstChild(el, 'm:e'), ctx);
|
|
630
|
+
// Function application: a thin space before an ordinary argument, none before a parenthesis.
|
|
631
|
+
const space = arg.first ? spaceBetween('op', arg.first, ctx) : 0;
|
|
632
|
+
const b = new Builder();
|
|
633
|
+
const nameWidth = name.box.width;
|
|
634
|
+
b.put(name.box, 0, 0);
|
|
635
|
+
b.put(arg.box, nameWidth + space, 0);
|
|
636
|
+
return { box: b.done(), left: 'op', right: arg.last ?? 'op' };
|
|
637
|
+
}
|
|
638
|
+
function limit(el, ctx) {
|
|
639
|
+
const upper = el.name === 'm:limUpp';
|
|
640
|
+
const s = em(ctx);
|
|
641
|
+
const base = row(firstChild(el, 'm:e'), ctx);
|
|
642
|
+
const lim = row(firstChild(el, 'm:lim'), scriptStyle(ctx, !upper)).box;
|
|
643
|
+
const box = upper ? stackLimits(base.box, undefined, lim, 0.1 * s, 0) : stackLimits(base.box, lim, undefined, 0, 0.1 * s);
|
|
644
|
+
return { box, left: base.first ?? 'ord', right: base.last ?? 'ord' };
|
|
645
|
+
}
|
|
646
|
+
function groupChr(el, ctx) {
|
|
647
|
+
const pr = firstChild(el, 'm:groupChrPr');
|
|
648
|
+
const chr = charProp(pr, 'm:chr', '⏟') || '⏟';
|
|
649
|
+
const pos = val(pr, 'm:pos') ?? 'bot';
|
|
650
|
+
// vertJc names the part aligned with the text: by default the base stays on the baseline.
|
|
651
|
+
const vertJc = val(pr, 'm:vertJc') ?? (pos === 'bot' ? 'top' : 'bot');
|
|
652
|
+
const s = em(ctx);
|
|
653
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
654
|
+
const base = row(firstChild(el, 'm:e'), ctx);
|
|
655
|
+
const bb = base.box;
|
|
656
|
+
const L = Math.max(bb.width, 0.5 * s);
|
|
657
|
+
const shape = groupShapeOf(chr);
|
|
658
|
+
const arrow = arrowOf(chr);
|
|
659
|
+
let sign;
|
|
660
|
+
if (shape && shape.kind !== 'arrow') {
|
|
661
|
+
const depth = Math.min(0.2 * s + 0.03 * L, 0.42 * s);
|
|
662
|
+
const frame = horizontalFrame(0, depth, 0, shape.outer);
|
|
663
|
+
sign = { width: L, ascent: depth, descent: 0, items: [filled(polygons(mapShape(delimiterShape(shape.kind, L, depth, s), frame)), color)], italic: 0 };
|
|
664
|
+
}
|
|
665
|
+
else if (arrow) {
|
|
666
|
+
const h = 0.3 * s;
|
|
667
|
+
sign = { width: L, ascent: h, descent: 0, items: arrowPaths(0, L, h / 2, s, arrow, color, false), italic: 0 };
|
|
668
|
+
}
|
|
669
|
+
else if (accentOf(chr) === 'bar' || chr === '_') {
|
|
670
|
+
const th = RULE * s;
|
|
671
|
+
sign = { width: L, ascent: th, descent: 0, items: [rect(0, -th, L, th, color)], italic: 0 };
|
|
672
|
+
}
|
|
673
|
+
else
|
|
674
|
+
sign = symbolBox(chr, s, color, ctx.fonts);
|
|
675
|
+
const gap = 0.06 * s;
|
|
676
|
+
const w = Math.max(bb.width, sign.width);
|
|
677
|
+
const signHeight = sign.ascent + sign.descent;
|
|
678
|
+
const signBaseline = pos === 'bot' ? bb.descent + gap + sign.ascent : -(bb.ascent + gap + sign.descent);
|
|
679
|
+
const b = new Builder();
|
|
680
|
+
b.put(bb, (w - bb.width) / 2, 0);
|
|
681
|
+
b.put(sign, (w - sign.width) / 2, signBaseline);
|
|
682
|
+
const box = b.done(w);
|
|
683
|
+
const signOnBaseline = (pos === 'bot' && vertJc === 'bot') || (pos === 'top' && vertJc === 'top');
|
|
684
|
+
if (signOnBaseline) {
|
|
685
|
+
// The character sits on the text line (centred on the axis) and the base moves above or below it.
|
|
686
|
+
const signCentre = signBaseline - sign.ascent + signHeight / 2;
|
|
687
|
+
const dy = -ctx.axis * s - signCentre;
|
|
688
|
+
shiftItems(box.items, 0, dy);
|
|
689
|
+
const ascent = box.ascent - dy;
|
|
690
|
+
const descent = box.descent + dy;
|
|
691
|
+
box.ascent = Math.max(0, ascent);
|
|
692
|
+
box.descent = Math.max(0, descent);
|
|
693
|
+
const cls = arrow ? 'rel' : 'ord';
|
|
694
|
+
return { box, left: cls, right: cls };
|
|
695
|
+
}
|
|
696
|
+
return ord(box);
|
|
697
|
+
}
|
|
698
|
+
// ---------------------------------------------------------------------------------------------
|
|
699
|
+
// Boxes, borders, phantoms.
|
|
700
|
+
function boxed(el, ctx) {
|
|
701
|
+
const r = row(firstChild(el, 'm:e'), ctx);
|
|
702
|
+
// An operator emulator spaces like the operator it holds ("==" as one relation).
|
|
703
|
+
if (onOff(firstChild(el, 'm:boxPr'), 'm:opEmu', false) && r.first)
|
|
704
|
+
return { box: r.box, left: r.first, right: r.last ?? r.first };
|
|
705
|
+
return ord(r.box);
|
|
706
|
+
}
|
|
707
|
+
function borderBox(el, ctx) {
|
|
708
|
+
const pr = firstChild(el, 'm:borderBoxPr');
|
|
709
|
+
const s = em(ctx);
|
|
710
|
+
const color = ctrlColor(pr) ?? ctx.color;
|
|
711
|
+
const inner = row(firstChild(el, 'm:e'), ctx).box;
|
|
712
|
+
const pad = 0.1 * s;
|
|
713
|
+
const t = 0.05 * s;
|
|
714
|
+
const w = inner.width + 2 * pad;
|
|
715
|
+
const top = -(Math.max(inner.ascent, X_HEIGHT * s) + pad);
|
|
716
|
+
const bottom = inner.descent + pad;
|
|
717
|
+
const b = new Builder();
|
|
718
|
+
b.put(inner, pad, 0);
|
|
719
|
+
const line = (x1, y1, x2, y2) => ({ type: 'line', x1, y1, x2, y2, width: t, color, style: 'solid' });
|
|
720
|
+
const add = (on, l) => {
|
|
721
|
+
if (on)
|
|
722
|
+
b.draw(l, Math.min(l.y1, l.y2) - t / 2, Math.max(l.y1, l.y2) + t / 2);
|
|
723
|
+
};
|
|
724
|
+
add(!onOff(pr, 'm:hideTop', false), line(0, top, w, top));
|
|
725
|
+
add(!onOff(pr, 'm:hideBot', false), line(0, bottom, w, bottom));
|
|
726
|
+
add(!onOff(pr, 'm:hideLeft', false), line(0, top, 0, bottom));
|
|
727
|
+
add(!onOff(pr, 'm:hideRight', false), line(w, top, w, bottom));
|
|
728
|
+
add(onOff(pr, 'm:strikeH', false), line(0, -ctx.axis * s, w, -ctx.axis * s));
|
|
729
|
+
add(onOff(pr, 'm:strikeV', false), line(w / 2, top, w / 2, bottom));
|
|
730
|
+
add(onOff(pr, 'm:strikeBLTR', false), line(0, bottom, w, top));
|
|
731
|
+
add(onOff(pr, 'm:strikeTLBR', false), line(0, top, w, bottom));
|
|
732
|
+
return ord(b.done(w));
|
|
733
|
+
}
|
|
734
|
+
function phantom(el, ctx) {
|
|
735
|
+
const pr = firstChild(el, 'm:phantPr');
|
|
736
|
+
const r = row(firstChild(el, 'm:e'), ctx);
|
|
737
|
+
const box = {
|
|
738
|
+
width: onOff(pr, 'm:zeroWid', false) ? 0 : r.box.width,
|
|
739
|
+
ascent: onOff(pr, 'm:zeroAsc', false) ? 0 : r.box.ascent,
|
|
740
|
+
descent: onOff(pr, 'm:zeroDesc', false) ? 0 : r.box.descent,
|
|
741
|
+
items: onOff(pr, 'm:show', true) ? r.box.items : [],
|
|
742
|
+
italic: 0,
|
|
743
|
+
};
|
|
744
|
+
return { box, left: r.first ?? 'ord', right: r.last ?? 'ord' };
|
|
745
|
+
}
|
|
746
|
+
// ---------------------------------------------------------------------------------------------
|
|
747
|
+
/** Join neighbouring text items of one font, size and colour on one baseline (fewer items to draw). */
|
|
748
|
+
export function mergeText(items) {
|
|
749
|
+
const out = [];
|
|
750
|
+
for (const it of items) {
|
|
751
|
+
const prev = out[out.length - 1];
|
|
752
|
+
if (prev?.type === 'text' &&
|
|
753
|
+
it.type === 'text' &&
|
|
754
|
+
prev.font === it.font &&
|
|
755
|
+
prev.size === it.size &&
|
|
756
|
+
prev.color === it.color &&
|
|
757
|
+
prev.scale === it.scale &&
|
|
758
|
+
Math.abs(prev.y - it.y) < 1e-6 &&
|
|
759
|
+
it.xs[0] !== undefined &&
|
|
760
|
+
it.xs[0] >= (prev.xs[prev.xs.length - 1] ?? -Infinity)) {
|
|
761
|
+
prev.text += it.text;
|
|
762
|
+
prev.chars.push(...it.chars);
|
|
763
|
+
prev.xs.push(...it.xs);
|
|
764
|
+
prev.widths.push(...it.widths);
|
|
765
|
+
prev.glyphs.push(...it.glyphs);
|
|
766
|
+
continue;
|
|
767
|
+
}
|
|
768
|
+
out.push(it);
|
|
769
|
+
}
|
|
770
|
+
return out;
|
|
771
|
+
}
|