pdf-parser.js 5.2.30 → 5.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (189) hide show
  1. package/dist/{afm-widths-BHD2HZIL.cjs → afm-widths-C6U0SQj9.cjs} +5 -1448
  2. package/dist/{afm-widths-B_IGbGky.js → afm-widths-p3UfaXme.js} +3 -1398
  3. package/dist/afm-widths.cjs +1 -1
  4. package/dist/afm-widths.js +1 -1
  5. package/dist/annotations.d.cts +3 -3
  6. package/dist/annotations.d.ts +3 -3
  7. package/dist/attachments.d.cts +2 -2
  8. package/dist/attachments.d.ts +2 -2
  9. package/dist/builtin-encoding.cjs +4 -3
  10. package/dist/builtin-encoding.js +2 -1
  11. package/dist/cmap.d.cts +1 -1
  12. package/dist/cmap.d.ts +1 -1
  13. package/dist/content-read.d.cts +1 -1
  14. package/dist/content-read.d.ts +1 -1
  15. package/dist/content-write.cjs +1 -1
  16. package/dist/content-write.d.cts +1 -1
  17. package/dist/content-write.d.ts +1 -1
  18. package/dist/content-write.js +1 -1
  19. package/dist/diagnostics-C9FxfZ6H.d.cts +22 -0
  20. package/dist/diagnostics-C9FxfZ6H.d.ts +22 -0
  21. package/dist/diagnostics.d.cts +1 -21
  22. package/dist/diagnostics.d.ts +1 -21
  23. package/dist/document.d.cts +1 -1
  24. package/dist/document.d.ts +1 -1
  25. package/dist/{embedded-font-ByD7ZiQS.d.ts → embedded-font-CQZbTTDM.d.ts} +1 -1
  26. package/dist/{embedded-font-DgVBedw6.d.cts → embedded-font-CqePfvrl.d.cts} +1 -1
  27. package/dist/embedded-font-write.d.cts +1 -1
  28. package/dist/embedded-font-write.d.ts +1 -1
  29. package/dist/embedded-font.cjs +2 -2
  30. package/dist/embedded-font.d.cts +1 -1
  31. package/dist/embedded-font.d.ts +1 -1
  32. package/dist/embedded-font.js +2 -2
  33. package/dist/encoding-glyph-unicode.cjs +353 -0
  34. package/dist/encoding-glyph-unicode.d.cts +4 -0
  35. package/dist/encoding-glyph-unicode.d.ts +4 -0
  36. package/dist/encoding-glyph-unicode.js +352 -0
  37. package/dist/encoding-standard-tables.cjs +531 -0
  38. package/dist/encoding-standard-tables.d.cts +7 -0
  39. package/dist/encoding-standard-tables.d.ts +7 -0
  40. package/dist/encoding-standard-tables.js +527 -0
  41. package/dist/encoding-symbol-tables.cjs +533 -0
  42. package/dist/encoding-symbol-tables.d.cts +7 -0
  43. package/dist/encoding-symbol-tables.d.ts +7 -0
  44. package/dist/encoding-symbol-tables.js +529 -0
  45. package/dist/encoding.cjs +1 -9
  46. package/dist/encoding.d.cts +1 -9
  47. package/dist/encoding.d.ts +1 -9
  48. package/dist/encoding.js +2 -2
  49. package/dist/encrypt.d.cts +1 -1
  50. package/dist/encrypt.d.ts +1 -1
  51. package/dist/filters.d.cts +1 -1
  52. package/dist/filters.d.ts +1 -1
  53. package/dist/font-read.cjs +4 -3
  54. package/dist/font-read.d.cts +2 -2
  55. package/dist/font-read.d.ts +2 -2
  56. package/dist/font-read.js +2 -1
  57. package/dist/font-registry.d.cts +1 -1
  58. package/dist/font-registry.d.ts +1 -1
  59. package/dist/form.d.cts +2 -2
  60. package/dist/form.d.ts +2 -2
  61. package/dist/gdef-table-D0Hzn4KD.d.cts +15 -0
  62. package/dist/gdef-table-DL0V8wEZ.d.ts +15 -0
  63. package/dist/gdef-table.d.cts +1 -14
  64. package/dist/gdef-table.d.ts +1 -14
  65. package/dist/glyf-contours.d.cts +12 -1
  66. package/dist/glyf-contours.d.ts +12 -1
  67. package/dist/gsub-context.cjs +13 -0
  68. package/dist/gsub-context.d.cts +34 -0
  69. package/dist/gsub-context.d.ts +34 -0
  70. package/dist/gsub-context.js +2 -0
  71. package/dist/gsub-table-BDpJWqpf.d.ts +52 -0
  72. package/dist/gsub-table-D6niKeLi.js +556 -0
  73. package/dist/gsub-table-DW7Wmi9V.d.cts +52 -0
  74. package/dist/gsub-table-hFfYmf1W.cjs +741 -0
  75. package/dist/gsub-table.cjs +21 -556
  76. package/dist/gsub-table.d.cts +2 -2
  77. package/dist/gsub-table.d.ts +2 -2
  78. package/dist/gsub-table.js +2 -554
  79. package/dist/images-read.d.cts +2 -2
  80. package/dist/images-read.d.ts +2 -2
  81. package/dist/index.cjs +6 -6
  82. package/dist/index.d.cts +2 -2
  83. package/dist/index.d.ts +2 -2
  84. package/dist/index.js +6 -6
  85. package/dist/interpret-CqXdXgkY.cjs +712 -0
  86. package/dist/interpret-aWWhdIzr.js +677 -0
  87. package/dist/interpret-colors.cjs +6 -0
  88. package/dist/interpret-colors.d.cts +9 -0
  89. package/dist/interpret-colors.d.ts +9 -0
  90. package/dist/interpret-colors.js +2 -0
  91. package/dist/interpret-marked.cjs +25 -0
  92. package/dist/interpret-marked.d.cts +11 -0
  93. package/dist/interpret-marked.d.ts +11 -0
  94. package/dist/interpret-marked.js +23 -0
  95. package/dist/interpret-shapes.cjs +212 -0
  96. package/dist/interpret-shapes.d.cts +32 -0
  97. package/dist/interpret-shapes.d.ts +32 -0
  98. package/dist/interpret-shapes.js +199 -0
  99. package/dist/interpret-types.cjs +0 -0
  100. package/dist/interpret-types.d.cts +123 -0
  101. package/dist/interpret-types.d.ts +123 -0
  102. package/dist/interpret-types.js +0 -0
  103. package/dist/interpret.cjs +3 -887
  104. package/dist/interpret.d.cts +5 -116
  105. package/dist/interpret.d.ts +5 -116
  106. package/dist/interpret.js +2 -887
  107. package/dist/math-font.cjs +1 -1
  108. package/dist/math-font.js +1 -1
  109. package/dist/measure.cjs +1 -1
  110. package/dist/measure.js +1 -1
  111. package/dist/names.d.cts +2 -2
  112. package/dist/names.d.ts +2 -2
  113. package/dist/navigation.d.cts +2 -2
  114. package/dist/navigation.d.ts +2 -2
  115. package/dist/optional-content.d.cts +2 -2
  116. package/dist/optional-content.d.ts +2 -2
  117. package/dist/ot-layout-common-BcsGRFBS.d.cts +10 -0
  118. package/dist/ot-layout-common-BcsGRFBS.d.ts +10 -0
  119. package/dist/ot-layout-common.d.cts +1 -9
  120. package/dist/ot-layout-common.d.ts +1 -9
  121. package/dist/parse.d.cts +1 -1
  122. package/dist/parse.d.ts +1 -1
  123. package/dist/predictors.d.cts +1 -1
  124. package/dist/predictors.d.ts +1 -1
  125. package/dist/raster-Cdm3idP_.js +794 -0
  126. package/dist/raster-EG2KYRZ0.cjs +829 -0
  127. package/dist/raster-text.cjs +5 -0
  128. package/dist/raster-text.d.cts +20 -0
  129. package/dist/raster-text.d.ts +20 -0
  130. package/dist/raster-text.js +2 -0
  131. package/dist/raster.cjs +4 -795
  132. package/dist/raster.d.cts +17 -6
  133. package/dist/raster.d.ts +17 -6
  134. package/dist/raster.js +2 -792
  135. package/dist/read-convert.cjs +310 -0
  136. package/dist/read-convert.d.cts +58 -0
  137. package/dist/read-convert.d.ts +58 -0
  138. package/dist/read-convert.js +295 -0
  139. package/dist/read.cjs +5 -293
  140. package/dist/read.d.cts +1 -1
  141. package/dist/read.d.ts +1 -1
  142. package/dist/read.js +4 -292
  143. package/dist/structure.d.cts +2 -2
  144. package/dist/structure.d.ts +2 -2
  145. package/dist/winansi.cjs +1 -1
  146. package/dist/winansi.js +1 -1
  147. package/dist/write-annotations.cjs +132 -0
  148. package/dist/write-annotations.d.cts +29 -0
  149. package/dist/write-annotations.d.ts +29 -0
  150. package/dist/write-annotations.js +121 -0
  151. package/dist/write-font-objects.cjs +76 -0
  152. package/dist/write-font-objects.d.cts +35 -0
  153. package/dist/write-font-objects.d.ts +35 -0
  154. package/dist/write-font-objects.js +75 -0
  155. package/dist/write-form.cjs +91 -0
  156. package/dist/write-form.d.cts +19 -0
  157. package/dist/write-form.d.ts +19 -0
  158. package/dist/write-form.js +89 -0
  159. package/dist/write-images.cjs +148 -0
  160. package/dist/write-images.d.cts +25 -0
  161. package/dist/write-images.d.ts +25 -0
  162. package/dist/write-images.js +140 -0
  163. package/dist/write-outline.cjs +46 -0
  164. package/dist/write-outline.d.cts +18 -0
  165. package/dist/write-outline.d.ts +18 -0
  166. package/dist/write-outline.js +45 -0
  167. package/dist/write-page-emit.cjs +97 -0
  168. package/dist/write-page-emit.d.cts +63 -0
  169. package/dist/write-page-emit.d.ts +63 -0
  170. package/dist/write-page-emit.js +96 -0
  171. package/dist/write-pages.cjs +37 -0
  172. package/dist/write-pages.d.cts +21 -0
  173. package/dist/write-pages.d.ts +21 -0
  174. package/dist/write-pages.js +36 -0
  175. package/dist/write-strings.cjs +41 -0
  176. package/dist/write-strings.d.cts +9 -0
  177. package/dist/write-strings.d.ts +9 -0
  178. package/dist/write-strings.js +37 -0
  179. package/dist/write.cjs +53 -553
  180. package/dist/write.d.cts +1 -1
  181. package/dist/write.d.ts +1 -1
  182. package/dist/write.js +49 -549
  183. package/dist/xref.d.cts +1 -1
  184. package/dist/xref.d.ts +1 -1
  185. package/package.json +1 -1
  186. package/dist/glyf-contours-B1PsI9oA.d.ts +0 -13
  187. package/dist/glyf-contours-NHBpgks-.d.cts +0 -13
  188. package/dist/gsub-table-Bm9kSMzk.d.cts +0 -16
  189. package/dist/gsub-table-CdfESH2-.d.ts +0 -16
@@ -0,0 +1,25 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_objects = require("./objects.cjs");
3
+ const require_pdf_text = require("./pdf-text.cjs");
4
+ //#region src/interpret-marked.ts
5
+ function markedContentProperties(operand, resources, resolver) {
6
+ if (operand?.kind === "dict") return operand;
7
+ if (operand?.kind === "name") {
8
+ const properties = resolver.resolveDict(require_objects.dictGet(resources, "Properties"));
9
+ return properties === void 0 ? void 0 : resolver.resolveDict(require_objects.dictGet(properties, operand.name));
10
+ }
11
+ }
12
+ function markedContentScopeProps(props) {
13
+ if (props === void 0) return {};
14
+ const actualTextObj = require_objects.dictGet(props, "ActualText");
15
+ const altObj = require_objects.dictGet(props, "Alt");
16
+ const mcid = require_objects.asNumber(require_objects.dictGet(props, "MCID"));
17
+ return {
18
+ ...actualTextObj?.kind === "string" ? { actualText: require_pdf_text.decodePdfString(actualTextObj.bytes) } : {},
19
+ ...altObj?.kind === "string" ? { alt: require_pdf_text.decodePdfString(altObj.bytes) } : {},
20
+ ...mcid !== void 0 && mcid >= 0 ? { mcid } : {}
21
+ };
22
+ }
23
+ //#endregion
24
+ exports.markedContentProperties = markedContentProperties;
25
+ exports.markedContentScopeProps = markedContentScopeProps;
@@ -0,0 +1,11 @@
1
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.cjs";
2
+ import { PdfObjectResolver } from "./interpret-types.cjs";
3
+ //#region src/interpret-marked.d.ts
4
+ declare function markedContentProperties(operand: PdfObject | undefined, resources: PdfDict, resolver: Readonly<PdfObjectResolver>): PdfDict | undefined;
5
+ declare function markedContentScopeProps(props: PdfDict | undefined): {
6
+ actualText?: string;
7
+ alt?: string;
8
+ mcid?: number;
9
+ };
10
+ //#endregion
11
+ export { markedContentProperties, markedContentScopeProps };
@@ -0,0 +1,11 @@
1
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.js";
2
+ import { PdfObjectResolver } from "./interpret-types.js";
3
+ //#region src/interpret-marked.d.ts
4
+ declare function markedContentProperties(operand: PdfObject | undefined, resources: PdfDict, resolver: Readonly<PdfObjectResolver>): PdfDict | undefined;
5
+ declare function markedContentScopeProps(props: PdfDict | undefined): {
6
+ actualText?: string;
7
+ alt?: string;
8
+ mcid?: number;
9
+ };
10
+ //#endregion
11
+ export { markedContentProperties, markedContentScopeProps };
@@ -0,0 +1,23 @@
1
+ import { asNumber, dictGet } from "./objects.js";
2
+ import { decodePdfString } from "./pdf-text.js";
3
+ //#region src/interpret-marked.ts
4
+ function markedContentProperties(operand, resources, resolver) {
5
+ if (operand?.kind === "dict") return operand;
6
+ if (operand?.kind === "name") {
7
+ const properties = resolver.resolveDict(dictGet(resources, "Properties"));
8
+ return properties === void 0 ? void 0 : resolver.resolveDict(dictGet(properties, operand.name));
9
+ }
10
+ }
11
+ function markedContentScopeProps(props) {
12
+ if (props === void 0) return {};
13
+ const actualTextObj = dictGet(props, "ActualText");
14
+ const altObj = dictGet(props, "Alt");
15
+ const mcid = asNumber(dictGet(props, "MCID"));
16
+ return {
17
+ ...actualTextObj?.kind === "string" ? { actualText: decodePdfString(actualTextObj.bytes) } : {},
18
+ ...altObj?.kind === "string" ? { alt: decodePdfString(altObj.bytes) } : {},
19
+ ...mcid !== void 0 && mcid >= 0 ? { mcid } : {}
20
+ };
21
+ }
22
+ //#endregion
23
+ export { markedContentProperties, markedContentScopeProps };
@@ -0,0 +1,212 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_matrix = require("./matrix.cjs");
3
+ //#region src/interpret-shapes.ts
4
+ const SHAPE_ABS_TOLERANCE_PT = .001;
5
+ const SHAPE_REL_TOLERANCE = 1e-4;
6
+ function shapeTolerance(extentPt) {
7
+ return Math.max(SHAPE_ABS_TOLERANCE_PT, Math.abs(extentPt) * SHAPE_REL_TOLERANCE);
8
+ }
9
+ function nearlyEqual(a, b, tolerance) {
10
+ return Math.abs(a - b) <= tolerance;
11
+ }
12
+ function nearlyEqualPoints(a, b, tolerance) {
13
+ return nearlyEqual(a.x, b.x, tolerance) && nearlyEqual(a.y, b.y, tolerance);
14
+ }
15
+ function boundsOf(points) {
16
+ const xs = points.map((p) => p.x);
17
+ const ys = points.map((p) => p.y);
18
+ return {
19
+ minX: Math.min(...xs),
20
+ minY: Math.min(...ys),
21
+ maxX: Math.max(...xs),
22
+ maxY: Math.max(...ys)
23
+ };
24
+ }
25
+ function closedPolygonCorners(subpath) {
26
+ if (!subpath.closed) return;
27
+ const corners = [{
28
+ x: subpath.startXPt,
29
+ y: subpath.startYPt
30
+ }];
31
+ for (const segment of subpath.segments) {
32
+ if (segment.kind !== "line") return;
33
+ corners.push({
34
+ x: segment.xPt,
35
+ y: segment.yPt
36
+ });
37
+ }
38
+ const first = corners[0];
39
+ const last = corners[corners.length - 1];
40
+ if (first === void 0 || last === void 0 || corners.length < 2) return;
41
+ const bounds = boundsOf(corners);
42
+ if (nearlyEqualPoints(first, last, shapeTolerance(Math.max(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY)))) corners.pop();
43
+ return corners;
44
+ }
45
+ const RECTANGLE_CORNER_COUNT = 4;
46
+ function detectRect(subpath, paint) {
47
+ const corners = closedPolygonCorners(subpath);
48
+ if (corners?.length !== RECTANGLE_CORNER_COUNT) return;
49
+ const { minX, minY, maxX, maxY } = boundsOf(corners);
50
+ const widthPt = maxX - minX;
51
+ const heightPt = maxY - minY;
52
+ const tolX = shapeTolerance(widthPt);
53
+ const tolY = shapeTolerance(heightPt);
54
+ if (widthPt <= tolX || heightPt <= tolY) return;
55
+ for (const corner of corners) {
56
+ if (!nearlyEqual(corner.x, minX, tolX) && !nearlyEqual(corner.x, maxX, tolX)) return;
57
+ if (!nearlyEqual(corner.y, minY, tolY) && !nearlyEqual(corner.y, maxY, tolY)) return;
58
+ }
59
+ for (let i = 0; i < corners.length; i += 1) {
60
+ const from = corners[i];
61
+ const to = corners[(i + 1) % corners.length];
62
+ if (from === void 0 || to === void 0) return;
63
+ if (nearlyEqual(from.x, to.x, tolX) === nearlyEqual(from.y, to.y, tolY)) return;
64
+ }
65
+ return {
66
+ kind: "rect",
67
+ xPt: minX,
68
+ yPt: minY,
69
+ widthPt,
70
+ heightPt,
71
+ fill: paint.fill,
72
+ stroke: paint.stroke
73
+ };
74
+ }
75
+ function cardinalExtremeOf(point, center, rx, ry, tolX, tolY) {
76
+ if (nearlyEqual(point.y, center.y, tolY)) {
77
+ if (nearlyEqual(point.x, center.x + rx, tolX)) return "right";
78
+ if (nearlyEqual(point.x, center.x - rx, tolX)) return "left";
79
+ return;
80
+ }
81
+ if (nearlyEqual(point.x, center.x, tolX)) {
82
+ if (nearlyEqual(point.y, center.y + ry, tolY)) return "top";
83
+ if (nearlyEqual(point.y, center.y - ry, tolY)) return "bottom";
84
+ }
85
+ }
86
+ function expectedEllipseControl(atExtreme, atExtremeKind, otherEnd, center, kx, ky) {
87
+ if (atExtremeKind === "right" || atExtremeKind === "left") return {
88
+ x: atExtreme.x,
89
+ y: center.y + Math.sign(otherEnd.y - center.y) * ky
90
+ };
91
+ return {
92
+ x: center.x + Math.sign(otherEnd.x - center.x) * kx,
93
+ y: atExtreme.y
94
+ };
95
+ }
96
+ const ELLIPSE_SEGMENT_COUNT = 4;
97
+ function detectEllipse(subpath, paint) {
98
+ const segments = subpath.segments;
99
+ if (!subpath.closed || segments.length !== ELLIPSE_SEGMENT_COUNT || segments.some((segment) => segment.kind !== "cubic")) return;
100
+ const start = {
101
+ x: subpath.startXPt,
102
+ y: subpath.startYPt
103
+ };
104
+ const onCurve = [start];
105
+ for (const segment of segments.slice(0, 3)) onCurve.push({
106
+ x: segment.xPt,
107
+ y: segment.yPt
108
+ });
109
+ const { minX, minY, maxX, maxY } = boundsOf(onCurve);
110
+ const rx = (maxX - minX) / 2;
111
+ const ry = (maxY - minY) / 2;
112
+ const center = {
113
+ x: (minX + maxX) / 2,
114
+ y: (minY + maxY) / 2
115
+ };
116
+ const tolX = shapeTolerance(rx);
117
+ const tolY = shapeTolerance(ry);
118
+ if (rx <= tolX || ry <= tolY) return;
119
+ const finalSegment = segments[3];
120
+ if (finalSegment === void 0 || !nearlyEqualPoints({
121
+ x: finalSegment.xPt,
122
+ y: finalSegment.yPt
123
+ }, start, Math.max(tolX, tolY))) return;
124
+ const extremes = onCurve.map((point) => cardinalExtremeOf(point, center, rx, ry, tolX, tolY));
125
+ if (extremes.some((extreme) => extreme === void 0) || new Set(extremes).size !== ELLIPSE_SEGMENT_COUNT) return;
126
+ const kx = rx * require_matrix.BEZIER_KAPPA;
127
+ const ky = ry * require_matrix.BEZIER_KAPPA;
128
+ const controlTolerance = Math.max(tolX, tolY);
129
+ for (let i = 0; i < ELLIPSE_SEGMENT_COUNT; i += 1) {
130
+ const segment = segments[i];
131
+ const from = onCurve[i];
132
+ const fromKind = extremes[i];
133
+ const to = onCurve[(i + 1) % ELLIPSE_SEGMENT_COUNT];
134
+ const toKind = extremes[(i + 1) % ELLIPSE_SEGMENT_COUNT];
135
+ if (segment?.kind !== "cubic" || from === void 0 || to === void 0 || fromKind === void 0 || toKind === void 0) return;
136
+ const expectedC1 = expectedEllipseControl(from, fromKind, to, center, kx, ky);
137
+ const expectedC2 = expectedEllipseControl(to, toKind, from, center, kx, ky);
138
+ if (!nearlyEqualPoints({
139
+ x: segment.c1xPt,
140
+ y: segment.c1yPt
141
+ }, expectedC1, controlTolerance)) return;
142
+ if (!nearlyEqualPoints({
143
+ x: segment.c2xPt,
144
+ y: segment.c2yPt
145
+ }, expectedC2, controlTolerance)) return;
146
+ }
147
+ return {
148
+ kind: "ellipse",
149
+ xPt: minX,
150
+ yPt: minY,
151
+ widthPt: maxX - minX,
152
+ heightPt: maxY - minY,
153
+ fill: paint.fill,
154
+ stroke: paint.stroke
155
+ };
156
+ }
157
+ function strokeStyleFromDashArray(dashArray) {
158
+ if (dashArray.length === 0) return;
159
+ return dashArray.length === 2 && dashArray[0] === 0 ? "dotted" : "dashed";
160
+ }
161
+ function detectLine(subpath, paint, style) {
162
+ const segment = subpath.segments[0];
163
+ if (subpath.closed || subpath.segments.length !== 1 || segment?.kind !== "line") return;
164
+ if (paint.fill !== void 0 || paint.stroke === void 0) return;
165
+ return {
166
+ kind: "line",
167
+ x1Pt: subpath.startXPt,
168
+ y1Pt: subpath.startYPt,
169
+ x2Pt: segment.xPt,
170
+ y2Pt: segment.yPt,
171
+ color: paint.stroke.color,
172
+ widthPt: paint.stroke.widthPt,
173
+ ...style !== void 0 ? { style } : {}
174
+ };
175
+ }
176
+ function classifyShape(subpaths, paint, style) {
177
+ const subpath = subpaths[0];
178
+ if (subpaths.length !== 1 || subpath === void 0) return;
179
+ return detectRect(subpath, paint) ?? detectEllipse(subpath, paint) ?? detectLine(subpath, paint, style);
180
+ }
181
+ var MarkedContentStack = class {
182
+ #frames;
183
+ constructor(seed = []) {
184
+ this.#frames = [...seed];
185
+ }
186
+ open(frame) {
187
+ this.#frames.push(frame);
188
+ }
189
+ close() {
190
+ this.#frames.pop();
191
+ }
192
+ inScope(key) {
193
+ for (let i = this.#frames.length - 1; i >= 0; i--) {
194
+ const frame = this.#frames[i];
195
+ if (frame !== void 0 && key in frame) return frame[key];
196
+ }
197
+ }
198
+ };
199
+ //#endregion
200
+ exports.MarkedContentStack = MarkedContentStack;
201
+ exports.boundsOf = boundsOf;
202
+ exports.cardinalExtremeOf = cardinalExtremeOf;
203
+ exports.classifyShape = classifyShape;
204
+ exports.closedPolygonCorners = closedPolygonCorners;
205
+ exports.detectEllipse = detectEllipse;
206
+ exports.detectLine = detectLine;
207
+ exports.detectRect = detectRect;
208
+ exports.expectedEllipseControl = expectedEllipseControl;
209
+ exports.nearlyEqual = nearlyEqual;
210
+ exports.nearlyEqualPoints = nearlyEqualPoints;
211
+ exports.shapeTolerance = shapeTolerance;
212
+ exports.strokeStyleFromDashArray = strokeStyleFromDashArray;
@@ -0,0 +1,32 @@
1
+ import { ExtractedEllipse, ExtractedLine, ExtractedPaint, ExtractedRect, ExtractedSubpath } from "./interpret-types.cjs";
2
+ import { MarkedContentProps } from "./interpret.cjs";
3
+ import { ContentStrokeStyle, Point } from "document-schema.js";
4
+ //#region src/interpret-shapes.d.ts
5
+ declare function shapeTolerance(extentPt: number): number;
6
+ declare function nearlyEqual(a: number, b: number, tolerance: number): boolean;
7
+ declare function nearlyEqualPoints(a: Point, b: Point, tolerance: number): boolean;
8
+ interface Bounds {
9
+ readonly minX: number;
10
+ readonly minY: number;
11
+ readonly maxX: number;
12
+ readonly maxY: number;
13
+ }
14
+ declare function boundsOf(points: readonly Point[]): Bounds;
15
+ declare function closedPolygonCorners(subpath: ExtractedSubpath): Point[] | undefined;
16
+ declare function detectRect(subpath: ExtractedSubpath, paint: ExtractedPaint): ExtractedRect | undefined;
17
+ type CardinalExtreme = "right" | "top" | "left" | "bottom";
18
+ declare function cardinalExtremeOf(point: Point, center: Point, rx: number, ry: number, tolX: number, tolY: number): CardinalExtreme | undefined;
19
+ declare function expectedEllipseControl(atExtreme: Point, atExtremeKind: CardinalExtreme, otherEnd: Point, center: Point, kx: number, ky: number): Point;
20
+ declare function detectEllipse(subpath: ExtractedSubpath, paint: ExtractedPaint): ExtractedEllipse | undefined;
21
+ declare function strokeStyleFromDashArray(dashArray: readonly number[]): ContentStrokeStyle | undefined;
22
+ declare function detectLine(subpath: ExtractedSubpath, paint: ExtractedPaint, style: ContentStrokeStyle | undefined): ExtractedLine | undefined;
23
+ declare function classifyShape(subpaths: readonly ExtractedSubpath[], paint: ExtractedPaint, style: ContentStrokeStyle | undefined): ExtractedRect | ExtractedEllipse | ExtractedLine | undefined;
24
+ declare class MarkedContentStack {
25
+ #private;
26
+ constructor(seed?: readonly MarkedContentProps[]);
27
+ open(frame: MarkedContentProps): void;
28
+ close(): void;
29
+ inScope<K extends keyof MarkedContentProps>(key: K): MarkedContentProps[K] | undefined;
30
+ }
31
+ //#endregion
32
+ export { MarkedContentStack, boundsOf, cardinalExtremeOf, classifyShape, closedPolygonCorners, detectEllipse, detectLine, detectRect, expectedEllipseControl, nearlyEqual, nearlyEqualPoints, shapeTolerance, strokeStyleFromDashArray };
@@ -0,0 +1,32 @@
1
+ import { ExtractedEllipse, ExtractedLine, ExtractedPaint, ExtractedRect, ExtractedSubpath } from "./interpret-types.js";
2
+ import { MarkedContentProps } from "./interpret.js";
3
+ import { ContentStrokeStyle, Point } from "document-schema.js";
4
+ //#region src/interpret-shapes.d.ts
5
+ declare function shapeTolerance(extentPt: number): number;
6
+ declare function nearlyEqual(a: number, b: number, tolerance: number): boolean;
7
+ declare function nearlyEqualPoints(a: Point, b: Point, tolerance: number): boolean;
8
+ interface Bounds {
9
+ readonly minX: number;
10
+ readonly minY: number;
11
+ readonly maxX: number;
12
+ readonly maxY: number;
13
+ }
14
+ declare function boundsOf(points: readonly Point[]): Bounds;
15
+ declare function closedPolygonCorners(subpath: ExtractedSubpath): Point[] | undefined;
16
+ declare function detectRect(subpath: ExtractedSubpath, paint: ExtractedPaint): ExtractedRect | undefined;
17
+ type CardinalExtreme = "right" | "top" | "left" | "bottom";
18
+ declare function cardinalExtremeOf(point: Point, center: Point, rx: number, ry: number, tolX: number, tolY: number): CardinalExtreme | undefined;
19
+ declare function expectedEllipseControl(atExtreme: Point, atExtremeKind: CardinalExtreme, otherEnd: Point, center: Point, kx: number, ky: number): Point;
20
+ declare function detectEllipse(subpath: ExtractedSubpath, paint: ExtractedPaint): ExtractedEllipse | undefined;
21
+ declare function strokeStyleFromDashArray(dashArray: readonly number[]): ContentStrokeStyle | undefined;
22
+ declare function detectLine(subpath: ExtractedSubpath, paint: ExtractedPaint, style: ContentStrokeStyle | undefined): ExtractedLine | undefined;
23
+ declare function classifyShape(subpaths: readonly ExtractedSubpath[], paint: ExtractedPaint, style: ContentStrokeStyle | undefined): ExtractedRect | ExtractedEllipse | ExtractedLine | undefined;
24
+ declare class MarkedContentStack {
25
+ #private;
26
+ constructor(seed?: readonly MarkedContentProps[]);
27
+ open(frame: MarkedContentProps): void;
28
+ close(): void;
29
+ inScope<K extends keyof MarkedContentProps>(key: K): MarkedContentProps[K] | undefined;
30
+ }
31
+ //#endregion
32
+ export { MarkedContentStack, boundsOf, cardinalExtremeOf, classifyShape, closedPolygonCorners, detectEllipse, detectLine, detectRect, expectedEllipseControl, nearlyEqual, nearlyEqualPoints, shapeTolerance, strokeStyleFromDashArray };
@@ -0,0 +1,199 @@
1
+ import { BEZIER_KAPPA } from "./matrix.js";
2
+ //#region src/interpret-shapes.ts
3
+ const SHAPE_ABS_TOLERANCE_PT = .001;
4
+ const SHAPE_REL_TOLERANCE = 1e-4;
5
+ function shapeTolerance(extentPt) {
6
+ return Math.max(SHAPE_ABS_TOLERANCE_PT, Math.abs(extentPt) * SHAPE_REL_TOLERANCE);
7
+ }
8
+ function nearlyEqual(a, b, tolerance) {
9
+ return Math.abs(a - b) <= tolerance;
10
+ }
11
+ function nearlyEqualPoints(a, b, tolerance) {
12
+ return nearlyEqual(a.x, b.x, tolerance) && nearlyEqual(a.y, b.y, tolerance);
13
+ }
14
+ function boundsOf(points) {
15
+ const xs = points.map((p) => p.x);
16
+ const ys = points.map((p) => p.y);
17
+ return {
18
+ minX: Math.min(...xs),
19
+ minY: Math.min(...ys),
20
+ maxX: Math.max(...xs),
21
+ maxY: Math.max(...ys)
22
+ };
23
+ }
24
+ function closedPolygonCorners(subpath) {
25
+ if (!subpath.closed) return;
26
+ const corners = [{
27
+ x: subpath.startXPt,
28
+ y: subpath.startYPt
29
+ }];
30
+ for (const segment of subpath.segments) {
31
+ if (segment.kind !== "line") return;
32
+ corners.push({
33
+ x: segment.xPt,
34
+ y: segment.yPt
35
+ });
36
+ }
37
+ const first = corners[0];
38
+ const last = corners[corners.length - 1];
39
+ if (first === void 0 || last === void 0 || corners.length < 2) return;
40
+ const bounds = boundsOf(corners);
41
+ if (nearlyEqualPoints(first, last, shapeTolerance(Math.max(bounds.maxX - bounds.minX, bounds.maxY - bounds.minY)))) corners.pop();
42
+ return corners;
43
+ }
44
+ const RECTANGLE_CORNER_COUNT = 4;
45
+ function detectRect(subpath, paint) {
46
+ const corners = closedPolygonCorners(subpath);
47
+ if (corners?.length !== RECTANGLE_CORNER_COUNT) return;
48
+ const { minX, minY, maxX, maxY } = boundsOf(corners);
49
+ const widthPt = maxX - minX;
50
+ const heightPt = maxY - minY;
51
+ const tolX = shapeTolerance(widthPt);
52
+ const tolY = shapeTolerance(heightPt);
53
+ if (widthPt <= tolX || heightPt <= tolY) return;
54
+ for (const corner of corners) {
55
+ if (!nearlyEqual(corner.x, minX, tolX) && !nearlyEqual(corner.x, maxX, tolX)) return;
56
+ if (!nearlyEqual(corner.y, minY, tolY) && !nearlyEqual(corner.y, maxY, tolY)) return;
57
+ }
58
+ for (let i = 0; i < corners.length; i += 1) {
59
+ const from = corners[i];
60
+ const to = corners[(i + 1) % corners.length];
61
+ if (from === void 0 || to === void 0) return;
62
+ if (nearlyEqual(from.x, to.x, tolX) === nearlyEqual(from.y, to.y, tolY)) return;
63
+ }
64
+ return {
65
+ kind: "rect",
66
+ xPt: minX,
67
+ yPt: minY,
68
+ widthPt,
69
+ heightPt,
70
+ fill: paint.fill,
71
+ stroke: paint.stroke
72
+ };
73
+ }
74
+ function cardinalExtremeOf(point, center, rx, ry, tolX, tolY) {
75
+ if (nearlyEqual(point.y, center.y, tolY)) {
76
+ if (nearlyEqual(point.x, center.x + rx, tolX)) return "right";
77
+ if (nearlyEqual(point.x, center.x - rx, tolX)) return "left";
78
+ return;
79
+ }
80
+ if (nearlyEqual(point.x, center.x, tolX)) {
81
+ if (nearlyEqual(point.y, center.y + ry, tolY)) return "top";
82
+ if (nearlyEqual(point.y, center.y - ry, tolY)) return "bottom";
83
+ }
84
+ }
85
+ function expectedEllipseControl(atExtreme, atExtremeKind, otherEnd, center, kx, ky) {
86
+ if (atExtremeKind === "right" || atExtremeKind === "left") return {
87
+ x: atExtreme.x,
88
+ y: center.y + Math.sign(otherEnd.y - center.y) * ky
89
+ };
90
+ return {
91
+ x: center.x + Math.sign(otherEnd.x - center.x) * kx,
92
+ y: atExtreme.y
93
+ };
94
+ }
95
+ const ELLIPSE_SEGMENT_COUNT = 4;
96
+ function detectEllipse(subpath, paint) {
97
+ const segments = subpath.segments;
98
+ if (!subpath.closed || segments.length !== ELLIPSE_SEGMENT_COUNT || segments.some((segment) => segment.kind !== "cubic")) return;
99
+ const start = {
100
+ x: subpath.startXPt,
101
+ y: subpath.startYPt
102
+ };
103
+ const onCurve = [start];
104
+ for (const segment of segments.slice(0, 3)) onCurve.push({
105
+ x: segment.xPt,
106
+ y: segment.yPt
107
+ });
108
+ const { minX, minY, maxX, maxY } = boundsOf(onCurve);
109
+ const rx = (maxX - minX) / 2;
110
+ const ry = (maxY - minY) / 2;
111
+ const center = {
112
+ x: (minX + maxX) / 2,
113
+ y: (minY + maxY) / 2
114
+ };
115
+ const tolX = shapeTolerance(rx);
116
+ const tolY = shapeTolerance(ry);
117
+ if (rx <= tolX || ry <= tolY) return;
118
+ const finalSegment = segments[3];
119
+ if (finalSegment === void 0 || !nearlyEqualPoints({
120
+ x: finalSegment.xPt,
121
+ y: finalSegment.yPt
122
+ }, start, Math.max(tolX, tolY))) return;
123
+ const extremes = onCurve.map((point) => cardinalExtremeOf(point, center, rx, ry, tolX, tolY));
124
+ if (extremes.some((extreme) => extreme === void 0) || new Set(extremes).size !== ELLIPSE_SEGMENT_COUNT) return;
125
+ const kx = rx * BEZIER_KAPPA;
126
+ const ky = ry * BEZIER_KAPPA;
127
+ const controlTolerance = Math.max(tolX, tolY);
128
+ for (let i = 0; i < ELLIPSE_SEGMENT_COUNT; i += 1) {
129
+ const segment = segments[i];
130
+ const from = onCurve[i];
131
+ const fromKind = extremes[i];
132
+ const to = onCurve[(i + 1) % ELLIPSE_SEGMENT_COUNT];
133
+ const toKind = extremes[(i + 1) % ELLIPSE_SEGMENT_COUNT];
134
+ if (segment?.kind !== "cubic" || from === void 0 || to === void 0 || fromKind === void 0 || toKind === void 0) return;
135
+ const expectedC1 = expectedEllipseControl(from, fromKind, to, center, kx, ky);
136
+ const expectedC2 = expectedEllipseControl(to, toKind, from, center, kx, ky);
137
+ if (!nearlyEqualPoints({
138
+ x: segment.c1xPt,
139
+ y: segment.c1yPt
140
+ }, expectedC1, controlTolerance)) return;
141
+ if (!nearlyEqualPoints({
142
+ x: segment.c2xPt,
143
+ y: segment.c2yPt
144
+ }, expectedC2, controlTolerance)) return;
145
+ }
146
+ return {
147
+ kind: "ellipse",
148
+ xPt: minX,
149
+ yPt: minY,
150
+ widthPt: maxX - minX,
151
+ heightPt: maxY - minY,
152
+ fill: paint.fill,
153
+ stroke: paint.stroke
154
+ };
155
+ }
156
+ function strokeStyleFromDashArray(dashArray) {
157
+ if (dashArray.length === 0) return;
158
+ return dashArray.length === 2 && dashArray[0] === 0 ? "dotted" : "dashed";
159
+ }
160
+ function detectLine(subpath, paint, style) {
161
+ const segment = subpath.segments[0];
162
+ if (subpath.closed || subpath.segments.length !== 1 || segment?.kind !== "line") return;
163
+ if (paint.fill !== void 0 || paint.stroke === void 0) return;
164
+ return {
165
+ kind: "line",
166
+ x1Pt: subpath.startXPt,
167
+ y1Pt: subpath.startYPt,
168
+ x2Pt: segment.xPt,
169
+ y2Pt: segment.yPt,
170
+ color: paint.stroke.color,
171
+ widthPt: paint.stroke.widthPt,
172
+ ...style !== void 0 ? { style } : {}
173
+ };
174
+ }
175
+ function classifyShape(subpaths, paint, style) {
176
+ const subpath = subpaths[0];
177
+ if (subpaths.length !== 1 || subpath === void 0) return;
178
+ return detectRect(subpath, paint) ?? detectEllipse(subpath, paint) ?? detectLine(subpath, paint, style);
179
+ }
180
+ var MarkedContentStack = class {
181
+ #frames;
182
+ constructor(seed = []) {
183
+ this.#frames = [...seed];
184
+ }
185
+ open(frame) {
186
+ this.#frames.push(frame);
187
+ }
188
+ close() {
189
+ this.#frames.pop();
190
+ }
191
+ inScope(key) {
192
+ for (let i = this.#frames.length - 1; i >= 0; i--) {
193
+ const frame = this.#frames[i];
194
+ if (frame !== void 0 && key in frame) return frame[key];
195
+ }
196
+ }
197
+ };
198
+ //#endregion
199
+ export { MarkedContentStack, boundsOf, cardinalExtremeOf, classifyShape, closedPolygonCorners, detectEllipse, detectLine, detectRect, expectedEllipseControl, nearlyEqual, nearlyEqualPoints, shapeTolerance, strokeStyleFromDashArray };
File without changes
@@ -0,0 +1,123 @@
1
+ import { i as PdfDiagnosticSink } from "./diagnostics-C9FxfZ6H.cjs";
2
+ import { n as PdfDict, r as PdfObject } from "./objects-e2RS3-yT.cjs";
3
+ import { r as Matrix } from "./matrix-B7_SBKQ5.cjs";
4
+ import { Color, ContentStrokeStyle } from "document-schema.js";
5
+ //#region src/interpret-types.d.ts
6
+ interface ExtractedTextRun {
7
+ readonly kind: "text";
8
+ readonly codes: Uint8Array<ArrayBuffer>;
9
+ readonly fontResourceName: string;
10
+ readonly resources: PdfDict;
11
+ readonly startMatrix: Matrix;
12
+ readonly endMatrix: Matrix;
13
+ readonly sizePt: number;
14
+ readonly color: Color;
15
+ readonly layerName?: string;
16
+ readonly actualText?: string;
17
+ readonly alt?: string;
18
+ readonly mcid?: number;
19
+ readonly vertical?: boolean;
20
+ }
21
+ interface ExtractedPaint {
22
+ readonly fill: Color | undefined;
23
+ readonly stroke: {
24
+ readonly color: Color;
25
+ readonly widthPt: number;
26
+ } | undefined;
27
+ readonly layerName?: string;
28
+ readonly mcid?: number;
29
+ }
30
+ interface ExtractedRect extends ExtractedPaint {
31
+ readonly kind: "rect";
32
+ readonly xPt: number;
33
+ readonly yPt: number;
34
+ readonly widthPt: number;
35
+ readonly heightPt: number;
36
+ }
37
+ interface ExtractedEllipse extends ExtractedPaint {
38
+ readonly kind: "ellipse";
39
+ readonly xPt: number;
40
+ readonly yPt: number;
41
+ readonly widthPt: number;
42
+ readonly heightPt: number;
43
+ }
44
+ interface ExtractedLine {
45
+ readonly kind: "line";
46
+ readonly x1Pt: number;
47
+ readonly y1Pt: number;
48
+ readonly x2Pt: number;
49
+ readonly y2Pt: number;
50
+ readonly color: Color;
51
+ readonly widthPt: number;
52
+ readonly style?: ContentStrokeStyle;
53
+ readonly layerName?: string;
54
+ readonly mcid?: number;
55
+ }
56
+ type ExtractedPathSegment = {
57
+ readonly kind: "line";
58
+ readonly xPt: number;
59
+ readonly yPt: number;
60
+ } | {
61
+ readonly kind: "cubic";
62
+ readonly c1xPt: number;
63
+ readonly c1yPt: number;
64
+ readonly c2xPt: number;
65
+ readonly c2yPt: number;
66
+ readonly xPt: number;
67
+ readonly yPt: number;
68
+ };
69
+ interface ExtractedSubpath {
70
+ readonly startXPt: number;
71
+ readonly startYPt: number;
72
+ readonly segments: readonly ExtractedPathSegment[];
73
+ readonly closed: boolean;
74
+ }
75
+ interface ExtractedPath extends ExtractedPaint {
76
+ readonly kind: "path";
77
+ readonly subpaths: readonly ExtractedSubpath[];
78
+ readonly fillRule: "nonzero" | "evenodd";
79
+ readonly style?: ContentStrokeStyle;
80
+ }
81
+ interface ExtractedImage {
82
+ readonly kind: "image";
83
+ readonly resourceName: string;
84
+ readonly resources: PdfDict;
85
+ readonly matrix: Matrix;
86
+ readonly layerName?: string;
87
+ readonly mcid?: number;
88
+ }
89
+ interface ExtractedInlineImage {
90
+ readonly kind: "inlineImage";
91
+ readonly dict: PdfDict;
92
+ readonly data: Uint8Array<ArrayBuffer>;
93
+ readonly matrix: Matrix;
94
+ readonly layerName?: string;
95
+ readonly mcid?: number;
96
+ }
97
+ type ExtractedItem = ExtractedTextRun | ExtractedRect | ExtractedEllipse | ExtractedLine | ExtractedPath | ExtractedImage | ExtractedInlineImage;
98
+ interface VerticalGlyphAdvance {
99
+ readonly displacementPer1000: number;
100
+ readonly positionXPer1000: number;
101
+ readonly positionYPer1000: number;
102
+ }
103
+ interface GlyphAdvance {
104
+ readonly widthPer1000: number;
105
+ readonly byteLengthConsumed: number;
106
+ readonly vertical?: VerticalGlyphAdvance;
107
+ }
108
+ interface FontMetricsPort {
109
+ glyphAdvance: (fontResourceName: string, resources: PdfDict, codes: Uint8Array<ArrayBuffer>, byteOffset: number) => GlyphAdvance | undefined;
110
+ isVertical: (fontResourceName: string, resources: PdfDict) => boolean;
111
+ }
112
+ interface PdfObjectResolver {
113
+ resolve: (obj: PdfObject | undefined) => PdfObject | undefined;
114
+ resolveDict: (obj: PdfObject | undefined) => PdfDict | undefined;
115
+ }
116
+ interface InterpretContext {
117
+ readonly fontMetrics: FontMetricsPort;
118
+ readonly resolver: PdfObjectResolver;
119
+ readonly sink: PdfDiagnosticSink;
120
+ readonly layerNameOf?: (obj: PdfObject | undefined) => string | undefined;
121
+ }
122
+ //#endregion
123
+ export { ExtractedEllipse, ExtractedImage, ExtractedInlineImage, ExtractedItem, ExtractedLine, ExtractedPaint, ExtractedPath, ExtractedPathSegment, ExtractedRect, ExtractedSubpath, ExtractedTextRun, FontMetricsPort, GlyphAdvance, InterpretContext, PdfObjectResolver, VerticalGlyphAdvance };