@pdfme/pdf-lib 6.0.3-dev.0 → 6.0.4-dev.1

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 (449) hide show
  1. package/{src/api/Embeddable.ts → dist/api/Embeddable.d.ts} +1 -1
  2. package/dist/api/PDFDocument.d.ts +778 -0
  3. package/dist/api/PDFDocumentOptions.d.ts +38 -0
  4. package/dist/api/PDFEmbeddedFile.d.ts +38 -0
  5. package/dist/api/PDFEmbeddedPage.d.ts +73 -0
  6. package/dist/api/PDFFont.d.ts +94 -0
  7. package/dist/api/PDFImage.d.ts +95 -0
  8. package/dist/api/PDFJavaScript.d.ts +38 -0
  9. package/dist/api/PDFPage.d.ts +910 -0
  10. package/dist/api/PDFPageOptions.d.ts +172 -0
  11. package/dist/api/StandardFonts.d.ts +16 -0
  12. package/dist/api/colors.d.ts +34 -0
  13. package/dist/api/errors.d.ts +42 -0
  14. package/dist/api/form/PDFButton.d.ts +137 -0
  15. package/dist/api/form/PDFCheckBox.d.ts +142 -0
  16. package/dist/api/form/PDFDropdown.d.ts +402 -0
  17. package/dist/api/form/PDFField.d.ts +203 -0
  18. package/dist/api/form/PDFForm.d.ts +411 -0
  19. package/dist/api/form/PDFOptionList.d.ts +335 -0
  20. package/dist/api/form/PDFRadioGroup.d.ts +252 -0
  21. package/dist/api/form/PDFSignature.d.ts +29 -0
  22. package/dist/api/form/PDFTextField.d.ts +537 -0
  23. package/dist/api/form/appearances.d.ts +41 -0
  24. package/dist/api/image/alignment.d.ts +5 -0
  25. package/dist/api/objects.d.ts +4 -0
  26. package/dist/api/operations.d.ts +226 -0
  27. package/dist/api/operators.d.ts +90 -0
  28. package/dist/api/rotations.d.ts +38 -0
  29. package/dist/api/sizes.d.ts +52 -0
  30. package/dist/api/svg.d.ts +70 -0
  31. package/dist/api/svgPath.d.ts +2 -0
  32. package/dist/api/text/alignment.d.ts +5 -0
  33. package/dist/api/text/layout.d.ts +54 -0
  34. package/dist/core/PDFContext.d.ts +84 -0
  35. package/dist/core/PDFObjectCopier.d.ts +37 -0
  36. package/dist/core/acroform/PDFAcroButton.d.ts +21 -0
  37. package/dist/core/acroform/PDFAcroCheckBox.d.ts +13 -0
  38. package/dist/core/acroform/PDFAcroChoice.d.ts +20 -0
  39. package/dist/core/acroform/PDFAcroComboBox.d.ts +9 -0
  40. package/dist/core/acroform/PDFAcroField.d.ts +36 -0
  41. package/dist/core/acroform/PDFAcroForm.d.ts +20 -0
  42. package/dist/core/acroform/PDFAcroListBox.d.ts +9 -0
  43. package/dist/core/acroform/PDFAcroNonTerminal.d.ts +13 -0
  44. package/dist/core/acroform/PDFAcroPushButton.d.ts +9 -0
  45. package/dist/core/acroform/PDFAcroRadioButton.d.ts +13 -0
  46. package/{src/core/acroform/PDFAcroSignature.ts → dist/core/acroform/PDFAcroSignature.d.ts} +2 -4
  47. package/dist/core/acroform/PDFAcroTerminal.d.ts +16 -0
  48. package/dist/core/acroform/PDFAcroText.d.ts +22 -0
  49. package/dist/core/acroform/flags.d.ts +141 -0
  50. package/dist/core/acroform/utils.d.ts +6 -0
  51. package/dist/core/annotation/AppearanceCharacteristics.d.ts +33 -0
  52. package/dist/core/annotation/BorderStyle.d.ts +11 -0
  53. package/dist/core/annotation/PDFAnnotation.d.ts +51 -0
  54. package/dist/core/annotation/PDFWidgetAnnotation.d.ts +26 -0
  55. package/dist/core/annotation/flags.d.ts +79 -0
  56. package/dist/core/crypto.d.ts +95 -0
  57. package/dist/core/document/PDFCrossRefSection.d.ts +27 -0
  58. package/dist/core/document/PDFHeader.d.ts +10 -0
  59. package/dist/core/document/PDFTrailer.d.ts +9 -0
  60. package/dist/core/document/PDFTrailerDict.d.ts +10 -0
  61. package/dist/core/embedders/CMap.d.ts +3 -0
  62. package/dist/core/embedders/CustomFontEmbedder.d.ts +44 -0
  63. package/dist/core/embedders/CustomFontSubsetEmbedder.d.ts +20 -0
  64. package/dist/core/embedders/FileEmbedder.d.ts +33 -0
  65. package/dist/core/embedders/FontFlags.d.ts +13 -0
  66. package/dist/core/embedders/JavaScriptEmbedder.d.ts +10 -0
  67. package/dist/core/embedders/JpegEmbedder.d.ts +23 -0
  68. package/dist/core/embedders/PDFPageEmbedder.d.ts +37 -0
  69. package/dist/core/embedders/PngEmbedder.d.ts +19 -0
  70. package/dist/core/embedders/StandardFontEmbedder.d.ts +36 -0
  71. package/dist/core/errors.d.ts +94 -0
  72. package/{src/core/index.ts → dist/core/index.d.ts} +1 -17
  73. package/dist/core/interactive/ViewerPreferences.d.ts +347 -0
  74. package/dist/core/objects/PDFArray.d.ts +63 -0
  75. package/dist/core/objects/PDFBool.d.ts +13 -0
  76. package/dist/core/objects/PDFDict.d.ts +62 -0
  77. package/dist/core/objects/PDFHexString.d.ts +16 -0
  78. package/dist/core/objects/PDFInvalidObject.d.ts +11 -0
  79. package/dist/core/objects/PDFName.d.ts +41 -0
  80. package/dist/core/objects/PDFNull.d.ts +10 -0
  81. package/dist/core/objects/PDFNumber.d.ts +15 -0
  82. package/dist/core/objects/PDFObject.d.ts +8 -0
  83. package/dist/core/objects/PDFRawStream.d.ts +16 -0
  84. package/dist/core/objects/PDFRef.d.ts +13 -0
  85. package/dist/core/objects/PDFStream.d.ts +16 -0
  86. package/dist/core/objects/PDFString.d.ts +16 -0
  87. package/dist/core/operators/PDFOperator.d.ts +19 -0
  88. package/dist/core/operators/PDFOperatorNames.d.ts +76 -0
  89. package/dist/core/parser/BaseParser.d.ts +14 -0
  90. package/dist/core/parser/ByteStream.d.ts +26 -0
  91. package/dist/core/parser/PDFObjectParser.d.ts +31 -0
  92. package/dist/core/parser/PDFObjectStreamParser.d.ts +13 -0
  93. package/dist/core/parser/PDFParser.d.ts +46 -0
  94. package/dist/core/parser/PDFXRefStreamParser.d.ts +21 -0
  95. package/dist/core/streams/Ascii85Stream.d.ts +9 -0
  96. package/dist/core/streams/AsciiHexStream.d.ts +9 -0
  97. package/dist/core/streams/DecodeStream.d.ts +26 -0
  98. package/dist/core/streams/DecryptStream.d.ts +12 -0
  99. package/dist/core/streams/FlateStream.d.ts +13 -0
  100. package/dist/core/streams/LZWStream.d.ts +12 -0
  101. package/dist/core/streams/RunLengthStream.d.ts +8 -0
  102. package/dist/core/streams/Stream.d.ts +34 -0
  103. package/dist/core/streams/decode.d.ts +3 -0
  104. package/dist/core/structures/PDFCatalog.d.ts +27 -0
  105. package/dist/core/structures/PDFContentStream.d.ts +15 -0
  106. package/dist/core/structures/PDFCrossRefStream.d.ts +53 -0
  107. package/dist/core/structures/PDFFlateStream.d.ts +13 -0
  108. package/dist/core/structures/PDFObjectStream.d.ts +20 -0
  109. package/dist/core/structures/PDFPageLeaf.d.ts +54 -0
  110. package/dist/core/structures/PDFPageTree.d.ts +41 -0
  111. package/dist/core/syntax/CharCodes.d.ts +61 -0
  112. package/dist/core/syntax/Delimiters.d.ts +1 -0
  113. package/dist/core/syntax/Irregular.d.ts +1 -0
  114. package/dist/core/syntax/Keywords.d.ts +22 -0
  115. package/dist/core/syntax/Numeric.d.ts +3 -0
  116. package/dist/core/syntax/Whitespace.d.ts +1 -0
  117. package/dist/core/writers/PDFStreamWriter.d.ts +19 -0
  118. package/dist/core/writers/PDFWriter.d.ts +29 -0
  119. package/dist/index.js +19124 -0
  120. package/dist/index.js.map +1 -0
  121. package/dist/types/fontkit.d.ts +597 -0
  122. package/{src/types/index.ts → dist/types/index.d.ts} +8 -13
  123. package/{src/types/matrix.ts → dist/types/matrix.d.ts} +1 -2
  124. package/dist/utils/Cache.d.ts +10 -0
  125. package/dist/utils/arrays.d.ts +16 -0
  126. package/dist/utils/async.d.ts +5 -0
  127. package/dist/utils/base64.d.ts +10 -0
  128. package/dist/utils/elements/Arc.d.ts +22 -0
  129. package/dist/utils/elements/Circle.d.ts +14 -0
  130. package/dist/utils/elements/Ellipse.d.ts +20 -0
  131. package/dist/utils/elements/GraphElement.d.ts +6 -0
  132. package/dist/utils/elements/Line.d.ts +23 -0
  133. package/dist/utils/elements/Plot.d.ts +11 -0
  134. package/dist/utils/elements/Point.d.ts +18 -0
  135. package/dist/utils/elements/Rectangle.d.ts +21 -0
  136. package/dist/utils/elements/Segment.d.ts +19 -0
  137. package/dist/utils/errors.d.ts +1 -0
  138. package/dist/utils/intersections.d.ts +9 -0
  139. package/dist/utils/maths.d.ts +45 -0
  140. package/dist/utils/numbers.d.ts +23 -0
  141. package/dist/utils/objects.d.ts +15 -0
  142. package/dist/utils/pdfDocEncoding.d.ts +7 -0
  143. package/dist/utils/png.d.ts +17 -0
  144. package/dist/utils/rng.d.ts +13 -0
  145. package/dist/utils/strings.d.ts +25 -0
  146. package/{src/utils/unicode.ts → dist/utils/unicode.d.ts} +8 -195
  147. package/dist/utils/validators.d.ts +26 -0
  148. package/package.json +6 -1
  149. package/__tests__/api/PDFDocument.spec.ts +0 -576
  150. package/__tests__/api/PDFImage.spec.ts +0 -60
  151. package/__tests__/api/PDFPage.spec.ts +0 -151
  152. package/__tests__/api/form/PDFCheckBox.spec.ts +0 -71
  153. package/__tests__/api/form/PDFDropdown.spec.ts +0 -118
  154. package/__tests__/api/form/PDFForm.spec.ts +0 -370
  155. package/__tests__/api/form/PDFOptionList.spec.ts +0 -104
  156. package/__tests__/api/form/PDFRadioGroup.spec.ts +0 -183
  157. package/__tests__/api/form/PDFTextField.spec.ts +0 -148
  158. package/__tests__/api/svgPath.spec.ts +0 -153
  159. package/__tests__/api/text/layout.spec.ts +0 -166
  160. package/__tests__/core/PDFContext.spec.ts +0 -194
  161. package/__tests__/core/PDFObjectCopier.spec.ts +0 -421
  162. package/__tests__/core/acroform/PDFAcroField.spec.ts +0 -98
  163. package/__tests__/core/acroform/PDFAcroTerminal.spec.ts +0 -39
  164. package/__tests__/core/annotation/PDFWidgetAnnotation.spec.ts +0 -40
  165. package/__tests__/core/document/PDFCrossRefSection.spec.ts +0 -88
  166. package/__tests__/core/document/PDFHeader.spec.ts +0 -22
  167. package/__tests__/core/document/PDFTrailer.spec.ts +0 -28
  168. package/__tests__/core/document/PDFTrailerDict.spec.ts +0 -24
  169. package/__tests__/core/embedders/CMap.spec.ts +0 -47
  170. package/__tests__/core/embedders/CustomFontEmbedder.spec.ts +0 -96
  171. package/__tests__/core/embedders/CustomFontSubsetEmbedder.spec.ts +0 -62
  172. package/__tests__/core/embedders/FileEmbedder.spec.ts +0 -54
  173. package/__tests__/core/embedders/JpegEmbedder.spec.ts +0 -67
  174. package/__tests__/core/embedders/PDFPageEmbedder.spec.ts +0 -65
  175. package/__tests__/core/embedders/PngEmbedder.spec.ts +0 -87
  176. package/__tests__/core/embedders/StandardFontEmbedder.spec.ts +0 -79
  177. package/__tests__/core/embedders/data/SourceHanSerifJP-Regular.otf.cmap +0 -16096
  178. package/__tests__/core/embedders/data/Ubuntu-R.ttf.cmap +0 -1211
  179. package/__tests__/core/objects/PDFArray.spec.ts +0 -134
  180. package/__tests__/core/objects/PDFBool.spec.ts +0 -42
  181. package/__tests__/core/objects/PDFDict.spec.ts +0 -195
  182. package/__tests__/core/objects/PDFHexString.spec.ts +0 -168
  183. package/__tests__/core/objects/PDFInvalidObject.spec.ts +0 -32
  184. package/__tests__/core/objects/PDFName.spec.ts +0 -122
  185. package/__tests__/core/objects/PDFNull.spec.ts +0 -30
  186. package/__tests__/core/objects/PDFNumber.spec.ts +0 -50
  187. package/__tests__/core/objects/PDFObject.spec.ts +0 -29
  188. package/__tests__/core/objects/PDFRawStream.spec.ts +0 -45
  189. package/__tests__/core/objects/PDFRef.spec.ts +0 -60
  190. package/__tests__/core/objects/PDFString.spec.ts +0 -237
  191. package/__tests__/core/operators/PDFOperator.spec.ts +0 -77
  192. package/__tests__/core/parser/PDFObjectParser.spec.ts +0 -730
  193. package/__tests__/core/parser/PDFObjectStreamParser.spec.ts +0 -158
  194. package/__tests__/core/parser/PDFParser.spec.ts +0 -398
  195. package/__tests__/core/parser/PDFXRefStreamParser.spec.ts +0 -187
  196. package/__tests__/core/parser/data/object-stream-invalid +0 -2
  197. package/__tests__/core/parser/data/object-stream1 +0 -16
  198. package/__tests__/core/parser/data/object-stream2 +0 -10
  199. package/__tests__/core/parser/data/object-stream3 +0 -0
  200. package/__tests__/core/parser/data/object-stream4 +0 -0
  201. package/__tests__/core/parser/data/xref-stream1 +0 -0
  202. package/__tests__/core/parser/data/xref-stream2 +0 -0
  203. package/__tests__/core/parser/data/xref-stream3 +0 -0
  204. package/__tests__/core/parser/data/xref-stream4 +0 -0
  205. package/__tests__/core/streams/Ascii85Stream.spec.ts +0 -20
  206. package/__tests__/core/streams/AsciiHexStream.spec.ts +0 -20
  207. package/__tests__/core/streams/FlateStream.spec.ts +0 -20
  208. package/__tests__/core/streams/LZWStream.spec.ts +0 -20
  209. package/__tests__/core/streams/RunLengthStream.spec.ts +0 -20
  210. package/__tests__/core/streams/Stream.spec.ts +0 -128
  211. package/__tests__/core/streams/data/ascii85/1.decoded +0 -0
  212. package/__tests__/core/streams/data/ascii85/1.encoded +0 -43
  213. package/__tests__/core/streams/data/asciihex/1.decoded +0 -1
  214. package/__tests__/core/streams/data/asciihex/1.encoded +0 -1
  215. package/__tests__/core/streams/data/asciihex/2.decoded +0 -7
  216. package/__tests__/core/streams/data/asciihex/2.encoded +0 -1
  217. package/__tests__/core/streams/data/flate/1.corrupt.encoded +0 -1
  218. package/__tests__/core/streams/data/flate/1.decoded +0 -0
  219. package/__tests__/core/streams/data/flate/1.encoded +0 -2
  220. package/__tests__/core/streams/data/flate/2.decoded +0 -33
  221. package/__tests__/core/streams/data/flate/2.encoded +0 -0
  222. package/__tests__/core/streams/data/flate/3.decoded +0 -15
  223. package/__tests__/core/streams/data/flate/3.encoded +0 -0
  224. package/__tests__/core/streams/data/flate/4.decoded +0 -16
  225. package/__tests__/core/streams/data/flate/4.encoded +0 -0
  226. package/__tests__/core/streams/data/flate/5.decoded +0 -0
  227. package/__tests__/core/streams/data/flate/5.encoded +0 -0
  228. package/__tests__/core/streams/data/flate/6.decoded +0 -17
  229. package/__tests__/core/streams/data/flate/6.encoded +0 -0
  230. package/__tests__/core/streams/data/flate/7.decoded +0 -20
  231. package/__tests__/core/streams/data/flate/7.encoded +0 -1
  232. package/__tests__/core/streams/data/lzw/1.decoded +0 -0
  233. package/__tests__/core/streams/data/lzw/1.encoded +0 -0
  234. package/__tests__/core/streams/data/lzw/2.decoded +0 -0
  235. package/__tests__/core/streams/data/lzw/2.encoded +0 -0
  236. package/__tests__/core/streams/data/lzw/3.decoded +0 -14
  237. package/__tests__/core/streams/data/lzw/3.encoded +0 -2
  238. package/__tests__/core/streams/data/lzw/4.decoded +0 -14
  239. package/__tests__/core/streams/data/lzw/4.encoded +0 -2
  240. package/__tests__/core/streams/data/runlength/1.decoded +0 -1
  241. package/__tests__/core/streams/data/runlength/1.encoded +0 -1
  242. package/__tests__/core/streams/data/runlength/2.decoded +0 -1
  243. package/__tests__/core/streams/data/runlength/2.encoded +0 -1
  244. package/__tests__/core/streams/data/runlength/3.decoded +0 -0
  245. package/__tests__/core/streams/data/runlength/3.encoded +0 -0
  246. package/__tests__/core/streams/data/runlength/4.decoded +0 -1
  247. package/__tests__/core/streams/data/runlength/4.encoded +0 -1
  248. package/__tests__/core/streams/data/runlength/5.decoded +0 -1
  249. package/__tests__/core/streams/data/runlength/5.encoded +0 -1
  250. package/__tests__/core/streams/data/runlength/empty.encoded +0 -1
  251. package/__tests__/core/structures/PDFCatalog.spec.ts +0 -108
  252. package/__tests__/core/structures/PDFContentStream.spec.ts +0 -117
  253. package/__tests__/core/structures/PDFCrossRefStream.spec.ts +0 -145
  254. package/__tests__/core/structures/PDFObjectStream.spec.ts +0 -138
  255. package/__tests__/core/structures/PDFPageLeaf.spec.ts +0 -355
  256. package/__tests__/core/structures/PDFPageTree.spec.ts +0 -592
  257. package/__tests__/core/writers/PDFStreamWriter.spec.ts +0 -71
  258. package/__tests__/core/writers/PDFWriter.spec.ts +0 -144
  259. package/__tests__/core/writers/data/stream-writer-1.pdf +0 -0
  260. package/__tests__/index.test.ts +0 -12
  261. package/__tests__/utils/base64.spec.ts +0 -61
  262. package/__tests__/utils/data/simple.pdf +0 -201
  263. package/__tests__/utils/data/simple.pdf.base64 +0 -1
  264. package/__tests__/utils/pdfDocEncoding.spec.ts +0 -82
  265. package/__tests__/utils/png.spec.ts +0 -53
  266. package/__tests__/utils/rng.spec.ts +0 -14
  267. package/__tests__/utils/strings.spec.ts +0 -98
  268. package/__tests__/utils/unicode.spec.ts +0 -337
  269. package/assets/fonts/source_hans_jp/SourceHanSerifJP-Regular.otf +0 -0
  270. package/assets/fonts/ubuntu/Ubuntu-B.ttf +0 -0
  271. package/assets/fonts/ubuntu/Ubuntu-R.ttf +0 -0
  272. package/assets/images/cat_riding_unicorn.jpg +0 -0
  273. package/assets/images/cmyk_colorspace.jpg +0 -0
  274. package/assets/images/greyscale_bird.png +0 -0
  275. package/assets/images/minions_banana_alpha.png +0 -0
  276. package/assets/images/minions_banana_no_alpha.png +0 -0
  277. package/assets/images/minions_laughing.jpg +0 -0
  278. package/assets/pdfs/bixby_guide.pdf +0 -0
  279. package/assets/pdfs/encrypted_new.pdf +0 -0
  280. package/assets/pdfs/encrypted_old.pdf +0 -0
  281. package/assets/pdfs/fancy_fields.pdf +0 -0
  282. package/assets/pdfs/giraffe.pdf +0 -0
  283. package/assets/pdfs/invalid_root_ref.pdf +0 -0
  284. package/assets/pdfs/just_metadata.pdf +0 -49
  285. package/assets/pdfs/missing_endobj_keyword.pdf +0 -70
  286. package/assets/pdfs/missing_xref_trailer_dict.pdf +0 -141
  287. package/assets/pdfs/normal.pdf +0 -0
  288. package/assets/pdfs/pdf20examples/PDF 2.0 with offset start.pdf +0 -179
  289. package/assets/pdfs/stuff_following_header.pdf +0 -0
  290. package/assets/pdfs/us_constitution.pdf +0 -0
  291. package/assets/pdfs/with_comments.pdf +0 -0
  292. package/assets/pdfs/with_invalid_objects.pdf +0 -53
  293. package/assets/pdfs/with_invalid_stream_EOL.pdf +0 -0
  294. package/assets/pdfs/with_null_parent_entry.pdf +0 -0
  295. package/assets/pdfs/with_signature.pdf +0 -0
  296. package/assets/pdfs/with_update_sections.pdf +0 -0
  297. package/assets/pdfs/with_viewer_prefs.pdf +0 -0
  298. package/assets/pdfs/with_xfa_fields.pdf +0 -0
  299. package/src/api/PDFDocument.ts +0 -1389
  300. package/src/api/PDFDocumentOptions.ts +0 -45
  301. package/src/api/PDFEmbeddedFile.ts +0 -87
  302. package/src/api/PDFEmbeddedPage.ts +0 -100
  303. package/src/api/PDFFont.ts +0 -149
  304. package/src/api/PDFImage.ts +0 -143
  305. package/src/api/PDFJavaScript.ts +0 -75
  306. package/src/api/PDFPage.ts +0 -1604
  307. package/src/api/PDFPageOptions.ts +0 -175
  308. package/src/api/StandardFonts.ts +0 -16
  309. package/src/api/colors.ts +0 -109
  310. package/src/api/errors.ts +0 -110
  311. package/src/api/form/PDFButton.ts +0 -242
  312. package/src/api/form/PDFCheckBox.ts +0 -247
  313. package/src/api/form/PDFDropdown.ts +0 -637
  314. package/src/api/form/PDFField.ts +0 -491
  315. package/src/api/form/PDFForm.ts +0 -842
  316. package/src/api/form/PDFOptionList.ts +0 -555
  317. package/src/api/form/PDFRadioGroup.ts +0 -455
  318. package/src/api/form/PDFSignature.ts +0 -44
  319. package/src/api/form/PDFTextField.ts +0 -811
  320. package/src/api/form/appearances.ts +0 -655
  321. package/src/api/image/alignment.ts +0 -5
  322. package/src/api/objects.ts +0 -10
  323. package/src/api/operations.ts +0 -873
  324. package/src/api/operators.ts +0 -335
  325. package/src/api/rotations.ts +0 -84
  326. package/src/api/sizes.ts +0 -52
  327. package/src/api/svg.ts +0 -888
  328. package/src/api/svgPath.ts +0 -460
  329. package/src/api/text/alignment.ts +0 -5
  330. package/src/api/text/layout.ts +0 -328
  331. package/src/core/PDFContext.ts +0 -274
  332. package/src/core/PDFObjectCopier.ts +0 -142
  333. package/src/core/acroform/PDFAcroButton.ts +0 -104
  334. package/src/core/acroform/PDFAcroCheckBox.ts +0 -48
  335. package/src/core/acroform/PDFAcroChoice.ts +0 -143
  336. package/src/core/acroform/PDFAcroComboBox.ts +0 -21
  337. package/src/core/acroform/PDFAcroField.ts +0 -168
  338. package/src/core/acroform/PDFAcroForm.ts +0 -96
  339. package/src/core/acroform/PDFAcroListBox.ts +0 -19
  340. package/src/core/acroform/PDFAcroNonTerminal.ts +0 -33
  341. package/src/core/acroform/PDFAcroPushButton.ts +0 -21
  342. package/src/core/acroform/PDFAcroRadioButton.ts +0 -57
  343. package/src/core/acroform/PDFAcroTerminal.ts +0 -70
  344. package/src/core/acroform/PDFAcroText.ts +0 -76
  345. package/src/core/acroform/flags.ts +0 -162
  346. package/src/core/acroform/utils.ts +0 -129
  347. package/src/core/annotation/AppearanceCharacteristics.ts +0 -133
  348. package/src/core/annotation/BorderStyle.ts +0 -31
  349. package/src/core/annotation/PDFAnnotation.ts +0 -148
  350. package/src/core/annotation/PDFWidgetAnnotation.ts +0 -111
  351. package/src/core/annotation/flags.ts +0 -90
  352. package/src/core/crypto.ts +0 -1844
  353. package/src/core/document/PDFCrossRefSection.ts +0 -161
  354. package/src/core/document/PDFHeader.ts +0 -48
  355. package/src/core/document/PDFTrailer.ts +0 -48
  356. package/src/core/document/PDFTrailerDict.ts +0 -39
  357. package/src/core/embedders/CMap.ts +0 -65
  358. package/src/core/embedders/CustomFontEmbedder.ts +0 -237
  359. package/src/core/embedders/CustomFontSubsetEmbedder.ts +0 -89
  360. package/src/core/embedders/FileEmbedder.ts +0 -77
  361. package/src/core/embedders/FontFlags.ts +0 -45
  362. package/src/core/embedders/JavaScriptEmbedder.ts +0 -34
  363. package/src/core/embedders/JpegEmbedder.ts +0 -118
  364. package/src/core/embedders/PDFPageEmbedder.ts +0 -139
  365. package/src/core/embedders/PngEmbedder.ts +0 -69
  366. package/src/core/embedders/StandardFontEmbedder.ts +0 -121
  367. package/src/core/errors.ts +0 -226
  368. package/src/core/interactive/ViewerPreferences.ts +0 -565
  369. package/src/core/objects/PDFArray.ts +0 -179
  370. package/src/core/objects/PDFBool.ts +0 -53
  371. package/src/core/objects/PDFDict.ts +0 -220
  372. package/src/core/objects/PDFHexString.ts +0 -94
  373. package/src/core/objects/PDFInvalidObject.ts +0 -34
  374. package/src/core/objects/PDFName.ts +0 -152
  375. package/src/core/objects/PDFNull.ts +0 -30
  376. package/src/core/objects/PDFNumber.ts +0 -44
  377. package/src/core/objects/PDFObject.ts +0 -22
  378. package/src/core/objects/PDFRawStream.ts +0 -41
  379. package/src/core/objects/PDFRef.ts +0 -51
  380. package/src/core/objects/PDFStream.ts +0 -87
  381. package/src/core/objects/PDFString.ts +0 -118
  382. package/src/core/operators/PDFOperator.ts +0 -72
  383. package/src/core/operators/PDFOperatorNames.ts +0 -92
  384. package/src/core/parser/BaseParser.ts +0 -119
  385. package/src/core/parser/ByteStream.ts +0 -76
  386. package/src/core/parser/PDFObjectParser.ts +0 -302
  387. package/src/core/parser/PDFObjectStreamParser.ts +0 -65
  388. package/src/core/parser/PDFParser.ts +0 -363
  389. package/src/core/parser/PDFXRefStreamParser.ts +0 -129
  390. package/src/core/streams/Ascii85Stream.ts +0 -97
  391. package/src/core/streams/AsciiHexStream.ts +0 -77
  392. package/src/core/streams/DecodeStream.ts +0 -188
  393. package/src/core/streams/DecryptStream.ts +0 -49
  394. package/src/core/streams/FlateStream.ts +0 -397
  395. package/src/core/streams/LZWStream.ts +0 -157
  396. package/src/core/streams/RunLengthStream.ts +0 -55
  397. package/src/core/streams/Stream.ts +0 -126
  398. package/src/core/streams/decode.ts +0 -70
  399. package/src/core/structures/PDFCatalog.ts +0 -81
  400. package/src/core/structures/PDFContentStream.ts +0 -58
  401. package/src/core/structures/PDFCrossRefStream.ts +0 -238
  402. package/src/core/structures/PDFFlateStream.ts +0 -40
  403. package/src/core/structures/PDFObjectStream.ts +0 -91
  404. package/src/core/structures/PDFPageLeaf.ts +0 -244
  405. package/src/core/structures/PDFPageTree.ts +0 -192
  406. package/src/core/syntax/CharCodes.ts +0 -62
  407. package/src/core/syntax/Delimiters.ts +0 -14
  408. package/src/core/syntax/Irregular.ts +0 -10
  409. package/src/core/syntax/Keywords.ts +0 -57
  410. package/src/core/syntax/Numeric.ts +0 -26
  411. package/src/core/syntax/Whitespace.ts +0 -10
  412. package/src/core/writers/PDFStreamWriter.ts +0 -114
  413. package/src/core/writers/PDFWriter.ts +0 -146
  414. package/src/types/fontkit.ts +0 -643
  415. package/src/utils/Cache.ts +0 -26
  416. package/src/utils/arrays.ts +0 -153
  417. package/src/utils/async.ts +0 -8
  418. package/src/utils/base64.ts +0 -98
  419. package/src/utils/elements/Arc.ts +0 -97
  420. package/src/utils/elements/Circle.ts +0 -47
  421. package/src/utils/elements/Ellipse.ts +0 -117
  422. package/src/utils/elements/GraphElement.ts +0 -14
  423. package/src/utils/elements/Line.ts +0 -83
  424. package/src/utils/elements/Plot.ts +0 -50
  425. package/src/utils/elements/Point.ts +0 -37
  426. package/src/utils/elements/Rectangle.ts +0 -65
  427. package/src/utils/elements/Segment.ts +0 -81
  428. package/src/utils/errors.ts +0 -3
  429. package/src/utils/intersections.ts +0 -237
  430. package/src/utils/maths.ts +0 -96
  431. package/src/utils/numbers.ts +0 -55
  432. package/src/utils/objects.ts +0 -13
  433. package/src/utils/pdfDocEncoding.ts +0 -69
  434. package/src/utils/png.ts +0 -100
  435. package/src/utils/rng.ts +0 -21
  436. package/src/utils/strings.ts +0 -191
  437. package/src/utils/validators.ts +0 -192
  438. package/tsconfig.build.json +0 -14
  439. package/tsconfig.json +0 -15
  440. package/vite.config.mts +0 -39
  441. /package/{src/api/form/index.ts → dist/api/form/index.d.ts} +0 -0
  442. /package/{src/api/image/index.ts → dist/api/image/index.d.ts} +0 -0
  443. /package/{src/api/index.ts → dist/api/index.d.ts} +0 -0
  444. /package/{src/api/text/index.ts → dist/api/text/index.d.ts} +0 -0
  445. /package/{src/core/acroform/index.ts → dist/core/acroform/index.d.ts} +0 -0
  446. /package/{src/core/annotation/index.ts → dist/core/annotation/index.d.ts} +0 -0
  447. /package/{src/index.ts → dist/index.d.ts} +0 -0
  448. /package/{src/utils/elements/index.ts → dist/utils/elements/index.d.ts} +0 -0
  449. /package/{src/utils/index.ts → dist/utils/index.d.ts} +0 -0
package/src/api/svg.ts DELETED
@@ -1,888 +0,0 @@
1
- import {
2
- parse as parseHtml,
3
- HTMLElement,
4
- Attributes,
5
- Node,
6
- NodeType,
7
- } from 'node-html-better-parser';
8
- import { Color, colorString } from './colors';
9
- import { Degrees, degreesToRadians } from './rotations';
10
- import PDFFont from './PDFFont';
11
- import PDFPage from './PDFPage';
12
- import { PDFPageDrawSVGElementOptions } from './PDFPageOptions';
13
- import { LineCapStyle, LineJoinStyle, FillRule } from './operators';
14
- import { TransformationMatrix, identityMatrix } from '../types/matrix';
15
- import { Coordinates, Space } from '../types';
16
-
17
- interface Position {
18
- x: number;
19
- y: number;
20
- }
21
-
22
- interface Size {
23
- width: number;
24
- height: number;
25
- }
26
-
27
- type Box = Position & Size;
28
-
29
- type SVGStyle = Record<string, string>;
30
-
31
- type InheritedAttributes = {
32
- width: number;
33
- height: number;
34
- fill?: Color;
35
- fillOpacity?: number;
36
- stroke?: Color;
37
- strokeWidth?: number;
38
- strokeOpacity?: number;
39
- strokeLineCap?: LineCapStyle;
40
- fillRule?: FillRule;
41
- strokeLineJoin?: LineJoinStyle;
42
- fontFamily?: string;
43
- fontStyle?: string;
44
- fontWeight?: string;
45
- fontSize?: number;
46
- rotation?: Degrees;
47
- viewBox: Box;
48
- };
49
- type SVGAttributes = {
50
- rotate?: Degrees;
51
- scale?: number;
52
- skewX?: Degrees;
53
- skewY?: Degrees;
54
- width?: number;
55
- height?: number;
56
- x?: number;
57
- y?: number;
58
- cx?: number;
59
- cy?: number;
60
- r?: number;
61
- rx?: number;
62
- ry?: number;
63
- x1?: number;
64
- y1?: number;
65
- x2?: number;
66
- y2?: number;
67
- d?: string;
68
- src?: string;
69
- textAnchor?: string;
70
- preserveAspectRatio?: string;
71
- strokeWidth?: number;
72
- dominantBaseline?: string;
73
- points?: string;
74
- };
75
-
76
- type TransformAttributes = {
77
- matrix: TransformationMatrix;
78
- clipSpaces: Space[];
79
- };
80
-
81
- export type SVGElement = HTMLElement & {
82
- svgAttributes: InheritedAttributes & SVGAttributes & TransformAttributes;
83
- };
84
-
85
- interface SVGElementToDrawMap {
86
- [cmd: string]: (a: SVGElement) => Promise<void>;
87
- }
88
-
89
- const combineMatrix = (
90
- [a, b, c, d, e, f]: TransformationMatrix,
91
- [a2, b2, c2, d2, e2, f2]: TransformationMatrix,
92
- ): TransformationMatrix => [
93
- a * a2 + c * b2,
94
- b * a2 + d * b2,
95
- a * c2 + c * d2,
96
- b * c2 + d * d2,
97
- a * e2 + c * f2 + e,
98
- b * e2 + d * f2 + f,
99
- ];
100
-
101
- const applyTransformation = (
102
- [a, b, c, d, e, f]: TransformationMatrix,
103
- { x, y }: Coordinates,
104
- ): Coordinates => ({
105
- x: a * x + c * y + e,
106
- y: b * x + d * y + f,
107
- });
108
-
109
- type TransformationName =
110
- | 'scale'
111
- | 'scaleX'
112
- | 'scaleY'
113
- | 'translate'
114
- | 'translateX'
115
- | 'translateY'
116
- | 'rotate'
117
- | 'skewX'
118
- | 'skewY'
119
- | 'matrix';
120
- const transformationToMatrix = (name: TransformationName, args: number[]): TransformationMatrix => {
121
- switch (name) {
122
- case 'scale':
123
- case 'scaleX':
124
- case 'scaleY': {
125
- // [sx 0 0 sy 0 0]
126
- const [sx, sy = sx] = args;
127
- return [name === 'scaleY' ? 1 : sx, 0, 0, name === 'scaleX' ? 1 : sy, 0, 0];
128
- }
129
- case 'translate':
130
- case 'translateX':
131
- case 'translateY': {
132
- // [1 0 0 1 tx ty]
133
- const [tx, ty = tx] = args;
134
- // -ty is necessary because the pdf's y axis is inverted
135
- return [1, 0, 0, 1, name === 'translateY' ? 0 : tx, name === 'translateX' ? 0 : -ty];
136
- }
137
- case 'rotate': {
138
- // [cos(a) sin(a) -sin(a) cos(a) 0 0]
139
- const [a, x = 0, y = 0] = args;
140
- const t1 = transformationToMatrix('translate', [x, y]);
141
- const t2 = transformationToMatrix('translate', [-x, -y]);
142
- // -args[0] -> the '-' operator is necessary because the pdf rotation system is inverted
143
- const aRadians = degreesToRadians(-a);
144
- const r: TransformationMatrix = [
145
- Math.cos(aRadians),
146
- Math.sin(aRadians),
147
- -Math.sin(aRadians),
148
- Math.cos(aRadians),
149
- 0,
150
- 0,
151
- ];
152
- // rotation around a point is the combination of: translate * rotate * (-translate)
153
- return combineMatrix(combineMatrix(t1, r), t2);
154
- }
155
- case 'skewY':
156
- case 'skewX': {
157
- // [1 tan(a) 0 1 0 0]
158
- // [1 0 tan(a) 1 0 0]
159
- // -args[0] -> the '-' operator is necessary because the pdf rotation system is inverted
160
- const a = degreesToRadians(-args[0]);
161
- const skew = Math.tan(a);
162
- const skewX = name === 'skewX' ? skew : 0;
163
- const skewY = name === 'skewY' ? skew : 0;
164
- return [1, skewY, skewX, 1, 0, 0];
165
- }
166
- case 'matrix': {
167
- const [a, b, c, d, e, f] = args;
168
- const r = transformationToMatrix('scale', [1, -1]);
169
- const m: TransformationMatrix = [a, b, c, d, e, f];
170
- return combineMatrix(combineMatrix(r, m), r);
171
- }
172
- default:
173
- return identityMatrix;
174
- }
175
- };
176
-
177
- const combineTransformation = (
178
- matrix: TransformationMatrix,
179
- name: TransformationName,
180
- args: number[],
181
- ) => combineMatrix(matrix, transformationToMatrix(name, args));
182
-
183
- const StrokeLineCapMap: Record<string, LineCapStyle> = {
184
- butt: LineCapStyle.Butt,
185
- round: LineCapStyle.Round,
186
- square: LineCapStyle.Projecting,
187
- };
188
-
189
- const FillRuleMap: Record<string, FillRule> = {
190
- evenodd: FillRule.EvenOdd,
191
- nonzero: FillRule.NonZero,
192
- };
193
-
194
- const StrokeLineJoinMap: Record<string, LineJoinStyle> = {
195
- bevel: LineJoinStyle.Bevel,
196
- miter: LineJoinStyle.Miter,
197
- round: LineJoinStyle.Round,
198
- };
199
-
200
- // TODO: Improve type system to require the correct props for each tagName.
201
- /** methods to draw SVGElements onto a PDFPage */
202
- const runnersToPage = (
203
- page: PDFPage,
204
- options: PDFPageDrawSVGElementOptions,
205
- ): SVGElementToDrawMap => ({
206
- async text(element) {
207
- const anchor = element.svgAttributes.textAnchor;
208
- const dominantBaseline = element.svgAttributes.dominantBaseline;
209
- const text = element.text.trim().replace(/\s/g, ' ');
210
- const fontSize = element.svgAttributes.fontSize || 12;
211
-
212
- /** This will find the best font for the provided style in the list */
213
- function getBestFont(style: InheritedAttributes, fonts: { [fontName: string]: PDFFont }) {
214
- const family = style.fontFamily;
215
- if (!family) return undefined;
216
- const isBold = style.fontWeight === 'bold' || Number(style.fontWeight) >= 700;
217
- const isItalic = style.fontStyle === 'italic';
218
- const getFont = (bold: boolean, italic: boolean, family: string) =>
219
- fonts[family + (bold ? '_bold' : '') + (italic ? '_italic' : '')];
220
- return (
221
- getFont(isBold, isItalic, family) ||
222
- getFont(isBold, false, family) ||
223
- getFont(false, isItalic, family) ||
224
- getFont(false, false, family) ||
225
- Object.keys(fonts).find((fontFamily) => fontFamily.startsWith(family))
226
- );
227
- }
228
-
229
- const font = options.fonts && getBestFont(element.svgAttributes, options.fonts);
230
- const textWidth = (font || page.getFont()[0]).widthOfTextAtSize(text, fontSize);
231
-
232
- const textHeight = (font || page.getFont()[0]).heightAtSize(fontSize);
233
- const offsetX = anchor === 'middle' ? textWidth / 2 : anchor === 'end' ? textWidth : 0;
234
-
235
- const offsetY =
236
- dominantBaseline === 'text-before-edge'
237
- ? textHeight
238
- : dominantBaseline === 'text-after-edge'
239
- ? -textHeight
240
- : dominantBaseline === 'middle'
241
- ? textHeight / 2
242
- : 0;
243
-
244
- page.drawText(text, {
245
- x: -offsetX,
246
- y: -offsetY,
247
- font,
248
- // TODO: the font size should be correctly scaled too
249
- size: fontSize,
250
- color: element.svgAttributes.fill,
251
- opacity: element.svgAttributes.fillOpacity,
252
- matrix: element.svgAttributes.matrix,
253
- clipSpaces: element.svgAttributes.clipSpaces,
254
- });
255
- },
256
- async line(element) {
257
- page.drawLine({
258
- start: {
259
- x: element.svgAttributes.x1 || 0,
260
- y: -element.svgAttributes.y1! || 0,
261
- },
262
- end: {
263
- x: element.svgAttributes.x2! || 0,
264
- y: -element.svgAttributes.y2! || 0,
265
- },
266
- thickness: element.svgAttributes.strokeWidth,
267
- color: element.svgAttributes.stroke,
268
- opacity: element.svgAttributes.strokeOpacity,
269
- lineCap: element.svgAttributes.strokeLineCap,
270
- matrix: element.svgAttributes.matrix,
271
- clipSpaces: element.svgAttributes.clipSpaces,
272
- });
273
- },
274
- async path(element) {
275
- if (!element.svgAttributes.d) return;
276
- // See https://jsbin.com/kawifomupa/edit?html,output and
277
- page.drawSvgPath(element.svgAttributes.d, {
278
- x: 0,
279
- y: 0,
280
- borderColor: element.svgAttributes.stroke,
281
- borderWidth: element.svgAttributes.strokeWidth,
282
- borderOpacity: element.svgAttributes.strokeOpacity,
283
- borderLineCap: element.svgAttributes.strokeLineCap,
284
- color: element.svgAttributes.fill,
285
- opacity: element.svgAttributes.fillOpacity,
286
- fillRule: element.svgAttributes.fillRule,
287
- matrix: element.svgAttributes.matrix,
288
- clipSpaces: element.svgAttributes.clipSpaces,
289
- });
290
- },
291
- async image(element) {
292
- const { src } = element.svgAttributes;
293
- if (!src) return;
294
- const isPng = src.match(/\.png(\?|$)|^data:image\/png;base64/gim);
295
- const img = isPng ? await page.doc.embedPng(src) : await page.doc.embedJpg(src);
296
-
297
- const { x, y, width, height } = getFittingRectangle(
298
- img.width,
299
- img.height,
300
- element.svgAttributes.width || img.width,
301
- element.svgAttributes.height || img.height,
302
- element.svgAttributes.preserveAspectRatio,
303
- );
304
- page.drawImage(img, {
305
- x,
306
- y: -y - height,
307
- width,
308
- height,
309
- opacity: element.svgAttributes.fillOpacity,
310
- matrix: element.svgAttributes.matrix,
311
- clipSpaces: element.svgAttributes.clipSpaces,
312
- });
313
- },
314
- async rect(element) {
315
- if (!element.svgAttributes.fill && !element.svgAttributes.stroke) return;
316
- page.drawRectangle({
317
- x: 0,
318
- y: 0,
319
- width: element.svgAttributes.width,
320
- height: element.svgAttributes.height * -1,
321
- borderColor: element.svgAttributes.stroke,
322
- borderWidth: element.svgAttributes.strokeWidth,
323
- borderOpacity: element.svgAttributes.strokeOpacity,
324
- borderLineCap: element.svgAttributes.strokeLineCap,
325
- color: element.svgAttributes.fill,
326
- opacity: element.svgAttributes.fillOpacity,
327
- matrix: element.svgAttributes.matrix,
328
- clipSpaces: element.svgAttributes.clipSpaces,
329
- });
330
- },
331
- async ellipse(element) {
332
- page.drawEllipse({
333
- x: element.svgAttributes.cx || 0,
334
- y: -(element.svgAttributes.cy || 0),
335
- xScale: element.svgAttributes.rx,
336
- yScale: element.svgAttributes.ry,
337
- borderColor: element.svgAttributes.stroke,
338
- borderWidth: element.svgAttributes.strokeWidth,
339
- borderOpacity: element.svgAttributes.strokeOpacity,
340
- borderLineCap: element.svgAttributes.strokeLineCap,
341
- color: element.svgAttributes.fill,
342
- opacity: element.svgAttributes.fillOpacity,
343
- matrix: element.svgAttributes.matrix,
344
- clipSpaces: element.svgAttributes.clipSpaces,
345
- });
346
- },
347
- async circle(element) {
348
- return runnersToPage(page, options).ellipse(element);
349
- },
350
- });
351
-
352
- const styleOrAttribute = (
353
- attributes: Attributes,
354
- style: SVGStyle,
355
- attribute: string,
356
- def?: string,
357
- ): string => {
358
- const value = style[attribute] || attributes[attribute];
359
- if (!value && typeof def !== 'undefined') return def;
360
- return value;
361
- };
362
-
363
- const parseStyles = (style: string): SVGStyle => {
364
- const cssRegex = /([^:\s]+)\s*:\s*([^;]+)/g;
365
- const css: SVGStyle = {};
366
- let match = cssRegex.exec(style);
367
- while (match != null) {
368
- css[match[1]] = match[2];
369
- match = cssRegex.exec(style);
370
- }
371
- return css;
372
- };
373
-
374
- const parseColor = (
375
- color: string,
376
- inherited?: { rgb: Color; alpha?: string },
377
- ): { rgb: Color; alpha?: string } | undefined => {
378
- if (!color || color.length === 0) return undefined;
379
- if (['none', 'transparent'].includes(color)) return undefined;
380
- if (color === 'currentColor') return inherited || parseColor('#000000');
381
- const parsedColor = colorString(color);
382
- return {
383
- rgb: parsedColor.rgb,
384
- alpha: parsedColor.alpha ? parsedColor.alpha + '' : undefined,
385
- };
386
- };
387
-
388
- type ParsedAttributes = {
389
- inherited: InheritedAttributes;
390
- tagName: string;
391
- svgAttributes: SVGAttributes;
392
- matrix: TransformationMatrix;
393
- };
394
-
395
- const parseAttributes = (
396
- element: HTMLElement,
397
- inherited: InheritedAttributes,
398
- matrix: TransformationMatrix,
399
- ): ParsedAttributes => {
400
- const attributes = element.attributes;
401
- const style = parseStyles(attributes.style);
402
- const widthRaw = styleOrAttribute(attributes, style, 'width', '');
403
- const heightRaw = styleOrAttribute(attributes, style, 'height', '');
404
- const fillRaw = parseColor(styleOrAttribute(attributes, style, 'fill'));
405
- const fillOpacityRaw = styleOrAttribute(attributes, style, 'fill-opacity');
406
- const opacityRaw = styleOrAttribute(attributes, style, 'opacity');
407
- const strokeRaw = parseColor(styleOrAttribute(attributes, style, 'stroke'));
408
- const strokeOpacityRaw = styleOrAttribute(attributes, style, 'stroke-opacity');
409
- const strokeLineCapRaw = styleOrAttribute(attributes, style, 'stroke-linecap');
410
- const strokeLineJoinRaw = styleOrAttribute(attributes, style, 'stroke-linejoin');
411
- const fillRuleRaw = styleOrAttribute(attributes, style, 'fill-rule');
412
- const strokeWidthRaw = styleOrAttribute(attributes, style, 'stroke-width');
413
- const fontFamilyRaw = styleOrAttribute(attributes, style, 'font-family');
414
- const fontStyleRaw = styleOrAttribute(attributes, style, 'font-style');
415
- const fontWeightRaw = styleOrAttribute(attributes, style, 'font-weight');
416
- const fontSizeRaw = styleOrAttribute(attributes, style, 'font-size');
417
-
418
- const width = parseFloatValue(widthRaw, inherited.width);
419
- const height = parseFloatValue(heightRaw, inherited.height);
420
- const x = parseFloatValue(attributes.x, inherited.width);
421
- const y = parseFloatValue(attributes.y, inherited.height);
422
- const x1 = parseFloatValue(attributes.x1, inherited.width);
423
- const x2 = parseFloatValue(attributes.x2, inherited.width);
424
- const y1 = parseFloatValue(attributes.y1, inherited.height);
425
- const y2 = parseFloatValue(attributes.y2, inherited.height);
426
- const cx = parseFloatValue(attributes.cx, inherited.width);
427
- const cy = parseFloatValue(attributes.cy, inherited.height);
428
- const rx = parseFloatValue(attributes.rx || attributes.r, inherited.width);
429
- const ry = parseFloatValue(attributes.ry || attributes.r, inherited.height);
430
-
431
- const newInherited: InheritedAttributes = {
432
- fontFamily: fontFamilyRaw || inherited.fontFamily,
433
- fontStyle: fontStyleRaw || inherited.fontStyle,
434
- fontWeight: fontWeightRaw || inherited.fontWeight,
435
- fontSize: parseFloatValue(fontSizeRaw) ?? inherited.fontSize,
436
- fill: fillRaw?.rgb || inherited.fill,
437
- fillOpacity:
438
- parseFloatValue(fillOpacityRaw || opacityRaw || fillRaw?.alpha) ?? inherited.fillOpacity,
439
- fillRule: FillRuleMap[fillRuleRaw] || inherited.fillRule,
440
- stroke: strokeRaw?.rgb || inherited.stroke,
441
- strokeWidth: parseFloatValue(strokeWidthRaw) ?? inherited.strokeWidth,
442
- strokeOpacity:
443
- parseFloatValue(strokeOpacityRaw || opacityRaw || strokeRaw?.alpha) ??
444
- inherited.strokeOpacity,
445
- strokeLineCap: StrokeLineCapMap[strokeLineCapRaw] || inherited.strokeLineCap,
446
- strokeLineJoin: StrokeLineJoinMap[strokeLineJoinRaw] || inherited.strokeLineJoin,
447
- width: width || inherited.width,
448
- height: height || inherited.height,
449
- rotation: inherited.rotation,
450
- viewBox:
451
- element.tagName === 'svg' && element.attributes.viewBox
452
- ? parseViewBox(element.attributes.viewBox)!
453
- : inherited.viewBox,
454
- };
455
-
456
- const svgAttributes: SVGAttributes = {
457
- src: attributes.src || attributes['xlink:href'],
458
- textAnchor: attributes['text-anchor'],
459
- dominantBaseline: attributes['dominant-baseline'],
460
- preserveAspectRatio: attributes.preserveAspectRatio,
461
- };
462
-
463
- let transformList = attributes.transform || '';
464
- // Handle transformations set as direct attributes
465
- [
466
- 'translate',
467
- 'translateX',
468
- 'translateY',
469
- 'skewX',
470
- 'skewY',
471
- 'rotate',
472
- 'scale',
473
- 'scaleX',
474
- 'scaleY',
475
- 'matrix',
476
- ].forEach((name) => {
477
- if (attributes[name]) {
478
- transformList = attributes[name] + ' ' + transformList;
479
- }
480
- });
481
-
482
- // Convert x/y as if it was a translation
483
- if (x || y) {
484
- transformList = transformList + `translate(${x || 0} ${y || 0}) `;
485
- }
486
- let newMatrix = matrix;
487
- // Apply the transformations
488
- if (transformList) {
489
- const regexTransform = /(\w+)\((.+?)\)/g;
490
- let parsed = regexTransform.exec(transformList);
491
- while (parsed !== null) {
492
- const [, name, rawArgs] = parsed;
493
- const args = (rawArgs || '')
494
- .split(/\s*,\s*|\s+/)
495
- .filter((value) => value.length > 0)
496
- .map((value) => parseFloat(value));
497
- newMatrix = combineTransformation(newMatrix, name as TransformationName, args);
498
- parsed = regexTransform.exec(transformList);
499
- }
500
- }
501
-
502
- svgAttributes.x = x;
503
- svgAttributes.y = y;
504
-
505
- if (attributes.cx || attributes.cy) {
506
- svgAttributes.cx = cx;
507
- svgAttributes.cy = cy;
508
- }
509
- if (attributes.rx || attributes.ry || attributes.r) {
510
- svgAttributes.rx = rx;
511
- svgAttributes.ry = ry;
512
- }
513
- if (attributes.x1 || attributes.y1) {
514
- svgAttributes.x1 = x1;
515
- svgAttributes.y1 = y1;
516
- }
517
- if (attributes.x2 || attributes.y2) {
518
- svgAttributes.x2 = x2;
519
- svgAttributes.y2 = y2;
520
- }
521
- if (attributes.width || attributes.height) {
522
- svgAttributes.width = width ?? inherited.width;
523
- svgAttributes.height = height ?? inherited.height;
524
- }
525
-
526
- if (attributes.d) {
527
- newMatrix = combineTransformation(newMatrix, 'scale', [1, -1]);
528
- svgAttributes.d = attributes.d;
529
- }
530
-
531
- if (newInherited.fontFamily) {
532
- // Handle complex fontFamily like `"Linux Libertine O", serif`
533
- const inner = newInherited.fontFamily.match(/^"(.*?)"|^'(.*?)'/);
534
- if (inner) newInherited.fontFamily = inner[1] || inner[2];
535
- }
536
-
537
- if (newInherited.strokeWidth) {
538
- svgAttributes.strokeWidth = newInherited.strokeWidth;
539
- }
540
-
541
- return {
542
- inherited: newInherited,
543
- svgAttributes,
544
- tagName: element.tagName,
545
- matrix: newMatrix,
546
- };
547
- };
548
-
549
- const getFittingRectangle = (
550
- originalWidth: number,
551
- originalHeight: number,
552
- targetWidth: number,
553
- targetHeight: number,
554
- preserveAspectRatio?: string,
555
- ) => {
556
- if (preserveAspectRatio === 'none') {
557
- return { x: 0, y: 0, width: targetWidth, height: targetHeight };
558
- }
559
- const originalRatio = originalWidth / originalHeight;
560
- const targetRatio = targetWidth / targetHeight;
561
- const width = targetRatio > originalRatio ? originalRatio * targetHeight : targetWidth;
562
- const height = targetRatio >= originalRatio ? targetHeight : targetWidth / originalRatio;
563
- const dx = targetWidth - width;
564
- const dy = targetHeight - height;
565
- const [x, y] = (() => {
566
- switch (preserveAspectRatio) {
567
- case 'xMinYMin':
568
- return [0, 0];
569
- case 'xMidYMin':
570
- return [dx / 2, 0];
571
- case 'xMaxYMin':
572
- return [dx, dy / 2];
573
- case 'xMinYMid':
574
- return [0, dy];
575
- case 'xMaxYMid':
576
- return [dx, dy / 2];
577
- case 'xMinYMax':
578
- return [0, dy];
579
- case 'xMidYMax':
580
- return [dx / 2, dy];
581
- case 'xMaxYMax':
582
- return [dx, dy];
583
- case 'xMidYMid':
584
- default:
585
- return [dx / 2, dy / 2];
586
- }
587
- })();
588
- return { x, y, width, height };
589
- };
590
-
591
- const getAspectRatioTransformation = (
592
- matrix: TransformationMatrix,
593
- originalWidth: number,
594
- originalHeight: number,
595
- targetWidth: number,
596
- targetHeight: number,
597
- preserveAspectRatio?: string,
598
- ): {
599
- clipBox: TransformationMatrix;
600
- content: TransformationMatrix;
601
- } => {
602
- const scaleX = targetWidth / originalWidth;
603
- const scaleY = targetHeight / originalHeight;
604
- const boxScale = combineTransformation(matrix, 'scale', [scaleX, scaleY]);
605
- if (preserveAspectRatio === 'none') {
606
- return {
607
- clipBox: boxScale,
608
- content: boxScale,
609
- };
610
- }
611
- // TODO: the following code works for the 'meet' param but not for the 'slice'
612
- const scale = targetWidth > targetHeight ? scaleY : scaleX;
613
- const dx = targetWidth - originalWidth * scale;
614
- const dy = targetHeight - originalHeight * scale;
615
- const [x, y] = (() => {
616
- switch (preserveAspectRatio) {
617
- case 'xMinYMin':
618
- return [0, 0];
619
- case 'xMidYMin':
620
- return [dx / 2, 0];
621
- case 'xMaxYMin':
622
- return [dx, dy / 2];
623
- case 'xMinYMid':
624
- return [0, dy];
625
- case 'xMaxYMid':
626
- return [dx, dy / 2];
627
- case 'xMinYMax':
628
- return [0, dy];
629
- case 'xMidYMax':
630
- return [dx / 2, dy];
631
- case 'xMaxYMax':
632
- return [dx, dy];
633
- case 'xMidYMid':
634
- default:
635
- return [dx / 2, dy / 2];
636
- }
637
- })();
638
-
639
- const contentTransform = combineTransformation(
640
- combineTransformation(matrix, 'translate', [x, y]),
641
- 'scale',
642
- [scale],
643
- );
644
-
645
- return {
646
- clipBox: boxScale,
647
- content: contentTransform,
648
- };
649
- };
650
-
651
- const parseHTMLNode = (
652
- node: Node,
653
- inherited: InheritedAttributes,
654
- matrix: TransformationMatrix,
655
- clipSpaces: Space[],
656
- ): SVGElement[] => {
657
- if (node.nodeType === NodeType.COMMENT_NODE) return [];
658
- else if (node.nodeType === NodeType.TEXT_NODE) return [];
659
- else if (node.tagName === 'g') {
660
- return parseGroupNode(node as HTMLElement & { tagName: 'g' }, inherited, matrix, clipSpaces);
661
- } else if (node.tagName === 'svg') {
662
- return parseSvgNode(node as HTMLElement & { tagName: 'svg' }, inherited, matrix, clipSpaces);
663
- } else {
664
- if (node.tagName === 'polygon') {
665
- node.tagName = 'path';
666
- node.attributes.d = `M${node.attributes.points}Z`;
667
- delete node.attributes.points;
668
- }
669
- const attributes = parseAttributes(node, inherited, matrix);
670
- const svgAttributes = {
671
- ...attributes.inherited,
672
- ...attributes.svgAttributes,
673
- matrix: attributes.matrix,
674
- clipSpaces,
675
- };
676
- Object.assign(node, { svgAttributes });
677
- return [node as SVGElement];
678
- }
679
- };
680
-
681
- const parseSvgNode = (
682
- node: HTMLElement & { tagName: 'svg' },
683
- inherited: InheritedAttributes,
684
- matrix: TransformationMatrix,
685
- clipSpaces: Space[],
686
- ): SVGElement[] => {
687
- // if the width/height aren't set, the svg will have the same dimension as the current drawing space
688
- if (!node.attributes.width) {
689
- node.setAttribute('width', inherited.viewBox.width + '');
690
- }
691
- if (!node.attributes.height) {
692
- node.setAttribute('height', inherited.viewBox.height + '');
693
- }
694
- const attributes = parseAttributes(node, inherited, matrix);
695
- const result: SVGElement[] = [];
696
- const viewBox = node.attributes.viewBox
697
- ? parseViewBox(node.attributes.viewBox)!
698
- : node.attributes.width && node.attributes.height
699
- ? parseViewBox(`0 0 ${node.attributes.width} ${node.attributes.height}`)!
700
- : inherited.viewBox;
701
- const x = parseFloat(node.attributes.x) || 0;
702
- const y = parseFloat(node.attributes.y) || 0;
703
-
704
- let newMatrix = combineTransformation(matrix, 'translate', [x, y]);
705
-
706
- const { clipBox: clipBoxTransform, content: contentTransform } = getAspectRatioTransformation(
707
- newMatrix,
708
- viewBox.width,
709
- viewBox.height,
710
- parseFloat(node.attributes.width),
711
- parseFloat(node.attributes.height),
712
- node.attributes.preserveAspectRatio || 'xMidYMid',
713
- );
714
-
715
- const topLeft = applyTransformation(clipBoxTransform, {
716
- x: 0,
717
- y: 0,
718
- });
719
-
720
- const topRight = applyTransformation(clipBoxTransform, {
721
- x: viewBox.width,
722
- y: 0,
723
- });
724
-
725
- const bottomRight = applyTransformation(clipBoxTransform, {
726
- x: viewBox.width,
727
- y: -viewBox.height,
728
- });
729
-
730
- const bottomLeft = applyTransformation(clipBoxTransform, {
731
- x: 0,
732
- y: -viewBox.height,
733
- });
734
-
735
- const baseClipSpace: Space = {
736
- topLeft,
737
- topRight,
738
- bottomRight,
739
- bottomLeft,
740
- };
741
-
742
- // TODO: maybe this is the correct transformation
743
- // newMatrix = combineTransformation(newMatrix, 'translate', [-baseClipSpace.xMin, -baseClipSpace.yMin])
744
- newMatrix = combineTransformation(contentTransform, 'translate', [-viewBox.x, -viewBox.y]);
745
-
746
- node.childNodes.forEach((child) => {
747
- const parsedNodes = parseHTMLNode(child, { ...attributes.inherited, viewBox }, newMatrix, [
748
- ...clipSpaces,
749
- baseClipSpace,
750
- ]);
751
- result.push(...parsedNodes);
752
- });
753
- return result;
754
- };
755
-
756
- const parseGroupNode = (
757
- node: HTMLElement & { tagName: 'g' },
758
- inherited: InheritedAttributes,
759
- matrix: TransformationMatrix,
760
- clipSpaces: Space[],
761
- ): SVGElement[] => {
762
- const attributes = parseAttributes(node, inherited, matrix);
763
- const result: SVGElement[] = [];
764
- node.childNodes.forEach((child) => {
765
- result.push(...parseHTMLNode(child, attributes.inherited, attributes.matrix, clipSpaces));
766
- });
767
- return result;
768
- };
769
-
770
- const parseFloatValue = (value?: string, reference = 1) => {
771
- if (!value) return undefined;
772
- const v = parseFloat(value);
773
- if (isNaN(v)) return undefined;
774
- if (value.endsWith('%')) return (v * reference) / 100;
775
- return v;
776
- };
777
-
778
- const parseViewBox = (viewBox?: string): Box | undefined => {
779
- if (!viewBox) return;
780
- const [xViewBox = 0, yViewBox = 0, widthViewBox = 1, heightViewBox = 1] = (viewBox || '')
781
- .split(' ')
782
- .map((val) => parseFloatValue(val));
783
- return {
784
- x: xViewBox,
785
- y: yViewBox,
786
- width: widthViewBox,
787
- height: heightViewBox,
788
- };
789
- };
790
-
791
- const parse = (
792
- svg: string,
793
- { width, height, fontSize }: PDFPageDrawSVGElementOptions,
794
- size: Size,
795
- matrix: TransformationMatrix,
796
- ): SVGElement[] => {
797
- const htmlElement = parseHtml(svg).firstChild as HTMLElement;
798
- if (width) htmlElement.setAttribute('width', width + '');
799
- if (height) htmlElement.setAttribute('height', height + '');
800
- if (fontSize) htmlElement.setAttribute('font-size', fontSize + '');
801
- // TODO: what should be the default viewBox?
802
- return parseHTMLNode(
803
- htmlElement,
804
- {
805
- ...size,
806
- viewBox: parseViewBox(htmlElement.attributes.viewBox || '0 0 1 1')!,
807
- },
808
- matrix,
809
- [],
810
- );
811
- };
812
-
813
- export const drawSvg = async (
814
- page: PDFPage,
815
- svg: string,
816
- options: PDFPageDrawSVGElementOptions,
817
- ) => {
818
- if (!svg) return;
819
- const size = page.getSize();
820
- const firstChild = parseHtml(svg).firstChild as HTMLElement;
821
-
822
- const attributes = firstChild.attributes;
823
- const style = parseStyles(attributes.style);
824
-
825
- const widthRaw = styleOrAttribute(attributes, style, 'width', '');
826
- const heightRaw = styleOrAttribute(attributes, style, 'height', '');
827
-
828
- const width = options.width !== undefined ? options.width : parseFloat(widthRaw);
829
- const height = options.height !== undefined ? options.height : parseFloat(heightRaw);
830
-
831
- // it's important to add the viewBox to allow svg resizing through the options
832
- if (!attributes.viewBox) {
833
- firstChild.setAttribute('viewBox', `0 0 ${widthRaw || width} ${heightRaw || height}`);
834
- }
835
-
836
- if (options.width || options.height) {
837
- if (width !== undefined) style.width = width + (isNaN(width) ? '' : 'px');
838
- if (height !== undefined) {
839
- style.height = height + (isNaN(height) ? '' : 'px');
840
- }
841
- firstChild.setAttribute(
842
- 'style',
843
- Object.entries(style)
844
- .map(([key, val]) => `${key}:${val};`)
845
- .join(''),
846
- );
847
- }
848
-
849
- const baseTransformation: TransformationMatrix = [1, 0, 0, 1, options.x || 0, options.y || 0];
850
-
851
- const runners = runnersToPage(page, options);
852
- const elements = parse(firstChild.outerHTML, options, size, baseTransformation);
853
-
854
- await elements.reduce(async (prev, elt) => {
855
- await prev;
856
- // uncomment these lines to draw the clipSpaces
857
- // elt.svgAttributes.clipSpaces.forEach(space => {
858
- // page.drawLine({
859
- // start: space.topLeft,
860
- // end: space.topRight,
861
- // color: parseColor('#000000')?.rgb,
862
- // thickness: 1
863
- // })
864
-
865
- // page.drawLine({
866
- // start: space.topRight,
867
- // end: space.bottomRight,
868
- // color: parseColor('#000000')?.rgb,
869
- // thickness: 1
870
- // })
871
-
872
- // page.drawLine({
873
- // start: space.bottomRight,
874
- // end: space.bottomLeft,
875
- // color: parseColor('#000000')?.rgb,
876
- // thickness: 1
877
- // })
878
-
879
- // page.drawLine({
880
- // start: space.bottomLeft,
881
- // end: space.topLeft,
882
- // color: parseColor('#000000')?.rgb,
883
- // thickness: 1
884
- // })
885
- // })
886
- return runners[elt.tagName]?.(elt);
887
- }, Promise.resolve());
888
- };