documents.js 1.61.0 → 1.63.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.
Files changed (236) hide show
  1. package/README.md +120 -47
  2. package/dist/{blr-types-CYYC3yYU.d.cts → blr-types-DVgO1DQ9.d.cts} +1 -1
  3. package/dist/{blr-types-CYYC3yYU.d.ts → blr-types-DVgO1DQ9.d.ts} +1 -1
  4. package/dist/convert/capability.cjs +1 -0
  5. package/dist/convert/capability.d.cts +1 -1
  6. package/dist/convert/capability.d.ts +1 -1
  7. package/dist/convert/capability.js +1 -0
  8. package/dist/convert/convert.cjs +92 -38
  9. package/dist/convert/convert.d.cts +11 -4
  10. package/dist/convert/convert.d.ts +11 -4
  11. package/dist/convert/convert.js +93 -39
  12. package/dist/convert/local.cjs +15 -2
  13. package/dist/convert/local.js +15 -2
  14. package/dist/convert/port.d.cts +8 -4
  15. package/dist/convert/port.d.ts +8 -4
  16. package/dist/edit/docx/content.cjs +75 -13
  17. package/dist/edit/docx/content.d.cts +8 -2
  18. package/dist/edit/docx/content.d.ts +8 -2
  19. package/dist/edit/docx/content.js +75 -13
  20. package/dist/edit/docx/paragraph.cjs +14 -0
  21. package/dist/edit/docx/paragraph.d.cts +5 -0
  22. package/dist/edit/docx/paragraph.d.ts +5 -0
  23. package/dist/edit/docx/paragraph.js +14 -0
  24. package/dist/edit/docx/run.cjs +1 -1
  25. package/dist/edit/docx/run.js +1 -1
  26. package/dist/edit/docx/table.cjs +41 -0
  27. package/dist/edit/docx/table.d.cts +7 -1
  28. package/dist/edit/docx/table.d.ts +7 -1
  29. package/dist/edit/docx/table.js +42 -1
  30. package/dist/edit/geometry.cjs +38 -0
  31. package/dist/edit/geometry.d.cts +7 -0
  32. package/dist/edit/geometry.d.ts +7 -0
  33. package/dist/edit/geometry.js +36 -0
  34. package/dist/edit/odg/content.cjs +15 -12
  35. package/dist/edit/odg/content.js +15 -12
  36. package/dist/edit/odg/editor.d.cts +1 -1
  37. package/dist/edit/odg/editor.d.ts +1 -1
  38. package/dist/edit/odg/page.d.cts +3 -3
  39. package/dist/edit/odg/page.d.ts +3 -3
  40. package/dist/edit/odg/vector.cjs +22 -13
  41. package/dist/edit/odg/vector.d.cts +1 -1
  42. package/dist/edit/odg/vector.d.ts +1 -1
  43. package/dist/edit/odg/vector.js +23 -14
  44. package/dist/edit/odp/content.cjs +15 -0
  45. package/dist/edit/odp/content.js +16 -1
  46. package/dist/edit/odp/editor.d.cts +1 -1
  47. package/dist/edit/odp/editor.d.ts +1 -1
  48. package/dist/edit/odp/image.d.cts +1 -1
  49. package/dist/edit/odp/image.d.ts +1 -1
  50. package/dist/edit/odp/shape.cjs +11 -30
  51. package/dist/edit/odp/shape.d.cts +2 -2
  52. package/dist/edit/odp/shape.d.ts +2 -2
  53. package/dist/edit/odp/shape.js +13 -33
  54. package/dist/edit/odp/slide.cjs +11 -0
  55. package/dist/edit/odp/slide.d.cts +14 -4
  56. package/dist/edit/odp/slide.d.ts +14 -4
  57. package/dist/edit/odp/slide.js +12 -1
  58. package/dist/edit/odt/content.cjs +56 -16
  59. package/dist/edit/odt/content.d.cts +4 -2
  60. package/dist/edit/odt/content.d.ts +4 -2
  61. package/dist/edit/odt/content.js +56 -17
  62. package/dist/edit/odt/editor.d.cts +1 -1
  63. package/dist/edit/odt/editor.d.ts +1 -1
  64. package/dist/edit/odt/table.cjs +36 -0
  65. package/dist/edit/odt/table.d.cts +1 -36
  66. package/dist/edit/odt/table.d.ts +1 -36
  67. package/dist/edit/odt/table.js +37 -1
  68. package/dist/edit/pptx/content.cjs +71 -3
  69. package/dist/edit/pptx/content.js +71 -3
  70. package/dist/edit/pptx/editor.d.cts +1 -1
  71. package/dist/edit/pptx/editor.d.ts +1 -1
  72. package/dist/edit/pptx/image.d.cts +1 -1
  73. package/dist/edit/pptx/image.d.ts +1 -1
  74. package/dist/edit/pptx/shape.cjs +23 -1
  75. package/dist/edit/pptx/shape.d.cts +2 -2
  76. package/dist/edit/pptx/shape.d.ts +2 -2
  77. package/dist/edit/pptx/shape.js +23 -3
  78. package/dist/edit/pptx/slide.cjs +10 -1
  79. package/dist/edit/pptx/slide.d.cts +11 -4
  80. package/dist/edit/pptx/slide.d.ts +11 -4
  81. package/dist/edit/pptx/slide.js +10 -1
  82. package/dist/edit/pptx/table.cjs +136 -0
  83. package/dist/edit/pptx/table.d.cts +2 -0
  84. package/dist/edit/pptx/table.d.ts +2 -0
  85. package/dist/edit/pptx/table.js +131 -0
  86. package/dist/firebird/backup.d.cts +1 -1
  87. package/dist/firebird/backup.d.ts +1 -1
  88. package/dist/firebird/blr-types.cjs +2 -2
  89. package/dist/firebird/blr-types.d.cts +1 -1
  90. package/dist/firebird/blr-types.d.ts +1 -1
  91. package/dist/firebird/blr-types.js +2 -2
  92. package/dist/firebird/data.cjs +68 -14
  93. package/dist/firebird/data.d.cts +1 -1
  94. package/dist/firebird/data.d.ts +1 -1
  95. package/dist/firebird/data.js +68 -14
  96. package/dist/firebird/schema.cjs +13 -1
  97. package/dist/firebird/schema.d.cts +1 -1
  98. package/dist/firebird/schema.d.ts +1 -1
  99. package/dist/firebird/schema.js +13 -1
  100. package/dist/fonts/obfuscation.cjs +50 -0
  101. package/dist/fonts/obfuscation.d.cts +9 -0
  102. package/dist/fonts/obfuscation.d.ts +9 -0
  103. package/dist/fonts/obfuscation.js +46 -0
  104. package/dist/fonts/odf.cjs +86 -0
  105. package/dist/fonts/odf.d.cts +9 -0
  106. package/dist/fonts/odf.d.ts +9 -0
  107. package/dist/fonts/odf.js +84 -0
  108. package/dist/fonts/ooxml.cjs +129 -0
  109. package/dist/fonts/ooxml.d.cts +9 -0
  110. package/dist/fonts/ooxml.d.ts +9 -0
  111. package/dist/fonts/ooxml.js +127 -0
  112. package/dist/fonts/registry.cjs +18 -0
  113. package/dist/fonts/registry.d.cts +2 -0
  114. package/dist/fonts/registry.d.ts +2 -0
  115. package/dist/fonts/registry.js +16 -0
  116. package/dist/{geometry-DvwdrIij.d.cts → geometry-DaJr8yQW.d.cts} +2 -2
  117. package/dist/{geometry-DvwdrIij.d.ts → geometry-DaJr8yQW.d.ts} +2 -2
  118. package/dist/hsqldb/binary-script.cjs +154 -0
  119. package/dist/hsqldb/binary-script.d.cts +15 -0
  120. package/dist/hsqldb/binary-script.d.ts +15 -0
  121. package/dist/hsqldb/binary-script.js +151 -0
  122. package/dist/hsqldb/cache.cjs +18 -11
  123. package/dist/hsqldb/cache.d.cts +9 -4
  124. package/dist/hsqldb/cache.d.ts +9 -4
  125. package/dist/hsqldb/cache.js +18 -11
  126. package/dist/hsqldb/rowformat.cjs +60 -16
  127. package/dist/hsqldb/rowformat.d.cts +2 -21
  128. package/dist/hsqldb/rowformat.d.ts +2 -21
  129. package/dist/hsqldb/rowformat.js +60 -17
  130. package/dist/{image-BZDy72Jb.d.cts → image-7snmKIxn.d.cts} +1 -1
  131. package/dist/{image-B-xQWYYS.d.ts → image-Cs8xF753.d.ts} +1 -1
  132. package/dist/{image-CeqwL-Qs.d.ts → image-DHcA9kaK.d.ts} +1 -1
  133. package/dist/{image-DWX6cNOD.d.cts → image-M5SezZVI.d.cts} +1 -1
  134. package/dist/index.cjs +86 -3
  135. package/dist/index.d.cts +33 -21
  136. package/dist/index.d.ts +33 -21
  137. package/dist/index.js +18 -5
  138. package/dist/layout/cell-typing.cjs +209 -0
  139. package/dist/layout/cell-typing.d.cts +22 -0
  140. package/dist/layout/cell-typing.d.ts +22 -0
  141. package/dist/layout/cell-typing.js +208 -0
  142. package/dist/layout/drawing.cjs +193 -17
  143. package/dist/layout/drawing.js +193 -17
  144. package/dist/layout/engine.cjs +46 -29
  145. package/dist/layout/engine.d.cts +0 -2
  146. package/dist/layout/engine.d.ts +0 -2
  147. package/dist/layout/engine.js +47 -30
  148. package/dist/layout/lattice.cjs +139 -0
  149. package/dist/layout/lattice.d.cts +20 -0
  150. package/dist/layout/lattice.d.ts +20 -0
  151. package/dist/layout/lattice.js +135 -0
  152. package/dist/layout/reconstruct.cjs +176 -113
  153. package/dist/layout/reconstruct.d.cts +2 -0
  154. package/dist/layout/reconstruct.d.ts +2 -0
  155. package/dist/layout/reconstruct.js +173 -110
  156. package/dist/layout/shared.cjs +52 -0
  157. package/dist/layout/shared.d.cts +4 -2
  158. package/dist/layout/shared.d.ts +4 -2
  159. package/dist/layout/shared.js +51 -1
  160. package/dist/layout/sheets.cjs +45 -11
  161. package/dist/layout/sheets.js +46 -12
  162. package/dist/layout/slides.cjs +7 -7
  163. package/dist/layout/slides.d.cts +0 -3
  164. package/dist/layout/slides.d.ts +0 -3
  165. package/dist/layout/slides.js +7 -7
  166. package/dist/markdown/write.cjs +19 -6
  167. package/dist/markdown/write.js +19 -6
  168. package/dist/mathml/layout.cjs +2 -2
  169. package/dist/mathml/layout.d.cts +1 -1
  170. package/dist/mathml/layout.d.ts +1 -1
  171. package/dist/mathml/layout.js +2 -2
  172. package/dist/model/embedded-drawing.cjs +32 -0
  173. package/dist/model/embedded-drawing.d.cts +8 -0
  174. package/dist/model/embedded-drawing.d.ts +8 -0
  175. package/dist/model/embedded-drawing.js +30 -0
  176. package/dist/model/formula.cjs +31 -0
  177. package/dist/model/formula.d.cts +6 -6
  178. package/dist/model/formula.d.ts +6 -6
  179. package/dist/model/formula.js +27 -0
  180. package/dist/model/geometry.d.cts +1 -1
  181. package/dist/model/geometry.d.ts +1 -1
  182. package/dist/model/paint-order.cjs +15 -0
  183. package/dist/model/paint-order.d.cts +14 -0
  184. package/dist/model/paint-order.d.ts +14 -0
  185. package/dist/model/paint-order.js +14 -0
  186. package/dist/odb/components.cjs +12 -0
  187. package/dist/odb/components.d.cts +6 -0
  188. package/dist/odb/components.d.ts +6 -0
  189. package/dist/odb/components.js +10 -0
  190. package/dist/odb/read.cjs +11 -7
  191. package/dist/odb/read.d.cts +3 -2
  192. package/dist/odb/read.d.ts +3 -2
  193. package/dist/odb/read.js +11 -7
  194. package/dist/odf/formula/detect.cjs +86 -18
  195. package/dist/odf/formula/detect.d.cts +10 -6
  196. package/dist/odf/formula/detect.d.ts +10 -6
  197. package/dist/odf/formula/detect.js +86 -19
  198. package/dist/odf/formula/read.cjs +4 -16
  199. package/dist/odf/formula/read.d.cts +4 -8
  200. package/dist/odf/formula/read.d.ts +4 -8
  201. package/dist/odf/formula/read.js +5 -17
  202. package/dist/odf/odp/read.cjs +9 -19
  203. package/dist/odf/odp/read.d.cts +2 -7
  204. package/dist/odf/odp/read.d.ts +2 -7
  205. package/dist/odf/odp/read.js +10 -20
  206. package/dist/odf/odt/read.cjs +63 -23
  207. package/dist/odf/odt/read.d.cts +2 -7
  208. package/dist/odf/odt/read.d.ts +2 -7
  209. package/dist/odf/odt/read.js +64 -24
  210. package/dist/omml/write.cjs +258 -0
  211. package/dist/omml/write.d.cts +2 -0
  212. package/dist/omml/write.d.ts +2 -0
  213. package/dist/omml/write.js +256 -0
  214. package/dist/registry-CrSqLIcn.d.ts +19 -0
  215. package/dist/registry-CtPhMD0f.d.cts +19 -0
  216. package/dist/rowformat-Cl2exlEh.d.cts +25 -0
  217. package/dist/rowformat-Cl2exlEh.d.ts +25 -0
  218. package/dist/{schema-Acco3Ezi.d.cts → schema-BeR_KJ0X.d.cts} +3 -1
  219. package/dist/{schema-VkAA6pF5.d.ts → schema-CSKwSb_v.d.ts} +3 -1
  220. package/dist/{shape-BUfjw5eZ.d.cts → shape-BhkP6HB8.d.cts} +6 -2
  221. package/dist/{shape-CTkl6kt8.d.ts → shape-BkVcwPcf.d.ts} +6 -2
  222. package/dist/{shape-BfN3m52m.d.ts → shape-CVXedeL7.d.ts} +3 -2
  223. package/dist/{shape-CN2ycxOo.d.cts → shape-CXZ6fejV.d.cts} +3 -2
  224. package/dist/table-C3CPZ--2.d.cts +35 -0
  225. package/dist/table-DjYcNOFm.d.cts +44 -0
  226. package/dist/table-DqfqMkVq.d.ts +44 -0
  227. package/dist/table-DvS_7UQS.d.ts +35 -0
  228. package/dist/{vector-ClohqsOe.d.cts → vector-Puuqu_IT.d.cts} +4 -0
  229. package/dist/{vector-ClohqsOe.d.ts → vector-Puuqu_IT.d.ts} +4 -0
  230. package/dist/write-DuyfnbL_.d.cts +16 -0
  231. package/dist/write-Zqwx6pka.d.ts +16 -0
  232. package/package.json +4 -4
  233. package/dist/odf/formula/placeholder.cjs +0 -36
  234. package/dist/odf/formula/placeholder.d.cts +0 -14
  235. package/dist/odf/formula/placeholder.d.ts +0 -14
  236. package/dist/odf/formula/placeholder.js +0 -36
@@ -1,28 +1,191 @@
1
1
  import { flipY } from "../model/geometry.js";
2
+ import { mergeByPaintOrder } from "../model/paint-order.js";
2
3
  import { convertShape } from "./slides.js";
3
4
  import { LAYOUT_FORMAT_VERSION } from "document-schema.js";
5
+ import { rotatePointAboutCenter } from "pdf-codec";
4
6
  //#region src/layout/drawing.ts
7
+ function isRotated(rotationDeg) {
8
+ return rotationDeg !== void 0 && rotationDeg !== 0;
9
+ }
10
+ function boxCenter(box) {
11
+ return {
12
+ x: box.xPt + box.widthPt / 2,
13
+ y: box.yPt + box.heightPt / 2
14
+ };
15
+ }
16
+ function rotator(flipped, rotationDeg) {
17
+ const center = boxCenter(flipped);
18
+ const ccwDeg = -rotationDeg;
19
+ return (point) => rotatePointAboutCenter(point, center, ccwDeg);
20
+ }
21
+ function boxCorners(box) {
22
+ return [
23
+ {
24
+ x: box.xPt,
25
+ y: box.yPt
26
+ },
27
+ {
28
+ x: box.xPt + box.widthPt,
29
+ y: box.yPt
30
+ },
31
+ {
32
+ x: box.xPt + box.widthPt,
33
+ y: box.yPt + box.heightPt
34
+ },
35
+ {
36
+ x: box.xPt,
37
+ y: box.yPt + box.heightPt
38
+ }
39
+ ];
40
+ }
5
41
  function convertRectVector(vector, pageHeightPt, out) {
6
42
  const flipped = flipY(vector.frame, pageHeightPt);
43
+ if (!isRotated(vector.rotationDeg)) {
44
+ out.push({
45
+ kind: "rect",
46
+ xPt: flipped.xPt,
47
+ yPt: flipped.yPt,
48
+ widthPt: flipped.widthPt,
49
+ heightPt: flipped.heightPt,
50
+ fill: vector.fill,
51
+ stroke: vector.stroke,
52
+ sourcePath: vector.sourcePath
53
+ });
54
+ return;
55
+ }
56
+ const rotate = rotator(flipped, vector.rotationDeg);
57
+ const [start, ...rest] = boxCorners(flipped).map(rotate);
58
+ const subpath = {
59
+ startXPt: start.x,
60
+ startYPt: start.y,
61
+ closed: true,
62
+ segments: rest.map((corner) => ({
63
+ kind: "line",
64
+ xPt: corner.x,
65
+ yPt: corner.y
66
+ }))
67
+ };
7
68
  out.push({
8
- kind: "rect",
9
- xPt: flipped.xPt,
10
- yPt: flipped.yPt,
11
- widthPt: flipped.widthPt,
12
- heightPt: flipped.heightPt,
69
+ kind: "path",
70
+ subpaths: [subpath],
13
71
  fill: vector.fill,
14
72
  stroke: vector.stroke,
15
73
  sourcePath: vector.sourcePath
16
74
  });
17
75
  }
76
+ const CIRCLE_CUBIC_RATIO = 4 / 3 * (Math.SQRT2 - 1);
77
+ function ellipseCubicPoints(flipped) {
78
+ const cx = flipped.xPt + flipped.widthPt / 2;
79
+ const cy = flipped.yPt + flipped.heightPt / 2;
80
+ const rx = flipped.widthPt / 2;
81
+ const ry = flipped.heightPt / 2;
82
+ const kx = rx * CIRCLE_CUBIC_RATIO;
83
+ const ky = ry * CIRCLE_CUBIC_RATIO;
84
+ return {
85
+ start: {
86
+ x: cx + rx,
87
+ y: cy
88
+ },
89
+ arcs: [
90
+ {
91
+ c1: {
92
+ x: cx + rx,
93
+ y: cy + ky
94
+ },
95
+ c2: {
96
+ x: cx + kx,
97
+ y: cy + ry
98
+ },
99
+ to: {
100
+ x: cx,
101
+ y: cy + ry
102
+ }
103
+ },
104
+ {
105
+ c1: {
106
+ x: cx - kx,
107
+ y: cy + ry
108
+ },
109
+ c2: {
110
+ x: cx - rx,
111
+ y: cy + ky
112
+ },
113
+ to: {
114
+ x: cx - rx,
115
+ y: cy
116
+ }
117
+ },
118
+ {
119
+ c1: {
120
+ x: cx - rx,
121
+ y: cy - ky
122
+ },
123
+ c2: {
124
+ x: cx - kx,
125
+ y: cy - ry
126
+ },
127
+ to: {
128
+ x: cx,
129
+ y: cy - ry
130
+ }
131
+ },
132
+ {
133
+ c1: {
134
+ x: cx + kx,
135
+ y: cy - ry
136
+ },
137
+ c2: {
138
+ x: cx + rx,
139
+ y: cy - ky
140
+ },
141
+ to: {
142
+ x: cx + rx,
143
+ y: cy
144
+ }
145
+ }
146
+ ]
147
+ };
148
+ }
18
149
  function convertEllipseVector(vector, pageHeightPt, out) {
19
150
  const flipped = flipY(vector.frame, pageHeightPt);
151
+ if (!isRotated(vector.rotationDeg)) {
152
+ out.push({
153
+ kind: "ellipse",
154
+ xPt: flipped.xPt,
155
+ yPt: flipped.yPt,
156
+ widthPt: flipped.widthPt,
157
+ heightPt: flipped.heightPt,
158
+ fill: vector.fill,
159
+ stroke: vector.stroke,
160
+ sourcePath: vector.sourcePath
161
+ });
162
+ return;
163
+ }
164
+ const rotate = rotator(flipped, vector.rotationDeg);
165
+ const { start, arcs } = ellipseCubicPoints(flipped);
166
+ const rotatedStart = rotate(start);
167
+ const segments = arcs.map((arc) => {
168
+ const c1 = rotate(arc.c1);
169
+ const c2 = rotate(arc.c2);
170
+ const to = rotate(arc.to);
171
+ return {
172
+ kind: "cubic",
173
+ c1xPt: c1.x,
174
+ c1yPt: c1.y,
175
+ c2xPt: c2.x,
176
+ c2yPt: c2.y,
177
+ xPt: to.x,
178
+ yPt: to.y
179
+ };
180
+ });
20
181
  out.push({
21
- kind: "ellipse",
22
- xPt: flipped.xPt,
23
- yPt: flipped.yPt,
24
- widthPt: flipped.widthPt,
25
- heightPt: flipped.heightPt,
182
+ kind: "path",
183
+ subpaths: [{
184
+ startXPt: rotatedStart.x,
185
+ startYPt: rotatedStart.y,
186
+ closed: true,
187
+ segments
188
+ }],
26
189
  fill: vector.fill,
27
190
  stroke: vector.stroke,
28
191
  sourcePath: vector.sourcePath
@@ -47,24 +210,37 @@ function placePathPoint(frame, point, pageHeightPt) {
47
210
  };
48
211
  }
49
212
  function convertPathVector(vector, pageHeightPt, out) {
213
+ const rotate = isRotated(vector.rotationDeg) ? rotator(flipY(vector.frame, pageHeightPt), vector.rotationDeg) : void 0;
214
+ const place = (point) => {
215
+ const placed = placePathPoint(vector.frame, point, pageHeightPt);
216
+ if (rotate === void 0) return placed;
217
+ const rotated = rotate({
218
+ x: placed.xPt,
219
+ y: placed.yPt
220
+ });
221
+ return {
222
+ xPt: rotated.x,
223
+ yPt: rotated.y
224
+ };
225
+ };
50
226
  const subpaths = vector.subpaths.map((subpath) => {
51
- const start = placePathPoint(vector.frame, subpath.start, pageHeightPt);
227
+ const start = place(subpath.start);
52
228
  return {
53
229
  startXPt: start.xPt,
54
230
  startYPt: start.yPt,
55
231
  closed: subpath.closed,
56
232
  segments: subpath.segments.map((segment) => {
57
233
  if (segment.kind === "line") {
58
- const to = placePathPoint(vector.frame, segment.to, pageHeightPt);
234
+ const to = place(segment.to);
59
235
  return {
60
236
  kind: "line",
61
237
  xPt: to.xPt,
62
238
  yPt: to.yPt
63
239
  };
64
240
  }
65
- const control1 = placePathPoint(vector.frame, segment.control1, pageHeightPt);
66
- const control2 = placePathPoint(vector.frame, segment.control2, pageHeightPt);
67
- const to = placePathPoint(vector.frame, segment.to, pageHeightPt);
241
+ const control1 = place(segment.control1);
242
+ const control2 = place(segment.control2);
243
+ const to = place(segment.to);
68
244
  return {
69
245
  kind: "cubic",
70
246
  c1xPt: control1.xPt,
@@ -94,8 +270,8 @@ function convertVector(vector, pageHeightPt, out) {
94
270
  }
95
271
  function convertPage(page, measurer, images) {
96
272
  const items = [];
97
- for (const vector of page.vectors) convertVector(vector, page.size.heightPt, items);
98
- for (const shape of page.shapes) convertShape(shape, page.size.heightPt, measurer, images, items);
273
+ for (const entry of mergeByPaintOrder(page.vectors, page.shapes)) if (entry.kind === "vector") convertVector(entry.value, page.size.heightPt, items);
274
+ else convertShape(entry.value, page.size.heightPt, measurer, images, items);
99
275
  return {
100
276
  widthPt: page.size.widthPt,
101
277
  heightPt: page.size.heightPt,
@@ -1,5 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  const require_model_geometry = require("../model/geometry.cjs");
3
+ const require_model_formula = require("../model/formula.cjs");
3
4
  const require_mathml_layout = require("../mathml/layout.cjs");
4
5
  const require_layout_shared = require("./shared.cjs");
5
6
  let document_schema_js = require("document-schema.js");
@@ -33,14 +34,16 @@ function layoutParagraphFlow(paragraph, section, pages, state, contentLeftXDown,
33
34
  const fallbackRun = effectiveRuns[0];
34
35
  const paragraphLeftXDown = contentLeftXDown + (paragraph.indentLeftPt ?? 0);
35
36
  const paragraphWidthPt = Math.max(0, contentWidthPt - (paragraph.indentLeftPt ?? 0));
36
- (0, pdf_codec.wrapRunsToWidth)(effectiveRuns, measurer, paragraphWidthPt).forEach((line, lineIndex) => {
37
+ const lines = (0, pdf_codec.wrapRunsToWidth)(effectiveRuns, measurer, paragraphWidthPt);
38
+ lines.forEach((line, lineIndex) => {
37
39
  const lineHeightPt = require_layout_shared.lineNaturalHeightPt(line, measurer, fallbackRun) * (paragraph.lineSpacing ?? 1);
38
40
  ensureRoom(state, section, pages, lineHeightPt, contentBottomYDown);
39
41
  const baselineYDown = state.cursorYDown + line.ascentPt;
40
42
  const firstLineIndentPt = lineIndex === 0 ? paragraph.indentFirstLinePt ?? 0 : 0;
41
43
  const alignOffsetPt = require_layout_shared.alignmentOffsetPt(paragraph.alignment, paragraphWidthPt, line.widthPt);
42
- for (const fragment of line.fragments) {
43
- const xPt = paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt;
44
+ const justifyGapsPt = paragraph.alignment === "justify" && lineIndex < lines.length - 1 ? require_layout_shared.justifyLineGapsPt(line, paragraphWidthPt, measurer) : void 0;
45
+ line.fragments.forEach((fragment, fragmentIndex) => {
46
+ const xPt = paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt + (justifyGapsPt?.[fragmentIndex] ?? 0);
44
47
  const yPt = section.pageSize.heightPt - baselineYDown;
45
48
  const textItem = {
46
49
  kind: "text",
@@ -67,7 +70,7 @@ function layoutParagraphFlow(paragraph, section, pages, state, contentLeftXDown,
67
70
  };
68
71
  state.items.push(link);
69
72
  }
70
- }
73
+ });
71
74
  state.cursorYDown += lineHeightPt;
72
75
  });
73
76
  state.cursorYDown += paragraph.spacingAfterPt ?? 0;
@@ -78,21 +81,25 @@ function layoutParagraphInCell(paragraph, cellLeftXDown, cellWidthPt, startYDown
78
81
  const fallbackRun = effectiveRuns[0];
79
82
  const paragraphLeftXDown = cellLeftXDown + (paragraph.indentLeftPt ?? 0);
80
83
  const paragraphWidthPt = Math.max(0, cellWidthPt - (paragraph.indentLeftPt ?? 0));
81
- (0, pdf_codec.wrapRunsToWidth)(effectiveRuns, measurer, paragraphWidthPt).forEach((line, lineIndex) => {
84
+ const lines = (0, pdf_codec.wrapRunsToWidth)(effectiveRuns, measurer, paragraphWidthPt);
85
+ lines.forEach((line, lineIndex) => {
82
86
  const lineHeightPt = require_layout_shared.lineNaturalHeightPt(line, measurer, fallbackRun) * (paragraph.lineSpacing ?? 1);
83
87
  const baselineYDown = cursorYDown + line.ascentPt;
84
88
  const firstLineIndentPt = lineIndex === 0 ? paragraph.indentFirstLinePt ?? 0 : 0;
85
89
  const alignOffsetPt = require_layout_shared.alignmentOffsetPt(paragraph.alignment, paragraphWidthPt, line.widthPt);
86
- for (const fragment of line.fragments) out.push({
87
- kind: "text",
88
- text: fragment.text,
89
- xPt: paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt,
90
- yPt: pageHeightPt - baselineYDown,
91
- font: fragment.font,
92
- sizePt: fragment.sizePt,
93
- color: fragment.color,
94
- underline: fragment.underline,
95
- sourcePath: fragment.sourcePath
90
+ const justifyGapsPt = paragraph.alignment === "justify" && lineIndex < lines.length - 1 ? require_layout_shared.justifyLineGapsPt(line, paragraphWidthPt, measurer) : void 0;
91
+ line.fragments.forEach((fragment, fragmentIndex) => {
92
+ out.push({
93
+ kind: "text",
94
+ text: fragment.text,
95
+ xPt: paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt + (justifyGapsPt?.[fragmentIndex] ?? 0),
96
+ yPt: pageHeightPt - baselineYDown,
97
+ font: fragment.font,
98
+ sizePt: fragment.sizePt,
99
+ color: fragment.color,
100
+ underline: fragment.underline,
101
+ sourcePath: fragment.sourcePath
102
+ });
96
103
  });
97
104
  cursorYDown += lineHeightPt;
98
105
  });
@@ -109,13 +116,15 @@ function layoutTableFlow(table, section, pages, state, contentLeftXDown, content
109
116
  for (const cell of row.cells) {
110
117
  const span = cell.colSpan ?? 1;
111
118
  const cellWidthPt = require_layout_shared.sumColumnWidthsPt(table.columnWidthsPt, colIndex, span) * scale;
119
+ const cellFrameYDown = {
120
+ xPt: cellXDown,
121
+ yPt: state.cursorYDown,
122
+ widthPt: cellWidthPt,
123
+ heightPt: rowHeightPt
124
+ };
125
+ const cellSourcePath = cell.sourcePath ?? table.sourcePath;
112
126
  if (cell.background !== void 0) {
113
- const cellFrame = require_model_geometry.flipY({
114
- xPt: cellXDown,
115
- yPt: state.cursorYDown,
116
- widthPt: cellWidthPt,
117
- heightPt: rowHeightPt
118
- }, section.pageSize.heightPt);
127
+ const cellFrame = require_model_geometry.flipY(cellFrameYDown, section.pageSize.heightPt);
119
128
  state.items.push({
120
129
  kind: "rect",
121
130
  xPt: cellFrame.xPt,
@@ -123,9 +132,10 @@ function layoutTableFlow(table, section, pages, state, contentLeftXDown, content
123
132
  widthPt: cellFrame.widthPt,
124
133
  heightPt: cellFrame.heightPt,
125
134
  fill: cell.background,
126
- sourcePath: table.sourcePath
135
+ sourcePath: cellSourcePath
127
136
  });
128
137
  }
138
+ if (cell.borders !== void 0) require_layout_shared.pushCellBorderLines(cell.borders, cellFrameYDown, section.pageSize.heightPt, cellSourcePath, state.items);
129
139
  let cellCursorYDown = state.cursorYDown;
130
140
  for (const block of cell.blocks) if (block.kind === "paragraph") cellCursorYDown = layoutParagraphInCell(block, cellXDown, cellWidthPt, cellCursorYDown, section.pageSize.heightPt, measurer, state.items);
131
141
  cellXDown += cellWidthPt;
@@ -155,16 +165,23 @@ function layoutImageFlow(block, section, pages, state, contentLeftXDown, content
155
165
  state.items.push(imageItem);
156
166
  state.cursorYDown += block.heightPt;
157
167
  }
158
- function layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, options, formulas) {
159
- const embedded = block.sourcePath === void 0 ? void 0 : options.formulas?.get(block.sourcePath);
160
- if (embedded === void 0) {
161
- const fallbackParagraph = block.document.kind === "wordprocessing" ? block.document.sections[0]?.blocks[0] : void 0;
162
- if (fallbackParagraph?.kind === "paragraph") layoutParagraphFlow(fallbackParagraph, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
168
+ function layoutFormulaFallback(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer) {
169
+ const formula = require_model_formula.formulaOfBlock(block);
170
+ if (formula === void 0) return;
171
+ layoutParagraphFlow({
172
+ kind: "paragraph",
173
+ runs: [{ text: require_model_formula.formulaPlaceholderText(formula) }]
174
+ }, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
175
+ }
176
+ function layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, formulas) {
177
+ const formula = require_model_formula.formulaOfBlock(block);
178
+ if (formula === void 0 || formula.mathml.length === 0) {
179
+ layoutFormulaFallback(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
163
180
  return;
164
181
  }
165
182
  const sizePt = formulaSizePtFromFrame(block.frame.heightPt);
166
183
  const metrics = (0, pdf_codec.loadMathFont)().metricsAt(sizePt);
167
- const { box } = require_mathml_layout.layoutFormula(embedded.mathml, {
184
+ const { box } = require_mathml_layout.layoutFormula(formula.mathml, {
168
185
  metrics,
169
186
  sizePt,
170
187
  color: document_schema_js.COLOR_BLACK
@@ -194,7 +211,7 @@ function paginateSection(section, measurer, images, pages, options, formulas) {
194
211
  } else if (block.kind === "paragraph") layoutParagraphFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
195
212
  else if (block.kind === "table") layoutTableFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
196
213
  else if (block.kind === "image") layoutImageFlow(block, section, pages, state, contentLeftXDown, contentBottomYDown, images);
197
- else if (block.kind === "embeddedObject" && block.objectKind === "formula") layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, options, formulas);
214
+ else if (block.kind === "embeddedObject" && block.objectKind === "formula") layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, formulas);
198
215
  flushPage(state, section, pages);
199
216
  }
200
217
  function convertWordprocessingToLayout(doc, options) {
@@ -1,10 +1,8 @@
1
- import { EmbeddedFormula } from "../model/formula.cjs";
2
1
  import { ContentDocument, LayoutDocument } from "document-schema.js";
3
2
  import { PositionedFormula, TextMeasurer } from "pdf-codec";
4
3
  //#region src/layout/engine.d.ts
5
4
  interface EngineLayoutOptions {
6
5
  readonly measurer: TextMeasurer;
7
- readonly formulas?: ReadonlyMap<string, EmbeddedFormula>;
8
6
  }
9
7
  interface WordprocessingLayoutResult {
10
8
  readonly document: LayoutDocument;
@@ -1,10 +1,8 @@
1
- import { EmbeddedFormula } from "../model/formula.js";
2
1
  import { ContentDocument, LayoutDocument } from "document-schema.js";
3
2
  import { PositionedFormula, TextMeasurer } from "pdf-codec";
4
3
  //#region src/layout/engine.d.ts
5
4
  interface EngineLayoutOptions {
6
5
  readonly measurer: TextMeasurer;
7
- readonly formulas?: ReadonlyMap<string, EmbeddedFormula>;
8
6
  }
9
7
  interface WordprocessingLayoutResult {
10
8
  readonly document: LayoutDocument;
@@ -1,6 +1,7 @@
1
1
  import { flipY } from "../model/geometry.js";
2
+ import { formulaOfBlock, formulaPlaceholderText } from "../model/formula.js";
2
3
  import { layoutFormula } from "../mathml/layout.js";
3
- import { alignmentOffsetPt, effectiveStyledRuns, estimateRowHeightPt, lineNaturalHeightPt, registerImage, sumColumnWidthsPt } from "./shared.js";
4
+ import { alignmentOffsetPt, effectiveStyledRuns, estimateRowHeightPt, justifyLineGapsPt, lineNaturalHeightPt, pushCellBorderLines, registerImage, sumColumnWidthsPt } from "./shared.js";
4
5
  import { COLOR_BLACK, LAYOUT_FORMAT_VERSION } from "document-schema.js";
5
6
  import { loadMathFont, wrapRunsToWidth } from "pdf-codec";
6
7
  //#region src/layout/engine.ts
@@ -32,14 +33,16 @@ function layoutParagraphFlow(paragraph, section, pages, state, contentLeftXDown,
32
33
  const fallbackRun = effectiveRuns[0];
33
34
  const paragraphLeftXDown = contentLeftXDown + (paragraph.indentLeftPt ?? 0);
34
35
  const paragraphWidthPt = Math.max(0, contentWidthPt - (paragraph.indentLeftPt ?? 0));
35
- wrapRunsToWidth(effectiveRuns, measurer, paragraphWidthPt).forEach((line, lineIndex) => {
36
+ const lines = wrapRunsToWidth(effectiveRuns, measurer, paragraphWidthPt);
37
+ lines.forEach((line, lineIndex) => {
36
38
  const lineHeightPt = lineNaturalHeightPt(line, measurer, fallbackRun) * (paragraph.lineSpacing ?? 1);
37
39
  ensureRoom(state, section, pages, lineHeightPt, contentBottomYDown);
38
40
  const baselineYDown = state.cursorYDown + line.ascentPt;
39
41
  const firstLineIndentPt = lineIndex === 0 ? paragraph.indentFirstLinePt ?? 0 : 0;
40
42
  const alignOffsetPt = alignmentOffsetPt(paragraph.alignment, paragraphWidthPt, line.widthPt);
41
- for (const fragment of line.fragments) {
42
- const xPt = paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt;
43
+ const justifyGapsPt = paragraph.alignment === "justify" && lineIndex < lines.length - 1 ? justifyLineGapsPt(line, paragraphWidthPt, measurer) : void 0;
44
+ line.fragments.forEach((fragment, fragmentIndex) => {
45
+ const xPt = paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt + (justifyGapsPt?.[fragmentIndex] ?? 0);
43
46
  const yPt = section.pageSize.heightPt - baselineYDown;
44
47
  const textItem = {
45
48
  kind: "text",
@@ -66,7 +69,7 @@ function layoutParagraphFlow(paragraph, section, pages, state, contentLeftXDown,
66
69
  };
67
70
  state.items.push(link);
68
71
  }
69
- }
72
+ });
70
73
  state.cursorYDown += lineHeightPt;
71
74
  });
72
75
  state.cursorYDown += paragraph.spacingAfterPt ?? 0;
@@ -77,21 +80,25 @@ function layoutParagraphInCell(paragraph, cellLeftXDown, cellWidthPt, startYDown
77
80
  const fallbackRun = effectiveRuns[0];
78
81
  const paragraphLeftXDown = cellLeftXDown + (paragraph.indentLeftPt ?? 0);
79
82
  const paragraphWidthPt = Math.max(0, cellWidthPt - (paragraph.indentLeftPt ?? 0));
80
- wrapRunsToWidth(effectiveRuns, measurer, paragraphWidthPt).forEach((line, lineIndex) => {
83
+ const lines = wrapRunsToWidth(effectiveRuns, measurer, paragraphWidthPt);
84
+ lines.forEach((line, lineIndex) => {
81
85
  const lineHeightPt = lineNaturalHeightPt(line, measurer, fallbackRun) * (paragraph.lineSpacing ?? 1);
82
86
  const baselineYDown = cursorYDown + line.ascentPt;
83
87
  const firstLineIndentPt = lineIndex === 0 ? paragraph.indentFirstLinePt ?? 0 : 0;
84
88
  const alignOffsetPt = alignmentOffsetPt(paragraph.alignment, paragraphWidthPt, line.widthPt);
85
- for (const fragment of line.fragments) out.push({
86
- kind: "text",
87
- text: fragment.text,
88
- xPt: paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt,
89
- yPt: pageHeightPt - baselineYDown,
90
- font: fragment.font,
91
- sizePt: fragment.sizePt,
92
- color: fragment.color,
93
- underline: fragment.underline,
94
- sourcePath: fragment.sourcePath
89
+ const justifyGapsPt = paragraph.alignment === "justify" && lineIndex < lines.length - 1 ? justifyLineGapsPt(line, paragraphWidthPt, measurer) : void 0;
90
+ line.fragments.forEach((fragment, fragmentIndex) => {
91
+ out.push({
92
+ kind: "text",
93
+ text: fragment.text,
94
+ xPt: paragraphLeftXDown + firstLineIndentPt + alignOffsetPt + fragment.xOffsetPt + (justifyGapsPt?.[fragmentIndex] ?? 0),
95
+ yPt: pageHeightPt - baselineYDown,
96
+ font: fragment.font,
97
+ sizePt: fragment.sizePt,
98
+ color: fragment.color,
99
+ underline: fragment.underline,
100
+ sourcePath: fragment.sourcePath
101
+ });
95
102
  });
96
103
  cursorYDown += lineHeightPt;
97
104
  });
@@ -108,13 +115,15 @@ function layoutTableFlow(table, section, pages, state, contentLeftXDown, content
108
115
  for (const cell of row.cells) {
109
116
  const span = cell.colSpan ?? 1;
110
117
  const cellWidthPt = sumColumnWidthsPt(table.columnWidthsPt, colIndex, span) * scale;
118
+ const cellFrameYDown = {
119
+ xPt: cellXDown,
120
+ yPt: state.cursorYDown,
121
+ widthPt: cellWidthPt,
122
+ heightPt: rowHeightPt
123
+ };
124
+ const cellSourcePath = cell.sourcePath ?? table.sourcePath;
111
125
  if (cell.background !== void 0) {
112
- const cellFrame = flipY({
113
- xPt: cellXDown,
114
- yPt: state.cursorYDown,
115
- widthPt: cellWidthPt,
116
- heightPt: rowHeightPt
117
- }, section.pageSize.heightPt);
126
+ const cellFrame = flipY(cellFrameYDown, section.pageSize.heightPt);
118
127
  state.items.push({
119
128
  kind: "rect",
120
129
  xPt: cellFrame.xPt,
@@ -122,9 +131,10 @@ function layoutTableFlow(table, section, pages, state, contentLeftXDown, content
122
131
  widthPt: cellFrame.widthPt,
123
132
  heightPt: cellFrame.heightPt,
124
133
  fill: cell.background,
125
- sourcePath: table.sourcePath
134
+ sourcePath: cellSourcePath
126
135
  });
127
136
  }
137
+ if (cell.borders !== void 0) pushCellBorderLines(cell.borders, cellFrameYDown, section.pageSize.heightPt, cellSourcePath, state.items);
128
138
  let cellCursorYDown = state.cursorYDown;
129
139
  for (const block of cell.blocks) if (block.kind === "paragraph") cellCursorYDown = layoutParagraphInCell(block, cellXDown, cellWidthPt, cellCursorYDown, section.pageSize.heightPt, measurer, state.items);
130
140
  cellXDown += cellWidthPt;
@@ -154,16 +164,23 @@ function layoutImageFlow(block, section, pages, state, contentLeftXDown, content
154
164
  state.items.push(imageItem);
155
165
  state.cursorYDown += block.heightPt;
156
166
  }
157
- function layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, options, formulas) {
158
- const embedded = block.sourcePath === void 0 ? void 0 : options.formulas?.get(block.sourcePath);
159
- if (embedded === void 0) {
160
- const fallbackParagraph = block.document.kind === "wordprocessing" ? block.document.sections[0]?.blocks[0] : void 0;
161
- if (fallbackParagraph?.kind === "paragraph") layoutParagraphFlow(fallbackParagraph, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
167
+ function layoutFormulaFallback(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer) {
168
+ const formula = formulaOfBlock(block);
169
+ if (formula === void 0) return;
170
+ layoutParagraphFlow({
171
+ kind: "paragraph",
172
+ runs: [{ text: formulaPlaceholderText(formula) }]
173
+ }, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
174
+ }
175
+ function layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, formulas) {
176
+ const formula = formulaOfBlock(block);
177
+ if (formula === void 0 || formula.mathml.length === 0) {
178
+ layoutFormulaFallback(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
162
179
  return;
163
180
  }
164
181
  const sizePt = formulaSizePtFromFrame(block.frame.heightPt);
165
182
  const metrics = loadMathFont().metricsAt(sizePt);
166
- const { box } = layoutFormula(embedded.mathml, {
183
+ const { box } = layoutFormula(formula.mathml, {
167
184
  metrics,
168
185
  sizePt,
169
186
  color: COLOR_BLACK
@@ -193,7 +210,7 @@ function paginateSection(section, measurer, images, pages, options, formulas) {
193
210
  } else if (block.kind === "paragraph") layoutParagraphFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
194
211
  else if (block.kind === "table") layoutTableFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer);
195
212
  else if (block.kind === "image") layoutImageFlow(block, section, pages, state, contentLeftXDown, contentBottomYDown, images);
196
- else if (block.kind === "embeddedObject" && block.objectKind === "formula") layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, options, formulas);
213
+ else if (block.kind === "embeddedObject" && block.objectKind === "formula") layoutFormulaFlow(block, section, pages, state, contentLeftXDown, contentWidthPt, contentBottomYDown, measurer, formulas);
197
214
  flushPage(state, section, pages);
198
215
  }
199
216
  function convertWordprocessingToLayout(doc, options) {