pulpweaver4 0.0.91 → 0.0.185

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 (502) hide show
  1. package/dist/ast/djot.js +23 -0
  2. package/dist/ast/djot.js.map +1 -1
  3. package/dist/ast/index.d.ts +1 -0
  4. package/dist/ast/index.js +1 -0
  5. package/dist/ast/index.js.map +1 -1
  6. package/dist/ast/nodes.d.ts +59 -5
  7. package/dist/ast/nodes.js.map +1 -1
  8. package/dist/ast/table.d.ts +27 -0
  9. package/dist/ast/table.js +32 -0
  10. package/dist/ast/table.js.map +1 -0
  11. package/dist/browser.js +2420 -0
  12. package/dist/citations/index.d.ts +1 -1
  13. package/dist/citations/index.js +1 -1
  14. package/dist/citations/index.js.map +1 -1
  15. package/dist/citations/project.d.ts +12 -2
  16. package/dist/citations/project.js +21 -7
  17. package/dist/citations/project.js.map +1 -1
  18. package/dist/colour/images.d.ts +11 -2
  19. package/dist/colour/images.js +53 -3
  20. package/dist/colour/images.js.map +1 -1
  21. package/dist/colour/ink-limit.d.ts +2 -2
  22. package/dist/colour/ink-limit.js +6 -0
  23. package/dist/colour/ink-limit.js.map +1 -1
  24. package/dist/colour/previews.d.ts +8 -4
  25. package/dist/colour/previews.js +11 -8
  26. package/dist/colour/previews.js.map +1 -1
  27. package/dist/config/config.d.ts +91 -3
  28. package/dist/config/config.js +200 -3
  29. package/dist/config/config.js.map +1 -1
  30. package/dist/config/project-schema.d.ts +0 -8
  31. package/dist/config/project-schema.js +207 -12
  32. package/dist/config/project-schema.js.map +1 -1
  33. package/dist/config/schema.js +10 -0
  34. package/dist/config/schema.js.map +1 -1
  35. package/dist/config/secrets.d.ts +11 -0
  36. package/dist/config/secrets.js +19 -0
  37. package/dist/config/secrets.js.map +1 -0
  38. package/dist/csl/project.d.ts +19 -4
  39. package/dist/csl/project.js +23 -10
  40. package/dist/csl/project.js.map +1 -1
  41. package/dist/fonts/index.d.ts +1 -1
  42. package/dist/fonts/index.js +1 -1
  43. package/dist/fonts/index.js.map +1 -1
  44. package/dist/fonts/lint.d.ts +51 -0
  45. package/dist/fonts/lint.js +101 -16
  46. package/dist/fonts/lint.js.map +1 -1
  47. package/dist/fonts/walk.d.ts +13 -0
  48. package/dist/fonts/walk.js +50 -33
  49. package/dist/fonts/walk.js.map +1 -1
  50. package/dist/html/css.d.ts +4 -0
  51. package/dist/html/css.js +11 -3
  52. package/dist/html/css.js.map +1 -1
  53. package/dist/html/dom.d.ts +6 -1
  54. package/dist/html/dom.js +28 -6
  55. package/dist/html/dom.js.map +1 -1
  56. package/dist/html/generated.d.ts +17 -0
  57. package/dist/html/generated.js +21 -0
  58. package/dist/html/generated.js.map +1 -0
  59. package/dist/html/images.d.ts +18 -0
  60. package/dist/html/images.js +49 -2
  61. package/dist/html/images.js.map +1 -1
  62. package/dist/html/index.d.ts +1 -0
  63. package/dist/html/index.js +1 -0
  64. package/dist/html/index.js.map +1 -1
  65. package/dist/html/render.d.ts +5 -4
  66. package/dist/html/render.js +24 -6
  67. package/dist/html/render.js.map +1 -1
  68. package/dist/hyphenation/exceptions.d.ts +5 -0
  69. package/dist/hyphenation/exceptions.js +11 -0
  70. package/dist/hyphenation/exceptions.js.map +1 -1
  71. package/dist/hyphenation/index.d.ts +1 -1
  72. package/dist/hyphenation/index.js +1 -1
  73. package/dist/hyphenation/index.js.map +1 -1
  74. package/dist/import/convert.d.ts +21 -0
  75. package/dist/import/convert.js +65 -0
  76. package/dist/import/convert.js.map +1 -0
  77. package/dist/incremental/cache.d.ts +59 -0
  78. package/dist/incremental/cache.js +64 -0
  79. package/dist/incremental/cache.js.map +1 -0
  80. package/dist/incremental/index.d.ts +3 -0
  81. package/dist/incremental/index.js +4 -0
  82. package/dist/incremental/index.js.map +1 -0
  83. package/dist/incremental/keys.d.ts +27 -0
  84. package/dist/incremental/keys.js +89 -0
  85. package/dist/incremental/keys.js.map +1 -0
  86. package/dist/incremental/session.d.ts +180 -0
  87. package/dist/incremental/session.js +401 -0
  88. package/dist/incremental/session.js.map +1 -0
  89. package/dist/index.d.ts +6 -0
  90. package/dist/index.js +6 -0
  91. package/dist/index.js.map +1 -1
  92. package/dist/indexes/layout.d.ts +8 -2
  93. package/dist/indexes/layout.js +6 -4
  94. package/dist/indexes/layout.js.map +1 -1
  95. package/dist/layout/cache.d.ts +174 -0
  96. package/dist/layout/cache.js +150 -0
  97. package/dist/layout/cache.js.map +1 -0
  98. package/dist/layout/converge.d.ts +28 -0
  99. package/dist/layout/converge.js +102 -9
  100. package/dist/layout/converge.js.map +1 -1
  101. package/dist/layout/counters.d.ts +4 -0
  102. package/dist/layout/counters.js +15 -0
  103. package/dist/layout/counters.js.map +1 -1
  104. package/dist/layout/index.d.ts +5 -2
  105. package/dist/layout/index.js +3 -0
  106. package/dist/layout/index.js.map +1 -1
  107. package/dist/layout/lists.d.ts +52 -3
  108. package/dist/layout/lists.js +513 -46
  109. package/dist/layout/lists.js.map +1 -1
  110. package/dist/layout/lookahead.d.ts +151 -0
  111. package/dist/layout/lookahead.js +581 -0
  112. package/dist/layout/lookahead.js.map +1 -0
  113. package/dist/layout/margin-boxes.d.ts +11 -1
  114. package/dist/layout/margin-boxes.js +24 -1
  115. package/dist/layout/margin-boxes.js.map +1 -1
  116. package/dist/layout/page-rules.d.ts +6 -1
  117. package/dist/layout/page-rules.js +42 -1
  118. package/dist/layout/page-rules.js.map +1 -1
  119. package/dist/layout/page.js +4 -2
  120. package/dist/layout/page.js.map +1 -1
  121. package/dist/layout/pagebuilder.d.ts +248 -3
  122. package/dist/layout/pagebuilder.js +512 -19
  123. package/dist/layout/pagebuilder.js.map +1 -1
  124. package/dist/layout/pages.d.ts +108 -1
  125. package/dist/layout/pages.js +1362 -159
  126. package/dist/layout/pages.js.map +1 -1
  127. package/dist/layout/paragraph.d.ts +14 -0
  128. package/dist/layout/paragraph.js +304 -31
  129. package/dist/layout/paragraph.js.map +1 -1
  130. package/dist/layout/pre.js +4 -0
  131. package/dist/layout/pre.js.map +1 -1
  132. package/dist/layout/protrusion.d.ts +26 -0
  133. package/dist/layout/protrusion.js +39 -0
  134. package/dist/layout/protrusion.js.map +1 -0
  135. package/dist/layout/ranges.d.ts +2 -0
  136. package/dist/layout/ranges.js +22 -0
  137. package/dist/layout/ranges.js.map +1 -0
  138. package/dist/layout/region.d.ts +2 -1
  139. package/dist/layout/region.js +9 -5
  140. package/dist/layout/region.js.map +1 -1
  141. package/dist/layout/templates.d.ts +71 -0
  142. package/dist/layout/templates.js +290 -0
  143. package/dist/layout/templates.js.map +1 -0
  144. package/dist/layout/vlist.d.ts +3 -3
  145. package/dist/linebreak/items.d.ts +27 -2
  146. package/dist/linebreak/items.js +10 -3
  147. package/dist/linebreak/items.js.map +1 -1
  148. package/dist/linebreak/knuth-plass.d.ts +20 -0
  149. package/dist/linebreak/knuth-plass.js +68 -16
  150. package/dist/linebreak/knuth-plass.js.map +1 -1
  151. package/dist/lint/book.d.ts +13 -0
  152. package/dist/lint/book.js +52 -0
  153. package/dist/lint/book.js.map +1 -0
  154. package/dist/lint/config.d.ts +9 -0
  155. package/dist/lint/config.js +40 -6
  156. package/dist/lint/config.js.map +1 -1
  157. package/dist/lint/linter.js +1 -0
  158. package/dist/lint/linter.js.map +1 -1
  159. package/dist/lint/project.js +1 -1
  160. package/dist/lint/project.js.map +1 -1
  161. package/dist/lint/rules/layout.d.ts +3 -1
  162. package/dist/lint/rules/layout.js +8 -6
  163. package/dist/lint/rules/layout.js.map +1 -1
  164. package/dist/lint/rules/source.js +1 -1
  165. package/dist/lint/rules/source.js.map +1 -1
  166. package/dist/lint/testing.js +1 -0
  167. package/dist/lint/testing.js.map +1 -1
  168. package/dist/measure/bidi-data.d.ts +14 -0
  169. package/dist/measure/bidi-data.js +69 -0
  170. package/dist/measure/bidi-data.js.map +1 -0
  171. package/dist/measure/bidi.d.ts +70 -0
  172. package/dist/measure/bidi.js +225 -0
  173. package/dist/measure/bidi.js.map +1 -0
  174. package/dist/measure/css.d.ts +5 -0
  175. package/dist/measure/css.js +2 -0
  176. package/dist/measure/css.js.map +1 -1
  177. package/dist/measure/measurer.d.ts +36 -2
  178. package/dist/measure/measurer.js +220 -22
  179. package/dist/measure/measurer.js.map +1 -1
  180. package/dist/measure/model.d.ts +20 -2
  181. package/dist/measure/model.js +13 -6
  182. package/dist/measure/model.js.map +1 -1
  183. package/dist/measure/script-data.d.ts +6 -0
  184. package/dist/measure/script-data.js +34 -0
  185. package/dist/measure/script-data.js.map +1 -0
  186. package/dist/measure/scripts.d.ts +36 -0
  187. package/dist/measure/scripts.js +293 -0
  188. package/dist/measure/scripts.js.map +1 -0
  189. package/dist/output/output.d.ts +35 -3
  190. package/dist/output/output.js +26 -4
  191. package/dist/output/output.js.map +1 -1
  192. package/dist/paint/backgrounds.js +7 -20
  193. package/dist/paint/backgrounds.js.map +1 -1
  194. package/dist/paint/blend.d.ts +33 -13
  195. package/dist/paint/blend.js +73 -21
  196. package/dist/paint/blend.js.map +1 -1
  197. package/dist/paint/cache.d.ts +80 -0
  198. package/dist/paint/cache.js +537 -0
  199. package/dist/paint/cache.js.map +1 -0
  200. package/dist/paint/canvas.d.ts +4 -13
  201. package/dist/paint/canvas.js +45 -5
  202. package/dist/paint/canvas.js.map +1 -1
  203. package/dist/paint/context.d.ts +2 -0
  204. package/dist/paint/emphasis.js +1 -1
  205. package/dist/paint/emphasis.js.map +1 -1
  206. package/dist/paint/flatten.d.ts +24 -2
  207. package/dist/paint/flatten.js +55 -12
  208. package/dist/paint/flatten.js.map +1 -1
  209. package/dist/paint/forms.d.ts +64 -0
  210. package/dist/paint/forms.js +319 -0
  211. package/dist/paint/forms.js.map +1 -0
  212. package/dist/paint/images.d.ts +4 -1
  213. package/dist/paint/images.js +13 -2
  214. package/dist/paint/images.js.map +1 -1
  215. package/dist/paint/layers.d.ts +64 -0
  216. package/dist/paint/layers.js +137 -0
  217. package/dist/paint/layers.js.map +1 -0
  218. package/dist/paint/painter.d.ts +19 -6
  219. package/dist/paint/painter.js +15 -135
  220. package/dist/paint/painter.js.map +1 -1
  221. package/dist/paint/stacking.d.ts +1 -1
  222. package/dist/paint/stacking.js +9 -2
  223. package/dist/paint/stacking.js.map +1 -1
  224. package/dist/paint/text.d.ts +14 -3
  225. package/dist/paint/text.js +25 -4
  226. package/dist/paint/text.js.map +1 -1
  227. package/dist/paint/vertical.js +2 -2
  228. package/dist/paint/vertical.js.map +1 -1
  229. package/dist/pdf/encoded.d.ts +16 -0
  230. package/dist/pdf/encoded.js +35 -0
  231. package/dist/pdf/encoded.js.map +1 -0
  232. package/dist/pdf/include/colour.d.ts +105 -0
  233. package/dist/pdf/include/colour.js +349 -0
  234. package/dist/pdf/include/colour.js.map +1 -0
  235. package/dist/pdf/include/functions.d.ts +17 -0
  236. package/dist/pdf/include/functions.js +389 -0
  237. package/dist/pdf/include/functions.js.map +1 -0
  238. package/dist/pdf/include/pages.d.ts +14 -0
  239. package/dist/pdf/include/pages.js +27 -0
  240. package/dist/pdf/include/pages.js.map +1 -0
  241. package/dist/pdf/include/pdfjs/LICENSE +177 -0
  242. package/dist/pdf/include/pdfjs/pdf.min.mjs +27 -0
  243. package/dist/pdf/include/pdfjs/pdf.worker.min.mjs +27 -0
  244. package/dist/pdf/include/pdfjs/standard_fonts/FoxitDingbats.pfb +0 -0
  245. package/dist/pdf/include/pdfjs/standard_fonts/FoxitFixed.pfb +0 -0
  246. package/dist/pdf/include/pdfjs/standard_fonts/FoxitFixedBold.pfb +0 -0
  247. package/dist/pdf/include/pdfjs/standard_fonts/FoxitFixedBoldItalic.pfb +0 -0
  248. package/dist/pdf/include/pdfjs/standard_fonts/FoxitFixedItalic.pfb +0 -0
  249. package/dist/pdf/include/pdfjs/standard_fonts/FoxitSerif.pfb +0 -0
  250. package/dist/pdf/include/pdfjs/standard_fonts/FoxitSerifBold.pfb +0 -0
  251. package/dist/pdf/include/pdfjs/standard_fonts/FoxitSerifBoldItalic.pfb +0 -0
  252. package/dist/pdf/include/pdfjs/standard_fonts/FoxitSerifItalic.pfb +0 -0
  253. package/dist/pdf/include/pdfjs/standard_fonts/FoxitSymbol.pfb +0 -0
  254. package/dist/pdf/include/pdfjs/standard_fonts/LICENSE_FOXIT +27 -0
  255. package/dist/pdf/include/pdfjs/standard_fonts/LICENSE_LIBERATION +363 -0
  256. package/dist/pdf/include/pdfjs/standard_fonts/LiberationSans-Bold.ttf +0 -0
  257. package/dist/pdf/include/pdfjs/standard_fonts/LiberationSans-BoldItalic.ttf +0 -0
  258. package/dist/pdf/include/pdfjs/standard_fonts/LiberationSans-Italic.ttf +0 -0
  259. package/dist/pdf/include/pdfjs/standard_fonts/LiberationSans-Regular.ttf +0 -0
  260. package/dist/pdf/include/pdfjs/wasm/LICENSE_JBIG2 +196 -0
  261. package/dist/pdf/include/pdfjs/wasm/LICENSE_OPENJPEG +39 -0
  262. package/dist/pdf/include/pdfjs/wasm/LICENSE_PDFJS_JBIG2 +13 -0
  263. package/dist/pdf/include/pdfjs/wasm/LICENSE_PDFJS_OPENJPEG +22 -0
  264. package/dist/pdf/include/pdfjs/wasm/LICENSE_PDFJS_QCMS +19 -0
  265. package/dist/pdf/include/pdfjs/wasm/LICENSE_QCMS +21 -0
  266. package/dist/pdf/include/pdfjs/wasm/jbig2.wasm +0 -0
  267. package/dist/pdf/include/pdfjs/wasm/openjpeg.wasm +0 -0
  268. package/dist/pdf/include/pdfjs/wasm/qcms_bg.wasm +0 -0
  269. package/dist/pdf/include/pdfjs/wasm/quickjs-eval.wasm +0 -0
  270. package/dist/pdf/include/prepare.d.ts +58 -0
  271. package/dist/pdf/include/prepare.js +266 -0
  272. package/dist/pdf/include/prepare.js.map +1 -0
  273. package/dist/pdf/include/preview.d.ts +38 -0
  274. package/dist/pdf/include/preview.js +101 -0
  275. package/dist/pdf/include/preview.js.map +1 -0
  276. package/dist/pdf/include/rewrite.d.ts +57 -0
  277. package/dist/pdf/include/rewrite.js +803 -0
  278. package/dist/pdf/include/rewrite.js.map +1 -0
  279. package/dist/pdf/include/serialize.d.ts +32 -0
  280. package/dist/pdf/include/serialize.js +158 -0
  281. package/dist/pdf/include/serialize.js.map +1 -0
  282. package/dist/pdf/include/testing.d.ts +26 -0
  283. package/dist/pdf/include/testing.js +50 -0
  284. package/dist/pdf/include/testing.js.map +1 -0
  285. package/dist/pdf/index.d.ts +1 -0
  286. package/dist/pdf/index.js +1 -0
  287. package/dist/pdf/index.js.map +1 -1
  288. package/dist/pdf/structure/annotations.d.ts +33 -0
  289. package/dist/pdf/structure/annotations.js +112 -0
  290. package/dist/pdf/structure/annotations.js.map +1 -0
  291. package/dist/pdf/structure/forms.d.ts +10 -0
  292. package/dist/pdf/structure/forms.js +46 -0
  293. package/dist/pdf/structure/forms.js.map +1 -0
  294. package/dist/pdf/structure/index.d.ts +41 -5
  295. package/dist/pdf/structure/index.js +40 -5
  296. package/dist/pdf/structure/index.js.map +1 -1
  297. package/dist/pdf/structure/links.d.ts +2 -0
  298. package/dist/pdf/structure/links.js +1 -1
  299. package/dist/pdf/structure/links.js.map +1 -1
  300. package/dist/pdf/structure/presentation.d.ts +28 -0
  301. package/dist/pdf/structure/presentation.js +79 -0
  302. package/dist/pdf/structure/presentation.js.map +1 -0
  303. package/dist/pdf/structure/tags.d.ts +34 -2
  304. package/dist/pdf/structure/tags.js +117 -14
  305. package/dist/pdf/structure/tags.js.map +1 -1
  306. package/dist/pdf/types.d.ts +357 -11
  307. package/dist/pdf/wasm/pulpweaver4_pdf.d.ts +44 -2
  308. package/dist/pdf/wasm/pulpweaver4_pdf.js +191 -2
  309. package/dist/pdf/wasm/pulpweaver4_pdf_bg.wasm +0 -0
  310. package/dist/pdf/writer.d.ts +13 -1
  311. package/dist/pdf/writer.js +26 -1
  312. package/dist/pdf/writer.js.map +1 -1
  313. package/dist/plugin/define.d.ts +200 -15
  314. package/dist/plugin/define.js +124 -15
  315. package/dist/plugin/define.js.map +1 -1
  316. package/dist/plugin/failure.d.ts +1 -1
  317. package/dist/plugin/index.d.ts +5 -4
  318. package/dist/plugin/index.js +5 -4
  319. package/dist/plugin/index.js.map +1 -1
  320. package/dist/plugin/reference.d.ts +38 -0
  321. package/dist/plugin/reference.js +287 -0
  322. package/dist/plugin/reference.js.map +1 -0
  323. package/dist/plugin/register.d.ts +39 -7
  324. package/dist/plugin/register.js +83 -2
  325. package/dist/plugin/register.js.map +1 -1
  326. package/dist/preflight/index.d.ts +1 -1
  327. package/dist/preflight/index.js +1 -1
  328. package/dist/preflight/index.js.map +1 -1
  329. package/dist/preflight/pdfx.d.ts +35 -0
  330. package/dist/preflight/pdfx.js +33 -2
  331. package/dist/preflight/pdfx.js.map +1 -1
  332. package/dist/preflight/reader.d.ts +10 -3
  333. package/dist/preflight/reader.js +86 -5
  334. package/dist/preflight/reader.js.map +1 -1
  335. package/dist/print/impose.d.ts +66 -0
  336. package/dist/print/impose.js +331 -0
  337. package/dist/print/impose.js.map +1 -0
  338. package/dist/print/output.js +400 -50
  339. package/dist/print/output.js.map +1 -1
  340. package/dist/problem.js +4 -3
  341. package/dist/problem.js.map +1 -1
  342. package/dist/profile/index.d.ts +1 -0
  343. package/dist/profile/index.js +1 -0
  344. package/dist/profile/index.js.map +1 -1
  345. package/dist/profile/lint.d.ts +1 -1
  346. package/dist/profile/lint.js +29 -1
  347. package/dist/profile/lint.js.map +1 -1
  348. package/dist/profile/list-table.d.ts +51 -0
  349. package/dist/profile/list-table.js +137 -0
  350. package/dist/profile/list-table.js.map +1 -0
  351. package/dist/profile/metadata.js +3 -0
  352. package/dist/profile/metadata.js.map +1 -1
  353. package/dist/profile/profile.d.ts +3 -0
  354. package/dist/profile/profile.js +9 -1
  355. package/dist/profile/profile.js.map +1 -1
  356. package/dist/project.js +3 -0
  357. package/dist/project.js.map +1 -1
  358. package/dist/qpdf/index.d.ts +2 -0
  359. package/dist/qpdf/index.js +3 -0
  360. package/dist/qpdf/index.js.map +1 -0
  361. package/dist/qpdf/qpdf.d.ts +125 -0
  362. package/dist/qpdf/qpdf.js +205 -0
  363. package/dist/qpdf/qpdf.js.map +1 -0
  364. package/dist/qpdf/testing.d.ts +12 -0
  365. package/dist/qpdf/testing.js +9 -0
  366. package/dist/qpdf/testing.js.map +1 -0
  367. package/dist/qpdf/wasm/LICENSE-qpdf +202 -0
  368. package/dist/qpdf/wasm/LICENSE-zlib +22 -0
  369. package/dist/qpdf/wasm/NOTICE-qpdf.md +42 -0
  370. package/dist/qpdf/wasm/qpdf.wasm +0 -0
  371. package/dist/render/index.d.ts +3 -0
  372. package/dist/render/index.js +2 -0
  373. package/dist/render/index.js.map +1 -0
  374. package/dist/render/render.d.ts +98 -0
  375. package/dist/render/render.js +243 -0
  376. package/dist/render/render.js.map +1 -0
  377. package/dist/reproducible/clock.d.ts +4 -5
  378. package/dist/reproducible/clock.js +4 -5
  379. package/dist/reproducible/clock.js.map +1 -1
  380. package/dist/spike/spike-page.chromium.json +4 -4
  381. package/dist/style/colours.d.ts +7 -2
  382. package/dist/style/colours.js +24 -2
  383. package/dist/style/colours.js.map +1 -1
  384. package/dist/style/compile.js +1 -1
  385. package/dist/style/compile.js.map +1 -1
  386. package/dist/style/defaults.d.ts +9 -3
  387. package/dist/style/defaults.js +14 -2
  388. package/dist/style/defaults.js.map +1 -1
  389. package/dist/style/gcpm.d.ts +1 -1
  390. package/dist/style/gcpm.js +17 -5
  391. package/dist/style/gcpm.js.map +1 -1
  392. package/dist/style/properties.js +84 -0
  393. package/dist/style/properties.js.map +1 -1
  394. package/dist/test/book.d.ts +22 -2
  395. package/dist/test/book.js +35 -2
  396. package/dist/test/book.js.map +1 -1
  397. package/dist/test/build.d.ts +23 -0
  398. package/dist/test/build.js +86 -33
  399. package/dist/test/build.js.map +1 -1
  400. package/dist/test/layout.js +128 -19
  401. package/dist/test/layout.js.map +1 -1
  402. package/dist/test/limits.d.ts +8 -0
  403. package/dist/test/limits.js +9 -0
  404. package/dist/test/limits.js.map +1 -0
  405. package/dist/test/pipeline.d.ts +23 -0
  406. package/dist/test/pipeline.js.map +1 -1
  407. package/dist/test/render.d.ts +26 -3
  408. package/dist/test/render.js +151 -23
  409. package/dist/test/render.js.map +1 -1
  410. package/dist/test/runner.d.ts +6 -3
  411. package/dist/test/runner.js +172 -105
  412. package/dist/test/runner.js.map +1 -1
  413. package/dist/test/text.js +2 -2
  414. package/dist/test/text.js.map +1 -1
  415. package/dist/text/output.js +2 -2
  416. package/dist/text/output.js.map +1 -1
  417. package/dist/text/render.d.ts +4 -3
  418. package/dist/text/render.js +126 -4
  419. package/dist/text/render.js.map +1 -1
  420. package/dist/transform/index.d.ts +1 -1
  421. package/dist/transform/index.js +1 -1
  422. package/dist/transform/index.js.map +1 -1
  423. package/dist/transform/run.d.ts +2 -0
  424. package/dist/transform/run.js +32 -16
  425. package/dist/transform/run.js.map +1 -1
  426. package/dist/web/output.js +116 -10
  427. package/dist/web/output.js.map +1 -1
  428. package/dist/web/runtime/justify.js.txt +2 -2
  429. package/dist/web/site.d.ts +2 -0
  430. package/dist/web/site.js +8 -1
  431. package/dist/web/site.js.map +1 -1
  432. package/docs/README.md +6 -1
  433. package/docs/manual/chapters/front.dj +34 -0
  434. package/docs/manual/chapters/guides/citations.dj +426 -0
  435. package/docs/manual/chapters/guides/colour-and-prepress.dj +261 -0
  436. package/docs/manual/chapters/guides/editions.dj +329 -0
  437. package/docs/manual/chapters/guides/in-a-web-page.dj +143 -0
  438. package/docs/manual/chapters/guides/lines-and-pages.dj +681 -0
  439. package/docs/manual/chapters/guides/lint-rules.dj +355 -0
  440. package/docs/manual/chapters/guides/pdfs.dj +501 -0
  441. package/docs/manual/chapters/guides/plugins.dj +425 -0
  442. package/docs/manual/chapters/guides/testing.dj +264 -0
  443. package/docs/manual/chapters/guides/writing-in-djot.dj +537 -0
  444. package/docs/manual/chapters/start/for-latex-users.dj +256 -0
  445. package/docs/manual/chapters/start/getting-started.dj +167 -0
  446. package/docs/manual/chapters/start/your-first-book.dj +426 -0
  447. package/docs/manual/fonts/CrimsonPro-Italic-VF.ttf +0 -0
  448. package/docs/manual/fonts/CrimsonPro-VF.ttf +0 -0
  449. package/docs/manual/fonts/JetBrainsMono-Regular.otf +0 -0
  450. package/docs/manual/fonts/NotoSansSymbols2-Boxes.ttf +0 -0
  451. package/docs/manual/fonts/OFL.txt +95 -0
  452. package/docs/manual/pulpweaver4.toml +39 -0
  453. package/docs/manual/styles/manual.css +137 -0
  454. package/docs/reference/css-properties/index.md +9 -0
  455. package/docs/reference/css-properties/pw-expansion.md +31 -0
  456. package/docs/reference/css-properties/pw-flow-into.md +31 -0
  457. package/docs/reference/css-properties/pw-knockout.md +26 -0
  458. package/docs/reference/css-properties/pw-layer.md +24 -0
  459. package/docs/reference/css-properties/pw-page-breaking.md +30 -0
  460. package/docs/reference/css-properties/pw-page-duration.md +28 -0
  461. package/docs/reference/css-properties/pw-page-rotate.md +29 -0
  462. package/docs/reference/css-properties/pw-page-transition.md +29 -0
  463. package/docs/reference/css-properties/pw-protrusion.md +31 -0
  464. package/docs/reference/lint-rules/ibid-across-page.md +2 -2
  465. package/docs/reference/plugin-api/capabilities.md +2 -2
  466. package/docs/reference/plugin-api/claim-kind.md +1 -1
  467. package/docs/reference/plugin-api/combine-exceptions.md +19 -0
  468. package/docs/reference/plugin-api/element-break-context.md +34 -0
  469. package/docs/reference/plugin-api/element-break-item.md +24 -0
  470. package/docs/reference/plugin-api/element-breaker.md +24 -0
  471. package/docs/reference/plugin-api/flow-definition.md +30 -0
  472. package/docs/reference/plugin-api/generated-reference.md +32 -0
  473. package/docs/reference/plugin-api/index-definition.md +32 -0
  474. package/docs/reference/plugin-api/index-titles.md +18 -0
  475. package/docs/reference/plugin-api/index.md +27 -3
  476. package/docs/reference/plugin-api/insert-class-definition.md +46 -0
  477. package/docs/reference/plugin-api/insert-placement.md +22 -0
  478. package/docs/reference/plugin-api/insert-placements.md +18 -0
  479. package/docs/reference/plugin-api/layout-definition.md +9 -5
  480. package/docs/reference/plugin-api/layout-slot.md +6 -16
  481. package/docs/reference/plugin-api/lint-context.md +3 -1
  482. package/docs/reference/plugin-api/lint-definition.md +1 -1
  483. package/docs/reference/plugin-api/lint-edition.md +2 -0
  484. package/docs/reference/plugin-api/page-builder-event.md +22 -0
  485. package/docs/reference/plugin-api/page-builder-hook.md +7 -3
  486. package/docs/reference/plugin-api/page-builder-page-event.md +30 -0
  487. package/docs/reference/plugin-api/page-template-definition.md +34 -0
  488. package/docs/reference/plugin-api/page-template-region-definition.md +46 -0
  489. package/docs/reference/plugin-api/plugin-definition.md +5 -1
  490. package/docs/reference/plugin-api/plugin-reference-options.md +32 -0
  491. package/docs/reference/plugin-api/plugin-reference.md +22 -0
  492. package/docs/reference/plugin-api/plugin-registry.md +15 -5
  493. package/docs/reference/plugin-api/plugin-stage.md +1 -1
  494. package/docs/reference/plugin-api/plugin.md +9 -2
  495. package/docs/reference/plugin-api/reference-page.md +34 -0
  496. package/docs/reference/plugin-api/render-reference-page.md +18 -0
  497. package/docs/reference/plugin-api/renderers.md +1 -1
  498. package/docs/reference/pulpweaver4-toml/editions.md +239 -2
  499. package/docs/reference/pulpweaver4-toml/index.md +44 -1
  500. package/docs/reference/pulpweaver4-toml/inks.md +15 -4
  501. package/docs/reference/pulpweaver4-toml/lint.md +1 -1
  502. package/package.json +5 -1
@@ -0,0 +1,803 @@
1
+ /**
2
+ * An included PDF rewritten for an edition: the pages a book draws, copied
3
+ * into a file of their own with what the edition needs changed, which
4
+ * krilla then embeds as it is. Every object the pages reach is copied,
5
+ * renumbered; content streams are read op by op and written again, so that
6
+ * colours can be converted (`colour.ts`) and marked content taken out; and
7
+ * images, shadings, patterns, forms, soft masks and Type 3 glyphs are
8
+ * followed into, each converted once.
9
+ *
10
+ * - **Colour**: what the edition's target replaces (RGB, ICC-based and Lab
11
+ * colour in a CMYK edition) is converted wherever it is used: colour
12
+ * operators, images' samples (decoded, converted, written 8-bit under
13
+ * Flate), shadings' functions (sampled) and mesh vertices, Indexed
14
+ * lookup tables, Separation and DeviceN alternates, transparency groups'
15
+ * blending spaces and soft masks' backdrops. An image under a filter the
16
+ * core can't decode (JPEG 2000, JBIG2, CCITT) keeps its colour, and the
17
+ * standard the edition claims may then refuse it.
18
+ * - **Tags**: marked content other than optional content's (`/OC`) comes
19
+ * out: the book's own structure tree tags the figure, and the included
20
+ * PDF's marked-content ids and artifacts mean nothing there.
21
+ * - **Layers** stay (#168): the catalog's `/OCProperties` is copied, which
22
+ * says which groups start hidden, and a form or image XObject's own `/OC`
23
+ * becomes marked content around each `Do` that draws it (`/OC /PwOC…
24
+ * BDC`, the group in the resources' `/Properties`), which is what krilla
25
+ * carries into the book (patch 0016); a dictionary's `/OC` it would drop.
26
+ * - **The page's transparency group** comes out on request, for standards
27
+ * that refuse one on a form XObject, which the page becomes.
28
+ * - **`/Interpolate`** comes off images on request: PDF/A and PDF/X forbid it.
29
+ */
30
+ import { PdfDict, PdfFilterError, PdfRef, PdfStream, PdfString, contentOps } from '../../preflight/reader.js';
31
+ import { ColourSpaceError, sampledFunction } from './colour.js';
32
+ import { PdfFunctionError, readFunction } from './functions.js';
33
+ import { ContentWriter, flateStream, serializePdf } from './serialize.js';
34
+ /**
35
+ * Copies `pages` (from 1) of `pdf` into a file of their own, rewritten as
36
+ * `options` say. `decodeJpeg` decodes JPEG images whose colour needs
37
+ * converting; by default the browser's (`createImageBitmap`).
38
+ */
39
+ export async function rewritePdf(pdf, pages, options, decodeJpeg = browserJpeg) {
40
+ return new Rewriter(pdf, options, decodeJpeg).run(pages);
41
+ }
42
+ class Rewriter {
43
+ pdf;
44
+ options;
45
+ decodeJpeg;
46
+ objects = [];
47
+ /** Source objects (dictionaries, streams, arrays) and what each became. */
48
+ done = new Map();
49
+ unconverted = new Set();
50
+ /** Work left: objects allocated, filled once their content is converted. */
51
+ pending = [];
52
+ /** The resources of the page being copied, which a form without its own uses. */
53
+ inherited;
54
+ constructor(pdf, options, decodeJpeg) {
55
+ this.pdf = pdf;
56
+ this.options = options;
57
+ this.decodeJpeg = decodeJpeg;
58
+ }
59
+ async run(numbers) {
60
+ const all = this.pdf.pages();
61
+ const catalog = this.alloc();
62
+ const tree = this.alloc();
63
+ const kids = [];
64
+ for (const n of numbers) {
65
+ const page = all[n - 1];
66
+ if (!page)
67
+ throw new Error(`the PDF has no page ${n}`);
68
+ kids.push(this.page(page, tree));
69
+ }
70
+ this.set(tree, new PdfDict([['Type', 'Pages'], ['Kids', kids], ['Count', kids.length]]));
71
+ const root = new PdfDict([['Type', 'Catalog'], ['Pages', tree]]);
72
+ // Which groups start hidden, and their order: what krilla lists in the book's configuration.
73
+ const oc = this.pdf.catalog?.get('OCProperties');
74
+ if (oc !== undefined)
75
+ root.set('OCProperties', this.copy(oc));
76
+ this.set(catalog, root);
77
+ while (this.pending.length)
78
+ await Promise.all(this.pending.splice(0));
79
+ return { data: serializePdf(this.objects, catalog, this.options.version), unconverted: [...this.unconverted] };
80
+ }
81
+ alloc() {
82
+ this.objects.push(null);
83
+ return new PdfRef(this.objects.length, 0);
84
+ }
85
+ set(ref, v) {
86
+ this.objects[ref.num - 1] = v;
87
+ }
88
+ /** An object written once as `make` writes it, behind a new reference if the source was one. */
89
+ once(source, make) {
90
+ const v = this.pdf.resolve(source);
91
+ if (v === null || typeof v !== 'object' || v instanceof PdfString)
92
+ return this.copy(v);
93
+ const known = this.done.get(v);
94
+ if (known !== undefined)
95
+ return known;
96
+ if (!(source instanceof PdfRef)) {
97
+ const made = make(v);
98
+ if (made instanceof Promise)
99
+ throw new Error('rewrite: a direct object converted asynchronously');
100
+ this.done.set(v, made);
101
+ return made;
102
+ }
103
+ const ref = this.alloc();
104
+ this.done.set(v, ref);
105
+ const made = make(v);
106
+ if (made instanceof Promise)
107
+ this.pending.push(made.then((m) => this.set(ref, m)));
108
+ else
109
+ this.set(ref, made);
110
+ return ref;
111
+ }
112
+ /**
113
+ * Any other object, copied as it is, its references followed. A stream
114
+ * that is no source object's (a function the conversion made) gets a
115
+ * reference of its own, as streams must be indirect.
116
+ */
117
+ copy(v) {
118
+ if (v instanceof PdfRef)
119
+ return this.once(v, (x) => this.copyValue(x));
120
+ if (v instanceof PdfStream) {
121
+ const known = this.done.get(v);
122
+ if (known !== undefined)
123
+ return known;
124
+ const ref = this.alloc();
125
+ this.done.set(v, ref);
126
+ this.set(ref, this.copyValue(v));
127
+ return ref;
128
+ }
129
+ return this.copyValue(v);
130
+ }
131
+ copyValue(v) {
132
+ if (v === undefined || v === null || typeof v !== 'object' || v instanceof PdfString || v instanceof PdfRef)
133
+ return v instanceof PdfRef ? this.copy(v) : (v ?? null);
134
+ if (Array.isArray(v))
135
+ return v.map((x) => this.copy(x));
136
+ if (v instanceof PdfStream)
137
+ return new PdfStream(this.copyDict(v.dict), v.raw);
138
+ return this.copyDict(v);
139
+ }
140
+ copyDict(d, skip = []) {
141
+ const out = new PdfDict();
142
+ for (const [k, x] of d)
143
+ if (!skip.includes(k) && k !== 'Length')
144
+ out.set(k, this.copy(x));
145
+ return out;
146
+ }
147
+ note(what, e) {
148
+ if (e instanceof ColourSpaceError || e instanceof PdfFunctionError || e instanceof PdfFilterError)
149
+ this.unconverted.add(`${what}: ${e.message}`);
150
+ else
151
+ throw e;
152
+ }
153
+ page(page, parent) {
154
+ const ref = this.alloc();
155
+ const d = new PdfDict([['Type', 'Page'], ['Parent', parent]]);
156
+ d.set('MediaBox', this.copy(page.mediaBox ?? [0, 0, 612, 792]));
157
+ if (page.cropBox !== null)
158
+ d.set('CropBox', this.copy(page.cropBox));
159
+ if (page.rotate)
160
+ d.set('Rotate', page.rotate);
161
+ const ctx = { resources: page.resources };
162
+ this.inherited = page.resources;
163
+ d.set('Resources', this.resources(page.resources));
164
+ const group = this.pdf.dict(page.dict.get('Group'));
165
+ if (group && !this.options.dropPageGroup)
166
+ d.set('Group', this.group(group));
167
+ const contents = this.pdf.resolve(page.dict.get('Contents'));
168
+ const streams = (Array.isArray(contents) ? contents : [contents]).map((c) => this.pdf.stream(c)).filter((s) => s !== undefined);
169
+ // A page's content streams are one stream, cut anywhere between tokens.
170
+ const parts = streams.flatMap((s) => [this.pdf.decode(s), Uint8Array.of(10)]);
171
+ const all = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
172
+ parts.reduce((at, p) => (all.set(p, at), at + p.length), 0);
173
+ const content = this.alloc();
174
+ d.set('Contents', content);
175
+ this.pending.push(this.content(all, ctx).then((data) => this.set(content, flateStream(new PdfDict(), data))));
176
+ this.set(ref, d);
177
+ return ref;
178
+ }
179
+ resources(obj) {
180
+ const res = this.pdf.dict(obj);
181
+ if (!res)
182
+ return new PdfDict();
183
+ return this.once(obj, () => {
184
+ const out = new PdfDict();
185
+ for (const [category, entries] of res) {
186
+ const dict = this.pdf.dict(entries);
187
+ if (!dict) {
188
+ out.set(category, this.copy(entries));
189
+ continue;
190
+ }
191
+ const each = (f) => {
192
+ const d = new PdfDict();
193
+ for (const [name, v] of dict)
194
+ d.set(name, f(v));
195
+ return d;
196
+ };
197
+ switch (category) {
198
+ case 'ColorSpace':
199
+ out.set(category, each((v) => this.colourSpace(v)));
200
+ break;
201
+ case 'XObject':
202
+ out.set(category, each((v) => this.xobject(v)));
203
+ break;
204
+ case 'Pattern':
205
+ out.set(category, each((v) => this.pattern(v)));
206
+ break;
207
+ case 'Shading':
208
+ out.set(category, each((v) => this.shading(v)));
209
+ break;
210
+ case 'ExtGState':
211
+ out.set(category, each((v) => this.extGState(v)));
212
+ break;
213
+ case 'Font':
214
+ out.set(category, each((v) => this.font(v)));
215
+ break;
216
+ default: out.set(category, this.copy(entries));
217
+ }
218
+ }
219
+ // Each XObject's own group, by the name its `Do` marks it with.
220
+ const xobjects = this.pdf.dict(res.get('XObject'));
221
+ const groups = [...(xobjects ?? [])].flatMap(([name, x]) => {
222
+ const oc = this.pdf.stream(x)?.dict.get('OC');
223
+ return oc === undefined ? [] : [[name, oc]];
224
+ });
225
+ if (groups.length) {
226
+ const properties = new PdfDict();
227
+ for (const [name, v] of this.pdf.dict(res.get('Properties')) ?? [])
228
+ properties.set(name, this.copy(v));
229
+ for (const [name, oc] of groups)
230
+ properties.set(ocName(name), this.copy(oc));
231
+ out.set('Properties', properties);
232
+ }
233
+ return out;
234
+ });
235
+ }
236
+ /** The colour space, as the target writes it. */
237
+ converted(obj) {
238
+ if (!this.options.colours)
239
+ return undefined;
240
+ try {
241
+ return this.options.colours.space(obj);
242
+ }
243
+ catch (e) {
244
+ this.note('a colour space', e);
245
+ return undefined;
246
+ }
247
+ }
248
+ colourSpace(obj) {
249
+ const c = this.converted(obj);
250
+ return c ? this.copy(c.space) : this.copy(obj);
251
+ }
252
+ group(g) {
253
+ const out = this.copyDict(g, ['CS']);
254
+ if (g.has('CS'))
255
+ out.set('CS', this.colourSpace(g.get('CS')));
256
+ return out;
257
+ }
258
+ xobject(obj) {
259
+ return this.once(obj, (v) => {
260
+ if (!(v instanceof PdfStream))
261
+ return this.copy(v);
262
+ const subtype = this.pdf.resolve(v.dict.get('Subtype'));
263
+ if (subtype === 'Image')
264
+ return this.image(v);
265
+ if (subtype === 'Form')
266
+ return this.form(v);
267
+ return this.copy(v);
268
+ });
269
+ }
270
+ /** A form XObject, a tiling pattern or a soft mask's group: its content rewritten among its own resources. */
271
+ async form(s, uncoloured = false) {
272
+ const d = this.copyDict(s.dict, ['Resources', 'Group', 'Filter', 'DecodeParms', 'OC']);
273
+ if (s.dict.has('Resources'))
274
+ d.set('Resources', this.resources(s.dict.get('Resources')));
275
+ const group = this.pdf.dict(s.dict.get('Group'));
276
+ if (group)
277
+ d.set('Group', this.group(group));
278
+ const resources = this.pdf.dict(s.dict.get('Resources')) ?? this.inherited;
279
+ return flateStream(d, await this.content(this.pdf.decode(s), { resources, uncoloured }));
280
+ }
281
+ async image(s) {
282
+ const d = this.copyDict(s.dict, ['SMask', 'Mask', 'Interpolate', 'OC']);
283
+ if (s.dict.has('Interpolate') && !this.options.noInterpolate)
284
+ d.set('Interpolate', this.copy(s.dict.get('Interpolate')));
285
+ const smask = s.dict.get('SMask');
286
+ if (smask !== undefined)
287
+ d.set('SMask', this.pdf.stream(smask) ? this.xobject(smask) : this.copy(smask));
288
+ const mask = s.dict.get('Mask');
289
+ if (mask !== undefined)
290
+ d.set('Mask', this.pdf.stream(mask) ? this.xobject(mask) : this.copy(mask));
291
+ if (this.pdf.resolve(s.dict.get('ImageMask')) === true || !s.dict.has('ColorSpace'))
292
+ return new PdfStream(d, s.raw);
293
+ const space = this.converted(s.dict.get('ColorSpace'));
294
+ if (!space)
295
+ return new PdfStream(d, s.raw);
296
+ d.set('ColorSpace', this.copy(space.space));
297
+ if (!space.replaced)
298
+ return new PdfStream(d, s.raw);
299
+ try {
300
+ const samples = await this.samples(s.dict, s.raw, space.from);
301
+ if (!samples) {
302
+ // Its colour stays as it is: say why.
303
+ this.unconverted.add(`an image under ${describe(this.pdf.resolve(s.dict.get('Filter')))}, which can’t be decoded here`);
304
+ return new PdfStream(this.copyDict(s.dict, ['Interpolate', 'OC']), s.raw);
305
+ }
306
+ d.delete('Decode');
307
+ d.set('BitsPerComponent', 8);
308
+ return flateStream(d, space.pixels(samples));
309
+ }
310
+ catch (e) {
311
+ this.note('an image', e);
312
+ return new PdfStream(this.copyDict(s.dict, ['Interpolate', 'OC']), s.raw);
313
+ }
314
+ }
315
+ /**
316
+ * An image's samples, decoded and scaled to 8 bits by its `/Decode`, `n`
317
+ * components per pixel; undefined under a filter the core can't undo
318
+ * (JPEG 2000, JBIG2, CCITT, and JPEG without a browser).
319
+ */
320
+ async samples(dict, raw, n, inline = false) {
321
+ const width = this.pdf.resolve(dict.get(inline ? 'W' : 'Width') ?? dict.get('Width'));
322
+ const height = this.pdf.resolve(dict.get(inline ? 'H' : 'Height') ?? dict.get('Height'));
323
+ const bpc = this.pdf.resolve(dict.get(inline ? 'BPC' : 'BitsPerComponent') ?? dict.get('BitsPerComponent')) ?? 8;
324
+ if (typeof width !== 'number' || typeof height !== 'number' || typeof bpc !== 'number')
325
+ throw new PdfFilterError('an image without its size');
326
+ const filter = this.pdf.resolve(dict.get(inline ? 'F' : 'Filter') ?? dict.get('Filter'));
327
+ const filters = (Array.isArray(filter) ? filter.map((f) => this.pdf.resolve(f)) : filter === null ? [] : [filter]);
328
+ const last = filters.at(-1);
329
+ let data;
330
+ if (last === 'DCTDecode' || last === 'DCT') {
331
+ const before = new PdfDict(dict);
332
+ before.set('Filter', filters.slice(0, -1).map((f) => EXPAND[f] ?? f));
333
+ before.delete('F');
334
+ before.delete('DecodeParms');
335
+ before.delete('DP');
336
+ const jpeg = this.pdf.decode(new PdfStream(before, raw));
337
+ const decoded = await this.decodeJpeg(jpeg, n);
338
+ if (!decoded)
339
+ return undefined;
340
+ // A JPEG's samples are 8-bit; an inverted /Decode (Adobe's CMYK) still applies.
341
+ return applyDecode(decoded, n, 8, this.decodeArray(dict, inline), n * width * height);
342
+ }
343
+ if (filters.some((f) => f === 'JPXDecode' || f === 'JBIG2Decode' || f === 'CCITTFaxDecode' || f === 'CCF'))
344
+ return undefined;
345
+ const expanded = new PdfDict(dict);
346
+ if (inline) {
347
+ expanded.set('Filter', filters.map((f) => EXPAND[f] ?? f));
348
+ if (dict.has('DP'))
349
+ expanded.set('DecodeParms', dict.get('DP'));
350
+ }
351
+ data = this.pdf.decode(new PdfStream(expanded, raw));
352
+ const rowBits = width * n * bpc;
353
+ const rowBytes = Math.ceil(rowBits / 8);
354
+ const out = new Uint8Array(width * height * n);
355
+ const max = 2 ** bpc - 1;
356
+ for (let y = 0; y < height; y++) {
357
+ for (let i = 0; i < width * n; i++) {
358
+ const bit = y * rowBytes * 8 + i * bpc;
359
+ let v = 0;
360
+ if (bpc === 8)
361
+ v = data[bit >> 3] ?? 0;
362
+ else if (bpc === 16)
363
+ v = ((data[bit >> 3] ?? 0) << 8) | (data[(bit >> 3) + 1] ?? 0);
364
+ else
365
+ for (let b = 0; b < bpc; b++)
366
+ v = v * 2 + (((data[(bit + b) >> 3] ?? 0) >> (7 - ((bit + b) & 7))) & 1);
367
+ out[y * width * n + i] = Math.round((v / max) * 255);
368
+ }
369
+ }
370
+ return applyDecode(out, n, 8, this.decodeArray(dict, inline), out.length);
371
+ }
372
+ decodeArray(dict, inline) {
373
+ const a = this.pdf.array(dict.get(inline ? 'D' : 'Decode') ?? dict.get('Decode'))?.map((x) => this.pdf.resolve(x));
374
+ return a?.every((x) => typeof x === 'number') ? a : undefined;
375
+ }
376
+ pattern(obj) {
377
+ return this.once(obj, (v) => {
378
+ if (v instanceof PdfStream) {
379
+ // A tiling pattern: an uncoloured one's colour comes from where it is used.
380
+ return this.form(v, this.pdf.resolve(v.dict.get('PaintType')) === 2);
381
+ }
382
+ if (!(v instanceof PdfDict))
383
+ return this.copy(v);
384
+ const out = this.copyDict(v, ['Shading', 'ExtGState']);
385
+ if (v.has('Shading'))
386
+ out.set('Shading', this.shading(v.get('Shading')));
387
+ if (v.has('ExtGState'))
388
+ out.set('ExtGState', this.extGState(v.get('ExtGState')));
389
+ return out;
390
+ });
391
+ }
392
+ shading(obj) {
393
+ return this.once(obj, (v) => {
394
+ const dict = v instanceof PdfStream ? v.dict : v instanceof PdfDict ? v : undefined;
395
+ if (!dict)
396
+ return this.copy(v);
397
+ const space = this.converted(dict.get('ColorSpace'));
398
+ if (!space)
399
+ return this.copy(v);
400
+ const out = this.copyDict(dict, ['ColorSpace', 'Function', 'Background', 'Decode', 'Filter', 'DecodeParms']);
401
+ out.set('ColorSpace', this.copy(space.space));
402
+ if (!space.replaced) {
403
+ for (const k of ['Function', 'Background', 'Decode', 'Filter', 'DecodeParms'])
404
+ if (dict.has(k))
405
+ out.set(k, this.copy(dict.get(k)));
406
+ return v instanceof PdfStream ? new PdfStream(out, v.raw) : out;
407
+ }
408
+ try {
409
+ const background = this.pdf.array(dict.get('Background'))?.map((x) => this.pdf.resolve(x));
410
+ if (background?.every((x) => typeof x === 'number'))
411
+ out.set('Background', space.convert(background));
412
+ const type = this.pdf.resolve(dict.get('ShadingType'));
413
+ if (dict.has('Function')) {
414
+ const f = readFunction(this.pdf, dict.get('Function'));
415
+ const domain = this.pdf.array(dict.get('Domain'))?.map((x) => this.pdf.resolve(x));
416
+ const inputs = type === 1 ? 2 : 1;
417
+ const ranges = type === 1
418
+ ? [[domain?.[0] ?? 0, domain?.[1] ?? 1], [domain?.[2] ?? 0, domain?.[3] ?? 1]]
419
+ : type === 2 || type === 3 ? [[domain?.[0] ?? 0, domain?.[1] ?? 1]] : [this.meshParameterRange(dict)];
420
+ out.set('Function', this.alloc2(sampledFunction(inputs, space.to, ranges, (x) => space.convert(f(x)))));
421
+ if (v instanceof PdfStream) {
422
+ for (const k of ['Decode', 'Filter', 'DecodeParms'])
423
+ if (dict.has(k))
424
+ out.set(k, this.copy(dict.get(k)));
425
+ return new PdfStream(out, v.raw);
426
+ }
427
+ return out;
428
+ }
429
+ if (!(v instanceof PdfStream) || typeof type !== 'number' || type < 4)
430
+ throw new ColourSpaceError(`a shading of type ${String(type)} without a function`);
431
+ const { data, decode } = this.mesh(v, type, space);
432
+ out.set('Decode', decode);
433
+ return flateStream(out, data);
434
+ }
435
+ catch (e) {
436
+ this.note('a shading', e);
437
+ return this.copy(v);
438
+ }
439
+ });
440
+ }
441
+ /** A mesh shading's parametric value's range: its /Decode's last pair. */
442
+ meshParameterRange(dict) {
443
+ const d = this.pdf.array(dict.get('Decode'))?.map((x) => this.pdf.resolve(x));
444
+ return [d?.[4] ?? 0, d?.[5] ?? 1];
445
+ }
446
+ /** A new object holding `v`. */
447
+ alloc2(v) {
448
+ const ref = this.alloc();
449
+ this.set(ref, v);
450
+ return ref;
451
+ }
452
+ /**
453
+ * A mesh shading's vertex colours converted (types 4 to 7 without a
454
+ * function): its data read vertex by vertex (each of a triangle mesh's on
455
+ * a byte boundary, as readers read them), flags and coordinates copied
456
+ * bit for bit, colours decoded, converted and written at the same depth
457
+ * with a /Decode of 0 to 1.
458
+ */
459
+ mesh(s, type, space) {
460
+ const d = s.dict;
461
+ const int = (k) => {
462
+ const x = this.pdf.resolve(d.get(k));
463
+ return typeof x === 'number' ? x : 0;
464
+ };
465
+ const bpf = int('BitsPerFlag');
466
+ const bpc = int('BitsPerCoordinate');
467
+ const bpk = int('BitsPerComponent');
468
+ const decode = (this.pdf.array(d.get('Decode'))?.map((x) => this.pdf.resolve(x)) ?? []);
469
+ const r = new BitReader(this.pdf.decode(s));
470
+ const w = new BitWriter();
471
+ const max = 2 ** bpk - 1;
472
+ const colour = () => {
473
+ const c = [];
474
+ for (let i = 0; i < space.from; i++) {
475
+ const lo = decode[4 + 2 * i] ?? 0;
476
+ const hi = decode[5 + 2 * i] ?? 1;
477
+ c.push(lo + (r.read(bpk) / max) * (hi - lo));
478
+ }
479
+ for (const x of space.convert(c))
480
+ w.write(Math.round(Math.min(1, Math.max(0, x)) * max), bpk);
481
+ };
482
+ const point = () => {
483
+ w.write(r.read(bpc), bpc);
484
+ w.write(r.read(bpc), bpc);
485
+ };
486
+ const vertex = (type === 4 ? bpf : 0) + 2 * bpc + space.from * bpk;
487
+ while (r.has(type === 4 || type === 5 ? vertex : bpf)) {
488
+ if (type === 4 || type === 5) {
489
+ if (type === 4)
490
+ w.write(r.read(bpf), bpf);
491
+ point();
492
+ colour();
493
+ r.align();
494
+ w.align();
495
+ }
496
+ else {
497
+ const flag = r.read(bpf);
498
+ // What is left after the last patch is padding.
499
+ if (flag > 3)
500
+ break;
501
+ w.write(flag, bpf);
502
+ const points = (type === 6 ? 12 : 16) - (flag === 0 ? 0 : 4);
503
+ for (let i = 0; i < points; i++)
504
+ point();
505
+ for (let i = 0; i < (flag === 0 ? 4 : 2); i++)
506
+ colour();
507
+ }
508
+ }
509
+ return { data: w.bytes(), decode: [...decode.slice(0, 4), ...Array.from({ length: space.to }, () => [0, 1]).flat()] };
510
+ }
511
+ extGState(obj) {
512
+ return this.once(obj, (v) => {
513
+ if (!(v instanceof PdfDict))
514
+ return this.copy(v);
515
+ const out = this.copyDict(v, ['SMask']);
516
+ const smask = this.pdf.resolve(v.get('SMask'));
517
+ if (smask instanceof PdfDict) {
518
+ const m = this.copyDict(smask, ['G', 'BC']);
519
+ const g = this.pdf.stream(smask.get('G'));
520
+ if (g)
521
+ m.set('G', this.once(smask.get('G'), () => this.form(g)));
522
+ const bc = this.pdf.array(smask.get('BC'))?.map((x) => this.pdf.resolve(x));
523
+ const cs = g && this.pdf.dict(g.dict.get('Group'))?.get('CS');
524
+ const space = cs !== undefined ? this.converted(cs) : undefined;
525
+ if (bc?.every((x) => typeof x === 'number'))
526
+ m.set('BC', space ? space.convert(bc) : bc);
527
+ out.set('SMask', m);
528
+ }
529
+ else if (v.has('SMask'))
530
+ out.set('SMask', this.copy(v.get('SMask')));
531
+ return out;
532
+ });
533
+ }
534
+ font(obj) {
535
+ return this.once(obj, (v) => {
536
+ if (!(v instanceof PdfDict) || this.pdf.resolve(v.get('Subtype')) !== 'Type3')
537
+ return this.copy(v);
538
+ // A Type 3 font's glyphs are content, among its own resources.
539
+ const out = this.copyDict(v, ['CharProcs', 'Resources']);
540
+ if (v.has('Resources'))
541
+ out.set('Resources', this.resources(v.get('Resources')));
542
+ const resources = this.pdf.dict(v.get('Resources'));
543
+ const procs = new PdfDict();
544
+ for (const [name, g] of this.pdf.dict(v.get('CharProcs')) ?? []) {
545
+ const s = this.pdf.stream(g);
546
+ procs.set(name, s ? this.once(g, async () => flateStream(this.copyDict(s.dict, ['Filter', 'DecodeParms']), await this.content(this.pdf.decode(s), { resources }))) : this.copy(g));
547
+ }
548
+ out.set('CharProcs', procs);
549
+ return out;
550
+ });
551
+ }
552
+ /** A content stream rewritten: colours converted where the target replaces their space, marked content taken out. */
553
+ async content(data, ctx) {
554
+ const out = new ContentWriter();
555
+ const named = (category, name) => (typeof name === 'string' ? this.pdf.dict(ctx.resources?.get(category))?.get(name) : undefined);
556
+ let state = { fill: undefined, stroke: undefined };
557
+ const stack = [];
558
+ const marks = [];
559
+ let uncoloured = ctx.uncoloured ?? false;
560
+ const set = (op, space) => {
561
+ if (op === op.toLowerCase())
562
+ state.fill = space;
563
+ else
564
+ state.stroke = space;
565
+ };
566
+ const device = (op, family, operands) => {
567
+ const space = uncoloured ? undefined : this.converted(family);
568
+ set(op, space);
569
+ if (!space?.replaced || !operands.every((x) => typeof x === 'number'))
570
+ return out.op(op, operands);
571
+ const lower = op === op.toLowerCase();
572
+ const c = space.convert(operands);
573
+ out.op(DEVICE_OPERATOR[space.space][lower ? 0 : 1], c);
574
+ };
575
+ for (const { op, operands, data: inline } of contentOps(data)) {
576
+ switch (op) {
577
+ case 'q':
578
+ stack.push(state);
579
+ state = { ...state };
580
+ out.op(op, operands);
581
+ break;
582
+ case 'Q':
583
+ state = stack.pop() ?? state;
584
+ out.op(op, operands);
585
+ break;
586
+ case 'd1':
587
+ uncoloured = true;
588
+ out.op(op, operands);
589
+ break;
590
+ case 'g':
591
+ case 'G':
592
+ device(op, 'DeviceGray', operands);
593
+ break;
594
+ case 'rg':
595
+ case 'RG':
596
+ device(op, 'DeviceRGB', operands);
597
+ break;
598
+ case 'k':
599
+ case 'K':
600
+ device(op, 'DeviceCMYK', operands);
601
+ break;
602
+ case 'cs':
603
+ case 'CS': {
604
+ const name = operands[0];
605
+ const family = typeof name === 'string' && /^(Device(Gray|RGB|CMYK)|Pattern)$/.test(name);
606
+ const space = uncoloured ? undefined : this.converted(family ? name : named('ColorSpace', name));
607
+ set(op, space);
608
+ // A family named in the operator is replaced in it; a resource's name stays, its definition rewritten.
609
+ out.op(op, family && space?.replaced ? [space.space] : operands);
610
+ break;
611
+ }
612
+ case 'sc':
613
+ case 'scn':
614
+ case 'SC':
615
+ case 'SCN': {
616
+ const space = op === op.toLowerCase() ? state.fill : state.stroke;
617
+ const pattern = typeof operands.at(-1) === 'string' ? operands.slice(-1) : [];
618
+ const values = operands.slice(0, operands.length - pattern.length);
619
+ if (space?.replaced && values.length === space.from && values.every((x) => typeof x === 'number'))
620
+ out.op(op, [...space.convert(values), ...pattern]);
621
+ else
622
+ out.op(op, operands);
623
+ break;
624
+ }
625
+ case 'BI':
626
+ await this.inlineImage(operands[0], inline ?? new Uint8Array(), ctx, out);
627
+ break;
628
+ case 'BMC':
629
+ case 'BDC':
630
+ // Optional content stays, with what it marks; tags and artifacts go.
631
+ marks.push(!this.options.untag || operands[0] === 'OC');
632
+ if (marks.at(-1))
633
+ out.op(op, operands);
634
+ break;
635
+ case 'EMC':
636
+ if (marks.pop() ?? true)
637
+ out.op(op, operands);
638
+ break;
639
+ case 'Do': {
640
+ // An XObject in a group of its own is drawn inside that group's marked content.
641
+ const name = operands[0];
642
+ const grouped = typeof name === 'string' && this.pdf.stream(named('XObject', name))?.dict.has('OC');
643
+ if (grouped)
644
+ out.op('BDC', ['OC', ocName(name)]);
645
+ out.op(op, operands);
646
+ if (grouped)
647
+ out.op('EMC', []);
648
+ break;
649
+ }
650
+ default:
651
+ out.op(op, operands);
652
+ }
653
+ }
654
+ return out.bytes();
655
+ }
656
+ async inlineImage(dict, data, ctx, out) {
657
+ if (!(dict instanceof PdfDict))
658
+ return;
659
+ const mask = dict.get('IM') === true || dict.get('ImageMask') === true;
660
+ const cs = dict.get('CS') ?? dict.get('ColorSpace');
661
+ const resource = typeof cs === 'string' && !(cs in INLINE_DEVICE) ? this.pdf.dict(ctx.resources?.get('ColorSpace'))?.get(cs) : undefined;
662
+ const space = mask || cs === undefined ? undefined : this.converted(resource ?? (typeof cs === 'string' ? (INLINE_DEVICE[cs] ?? cs) : cs));
663
+ const copy = new PdfDict(dict);
664
+ if (this.options.noInterpolate) {
665
+ copy.delete('I');
666
+ copy.delete('Interpolate');
667
+ }
668
+ if (!space?.replaced) {
669
+ // A resource's name stays, its definition rewritten; an indexed or other array written in the image is replaced.
670
+ if (space && resource === undefined && Array.isArray(cs)) {
671
+ copy.delete('ColorSpace');
672
+ copy.set('CS', space.space);
673
+ }
674
+ return out.inlineImage(copy, data);
675
+ }
676
+ try {
677
+ const samples = await this.samples(dict, data, space.from, true);
678
+ if (!samples) {
679
+ this.unconverted.add('an inline image under a filter that can’t be decoded here');
680
+ return out.inlineImage(copy, data);
681
+ }
682
+ const converted = space.pixels(samples);
683
+ const image = new PdfDict([['W', dict.get('W') ?? dict.get('Width')], ['H', dict.get('H') ?? dict.get('Height')], ['CS', INLINE_NAME[space.space]], ['BPC', 8], ['F', 'AHx']]);
684
+ out.inlineImage(image, asciiHex(converted));
685
+ }
686
+ catch (e) {
687
+ this.note('an inline image', e);
688
+ out.inlineImage(copy, data);
689
+ }
690
+ }
691
+ }
692
+ /** The `/Properties` name an XObject's own optional content group is given in its resources. */
693
+ function ocName(xobject) {
694
+ return `PwOC${xobject}`;
695
+ }
696
+ /** The operators that set a device space's colour, fill then stroke. */
697
+ const DEVICE_OPERATOR = { DeviceGray: ['g', 'G'], DeviceRGB: ['rg', 'RG'], DeviceCMYK: ['k', 'K'] };
698
+ /** Inline images' abbreviated device spaces, and the reverse. */
699
+ const INLINE_DEVICE = { G: 'DeviceGray', RGB: 'DeviceRGB', CMYK: 'DeviceCMYK', DeviceGray: 'DeviceGray', DeviceRGB: 'DeviceRGB', DeviceCMYK: 'DeviceCMYK' };
700
+ const INLINE_NAME = { DeviceGray: 'G', DeviceRGB: 'RGB', DeviceCMYK: 'CMYK' };
701
+ /** Inline images' abbreviated filters. */
702
+ const EXPAND = { AHx: 'ASCIIHexDecode', A85: 'ASCII85Decode', LZW: 'LZWDecode', Fl: 'FlateDecode', RL: 'RunLengthDecode', CCF: 'CCITTFaxDecode', DCT: 'DCTDecode' };
703
+ function describe(v) {
704
+ if (Array.isArray(v))
705
+ return v.map(describe).join(' ');
706
+ return typeof v === 'string' ? `/${v}` : String(v);
707
+ }
708
+ /**
709
+ * 8-bit samples scaled by a /Decode array into the 8-bit samples a default
710
+ * /Decode would have given: each component's [Dmin, Dmax] mapped back to 0
711
+ * and 255 (an inverted Decode, as Adobe's CMYK JPEGs have, flipped).
712
+ */
713
+ function applyDecode(samples, n, _bits, decode, length) {
714
+ const s = samples.length === length ? samples : samples.subarray(0, length);
715
+ if (!decode || decode.length < 2 * n)
716
+ return s;
717
+ const identity = decode.every((x, i) => x === (i % 2 === 0 ? 0 : 1));
718
+ if (identity)
719
+ return s;
720
+ const out = new Uint8Array(s.length);
721
+ for (let i = 0; i < s.length; i++) {
722
+ const c = i % n;
723
+ const lo = decode[2 * c];
724
+ const hi = decode[2 * c + 1];
725
+ out[i] = Math.round(Math.min(1, Math.max(0, lo + (s[i] / 255) * (hi - lo))) * 255);
726
+ }
727
+ return out;
728
+ }
729
+ function asciiHex(data) {
730
+ let s = '';
731
+ for (let i = 0; i < data.length; i++) {
732
+ s += data[i].toString(16).padStart(2, '0');
733
+ if (i % 32 === 31)
734
+ s += '\n';
735
+ }
736
+ return new TextEncoder().encode(`${s}>`);
737
+ }
738
+ /** The browser's JPEG decoder, its samples as they are in the file (no colour management), 1 or 3 components. */
739
+ async function browserJpeg(data, components) {
740
+ if (typeof createImageBitmap !== 'function' || (components !== 1 && components !== 3))
741
+ return undefined;
742
+ const bitmap = await createImageBitmap(new Blob([data], { type: 'image/jpeg' }), { colorSpaceConversion: 'none', premultiplyAlpha: 'none' });
743
+ try {
744
+ const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
745
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
746
+ if (!ctx)
747
+ return undefined;
748
+ ctx.drawImage(bitmap, 0, 0);
749
+ const rgba = ctx.getImageData(0, 0, bitmap.width, bitmap.height).data;
750
+ const out = new Uint8Array(bitmap.width * bitmap.height * components);
751
+ for (let i = 0, j = 0; i < rgba.length; i += 4) {
752
+ out[j++] = rgba[i];
753
+ if (components === 3) {
754
+ out[j++] = rgba[i + 1];
755
+ out[j++] = rgba[i + 2];
756
+ }
757
+ }
758
+ return out;
759
+ }
760
+ finally {
761
+ bitmap.close();
762
+ }
763
+ }
764
+ /** Reads bits, most significant first. */
765
+ class BitReader {
766
+ data;
767
+ at = 0;
768
+ constructor(data) {
769
+ this.data = data;
770
+ }
771
+ has(bits) {
772
+ return this.at + bits <= this.data.length * 8;
773
+ }
774
+ read(bits) {
775
+ let v = 0;
776
+ for (let i = 0; i < bits; i++, this.at++)
777
+ v = v * 2 + (((this.data[this.at >> 3] ?? 0) >> (7 - (this.at & 7))) & 1);
778
+ return v;
779
+ }
780
+ align() {
781
+ this.at = Math.ceil(this.at / 8) * 8;
782
+ }
783
+ }
784
+ /** Writes bits, most significant first. */
785
+ class BitWriter {
786
+ out = [];
787
+ at = 0;
788
+ write(v, bits) {
789
+ for (let i = bits - 1; i >= 0; i--, this.at++) {
790
+ if ((this.at & 7) === 0)
791
+ this.out.push(0);
792
+ if (Math.floor(v / 2 ** i) % 2)
793
+ this.out[this.out.length - 1] |= 1 << (7 - (this.at & 7));
794
+ }
795
+ }
796
+ align() {
797
+ this.at = Math.ceil(this.at / 8) * 8;
798
+ }
799
+ bytes() {
800
+ return Uint8Array.from(this.out);
801
+ }
802
+ }
803
+ //# sourceMappingURL=rewrite.js.map