pdf-codec 1.3.0 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (198) hide show
  1. package/README.md +38 -0
  2. package/dist/afm-widths-BoeTOK2r.cjs +826 -0
  3. package/dist/afm-widths-Dxucrg7D.js +779 -0
  4. package/dist/afm-widths-DyDq56Ph.d.cts +19 -0
  5. package/dist/afm-widths-DyDq56Ph.d.ts +19 -0
  6. package/dist/afm-widths.cjs +4 -0
  7. package/dist/afm-widths.d.cts +2 -0
  8. package/dist/afm-widths.d.ts +2 -0
  9. package/dist/afm-widths.js +2 -0
  10. package/dist/assets/stix-two-math-font.cjs +5 -0
  11. package/dist/assets/stix-two-math-font.d.cts +4 -0
  12. package/dist/assets/stix-two-math-font.d.ts +4 -0
  13. package/dist/assets/stix-two-math-font.js +4 -0
  14. package/dist/bytes/crc32.cjs +21 -0
  15. package/dist/bytes/crc32.d.cts +4 -0
  16. package/dist/bytes/crc32.d.ts +4 -0
  17. package/dist/bytes/crc32.js +20 -0
  18. package/dist/bytes/flate.cjs +53 -0
  19. package/dist/bytes/flate.d.cts +12 -0
  20. package/dist/bytes/flate.d.ts +12 -0
  21. package/dist/bytes/flate.js +49 -0
  22. package/dist/bytes/reader.cjs +60 -0
  23. package/dist/bytes/reader.d.cts +20 -0
  24. package/dist/bytes/reader.d.ts +20 -0
  25. package/dist/bytes/reader.js +58 -0
  26. package/dist/bytes/writer.cjs +37 -0
  27. package/dist/bytes/writer.d.cts +13 -0
  28. package/dist/bytes/writer.d.ts +13 -0
  29. package/dist/bytes/writer.js +35 -0
  30. package/dist/cmap.cjs +110 -0
  31. package/dist/cmap.d.cts +8 -0
  32. package/dist/cmap.d.ts +8 -0
  33. package/dist/cmap.js +109 -0
  34. package/dist/codec.cjs +34 -0
  35. package/dist/codec.d.cts +167 -0
  36. package/dist/codec.d.ts +167 -0
  37. package/dist/codec.js +32 -0
  38. package/dist/content-read.cjs +126 -0
  39. package/dist/content-read.d.cts +21 -0
  40. package/dist/content-read.d.ts +21 -0
  41. package/dist/content-read.js +125 -0
  42. package/dist/content-write.cjs +138 -0
  43. package/dist/content-write.d.cts +24 -0
  44. package/dist/content-write.d.ts +24 -0
  45. package/dist/content-write.js +137 -0
  46. package/dist/diagnostics.cjs +21 -0
  47. package/dist/diagnostics.d.cts +19 -0
  48. package/dist/diagnostics.d.ts +19 -0
  49. package/dist/diagnostics.js +18 -0
  50. package/dist/document.cjs +162 -0
  51. package/dist/document.d.cts +12 -0
  52. package/dist/document.d.ts +12 -0
  53. package/dist/document.js +161 -0
  54. package/dist/encoding.cjs +5 -0
  55. package/dist/encoding.d.cts +6 -0
  56. package/dist/encoding.d.ts +6 -0
  57. package/dist/encoding.js +2 -0
  58. package/dist/filters.cjs +223 -0
  59. package/dist/filters.d.cts +14 -0
  60. package/dist/filters.d.ts +14 -0
  61. package/dist/filters.js +218 -0
  62. package/dist/font-read.cjs +199 -0
  63. package/dist/font-read.d.cts +23 -0
  64. package/dist/font-read.d.ts +23 -0
  65. package/dist/font-read.js +198 -0
  66. package/dist/font-style.cjs +35 -0
  67. package/dist/font-style.d.cts +14 -0
  68. package/dist/font-style.d.ts +14 -0
  69. package/dist/font-style.js +34 -0
  70. package/dist/fonts.cjs +124 -0
  71. package/dist/fonts.d.cts +15 -0
  72. package/dist/fonts.d.ts +15 -0
  73. package/dist/fonts.js +122 -0
  74. package/dist/formula.cjs +0 -0
  75. package/dist/formula.d.cts +10 -0
  76. package/dist/formula.d.ts +10 -0
  77. package/dist/formula.js +0 -0
  78. package/dist/image/jpeg-info.cjs +71 -0
  79. package/dist/image/jpeg-info.d.cts +12 -0
  80. package/dist/image/jpeg-info.d.ts +12 -0
  81. package/dist/image/jpeg-info.js +70 -0
  82. package/dist/image/png-decode.cjs +181 -0
  83. package/dist/image/png-decode.d.cts +14 -0
  84. package/dist/image/png-decode.d.ts +14 -0
  85. package/dist/image/png-decode.js +180 -0
  86. package/dist/image/png-encode.cjs +64 -0
  87. package/dist/image/png-encode.d.cts +8 -0
  88. package/dist/image/png-encode.d.ts +8 -0
  89. package/dist/image/png-encode.js +63 -0
  90. package/dist/image/png-filter.cjs +95 -0
  91. package/dist/image/png-filter.d.cts +6 -0
  92. package/dist/image/png-filter.d.ts +6 -0
  93. package/dist/image/png-filter.js +93 -0
  94. package/dist/images-read.cjs +250 -0
  95. package/dist/images-read.d.cts +13 -0
  96. package/dist/images-read.d.ts +13 -0
  97. package/dist/images-read.js +249 -0
  98. package/dist/index.cjs +45 -5912
  99. package/dist/index.d.cts +21 -483
  100. package/dist/index.d.ts +21 -483
  101. package/dist/index.js +18 -5885
  102. package/dist/interpret.cjs +587 -0
  103. package/dist/interpret.d.cts +84 -0
  104. package/dist/interpret.d.ts +84 -0
  105. package/dist/interpret.js +586 -0
  106. package/dist/lexer.cjs +180 -0
  107. package/dist/lexer.d.cts +29 -0
  108. package/dist/lexer.d.ts +29 -0
  109. package/dist/lexer.js +179 -0
  110. package/dist/math-cmap.cjs +81 -0
  111. package/dist/math-cmap.d.cts +6 -0
  112. package/dist/math-cmap.d.ts +6 -0
  113. package/dist/math-cmap.js +80 -0
  114. package/dist/math-content-write.cjs +83 -0
  115. package/dist/math-content-write.d.cts +11 -0
  116. package/dist/math-content-write.d.ts +11 -0
  117. package/dist/math-content-write.js +81 -0
  118. package/dist/math-font-write.cjs +100 -0
  119. package/dist/math-font-write.d.cts +19 -0
  120. package/dist/math-font-write.d.ts +19 -0
  121. package/dist/math-font-write.js +99 -0
  122. package/dist/math-font.cjs +103 -0
  123. package/dist/math-font.d.cts +25 -0
  124. package/dist/math-font.d.ts +25 -0
  125. package/dist/math-font.js +102 -0
  126. package/dist/math-hmtx.cjs +17 -0
  127. package/dist/math-hmtx.d.cts +8 -0
  128. package/dist/math-hmtx.d.ts +8 -0
  129. package/dist/math-hmtx.js +16 -0
  130. package/dist/math-table.cjs +117 -0
  131. package/dist/math-table.d.cts +45 -0
  132. package/dist/math-table.d.ts +45 -0
  133. package/dist/math-table.js +116 -0
  134. package/dist/math-types-Ba8qAf1p.d.cts +82 -0
  135. package/dist/math-types-Ba8qAf1p.d.ts +82 -0
  136. package/dist/math-types.cjs +0 -0
  137. package/dist/math-types.d.cts +2 -0
  138. package/dist/math-types.d.ts +2 -0
  139. package/dist/math-types.js +0 -0
  140. package/dist/matrix-B3c2_a2f.d.cts +18 -0
  141. package/dist/matrix-B3c2_a2f.d.ts +18 -0
  142. package/dist/matrix.cjs +93 -0
  143. package/dist/matrix.d.cts +2 -0
  144. package/dist/matrix.d.ts +2 -0
  145. package/dist/matrix.js +83 -0
  146. package/dist/measure.cjs +54 -0
  147. package/dist/measure.d.cts +20 -0
  148. package/dist/measure.d.ts +20 -0
  149. package/dist/measure.js +53 -0
  150. package/dist/objects.cjs +100 -0
  151. package/dist/objects.d.cts +58 -0
  152. package/dist/objects.d.ts +58 -0
  153. package/dist/objects.js +84 -0
  154. package/dist/parse.cjs +230 -0
  155. package/dist/parse.d.cts +13 -0
  156. package/dist/parse.d.ts +13 -0
  157. package/dist/parse.js +228 -0
  158. package/dist/predictors.cjs +64 -0
  159. package/dist/predictors.d.cts +13 -0
  160. package/dist/predictors.d.ts +13 -0
  161. package/dist/predictors.js +62 -0
  162. package/dist/read.cjs +418 -0
  163. package/dist/read.d.cts +21 -0
  164. package/dist/read.d.ts +21 -0
  165. package/dist/read.js +413 -0
  166. package/dist/serialize.cjs +69 -0
  167. package/dist/serialize.d.cts +8 -0
  168. package/dist/serialize.d.ts +8 -0
  169. package/dist/serialize.js +66 -0
  170. package/dist/sfnt.cjs +52 -0
  171. package/dist/sfnt.d.cts +17 -0
  172. package/dist/sfnt.d.ts +17 -0
  173. package/dist/sfnt.js +46 -0
  174. package/dist/text-layout.cjs +224 -0
  175. package/dist/text-layout.d.cts +37 -0
  176. package/dist/text-layout.d.ts +37 -0
  177. package/dist/text-layout.js +222 -0
  178. package/dist/util/abort.cjs +7 -0
  179. package/dist/util/abort.d.cts +4 -0
  180. package/dist/util/abort.d.ts +4 -0
  181. package/dist/util/abort.js +6 -0
  182. package/dist/util/base64.cjs +48 -0
  183. package/dist/util/base64.d.cts +5 -0
  184. package/dist/util/base64.d.ts +5 -0
  185. package/dist/util/base64.js +46 -0
  186. package/dist/winansi.cjs +5 -0
  187. package/dist/winansi.d.cts +20 -0
  188. package/dist/winansi.d.ts +20 -0
  189. package/dist/winansi.js +2 -0
  190. package/dist/write.cjs +421 -0
  191. package/dist/write.d.cts +16 -0
  192. package/dist/write.d.ts +16 -0
  193. package/dist/write.js +419 -0
  194. package/dist/xref.cjs +325 -0
  195. package/dist/xref.d.cts +21 -0
  196. package/dist/xref.d.ts +21 -0
  197. package/dist/xref.js +324 -0
  198. package/package.json +9 -1
@@ -0,0 +1,138 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_afm_widths = require("./afm-widths-BoeTOK2r.cjs");
3
+ const require_bytes_writer = require("./bytes/writer.cjs");
4
+ const require_objects = require("./objects.cjs");
5
+ const require_matrix = require("./matrix.cjs");
6
+ const require_serialize = require("./serialize.cjs");
7
+ //#region src/content-write.ts
8
+ const BEZIER_KAPPA = .5522847498;
9
+ function writeRgbOperator(writer, color, operator) {
10
+ writer.writeAscii(`${require_serialize.formatNumber(color.r)} ${require_serialize.formatNumber(color.g)} ${require_serialize.formatNumber(color.b)} ${operator}\n`);
11
+ }
12
+ function writeMatrixOperator(writer, m, operator) {
13
+ writer.writeAscii(`${m.map(require_serialize.formatNumber).join(" ")} ${operator}\n`);
14
+ }
15
+ function anchorMatrix(xPt, yPt, rotationDeg) {
16
+ const r = require_matrix.rotationMatrix(rotationDeg ?? 0);
17
+ return [
18
+ r[0],
19
+ r[1],
20
+ r[2],
21
+ r[3],
22
+ xPt,
23
+ yPt
24
+ ];
25
+ }
26
+ function writeUnderline(writer, item, widthPt, measurer) {
27
+ const underline = measurer.underlineAtSize(item.font, item.sizePt);
28
+ writer.writeAscii("q\n");
29
+ writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "cm");
30
+ writeRgbOperator(writer, item.color, "rg");
31
+ writer.writeAscii(`0 ${require_serialize.formatNumber(underline.offsetPt)} ${require_serialize.formatNumber(widthPt)} ${require_serialize.formatNumber(underline.thicknessPt)} re\n`);
32
+ writer.writeAscii("f\n");
33
+ writer.writeAscii("Q\n");
34
+ }
35
+ function writeText(writer, item, context, substitutions) {
36
+ const font = context.resolveFont(item.font);
37
+ const encoded = require_afm_widths.encodeForShow(item.text, font.standardName);
38
+ substitutions.push(...encoded.substitutions);
39
+ const scalePercent = context.measurer.horizontalScaleFor(item.font) * 100;
40
+ writer.writeAscii("BT\n");
41
+ writer.writeAscii(`/${font.resourceName} ${require_serialize.formatNumber(item.sizePt)} Tf\n`);
42
+ writer.writeAscii(`${require_serialize.formatNumber(scalePercent)} Tz\n`);
43
+ writeRgbOperator(writer, item.color, "rg");
44
+ writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "Tm");
45
+ require_serialize.writeObject(writer, require_objects.pdfHexString(encoded.codes));
46
+ writer.writeAscii(" Tj\n");
47
+ writer.writeAscii("ET\n");
48
+ if (item.underline === true) {
49
+ const scale = context.measurer.horizontalScaleFor(item.font);
50
+ writeUnderline(writer, item, item.widthPt ?? encoded.width1000 / 1e3 * item.sizePt * scale, context.measurer);
51
+ }
52
+ }
53
+ function paintOperatorFor(fill, stroke, fillRule) {
54
+ const evenOdd = fillRule === "evenodd";
55
+ if (fill !== void 0 && stroke !== void 0) return evenOdd ? "B*" : "B";
56
+ if (fill !== void 0) return evenOdd ? "f*" : "f";
57
+ if (stroke !== void 0) return "S";
58
+ }
59
+ function formatPoint(x, y) {
60
+ return `${require_serialize.formatNumber(x)} ${require_serialize.formatNumber(y)}`;
61
+ }
62
+ function writeFillAndStroke(writer, fill, stroke) {
63
+ if (fill !== void 0) writeRgbOperator(writer, fill, "rg");
64
+ if (stroke !== void 0) {
65
+ writeRgbOperator(writer, stroke.color, "RG");
66
+ writer.writeAscii(`${require_serialize.formatNumber(stroke.widthPt)} w\n`);
67
+ }
68
+ }
69
+ function writeRect(writer, item) {
70
+ const paint = paintOperatorFor(item.fill, item.stroke);
71
+ if (paint === void 0) return;
72
+ writeFillAndStroke(writer, item.fill, item.stroke);
73
+ writer.writeAscii(`${require_serialize.formatNumber(item.xPt)} ${require_serialize.formatNumber(item.yPt)} ${require_serialize.formatNumber(item.widthPt)} ${require_serialize.formatNumber(item.heightPt)} re\n`);
74
+ writer.writeAscii(`${paint}\n`);
75
+ }
76
+ function writeLine(writer, item) {
77
+ writeRgbOperator(writer, item.color, "RG");
78
+ writer.writeAscii(`${require_serialize.formatNumber(item.widthPt)} w\n`);
79
+ writer.writeAscii(`${require_serialize.formatNumber(item.x1Pt)} ${require_serialize.formatNumber(item.y1Pt)} m ${require_serialize.formatNumber(item.x2Pt)} ${require_serialize.formatNumber(item.y2Pt)} l\n`);
80
+ writer.writeAscii("S\n");
81
+ }
82
+ function writeEllipse(writer, item) {
83
+ const paint = paintOperatorFor(item.fill, item.stroke);
84
+ if (paint === void 0) return;
85
+ writeFillAndStroke(writer, item.fill, item.stroke);
86
+ const cx = item.xPt + item.widthPt / 2;
87
+ const cy = item.yPt + item.heightPt / 2;
88
+ const rx = item.widthPt / 2;
89
+ const ry = item.heightPt / 2;
90
+ const kx = rx * BEZIER_KAPPA;
91
+ const ky = ry * BEZIER_KAPPA;
92
+ writer.writeAscii(`${formatPoint(cx + rx, cy)} m\n`);
93
+ writer.writeAscii(`${formatPoint(cx + rx, cy + ky)} ${formatPoint(cx + kx, cy + ry)} ${formatPoint(cx, cy + ry)} c\n`);
94
+ writer.writeAscii(`${formatPoint(cx - kx, cy + ry)} ${formatPoint(cx - rx, cy + ky)} ${formatPoint(cx - rx, cy)} c\n`);
95
+ writer.writeAscii(`${formatPoint(cx - rx, cy - ky)} ${formatPoint(cx - kx, cy - ry)} ${formatPoint(cx, cy - ry)} c\n`);
96
+ writer.writeAscii(`${formatPoint(cx + kx, cy - ry)} ${formatPoint(cx + rx, cy - ky)} ${formatPoint(cx + rx, cy)} c\n`);
97
+ writer.writeAscii("h\n");
98
+ writer.writeAscii(`${paint}\n`);
99
+ }
100
+ function writeSubpath(writer, subpath) {
101
+ writer.writeAscii(`${formatPoint(subpath.startXPt, subpath.startYPt)} m\n`);
102
+ for (const segment of subpath.segments) if (segment.kind === "line") writer.writeAscii(`${formatPoint(segment.xPt, segment.yPt)} l\n`);
103
+ else writer.writeAscii(`${formatPoint(segment.c1xPt, segment.c1yPt)} ${formatPoint(segment.c2xPt, segment.c2yPt)} ${formatPoint(segment.xPt, segment.yPt)} c\n`);
104
+ if (subpath.closed) writer.writeAscii("h\n");
105
+ }
106
+ function writePath(writer, item) {
107
+ const paint = paintOperatorFor(item.fill, item.stroke, item.fillRule);
108
+ if (paint === void 0) return;
109
+ writeFillAndStroke(writer, item.fill, item.stroke);
110
+ for (const subpath of item.subpaths) writeSubpath(writer, subpath);
111
+ writer.writeAscii(`${paint}\n`);
112
+ }
113
+ function writeImage(writer, item, context) {
114
+ const image = context.resolveImage(item.imageId);
115
+ const scaled = require_matrix.scaleMatrix(item.widthPt, item.heightPt);
116
+ const rotated = require_matrix.multiplyMatrices(scaled, require_matrix.rotationMatrix(item.rotationDeg ?? 0));
117
+ const placed = require_matrix.multiplyMatrices(rotated, require_matrix.translationMatrix(item.xPt, item.yPt));
118
+ writer.writeAscii("q\n");
119
+ writeMatrixOperator(writer, placed, "cm");
120
+ writer.writeAscii(`/${image.resourceName} Do\n`);
121
+ writer.writeAscii("Q\n");
122
+ }
123
+ function writeContentStream(items, context) {
124
+ const writer = new require_bytes_writer.ByteWriter();
125
+ const substitutions = [];
126
+ for (const item of items) if (item.kind === "text") writeText(writer, item, context, substitutions);
127
+ else if (item.kind === "image") writeImage(writer, item, context);
128
+ else if (item.kind === "rect") writeRect(writer, item);
129
+ else if (item.kind === "line") writeLine(writer, item);
130
+ else if (item.kind === "ellipse") writeEllipse(writer, item);
131
+ else if (item.kind === "path") writePath(writer, item);
132
+ return {
133
+ bytes: writer.toBytes(),
134
+ substitutions
135
+ };
136
+ }
137
+ //#endregion
138
+ exports.writeContentStream = writeContentStream;
@@ -0,0 +1,24 @@
1
+ import { r as StandardFontName } from "./afm-widths-DyDq56Ph.cjs";
2
+ import { TextMeasurer } from "./measure.cjs";
3
+ import { WinAnsiSubstitution } from "./winansi.cjs";
4
+ import { LayoutFont, LayoutItem } from "document-schema.js";
5
+ //#region src/content-write.d.ts
6
+ interface ResolvedFontResource {
7
+ readonly resourceName: string;
8
+ readonly standardName: StandardFontName;
9
+ }
10
+ interface ResolvedImageResource {
11
+ readonly resourceName: string;
12
+ }
13
+ interface ContentWriteContext {
14
+ readonly measurer: TextMeasurer;
15
+ resolveFont(font: LayoutFont): ResolvedFontResource;
16
+ resolveImage(imageId: string): ResolvedImageResource;
17
+ }
18
+ interface ContentStreamResult {
19
+ readonly bytes: Uint8Array<ArrayBuffer>;
20
+ readonly substitutions: readonly WinAnsiSubstitution[];
21
+ }
22
+ declare function writeContentStream(items: readonly LayoutItem[], context: ContentWriteContext): ContentStreamResult;
23
+ //#endregion
24
+ export { ContentStreamResult, ContentWriteContext, ResolvedFontResource, ResolvedImageResource, writeContentStream };
@@ -0,0 +1,24 @@
1
+ import { r as StandardFontName } from "./afm-widths-DyDq56Ph.js";
2
+ import { TextMeasurer } from "./measure.js";
3
+ import { WinAnsiSubstitution } from "./winansi.js";
4
+ import { LayoutFont, LayoutItem } from "document-schema.js";
5
+ //#region src/content-write.d.ts
6
+ interface ResolvedFontResource {
7
+ readonly resourceName: string;
8
+ readonly standardName: StandardFontName;
9
+ }
10
+ interface ResolvedImageResource {
11
+ readonly resourceName: string;
12
+ }
13
+ interface ContentWriteContext {
14
+ readonly measurer: TextMeasurer;
15
+ resolveFont(font: LayoutFont): ResolvedFontResource;
16
+ resolveImage(imageId: string): ResolvedImageResource;
17
+ }
18
+ interface ContentStreamResult {
19
+ readonly bytes: Uint8Array<ArrayBuffer>;
20
+ readonly substitutions: readonly WinAnsiSubstitution[];
21
+ }
22
+ declare function writeContentStream(items: readonly LayoutItem[], context: ContentWriteContext): ContentStreamResult;
23
+ //#endregion
24
+ export { ContentStreamResult, ContentWriteContext, ResolvedFontResource, ResolvedImageResource, writeContentStream };
@@ -0,0 +1,137 @@
1
+ import { o as encodeForShow } from "./afm-widths-Dxucrg7D.js";
2
+ import { ByteWriter } from "./bytes/writer.js";
3
+ import { pdfHexString } from "./objects.js";
4
+ import { multiplyMatrices, rotationMatrix, scaleMatrix, translationMatrix } from "./matrix.js";
5
+ import { formatNumber, writeObject } from "./serialize.js";
6
+ //#region src/content-write.ts
7
+ const BEZIER_KAPPA = .5522847498;
8
+ function writeRgbOperator(writer, color, operator) {
9
+ writer.writeAscii(`${formatNumber(color.r)} ${formatNumber(color.g)} ${formatNumber(color.b)} ${operator}\n`);
10
+ }
11
+ function writeMatrixOperator(writer, m, operator) {
12
+ writer.writeAscii(`${m.map(formatNumber).join(" ")} ${operator}\n`);
13
+ }
14
+ function anchorMatrix(xPt, yPt, rotationDeg) {
15
+ const r = rotationMatrix(rotationDeg ?? 0);
16
+ return [
17
+ r[0],
18
+ r[1],
19
+ r[2],
20
+ r[3],
21
+ xPt,
22
+ yPt
23
+ ];
24
+ }
25
+ function writeUnderline(writer, item, widthPt, measurer) {
26
+ const underline = measurer.underlineAtSize(item.font, item.sizePt);
27
+ writer.writeAscii("q\n");
28
+ writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "cm");
29
+ writeRgbOperator(writer, item.color, "rg");
30
+ writer.writeAscii(`0 ${formatNumber(underline.offsetPt)} ${formatNumber(widthPt)} ${formatNumber(underline.thicknessPt)} re\n`);
31
+ writer.writeAscii("f\n");
32
+ writer.writeAscii("Q\n");
33
+ }
34
+ function writeText(writer, item, context, substitutions) {
35
+ const font = context.resolveFont(item.font);
36
+ const encoded = encodeForShow(item.text, font.standardName);
37
+ substitutions.push(...encoded.substitutions);
38
+ const scalePercent = context.measurer.horizontalScaleFor(item.font) * 100;
39
+ writer.writeAscii("BT\n");
40
+ writer.writeAscii(`/${font.resourceName} ${formatNumber(item.sizePt)} Tf\n`);
41
+ writer.writeAscii(`${formatNumber(scalePercent)} Tz\n`);
42
+ writeRgbOperator(writer, item.color, "rg");
43
+ writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "Tm");
44
+ writeObject(writer, pdfHexString(encoded.codes));
45
+ writer.writeAscii(" Tj\n");
46
+ writer.writeAscii("ET\n");
47
+ if (item.underline === true) {
48
+ const scale = context.measurer.horizontalScaleFor(item.font);
49
+ writeUnderline(writer, item, item.widthPt ?? encoded.width1000 / 1e3 * item.sizePt * scale, context.measurer);
50
+ }
51
+ }
52
+ function paintOperatorFor(fill, stroke, fillRule) {
53
+ const evenOdd = fillRule === "evenodd";
54
+ if (fill !== void 0 && stroke !== void 0) return evenOdd ? "B*" : "B";
55
+ if (fill !== void 0) return evenOdd ? "f*" : "f";
56
+ if (stroke !== void 0) return "S";
57
+ }
58
+ function formatPoint(x, y) {
59
+ return `${formatNumber(x)} ${formatNumber(y)}`;
60
+ }
61
+ function writeFillAndStroke(writer, fill, stroke) {
62
+ if (fill !== void 0) writeRgbOperator(writer, fill, "rg");
63
+ if (stroke !== void 0) {
64
+ writeRgbOperator(writer, stroke.color, "RG");
65
+ writer.writeAscii(`${formatNumber(stroke.widthPt)} w\n`);
66
+ }
67
+ }
68
+ function writeRect(writer, item) {
69
+ const paint = paintOperatorFor(item.fill, item.stroke);
70
+ if (paint === void 0) return;
71
+ writeFillAndStroke(writer, item.fill, item.stroke);
72
+ writer.writeAscii(`${formatNumber(item.xPt)} ${formatNumber(item.yPt)} ${formatNumber(item.widthPt)} ${formatNumber(item.heightPt)} re\n`);
73
+ writer.writeAscii(`${paint}\n`);
74
+ }
75
+ function writeLine(writer, item) {
76
+ writeRgbOperator(writer, item.color, "RG");
77
+ writer.writeAscii(`${formatNumber(item.widthPt)} w\n`);
78
+ writer.writeAscii(`${formatNumber(item.x1Pt)} ${formatNumber(item.y1Pt)} m ${formatNumber(item.x2Pt)} ${formatNumber(item.y2Pt)} l\n`);
79
+ writer.writeAscii("S\n");
80
+ }
81
+ function writeEllipse(writer, item) {
82
+ const paint = paintOperatorFor(item.fill, item.stroke);
83
+ if (paint === void 0) return;
84
+ writeFillAndStroke(writer, item.fill, item.stroke);
85
+ const cx = item.xPt + item.widthPt / 2;
86
+ const cy = item.yPt + item.heightPt / 2;
87
+ const rx = item.widthPt / 2;
88
+ const ry = item.heightPt / 2;
89
+ const kx = rx * BEZIER_KAPPA;
90
+ const ky = ry * BEZIER_KAPPA;
91
+ writer.writeAscii(`${formatPoint(cx + rx, cy)} m\n`);
92
+ writer.writeAscii(`${formatPoint(cx + rx, cy + ky)} ${formatPoint(cx + kx, cy + ry)} ${formatPoint(cx, cy + ry)} c\n`);
93
+ writer.writeAscii(`${formatPoint(cx - kx, cy + ry)} ${formatPoint(cx - rx, cy + ky)} ${formatPoint(cx - rx, cy)} c\n`);
94
+ writer.writeAscii(`${formatPoint(cx - rx, cy - ky)} ${formatPoint(cx - kx, cy - ry)} ${formatPoint(cx, cy - ry)} c\n`);
95
+ writer.writeAscii(`${formatPoint(cx + kx, cy - ry)} ${formatPoint(cx + rx, cy - ky)} ${formatPoint(cx + rx, cy)} c\n`);
96
+ writer.writeAscii("h\n");
97
+ writer.writeAscii(`${paint}\n`);
98
+ }
99
+ function writeSubpath(writer, subpath) {
100
+ writer.writeAscii(`${formatPoint(subpath.startXPt, subpath.startYPt)} m\n`);
101
+ for (const segment of subpath.segments) if (segment.kind === "line") writer.writeAscii(`${formatPoint(segment.xPt, segment.yPt)} l\n`);
102
+ else writer.writeAscii(`${formatPoint(segment.c1xPt, segment.c1yPt)} ${formatPoint(segment.c2xPt, segment.c2yPt)} ${formatPoint(segment.xPt, segment.yPt)} c\n`);
103
+ if (subpath.closed) writer.writeAscii("h\n");
104
+ }
105
+ function writePath(writer, item) {
106
+ const paint = paintOperatorFor(item.fill, item.stroke, item.fillRule);
107
+ if (paint === void 0) return;
108
+ writeFillAndStroke(writer, item.fill, item.stroke);
109
+ for (const subpath of item.subpaths) writeSubpath(writer, subpath);
110
+ writer.writeAscii(`${paint}\n`);
111
+ }
112
+ function writeImage(writer, item, context) {
113
+ const image = context.resolveImage(item.imageId);
114
+ const scaled = scaleMatrix(item.widthPt, item.heightPt);
115
+ const rotated = multiplyMatrices(scaled, rotationMatrix(item.rotationDeg ?? 0));
116
+ const placed = multiplyMatrices(rotated, translationMatrix(item.xPt, item.yPt));
117
+ writer.writeAscii("q\n");
118
+ writeMatrixOperator(writer, placed, "cm");
119
+ writer.writeAscii(`/${image.resourceName} Do\n`);
120
+ writer.writeAscii("Q\n");
121
+ }
122
+ function writeContentStream(items, context) {
123
+ const writer = new ByteWriter();
124
+ const substitutions = [];
125
+ for (const item of items) if (item.kind === "text") writeText(writer, item, context, substitutions);
126
+ else if (item.kind === "image") writeImage(writer, item, context);
127
+ else if (item.kind === "rect") writeRect(writer, item);
128
+ else if (item.kind === "line") writeLine(writer, item);
129
+ else if (item.kind === "ellipse") writeEllipse(writer, item);
130
+ else if (item.kind === "path") writePath(writer, item);
131
+ return {
132
+ bytes: writer.toBytes(),
133
+ substitutions
134
+ };
135
+ }
136
+ //#endregion
137
+ export { writeContentStream };
@@ -0,0 +1,21 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/diagnostics.ts
3
+ const NOOP_DIAGNOSTIC_SINK = () => {};
4
+ var PdfParseError = class extends Error {
5
+ code;
6
+ constructor(code, message) {
7
+ super(message);
8
+ this.name = "PdfParseError";
9
+ this.code = code;
10
+ }
11
+ };
12
+ var PdfEncryptedError = class extends PdfParseError {
13
+ constructor(message = "this PDF is encrypted and unsupported") {
14
+ super("pdf/encrypted", message);
15
+ this.name = "PdfEncryptedError";
16
+ }
17
+ };
18
+ //#endregion
19
+ exports.NOOP_DIAGNOSTIC_SINK = NOOP_DIAGNOSTIC_SINK;
20
+ exports.PdfEncryptedError = PdfEncryptedError;
21
+ exports.PdfParseError = PdfParseError;
@@ -0,0 +1,19 @@
1
+ //#region src/diagnostics.d.ts
2
+ type PdfDiagnosticSeverity = 'info' | 'warning';
3
+ interface PdfDiagnostic {
4
+ readonly code: string;
5
+ readonly severity: PdfDiagnosticSeverity;
6
+ readonly message: string;
7
+ readonly pageIndex?: number;
8
+ }
9
+ type PdfDiagnosticSink = (diagnostic: PdfDiagnostic) => void;
10
+ declare const NOOP_DIAGNOSTIC_SINK: PdfDiagnosticSink;
11
+ declare class PdfParseError extends Error {
12
+ readonly code: string;
13
+ constructor(code: string, message: string);
14
+ }
15
+ declare class PdfEncryptedError extends PdfParseError {
16
+ constructor(message?: string);
17
+ }
18
+ //#endregion
19
+ export { NOOP_DIAGNOSTIC_SINK, PdfDiagnostic, PdfDiagnosticSeverity, PdfDiagnosticSink, PdfEncryptedError, PdfParseError };
@@ -0,0 +1,19 @@
1
+ //#region src/diagnostics.d.ts
2
+ type PdfDiagnosticSeverity = 'info' | 'warning';
3
+ interface PdfDiagnostic {
4
+ readonly code: string;
5
+ readonly severity: PdfDiagnosticSeverity;
6
+ readonly message: string;
7
+ readonly pageIndex?: number;
8
+ }
9
+ type PdfDiagnosticSink = (diagnostic: PdfDiagnostic) => void;
10
+ declare const NOOP_DIAGNOSTIC_SINK: PdfDiagnosticSink;
11
+ declare class PdfParseError extends Error {
12
+ readonly code: string;
13
+ constructor(code: string, message: string);
14
+ }
15
+ declare class PdfEncryptedError extends PdfParseError {
16
+ constructor(message?: string);
17
+ }
18
+ //#endregion
19
+ export { NOOP_DIAGNOSTIC_SINK, PdfDiagnostic, PdfDiagnosticSeverity, PdfDiagnosticSink, PdfEncryptedError, PdfParseError };
@@ -0,0 +1,18 @@
1
+ //#region src/diagnostics.ts
2
+ const NOOP_DIAGNOSTIC_SINK = () => {};
3
+ var PdfParseError = class extends Error {
4
+ code;
5
+ constructor(code, message) {
6
+ super(message);
7
+ this.name = "PdfParseError";
8
+ this.code = code;
9
+ }
10
+ };
11
+ var PdfEncryptedError = class extends PdfParseError {
12
+ constructor(message = "this PDF is encrypted and unsupported") {
13
+ super("pdf/encrypted", message);
14
+ this.name = "PdfEncryptedError";
15
+ }
16
+ };
17
+ //#endregion
18
+ export { NOOP_DIAGNOSTIC_SINK, PdfEncryptedError, PdfParseError };
@@ -0,0 +1,162 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_bytes_reader = require("./bytes/reader.cjs");
3
+ const require_lexer = require("./lexer.cjs");
4
+ const require_diagnostics = require("./diagnostics.cjs");
5
+ const require_objects = require("./objects.cjs");
6
+ const require_filters = require("./filters.cjs");
7
+ const require_parse = require("./parse.cjs");
8
+ const require_xref = require("./xref.cjs");
9
+ //#region src/document.ts
10
+ const MAX_RESOLVE_DEPTH = 64;
11
+ const INHERITABLE_PAGE_KEYS = [
12
+ "Resources",
13
+ "MediaBox",
14
+ "CropBox",
15
+ "Rotate"
16
+ ];
17
+ function openPdfDocument(bytes, sink) {
18
+ const xref = require_xref.readXref(bytes, sink);
19
+ if (require_objects.dictGet(xref.trailer, "Encrypt") !== void 0) throw new require_diagnostics.PdfEncryptedError();
20
+ const objectCache = /* @__PURE__ */ new Map();
21
+ const objStmCache = /* @__PURE__ */ new Map();
22
+ function fetchDirect(num, entry) {
23
+ const reader = new require_bytes_reader.ByteReader(bytes);
24
+ reader.seek(entry.offset);
25
+ const indirect = require_parse.parseIndirectObject(reader, sink);
26
+ if (indirect === void 0) {
27
+ sink({
28
+ code: "pdf/object-missing-value",
29
+ severity: "warning",
30
+ message: `object ${String(num)} could not be parsed at its recorded offset`
31
+ });
32
+ return require_objects.pdfNull();
33
+ }
34
+ return indirect.value;
35
+ }
36
+ function decodeObjectStream(streamObjNum) {
37
+ const cached = objStmCache.get(streamObjNum);
38
+ if (cached !== void 0) return cached;
39
+ const streamEntry = xref.entries.get(streamObjNum);
40
+ const streamObj = streamEntry?.type === "offset" ? fetchDirect(streamObjNum, streamEntry) : void 0;
41
+ if (streamObj?.kind !== "stream") {
42
+ sink({
43
+ code: "pdf/object-missing-value",
44
+ severity: "warning",
45
+ message: `object ${String(streamObjNum)} referenced as an object stream is not directly-located, or is not actually a stream`
46
+ });
47
+ objStmCache.set(streamObjNum, []);
48
+ return [];
49
+ }
50
+ const decoded = require_filters.decodeStream(streamObj.raw, streamObj.dict, sink);
51
+ const n = require_objects.asNumber(require_objects.dictGet(streamObj.dict, "N")) ?? 0;
52
+ const first = require_objects.asNumber(require_objects.dictGet(streamObj.dict, "First")) ?? 0;
53
+ const headerReader = new require_bytes_reader.ByteReader(decoded.bytes);
54
+ const relativeOffsets = [];
55
+ for (let i = 0; i < n; i++) {
56
+ const numTok = require_lexer.nextToken(headerReader);
57
+ const offTok = require_lexer.nextToken(headerReader);
58
+ if (numTok?.kind !== "number" || offTok?.kind !== "number") {
59
+ sink({
60
+ code: "pdf/object-missing-value",
61
+ severity: "warning",
62
+ message: `object stream ${String(streamObjNum)} header is truncated at entry ${String(i)}`
63
+ });
64
+ break;
65
+ }
66
+ relativeOffsets.push(offTok.value);
67
+ }
68
+ const values = relativeOffsets.map((relativeOffset) => {
69
+ const valueReader = new require_bytes_reader.ByteReader(decoded.bytes);
70
+ valueReader.seek(first + relativeOffset);
71
+ return require_parse.parseValue(valueReader, sink) ?? require_objects.pdfNull();
72
+ });
73
+ objStmCache.set(streamObjNum, values);
74
+ return values;
75
+ }
76
+ function fetch(num) {
77
+ const cached = objectCache.get(num);
78
+ if (cached !== void 0) return cached;
79
+ const entry = xref.entries.get(num);
80
+ if (entry === void 0) {
81
+ sink({
82
+ code: "pdf/object-missing-value",
83
+ severity: "warning",
84
+ message: `object ${String(num)} is referenced but not present in the cross-reference table`
85
+ });
86
+ return require_objects.pdfNull();
87
+ }
88
+ const value = entry.type === "offset" ? fetchDirect(num, entry) : decodeObjectStream(entry.streamObjNum)[entry.indexInStream] ?? require_objects.pdfNull();
89
+ objectCache.set(num, value);
90
+ return value;
91
+ }
92
+ function resolve(obj) {
93
+ let current = obj;
94
+ let depth = 0;
95
+ while (current?.kind === "ref" && depth < MAX_RESOLVE_DEPTH) {
96
+ current = fetch(current.num);
97
+ depth++;
98
+ }
99
+ if (depth >= MAX_RESOLVE_DEPTH) {
100
+ sink({
101
+ code: "pdf/reference-cycle",
102
+ severity: "warning",
103
+ message: "a chain of indirect references did not resolve within the depth limit; treating it as null"
104
+ });
105
+ return require_objects.pdfNull();
106
+ }
107
+ return current;
108
+ }
109
+ function resolveDict(obj) {
110
+ return require_objects.asDict(resolve(obj));
111
+ }
112
+ function requireCatalog(dict) {
113
+ if (dict === void 0 || !require_objects.isName(require_objects.dictGet(dict, "Type"), "Catalog")) throw new require_diagnostics.PdfParseError("pdf/no-root", "no resolvable /Root catalog was found, even after cross-reference recovery");
114
+ return dict;
115
+ }
116
+ const catalog = requireCatalog(resolveDict(require_objects.dictGet(xref.trailer, "Root")));
117
+ function pages() {
118
+ const pagesRoot = resolveDict(require_objects.dictGet(catalog, "Pages"));
119
+ if (pagesRoot === void 0) return [];
120
+ const result = [];
121
+ walkPageTree(pagesRoot, {}, /* @__PURE__ */ new Set(), result);
122
+ return result;
123
+ }
124
+ function walkPageTree(node, inherited, visited, result) {
125
+ if (visited.has(node)) {
126
+ sink({
127
+ code: "pdf/page-tree-cycle",
128
+ severity: "warning",
129
+ message: "the page tree contains a cycle; stopping descent at the repeated node"
130
+ });
131
+ return;
132
+ }
133
+ visited.add(node);
134
+ const merged = { ...inherited };
135
+ for (const key of INHERITABLE_PAGE_KEYS) {
136
+ const own = require_objects.dictGet(node, key);
137
+ if (own !== void 0) merged[key] = own;
138
+ }
139
+ const kids = require_objects.asArray(require_objects.dictGet(node, "Kids"));
140
+ if (kids === void 0) {
141
+ const entries = new Map(node.entries);
142
+ for (const key of INHERITABLE_PAGE_KEYS) if (!entries.has(key) && merged[key] !== void 0) entries.set(key, merged[key]);
143
+ result.push({
144
+ kind: "dict",
145
+ entries
146
+ });
147
+ return;
148
+ }
149
+ for (const kid of kids) {
150
+ const kidDict = resolveDict(kid);
151
+ if (kidDict !== void 0) walkPageTree(kidDict, merged, visited, result);
152
+ }
153
+ }
154
+ return {
155
+ trailer: xref.trailer,
156
+ resolve,
157
+ resolveDict,
158
+ pages
159
+ };
160
+ }
161
+ //#endregion
162
+ exports.openPdfDocument = openPdfDocument;
@@ -0,0 +1,12 @@
1
+ import { PdfDiagnosticSink } from "./diagnostics.cjs";
2
+ import { PdfDict, PdfObject } from "./objects.cjs";
3
+ //#region src/document.d.ts
4
+ interface PdfDocument {
5
+ readonly trailer: PdfDict;
6
+ resolve(obj: PdfObject | undefined): PdfObject | undefined;
7
+ resolveDict(obj: PdfObject | undefined): PdfDict | undefined;
8
+ pages(): PdfDict[];
9
+ }
10
+ declare function openPdfDocument(bytes: Uint8Array<ArrayBuffer>, sink: PdfDiagnosticSink): PdfDocument;
11
+ //#endregion
12
+ export { PdfDocument, openPdfDocument };
@@ -0,0 +1,12 @@
1
+ import { PdfDiagnosticSink } from "./diagnostics.js";
2
+ import { PdfDict, PdfObject } from "./objects.js";
3
+ //#region src/document.d.ts
4
+ interface PdfDocument {
5
+ readonly trailer: PdfDict;
6
+ resolve(obj: PdfObject | undefined): PdfObject | undefined;
7
+ resolveDict(obj: PdfObject | undefined): PdfDict | undefined;
8
+ pages(): PdfDict[];
9
+ }
10
+ declare function openPdfDocument(bytes: Uint8Array<ArrayBuffer>, sink: PdfDiagnosticSink): PdfDocument;
11
+ //#endregion
12
+ export { PdfDocument, openPdfDocument };