postext-pdf 1.63.1 → 1.63.3

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 (338) hide show
  1. package/dist/__bench__/arabicShaping.bench.test.js +1 -1
  2. package/dist/__bench__/arabicShaping.bench.test.js.map +1 -1
  3. package/dist/__tests__/accessibility.test.js +1 -1
  4. package/dist/__tests__/accessibility.test.js.map +1 -1
  5. package/dist/__tests__/boxBorderInside.test.js +1 -1
  6. package/dist/__tests__/boxBorderInside.test.js.map +1 -1
  7. package/dist/__tests__/boxShape.test.js +2 -2
  8. package/dist/__tests__/boxShape.test.js.map +1 -1
  9. package/dist/__tests__/canvasShaping.test.d.ts +1 -1
  10. package/dist/__tests__/canvasShaping.test.d.ts.map +1 -1
  11. package/dist/__tests__/canvasShaping.test.js +1 -1
  12. package/dist/__tests__/canvasShaping.test.js.map +1 -1
  13. package/dist/__tests__/comic.test.js +1 -1
  14. package/dist/__tests__/comic.test.js.map +1 -1
  15. package/dist/__tests__/comicMarkPairs.test.js +1 -1
  16. package/dist/__tests__/comicMarkPairs.test.js.map +1 -1
  17. package/dist/__tests__/composedDecorationImages.test.js +1 -1
  18. package/dist/__tests__/composedDecorationImages.test.js.map +1 -1
  19. package/dist/__tests__/composedFrameBoxes.test.js +1 -1
  20. package/dist/__tests__/composedFrameBoxes.test.js.map +1 -1
  21. package/dist/__tests__/composedPage.test.js +2 -2
  22. package/dist/__tests__/composedPage.test.js.map +1 -1
  23. package/dist/__tests__/declaredFiles.test.js +1 -1
  24. package/dist/__tests__/declaredFiles.test.js.map +1 -1
  25. package/dist/__tests__/dottedText.test.js +3 -3
  26. package/dist/__tests__/dottedText.test.js.map +1 -1
  27. package/dist/__tests__/headerFooter.test.js +2 -2
  28. package/dist/__tests__/headerFooter.test.js.map +1 -1
  29. package/dist/__tests__/pathBlock.test.js +2 -2
  30. package/dist/__tests__/pathBlock.test.js.map +1 -1
  31. package/dist/__tests__/printMaster.test.js +1 -1
  32. package/dist/__tests__/printMaster.test.js.map +1 -1
  33. package/dist/__tests__/rtlPdf.test.js +1 -1
  34. package/dist/__tests__/rtlPdf.test.js.map +1 -1
  35. package/dist/__tests__/shapingLanguage.test.js +1 -1
  36. package/dist/__tests__/shapingLanguage.test.js.map +1 -1
  37. package/dist/__tests__/shapingLanguageRuns.test.js +1 -1
  38. package/dist/__tests__/shapingLanguageRuns.test.js.map +1 -1
  39. package/dist/__tests__/shapingLanguageRunsDocument.test.js +1 -1
  40. package/dist/__tests__/shapingLanguageRunsDocument.test.js.map +1 -1
  41. package/dist/__tests__/sharedGlyphReadback.test.d.ts +1 -1
  42. package/dist/__tests__/sharedGlyphReadback.test.d.ts.map +1 -1
  43. package/dist/__tests__/sharedGlyphReadback.test.js +1 -1
  44. package/dist/__tests__/sharedGlyphReadback.test.js.map +1 -1
  45. package/dist/__tests__/svgVector.test.js +2 -2
  46. package/dist/__tests__/svgVector.test.js.map +1 -1
  47. package/dist/faceFiles.js +1 -1
  48. package/dist/faceFiles.js.map +1 -1
  49. package/dist/fontCache/fontFiles.d.ts +172 -0
  50. package/dist/fontCache/fontFiles.d.ts.map +1 -0
  51. package/dist/fontCache/fontFiles.js +81 -0
  52. package/dist/fontCache/fontFiles.js.map +1 -0
  53. package/dist/fontCache/glyphTables.d.ts +38 -0
  54. package/dist/fontCache/glyphTables.d.ts.map +1 -0
  55. package/dist/fontCache/glyphTables.js +107 -0
  56. package/dist/fontCache/glyphTables.js.map +1 -0
  57. package/dist/fontCache.d.ts +5 -189
  58. package/dist/fontCache.d.ts.map +1 -1
  59. package/dist/fontCache.js +5 -184
  60. package/dist/fontCache.js.map +1 -1
  61. package/dist/index.d.ts +2 -2
  62. package/dist/index.d.ts.map +1 -1
  63. package/dist/index.js +2 -2
  64. package/dist/index.js.map +1 -1
  65. package/dist/pdf-backend/blocks/blockRender/lineText.d.ts +23 -0
  66. package/dist/pdf-backend/blocks/blockRender/lineText.d.ts.map +1 -0
  67. package/dist/pdf-backend/blocks/blockRender/lineText.js +200 -0
  68. package/dist/pdf-backend/blocks/blockRender/lineText.js.map +1 -0
  69. package/dist/pdf-backend/blocks/blockRender/markers.d.ts +23 -0
  70. package/dist/pdf-backend/blocks/blockRender/markers.d.ts.map +1 -0
  71. package/dist/pdf-backend/blocks/blockRender/markers.js +140 -0
  72. package/dist/pdf-backend/blocks/blockRender/markers.js.map +1 -0
  73. package/dist/pdf-backend/blocks/blockRender/segments.d.ts +74 -0
  74. package/dist/pdf-backend/blocks/blockRender/segments.d.ts.map +1 -0
  75. package/dist/pdf-backend/blocks/blockRender/segments.js +409 -0
  76. package/dist/pdf-backend/blocks/blockRender/segments.js.map +1 -0
  77. package/dist/pdf-backend/{blockRender.d.ts → blocks/blockRender.d.ts} +7 -10
  78. package/dist/pdf-backend/blocks/blockRender.d.ts.map +1 -0
  79. package/dist/pdf-backend/blocks/blockRender.js +146 -0
  80. package/dist/pdf-backend/blocks/blockRender.js.map +1 -0
  81. package/dist/pdf-backend/{chip.d.ts → blocks/chip.d.ts} +3 -3
  82. package/dist/pdf-backend/blocks/chip.d.ts.map +1 -0
  83. package/dist/pdf-backend/{chip.js → blocks/chip.js} +5 -5
  84. package/dist/pdf-backend/blocks/chip.js.map +1 -0
  85. package/dist/pdf-backend/{inlineIcon.d.ts → blocks/inlineIcon.d.ts} +3 -3
  86. package/dist/pdf-backend/blocks/inlineIcon.d.ts.map +1 -0
  87. package/dist/pdf-backend/{inlineIcon.js → blocks/inlineIcon.js} +3 -3
  88. package/dist/pdf-backend/blocks/inlineIcon.js.map +1 -0
  89. package/dist/pdf-backend/blocks/renderResourceBlock/lines.d.ts +37 -0
  90. package/dist/pdf-backend/blocks/renderResourceBlock/lines.d.ts.map +1 -0
  91. package/dist/pdf-backend/blocks/renderResourceBlock/lines.js +271 -0
  92. package/dist/pdf-backend/blocks/renderResourceBlock/lines.js.map +1 -0
  93. package/dist/pdf-backend/blocks/renderResourceBlock/overlays.d.ts +21 -0
  94. package/dist/pdf-backend/blocks/renderResourceBlock/overlays.d.ts.map +1 -0
  95. package/dist/pdf-backend/blocks/renderResourceBlock/overlays.js +164 -0
  96. package/dist/pdf-backend/blocks/renderResourceBlock/overlays.js.map +1 -0
  97. package/dist/pdf-backend/blocks/renderResourceBlock/preload.d.ts +27 -0
  98. package/dist/pdf-backend/blocks/renderResourceBlock/preload.d.ts.map +1 -0
  99. package/dist/pdf-backend/blocks/renderResourceBlock/preload.js +330 -0
  100. package/dist/pdf-backend/blocks/renderResourceBlock/preload.js.map +1 -0
  101. package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.d.ts +38 -0
  102. package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.d.ts.map +1 -0
  103. package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.js +218 -0
  104. package/dist/pdf-backend/blocks/renderResourceBlock/rasterize.js.map +1 -0
  105. package/dist/pdf-backend/blocks/renderResourceBlock/table.d.ts +17 -0
  106. package/dist/pdf-backend/blocks/renderResourceBlock/table.d.ts.map +1 -0
  107. package/dist/pdf-backend/blocks/renderResourceBlock/table.js +203 -0
  108. package/dist/pdf-backend/blocks/renderResourceBlock/table.js.map +1 -0
  109. package/dist/pdf-backend/blocks/renderResourceBlock.d.ts +59 -0
  110. package/dist/pdf-backend/blocks/renderResourceBlock.d.ts.map +1 -0
  111. package/dist/pdf-backend/blocks/renderResourceBlock.js +201 -0
  112. package/dist/pdf-backend/blocks/renderResourceBlock.js.map +1 -0
  113. package/dist/pdf-backend/{scoreRender.d.ts → blocks/scoreRender.d.ts} +2 -2
  114. package/dist/pdf-backend/blocks/scoreRender.d.ts.map +1 -0
  115. package/dist/pdf-backend/{scoreRender.js → blocks/scoreRender.js} +2 -2
  116. package/dist/pdf-backend/blocks/scoreRender.js.map +1 -0
  117. package/dist/pdf-backend/{taskBox.d.ts → blocks/taskBox.d.ts} +1 -1
  118. package/dist/pdf-backend/blocks/taskBox.d.ts.map +1 -0
  119. package/dist/pdf-backend/{taskBox.js → blocks/taskBox.js} +1 -1
  120. package/dist/pdf-backend/blocks/taskBox.js.map +1 -0
  121. package/dist/pdf-backend/index.d.ts +8 -114
  122. package/dist/pdf-backend/index.d.ts.map +1 -1
  123. package/dist/pdf-backend/index.js +16 -614
  124. package/dist/pdf-backend/index.js.map +1 -1
  125. package/dist/pdf-backend/{comic.d.ts → page/comic.d.ts} +6 -6
  126. package/dist/pdf-backend/page/comic.d.ts.map +1 -0
  127. package/dist/pdf-backend/{comic.js → page/comic.js} +4 -4
  128. package/dist/pdf-backend/page/comic.js.map +1 -0
  129. package/dist/pdf-backend/{headerFooter.d.ts → page/headerFooter.d.ts} +4 -4
  130. package/dist/pdf-backend/page/headerFooter.d.ts.map +1 -0
  131. package/dist/pdf-backend/{headerFooter.js → page/headerFooter.js} +8 -8
  132. package/dist/pdf-backend/page/headerFooter.js.map +1 -0
  133. package/dist/pdf-backend/{pageDecorations.d.ts → page/pageDecorations.d.ts} +1 -1
  134. package/dist/pdf-backend/page/pageDecorations.d.ts.map +1 -0
  135. package/dist/pdf-backend/{pageDecorations.js → page/pageDecorations.js} +1 -1
  136. package/dist/pdf-backend/page/pageDecorations.js.map +1 -0
  137. package/dist/pdf-backend/page/pageLabels.d.ts.map +1 -0
  138. package/dist/pdf-backend/page/pageLabels.js.map +1 -0
  139. package/dist/pdf-backend/primitives/actualText.d.ts +91 -0
  140. package/dist/pdf-backend/primitives/actualText.d.ts.map +1 -0
  141. package/dist/pdf-backend/primitives/actualText.js +154 -0
  142. package/dist/pdf-backend/primitives/actualText.js.map +1 -0
  143. package/dist/pdf-backend/primitives/color.d.ts +22 -0
  144. package/dist/pdf-backend/primitives/color.d.ts.map +1 -0
  145. package/dist/pdf-backend/primitives/color.js +116 -0
  146. package/dist/pdf-backend/primitives/color.js.map +1 -0
  147. package/dist/pdf-backend/primitives/matrix.d.ts +55 -0
  148. package/dist/pdf-backend/primitives/matrix.d.ts.map +1 -0
  149. package/dist/pdf-backend/primitives/matrix.js +124 -0
  150. package/dist/pdf-backend/primitives/matrix.js.map +1 -0
  151. package/dist/pdf-backend/primitives/measuredText.d.ts +61 -0
  152. package/dist/pdf-backend/primitives/measuredText.d.ts.map +1 -0
  153. package/dist/pdf-backend/primitives/measuredText.js +142 -0
  154. package/dist/pdf-backend/primitives/measuredText.js.map +1 -0
  155. package/dist/pdf-backend/primitives/shapedRuns.d.ts +49 -0
  156. package/dist/pdf-backend/primitives/shapedRuns.d.ts.map +1 -0
  157. package/dist/pdf-backend/primitives/shapedRuns.js +367 -0
  158. package/dist/pdf-backend/primitives/shapedRuns.js.map +1 -0
  159. package/dist/pdf-backend/primitives/shapes.d.ts +35 -0
  160. package/dist/pdf-backend/primitives/shapes.d.ts.map +1 -0
  161. package/dist/pdf-backend/primitives/shapes.js +166 -0
  162. package/dist/pdf-backend/primitives/shapes.js.map +1 -0
  163. package/dist/pdf-backend/primitives/textShows.d.ts +90 -0
  164. package/dist/pdf-backend/primitives/textShows.d.ts.map +1 -0
  165. package/dist/pdf-backend/primitives/textShows.js +167 -0
  166. package/dist/pdf-backend/primitives/textShows.js.map +1 -0
  167. package/dist/pdf-backend/primitives.d.ts +16 -308
  168. package/dist/pdf-backend/primitives.d.ts.map +1 -1
  169. package/dist/pdf-backend/primitives.js +15 -1195
  170. package/dist/pdf-backend/primitives.js.map +1 -1
  171. package/dist/pdf-backend/renderToPdf/composedFrames.d.ts +19 -0
  172. package/dist/pdf-backend/renderToPdf/composedFrames.d.ts.map +1 -0
  173. package/dist/pdf-backend/renderToPdf/composedFrames.js +156 -0
  174. package/dist/pdf-backend/renderToPdf/composedFrames.js.map +1 -0
  175. package/dist/pdf-backend/renderToPdf/metadata.d.ts +31 -0
  176. package/dist/pdf-backend/renderToPdf/metadata.d.ts.map +1 -0
  177. package/dist/pdf-backend/renderToPdf/metadata.js +85 -0
  178. package/dist/pdf-backend/renderToPdf/metadata.js.map +1 -0
  179. package/dist/pdf-backend/renderToPdf/paintPage.d.ts +16 -0
  180. package/dist/pdf-backend/renderToPdf/paintPage.d.ts.map +1 -0
  181. package/dist/pdf-backend/renderToPdf/paintPage.js +348 -0
  182. package/dist/pdf-backend/renderToPdf/paintPage.js.map +1 -0
  183. package/dist/pdf-backend/renderToPdf/warnings.d.ts +114 -0
  184. package/dist/pdf-backend/renderToPdf/warnings.d.ts.map +1 -0
  185. package/dist/pdf-backend/renderToPdf/warnings.js +35 -0
  186. package/dist/pdf-backend/renderToPdf/warnings.js.map +1 -0
  187. package/dist/pdf-backend/structure/links.d.ts.map +1 -0
  188. package/dist/pdf-backend/structure/links.js.map +1 -0
  189. package/dist/pdf-backend/structure/outlines.d.ts.map +1 -0
  190. package/dist/pdf-backend/structure/outlines.js.map +1 -0
  191. package/dist/pdf-backend/structure/structureFlow.d.ts.map +1 -0
  192. package/dist/pdf-backend/structure/structureFlow.js.map +1 -0
  193. package/dist/pdf-backend/{tagging.d.ts → structure/tagging.d.ts} +1 -1
  194. package/dist/pdf-backend/structure/tagging.d.ts.map +1 -0
  195. package/dist/pdf-backend/structure/tagging.js.map +1 -0
  196. package/dist/pdf-backend/{annotations.d.ts → text/annotations.d.ts} +3 -3
  197. package/dist/pdf-backend/text/annotations.d.ts.map +1 -0
  198. package/dist/pdf-backend/{annotations.js → text/annotations.js} +3 -3
  199. package/dist/pdf-backend/text/annotations.js.map +1 -0
  200. package/dist/pdf-backend/{directedLine.d.ts → text/directedLine.d.ts} +1 -1
  201. package/dist/pdf-backend/text/directedLine.d.ts.map +1 -0
  202. package/dist/pdf-backend/{directedLine.js → text/directedLine.js} +1 -1
  203. package/dist/pdf-backend/text/directedLine.js.map +1 -0
  204. package/dist/pdf-backend/text/fallbackSpaces.d.ts.map +1 -0
  205. package/dist/pdf-backend/text/fallbackSpaces.js.map +1 -0
  206. package/dist/pdf-backend/text/fontHelpers.d.ts.map +1 -0
  207. package/dist/pdf-backend/text/fontHelpers.js.map +1 -0
  208. package/dist/pdf-backend/text/inkScale.d.ts.map +1 -0
  209. package/dist/pdf-backend/text/inkScale.js.map +1 -0
  210. package/dist/pdf-backend/{rtlRun.d.ts → text/rtlRun.d.ts} +1 -1
  211. package/dist/pdf-backend/text/rtlRun.d.ts.map +1 -0
  212. package/dist/pdf-backend/{rtlRun.js → text/rtlRun.js} +2 -2
  213. package/dist/pdf-backend/text/rtlRun.js.map +1 -0
  214. package/dist/pdf-backend/text/shapedText/shaping.d.ts +70 -0
  215. package/dist/pdf-backend/text/shapedText/shaping.d.ts.map +1 -0
  216. package/dist/pdf-backend/text/shapedText/shaping.js +380 -0
  217. package/dist/pdf-backend/text/shapedText/shaping.js.map +1 -0
  218. package/dist/pdf-backend/{shapedText.d.ts → text/shapedText.d.ts} +2 -2
  219. package/dist/pdf-backend/text/shapedText.d.ts.map +1 -0
  220. package/dist/pdf-backend/text/shapedText.js +311 -0
  221. package/dist/pdf-backend/text/shapedText.js.map +1 -0
  222. package/dist/pdf-backend/{verticalText.d.ts → text/verticalText.d.ts} +1 -1
  223. package/dist/pdf-backend/text/verticalText.d.ts.map +1 -0
  224. package/dist/pdf-backend/{verticalText.js → text/verticalText.js} +4 -4
  225. package/dist/pdf-backend/text/verticalText.js.map +1 -0
  226. package/dist/pdf-backend/text/wordPieces.d.ts.map +1 -0
  227. package/dist/pdf-backend/{wordPieces.js → text/wordPieces.js} +1 -1
  228. package/dist/pdf-backend/text/wordPieces.js.map +1 -0
  229. package/dist/pdf-backend/{pathBlock.d.ts → vector/pathBlock.d.ts} +1 -1
  230. package/dist/pdf-backend/vector/pathBlock.d.ts.map +1 -0
  231. package/dist/pdf-backend/{pathBlock.js → vector/pathBlock.js} +1 -1
  232. package/dist/pdf-backend/vector/pathBlock.js.map +1 -0
  233. package/dist/pdf-backend/vector/svgVector/emit.d.ts +11 -0
  234. package/dist/pdf-backend/vector/svgVector/emit.d.ts.map +1 -0
  235. package/dist/pdf-backend/vector/svgVector/emit.js +281 -0
  236. package/dist/pdf-backend/vector/svgVector/emit.js.map +1 -0
  237. package/dist/pdf-backend/vector/svgVector/geometry.d.ts +18 -0
  238. package/dist/pdf-backend/vector/svgVector/geometry.d.ts.map +1 -0
  239. package/dist/pdf-backend/vector/svgVector/geometry.js +382 -0
  240. package/dist/pdf-backend/vector/svgVector/geometry.js.map +1 -0
  241. package/dist/pdf-backend/vector/svgVector/style.d.ts +37 -0
  242. package/dist/pdf-backend/vector/svgVector/style.d.ts.map +1 -0
  243. package/dist/pdf-backend/vector/svgVector/style.js +314 -0
  244. package/dist/pdf-backend/vector/svgVector/style.js.map +1 -0
  245. package/dist/pdf-backend/vector/svgVector/walk.d.ts +15 -0
  246. package/dist/pdf-backend/vector/svgVector/walk.d.ts.map +1 -0
  247. package/dist/pdf-backend/vector/svgVector/walk.js +405 -0
  248. package/dist/pdf-backend/vector/svgVector/walk.js.map +1 -0
  249. package/dist/pdf-backend/vector/svgVector/xml.d.ts +20 -0
  250. package/dist/pdf-backend/vector/svgVector/xml.d.ts.map +1 -0
  251. package/dist/pdf-backend/vector/svgVector/xml.js +160 -0
  252. package/dist/pdf-backend/vector/svgVector/xml.js.map +1 -0
  253. package/dist/pdf-backend/{svgVector.d.ts → vector/svgVector.d.ts} +2 -12
  254. package/dist/pdf-backend/vector/svgVector.d.ts.map +1 -0
  255. package/dist/pdf-backend/vector/svgVector.js +95 -0
  256. package/dist/pdf-backend/vector/svgVector.js.map +1 -0
  257. package/dist/pdf-backend/vector/tint.d.ts.map +1 -0
  258. package/dist/pdf-backend/{tint.js → vector/tint.js} +1 -1
  259. package/dist/pdf-backend/vector/tint.js.map +1 -0
  260. package/dist/worker/client.d.ts +1 -1
  261. package/dist/worker/client.d.ts.map +1 -1
  262. package/dist/worker/client.js +1 -1
  263. package/dist/worker/client.js.map +1 -1
  264. package/package.json +3 -3
  265. package/dist/pdf-backend/annotations.d.ts.map +0 -1
  266. package/dist/pdf-backend/annotations.js.map +0 -1
  267. package/dist/pdf-backend/blockRender.d.ts.map +0 -1
  268. package/dist/pdf-backend/blockRender.js +0 -866
  269. package/dist/pdf-backend/blockRender.js.map +0 -1
  270. package/dist/pdf-backend/chip.d.ts.map +0 -1
  271. package/dist/pdf-backend/chip.js.map +0 -1
  272. package/dist/pdf-backend/comic.d.ts.map +0 -1
  273. package/dist/pdf-backend/comic.js.map +0 -1
  274. package/dist/pdf-backend/directedLine.d.ts.map +0 -1
  275. package/dist/pdf-backend/directedLine.js.map +0 -1
  276. package/dist/pdf-backend/fallbackSpaces.d.ts.map +0 -1
  277. package/dist/pdf-backend/fallbackSpaces.js.map +0 -1
  278. package/dist/pdf-backend/fontHelpers.d.ts.map +0 -1
  279. package/dist/pdf-backend/fontHelpers.js.map +0 -1
  280. package/dist/pdf-backend/headerFooter.d.ts.map +0 -1
  281. package/dist/pdf-backend/headerFooter.js.map +0 -1
  282. package/dist/pdf-backend/inkScale.d.ts.map +0 -1
  283. package/dist/pdf-backend/inkScale.js.map +0 -1
  284. package/dist/pdf-backend/inlineIcon.d.ts.map +0 -1
  285. package/dist/pdf-backend/inlineIcon.js.map +0 -1
  286. package/dist/pdf-backend/links.d.ts.map +0 -1
  287. package/dist/pdf-backend/links.js.map +0 -1
  288. package/dist/pdf-backend/outlines.d.ts.map +0 -1
  289. package/dist/pdf-backend/outlines.js.map +0 -1
  290. package/dist/pdf-backend/pageDecorations.d.ts.map +0 -1
  291. package/dist/pdf-backend/pageDecorations.js.map +0 -1
  292. package/dist/pdf-backend/pageLabels.d.ts.map +0 -1
  293. package/dist/pdf-backend/pageLabels.js.map +0 -1
  294. package/dist/pdf-backend/pathBlock.d.ts.map +0 -1
  295. package/dist/pdf-backend/pathBlock.js.map +0 -1
  296. package/dist/pdf-backend/renderResourceBlock.d.ts +0 -120
  297. package/dist/pdf-backend/renderResourceBlock.d.ts.map +0 -1
  298. package/dist/pdf-backend/renderResourceBlock.js +0 -1351
  299. package/dist/pdf-backend/renderResourceBlock.js.map +0 -1
  300. package/dist/pdf-backend/rtlRun.d.ts.map +0 -1
  301. package/dist/pdf-backend/rtlRun.js.map +0 -1
  302. package/dist/pdf-backend/scoreRender.d.ts.map +0 -1
  303. package/dist/pdf-backend/scoreRender.js.map +0 -1
  304. package/dist/pdf-backend/shapedText.d.ts.map +0 -1
  305. package/dist/pdf-backend/shapedText.js +0 -682
  306. package/dist/pdf-backend/shapedText.js.map +0 -1
  307. package/dist/pdf-backend/structureFlow.d.ts.map +0 -1
  308. package/dist/pdf-backend/structureFlow.js.map +0 -1
  309. package/dist/pdf-backend/svgVector.d.ts.map +0 -1
  310. package/dist/pdf-backend/svgVector.js +0 -1610
  311. package/dist/pdf-backend/svgVector.js.map +0 -1
  312. package/dist/pdf-backend/tagging.d.ts.map +0 -1
  313. package/dist/pdf-backend/tagging.js.map +0 -1
  314. package/dist/pdf-backend/taskBox.d.ts.map +0 -1
  315. package/dist/pdf-backend/taskBox.js.map +0 -1
  316. package/dist/pdf-backend/tint.d.ts.map +0 -1
  317. package/dist/pdf-backend/tint.js.map +0 -1
  318. package/dist/pdf-backend/verticalText.d.ts.map +0 -1
  319. package/dist/pdf-backend/verticalText.js.map +0 -1
  320. package/dist/pdf-backend/wordPieces.d.ts.map +0 -1
  321. package/dist/pdf-backend/wordPieces.js.map +0 -1
  322. /package/dist/pdf-backend/{pageLabels.d.ts → page/pageLabels.d.ts} +0 -0
  323. /package/dist/pdf-backend/{pageLabels.js → page/pageLabels.js} +0 -0
  324. /package/dist/pdf-backend/{links.d.ts → structure/links.d.ts} +0 -0
  325. /package/dist/pdf-backend/{links.js → structure/links.js} +0 -0
  326. /package/dist/pdf-backend/{outlines.d.ts → structure/outlines.d.ts} +0 -0
  327. /package/dist/pdf-backend/{outlines.js → structure/outlines.js} +0 -0
  328. /package/dist/pdf-backend/{structureFlow.d.ts → structure/structureFlow.d.ts} +0 -0
  329. /package/dist/pdf-backend/{structureFlow.js → structure/structureFlow.js} +0 -0
  330. /package/dist/pdf-backend/{tagging.js → structure/tagging.js} +0 -0
  331. /package/dist/pdf-backend/{fallbackSpaces.d.ts → text/fallbackSpaces.d.ts} +0 -0
  332. /package/dist/pdf-backend/{fallbackSpaces.js → text/fallbackSpaces.js} +0 -0
  333. /package/dist/pdf-backend/{fontHelpers.d.ts → text/fontHelpers.d.ts} +0 -0
  334. /package/dist/pdf-backend/{fontHelpers.js → text/fontHelpers.js} +0 -0
  335. /package/dist/pdf-backend/{inkScale.d.ts → text/inkScale.d.ts} +0 -0
  336. /package/dist/pdf-backend/{inkScale.js → text/inkScale.js} +0 -0
  337. /package/dist/pdf-backend/{wordPieces.d.ts → text/wordPieces.d.ts} +0 -0
  338. /package/dist/pdf-backend/{tint.d.ts → vector/tint.d.ts} +0 -0
@@ -1,11 +1,18 @@
1
- import { rgb, cmyk, grayscale, pushGraphicsState, popGraphicsState, rectangle, clip, endPath, concatTransformationMatrix, PDFArray, PDFHexString, PDFName, PDFNumber, PDFOperator, PDFOperatorNames, beginText, endText, setFillingColor, setFontAndSize, showText, setTextMatrix, moveTo, lineTo, appendBezierCurve, closePath, stroke, fill, setStrokingColor, setLineWidth, setTextRenderingMode, TextRenderingMode, setGraphicsState, setCharacterSpacing, setLineCap, setDashPattern, LineCapStyle, } from 'pdf-lib';
2
- import { colorAlpha, hexToRgb, rgbToCmyk, rgbToGrayscale } from '../colors.js';
3
- import { isPrintMode, modeKey, overPaper } from '../print/colorMode.js';
4
- import { graphemesOf, joinsLetters, scriptContextUntracked, textDots } from 'postext';
5
- import { fallbackPieces, isDefaultIgnorable, isSpaceCharacter } from './fallbackSpaces.js';
6
- import { fileRuns, inRun, noteMissingGlyphs, wordRuns } from '../faceFiles.js';
7
- import { needsComplexShaping } from '../complexShaping.js';
8
- import { shapesAsCanvas, shapingKey, shapingLanguage, withShapingScript } from '../shapingLanguage.js';
1
+ import { setCharacterSpacing, pushGraphicsState, setStrokingColor, setLineWidth, setLineCap, LineCapStyle, moveTo, lineTo, stroke, popGraphicsState } from 'pdf-lib';
2
+ import { scriptContextUntracked, textDots, joinsLetters } from 'postext';
3
+ import { withShapingScript, shapingLanguage } from '../shapingLanguage.js';
4
+ import { pushTextObject, beginActualTextSpan, endActualTextSpan } from './primitives/actualText.js';
5
+ import { opt, alphaStateOp, alphaOf } from './primitives/color.js';
6
+ import { measuredTextShows } from './primitives/measuredText.js';
7
+ import { textAdvancePx } from './primitives/shapedRuns.js';
8
+ import { textShows, letterTrackingTj } from './primitives/textShows.js';
9
+ export { applyMatrix, pushTransform, popTransform, quarterTurnMatrix, mirrorMatrix, counterFlipPx, composeMatrix, pushFrame, popFrame, mapPointPt, outerRectMap, mapRectThrough } from './primitives/matrix.js';
10
+ export { drawSwatchPx, drawShapedSwatchPx, fillRectPx, fillRectsPx, drawLinePx, pushClipRect, popClip, pushClipOutline, fillOutlinePx, drawChipBoxPx, strokeOutlinePx } from './primitives/shapes.js';
11
+ export { makeScale, whiteColor, colorFromHex, alphaOf, alphaStateOp, opt, rgbFromHex } from './primitives/color.js';
12
+ export { showTextShaped, textShows, letterTrackingTj } from './primitives/textShows.js';
13
+ export { textAdvancePx } from './primitives/shapedRuns.js';
14
+ export { cellPieces, measuredTextOperator, measuredTextShows, fontKeyOn } from './primitives/measuredText.js';
15
+ export { pushTextObject, withActualText, beginActualTextSpan, cjkLineText, readText, compressedMarkSpacingPx, endActualTextSpan } from './primitives/actualText.js';
9
16
  /** Set the character spacing (`Tc`) text is painted with, px, and record
10
17
  * it on the context. */
11
18
  export function setTrackingPx(ctx, px) {
@@ -35,967 +42,6 @@ export function paintInScript(ctx, script, paint) {
35
42
  setTrackingPx(ctx, tracking);
36
43
  }
37
44
  }
38
- /** Apply `m` to a point. */
39
- export function applyMatrix(m, x, y) {
40
- return { x: m[0] * x + m[2] * y + m[4], y: m[1] * x + m[3] * y + m[5] };
41
- }
42
- /** Push a graphics state with `m` concatenated onto the CTM. Everything
43
- * drawn until {@link popTransform} goes through it. */
44
- export function pushTransform(ctx, m) {
45
- ctx.tags?.close();
46
- ctx.page.pushOperators(pushGraphicsState(), concatTransformationMatrix(...m));
47
- }
48
- export function popTransform(ctx) {
49
- ctx.tags?.close();
50
- ctx.page.pushOperators(popGraphicsState());
51
- }
52
- /** The quarter-turn matrix of a turned frame (`VDTResourceRotation`, or
53
- * a vertical page's flow, `VDTFlowFrame.rotation`) in the backend's
54
- * points, y up: a frame whose `*Px` helpers flip y against the page
55
- * height, as the page's own do. */
56
- export function quarterTurnMatrix(rot, scale, pageHeightPt) {
57
- return rot.direction === 'ccw'
58
- ? [0, 1, -1, 0, rot.originX * scale + pageHeightPt, pageHeightPt - rot.originY * scale]
59
- : [0, -1, 1, 0, rot.originX * scale - pageHeightPt, pageHeightPt - rot.originY * scale];
60
- }
61
- /** The matrix of a right-to-left page's flow (`VDTMirroredFlowFrame`,
62
- * `mirror.originX` px) in the backend's points: the flow turned over the
63
- * sheet's vertical axis, `x` → `originX − x`. */
64
- export function mirrorMatrix(originX, scale) {
65
- return [-1, 0, 0, 1, originX * scale, 0];
66
- }
67
- /** Paint what `paint` draws turned back about the horizontal extent
68
- * `[leftPx, leftPx + widthPx]` of the current frame, on a mirrored page
69
- * (`ctx.mirror`): a picture or a formula lands where the mirrored layout
70
- * put its box and reads unmirrored. Elsewhere it just paints. */
71
- export function counterFlipPx(ctx, leftPx, widthPx, paint) {
72
- if (!ctx.mirror) {
73
- paint();
74
- return;
75
- }
76
- const s = ctx.scale;
77
- // The transform closes the open marked content (#900): the picture or
78
- // formula is painted in the element (or artifact) it was routed to, and
79
- // so is what its painter draws after it.
80
- const open = ctx.tags?.current ?? null;
81
- pushTransform(ctx, [-1, 0, 0, 1, (2 * leftPx + widthPx) * s, 0]);
82
- ctx.tags?.resume(open);
83
- ctx.mirror = false;
84
- try {
85
- paint();
86
- }
87
- finally {
88
- ctx.mirror = true;
89
- popTransform(ctx);
90
- ctx.tags?.resume(open);
91
- }
92
- }
93
- /** `inner` then `outer`: the matrix that maps a point of `inner`'s frame
94
- * through both. */
95
- export function composeMatrix(inner, outer) {
96
- return [
97
- inner[0] * outer[0] + inner[1] * outer[2],
98
- inner[0] * outer[1] + inner[1] * outer[3],
99
- inner[2] * outer[0] + inner[3] * outer[2],
100
- inner[2] * outer[1] + inner[3] * outer[3],
101
- inner[4] * outer[0] + inner[5] * outer[2] + outer[4],
102
- inner[4] * outer[1] + inner[5] * outer[3] + outer[5],
103
- ];
104
- }
105
- /** Enter a frame: `m` concatenated onto the CTM ({@link pushTransform}),
106
- * and the rects and points computed inside it mapped onto the page
107
- * through it and every frame around it (`ctx.mapRectPt`,
108
- * {@link mapPointPt}). Frames nest; {@link popFrame} leaves the last. */
109
- export function pushFrame(ctx, m) {
110
- pushTransform(ctx, m);
111
- const frames = ctx.frames ?? (ctx.frames = []);
112
- const outer = frames[frames.length - 1];
113
- const composed = outer ? composeMatrix(m, outer) : m;
114
- frames.push(composed);
115
- ctx.mapRectPt = (r) => mapRectThrough(composed, r);
116
- }
117
- /** Leave the frame {@link pushFrame} entered last. */
118
- export function popFrame(ctx) {
119
- popTransform(ctx);
120
- const frames = ctx.frames;
121
- frames?.pop();
122
- const outer = frames?.[frames.length - 1];
123
- if (outer)
124
- ctx.mapRectPt = (r) => mapRectThrough(outer, r);
125
- else {
126
- delete ctx.mapRectPt;
127
- delete ctx.frames;
128
- }
129
- }
130
- /** A point `(x, y)` in points of the current frame, on the page. */
131
- export function mapPointPt(ctx, x, y) {
132
- const m = ctx.frames?.[ctx.frames.length - 1];
133
- return m ? applyMatrix(m, x, y) : { x, y };
134
- }
135
- /** The rect map of the frame around the current one (the page's for a
136
- * block turned inside a vertical page's flow), or undefined on the
137
- * page. */
138
- export function outerRectMap(ctx) {
139
- const frames = ctx.frames;
140
- const outer = frames && frames.length > 1 ? frames[frames.length - 2] : undefined;
141
- return outer ? (r) => mapRectThrough(outer, r) : undefined;
142
- }
143
- /** Map a rect `[x1, y1, x2, y2]` through `m`: the box its four corners
144
- * cover, min / max (a frame turned by any angle, #910, covers more than
145
- * the box of two opposite corners). */
146
- export function mapRectThrough(m, rect) {
147
- const pts = [
148
- applyMatrix(m, rect[0], rect[1]),
149
- applyMatrix(m, rect[2], rect[1]),
150
- applyMatrix(m, rect[0], rect[3]),
151
- applyMatrix(m, rect[2], rect[3]),
152
- ];
153
- const xs = pts.map((p) => p.x);
154
- const ys = pts.map((p) => p.y);
155
- return [Math.min(...xs), Math.min(...ys), Math.max(...xs), Math.max(...ys)];
156
- }
157
- /** Inline colour swatch (`:swatch{…}`): a square of `sidePx` on the
158
- * baseline, filled with `fill` (hex) when resolved and outlined in `ink`. */
159
- export function drawSwatchPx(ctx, xPx, baselinePx, sidePx, fill, ink) {
160
- const { scale, pageHeightPt } = ctx;
161
- const strokePx = Math.max(0.5, sidePx * 0.06);
162
- const x = (xPx + strokePx / 2) * scale;
163
- const y = pageHeightPt - (baselinePx - strokePx / 2) * scale;
164
- const side = (sidePx - strokePx) * scale;
165
- const fillColor = fill ? colorFromHex(fill, ctx.colorSpace) : undefined;
166
- const fillAlpha = alphaOf(fillColor);
167
- const inkAlpha = alphaOf(ink);
168
- ctx.page.drawRectangle({
169
- x,
170
- y,
171
- width: side,
172
- height: side,
173
- ...(fillColor ? { color: fillColor } : {}),
174
- borderColor: ink,
175
- borderWidth: Math.max(0.01, strokePx * scale),
176
- ...(fillColor && fillAlpha < 1 ? { opacity: fillAlpha } : {}),
177
- ...(inkAlpha < 1 ? { borderOpacity: inkAlpha } : {}),
178
- });
179
- }
180
- /** A swatch with a shape of its own (#1104, see `swatchGeometry`): the
181
- * fill, then the outline stroked inside the square's edge, both with
182
- * rounded corners. */
183
- export function drawShapedSwatchPx(ctx, g, fill, ink) {
184
- const r = g.radius;
185
- if (fill)
186
- fillOutlinePx(ctx, { x: g.x, y: g.y, width: g.side, height: g.side, radii: [r, r, r, r] }, colorFromHex(fill, ctx.colorSpace));
187
- if (g.stroke > 0) {
188
- const half = g.stroke / 2;
189
- const ri = Math.max(0, r - half);
190
- strokeOutlinePx(ctx, { x: g.x + half, y: g.y + half, width: g.side - g.stroke, height: g.side - g.stroke, radii: [ri, ri, ri, ri] }, ink, g.stroke);
191
- }
192
- }
193
- export function makeScale(dpi) {
194
- return 72 / dpi;
195
- }
196
- export function whiteColor(colorSpace) {
197
- if (colorSpace === 'cmyk' || isPrintMode(colorSpace))
198
- return cmyk(0, 0, 0, 0);
199
- if (colorSpace === 'grayscale')
200
- return grayscale(1);
201
- return rgb(1, 1, 1);
202
- }
203
- // Memoized: called per text segment during rendering, while a document only
204
- // uses a handful of colors. pdf-lib Color objects are plain immutable
205
- // descriptors, so sharing instances is safe.
206
- const colorCache = new Map();
207
- /**
208
- * The pdf-lib colour of a document colour string — `#rgb`, `#rrggbb`, with
209
- * an alpha channel (`#rgba`, `#rrggbbaa`), `rgb()` / `rgba()` or
210
- * `transparent` — in the output colour space. A translucent colour carries
211
- * its opacity along (see {@link alphaOf}); malformed input paints black.
212
- */
213
- export function colorFromHex(hex, colorSpace) {
214
- const key = `${modeKey(colorSpace)}|${hex}`;
215
- const cached = colorCache.get(key);
216
- if (cached)
217
- return cached;
218
- const c = hexToRgb(hex);
219
- let color;
220
- if (isPrintMode(colorSpace)) {
221
- // Through the output profile; PDF/X-1a has no transparency, so a
222
- // translucent colour is set as it would print over the paper.
223
- let alpha = colorAlpha(hex);
224
- let [r, g, b] = [c.r, c.g, c.b];
225
- if (alpha < 1 && colorSpace.flattenTransparency) {
226
- [r, g, b] = overPaper(r, g, b, alpha);
227
- alpha = 1;
228
- }
229
- const exact = colorSpace.authored.get(hex.slice(0, 7).toLowerCase());
230
- if (exact && alpha < 1 && colorSpace.flattenTransparency) {
231
- // A tint of the authored ink mix stands in for it over the paper.
232
- const t = colorAlpha(hex);
233
- color = cmyk(round4((exact.c / 100) * t), round4((exact.m / 100) * t), round4((exact.y / 100) * t), round4((exact.k / 100) * t));
234
- }
235
- else if (exact) {
236
- color = cmyk(round4(exact.c / 100), round4(exact.m / 100), round4(exact.y / 100), round4(exact.k / 100));
237
- }
238
- else {
239
- const k = colorSpace.transform.fromRgb(r, g, b);
240
- color = cmyk(round4(k.c), round4(k.m), round4(k.y), round4(k.k));
241
- }
242
- if (alpha < 1)
243
- color.alpha = alpha;
244
- colorCache.set(key, color);
245
- return color;
246
- }
247
- if (colorSpace === 'cmyk') {
248
- const k = rgbToCmyk(c);
249
- color = cmyk(k.c, k.m, k.y, k.k);
250
- }
251
- else if (colorSpace === 'grayscale') {
252
- color = grayscale(rgbToGrayscale(c));
253
- }
254
- else {
255
- color = rgb(c.r, c.g, c.b);
256
- }
257
- const alpha = colorAlpha(hex);
258
- if (alpha < 1)
259
- color.alpha = alpha;
260
- colorCache.set(key, color);
261
- return color;
262
- }
263
- /** Four decimals: enough for a separation, and exact K 1 stays exact. */
264
- function round4(n) {
265
- return Math.round(n * 10000) / 10000;
266
- }
267
- /** Opacity of a colour made by {@link colorFromHex}, 0 … 1. */
268
- export function alphaOf(color) {
269
- return color?.alpha ?? 1;
270
- }
271
- /** ExtGState per distinct (fill alpha, stroke alpha) pair, per page. */
272
- const alphaStates = new WeakMap();
273
- const roundAlpha = (n) => Math.round(Math.max(0, Math.min(1, n)) * 1000) / 1000;
274
- /** The `gs` operator setting constant fill (`ca`) and stroke (`CA`) alpha,
275
- * one shared ExtGState per pair and page; null when both are opaque. */
276
- export function alphaStateOp(ctx, fillAlpha, strokeAlpha = 1) {
277
- const ca = roundAlpha(fillAlpha);
278
- const CA = roundAlpha(strokeAlpha);
279
- if (ca >= 1 && CA >= 1)
280
- return null;
281
- let states = alphaStates.get(ctx.page);
282
- if (!states) {
283
- states = new Map();
284
- alphaStates.set(ctx.page, states);
285
- }
286
- const key = `${ca}|${CA}`;
287
- let name = states.get(key);
288
- if (!name) {
289
- const dict = ctx.page.doc.context.obj({ Type: 'ExtGState', ca, CA });
290
- name = ctx.page.node.newExtGState('GSa', dict);
291
- states.set(key, name);
292
- }
293
- return setGraphicsState(name);
294
- }
295
- /** `op` as a one-element list when set, for spreading into operator runs. */
296
- export function opt(op) {
297
- return op ? [op] : [];
298
- }
299
- /** Back-compat alias — defaults to RGB. */
300
- export function rgbFromHex(hex) {
301
- return colorFromHex(hex, 'rgb');
302
- }
303
- export function fillRectPx(ctx, xPx, yPx, wPx, hPx, color, blendMode) {
304
- const { scale, pageHeightPt } = ctx;
305
- const x = xPx * scale;
306
- const y = pageHeightPt - (yPx + hPx) * scale;
307
- const width = wPx * scale;
308
- const height = hPx * scale;
309
- const alpha = alphaOf(color);
310
- if (alpha < 1 && !blendMode) {
311
- ctx.page.pushOperators(pushGraphicsState(), ...opt(alphaStateOp(ctx, alpha)), setFillingColor(color), rectangle(x, y, width, height), fill(), popGraphicsState());
312
- return;
313
- }
314
- ctx.page.drawRectangle({
315
- x,
316
- y,
317
- width,
318
- height,
319
- color,
320
- ...(blendMode ? { blendMode } : {}),
321
- ...(alpha < 1 ? { opacity: alpha } : {}),
322
- });
323
- }
324
- /** Fill several rectangles (px) in one colour with a single fill operation:
325
- * the viewer rasterises them as one shape, so rectangles that abut or
326
- * overlap leave no seam between them. */
327
- export function fillRectsPx(ctx, rects, color) {
328
- if (rects.length === 0)
329
- return;
330
- const { scale, pageHeightPt } = ctx;
331
- const alpha = alphaOf(color);
332
- ctx.page.pushOperators(pushGraphicsState(), ...(alpha < 1 ? opt(alphaStateOp(ctx, alpha)) : []), setFillingColor(color), ...rects.map((r) => rectangle(r.x * scale, pageHeightPt - (r.y + r.height) * scale, r.width * scale, r.height * scale)), fill(), popGraphicsState());
333
- }
334
- export function drawLinePx(ctx, x1Px, y1Px, x2Px, y2Px, color, thicknessPx) {
335
- const { scale, pageHeightPt } = ctx;
336
- const alpha = alphaOf(color);
337
- if (alpha < 1) {
338
- ctx.page.pushOperators(pushGraphicsState(), ...opt(alphaStateOp(ctx, 1, alpha)), setStrokingColor(color), setLineWidth(Math.max(0.01, thicknessPx * scale)), moveTo(x1Px * scale, pageHeightPt - y1Px * scale), lineTo(x2Px * scale, pageHeightPt - y2Px * scale), stroke(), popGraphicsState());
339
- return;
340
- }
341
- ctx.page.drawLine({
342
- start: { x: x1Px * scale, y: pageHeightPt - y1Px * scale },
343
- end: { x: x2Px * scale, y: pageHeightPt - y2Px * scale },
344
- color,
345
- thickness: Math.max(0.01, thicknessPx * scale),
346
- });
347
- }
348
- // Text is drawn a word at a time (the layout engine positions every
349
- // word), and pdf-lib's `drawText` shapes each call afresh through fontkit —
350
- // the same words, over and over. The show-text operator of a (font, text)
351
- // pair never changes, so it is shaped once and reused; the font's resource
352
- // key on a page likewise.
353
- const showByFont = new WeakMap();
354
- const ENCODE_CACHE_SLOTS = 50_000;
355
- /** Round a TJ adjustment to 1/100 of a text-space unit. */
356
- function tjNumber(n) {
357
- return Math.round(n * 100) / 100;
358
- }
359
- /**
360
- * The show-text operator for `text`, carrying the shaper's glyph positions.
361
- *
362
- * pdf-lib's `encodeText` shapes through fontkit but keeps only the glyph
363
- * ids, so a viewer advances each glyph by its raw width (the `W` array) and
364
- * the GPOS kerning the canvas measured with is lost: in a tightly kerned
365
- * face (Geist: `referencia` is 0.21 em narrower kerned) each word paints
366
- * wider than the layout engine placed it and eats the following space.
367
- * Emit a `TJ` array instead whose numbers restore every glyph's
368
- * `xAdvance` / `xOffset`; a run without adjustments stays a plain `Tj`.
369
- */
370
- export function showTextShaped(font, text, letterTj = 0) {
371
- // A tracked text with a ligature is another show at each tracking
372
- // ({@link letterTrackedParts}); it is not kept.
373
- if (letterTj !== 0) {
374
- const spaced = shownRun(font, text);
375
- if (spaced && spaced.joined.length > 0)
376
- return partsOperator(font, plainParts(letterTrackedParts(spaced, letterTj)));
377
- }
378
- let cache = showByFont.get(font);
379
- if (!cache) {
380
- cache = new Map();
381
- showByFont.set(font, cache);
382
- }
383
- // Text shaped in another language or script is another show.
384
- const key = shapingKey(text);
385
- const hit = cache.get(key);
386
- if (hit)
387
- return hit;
388
- const run = shownRun(font, text);
389
- let op;
390
- if (run)
391
- op = partsOperator(font, plainParts(run.parts));
392
- else {
393
- // A standard font: no shaper, pdf-lib's own encoding.
394
- noteMissingGlyphs(font, text);
395
- op = showText(font.encodeText(text));
396
- }
397
- if (cache.size >= ENCODE_CACHE_SLOTS)
398
- cache.clear();
399
- cache.set(key, op);
400
- return op;
401
- }
402
- /** The show-text operators of `text` in `font`'s face: one
403
- * ({@link showTextShaped}) for a face of one file, else one per run of
404
- * characters set in the same file (see `faceFiles.ts`). A glyph the
405
- * font's `/ToUnicode` map reads as other text is shown in a span of its
406
- * own ({@link glyphReads}), unless `read` is false: the caller reads the
407
- * text some other way (an `/ActualText` over all of it, a figure's
408
- * alternate text).
409
- *
410
- * In a document shaped in its own language the text is shaped word by
411
- * word, as the canvas shaped it (`wordRuns`, #770), and the words that
412
- * follow each other in one file are one operator; with `whole` it is
413
- * shaped as one string, as the text of a drawing is (a browser lays an
414
- * SVG's text out as it does a page's, not as its canvas does). */
415
- export function textShows(font, text, read = true, whole = false, letterTj = 0) {
416
- const runs = whole ? fileRuns(font, text) : wordRuns(font, text);
417
- if (runs.length > 1 && !whole && shapesAsCanvas()) {
418
- const joined = joinedShows(runs, read, letterTj);
419
- if (joined)
420
- return joined;
421
- }
422
- return runs.flatMap((run) => inRun(run, () => {
423
- const shown = read ? shownRun(run.font, run.text) : undefined;
424
- return shown?.reads ? partsShows(run.font, letterTrackedParts(shown, letterTj)) : [{ font: run.font, op: showTextShaped(run.font, run.text, letterTj) }];
425
- }));
426
- }
427
- /**
428
- * The character spacing a ligature is owed for the letters it joins, as a
429
- * `TJ` number (thousandths of the font size), for a text of `sizePx`
430
- * painted under a character spacing of `trackingPx` (#770); 0 when it is
431
- * owed none.
432
- *
433
- * The layout counts the tracking once a letter, and `Tc` follows every
434
- * glyph: a ligature, one glyph for two or three letters, advanced by one
435
- * tracking, and the word ended a tracking short of the width measured for
436
- * each letter joined (`shift` in Source Serif 4 at 19.58 px, 0.39 px a
437
- * letter: 0.39 px short). In a document shaped in its own language the
438
- * pen moves on after a ligature by the tracking of its other letters
439
- * ({@link letterTrackedParts}), as the canvas and the HTML set it; one
440
- * that keeps the host's language prints as it did.
441
- */
442
- export function letterTrackingTj(trackingPx, sizePx) {
443
- return trackingPx !== 0 && sizePx > 0 && shapesAsCanvas() ? (trackingPx / sizePx) * 1000 : 0;
444
- }
445
- /**
446
- * The parts of `run` with, after each glyph that stands for several
447
- * letters ({@link ShownRun.joined}), a pen movement of `letterTj` for
448
- * each letter but the first: the tracking `Tc` gave the glyph once and
449
- * the layout counted once a letter. The run's own parts when it holds no
450
- * such glyph or is not tracked.
451
- */
452
- function letterTrackedParts(run, letterTj) {
453
- if (letterTj === 0 || run.joined.length === 0)
454
- return run.parts;
455
- const out = [];
456
- // Glyphs shown so far, and the next entry of `joined` to place.
457
- let glyph = 0;
458
- let next = 0;
459
- for (const part of run.parts) {
460
- if (typeof part === 'number') {
461
- pushPart(out, part);
462
- continue;
463
- }
464
- const hex = typeof part === 'string' ? part : part.hex;
465
- const count = hex.length / 4;
466
- if (typeof part === 'object') {
467
- out.push(part);
468
- for (; next < run.joined.length && run.joined[next][0] < glyph + count; next++)
469
- pushPart(out, tjNumber(-run.joined[next][1] * letterTj));
470
- glyph += count;
471
- continue;
472
- }
473
- let from = 0;
474
- for (let k = 0; k < count && next < run.joined.length; k++) {
475
- if (run.joined[next][0] !== glyph + k)
476
- continue;
477
- pushPart(out, hex.slice(from * 4, (k + 1) * 4));
478
- pushPart(out, tjNumber(-run.joined[next][1] * letterTj));
479
- from = k + 1;
480
- next++;
481
- }
482
- if (from < count)
483
- pushPart(out, hex.slice(from * 4));
484
- glyph += count;
485
- }
486
- return out;
487
- }
488
- /** The show-text operators of `runs` (`wordRuns`), the runs that follow
489
- * each other in one file joined into one operator: the pen carries from
490
- * each to the next, and nothing is kerned across. Undefined when a run's
491
- * font carries no shaper (a standard font). */
492
- function joinedShows(runs, read, letterTj = 0) {
493
- const groups = [];
494
- for (const run of runs) {
495
- const shown = inRun(run, () => shownRun(run.font, run.text));
496
- if (!shown)
497
- return undefined;
498
- let group = groups[groups.length - 1];
499
- if (!group || group.font !== run.font) {
500
- group = { font: run.font, parts: [] };
501
- groups.push(group);
502
- }
503
- for (const part of letterTrackedParts(shown, letterTj))
504
- pushPart(group.parts, read || typeof part !== 'object' ? part : part.hex);
505
- }
506
- return groups.flatMap((g) => partsShows(g.font, g.parts));
507
- }
508
- function shapingFace(font) {
509
- const embedder = font.embedder;
510
- const face = embedder?.font;
511
- if (!face || typeof face.layout !== 'function' || !face.unitsPerEm)
512
- return undefined;
513
- return { face, features: embedder.fontFeatures };
514
- }
515
- /**
516
- * What the glyphs fontkit laid `text` out with are read as, where the
517
- * font's `/ToUnicode` map reads them as other text (#755); undefined when
518
- * the map reads every one as written, which is nearly always.
519
- *
520
- * The map is one per font, and a glyph keeps the characters it was first
521
- * given (fontkit makes a glyph once, with the characters of the text that
522
- * first showed it; `shapedText.ts` does the same for the glyphs HarfBuzz
523
- * shows). A glyph two characters share therefore reads as the first of
524
- * them the document set: the bracket a right-to-left run mirrors is the
525
- * glyph of the other bracket, so after `(١)` in an Arabic note the glyph
526
- * of `)` stands for `(`, and a later `(Brill, 2017)` in the same font read
527
- * back `(Brill, 2017(`. A form the shaper puts in a character's place
528
- * takes the characters of that character's own glyph, the wrong ones
529
- * with them. The same goes for any two characters the face's cmap draws
530
- * with one glyph: the hyphen-minus and the hyphen, the space and the
531
- * no-break space. Such a glyph is shown in a span whose `/ActualText` is
532
- * its character here, as `shapedText.ts` does for a cluster HarfBuzz
533
- * shaped: the first use of a glyph costs nothing, each use for another
534
- * character one span.
535
- *
536
- * The glyphs are walked against the text. A glyph whose own characters
537
- * are the next ones of the text reads right; one with no characters (the
538
- * second glyph of a decomposed letter) reads as nothing; one that holds
539
- * one character, and not the next of the text, is read as that next
540
- * character. Three glyphs are left as they read: `.notdef`, which stands
541
- * for every character the face lacks; the space glyph fontkit hides an
542
- * invisible character behind; and the space glyph, read as a space, under
543
- * a no-break space or any other space the face draws with it (a space the
544
- * face lacks reads as a space too, see `fallbackSpaces.ts`). The reads
545
- * hold only when the walk ends where the text does (every glyph accounted
546
- * for, one way or another); when it does not — a glyph for several
547
- * characters that holds others, a variation selector — the run is left as
548
- * it reads.
549
- */
550
- function glyphReads(face, glyphs, text) {
551
- // fontkit hands a right-to-left run's glyphs over in visual order: such
552
- // text is HarfBuzz's whenever it is loaded, and is not walked here.
553
- if (needsComplexShaping(text))
554
- return undefined;
555
- let reads;
556
- let space;
557
- // UTF-16 offset in `text` of the next character to read.
558
- let at = 0;
559
- for (let i = 0; i < glyphs.length; i++) {
560
- const glyph = glyphs[i];
561
- const own = glyph.codePoints;
562
- if (glyph.id !== 0 && (!own || own.length === 0))
563
- continue;
564
- let end = at;
565
- for (const cp of own ?? []) {
566
- if (text.codePointAt(end) !== cp) {
567
- end = -1;
568
- break;
569
- }
570
- end += cp > 0xffff ? 2 : 1;
571
- }
572
- if (end > at) {
573
- at = end;
574
- continue;
575
- }
576
- const cp = text.codePointAt(at);
577
- if (cp === undefined)
578
- return undefined;
579
- at += cp > 0xffff ? 2 : 1;
580
- if (glyph.id === 0)
581
- continue;
582
- if (isDefaultIgnorable(cp) && glyph.id === (space ??= cmapGlyphId(face, 0x20)))
583
- continue;
584
- if (own.length !== 1)
585
- return undefined;
586
- if (own[0] === 0x20 && isSpaceCharacter(cp))
587
- continue;
588
- reads ??= new Array(glyphs.length);
589
- reads[i] = String.fromCodePoint(cp);
590
- }
591
- return at === text.length ? reads : undefined;
592
- }
593
- /** The id of the glyph the face's cmap gives `cp` (0: none), read from the
594
- * cmap itself: `glyphForCodePoint` would make the glyph, with `cp` as the
595
- * characters it then keeps in the font's map. */
596
- function cmapGlyphId(face, cp) {
597
- const cmap = face._cmapProcessor;
598
- return typeof cmap?.lookup === 'function' ? cmap.lookup(cp) || 0 : 0;
599
- }
600
- /** The `TJ` array (hex glyph runs and adjustments) of a shaped run, with
601
- * the sum of the glyphs' own widths (their `W` entries), in thousandths
602
- * of the font size; undefined when the shaper's positions cannot be
603
- * matched to its glyphs. A glyph the font's map reads as other text is a
604
- * part of its own ({@link glyphReads}). */
605
- function shapedRun(font, text, encoded) {
606
- const shaping = shapingFace(font);
607
- if (!shaping)
608
- return undefined;
609
- const { face } = shaping;
610
- const run = face.layout(text, shaping.features);
611
- const hex = encoded.asString();
612
- const count = run.glyphs.length;
613
- if (count === 0 || hex.length !== count * 4 || run.positions.length !== count)
614
- return undefined;
615
- const reads = glyphReads(face, run.glyphs, text);
616
- // TJ numbers are thousandths of text space, subtracted from the pen: a
617
- // glyph drawn at `pen + xOffset` then advancing by `xAdvance` (not its
618
- // W width `advanceWidth`) is `-xOffset, glyph, -(xAdvance - w - xOffset)`.
619
- const toText = 1000 / face.unitsPerEm;
620
- const subsetFont = isSubsetFont(font);
621
- const parts = [];
622
- let pendingHex = '';
623
- let pendingAdjust = 0;
624
- const flushHex = () => {
625
- if (pendingHex)
626
- parts.push(pendingHex);
627
- pendingHex = '';
628
- };
629
- let widths = 0;
630
- // The glyphs that stand for several letters (a ligature), by index,
631
- // with the letters each joins beyond its first.
632
- const joined = [];
633
- for (let i = 0; i < count; i++) {
634
- const own = run.glyphs[i].codePoints;
635
- if (own && own.length > 1) {
636
- const letters = graphemesOf(String.fromCodePoint(...own)).length;
637
- if (letters > 1)
638
- joined.push([i, letters - 1]);
639
- }
640
- const pos = run.positions[i];
641
- const lead = tjNumber(pendingAdjust - pos.xOffset * toText);
642
- if (lead !== 0) {
643
- flushHex();
644
- parts.push(lead);
645
- }
646
- const cid = hex.slice(i * 4, i * 4 + 4);
647
- const read = reads?.[i];
648
- if (read !== undefined) {
649
- flushHex();
650
- parts.push({ hex: cid, text: read });
651
- }
652
- else
653
- pendingHex += cid;
654
- pendingAdjust = -(pos.xAdvance - run.glyphs[i].advanceWidth - pos.xOffset) * toText;
655
- widths += cid === '0000' && subsetFont ? MISSING_WIDTH : run.glyphs[i].advanceWidth * toText;
656
- }
657
- flushHex();
658
- return { parts, widths, joined };
659
- }
660
- /** The width a viewer gives a glyph with no `W` entry (no `DW` is written). */
661
- const MISSING_WIDTH = 1000;
662
- /** Whether pdf-lib embeds `font` as a subset, whose `W` array never lists
663
- * the notdef glyph (CID 0): a viewer advances it by {@link MISSING_WIDTH}. */
664
- function isSubsetFont(font) {
665
- return !!font.embedder?.subset;
666
- }
667
- function adjustedOperator(font, parts) {
668
- const array = PDFArray.withContext(font.doc.context);
669
- for (const part of parts) {
670
- array.push(typeof part === 'number' ? PDFNumber.of(part) : PDFHexString.of(part));
671
- }
672
- return PDFOperator.of(PDFOperatorNames.ShowTextAdjusted, [array]);
673
- }
674
- /** A plain `Tj` for a single glyph run (or none), else a `TJ`. */
675
- function partsOperator(font, parts) {
676
- if (parts.length === 0)
677
- return showText(PDFHexString.of(''));
678
- return parts.length === 1 && typeof parts[0] === 'string' ? showText(PDFHexString.of(parts[0])) : adjustedOperator(font, parts);
679
- }
680
- /** Append a part to a `TJ` array, merging it into a neighbouring glyph run
681
- * or number; a zero adjustment is dropped. A glyph read through a span
682
- * stays a part of its own. */
683
- function pushPart(parts, part) {
684
- const last = parts[parts.length - 1];
685
- if (typeof part === 'object')
686
- parts.push(part);
687
- else if (typeof part === 'number') {
688
- if (part === 0)
689
- return;
690
- if (typeof last === 'number')
691
- parts[parts.length - 1] = tjNumber(last + part);
692
- else
693
- parts.push(part);
694
- }
695
- else if (typeof last === 'string')
696
- parts[parts.length - 1] = last + part;
697
- else
698
- parts.push(part);
699
- }
700
- /** Glyphs and pen advance of a run of parts whose glyph widths sum to
701
- * `widths` (thousandths of the font size). */
702
- function shownRunOf(parts, widths, joined = []) {
703
- let advance = widths;
704
- let glyphs = 0;
705
- let reads = false;
706
- for (const part of parts) {
707
- if (typeof part === 'number')
708
- advance -= part;
709
- else if (typeof part === 'string')
710
- glyphs += part.length / 4;
711
- else {
712
- glyphs += part.hex.length / 4;
713
- reads = true;
714
- }
715
- }
716
- let letters = 0;
717
- for (const [, more] of joined)
718
- letters += more;
719
- return { parts, advance, glyphs, reads, joined, letters };
720
- }
721
- /** `parts` with every glyph read through the font's map: one show-text
722
- * array, as before any glyph took a span. */
723
- function plainParts(parts) {
724
- const out = [];
725
- for (const part of parts)
726
- pushPart(out, typeof part === 'object' ? part.hex : part);
727
- return out;
728
- }
729
- /**
730
- * The show-text operators of `parts` in `font`: one, or, around each glyph
731
- * read through a span ({@link ReadGlyph}), the span's `BDC`, the glyph and
732
- * its `EMC`. The glyphs and the pen adjustments are the same ones in the
733
- * same order, cut into several arrays: nothing moves on the page.
734
- */
735
- function partsShows(font, parts) {
736
- const shows = [];
737
- let plain = [];
738
- const flush = () => {
739
- if (plain.length > 0)
740
- shows.push({ font, op: partsOperator(font, plain) });
741
- plain = [];
742
- };
743
- for (const part of parts) {
744
- if (typeof part !== 'object') {
745
- pushPart(plain, part);
746
- continue;
747
- }
748
- flush();
749
- const props = font.doc.context.obj({ ActualText: PDFHexString.fromText(part.text) });
750
- shows.push({ font, op: PDFOperator.of(PDFOperatorNames.BeginMarkedContentSequence, [PDFName.of('Span'), props]) }, { font, op: showText(PDFHexString.of(part.hex)) }, { font, op: PDFOperator.of(PDFOperatorNames.EndMarkedContent) });
751
- }
752
- flush();
753
- if (shows.length === 0)
754
- shows.push({ font, op: partsOperator(font, []) });
755
- return shows;
756
- }
757
- /**
758
- * The parts of a text holding a space the face has no glyph for (a narrow
759
- * no-break space, a figure space…) or an invisible character (a word
760
- * joiner): painted as the canvas measured it, not as `.notdef` boxes (see
761
- * `fallbackSpaces.ts`). A missing space is the face's space glyph — text
762
- * extraction still finds a space there — moved to the browser's advance
763
- * for it; an invisible character is not drawn.
764
- */
765
- function fallbackRun(font, face, pieces) {
766
- const toText = 1000 / face.unitsPerEm;
767
- const spaceGlyph = face.hasGlyphForCodePoint(0x20) ? face.glyphForCodePoint(0x20) : undefined;
768
- const parts = [];
769
- let widths = 0;
770
- // The ligatures of the pieces, by their glyphs' index in the whole run.
771
- const joined = [];
772
- let glyphs = 0;
773
- const glyphsOf = (shown) => shown.reduce((n, part) => n + (typeof part === 'number' ? 0 : (typeof part === 'string' ? part : part.hex).length / 4), 0);
774
- for (const piece of pieces) {
775
- if ('space' in piece) {
776
- if (spaceGlyph) {
777
- // The face's space glyph, read as a space whatever the font's map
778
- // gives it (a no-break space set before any space, #755).
779
- const space = font.encodeText(' ');
780
- const shown = shapedRun(font, ' ', space)?.parts ?? [space.asString()];
781
- for (const part of shown)
782
- pushPart(parts, part);
783
- glyphs += glyphsOf(shown);
784
- pushPart(parts, tjNumber((spaceGlyph.advanceWidth - piece.space) * toText));
785
- widths += spaceGlyph.advanceWidth * toText;
786
- }
787
- else {
788
- pushPart(parts, tjNumber(-piece.space * toText));
789
- }
790
- continue;
791
- }
792
- const encoded = font.encodeText(piece.text);
793
- const shaped = shapedRun(font, piece.text, encoded);
794
- const shown = shaped?.parts ?? [encoded.asString()];
795
- for (const part of shown)
796
- pushPart(parts, part);
797
- for (const [at, more] of shaped?.joined ?? [])
798
- joined.push([glyphs + at, more]);
799
- glyphs += glyphsOf(shown);
800
- widths += shaped?.widths ?? font.widthOfTextAtSize(piece.text, 1000);
801
- }
802
- return shownRunOf(parts, widths, joined);
803
- }
804
- const runsByFont = new WeakMap();
805
- /** `text` as {@link showTextShaped} paints it, with the advance and glyph
806
- * count it leaves (cached per font). Undefined when the font carries no
807
- * shaper to measure it with. */
808
- function shownRun(font, text) {
809
- let cache = runsByFont.get(font);
810
- if (!cache) {
811
- cache = new Map();
812
- runsByFont.set(font, cache);
813
- }
814
- const key = shapingKey(text);
815
- const hit = cache.get(key);
816
- if (hit)
817
- return hit;
818
- const shaping = shapingFace(font);
819
- if (!shaping)
820
- return undefined;
821
- noteMissingGlyphs(font, text);
822
- const pieces = fallbackPieces(shaping.face, text);
823
- let run;
824
- if (pieces) {
825
- run = fallbackRun(font, shaping.face, pieces);
826
- }
827
- else {
828
- const encoded = font.encodeText(text);
829
- const hex = encoded.asString();
830
- const shaped = shapedRun(font, text, encoded);
831
- run = shownRunOf(shaped?.parts ?? (hex ? [hex] : []), shaped?.widths ?? font.widthOfTextAtSize(text, 1000), shaped?.joined);
832
- }
833
- if (cache.size >= ENCODE_CACHE_SLOTS)
834
- cache.clear();
835
- cache.set(key, run);
836
- return run;
837
- }
838
- /** How far `text` moves the pen in `font`'s face at `sizePx`, px, with
839
- * `trackingPx` after every glyph (as {@link showTextShaped} paints it,
840
- * each run in the file of the face that has it), and after every letter
841
- * a ligature joins in a document shaped in its own language
842
- * ({@link letterTrackingTj}); and how many glyphs it shows. */
843
- export function textAdvancePx(font, text, sizePx, trackingPx = 0) {
844
- let advance = 0;
845
- let glyphs = 0;
846
- // Letters of ligatures beyond each one's first.
847
- let letters = 0;
848
- const perLetter = trackingPx !== 0 && shapesAsCanvas();
849
- for (const word of wordRuns(font, text)) {
850
- const { font: file, text: part } = word;
851
- const run = inRun(word, () => shownRun(file, part));
852
- if (!run) {
853
- advance += file.widthOfTextAtSize(part, sizePx);
854
- glyphs += [...part].length;
855
- continue;
856
- }
857
- advance += (run.advance / 1000) * sizePx;
858
- glyphs += run.glyphs;
859
- if (perLetter)
860
- letters += run.letters;
861
- }
862
- return { advance: advance + (glyphs + letters) * trackingPx, glyphs };
863
- }
864
- /** How far (px) the characters of a segment, each at its own advance, may
865
- * be from the width the layout gave the segment and still be the ones it
866
- * measured. A browser's sum of advances is not the font's to the last
867
- * digit: Chrome sets an em of Noto Serif JP 15.62 px wide at 15.625 px,
868
- * 0.135 px less over a line of 27 characters. Half a thousandth of the
869
- * width covers that and stays under a kerned pair. */
870
- function cellTolerance(widthPx) {
871
- return Math.max(0.02, 0.0005 * Math.abs(widthPx));
872
- }
873
- /**
874
- * The characters of `text`, a segment of a line of the CJK composer
875
- * `widthPx` wide, each with its own advance, when the layout measured them
876
- * one by one and a shaper sets them otherwise together (#770); undefined
877
- * for any other text, which is painted as it is.
878
- *
879
- * The composer measures every CJK character alone and lets the ones that
880
- * advance alike share a segment, which is then one string to a renderer.
881
- * A face may kern inside it: Noto Serif JP sets `す。` 0.08 em closer
882
- * and `れ、` 0.06 em further apart, Noto Sans JP `す。` 0.1 em closer,
883
- * so the glyphs after the pair stood off their places and the segment
884
- * ended off its width. A line does not say how its segments were measured,
885
- * so the PDF asks its own shaper, as it does for the pieces of a word
886
- * (`wordPieces.ts`): the characters are painted one by one, each where the
887
- * one before ends, when together they are as wide as the layout measured
888
- * the segment ({@link cellTolerance}) and nearer to it than the string
889
- * shaped as the canvas shapes it. A Western run, measured whole, keeps its
890
- * kerning: whole, it is as wide as it was measured.
891
- *
892
- * `trackingPx` is the character spacing the segment is painted with. Only
893
- * in a document shaped in its own language; one that keeps the host's
894
- * prints its segments as it always did.
895
- */
896
- export function cellPieces(font, text, widthPx, sizePx, trackingPx = 0) {
897
- if (text.length < 2 || !(sizePx > 0) || !shapesAsCanvas() || needsComplexShaping(text))
898
- return undefined;
899
- const graphemes = graphemesOf(text);
900
- if (graphemes.length < 2)
901
- return undefined;
902
- const whole = Math.abs(textAdvancePx(font, text, sizePx, trackingPx).advance - widthPx);
903
- // Shaped whole it is where it was measured: nothing kerns inside.
904
- if (whole <= 0.02)
905
- return undefined;
906
- const pieces = graphemes.map((g) => ({ text: g, width: textAdvancePx(font, g, sizePx, trackingPx).advance }));
907
- let sum = 0;
908
- for (const piece of pieces)
909
- sum += piece.width;
910
- const apart = Math.abs(sum - widthPx);
911
- return apart <= cellTolerance(widthPx) && apart < whole ? pieces : undefined;
912
- }
913
- /** Below this (thousandths of the font size) the pen is left where the
914
- * glyphs put it and the difference is carried to the next piece: the
915
- * layout and the embedded face agree on almost every word, and the
916
- * `TJ` then holds only the kerning. */
917
- const MEASURED_TOLERANCE = 0.5;
918
- /**
919
- * One show-text operator for a line's pieces (its words and spaces, each
920
- * with the width the layout measured it at): every piece starts where the
921
- * layout put it (EF-137). The face's own glyphs set each piece, a `TJ`
922
- * number after it moves the pen to where the next one starts, so a glyph
923
- * the embedded face lacks (measured by the browser in another font) or
924
- * any other difference between the two measures never shifts the words
925
- * after it. Spaces stay real space glyphs, for text extraction.
926
- * `trackingPx` is the character spacing (`Tc`) the line is painted with,
927
- * added after every glyph. Undefined when the font carries no shaper.
928
- */
929
- export function measuredTextOperator(font, pieces, sizePx, trackingPx = 0) {
930
- const shows = measuredTextShows(font, pieces, sizePx, trackingPx, false);
931
- return shows && shows.length === 1 ? shows[0].op : undefined;
932
- }
933
- /**
934
- * {@link measuredTextOperator} for a face of any number of files: the
935
- * pieces cut into runs by file (see `faceFiles.ts`), one show-text
936
- * operator per stretch set in the same file. The pen carries across the
937
- * font switches, so every piece still starts where the layout put it. A
938
- * glyph the font's `/ToUnicode` map reads as other text is shown in a span
939
- * of its own between two of them ({@link glyphReads}).
940
- */
941
- export function measuredTextShows(font, pieces, sizePx, trackingPx = 0,
942
- /** False when the caller reads the text some other way: no glyph takes
943
- * a span of its own (see {@link textShows}). */
944
- read = true) {
945
- if (!(sizePx > 0))
946
- return undefined;
947
- const groups = [];
948
- let group;
949
- const tracking = (trackingPx / sizePx) * 1000;
950
- // What a ligature is owed for the letters it joins (#770).
951
- const letterTj = letterTrackingTj(trackingPx, sizePx);
952
- // How far the pen is right of where the layout wants it, in thousandths.
953
- let ahead = 0;
954
- for (let i = 0; i < pieces.length; i++) {
955
- const piece = pieces[i];
956
- let advance = 0;
957
- for (const word of wordRuns(font, piece.text)) {
958
- const file = word.font;
959
- const run = inRun(word, () => shownRun(file, word.text));
960
- if (!run)
961
- return undefined;
962
- if (!group || group.font !== file) {
963
- group = { font: file, parts: [] };
964
- groups.push(group);
965
- }
966
- for (const part of letterTrackedParts(run, letterTj))
967
- pushPart(group.parts, read || typeof part !== 'object' ? part : part.hex);
968
- advance += run.advance + run.glyphs * tracking + (letterTj !== 0 ? run.letters * letterTj : 0);
969
- }
970
- ahead += advance - (piece.width / sizePx) * 1000;
971
- if (i < pieces.length - 1 && Math.abs(ahead) >= MEASURED_TOLERANCE) {
972
- const n = tjNumber(ahead);
973
- if (!group) {
974
- group = { font, parts: [] };
975
- groups.push(group);
976
- }
977
- pushPart(group.parts, n);
978
- ahead -= n;
979
- }
980
- }
981
- if (groups.length === 0)
982
- groups.push({ font, parts: [] });
983
- return groups.flatMap((g) => partsShows(g.font, g.parts));
984
- }
985
- const fontKeysByPage = new WeakMap();
986
- export function fontKeyOn(page, font) {
987
- let keys = fontKeysByPage.get(page);
988
- if (!keys) {
989
- keys = new Map();
990
- fontKeysByPage.set(page, keys);
991
- }
992
- let key = keys.get(font);
993
- if (!key) {
994
- key = page.node.newFontDictionary(font.name, font.ref);
995
- keys.set(font, key);
996
- }
997
- return key;
998
- }
999
45
  /** The outline of text shown invisibly (see {@link TextOutline.invisible}). */
1000
46
  export function invisibleText(color) {
1001
47
  return { color, widthPx: 0, invisible: true };
@@ -1122,230 +168,4 @@ export function drawMeasuredTextPx(ctx, pieces, xPx, baselinePx, font, sizePx, c
1122
168
  pushTextObject(ctx, shows, xPx, baselinePx, font, sizePx, color, undefined, actualText, advance);
1123
169
  return true;
1124
170
  }
1125
- /**
1126
- * The operators pdf-lib's `drawText` emits, around the show-text operators
1127
- * of a text: one, or one per file of a face made of several (each after
1128
- * its own `Tf`; the pen carries over). With `actualText` the show-text
1129
- * operators sit in a `/Span` marked-content sequence whose `/ActualText` is
1130
- * that text (PDF 1.7 §14.9.4), inside the text object, where readers expect
1131
- * it: copying, text extraction and assistive technology read it instead of
1132
- * the glyphs. The span has no MCID, so in a tagged PDF it nests in the
1133
- * structure sequence open around it.
1134
- *
1135
- * On a mirrored page (`ctx.mirror`) the text matrix turns the glyphs back
1136
- * about the run's box, `[xPx, xPx + advancePx]` (`advancePx`, else the
1137
- * advance {@link drawTextPx} measured): the pen starts at the box's right
1138
- * edge and runs leftwards in the flow, which the page's mirror shows
1139
- * left to right on the sheet.
1140
- */
1141
- export function pushTextObject(ctx, shows, xPx, baselinePx, font, sizePx, color, outline, actualText, advancePx) {
1142
- const { scale, pageHeightPt } = ctx;
1143
- outline ??= ctx.textOutline;
1144
- const textMatrix = ctx.mirror
1145
- ? setTextMatrix(-1, 0, 0, 1, (xPx + (advancePx ?? ctx.runAdvancePx ?? 0)) * scale, pageHeightPt - baselinePx * scale)
1146
- : setTextMatrix(1, 0, 0, 1, xPx * scale, pageHeightPt - baselinePx * scale);
1147
- // Text render mode 2 fills then strokes the glyphs; mode 1 only strokes.
1148
- const outlineOps = outline?.invisible
1149
- ? [setTextRenderingMode(TextRenderingMode.Invisible)]
1150
- : outline && outline.widthPx > 0
1151
- ? [
1152
- setStrokingColor(outline.color),
1153
- setLineWidth(outline.widthPx * scale),
1154
- setTextRenderingMode(outline.hollow ? TextRenderingMode.Outline : TextRenderingMode.FillAndOutline),
1155
- ]
1156
- : [];
1157
- // The first show's font is set with the text matrix; each later show in
1158
- // another file of the face switches to it.
1159
- const first = shows[0]?.font ?? font;
1160
- const body = [];
1161
- let current = first;
1162
- for (const show of shows) {
1163
- if (show.font !== current) {
1164
- body.push(setFontAndSize(fontKeyOn(ctx.page, show.font), sizePx * scale));
1165
- current = show.font;
1166
- }
1167
- body.push(show.op);
1168
- }
1169
- // The operators pdf-lib's `drawText` emits, with the encoding cached.
1170
- ctx.page.pushOperators(pushGraphicsState(),
1171
- // The outline's own alpha is the stroke alpha of the glyphs.
1172
- ...opt(alphaStateOp(ctx, alphaOf(color), outlineOps.length > 0 ? alphaOf(outline.color) : 1)), ...outlineOps, beginText(), setFillingColor(color), setFontAndSize(fontKeyOn(ctx.page, first), sizePx * scale), textMatrix, ...(actualText === undefined ? body : withActualText(ctx, actualText, body)), endText(), popGraphicsState());
1173
- }
1174
- /** `shows` inside a `/Span` whose `/ActualText` is `text`. */
1175
- export function withActualText(ctx, text, shows) {
1176
- // A property list (`<< /ActualText … >>`) is an inline dictionary operand
1177
- // of `BDC`; pdf-lib types operator arguments narrowly but serialises it.
1178
- const props = ctx.page.doc.context.obj({ ActualText: PDFHexString.fromText(text) });
1179
- return [
1180
- PDFOperator.of(PDFOperatorNames.BeginMarkedContentSequence, [PDFName.of('Span'), props]),
1181
- ...shows,
1182
- PDFOperator.of(PDFOperatorNames.EndMarkedContent),
1183
- ];
1184
- }
1185
- /**
1186
- * Open a `/Span` marked-content sequence whose `/ActualText` is `text`
1187
- * around everything painted until {@link endActualTextSpan}: a whole line
1188
- * set in pieces. A composed CJK line is painted segment by segment with
1189
- * gaps between them — characters spread for justification, the space
1190
- * between Han and Latin — which text extraction reads as word spaces
1191
- * ("我 们 用 iPhone"); the span gives it the line as written. The tagger's
1192
- * open sequence is closed first, and again before the span ends, so the
1193
- * structure's sequences nest inside it.
1194
- */
1195
- export function beginActualTextSpan(ctx, text, state) {
1196
- ctx.tags?.close();
1197
- ctx.readSpans = (ctx.readSpans ?? 0) + 1;
1198
- const props = ctx.page.doc.context.obj({ ActualText: PDFHexString.fromText(text) });
1199
- ctx.page.pushOperators(...lineTextState(ctx, state), PDFOperator.of(PDFOperatorNames.BeginMarkedContentSequence, [PDFName.of('Span'), props]));
1200
- }
1201
- /** A text object that shows nothing and leaves the line's font, size and
1202
- * position as the text state: every piece of a line restores the
1203
- * graphics state it painted in, and a reader that places an
1204
- * `/ActualText` with the text state in force where the span begins or
1205
- * ends (poppler) would otherwise give the line no height and misplace it
1206
- * in reading order. The font is the file the line's first text is drawn
1207
- * from, so nothing is embedded for it. */
1208
- function lineTextState(ctx, state) {
1209
- const { scale, pageHeightPt } = ctx;
1210
- const font = fileRuns(state.font, state.text)[0]?.font ?? state.font;
1211
- return [
1212
- beginText(),
1213
- setFontAndSize(fontKeyOn(ctx.page, font), state.sizePx * scale),
1214
- setTextMatrix(1, 0, 0, 1, state.xPx * scale, pageHeightPt - state.baselinePx * scale),
1215
- endText(),
1216
- ];
1217
- }
1218
- /** The text a composed CJK line is read as when its segments are painted
1219
- * apart (see {@link beginActualTextSpan}): the segments' text, when one
1220
- * of them carries tracking, is a Han–Latin space, is a mark that gave up
1221
- * blank (its glyph painted over its neighbour's box, `inkOffset`), hangs,
1222
- * is a ruby base (the line reads its base, not the reading painted over
1223
- * it), a warichu note's part (read once, upper row first) or a character
1224
- * with kanbun marks (read with its 送り仮名, {@link readText}); else
1225
- * undefined. */
1226
- export function cjkLineText(segments) {
1227
- return segments.some((s) => s.autospace || s.tracking !== undefined || s.inkOffset !== undefined || s.hangs || s.ruby || s.warichu || s.kunten)
1228
- ? segments.map(readText).join('')
1229
- : undefined;
1230
- }
1231
- /** A segment's text as it is read: a character with kanbun marks (#430)
1232
- * is followed by its 送り仮名 (`學ビテ`, as kanbun is typed with them);
1233
- * its 返り点 are reading-order marks, left out. */
1234
- export function readText(s) {
1235
- return s.kunten?.okuri ? s.text + s.kunten.okuri : s.text;
1236
- }
1237
- /**
1238
- * The character spacing (`Tc`, px) a CJK mark that gave up blank is shown
1239
- * with (`inkOffset` set): painted `-inkOffset` before its box, its glyph
1240
- * then advances to where its segment ends, so the box a reader gives the
1241
- * glyph (its advance plus `Tc`) never runs over the next character's —
1242
- * poppler read a compressed `:` over the `「` after it as a line break.
1243
- * Undefined for any other segment, or when the font cannot measure it.
1244
- */
1245
- export function compressedMarkSpacingPx(font, seg, sizePx) {
1246
- if (seg.inkOffset === undefined || !(sizePx > 0))
1247
- return undefined;
1248
- let advance = 0;
1249
- let glyphs = 0;
1250
- for (const { font: file, text } of fileRuns(font, seg.text)) {
1251
- const run = shownRun(file, text);
1252
- if (!run)
1253
- return undefined;
1254
- advance += run.advance;
1255
- glyphs += run.glyphs;
1256
- }
1257
- // Character spacing follows every glyph: only a mark shown as one.
1258
- if (glyphs !== 1)
1259
- return undefined;
1260
- // A stretched glyph (`inkScale`, shown with `Tz`): the scaling applies
1261
- // to the character spacing as well.
1262
- return (seg.width - seg.inkOffset) / (seg.inkScale ?? 1) - (advance / 1000) * sizePx;
1263
- }
1264
- /** Close the span {@link beginActualTextSpan} opened, with the line's text
1265
- * state in force (see `lineTextState`). */
1266
- export function endActualTextSpan(ctx, state) {
1267
- ctx.tags?.close();
1268
- ctx.readSpans = Math.max(0, (ctx.readSpans ?? 0) - 1);
1269
- ctx.page.pushOperators(...lineTextState(ctx, state), PDFOperator.of(PDFOperatorNames.EndMarkedContent));
1270
- }
1271
- export function pushClipRect(ctx, xPx, yPx, wPx, hPx) {
1272
- const { scale, pageHeightPt } = ctx;
1273
- const x = xPx * scale;
1274
- const y = pageHeightPt - (yPx + hPx) * scale;
1275
- const width = wPx * scale;
1276
- const height = hPx * scale;
1277
- // Marked content must nest inside the graphics state it was opened in.
1278
- ctx.tags?.close();
1279
- ctx.page.pushOperators(pushGraphicsState(), rectangle(x, y, width, height), clip(), endPath());
1280
- }
1281
- export function popClip(ctx) {
1282
- ctx.tags?.close();
1283
- ctx.page.pushOperators(popGraphicsState());
1284
- }
1285
- /** Bézier handle length of a quarter circle, as a fraction of its radius. */
1286
- const KAPPA = 0.5522847498;
1287
- /** Path operators tracing a rounded outline given in px (top-down), in the
1288
- * points of the current frame. Corners of radius 0 stay square. */
1289
- function roundedOutlineOps(ctx, o) {
1290
- const { scale, pageHeightPt } = ctx;
1291
- const X = (px) => px * scale;
1292
- const Y = (py) => pageHeightPt - py * scale;
1293
- const { x, y, width: w, height: h } = o;
1294
- const [tl, tr, br, bl] = o.radii;
1295
- const k = KAPPA;
1296
- const ops = [moveTo(X(x + tl), Y(y)), lineTo(X(x + w - tr), Y(y))];
1297
- if (tr > 0)
1298
- ops.push(appendBezierCurve(X(x + w - tr + tr * k), Y(y), X(x + w), Y(y + tr - tr * k), X(x + w), Y(y + tr)));
1299
- ops.push(lineTo(X(x + w), Y(y + h - br)));
1300
- if (br > 0)
1301
- ops.push(appendBezierCurve(X(x + w), Y(y + h - br + br * k), X(x + w - br + br * k), Y(y + h), X(x + w - br), Y(y + h)));
1302
- ops.push(lineTo(X(x + bl), Y(y + h)));
1303
- if (bl > 0)
1304
- ops.push(appendBezierCurve(X(x + bl - bl * k), Y(y + h), X(x), Y(y + h - bl + bl * k), X(x), Y(y + h - bl)));
1305
- ops.push(lineTo(X(x), Y(y + tl)));
1306
- if (tl > 0)
1307
- ops.push(appendBezierCurve(X(x), Y(y + tl - tl * k), X(x + tl - tl * k), Y(y), X(x + tl), Y(y)));
1308
- ops.push(closePath());
1309
- return ops;
1310
- }
1311
- /** Push a graphics state clipped to a rounded outline (px); undo with
1312
- * {@link popClip}. */
1313
- export function pushClipOutline(ctx, o) {
1314
- ctx.tags?.close();
1315
- ctx.page.pushOperators(pushGraphicsState(), ...roundedOutlineOps(ctx, o), clip(), endPath());
1316
- }
1317
- /** Fill a rounded outline (px). */
1318
- export function fillOutlinePx(ctx, o, color) {
1319
- ctx.page.pushOperators(pushGraphicsState(), ...opt(alphaStateOp(ctx, alphaOf(color))), setFillingColor(color), ...roundedOutlineOps(ctx, o), fill(), popGraphicsState());
1320
- }
1321
- /** The box of an inline chip (`:chip[…]`) whose segment starts at `xPx` on
1322
- * a line of baseline `baselinePx`: the fill, then the outline stroked
1323
- * inside the box edge (the text runs are painted by the caller). */
1324
- export function drawChipBoxPx(ctx, chip, xPx, baselinePx) {
1325
- const x = xPx + chip.marginLeft;
1326
- const y = baselinePx - chip.ascent;
1327
- const h = chip.ascent + chip.descent;
1328
- const r = chip.borderRadius;
1329
- if (chip.background) {
1330
- fillOutlinePx(ctx, { x, y, width: chip.boxWidth, height: h, radii: [r, r, r, r] }, colorFromHex(chip.background, ctx.colorSpace));
1331
- }
1332
- if (chip.underline && chip.borderColor && chip.borderWidth > 0) {
1333
- // A blank to write on (#1227): a line under the box only.
1334
- const { scale, pageHeightPt } = ctx;
1335
- const w = chip.borderWidth;
1336
- const ly = pageHeightPt - (y + h - w / 2) * scale;
1337
- const color = colorFromHex(chip.borderColor, ctx.colorSpace);
1338
- const dash = chip.underline === 'dotted' ? [0.01 * scale, w * 2.2 * scale] : chip.underline === 'dashed' ? [w * 3 * scale, w * 2 * scale] : undefined;
1339
- ctx.page.pushOperators(pushGraphicsState(), ...opt(alphaStateOp(ctx, 1, alphaOf(color))), setStrokingColor(color), setLineWidth(Math.max(0.01, w * scale)), ...(dash ? [setDashPattern(dash, 0)] : []), ...(chip.underline === 'dotted' ? [setLineCap(LineCapStyle.Round)] : []), moveTo(x * scale, ly), lineTo((x + chip.boxWidth) * scale, ly), stroke(), popGraphicsState());
1340
- }
1341
- else if (chip.borderColor && chip.borderWidth > 0) {
1342
- const half = chip.borderWidth / 2;
1343
- const ri = Math.max(0, r - half);
1344
- strokeOutlinePx(ctx, { x: x + half, y: y + half, width: chip.boxWidth - chip.borderWidth, height: h - chip.borderWidth, radii: [ri, ri, ri, ri] }, colorFromHex(chip.borderColor, ctx.colorSpace), chip.borderWidth);
1345
- }
1346
- }
1347
- /** Stroke a rounded outline (px) `thicknessPx` wide, centred on it. */
1348
- export function strokeOutlinePx(ctx, o, color, thicknessPx) {
1349
- ctx.page.pushOperators(pushGraphicsState(), ...opt(alphaStateOp(ctx, 1, alphaOf(color))), setStrokingColor(color), setLineWidth(Math.max(0.01, thicknessPx * ctx.scale)), ...roundedOutlineOps(ctx, o), stroke(), popGraphicsState());
1350
- }
1351
171
  //# sourceMappingURL=primitives.js.map