pdf-codec 1.3.1 → 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 +9 -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,21 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/bytes/crc32.ts
3
+ const CRC32_POLYNOMIAL = 3988292384;
4
+ const BYTE_VALUES = 256;
5
+ const BITS_PER_BYTE = 8;
6
+ const CRC32_TABLE = (() => {
7
+ const table = new Uint32Array(BYTE_VALUES);
8
+ for (let n = 0; n < BYTE_VALUES; n++) {
9
+ let c = n;
10
+ for (let k = 0; k < BITS_PER_BYTE; k++) c = (c & 1) === 1 ? CRC32_POLYNOMIAL ^ c >>> 1 : c >>> 1;
11
+ table[n] = c >>> 0;
12
+ }
13
+ return table;
14
+ })();
15
+ function crc32(bytes) {
16
+ let crc = 4294967295;
17
+ for (const byte of bytes) crc = CRC32_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
18
+ return (crc ^ 4294967295) >>> 0;
19
+ }
20
+ //#endregion
21
+ exports.crc32 = crc32;
@@ -0,0 +1,4 @@
1
+ //#region src/bytes/crc32.d.ts
2
+ declare function crc32(bytes: Uint8Array<ArrayBuffer>): number;
3
+ //#endregion
4
+ export { crc32 };
@@ -0,0 +1,4 @@
1
+ //#region src/bytes/crc32.d.ts
2
+ declare function crc32(bytes: Uint8Array<ArrayBuffer>): number;
3
+ //#endregion
4
+ export { crc32 };
@@ -0,0 +1,20 @@
1
+ //#region src/bytes/crc32.ts
2
+ const CRC32_POLYNOMIAL = 3988292384;
3
+ const BYTE_VALUES = 256;
4
+ const BITS_PER_BYTE = 8;
5
+ const CRC32_TABLE = (() => {
6
+ const table = new Uint32Array(BYTE_VALUES);
7
+ for (let n = 0; n < BYTE_VALUES; n++) {
8
+ let c = n;
9
+ for (let k = 0; k < BITS_PER_BYTE; k++) c = (c & 1) === 1 ? CRC32_POLYNOMIAL ^ c >>> 1 : c >>> 1;
10
+ table[n] = c >>> 0;
11
+ }
12
+ return table;
13
+ })();
14
+ function crc32(bytes) {
15
+ let crc = 4294967295;
16
+ for (const byte of bytes) crc = CRC32_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
17
+ return (crc ^ 4294967295) >>> 0;
18
+ }
19
+ //#endregion
20
+ export { crc32 };
@@ -0,0 +1,53 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_bytes_reader = require("./reader.cjs");
3
+ const require_bytes_writer = require("./writer.cjs");
4
+ let fflate = require("fflate");
5
+ //#region src/bytes/flate.ts
6
+ const MAX_INFLATE_OUTPUT_BYTES = 536870912;
7
+ function deflate(data, level) {
8
+ return (0, fflate.zlibSync)(data, level === void 0 ? void 0 : { level });
9
+ }
10
+ function inflate(data) {
11
+ const out = (0, fflate.unzlibSync)(data);
12
+ if (out.length > 536870912) throw new Error(`inflated output exceeds the ${MAX_INFLATE_OUTPUT_BYTES}-byte limit`);
13
+ return out;
14
+ }
15
+ function inflateTolerant(data) {
16
+ try {
17
+ return {
18
+ bytes: inflate(data),
19
+ recovered: false
20
+ };
21
+ } catch {}
22
+ let offset = 0;
23
+ while (offset < data.length && require_bytes_reader.isAsciiWhitespace(data[offset])) offset++;
24
+ if (offset > 0) try {
25
+ return {
26
+ bytes: inflate(data.subarray(offset)),
27
+ recovered: true
28
+ };
29
+ } catch {}
30
+ try {
31
+ return {
32
+ bytes: (0, fflate.inflateSync)(data),
33
+ recovered: true
34
+ };
35
+ } catch {}
36
+ const chunks = [];
37
+ const unzlib = new fflate.Unzlib((chunk) => {
38
+ chunks.push(chunk);
39
+ });
40
+ try {
41
+ unzlib.push(data, false);
42
+ } catch {}
43
+ if (chunks.length === 0) throw new Error("unable to inflate stream: no data could be recovered");
44
+ return {
45
+ bytes: require_bytes_writer.concatBytes(chunks),
46
+ recovered: true
47
+ };
48
+ }
49
+ //#endregion
50
+ exports.MAX_INFLATE_OUTPUT_BYTES = MAX_INFLATE_OUTPUT_BYTES;
51
+ exports.deflate = deflate;
52
+ exports.inflate = inflate;
53
+ exports.inflateTolerant = inflateTolerant;
@@ -0,0 +1,12 @@
1
+ //#region src/bytes/flate.d.ts
2
+ declare const MAX_INFLATE_OUTPUT_BYTES: number;
3
+ type DeflateLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
4
+ declare function deflate(data: Uint8Array<ArrayBuffer>, level?: DeflateLevel): Uint8Array<ArrayBuffer>;
5
+ declare function inflate(data: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
6
+ interface InflateResult {
7
+ readonly bytes: Uint8Array<ArrayBuffer>;
8
+ readonly recovered: boolean;
9
+ }
10
+ declare function inflateTolerant(data: Uint8Array<ArrayBuffer>): InflateResult;
11
+ //#endregion
12
+ export { DeflateLevel, InflateResult, MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant };
@@ -0,0 +1,12 @@
1
+ //#region src/bytes/flate.d.ts
2
+ declare const MAX_INFLATE_OUTPUT_BYTES: number;
3
+ type DeflateLevel = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
4
+ declare function deflate(data: Uint8Array<ArrayBuffer>, level?: DeflateLevel): Uint8Array<ArrayBuffer>;
5
+ declare function inflate(data: Uint8Array<ArrayBuffer>): Uint8Array<ArrayBuffer>;
6
+ interface InflateResult {
7
+ readonly bytes: Uint8Array<ArrayBuffer>;
8
+ readonly recovered: boolean;
9
+ }
10
+ declare function inflateTolerant(data: Uint8Array<ArrayBuffer>): InflateResult;
11
+ //#endregion
12
+ export { DeflateLevel, InflateResult, MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant };
@@ -0,0 +1,49 @@
1
+ import { isAsciiWhitespace } from "./reader.js";
2
+ import { concatBytes } from "./writer.js";
3
+ import { Unzlib, inflateSync, unzlibSync, zlibSync } from "fflate";
4
+ //#region src/bytes/flate.ts
5
+ const MAX_INFLATE_OUTPUT_BYTES = 536870912;
6
+ function deflate(data, level) {
7
+ return zlibSync(data, level === void 0 ? void 0 : { level });
8
+ }
9
+ function inflate(data) {
10
+ const out = unzlibSync(data);
11
+ if (out.length > 536870912) throw new Error(`inflated output exceeds the ${MAX_INFLATE_OUTPUT_BYTES}-byte limit`);
12
+ return out;
13
+ }
14
+ function inflateTolerant(data) {
15
+ try {
16
+ return {
17
+ bytes: inflate(data),
18
+ recovered: false
19
+ };
20
+ } catch {}
21
+ let offset = 0;
22
+ while (offset < data.length && isAsciiWhitespace(data[offset])) offset++;
23
+ if (offset > 0) try {
24
+ return {
25
+ bytes: inflate(data.subarray(offset)),
26
+ recovered: true
27
+ };
28
+ } catch {}
29
+ try {
30
+ return {
31
+ bytes: inflateSync(data),
32
+ recovered: true
33
+ };
34
+ } catch {}
35
+ const chunks = [];
36
+ const unzlib = new Unzlib((chunk) => {
37
+ chunks.push(chunk);
38
+ });
39
+ try {
40
+ unzlib.push(data, false);
41
+ } catch {}
42
+ if (chunks.length === 0) throw new Error("unable to inflate stream: no data could be recovered");
43
+ return {
44
+ bytes: concatBytes(chunks),
45
+ recovered: true
46
+ };
47
+ }
48
+ //#endregion
49
+ export { MAX_INFLATE_OUTPUT_BYTES, deflate, inflate, inflateTolerant };
@@ -0,0 +1,60 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/bytes/reader.ts
3
+ const ASCII_WHITESPACE_BYTES = /* @__PURE__ */ new Set([
4
+ 0,
5
+ 9,
6
+ 10,
7
+ 12,
8
+ 13,
9
+ 32
10
+ ]);
11
+ function isAsciiWhitespace(byte) {
12
+ return byte !== void 0 && ASCII_WHITESPACE_BYTES.has(byte);
13
+ }
14
+ var ByteReader = class {
15
+ bytes;
16
+ position = 0;
17
+ constructor(bytes) {
18
+ this.bytes = bytes;
19
+ }
20
+ get offset() {
21
+ return this.position;
22
+ }
23
+ get length() {
24
+ return this.bytes.length;
25
+ }
26
+ atEnd() {
27
+ return this.position >= this.bytes.length;
28
+ }
29
+ peek(aheadBy = 0) {
30
+ return this.bytes[this.position + aheadBy];
31
+ }
32
+ next() {
33
+ const byte = this.bytes[this.position];
34
+ if (byte !== void 0) this.position++;
35
+ return byte;
36
+ }
37
+ mark() {
38
+ return this.position;
39
+ }
40
+ reset(mark) {
41
+ this.position = mark;
42
+ }
43
+ seek(offset) {
44
+ this.position = offset;
45
+ }
46
+ slice(start, end) {
47
+ return this.bytes.subarray(start, end);
48
+ }
49
+ skipWhitespace() {
50
+ while (isAsciiWhitespace(this.peek())) this.position++;
51
+ }
52
+ matchKeyword(keyword) {
53
+ for (let i = 0; i < keyword.length; i++) if (this.peek(i) !== keyword.charCodeAt(i)) return false;
54
+ this.position += keyword.length;
55
+ return true;
56
+ }
57
+ };
58
+ //#endregion
59
+ exports.ByteReader = ByteReader;
60
+ exports.isAsciiWhitespace = isAsciiWhitespace;
@@ -0,0 +1,20 @@
1
+ //#region src/bytes/reader.d.ts
2
+ declare function isAsciiWhitespace(byte: number | undefined): boolean;
3
+ declare class ByteReader {
4
+ private readonly bytes;
5
+ private position;
6
+ constructor(bytes: Uint8Array<ArrayBuffer>);
7
+ get offset(): number;
8
+ get length(): number;
9
+ atEnd(): boolean;
10
+ peek(aheadBy?: number): number | undefined;
11
+ next(): number | undefined;
12
+ mark(): number;
13
+ reset(mark: number): void;
14
+ seek(offset: number): void;
15
+ slice(start: number, end: number): Uint8Array<ArrayBuffer>;
16
+ skipWhitespace(): void;
17
+ matchKeyword(keyword: string): boolean;
18
+ }
19
+ //#endregion
20
+ export { ByteReader, isAsciiWhitespace };
@@ -0,0 +1,20 @@
1
+ //#region src/bytes/reader.d.ts
2
+ declare function isAsciiWhitespace(byte: number | undefined): boolean;
3
+ declare class ByteReader {
4
+ private readonly bytes;
5
+ private position;
6
+ constructor(bytes: Uint8Array<ArrayBuffer>);
7
+ get offset(): number;
8
+ get length(): number;
9
+ atEnd(): boolean;
10
+ peek(aheadBy?: number): number | undefined;
11
+ next(): number | undefined;
12
+ mark(): number;
13
+ reset(mark: number): void;
14
+ seek(offset: number): void;
15
+ slice(start: number, end: number): Uint8Array<ArrayBuffer>;
16
+ skipWhitespace(): void;
17
+ matchKeyword(keyword: string): boolean;
18
+ }
19
+ //#endregion
20
+ export { ByteReader, isAsciiWhitespace };
@@ -0,0 +1,58 @@
1
+ //#region src/bytes/reader.ts
2
+ const ASCII_WHITESPACE_BYTES = /* @__PURE__ */ new Set([
3
+ 0,
4
+ 9,
5
+ 10,
6
+ 12,
7
+ 13,
8
+ 32
9
+ ]);
10
+ function isAsciiWhitespace(byte) {
11
+ return byte !== void 0 && ASCII_WHITESPACE_BYTES.has(byte);
12
+ }
13
+ var ByteReader = class {
14
+ bytes;
15
+ position = 0;
16
+ constructor(bytes) {
17
+ this.bytes = bytes;
18
+ }
19
+ get offset() {
20
+ return this.position;
21
+ }
22
+ get length() {
23
+ return this.bytes.length;
24
+ }
25
+ atEnd() {
26
+ return this.position >= this.bytes.length;
27
+ }
28
+ peek(aheadBy = 0) {
29
+ return this.bytes[this.position + aheadBy];
30
+ }
31
+ next() {
32
+ const byte = this.bytes[this.position];
33
+ if (byte !== void 0) this.position++;
34
+ return byte;
35
+ }
36
+ mark() {
37
+ return this.position;
38
+ }
39
+ reset(mark) {
40
+ this.position = mark;
41
+ }
42
+ seek(offset) {
43
+ this.position = offset;
44
+ }
45
+ slice(start, end) {
46
+ return this.bytes.subarray(start, end);
47
+ }
48
+ skipWhitespace() {
49
+ while (isAsciiWhitespace(this.peek())) this.position++;
50
+ }
51
+ matchKeyword(keyword) {
52
+ for (let i = 0; i < keyword.length; i++) if (this.peek(i) !== keyword.charCodeAt(i)) return false;
53
+ this.position += keyword.length;
54
+ return true;
55
+ }
56
+ };
57
+ //#endregion
58
+ export { ByteReader, isAsciiWhitespace };
@@ -0,0 +1,37 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/bytes/writer.ts
3
+ var ByteWriter = class {
4
+ chunks = [];
5
+ byteLength = 0;
6
+ get length() {
7
+ return this.byteLength;
8
+ }
9
+ writeBytes(bytes) {
10
+ if (bytes.length === 0) return;
11
+ this.chunks.push(bytes);
12
+ this.byteLength += bytes.length;
13
+ }
14
+ writeByte(byte) {
15
+ this.writeBytes(new Uint8Array([byte]));
16
+ }
17
+ writeAscii(text) {
18
+ this.writeBytes(new TextEncoder().encode(text));
19
+ }
20
+ toBytes() {
21
+ const out = new Uint8Array(this.byteLength);
22
+ let offset = 0;
23
+ for (const chunk of this.chunks) {
24
+ out.set(chunk, offset);
25
+ offset += chunk.length;
26
+ }
27
+ return out;
28
+ }
29
+ };
30
+ function concatBytes(chunks) {
31
+ const writer = new ByteWriter();
32
+ for (const chunk of chunks) writer.writeBytes(chunk);
33
+ return writer.toBytes();
34
+ }
35
+ //#endregion
36
+ exports.ByteWriter = ByteWriter;
37
+ exports.concatBytes = concatBytes;
@@ -0,0 +1,13 @@
1
+ //#region src/bytes/writer.d.ts
2
+ declare class ByteWriter {
3
+ private readonly chunks;
4
+ private byteLength;
5
+ get length(): number;
6
+ writeBytes(bytes: Uint8Array<ArrayBuffer>): void;
7
+ writeByte(byte: number): void;
8
+ writeAscii(text: string): void;
9
+ toBytes(): Uint8Array<ArrayBuffer>;
10
+ }
11
+ declare function concatBytes(chunks: readonly Uint8Array<ArrayBuffer>[]): Uint8Array<ArrayBuffer>;
12
+ //#endregion
13
+ export { ByteWriter, concatBytes };
@@ -0,0 +1,13 @@
1
+ //#region src/bytes/writer.d.ts
2
+ declare class ByteWriter {
3
+ private readonly chunks;
4
+ private byteLength;
5
+ get length(): number;
6
+ writeBytes(bytes: Uint8Array<ArrayBuffer>): void;
7
+ writeByte(byte: number): void;
8
+ writeAscii(text: string): void;
9
+ toBytes(): Uint8Array<ArrayBuffer>;
10
+ }
11
+ declare function concatBytes(chunks: readonly Uint8Array<ArrayBuffer>[]): Uint8Array<ArrayBuffer>;
12
+ //#endregion
13
+ export { ByteWriter, concatBytes };
@@ -0,0 +1,35 @@
1
+ //#region src/bytes/writer.ts
2
+ var ByteWriter = class {
3
+ chunks = [];
4
+ byteLength = 0;
5
+ get length() {
6
+ return this.byteLength;
7
+ }
8
+ writeBytes(bytes) {
9
+ if (bytes.length === 0) return;
10
+ this.chunks.push(bytes);
11
+ this.byteLength += bytes.length;
12
+ }
13
+ writeByte(byte) {
14
+ this.writeBytes(new Uint8Array([byte]));
15
+ }
16
+ writeAscii(text) {
17
+ this.writeBytes(new TextEncoder().encode(text));
18
+ }
19
+ toBytes() {
20
+ const out = new Uint8Array(this.byteLength);
21
+ let offset = 0;
22
+ for (const chunk of this.chunks) {
23
+ out.set(chunk, offset);
24
+ offset += chunk.length;
25
+ }
26
+ return out;
27
+ }
28
+ };
29
+ function concatBytes(chunks) {
30
+ const writer = new ByteWriter();
31
+ for (const chunk of chunks) writer.writeBytes(chunk);
32
+ return writer.toBytes();
33
+ }
34
+ //#endregion
35
+ export { ByteWriter, concatBytes };
package/dist/cmap.cjs ADDED
@@ -0,0 +1,110 @@
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
+ //#region src/cmap.ts
5
+ function hexBytesToNumber(bytes) {
6
+ let value = 0;
7
+ for (const byte of bytes) value = value * 256 + byte;
8
+ return value;
9
+ }
10
+ function decodeUtf16BEString(bytes) {
11
+ const units = [];
12
+ for (let i = 0; i + 1 < bytes.length; i += 2) units.push((bytes[i] ?? 0) << 8 | (bytes[i + 1] ?? 0));
13
+ return String.fromCharCode(...units);
14
+ }
15
+ function parseToUnicodeCMap(bytes, sink) {
16
+ const reader = new require_bytes_reader.ByteReader(bytes);
17
+ const map = /* @__PURE__ */ new Map();
18
+ for (;;) {
19
+ const token = require_lexer.nextToken(reader);
20
+ if (token === void 0) break;
21
+ if (token.kind === "keyword" && token.value === "beginbfchar") readBfChar(reader, map, sink);
22
+ else if (token.kind === "keyword" && token.value === "beginbfrange") readBfRange(reader, map, sink);
23
+ }
24
+ return { lookup: (code) => map.get(code) };
25
+ }
26
+ function readBfChar(reader, map, sink) {
27
+ for (;;) {
28
+ const srcTok = require_lexer.nextToken(reader);
29
+ if (srcTok === void 0) {
30
+ sink({
31
+ code: "pdf/cmap-truncated",
32
+ severity: "warning",
33
+ message: "bfchar section was truncated before endbfchar"
34
+ });
35
+ return;
36
+ }
37
+ if (srcTok.kind === "keyword" && srcTok.value === "endbfchar") return;
38
+ if (srcTok.kind !== "hexString") continue;
39
+ const dstTok = require_lexer.nextToken(reader);
40
+ if (dstTok?.kind !== "hexString") {
41
+ sink({
42
+ code: "pdf/cmap-entry-invalid",
43
+ severity: "warning",
44
+ message: "bfchar entry had no valid destination hex string"
45
+ });
46
+ continue;
47
+ }
48
+ map.set(hexBytesToNumber(srcTok.value), decodeUtf16BEString(dstTok.value));
49
+ }
50
+ }
51
+ function readBfRange(reader, map, sink) {
52
+ for (;;) {
53
+ const loTok = require_lexer.nextToken(reader);
54
+ if (loTok === void 0) {
55
+ sink({
56
+ code: "pdf/cmap-truncated",
57
+ severity: "warning",
58
+ message: "bfrange section was truncated before endbfrange"
59
+ });
60
+ return;
61
+ }
62
+ if (loTok.kind === "keyword" && loTok.value === "endbfrange") return;
63
+ if (loTok.kind !== "hexString") continue;
64
+ const hiTok = require_lexer.nextToken(reader);
65
+ if (hiTok?.kind !== "hexString") {
66
+ sink({
67
+ code: "pdf/cmap-entry-invalid",
68
+ severity: "warning",
69
+ message: "bfrange entry had no valid high-end hex string"
70
+ });
71
+ continue;
72
+ }
73
+ const lo = hexBytesToNumber(loTok.value);
74
+ const hi = hexBytesToNumber(hiTok.value);
75
+ const dstTok = require_lexer.nextToken(reader);
76
+ if (dstTok?.kind === "hexString") registerBfRangeSingle(map, lo, hi, dstTok.value);
77
+ else if (dstTok?.kind === "arrayStart") readBfRangeArray(reader, map, lo, sink);
78
+ else sink({
79
+ code: "pdf/cmap-entry-invalid",
80
+ severity: "warning",
81
+ message: "bfrange entry had no valid destination"
82
+ });
83
+ }
84
+ }
85
+ function registerBfRangeSingle(map, lo, hi, dstBytes) {
86
+ if (dstBytes.length < 2) return;
87
+ const prefix = decodeUtf16BEString(dstBytes.subarray(0, dstBytes.length - 2));
88
+ const baseUnit = (dstBytes[dstBytes.length - 2] ?? 0) << 8 | (dstBytes[dstBytes.length - 1] ?? 0);
89
+ for (let code = lo; code <= hi; code++) map.set(code, prefix + String.fromCharCode(baseUnit + (code - lo)));
90
+ }
91
+ function readBfRangeArray(reader, map, lo, sink) {
92
+ let code = lo;
93
+ for (;;) {
94
+ const token = require_lexer.nextToken(reader);
95
+ if (token === void 0) {
96
+ sink({
97
+ code: "pdf/cmap-truncated",
98
+ severity: "warning",
99
+ message: "bfrange array destination was truncated"
100
+ });
101
+ return;
102
+ }
103
+ if (token.kind === "arrayEnd") return;
104
+ if (token.kind !== "hexString") continue;
105
+ map.set(code, decodeUtf16BEString(token.value));
106
+ code++;
107
+ }
108
+ }
109
+ //#endregion
110
+ exports.parseToUnicodeCMap = parseToUnicodeCMap;
@@ -0,0 +1,8 @@
1
+ import { PdfDiagnosticSink } from "./diagnostics.cjs";
2
+ //#region src/cmap.d.ts
3
+ interface ToUnicodeCMap {
4
+ lookup(code: number): string | undefined;
5
+ }
6
+ declare function parseToUnicodeCMap(bytes: Uint8Array<ArrayBuffer>, sink: PdfDiagnosticSink): ToUnicodeCMap;
7
+ //#endregion
8
+ export { ToUnicodeCMap, parseToUnicodeCMap };
package/dist/cmap.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ import { PdfDiagnosticSink } from "./diagnostics.js";
2
+ //#region src/cmap.d.ts
3
+ interface ToUnicodeCMap {
4
+ lookup(code: number): string | undefined;
5
+ }
6
+ declare function parseToUnicodeCMap(bytes: Uint8Array<ArrayBuffer>, sink: PdfDiagnosticSink): ToUnicodeCMap;
7
+ //#endregion
8
+ export { ToUnicodeCMap, parseToUnicodeCMap };
package/dist/cmap.js ADDED
@@ -0,0 +1,109 @@
1
+ import { ByteReader } from "./bytes/reader.js";
2
+ import { nextToken } from "./lexer.js";
3
+ //#region src/cmap.ts
4
+ function hexBytesToNumber(bytes) {
5
+ let value = 0;
6
+ for (const byte of bytes) value = value * 256 + byte;
7
+ return value;
8
+ }
9
+ function decodeUtf16BEString(bytes) {
10
+ const units = [];
11
+ for (let i = 0; i + 1 < bytes.length; i += 2) units.push((bytes[i] ?? 0) << 8 | (bytes[i + 1] ?? 0));
12
+ return String.fromCharCode(...units);
13
+ }
14
+ function parseToUnicodeCMap(bytes, sink) {
15
+ const reader = new ByteReader(bytes);
16
+ const map = /* @__PURE__ */ new Map();
17
+ for (;;) {
18
+ const token = nextToken(reader);
19
+ if (token === void 0) break;
20
+ if (token.kind === "keyword" && token.value === "beginbfchar") readBfChar(reader, map, sink);
21
+ else if (token.kind === "keyword" && token.value === "beginbfrange") readBfRange(reader, map, sink);
22
+ }
23
+ return { lookup: (code) => map.get(code) };
24
+ }
25
+ function readBfChar(reader, map, sink) {
26
+ for (;;) {
27
+ const srcTok = nextToken(reader);
28
+ if (srcTok === void 0) {
29
+ sink({
30
+ code: "pdf/cmap-truncated",
31
+ severity: "warning",
32
+ message: "bfchar section was truncated before endbfchar"
33
+ });
34
+ return;
35
+ }
36
+ if (srcTok.kind === "keyword" && srcTok.value === "endbfchar") return;
37
+ if (srcTok.kind !== "hexString") continue;
38
+ const dstTok = nextToken(reader);
39
+ if (dstTok?.kind !== "hexString") {
40
+ sink({
41
+ code: "pdf/cmap-entry-invalid",
42
+ severity: "warning",
43
+ message: "bfchar entry had no valid destination hex string"
44
+ });
45
+ continue;
46
+ }
47
+ map.set(hexBytesToNumber(srcTok.value), decodeUtf16BEString(dstTok.value));
48
+ }
49
+ }
50
+ function readBfRange(reader, map, sink) {
51
+ for (;;) {
52
+ const loTok = nextToken(reader);
53
+ if (loTok === void 0) {
54
+ sink({
55
+ code: "pdf/cmap-truncated",
56
+ severity: "warning",
57
+ message: "bfrange section was truncated before endbfrange"
58
+ });
59
+ return;
60
+ }
61
+ if (loTok.kind === "keyword" && loTok.value === "endbfrange") return;
62
+ if (loTok.kind !== "hexString") continue;
63
+ const hiTok = nextToken(reader);
64
+ if (hiTok?.kind !== "hexString") {
65
+ sink({
66
+ code: "pdf/cmap-entry-invalid",
67
+ severity: "warning",
68
+ message: "bfrange entry had no valid high-end hex string"
69
+ });
70
+ continue;
71
+ }
72
+ const lo = hexBytesToNumber(loTok.value);
73
+ const hi = hexBytesToNumber(hiTok.value);
74
+ const dstTok = nextToken(reader);
75
+ if (dstTok?.kind === "hexString") registerBfRangeSingle(map, lo, hi, dstTok.value);
76
+ else if (dstTok?.kind === "arrayStart") readBfRangeArray(reader, map, lo, sink);
77
+ else sink({
78
+ code: "pdf/cmap-entry-invalid",
79
+ severity: "warning",
80
+ message: "bfrange entry had no valid destination"
81
+ });
82
+ }
83
+ }
84
+ function registerBfRangeSingle(map, lo, hi, dstBytes) {
85
+ if (dstBytes.length < 2) return;
86
+ const prefix = decodeUtf16BEString(dstBytes.subarray(0, dstBytes.length - 2));
87
+ const baseUnit = (dstBytes[dstBytes.length - 2] ?? 0) << 8 | (dstBytes[dstBytes.length - 1] ?? 0);
88
+ for (let code = lo; code <= hi; code++) map.set(code, prefix + String.fromCharCode(baseUnit + (code - lo)));
89
+ }
90
+ function readBfRangeArray(reader, map, lo, sink) {
91
+ let code = lo;
92
+ for (;;) {
93
+ const token = nextToken(reader);
94
+ if (token === void 0) {
95
+ sink({
96
+ code: "pdf/cmap-truncated",
97
+ severity: "warning",
98
+ message: "bfrange array destination was truncated"
99
+ });
100
+ return;
101
+ }
102
+ if (token.kind === "arrayEnd") return;
103
+ if (token.kind !== "hexString") continue;
104
+ map.set(code, decodeUtf16BEString(token.value));
105
+ code++;
106
+ }
107
+ }
108
+ //#endregion
109
+ export { parseToUnicodeCMap };