abstract-document 11.1.2 → 11.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.
Files changed (121) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/lib/abstract-document-exporters/pdf/render-image.js +139 -12
  3. package/lib/abstract-document-exporters/pdf/render-image.js.map +1 -1
  4. package/package.json +3 -3
  5. package/src/abstract-document-exporters/__tests__/docx2/tmp/Group.docx +0 -0
  6. package/src/abstract-document-exporters/__tests__/docx2/tmp/Header and footer.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Header → footer.docx +0 -0
  7. package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed hyperlink and textrun.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed hyperlink → textrun.docx +0 -0
  8. package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed textrun and images.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Mixed textrun → images.docx +0 -0
  9. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple images.docx +0 -0
  10. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple tables.docx +0 -0
  11. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Center alignment.docx +0 -0
  12. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with End alignment.docx +0 -0
  13. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Start alignment that linebreaks.docx +0 -0
  14. package/src/abstract-document-exporters/__tests__/docx2/tmp/Multiple textrun with Start alignment.docx +0 -0
  15. package/src/abstract-document-exporters/__tests__/docx2/tmp/Page numbering.docx +0 -0
  16. package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table colSpan and rowSpan.docx and b/package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table colSpan → rowSpan.docx +0 -0
  17. package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table without rows.docx +0 -0
  18. package/src/abstract-document-exporters/__tests__/docx2/tmp/Simple table.docx +0 -0
  19. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single hyperlink.docx +0 -0
  20. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single image.docx +0 -0
  21. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Center alignment that linebreaks.docx +0 -0
  22. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Center alignment.docx +0 -0
  23. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with End alignment that linebreaks.docx +0 -0
  24. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with End alignment.docx +0 -0
  25. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Start alignment that linebreaks.docx +0 -0
  26. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun with Start alignment.docx +0 -0
  27. package/src/abstract-document-exporters/__tests__/docx2/tmp/Single textrun.docx +0 -0
  28. package/src/abstract-document-exporters/__tests__/docx2/tmp/hello with Arial font.docx +0 -0
  29. package/src/abstract-document-exporters/__tests__/docx2/tmp/hello.docx +0 -0
  30. package/src/abstract-document-exporters/__tests__/docx2/tmp/letter-dimensions.docx +0 -0
  31. package/src/abstract-document-exporters/__tests__/docx2/tmp/page-orientation-landscape.docx +0 -0
  32. package/src/abstract-document-exporters/__tests__/docx2/tmp/page-orientation-portrait.docx +0 -0
  33. package/src/abstract-document-exporters/__tests__/docx2/tmp/tocSeparator.docx +0 -0
  34. package/src/abstract-document-exporters/__tests__/docx2/tmp/world.docx +0 -0
  35. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position group.pdf +2 -2
  36. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position header and footer.pdf +2 -2
  37. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position paragraph.pdf +2 -2
  38. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position section in group.pdf +2 -2
  39. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position section in table.pdf +2 -2
  40. package/src/abstract-document-exporters/__tests__/pdf/tmp/Absolute position table.pdf +2 -2
  41. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group Paragraphs.pdf +2 -2
  42. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group Tables.pdf +2 -2
  43. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group no keeptogether.pdf +2 -2
  44. package/src/abstract-document-exporters/__tests__/pdf/tmp/Group too big for one page.pdf +2 -2
  45. package/src/abstract-document-exporters/__tests__/pdf/tmp/Header and footer.pdf +2 -2
  46. package/src/abstract-document-exporters/__tests__/pdf/tmp/Hyperlink internal linktarget.pdf +2 -2
  47. package/src/abstract-document-exporters/__tests__/pdf/tmp/Hyperlink internal.pdf +2 -2
  48. package/src/abstract-document-exporters/__tests__/pdf/tmp/Manual line break.pdf +2 -2
  49. package/src/abstract-document-exporters/__tests__/pdf/tmp/Manual line breaks empty lines.pdf +2 -2
  50. package/src/abstract-document-exporters/__tests__/pdf/tmp/Margins header body footer.pdf +2 -2
  51. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed hyperlink and textrun no underline.pdf +2 -2
  52. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed hyperlink and textrun with underline.pdf +2 -2
  53. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed hyperlink and textrun.pdf +2 -2
  54. package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed textrun and images.pdf and b/package/src/abstract-document-exporters/__tests__/pdf/tmp/Mixed textrun → images.pdf +0 -0
  55. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple images overflow.pdf +0 -0
  56. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple images.pdf +0 -0
  57. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Center alignment.pdf +2 -2
  58. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with End alignment.pdf +2 -2
  59. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Start alignment that linebreaks.pdf +2 -2
  60. package/src/abstract-document-exporters/__tests__/pdf/tmp/Multiple textrun with Start alignment.pdf +2 -2
  61. package/src/abstract-document-exporters/__tests__/pdf/tmp/Page numbering.pdf +2 -2
  62. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table row that doesnt fit page.pdf +2 -2
  63. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table single row.pdf +2 -2
  64. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with group.pdf +2 -2
  65. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with header 2.pdf +2 -2
  66. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with header.pdf +2 -2
  67. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin bottom.pdf +2 -2
  68. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin top single table.pdf +2 -2
  69. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with margin top.pdf +2 -2
  70. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with rowSpan multipages.pdf +2 -2
  71. package/src/abstract-document-exporters/__tests__/pdf/tmp/Pagebreak table with rowSpan.pdf +2 -2
  72. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table all auto.pdf +2 -2
  73. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table all fix.pdf +2 -2
  74. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table background.pdf +2 -2
  75. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table cell padding of image.pdf +0 -0
  76. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table cell padding of text.pdf +2 -2
  77. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table center alignment.pdf +2 -2
  78. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table colSpan and rowSpan.pdf +2 -2
  79. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table minimal row height.pdf +2 -2
  80. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table mix auto fix.pdf +2 -2
  81. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table padding.pdf +2 -2
  82. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table right alignment.pdf +2 -2
  83. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table row alignment.pdf +2 -2
  84. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table text alignment in cell with wrapping.pdf +2 -2
  85. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table text alignment in cell.pdf +2 -2
  86. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with default colored borders.pdf +2 -2
  87. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with header.pdf +2 -2
  88. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with multi colored borders.pdf +2 -2
  89. package/src/abstract-document-exporters/__tests__/pdf/tmp/Simple table with single colored borders.pdf +2 -2
  90. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single date.pdf +14 -14
  91. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single figure.pdf +15 -13
  92. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink centered.pdf +2 -2
  93. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink no underline.pdf +2 -2
  94. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink right aligned.pdf +2 -2
  95. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single hyperlink.pdf +2 -2
  96. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image from URL.pdf +0 -0
  97. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color hex.pdf +2 -2
  98. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color name.pdf +2 -2
  99. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg color url.pdf +2 -2
  100. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image svg dasharray.pdf +2 -2
  101. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single image.pdf +0 -0
  102. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun baseline alphabetic.pdf +2 -2
  103. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun baseline bottom.pdf +2 -2
  104. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun baseline hanging.pdf +2 -2
  105. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun baseline middle.pdf +2 -2
  106. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun baseline top.pdf +2 -2
  107. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Center alignment that linebreaks.pdf +2 -2
  108. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Center alignment.pdf +2 -2
  109. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with End alignment that linebreaks.pdf +2 -2
  110. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with End alignment.pdf +2 -2
  111. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Justify alignment.pdf +2 -2
  112. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Start alignment that linebreaks.pdf +2 -2
  113. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with Start alignment.pdf +2 -2
  114. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun with super and subscripts.pdf +2 -2
  115. package/src/abstract-document-exporters/__tests__/pdf/tmp/Single textrun.pdf +2 -2
  116. package/src/abstract-document-exporters/__tests__/pdf/tmp/Table of content separator.pdf +2 -2
  117. package/src/abstract-document-exporters/__tests__/pdf/tmp/hello with roman font.pdf +2 -2
  118. package/src/abstract-document-exporters/__tests__/pdf/tmp/hello.pdf +2 -2
  119. package/src/abstract-document-exporters/__tests__/pdf/tmp/pagebreak.pdf +2 -2
  120. package/src/abstract-document-exporters/__tests__/pdf/tmp/world.pdf +2 -2
  121. package/src/abstract-document-exporters/pdf/render-image.ts +142 -12
@@ -122,8 +122,9 @@ function abstractComponentToPdf(
122
122
  .lineWidth(component.strokeThickness)
123
123
  .moveTo(component.start.x, component.start.y)
124
124
  .lineTo(component.end.x, component.end.y)
125
- .strokeOpacity(colorToOpacity(component.strokeColor))
126
- .stroke(colorToRgb(component.strokeColor));
125
+ .strokeOpacity(colorToOpacity(component.strokeColor));
126
+ applyStrokeDashStyle(pdf, component.strokeDashStyle);
127
+ pdf.stroke(colorToRgb(component.strokeColor));
127
128
  break;
128
129
  case "polyline":
129
130
  for (let i = 0; i < component.points.length; ++i) {
@@ -134,10 +135,9 @@ function abstractComponentToPdf(
134
135
  pdf.lineTo(p.x, p.y);
135
136
  }
136
137
  }
137
- pdf
138
- .lineWidth(component.strokeThickness)
139
- .strokeOpacity(colorToOpacity(component.strokeColor))
140
- .stroke(colorToRgb(component.strokeColor));
138
+ pdf.lineWidth(component.strokeThickness).strokeOpacity(colorToOpacity(component.strokeColor));
139
+ applyStrokeDashStyle(pdf, component.strokeDashStyle);
140
+ pdf.stroke(colorToRgb(component.strokeColor));
141
141
  break;
142
142
  case "text":
143
143
  const font = getFontName(component.fontFamily, component.fontWeight, component.italic);
@@ -180,16 +180,18 @@ function abstractComponentToPdf(
180
180
  .lineWidth(component.strokeThickness)
181
181
  .ellipse(centerX, centerY, width * 0.5, height * 0.5)
182
182
  .strokeOpacity(colorToOpacity(component.strokeColor))
183
- .fillOpacity(colorToOpacity(component.fillColor))
184
- .fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
183
+ .fillOpacity(colorToOpacity(component.fillColor));
184
+ applyStrokeDashStyle(pdf, component.strokeDashStyle);
185
+ pdf.fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
185
186
  break;
186
187
  case "polygon":
187
188
  pdf
188
189
  .lineWidth(component.strokeThickness)
189
190
  .strokeOpacity(colorToOpacity(component.strokeColor))
190
191
  .fillOpacity(colorToOpacity(component.fillColor))
191
- .polygon(...component.points.map((p) => [p.x, p.y]))
192
- .fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
192
+ .polygon(...component.points.map((p) => [p.x, p.y]));
193
+ applyStrokeDashStyle(pdf, component.strokeDashStyle);
194
+ pdf.fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
193
195
  break;
194
196
  case "rectangle":
195
197
  const rWidth = component.bottomRight.x - component.topLeft.x;
@@ -198,8 +200,9 @@ function abstractComponentToPdf(
198
200
  .lineWidth(component.strokeThickness)
199
201
  .strokeOpacity(colorToOpacity(component.strokeColor))
200
202
  .fillOpacity(colorToOpacity(component.fillColor))
201
- .rect(component.topLeft.x, component.topLeft.y, rWidth, rHeight)
202
- .fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
203
+ .rect(component.topLeft.x, component.topLeft.y, rWidth, rHeight);
204
+ applyStrokeDashStyle(pdf, component.strokeDashStyle);
205
+ pdf.fillAndStroke(colorToRgb(component.fillColor), colorToRgb(component.strokeColor));
203
206
  break;
204
207
  default:
205
208
  break;
@@ -213,3 +216,130 @@ function colorToOpacity(color: AbstractImage.Color): number {
213
216
  function colorToRgb(color: AbstractImage.Color): [number, number, number] {
214
217
  return [color.r, color.g, color.b];
215
218
  }
219
+
220
+ function applyStrokeDashStyle(pdf: PDFKit.PDFDocument, dashStyle: AbstractImage.DashStyle): void {
221
+ if (dashStyle.dashes.length === 0) {
222
+ pdf.undash();
223
+ return;
224
+ }
225
+
226
+ let dashes = [...dashStyle.dashes];
227
+ let phase = dashStyle.offset;
228
+
229
+ // Anytime there's a 0 that isn't the first or last element of the array,
230
+ // we can remove it by combining the previous or next value. If it's a
231
+ // dash, then it's a zero-length dash between two spaces, so the dash can
232
+ // be eliminated and spaces combined by summing them, replacing all three
233
+ // values with the sum of the two spaces. If the 0 value is a space, then
234
+ // it's a zero-length space between two dashes, and the dashes can be
235
+ // similarly combined. So first we run that logic iteratively to remove
236
+ // all the 0s from the dash array that aren't the first or last element.
237
+ // Note that because we replace 3 values with one value, this doesn't
238
+ // change the even-ness of the length of dashArray.
239
+ for (;;) {
240
+ const index = dashes.slice(1, -1).indexOf(0);
241
+ if (index === -1) {
242
+ break;
243
+ }
244
+ const actualIndex = index + 1;
245
+ const replacementValue = dashes[actualIndex - 1] + dashes[actualIndex + 1];
246
+ dashes = dashes
247
+ .slice(0, actualIndex - 1)
248
+ .concat([replacementValue])
249
+ .concat(dashes.slice(actualIndex + 2));
250
+ }
251
+
252
+ // The stroke array only having two elements (a dash value and space
253
+ // value) is a special case.
254
+ if (dashes.length === 1) {
255
+ dashes = [dashes[0], dashes[0]];
256
+ if (dashes[0] === 0) {
257
+ pdf.strokeOpacity(0);
258
+ }
259
+ } else if (dashes.length === 2) {
260
+ if (dashes[0] === 0) {
261
+ // Regardless of the space value, the dash length is zero, so we're
262
+ // not actually drawing a stroke. We can't describe that in a
263
+ // doc.dash() call in a way that PDFKit will accept, so we set the
264
+ // stroke opacity to zero as our best approximation.
265
+ pdf.strokeOpacity(0);
266
+ return;
267
+ } else if (dashes[1] === 0) {
268
+ // Regardless of the dash value, the space value is zero, meaning
269
+ // we're actually drawing a solid stroke, not a dashed one. We can
270
+ // make this happen by just emptying out the dash array.
271
+ dashes = [];
272
+ }
273
+ } else {
274
+ if (dashes[0] === 0) {
275
+ // The first dash is zero-length. We fix this by combining the first
276
+ // space (just after the first dash) with the last space and updating
277
+ // the dash offset accordingly. For example, if we had
278
+ //
279
+ // [ 0 4 3 2 5 1 ] (dash offset 0)
280
+ //
281
+ // ␣␣␣␣---␣␣-----␣
282
+ // ⎸
283
+ //
284
+ // we'd end up with
285
+ //
286
+ // [ 3 2 5 5 ] (dash offset -4)
287
+ //
288
+ // ---␣␣-----␣␣␣␣␣
289
+ // ⎸
290
+ //
291
+ // Another example where the dash array also ends with a 0:
292
+ //
293
+ // [ 0 4 3 2 5 0 ] (dash offset 0)
294
+ //
295
+ // ␣␣␣␣---␣␣-----
296
+ // ⎸
297
+ //
298
+ // we'd end up with
299
+ //
300
+ // [ 3 2 5 4 ] (dash offset -4)
301
+ //
302
+ // ---␣␣-----␣␣␣␣
303
+ // ⎸
304
+ phase -= dashes[1];
305
+ dashes[dashes.length - 1] += dashes[1];
306
+ dashes = dashes.slice(2);
307
+ }
308
+ if (dashes[dashes.length - 1] === 0) {
309
+ // The last space is zero-length. We fix this by combining the last dash
310
+ // (just before the last space) with the first dash and updating the
311
+ // dash offset accordingly. For example, if we had
312
+ //
313
+ // [ 1 4 3 2 5 0 ] (dash offset 0)
314
+ //
315
+ // -␣␣␣␣---␣␣-----
316
+ // ⎸
317
+ //
318
+ // we'd end up with
319
+ //
320
+ // [ 6 4 3 2 ] (dash offset 5)
321
+ //
322
+ // ------␣␣␣␣---␣␣
323
+ // ⎸
324
+ //
325
+ phase += dashes[dashes.length - 2];
326
+ dashes[0] += dashes[dashes.length - 2];
327
+ dashes = dashes.slice(0, -2);
328
+ }
329
+ }
330
+
331
+ // Ensure the dash offset is non-negative (because of crbug.com/660850).
332
+ // First compute the total length of the dash array so we can add it to
333
+ // dash offset until dash offset is non-negative.
334
+ let length = 0;
335
+ for (const dash of dashes) {
336
+ length += dash;
337
+ }
338
+ if (length > 0) {
339
+ while (phase < 0) {
340
+ phase += length;
341
+ }
342
+ }
343
+
344
+ (pdf as { dash: (length: number | ReadonlyArray<number>, option: any) => any }).dash(dashes, { phase: phase });
345
+ }