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,15 @@
1
+ import { r as StandardFontName } from "./afm-widths-DyDq56Ph.js";
2
+ //#region src/fonts.d.ts
3
+ type StandardFamily = 'helvetica' | 'times' | 'courier';
4
+ interface FontFamilyResolution {
5
+ readonly family: StandardFamily;
6
+ readonly matched: boolean;
7
+ }
8
+ declare function resolveFontFamily(rawFamily: string): FontFamilyResolution;
9
+ interface ResolvedFont {
10
+ readonly standardName: StandardFontName;
11
+ readonly matched: boolean;
12
+ }
13
+ declare function resolveStandardFont(family: string, bold: boolean, italic: boolean): ResolvedFont;
14
+ //#endregion
15
+ export { FontFamilyResolution, ResolvedFont, resolveFontFamily, resolveStandardFont };
package/dist/fonts.js ADDED
@@ -0,0 +1,122 @@
1
+ //#region src/fonts.ts
2
+ const FAMILY_BY_NORMALIZED_NAME = /* @__PURE__ */ new Map([
3
+ ["helvetica", "helvetica"],
4
+ ["helveticaneue", "helvetica"],
5
+ ["arial", "helvetica"],
6
+ ["arialnarrow", "helvetica"],
7
+ ["arialblack", "helvetica"],
8
+ ["calibri", "helvetica"],
9
+ ["aptos", "helvetica"],
10
+ ["aptosdisplay", "helvetica"],
11
+ ["segoeui", "helvetica"],
12
+ ["tahoma", "helvetica"],
13
+ ["verdana", "helvetica"],
14
+ ["trebuchetms", "helvetica"],
15
+ ["centurygothic", "helvetica"],
16
+ ["franklingothicbook", "helvetica"],
17
+ ["gillsans", "helvetica"],
18
+ ["futura", "helvetica"],
19
+ ["roboto", "helvetica"],
20
+ ["opensans", "helvetica"],
21
+ ["lato", "helvetica"],
22
+ ["montserrat", "helvetica"],
23
+ ["poppins", "helvetica"],
24
+ ["inter", "helvetica"],
25
+ ["notosans", "helvetica"],
26
+ ["dejavusans", "helvetica"],
27
+ ["sourcesanspro", "helvetica"],
28
+ ["sansserif", "helvetica"],
29
+ ["liberationsans", "helvetica"],
30
+ ["nimbussans", "helvetica"],
31
+ ["times", "times"],
32
+ ["timesnewroman", "times"],
33
+ ["liberationserif", "times"],
34
+ ["nimbusroman", "times"],
35
+ ["georgia", "times"],
36
+ ["cambria", "times"],
37
+ ["constantia", "times"],
38
+ ["garamond", "times"],
39
+ ["bookantiqua", "times"],
40
+ ["palatino", "times"],
41
+ ["palatinolinotype", "times"],
42
+ ["bookmanoldstyle", "times"],
43
+ ["century", "times"],
44
+ ["centuryschoolbook", "times"],
45
+ ["minionpro", "times"],
46
+ ["merriweather", "times"],
47
+ ["notoserif", "times"],
48
+ ["dejavuserif", "times"],
49
+ ["serif", "times"],
50
+ ["courier", "courier"],
51
+ ["couriernew", "courier"],
52
+ ["liberationmono", "courier"],
53
+ ["nimbusmono", "courier"],
54
+ ["consolas", "courier"],
55
+ ["monaco", "courier"],
56
+ ["menlo", "courier"],
57
+ ["lucidaconsole", "courier"],
58
+ ["andalemono", "courier"],
59
+ ["cascadiacode", "courier"],
60
+ ["cascadiamono", "courier"],
61
+ ["sfmono", "courier"],
62
+ ["jetbrainsmono", "courier"],
63
+ ["firacode", "courier"],
64
+ ["sourcecodepro", "courier"],
65
+ ["notosansmono", "courier"],
66
+ ["dejavusansmono", "courier"],
67
+ ["monospace", "courier"]
68
+ ]);
69
+ const DEFAULT_FAMILY = "helvetica";
70
+ function normalizeFamilyName(raw) {
71
+ return raw.toLowerCase().replace(/[^a-z0-9]/g, "");
72
+ }
73
+ function resolveFontFamily(rawFamily) {
74
+ const normalized = normalizeFamilyName(rawFamily);
75
+ const exact = FAMILY_BY_NORMALIZED_NAME.get(normalized);
76
+ if (exact !== void 0) return {
77
+ family: exact,
78
+ matched: true
79
+ };
80
+ if (normalized.includes("mono")) return {
81
+ family: "courier",
82
+ matched: true
83
+ };
84
+ if (normalized.includes("serif") && !normalized.includes("sansserif")) return {
85
+ family: "times",
86
+ matched: true
87
+ };
88
+ return {
89
+ family: DEFAULT_FAMILY,
90
+ matched: false
91
+ };
92
+ }
93
+ const VARIANTS = {
94
+ helvetica: {
95
+ regular: "Helvetica",
96
+ bold: "Helvetica-Bold",
97
+ italic: "Helvetica-Oblique",
98
+ boldItalic: "Helvetica-BoldOblique"
99
+ },
100
+ times: {
101
+ regular: "Times-Roman",
102
+ bold: "Times-Bold",
103
+ italic: "Times-Italic",
104
+ boldItalic: "Times-BoldItalic"
105
+ },
106
+ courier: {
107
+ regular: "Courier",
108
+ bold: "Courier-Bold",
109
+ italic: "Courier-Oblique",
110
+ boldItalic: "Courier-BoldOblique"
111
+ }
112
+ };
113
+ function resolveStandardFont(family, bold, italic) {
114
+ const { family: standardFamily, matched } = resolveFontFamily(family);
115
+ const variants = VARIANTS[standardFamily];
116
+ return {
117
+ standardName: bold && italic ? variants.boldItalic : bold ? variants.bold : italic ? variants.italic : variants.regular,
118
+ matched
119
+ };
120
+ }
121
+ //#endregion
122
+ export { resolveFontFamily, resolveStandardFont };
File without changes
@@ -0,0 +1,10 @@
1
+ import { t as MathBox } from "./math-types-Ba8qAf1p.cjs";
2
+ //#region src/formula.d.ts
3
+ interface PositionedFormula {
4
+ readonly pageIndex: number;
5
+ readonly xPt: number;
6
+ readonly yPt: number;
7
+ readonly box: MathBox;
8
+ }
9
+ //#endregion
10
+ export { PositionedFormula };
@@ -0,0 +1,10 @@
1
+ import { t as MathBox } from "./math-types-Ba8qAf1p.js";
2
+ //#region src/formula.d.ts
3
+ interface PositionedFormula {
4
+ readonly pageIndex: number;
5
+ readonly xPt: number;
6
+ readonly yPt: number;
7
+ readonly box: MathBox;
8
+ }
9
+ //#endregion
10
+ export { PositionedFormula };
File without changes
@@ -0,0 +1,71 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/image/jpeg-info.ts
3
+ const SOI = 216;
4
+ const EOI = 217;
5
+ const APP14 = 238;
6
+ const SOF_MARKERS = /* @__PURE__ */ new Set([
7
+ 192,
8
+ 193,
9
+ 194,
10
+ 201,
11
+ 202
12
+ ]);
13
+ const PROGRESSIVE_SOF_MARKERS = /* @__PURE__ */ new Set([194, 202]);
14
+ const NO_PAYLOAD_MARKERS = /* @__PURE__ */ new Set([
15
+ 1,
16
+ 216,
17
+ 217,
18
+ 208,
19
+ 209,
20
+ 210,
21
+ 211,
22
+ 212,
23
+ 213,
24
+ 214,
25
+ 215
26
+ ]);
27
+ function requireByte(bytes, index) {
28
+ const value = bytes[index];
29
+ if (value === void 0) throw new Error("unexpected end of JPEG data");
30
+ return value;
31
+ }
32
+ function readUint16BE(bytes, offset) {
33
+ return requireByte(bytes, offset) << 8 | requireByte(bytes, offset + 1);
34
+ }
35
+ function readJpegInfo(bytes) {
36
+ if (requireByte(bytes, 0) !== 255 || requireByte(bytes, 1) !== SOI) throw new Error("not a valid JPEG file: missing SOI marker");
37
+ let offset = 2;
38
+ let adobeTransform;
39
+ while (offset < bytes.length) {
40
+ if (bytes[offset] !== 255) {
41
+ offset++;
42
+ continue;
43
+ }
44
+ let markerOffset = offset + 1;
45
+ while (bytes[markerOffset] === 255) markerOffset++;
46
+ const marker = bytes[markerOffset];
47
+ if (marker === void 0) break;
48
+ offset = markerOffset + 1;
49
+ if (marker === EOI) break;
50
+ if (NO_PAYLOAD_MARKERS.has(marker)) continue;
51
+ const segmentLength = readUint16BE(bytes, offset);
52
+ if (marker === APP14 && segmentLength >= 14) adobeTransform = requireByte(bytes, offset + 2 + 11);
53
+ if (SOF_MARKERS.has(marker)) {
54
+ const p = offset + 2;
55
+ const precision = requireByte(bytes, p);
56
+ const height = readUint16BE(bytes, p + 1);
57
+ return {
58
+ width: readUint16BE(bytes, p + 3),
59
+ height,
60
+ components: requireByte(bytes, p + 5),
61
+ precision,
62
+ progressive: PROGRESSIVE_SOF_MARKERS.has(marker),
63
+ adobeTransform
64
+ };
65
+ }
66
+ offset += segmentLength;
67
+ }
68
+ throw new Error("no SOF marker found in JPEG file");
69
+ }
70
+ //#endregion
71
+ exports.readJpegInfo = readJpegInfo;
@@ -0,0 +1,12 @@
1
+ //#region src/image/jpeg-info.d.ts
2
+ interface JpegInfo {
3
+ readonly width: number;
4
+ readonly height: number;
5
+ readonly components: number;
6
+ readonly precision: number;
7
+ readonly progressive: boolean;
8
+ readonly adobeTransform: number | undefined;
9
+ }
10
+ declare function readJpegInfo(bytes: Uint8Array<ArrayBuffer>): JpegInfo;
11
+ //#endregion
12
+ export { JpegInfo, readJpegInfo };
@@ -0,0 +1,12 @@
1
+ //#region src/image/jpeg-info.d.ts
2
+ interface JpegInfo {
3
+ readonly width: number;
4
+ readonly height: number;
5
+ readonly components: number;
6
+ readonly precision: number;
7
+ readonly progressive: boolean;
8
+ readonly adobeTransform: number | undefined;
9
+ }
10
+ declare function readJpegInfo(bytes: Uint8Array<ArrayBuffer>): JpegInfo;
11
+ //#endregion
12
+ export { JpegInfo, readJpegInfo };
@@ -0,0 +1,70 @@
1
+ //#region src/image/jpeg-info.ts
2
+ const SOI = 216;
3
+ const EOI = 217;
4
+ const APP14 = 238;
5
+ const SOF_MARKERS = /* @__PURE__ */ new Set([
6
+ 192,
7
+ 193,
8
+ 194,
9
+ 201,
10
+ 202
11
+ ]);
12
+ const PROGRESSIVE_SOF_MARKERS = /* @__PURE__ */ new Set([194, 202]);
13
+ const NO_PAYLOAD_MARKERS = /* @__PURE__ */ new Set([
14
+ 1,
15
+ 216,
16
+ 217,
17
+ 208,
18
+ 209,
19
+ 210,
20
+ 211,
21
+ 212,
22
+ 213,
23
+ 214,
24
+ 215
25
+ ]);
26
+ function requireByte(bytes, index) {
27
+ const value = bytes[index];
28
+ if (value === void 0) throw new Error("unexpected end of JPEG data");
29
+ return value;
30
+ }
31
+ function readUint16BE(bytes, offset) {
32
+ return requireByte(bytes, offset) << 8 | requireByte(bytes, offset + 1);
33
+ }
34
+ function readJpegInfo(bytes) {
35
+ if (requireByte(bytes, 0) !== 255 || requireByte(bytes, 1) !== SOI) throw new Error("not a valid JPEG file: missing SOI marker");
36
+ let offset = 2;
37
+ let adobeTransform;
38
+ while (offset < bytes.length) {
39
+ if (bytes[offset] !== 255) {
40
+ offset++;
41
+ continue;
42
+ }
43
+ let markerOffset = offset + 1;
44
+ while (bytes[markerOffset] === 255) markerOffset++;
45
+ const marker = bytes[markerOffset];
46
+ if (marker === void 0) break;
47
+ offset = markerOffset + 1;
48
+ if (marker === EOI) break;
49
+ if (NO_PAYLOAD_MARKERS.has(marker)) continue;
50
+ const segmentLength = readUint16BE(bytes, offset);
51
+ if (marker === APP14 && segmentLength >= 14) adobeTransform = requireByte(bytes, offset + 2 + 11);
52
+ if (SOF_MARKERS.has(marker)) {
53
+ const p = offset + 2;
54
+ const precision = requireByte(bytes, p);
55
+ const height = readUint16BE(bytes, p + 1);
56
+ return {
57
+ width: readUint16BE(bytes, p + 3),
58
+ height,
59
+ components: requireByte(bytes, p + 5),
60
+ precision,
61
+ progressive: PROGRESSIVE_SOF_MARKERS.has(marker),
62
+ adobeTransform
63
+ };
64
+ }
65
+ offset += segmentLength;
66
+ }
67
+ throw new Error("no SOF marker found in JPEG file");
68
+ }
69
+ //#endregion
70
+ export { readJpegInfo };
@@ -0,0 +1,181 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_bytes_crc32 = require("../bytes/crc32.cjs");
3
+ const require_bytes_writer = require("../bytes/writer.cjs");
4
+ const require_bytes_flate = require("../bytes/flate.cjs");
5
+ const require_image_png_filter = require("./png-filter.cjs");
6
+ //#region src/image/png-decode.ts
7
+ const PNG_SIGNATURE = [
8
+ 137,
9
+ 80,
10
+ 78,
11
+ 71,
12
+ 13,
13
+ 10,
14
+ 26,
15
+ 10
16
+ ];
17
+ function requireDataView(bytes) {
18
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
19
+ }
20
+ function readChunks(bytes, onWarning) {
21
+ const chunks = [];
22
+ const view = requireDataView(bytes);
23
+ let offset = PNG_SIGNATURE.length;
24
+ while (offset + 8 <= bytes.length) {
25
+ const length = view.getUint32(offset);
26
+ const typeBytes = bytes.subarray(offset + 4, offset + 8);
27
+ const type = new TextDecoder("latin1").decode(typeBytes);
28
+ const dataStart = offset + 8;
29
+ const dataEnd = dataStart + length;
30
+ if (dataEnd + 4 > bytes.length) throw new Error(`PNG chunk '${type}' declares a length that runs past the end of the file`);
31
+ const data = bytes.subarray(dataStart, dataEnd);
32
+ if (onWarning !== void 0) {
33
+ if (view.getUint32(dataEnd) !== require_bytes_crc32.crc32(require_bytes_writer.concatBytes([typeBytes, data]))) onWarning(`PNG chunk '${type}' failed its CRC32 check`);
34
+ }
35
+ chunks.push({
36
+ type,
37
+ data
38
+ });
39
+ offset = dataEnd + 4;
40
+ if (type === "IEND") break;
41
+ }
42
+ return chunks;
43
+ }
44
+ function parseIhdr(data) {
45
+ const view = requireDataView(data);
46
+ return {
47
+ width: view.getUint32(0),
48
+ height: view.getUint32(4),
49
+ bitDepth: data[8],
50
+ colorType: data[9],
51
+ interlace: data[12]
52
+ };
53
+ }
54
+ function channelsForColorType(colorType) {
55
+ if (colorType === 0) return 1;
56
+ if (colorType === 2) return 3;
57
+ if (colorType === 3) return 1;
58
+ if (colorType === 4) return 2;
59
+ if (colorType === 6) return 4;
60
+ throw new Error(`unsupported PNG colour type: ${colorType}`);
61
+ }
62
+ function filterBpp(bitDepth, channels) {
63
+ return Math.max(1, Math.ceil(bitDepth * channels / 8));
64
+ }
65
+ function unpackRow(rowBytes, width, channels, bitDepth) {
66
+ const sampleCount = width * channels;
67
+ const samples = new Array(sampleCount);
68
+ if (bitDepth === 8) for (let i = 0; i < sampleCount; i++) samples[i] = rowBytes[i];
69
+ else if (bitDepth === 16) for (let i = 0; i < sampleCount; i++) samples[i] = rowBytes[i * 2];
70
+ else {
71
+ const mask = (1 << bitDepth) - 1;
72
+ for (let i = 0; i < sampleCount; i++) {
73
+ const bitOffset = i * bitDepth;
74
+ const byteIndex = bitOffset >> 3;
75
+ const shift = 8 - bitDepth - (bitOffset & 7);
76
+ samples[i] = rowBytes[byteIndex] >> shift & mask;
77
+ }
78
+ }
79
+ return samples;
80
+ }
81
+ function readTrnsGrayValue(trns) {
82
+ return requireDataView(trns).getUint16(0);
83
+ }
84
+ function readTrnsRgbKey(trns) {
85
+ const view = requireDataView(trns);
86
+ return [
87
+ view.getUint16(0),
88
+ view.getUint16(2),
89
+ view.getUint16(4)
90
+ ];
91
+ }
92
+ function scaleToByte(sample, bitDepth) {
93
+ if (bitDepth === 16) return sample;
94
+ const maxSample = (1 << bitDepth) - 1;
95
+ return Math.round(sample * 255 / maxSample);
96
+ }
97
+ function decodePng(bytes, options = {}) {
98
+ for (let i = 0; i < PNG_SIGNATURE.length; i++) if (bytes[i] !== PNG_SIGNATURE[i]) throw new Error("not a valid PNG file: bad signature");
99
+ const chunks = readChunks(bytes, options.onWarning);
100
+ const ihdrChunk = chunks[0];
101
+ if (ihdrChunk?.type !== "IHDR") throw new Error("PNG file does not begin with an IHDR chunk");
102
+ const ihdr = parseIhdr(ihdrChunk.data);
103
+ if (ihdr.interlace !== 0) throw new Error("Adam7-interlaced PNG images are not supported");
104
+ const channels = channelsForColorType(ihdr.colorType);
105
+ const bpp = filterBpp(ihdr.bitDepth, channels);
106
+ const bytesPerRow = Math.ceil(ihdr.width * channels * ihdr.bitDepth / 8);
107
+ const idatChunks = chunks.filter((c) => c.type === "IDAT").map((c) => c.data);
108
+ if (idatChunks.length === 0) throw new Error("PNG file has no IDAT chunks");
109
+ const compressed = require_bytes_writer.concatBytes(idatChunks);
110
+ const { bytes: inflated, recovered } = require_bytes_flate.inflateTolerant(compressed);
111
+ if (recovered && options.onWarning !== void 0) options.onWarning("PNG IDAT stream required tolerant recovery (truncated or malformed)");
112
+ const unfiltered = require_image_png_filter.unfilterScanlines(inflated, ihdr.height, bytesPerRow, bpp);
113
+ const palette = ihdr.colorType === 3 ? chunks.find((c) => c.type === "PLTE")?.data : void 0;
114
+ if (ihdr.colorType === 3 && palette === void 0) throw new Error("indexed-colour PNG has no PLTE chunk");
115
+ const trns = chunks.find((c) => c.type === "tRNS")?.data;
116
+ return buildRawImage(ihdr, channels, bytesPerRow, unfiltered, palette, trns);
117
+ }
118
+ function buildRawImage(ihdr, channels, bytesPerRow, unfiltered, palette, trns) {
119
+ const { width, height, bitDepth, colorType } = ihdr;
120
+ const outChannels = colorType === 0 || colorType === 4 ? 1 : 3;
121
+ const data = new Uint8Array(width * height * outChannels);
122
+ const alpha = colorType === 4 || colorType === 6 || trns !== void 0 ? new Uint8Array(width * height).fill(255) : void 0;
123
+ const trnsGray = colorType === 0 && trns !== void 0 ? readTrnsGrayValue(trns) : void 0;
124
+ const trnsRgb = colorType === 2 && trns !== void 0 ? readTrnsRgbKey(trns) : void 0;
125
+ for (let y = 0; y < height; y++) {
126
+ const rowStart = y * bytesPerRow;
127
+ const samples = unpackRow(unfiltered.subarray(rowStart, rowStart + bytesPerRow), width, channels, bitDepth);
128
+ for (let x = 0; x < width; x++) {
129
+ const pixelBase = x * channels;
130
+ const outBase = (y * width + x) * outChannels;
131
+ const alphaIndex = y * width + x;
132
+ if (colorType === 0) {
133
+ const g = samples[pixelBase];
134
+ data[outBase] = scaleToByte(g, bitDepth);
135
+ if (alpha !== void 0 && trnsGray !== void 0) alpha[alphaIndex] = g === trnsGray ? 0 : 255;
136
+ } else if (colorType === 2) {
137
+ const r = samples[pixelBase];
138
+ const g = samples[pixelBase + 1];
139
+ const b = samples[pixelBase + 2];
140
+ data[outBase] = r;
141
+ data[outBase + 1] = g;
142
+ data[outBase + 2] = b;
143
+ if (alpha !== void 0 && trnsRgb !== void 0) {
144
+ const [kr, kg, kb] = trnsRgb;
145
+ alpha[alphaIndex] = r === kr && g === kg && b === kb ? 0 : 255;
146
+ }
147
+ } else if (colorType === 3) {
148
+ const index = samples[pixelBase];
149
+ if (palette === void 0) throw new Error("indexed-colour PNG has no PLTE chunk");
150
+ data[outBase] = palette[index * 3];
151
+ data[outBase + 1] = palette[index * 3 + 1];
152
+ data[outBase + 2] = palette[index * 3 + 2];
153
+ if (alpha !== void 0 && trns !== void 0) alpha[alphaIndex] = index < trns.length ? trns[index] : 255;
154
+ } else if (colorType === 4) {
155
+ const g = samples[pixelBase];
156
+ const a = samples[pixelBase + 1];
157
+ data[outBase] = scaleToByte(g, bitDepth);
158
+ if (alpha !== void 0) alpha[alphaIndex] = scaleToByte(a, bitDepth);
159
+ } else {
160
+ data[outBase] = samples[pixelBase];
161
+ data[outBase + 1] = samples[pixelBase + 1];
162
+ data[outBase + 2] = samples[pixelBase + 2];
163
+ if (alpha !== void 0) alpha[alphaIndex] = samples[pixelBase + 3];
164
+ }
165
+ }
166
+ }
167
+ return alpha === void 0 ? {
168
+ width,
169
+ height,
170
+ channels: outChannels,
171
+ data
172
+ } : {
173
+ width,
174
+ height,
175
+ channels: outChannels,
176
+ data,
177
+ alpha
178
+ };
179
+ }
180
+ //#endregion
181
+ exports.decodePng = decodePng;
@@ -0,0 +1,14 @@
1
+ //#region src/image/png-decode.d.ts
2
+ interface RawImage {
3
+ readonly width: number;
4
+ readonly height: number;
5
+ readonly channels: 1 | 3;
6
+ readonly data: Uint8Array<ArrayBuffer>;
7
+ readonly alpha?: Uint8Array<ArrayBuffer>;
8
+ }
9
+ interface PngDecodeOptions {
10
+ readonly onWarning?: (message: string) => void;
11
+ }
12
+ declare function decodePng(bytes: Uint8Array<ArrayBuffer>, options?: PngDecodeOptions): RawImage;
13
+ //#endregion
14
+ export { PngDecodeOptions, RawImage, decodePng };
@@ -0,0 +1,14 @@
1
+ //#region src/image/png-decode.d.ts
2
+ interface RawImage {
3
+ readonly width: number;
4
+ readonly height: number;
5
+ readonly channels: 1 | 3;
6
+ readonly data: Uint8Array<ArrayBuffer>;
7
+ readonly alpha?: Uint8Array<ArrayBuffer>;
8
+ }
9
+ interface PngDecodeOptions {
10
+ readonly onWarning?: (message: string) => void;
11
+ }
12
+ declare function decodePng(bytes: Uint8Array<ArrayBuffer>, options?: PngDecodeOptions): RawImage;
13
+ //#endregion
14
+ export { PngDecodeOptions, RawImage, decodePng };