js.documents 14.4.56 → 14.4.57

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.
Files changed (163) hide show
  1. package/dist/codecs/read.cjs +2 -2
  2. package/dist/codecs/read.js +1 -1
  3. package/dist/composition-plan-CfvhXeOC.cjs +627 -0
  4. package/dist/composition-plan-DU9nPPN7.d.cts +102 -0
  5. package/dist/composition-plan-f0zaExUW.js +508 -0
  6. package/dist/composition-plan-l6QPO4FX.d.ts +102 -0
  7. package/dist/convert/composition-plan.cjs +7 -0
  8. package/dist/convert/composition-plan.d.cts +2 -0
  9. package/dist/convert/composition-plan.d.ts +2 -0
  10. package/dist/convert/composition-plan.js +2 -0
  11. package/dist/convert/composition-to-pdf.cjs +12 -12
  12. package/dist/convert/composition-to-pdf.d.cts +1 -1
  13. package/dist/convert/composition-to-pdf.d.ts +1 -1
  14. package/dist/convert/composition-to-pdf.js +2 -2
  15. package/dist/convert/composition.cjs +16 -511
  16. package/dist/convert/composition.d.cts +2 -85
  17. package/dist/convert/composition.d.ts +2 -85
  18. package/dist/convert/composition.js +2 -502
  19. package/dist/convert/convert.cjs +2 -2
  20. package/dist/convert/convert.js +1 -1
  21. package/dist/convert/from-package.cjs +3 -3
  22. package/dist/convert/from-package.js +2 -2
  23. package/dist/convert/from-pdf.cjs +18 -18
  24. package/dist/convert/from-pdf.js +2 -2
  25. package/dist/convert/local.cjs +3 -3
  26. package/dist/convert/local.js +1 -1
  27. package/dist/edit/odp/slide.cjs +4 -4
  28. package/dist/edit/odp/slide.d.cts +1 -1
  29. package/dist/edit/odp/slide.d.ts +1 -1
  30. package/dist/edit/odp/slide.js +1 -1
  31. package/dist/edit/odt/content.d.cts +1 -1
  32. package/dist/edit/odt/content.d.ts +1 -1
  33. package/dist/edit/odt/editor.cjs +4 -4
  34. package/dist/edit/odt/editor.d.cts +1 -1
  35. package/dist/edit/odt/editor.d.ts +1 -1
  36. package/dist/edit/odt/editor.js +1 -1
  37. package/dist/edit/odt/table-grid.cjs +23 -0
  38. package/dist/edit/odt/table-grid.d.cts +54 -0
  39. package/dist/edit/odt/table-grid.d.ts +54 -0
  40. package/dist/edit/odt/table-grid.js +2 -0
  41. package/dist/edit/odt/table.cjs +12 -653
  42. package/dist/edit/odt/table.d.cts +2 -2
  43. package/dist/edit/odt/table.d.ts +2 -2
  44. package/dist/edit/odt/table.js +2 -647
  45. package/dist/{evaluate-BbOsgdmN.d.cts → evaluate-BSEUxiUK.d.cts} +3 -1
  46. package/dist/evaluate-CEzsBx3w.cjs +564 -0
  47. package/dist/{evaluate-C7fmk69X.d.ts → evaluate-D8fG2ZeB.d.ts} +3 -1
  48. package/dist/evaluate-IK-3aMv5.js +445 -0
  49. package/dist/evaluate-columns-CyqKHkIW.d.cts +41 -0
  50. package/dist/evaluate-columns-cOo8fOfT.d.ts +41 -0
  51. package/dist/index.cjs +24 -24
  52. package/dist/index.d.cts +6 -5
  53. package/dist/index.d.ts +6 -5
  54. package/dist/index.js +8 -8
  55. package/dist/latex/lint.cjs +2 -2
  56. package/dist/latex/lint.js +1 -1
  57. package/dist/latex/lower-fold.cjs +6 -0
  58. package/dist/latex/lower-fold.d.cts +17 -0
  59. package/dist/latex/lower-fold.d.ts +17 -0
  60. package/dist/latex/lower-fold.js +2 -0
  61. package/dist/latex/lower.cjs +11 -617
  62. package/dist/latex/lower.d.cts +18 -1
  63. package/dist/latex/lower.d.ts +18 -1
  64. package/dist/latex/lower.js +2 -616
  65. package/dist/latex/symbols.d.cts +1 -15
  66. package/dist/latex/symbols.d.ts +1 -15
  67. package/dist/latex/temml.d.cts +1 -24
  68. package/dist/latex/temml.d.ts +1 -24
  69. package/dist/layout/engine.cjs +2 -2
  70. package/dist/layout/engine.js +1 -1
  71. package/dist/layout/reconstruct-lines.cjs +315 -0
  72. package/dist/layout/reconstruct-lines.d.cts +2 -0
  73. package/dist/layout/reconstruct-lines.d.ts +2 -0
  74. package/dist/layout/reconstruct-lines.js +297 -0
  75. package/dist/layout/reconstruct-presentations-prod.cjs +9 -0
  76. package/dist/layout/reconstruct-presentations-prod.d.cts +26 -0
  77. package/dist/layout/reconstruct-presentations-prod.d.ts +26 -0
  78. package/dist/layout/reconstruct-presentations-prod.js +2 -0
  79. package/dist/layout/reconstruct-tables.cjs +5 -0
  80. package/dist/layout/reconstruct-tables.d.cts +22 -0
  81. package/dist/layout/reconstruct-tables.d.ts +22 -0
  82. package/dist/layout/reconstruct-tables.js +2 -0
  83. package/dist/layout/reconstruct.cjs +11 -1545
  84. package/dist/layout/reconstruct.d.cts +2 -14
  85. package/dist/layout/reconstruct.d.ts +2 -14
  86. package/dist/layout/reconstruct.js +2 -1542
  87. package/dist/layout/shared.cjs +2 -2
  88. package/dist/layout/shared.js +1 -1
  89. package/dist/layout/sheets-render.cjs +19 -0
  90. package/dist/layout/sheets-render.d.cts +41 -0
  91. package/dist/layout/sheets-render.d.ts +41 -0
  92. package/dist/layout/sheets-render.js +2 -0
  93. package/dist/layout/sheets.cjs +10 -544
  94. package/dist/layout/sheets.d.cts +13 -2
  95. package/dist/layout/sheets.d.ts +13 -2
  96. package/dist/layout/sheets.js +2 -543
  97. package/dist/layout/slides.cjs +2 -2
  98. package/dist/layout/slides.js +1 -1
  99. package/dist/layout-DIJzJk4x.js +526 -0
  100. package/dist/layout-iuiyl_Fk.cjs +621 -0
  101. package/dist/layout-types-oSbHvZid.d.cts +13 -0
  102. package/dist/layout-types-oSbHvZid.d.ts +13 -0
  103. package/dist/lower-DCs3rWI7.js +618 -0
  104. package/dist/lower-DzOb13w4.cjs +701 -0
  105. package/dist/markdown/math.cjs +2 -2
  106. package/dist/markdown/math.js +1 -1
  107. package/dist/mathml/layout-stretch.cjs +16 -0
  108. package/dist/mathml/layout-stretch.d.cts +24 -0
  109. package/dist/mathml/layout-stretch.d.ts +24 -0
  110. package/dist/mathml/layout-stretch.js +2 -0
  111. package/dist/mathml/layout-types.d.cts +1 -12
  112. package/dist/mathml/layout-types.d.ts +1 -12
  113. package/dist/mathml/layout.cjs +3 -525
  114. package/dist/mathml/layout.d.cts +2 -2
  115. package/dist/mathml/layout.d.ts +2 -2
  116. package/dist/mathml/layout.js +1 -523
  117. package/dist/odb/formula/evaluate.d.cts +1 -1
  118. package/dist/odb/formula/evaluate.d.ts +1 -1
  119. package/dist/odb/report/render.d.cts +1 -1
  120. package/dist/odb/report/render.d.ts +1 -1
  121. package/dist/odb/report/source.cjs +2 -2
  122. package/dist/odb/report/source.d.cts +1 -1
  123. package/dist/odb/report/source.d.ts +1 -1
  124. package/dist/odb/report/source.js +1 -1
  125. package/dist/odb/sql/evaluate-columns.cjs +122 -0
  126. package/dist/odb/sql/evaluate-columns.d.cts +2 -0
  127. package/dist/odb/sql/evaluate-columns.d.ts +2 -0
  128. package/dist/odb/sql/evaluate-columns.js +114 -0
  129. package/dist/odb/sql/evaluate-predicates.cjs +16 -0
  130. package/dist/odb/sql/evaluate-predicates.d.cts +23 -0
  131. package/dist/odb/sql/evaluate-predicates.d.ts +23 -0
  132. package/dist/odb/sql/evaluate-predicates.js +2 -0
  133. package/dist/odb/sql/evaluate.cjs +7 -556
  134. package/dist/odb/sql/evaluate.d.cts +2 -2
  135. package/dist/odb/sql/evaluate.d.ts +2 -2
  136. package/dist/odb/sql/evaluate.js +2 -552
  137. package/dist/read-BSH--XB4.js +682 -0
  138. package/dist/read-CahCZMwG.cjs +759 -0
  139. package/dist/reconstruct-BKOCAedo.d.ts +29 -0
  140. package/dist/reconstruct-D4TEteZ6.d.cts +29 -0
  141. package/dist/reconstruct-lines-B7t66ICm.d.cts +33 -0
  142. package/dist/reconstruct-lines-B7t66ICm.d.ts +33 -0
  143. package/dist/reconstruct-presentations-prod-B5fkPiRZ.js +1256 -0
  144. package/dist/reconstruct-presentations-prod-X315aBmI.cjs +1375 -0
  145. package/dist/sheets-BQCpNXsE.cjs +700 -0
  146. package/dist/sheets-DWDvGN-g.js +545 -0
  147. package/dist/svg/read-geometry.cjs +10 -0
  148. package/dist/svg/read-geometry.d.cts +24 -0
  149. package/dist/svg/read-geometry.d.ts +24 -0
  150. package/dist/svg/read-geometry.js +2 -0
  151. package/dist/svg/read.cjs +6 -680
  152. package/dist/svg/read.d.cts +17 -2
  153. package/dist/svg/read.d.ts +17 -2
  154. package/dist/svg/read.js +2 -679
  155. package/dist/symbols-BlzXfLEe.d.cts +16 -0
  156. package/dist/symbols-CKm2r4D0.d.ts +16 -0
  157. package/dist/table-B5gSMY7w.js +649 -0
  158. package/dist/{table-DrE6jEWv.d.cts → table-BK_Ao-Y6.d.cts} +9 -1
  159. package/dist/{table-B5htMG3g.d.ts → table-CxY85YUj.d.ts} +9 -1
  160. package/dist/table-DqUIOSPV.cjs +840 -0
  161. package/dist/temml-1-I9N9DD.d.cts +25 -0
  162. package/dist/temml-1-I9N9DD.d.ts +25 -0
  163. package/package.json +1 -1
@@ -0,0 +1,701 @@
1
+ const require_latex_temml = require("./latex/temml.cjs");
2
+ const require_latex_symbols = require("./latex/symbols.cjs");
3
+ const require_latex_rational = require("./latex/rational.cjs");
4
+ //#region src/latex/lower-fold.ts
5
+ function normaliseUnaryMinus(operators, segments) {
6
+ const outOperators = [];
7
+ const outSegments = [];
8
+ let pendingNegate = false;
9
+ let skipOperator = false;
10
+ for (const [index, segment] of segments.entries()) {
11
+ if (index > 0 && !skipOperator) {
12
+ const operator = operators[index - 1];
13
+ if (operator !== void 0) outOperators.push(operator);
14
+ }
15
+ skipOperator = false;
16
+ if (segment.nodes.length === 0 && operators[index] === "math:subtract") {
17
+ pendingNegate = !pendingNegate;
18
+ skipOperator = true;
19
+ continue;
20
+ }
21
+ outSegments.push({
22
+ nodes: segment.nodes,
23
+ negated: pendingNegate
24
+ });
25
+ pendingNegate = false;
26
+ }
27
+ return {
28
+ operators: outOperators,
29
+ segments: outSegments
30
+ };
31
+ }
32
+ function fold(first, operators, segments, context, detail) {
33
+ let runFirst = first;
34
+ let runOperators = [];
35
+ let runSegments = [];
36
+ const operands = [];
37
+ const relations = [];
38
+ for (let index = 0; index < operators.length; index += 1) {
39
+ const operator = operators[index];
40
+ const segment = segments[index];
41
+ if (operator === void 0 || segment === void 0) throw new Error("operator and segment lists diverged while folding a lowered sequence");
42
+ if (segment.nodes.length === 0) {
43
+ diagnose(context, "latex/operator-placement-unparsed", detail);
44
+ return unparsed(detail);
45
+ }
46
+ if (RELATION_OPERATORS.has(operator)) {
47
+ operands.push(foldArithmetic(runFirst, runOperators, runSegments, context));
48
+ relations.push(operator);
49
+ runFirst = lowerFoldSegment(segment, context);
50
+ runOperators = [];
51
+ runSegments = [];
52
+ continue;
53
+ }
54
+ runOperators.push(operator);
55
+ runSegments.push(segment);
56
+ }
57
+ operands.push(foldArithmetic(runFirst, runOperators, runSegments, context));
58
+ let folded = operands[0];
59
+ if (folded === void 0) throw new Error("fold produced no operand for a non-empty operator list");
60
+ for (let index = 0; index < relations.length; index += 1) {
61
+ const relation = relations[index];
62
+ const operand = operands[index + 1];
63
+ if (relation === void 0 || operand === void 0) throw new Error("relation and operand lists diverged while folding a lowered sequence");
64
+ folded = app(relation, [folded, operand]);
65
+ }
66
+ return folded;
67
+ }
68
+ function foldArithmetic(first, operators, segments, context) {
69
+ let folded = first;
70
+ for (let index = 0; index < operators.length; index += 1) {
71
+ const operator = operators[index];
72
+ const segment = segments[index];
73
+ if (operator === void 0 || segment === void 0) throw new Error("operator and segment lists diverged while folding an arithmetic run");
74
+ folded = app(operator, [folded, lowerFoldSegment(segment, context)]);
75
+ }
76
+ return folded;
77
+ }
78
+ function lowerFoldSegment(segment, context) {
79
+ const folded = lowerTerm(segment.nodes, context);
80
+ return segment.negated ? app(UNARY_MINUS_OPERATOR, [folded]) : folded;
81
+ }
82
+ //#endregion
83
+ //#region src/latex/lower.ts
84
+ const RELATION_SUBSCRIPT_MIN_NODES = 3;
85
+ const BINARY_ATOM_OPERATORS = {
86
+ "+": "math:add",
87
+ "-": "math:subtract",
88
+ "\\cdot": "math:multiply",
89
+ "\\times": "math:multiply",
90
+ "\\div": "math:divide"
91
+ };
92
+ const RELATION_ATOM_OPERATORS = {
93
+ "=": "math:eq",
94
+ "\\neq": "math:neq",
95
+ "\\ne": "math:neq",
96
+ "<": "math:lt",
97
+ "\\leq": "math:leq",
98
+ "\\le": "math:leq",
99
+ ">": "math:gt",
100
+ "\\geq": "math:geq",
101
+ "\\ge": "math:geq"
102
+ };
103
+ const SUBTRACT_OPERATOR = "math:subtract";
104
+ const UNARY_MINUS_OPERATOR = "math:negate";
105
+ const NAMED_FUNCTION_OPERATORS = {
106
+ "\\sin": "math:sin",
107
+ "\\cos": "math:cos",
108
+ "\\tan": "math:tan",
109
+ "\\cot": "math:cot",
110
+ "\\sec": "math:sec",
111
+ "\\csc": "math:csc",
112
+ "\\arcsin": "math:arcsin",
113
+ "\\arccos": "math:arccos",
114
+ "\\arctan": "math:arctan",
115
+ "\\sinh": "math:sinh",
116
+ "\\cosh": "math:cosh",
117
+ "\\tanh": "math:tanh",
118
+ "\\exp": "math:exp",
119
+ "\\log": "math:log",
120
+ "\\ln": "math:ln"
121
+ };
122
+ const PRESENTATION_ONLY_TYPES = /* @__PURE__ */ new Set([
123
+ "spacing",
124
+ "rule",
125
+ "kern"
126
+ ]);
127
+ const RELATION_OPERATORS = new Set(Object.values(RELATION_ATOM_OPERATORS));
128
+ const WRAPPER_TYPES = /* @__PURE__ */ new Set(["styling", "color"]);
129
+ function isRecord(value) {
130
+ return typeof value === "object" && value !== null && !Array.isArray(value);
131
+ }
132
+ function nodeText(node) {
133
+ return typeof node.text === "string" ? node.text : void 0;
134
+ }
135
+ function glyphOfNode(node) {
136
+ const text = nodeText(node);
137
+ return text === void 0 ? void 0 : require_latex_symbols.glyphOfSymbolText(text);
138
+ }
139
+ function numericText(text) {
140
+ return text.length === 1 && text >= "0" && text <= "9";
141
+ }
142
+ function diagnose(context, code, detail) {
143
+ context.diagnostics.push({
144
+ code,
145
+ ...detail === void 0 ? {} : { detail }
146
+ });
147
+ context.sink?.({
148
+ code,
149
+ ...detail === void 0 ? {} : { detail }
150
+ });
151
+ }
152
+ function unparsed(latex) {
153
+ return {
154
+ kind: "unparsed",
155
+ latex
156
+ };
157
+ }
158
+ function app(operator, args) {
159
+ return {
160
+ kind: "app",
161
+ operator,
162
+ args: [...args]
163
+ };
164
+ }
165
+ function spanOfNodes(context, nodes) {
166
+ let start;
167
+ let end;
168
+ const visit = (node) => {
169
+ const loc = node.loc;
170
+ if (isRecord(loc) && typeof loc.start === "number" && typeof loc.end === "number") {
171
+ if (start === void 0 || loc.start < start) start = loc.start;
172
+ if (end === void 0 || loc.end > end) end = loc.end;
173
+ }
174
+ for (const value of Object.values(node)) if (require_latex_temml.isTemmlNode(value)) visit(value);
175
+ else if (Array.isArray(value)) {
176
+ for (const element of value) if (require_latex_temml.isTemmlNode(element)) visit(element);
177
+ }
178
+ };
179
+ for (const node of nodes) visit(node);
180
+ return start === void 0 || end === void 0 ? "" : context.input.slice(start, end);
181
+ }
182
+ function nodesOfScript(script) {
183
+ if (!require_latex_temml.isTemmlNode(script)) return;
184
+ if (script.type === "ordgroup" || WRAPPER_TYPES.has(script.type)) {
185
+ const body = script.body;
186
+ return Array.isArray(body) && body.every(require_latex_temml.isTemmlNode) ? body : void 0;
187
+ }
188
+ return [script];
189
+ }
190
+ function lowerNodeList(nodes, context) {
191
+ const present = nodes.filter((node) => !PRESENTATION_ONLY_TYPES.has(node.type));
192
+ const segments = [];
193
+ const operators = [];
194
+ let current = [];
195
+ let unmappable;
196
+ for (const node of present) {
197
+ const operator = operatorOfNode(node);
198
+ if (operator !== void 0) {
199
+ segments.push(current);
200
+ operators.push(operator);
201
+ current = [];
202
+ continue;
203
+ }
204
+ if (isOperatorAtom(node)) {
205
+ unmappable = unmappable ?? nodeText(node) ?? node.type;
206
+ continue;
207
+ }
208
+ current.push(node);
209
+ }
210
+ segments.push(current);
211
+ if (unmappable !== void 0) {
212
+ const detail = spanOfNodes(context, present) || unmappable;
213
+ diagnose(context, "latex/operator-unmapped", detail);
214
+ return unparsed(detail);
215
+ }
216
+ const normalised = normaliseUnaryMinus(operators, segments.map((segmentNodes) => ({
217
+ nodes: segmentNodes,
218
+ negated: false
219
+ })));
220
+ const [firstSegment, ...restSegments] = normalised.segments;
221
+ if (firstSegment === void 0) {
222
+ diagnose(context, "latex/operator-placement-unparsed", spanOfNodes(context, present));
223
+ return unparsed(spanOfNodes(context, present));
224
+ }
225
+ if (normalised.operators.length === 0) return lowerFoldSegment(firstSegment, context);
226
+ const detail = spanOfNodes(context, present);
227
+ if (firstSegment.nodes.length === 0) {
228
+ diagnose(context, "latex/operator-placement-unparsed", detail);
229
+ return unparsed(detail);
230
+ }
231
+ return fold(lowerFoldSegment(firstSegment, context), normalised.operators, restSegments, context, detail);
232
+ }
233
+ function isOperatorAtom(node) {
234
+ return node.type === "atom" && (node.family === "bin" || node.family === "rel");
235
+ }
236
+ function operatorOfNode(node) {
237
+ if (node.type === "atom") {
238
+ const text = nodeText(node);
239
+ if (text === void 0) return;
240
+ return BINARY_ATOM_OPERATORS[text] ?? RELATION_ATOM_OPERATORS[text];
241
+ }
242
+ if (node.type === "textord" && nodeText(node) === "/") return "math:divide";
243
+ }
244
+ function lowerTerm(nodes, context) {
245
+ return lowerTermItems(termItems(nodes), context);
246
+ }
247
+ function lowerTermItems(items, context) {
248
+ const first = items[0];
249
+ if (first === void 0) {
250
+ diagnose(context, "latex/construct-unparsed");
251
+ return unparsed("");
252
+ }
253
+ if (first.kind === "node") {
254
+ const binder = readBinder(first.node, context);
255
+ if (binder.status === "binder") {
256
+ const rest = items.slice(1);
257
+ const body = rest.length === 0 ? implicitArgument(context) : lowerTermItems(rest, {
258
+ ...context,
259
+ binders: [binder.binder, ...context.binders]
260
+ });
261
+ return {
262
+ kind: binder.kind,
263
+ binder: binder.binder,
264
+ lower: binder.lower,
265
+ upper: binder.upper,
266
+ body
267
+ };
268
+ }
269
+ if (binder.status === "degraded") return unparsed(binder.detail);
270
+ const namedFunction = namedFunctionOfOp(first.node);
271
+ if (namedFunction !== void 0) {
272
+ const rest = items.slice(1);
273
+ return app(namedFunction, [rest.length === 0 ? implicitArgument(context) : lowerTermItems(rest, context)]);
274
+ }
275
+ }
276
+ const lowered = lowerTermItem(first, context);
277
+ if (items.length > 1) {
278
+ const detail = spanOfNodes(context, nodesOfItems(items));
279
+ diagnose(context, "latex/juxtaposition-unparsed", detail);
280
+ return unparsed(detail);
281
+ }
282
+ return lowered;
283
+ }
284
+ function nodesOfItems(items) {
285
+ const nodes = [];
286
+ for (const item of items) if (item.kind === "node") nodes.push(item.node);
287
+ else nodes.push(...item.nodes);
288
+ return nodes;
289
+ }
290
+ function implicitArgument(context) {
291
+ diagnose(context, "latex/construct-unparsed");
292
+ return unparsed("");
293
+ }
294
+ function termItems(nodes) {
295
+ const items = [];
296
+ let digits = [];
297
+ let digitNodes = [];
298
+ const flushDigits = () => {
299
+ if (digits.length > 0) {
300
+ items.push({
301
+ kind: "number",
302
+ literal: digits.join(""),
303
+ nodes: digitNodes
304
+ });
305
+ digits = [];
306
+ digitNodes = [];
307
+ }
308
+ };
309
+ for (const node of nodes) {
310
+ const text = nodeText(node);
311
+ if (node.type === "textord" && text !== void 0 && (numericText(text) || text === ".")) {
312
+ digits.push(text);
313
+ digitNodes.push(node);
314
+ continue;
315
+ }
316
+ flushDigits();
317
+ items.push({
318
+ kind: "node",
319
+ node
320
+ });
321
+ }
322
+ flushDigits();
323
+ return items;
324
+ }
325
+ function lowerTermItem(item, context) {
326
+ if (item.kind === "number") {
327
+ const rational = require_latex_rational.decimalToRational(item.literal);
328
+ if (rational === void 0) {
329
+ diagnose(context, "latex/construct-unparsed", item.literal);
330
+ return unparsed(item.literal);
331
+ }
332
+ return {
333
+ kind: "num",
334
+ numerator: rational.numerator,
335
+ denominator: rational.denominator
336
+ };
337
+ }
338
+ return lowerNode(item.node, context);
339
+ }
340
+ function lowerNode(node, context) {
341
+ switch (node.type) {
342
+ case "mathord": {
343
+ const glyph = glyphOfNode(node);
344
+ return glyph === void 0 ? degradeNode(node, context) : symbolExpression(glyph, context);
345
+ }
346
+ case "textord": {
347
+ const text = nodeText(node);
348
+ if (text === void 0) return degradeNode(node, context);
349
+ if (/^[0-9]$/.test(text)) return {
350
+ kind: "num",
351
+ numerator: text,
352
+ denominator: "1"
353
+ };
354
+ const glyph = require_latex_symbols.glyphOfSymbolText(text);
355
+ return glyph === void 0 ? degradeNode(node, context) : symbolExpression(glyph, context);
356
+ }
357
+ case "atom": return degradeNode(node, context);
358
+ case "genfrac": return lowerGenfrac(node, context);
359
+ case "sqrt": return lowerSqrt(node, context);
360
+ case "supsub": return lowerSupsub(node, context);
361
+ case "op": return degradeNode(node, context);
362
+ case "ordgroup":
363
+ case "styling":
364
+ case "color": {
365
+ const body = node.body;
366
+ if (!Array.isArray(body) || !body.every(require_latex_temml.isTemmlNode)) return degradeNode(node, context);
367
+ return lowerNodeList(body, context);
368
+ }
369
+ case "delimiter":
370
+ case "leftright": return lowerGrouping(node, context);
371
+ case "array": return lowerArray(node, context);
372
+ case "text": {
373
+ const detail = spanOfNodes(context, [node]);
374
+ diagnose(context, "latex/text-unparsed", detail);
375
+ return unparsed(detail);
376
+ }
377
+ default: return degradeNode(node, context);
378
+ }
379
+ }
380
+ function degradeNode(node, context) {
381
+ const span = spanOfNodes(context, [node]);
382
+ const detail = span !== "" ? span : nodeText(node) ?? node.type;
383
+ diagnose(context, "latex/construct-unparsed", detail);
384
+ return unparsed(detail);
385
+ }
386
+ function symbolExpression(glyph, context) {
387
+ if (context.binders.includes(glyph)) return {
388
+ kind: "sym",
389
+ id: glyph
390
+ };
391
+ return {
392
+ kind: "sym",
393
+ id: context.resolver.resolve(glyph)
394
+ };
395
+ }
396
+ function lowerGenfrac(node, context) {
397
+ const hasBar = node.hasBarLine !== false;
398
+ const delimited = node.leftDelim !== null && node.leftDelim !== void 0 || node.rightDelim !== null && node.rightDelim !== void 0;
399
+ if (!hasBar || delimited || !require_latex_temml.isTemmlNode(node.numer) || !require_latex_temml.isTemmlNode(node.denom)) {
400
+ const detail = spanOfNodes(context, [node]);
401
+ diagnose(context, "latex/genfrac-unparsed", detail);
402
+ return unparsed(detail);
403
+ }
404
+ return app("math:divide", [lowerNode(node.numer, context), lowerNode(node.denom, context)]);
405
+ }
406
+ function lowerSqrt(node, context) {
407
+ const index = node.index === null ? void 0 : node.index;
408
+ if (!require_latex_temml.isTemmlNode(node.body) || index !== void 0 && !require_latex_temml.isTemmlNode(index)) return degradeNode(node, context);
409
+ const body = lowerNode(node.body, context);
410
+ if (index === void 0) return app("math:sqrt", [body]);
411
+ return app("math:pow", [body, app("math:divide", [{
412
+ kind: "num",
413
+ numerator: "1",
414
+ denominator: "1"
415
+ }, lowerNode(index, context)])]);
416
+ }
417
+ function lowerGrouping(node, context) {
418
+ const body = node.body;
419
+ if (!Array.isArray(body) || !body.every(require_latex_temml.isTemmlNode)) return degradeNode(node, context);
420
+ if (body.length === 1) {
421
+ const only = body[0];
422
+ if (only?.type === "array") return lowerArray(only, context);
423
+ }
424
+ return lowerNodeList(body, context);
425
+ }
426
+ function lowerArray(node, context) {
427
+ const separated = Array.isArray(node.cols) && node.cols.some((col) => !require_latex_temml.isTemmlNode(col) || col.type === "separator");
428
+ const body = node.body;
429
+ const envClasses = node.envClasses;
430
+ const layoutEnvironment = Array.isArray(envClasses) && envClasses.length > 0;
431
+ if (layoutEnvironment || separated || !Array.isArray(body)) {
432
+ const detail = spanOfNodes(context, [node]);
433
+ diagnose(context, layoutEnvironment ? "latex/array-environment-unparsed" : "latex/construct-unparsed", detail);
434
+ return unparsed(detail);
435
+ }
436
+ const rows = [];
437
+ for (const row of body) {
438
+ if (!Array.isArray(row) || !row.every(require_latex_temml.isTemmlNode)) return degradeNode(node, context);
439
+ rows.push(row.map((cell) => lowerNode(cell, context)));
440
+ }
441
+ if (rows.length > 0 && new Set(rows.map((row) => row.length)).size > 1) {
442
+ const detail = spanOfNodes(context, [node]);
443
+ diagnose(context, "latex/construct-unparsed", detail);
444
+ return unparsed(detail);
445
+ }
446
+ return {
447
+ kind: "matrix",
448
+ rows
449
+ };
450
+ }
451
+ function lowerSupsub(node, context) {
452
+ const binder = readBinder(node, context);
453
+ if (binder.status === "binder") return {
454
+ kind: binder.kind,
455
+ binder: binder.binder,
456
+ lower: binder.lower,
457
+ upper: binder.upper,
458
+ body: implicitArgument(context)
459
+ };
460
+ if (binder.status === "degraded") return unparsed(binder.detail);
461
+ const detail = spanOfNodes(context, [node]);
462
+ const base = require_latex_temml.isTemmlNode(node.base) ? node.base : void 0;
463
+ const sub = nodesOfScript(node.sub);
464
+ const sup = nodesOfScript(node.sup);
465
+ if (sub !== void 0) {
466
+ const subWritten = simpleScriptGlyph(sub);
467
+ const baseGlyph = base !== void 0 && (base.type === "mathord" || base.type === "textord") ? glyphOfNode(base) : void 0;
468
+ if (baseGlyph === void 0 || subWritten === void 0) {
469
+ diagnose(context, "latex/subscript-unparsed", detail);
470
+ return unparsed(detail);
471
+ }
472
+ const subscriptedGlyph = `${baseGlyph}_${subWritten}`;
473
+ if (sup === void 0) return symbolExpression(subscriptedGlyph, context);
474
+ const tripleGlyph = `${subscriptedGlyph}^${scriptWrittenForm(sup, context)}`;
475
+ if (context.resolver.isCurated(tripleGlyph)) return symbolExpression(tripleGlyph, context);
476
+ return app("math:pow", [symbolExpression(subscriptedGlyph, context), lowerNodeList(sup, context)]);
477
+ }
478
+ if (sup === void 0) return degradeNode(node, context);
479
+ const exponent = lowerNodeList(sup, context);
480
+ if (base === void 0) return degradeNode(node, context);
481
+ if (base.type !== "mathord" && base.type !== "textord") {
482
+ const loweredBase = lowerNode(base, context);
483
+ if (loweredBase.kind === "unparsed") {
484
+ diagnose(context, "latex/script-base-unparsed", detail);
485
+ return unparsed(detail);
486
+ }
487
+ return app("math:pow", [loweredBase, exponent]);
488
+ }
489
+ const baseGlyph = glyphOfNode(base);
490
+ if (baseGlyph === void 0) return degradeNode(node, context);
491
+ const scriptedGlyph = `${baseGlyph}^${scriptWrittenForm(sup, context)}`;
492
+ if (context.resolver.isCurated(scriptedGlyph)) return symbolExpression(scriptedGlyph, context);
493
+ return app("math:pow", [symbolExpression(baseGlyph, context), exponent]);
494
+ }
495
+ function scriptWrittenForm(nodes, context) {
496
+ const span = spanOfNodes(context, nodes);
497
+ if (span !== "") return span;
498
+ return nodes.map((node) => glyphOfNode(node) ?? nodeText(node) ?? "").join("");
499
+ }
500
+ function simpleScriptGlyph(nodes) {
501
+ const parts = [];
502
+ for (const node of nodes) {
503
+ const text = nodeText(node);
504
+ if (node.type === "mathord") {
505
+ const glyph = glyphOfNode(node);
506
+ if (glyph === void 0) return;
507
+ parts.push(glyph);
508
+ continue;
509
+ }
510
+ if (node.type === "textord" && text !== void 0 && /^[0-9A-Za-z]$/.test(text)) {
511
+ parts.push(text);
512
+ continue;
513
+ }
514
+ return;
515
+ }
516
+ return parts.length === 0 ? void 0 : parts.join("");
517
+ }
518
+ function readBinder(node, context) {
519
+ if (node.type !== "supsub") return { status: "not-a-binder" };
520
+ const base = require_latex_temml.isTemmlNode(node.base) ? node.base : void 0;
521
+ if (base?.type !== "op" || base.symbol !== true || typeof base.name !== "string") return { status: "not-a-binder" };
522
+ if (base.name !== "\\sum" && base.name !== "\\prod") return { status: "not-a-binder" };
523
+ const kind = base.name === "\\sum" ? "sum" : "prod";
524
+ const detail = spanOfNodes(context, [node]);
525
+ const sub = nodesOfScript(node.sub);
526
+ const sup = nodesOfScript(node.sup);
527
+ const upper = sup === void 0 ? implicitBound(context) : lowerNodeList(sup, context);
528
+ if ((sub?.length ?? 0) >= RELATION_SUBSCRIPT_MIN_NODES) {
529
+ const first = sub?.[0];
530
+ const relation = sub?.[1];
531
+ const rest = sub?.slice(2) ?? [];
532
+ const firstGlyph = first?.type === "mathord" ? glyphOfNode(first) : void 0;
533
+ if (firstGlyph !== void 0 && relation?.type === "atom" && nodeText(relation) === "=") return {
534
+ status: "binder",
535
+ kind,
536
+ binder: firstGlyph,
537
+ lower: lowerNodeList(rest, context),
538
+ upper
539
+ };
540
+ }
541
+ if (sub?.length === 1) {
542
+ const singleGlyph = simpleScriptGlyph(sub);
543
+ if (singleGlyph !== void 0) {
544
+ diagnose(context, "latex/binder-bound-implicit", detail);
545
+ return {
546
+ status: "binder",
547
+ kind,
548
+ binder: singleGlyph,
549
+ lower: unparsed(""),
550
+ upper
551
+ };
552
+ }
553
+ }
554
+ diagnose(context, "latex/binder-bound-unreadable", detail);
555
+ return {
556
+ status: "degraded",
557
+ detail
558
+ };
559
+ }
560
+ function implicitBound(context) {
561
+ diagnose(context, "latex/binder-bound-implicit");
562
+ return unparsed("");
563
+ }
564
+ function namedFunctionOfOp(node) {
565
+ if (node.type !== "op" || node.symbol === true) return;
566
+ const name = typeof node.name === "string" ? node.name : void 0;
567
+ return name === void 0 ? void 0 : NAMED_FUNCTION_OPERATORS[name];
568
+ }
569
+ function lowerLatex(latex, options) {
570
+ return lowerParsed(latex, require_latex_temml.parseLatex(latex), options);
571
+ }
572
+ function lowerParsed(latex, parsed, options) {
573
+ if (latex.trim() === "") return {
574
+ expression: unparsed(""),
575
+ diagnostics: [],
576
+ mintedSymbols: []
577
+ };
578
+ const resolver = new require_latex_symbols.SymbolResolver(options?.symbolEntries ?? []);
579
+ const context = {
580
+ input: latex,
581
+ resolver,
582
+ binders: [],
583
+ diagnostics: [],
584
+ sink: options?.sink
585
+ };
586
+ if (parsed.status === "unparseable") {
587
+ diagnose(context, "latex/parse-error", parsed.message);
588
+ return {
589
+ expression: unparsed(latex),
590
+ diagnostics: context.diagnostics,
591
+ mintedSymbols: resolver.mintedEntries()
592
+ };
593
+ }
594
+ return {
595
+ expression: lowerNodeList(parsed.nodes, context),
596
+ diagnostics: context.diagnostics,
597
+ mintedSymbols: resolver.mintedEntries()
598
+ };
599
+ }
600
+ function latexToFormula(latex, options) {
601
+ const parsed = require_latex_temml.parseLatex(latex);
602
+ const lowering = lowerParsed(latex, parsed, options);
603
+ return {
604
+ formula: {
605
+ mathml: parsed.status === "parsed" ? [...parsed.mathml] : [],
606
+ presentation: { latex },
607
+ content: lowering.expression,
608
+ provenance: {
609
+ source: options?.source ?? "lowered:latex",
610
+ editTrail: []
611
+ }
612
+ },
613
+ diagnostics: lowering.diagnostics,
614
+ mintedSymbols: lowering.mintedSymbols
615
+ };
616
+ }
617
+ //#endregion
618
+ Object.defineProperty(exports, "RELATION_ATOM_OPERATORS", {
619
+ enumerable: true,
620
+ get: function() {
621
+ return RELATION_ATOM_OPERATORS;
622
+ }
623
+ });
624
+ Object.defineProperty(exports, "RELATION_OPERATORS", {
625
+ enumerable: true,
626
+ get: function() {
627
+ return RELATION_OPERATORS;
628
+ }
629
+ });
630
+ Object.defineProperty(exports, "SUBTRACT_OPERATOR", {
631
+ enumerable: true,
632
+ get: function() {
633
+ return SUBTRACT_OPERATOR;
634
+ }
635
+ });
636
+ Object.defineProperty(exports, "UNARY_MINUS_OPERATOR", {
637
+ enumerable: true,
638
+ get: function() {
639
+ return UNARY_MINUS_OPERATOR;
640
+ }
641
+ });
642
+ Object.defineProperty(exports, "app", {
643
+ enumerable: true,
644
+ get: function() {
645
+ return app;
646
+ }
647
+ });
648
+ Object.defineProperty(exports, "diagnose", {
649
+ enumerable: true,
650
+ get: function() {
651
+ return diagnose;
652
+ }
653
+ });
654
+ Object.defineProperty(exports, "fold", {
655
+ enumerable: true,
656
+ get: function() {
657
+ return fold;
658
+ }
659
+ });
660
+ Object.defineProperty(exports, "foldArithmetic", {
661
+ enumerable: true,
662
+ get: function() {
663
+ return foldArithmetic;
664
+ }
665
+ });
666
+ Object.defineProperty(exports, "latexToFormula", {
667
+ enumerable: true,
668
+ get: function() {
669
+ return latexToFormula;
670
+ }
671
+ });
672
+ Object.defineProperty(exports, "lowerFoldSegment", {
673
+ enumerable: true,
674
+ get: function() {
675
+ return lowerFoldSegment;
676
+ }
677
+ });
678
+ Object.defineProperty(exports, "lowerLatex", {
679
+ enumerable: true,
680
+ get: function() {
681
+ return lowerLatex;
682
+ }
683
+ });
684
+ Object.defineProperty(exports, "lowerTerm", {
685
+ enumerable: true,
686
+ get: function() {
687
+ return lowerTerm;
688
+ }
689
+ });
690
+ Object.defineProperty(exports, "normaliseUnaryMinus", {
691
+ enumerable: true,
692
+ get: function() {
693
+ return normaliseUnaryMinus;
694
+ }
695
+ });
696
+ Object.defineProperty(exports, "unparsed", {
697
+ enumerable: true,
698
+ get: function() {
699
+ return unparsed;
700
+ }
701
+ });