pdf-codec 1.5.0 → 1.7.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 (214) hide show
  1. package/README.md +100 -9
  2. package/dist/assets/caladea-bold.cjs +5 -0
  3. package/dist/assets/caladea-bold.d.cts +4 -0
  4. package/dist/assets/caladea-bold.d.ts +4 -0
  5. package/dist/assets/caladea-bold.js +4 -0
  6. package/dist/assets/caladea-bolditalic.cjs +5 -0
  7. package/dist/assets/caladea-bolditalic.d.cts +4 -0
  8. package/dist/assets/caladea-bolditalic.d.ts +4 -0
  9. package/dist/assets/caladea-bolditalic.js +4 -0
  10. package/dist/assets/caladea-italic.cjs +5 -0
  11. package/dist/assets/caladea-italic.d.cts +4 -0
  12. package/dist/assets/caladea-italic.d.ts +4 -0
  13. package/dist/assets/caladea-italic.js +4 -0
  14. package/dist/assets/caladea-regular.cjs +5 -0
  15. package/dist/assets/caladea-regular.d.cts +4 -0
  16. package/dist/assets/caladea-regular.d.ts +4 -0
  17. package/dist/assets/caladea-regular.js +4 -0
  18. package/dist/assets/carlito-bold.cjs +5 -0
  19. package/dist/assets/carlito-bold.d.cts +4 -0
  20. package/dist/assets/carlito-bold.d.ts +4 -0
  21. package/dist/assets/carlito-bold.js +4 -0
  22. package/dist/assets/carlito-bolditalic.cjs +5 -0
  23. package/dist/assets/carlito-bolditalic.d.cts +4 -0
  24. package/dist/assets/carlito-bolditalic.d.ts +4 -0
  25. package/dist/assets/carlito-bolditalic.js +4 -0
  26. package/dist/assets/carlito-italic.cjs +5 -0
  27. package/dist/assets/carlito-italic.d.cts +4 -0
  28. package/dist/assets/carlito-italic.d.ts +4 -0
  29. package/dist/assets/carlito-italic.js +4 -0
  30. package/dist/assets/carlito-regular.cjs +5 -0
  31. package/dist/assets/carlito-regular.d.cts +4 -0
  32. package/dist/assets/carlito-regular.d.ts +4 -0
  33. package/dist/assets/carlito-regular.js +4 -0
  34. package/dist/cff-bounds.cjs +432 -0
  35. package/dist/cff-bounds.d.cts +9 -0
  36. package/dist/cff-bounds.d.ts +9 -0
  37. package/dist/cff-bounds.js +431 -0
  38. package/dist/cff-probe.cjs +35 -0
  39. package/dist/cff-probe.d.cts +10 -0
  40. package/dist/cff-probe.d.ts +10 -0
  41. package/dist/cff-probe.js +34 -0
  42. package/dist/cff.cjs +158 -0
  43. package/dist/cff.d.cts +17 -0
  44. package/dist/cff.d.ts +17 -0
  45. package/dist/cff.js +150 -0
  46. package/dist/cmap-table.cjs +108 -0
  47. package/dist/cmap-table.d.cts +6 -0
  48. package/dist/cmap-table.d.ts +6 -0
  49. package/dist/cmap-table.js +107 -0
  50. package/dist/content-write.cjs +34 -14
  51. package/dist/content-write.d.cts +9 -2
  52. package/dist/content-write.d.ts +9 -2
  53. package/dist/content-write.js +34 -14
  54. package/dist/embedded-font-CLedn-mT.d.cts +42 -0
  55. package/dist/embedded-font-CbqkKgBl.d.ts +42 -0
  56. package/dist/embedded-font-write.cjs +92 -0
  57. package/dist/embedded-font-write.d.cts +22 -0
  58. package/dist/embedded-font-write.d.ts +22 -0
  59. package/dist/embedded-font-write.js +90 -0
  60. package/dist/embedded-font.cjs +142 -0
  61. package/dist/embedded-font.d.cts +2 -0
  62. package/dist/embedded-font.d.ts +2 -0
  63. package/dist/embedded-font.js +138 -0
  64. package/dist/filters.cjs +45 -2
  65. package/dist/filters.d.cts +4 -3
  66. package/dist/filters.d.ts +4 -3
  67. package/dist/filters.js +45 -2
  68. package/dist/font-registry-Cn7P9lAh.d.ts +38 -0
  69. package/dist/font-registry-yby02snZ.d.cts +38 -0
  70. package/dist/font-registry.cjs +152 -0
  71. package/dist/font-registry.d.cts +2 -0
  72. package/dist/font-registry.d.ts +2 -0
  73. package/dist/font-registry.js +150 -0
  74. package/dist/font-substitutes.cjs +13 -0
  75. package/dist/font-substitutes.d.cts +5 -0
  76. package/dist/font-substitutes.d.ts +5 -0
  77. package/dist/font-substitutes.js +12 -0
  78. package/dist/font-tables.cjs +127 -0
  79. package/dist/font-tables.d.cts +43 -0
  80. package/dist/font-tables.d.ts +43 -0
  81. package/dist/font-tables.js +122 -0
  82. package/dist/fonts.cjs +1 -0
  83. package/dist/fonts.d.cts +2 -1
  84. package/dist/fonts.d.ts +2 -1
  85. package/dist/fonts.js +1 -1
  86. package/dist/formula.d.cts +1 -1
  87. package/dist/formula.d.ts +1 -1
  88. package/dist/glyf.cjs +183 -0
  89. package/dist/glyf.d.cts +33 -0
  90. package/dist/glyf.d.ts +33 -0
  91. package/dist/glyf.js +181 -0
  92. package/dist/glyph-bounds-BV_Z40KS.d.cts +10 -0
  93. package/dist/glyph-bounds-BV_Z40KS.d.ts +10 -0
  94. package/dist/glyph-bounds.cjs +12 -0
  95. package/dist/glyph-bounds.d.cts +2 -0
  96. package/dist/glyph-bounds.d.ts +2 -0
  97. package/dist/glyph-bounds.js +11 -0
  98. package/dist/{math-hmtx.cjs → hmtx-table.cjs} +3 -3
  99. package/dist/{math-hmtx.d.ts → hmtx-table.d.cts} +2 -2
  100. package/dist/{math-hmtx.d.cts → hmtx-table.d.ts} +2 -2
  101. package/dist/{math-hmtx.js → hmtx-table.js} +3 -3
  102. package/dist/image/jbig2-arith.cjs +432 -0
  103. package/dist/image/jbig2-arith.d.cts +23 -0
  104. package/dist/image/jbig2-arith.d.ts +23 -0
  105. package/dist/image/jbig2-arith.js +426 -0
  106. package/dist/image/jbig2-bitmap.cjs +80 -0
  107. package/dist/image/jbig2-bitmap.d.cts +20 -0
  108. package/dist/image/jbig2-bitmap.d.ts +20 -0
  109. package/dist/image/jbig2-bitmap.js +72 -0
  110. package/dist/image/jbig2-errors.cjs +17 -0
  111. package/dist/image/jbig2-errors.d.cts +2 -0
  112. package/dist/image/jbig2-errors.d.ts +2 -0
  113. package/dist/image/jbig2-errors.js +15 -0
  114. package/dist/image/jbig2-generic.cjs +242 -0
  115. package/dist/image/jbig2-generic.d.cts +27 -0
  116. package/dist/image/jbig2-generic.d.ts +27 -0
  117. package/dist/image/jbig2-generic.js +237 -0
  118. package/dist/image/jbig2-text.cjs +175 -0
  119. package/dist/image/jbig2-text.d.cts +58 -0
  120. package/dist/image/jbig2-text.d.ts +58 -0
  121. package/dist/image/jbig2-text.js +170 -0
  122. package/dist/image/jbig2.cjs +333 -0
  123. package/dist/image/jbig2.d.cts +15 -0
  124. package/dist/image/jbig2.d.ts +15 -0
  125. package/dist/image/jbig2.js +332 -0
  126. package/dist/image/jp2-boxes.cjs +148 -0
  127. package/dist/image/jp2-boxes.d.cts +26 -0
  128. package/dist/image/jp2-boxes.d.ts +26 -0
  129. package/dist/image/jp2-boxes.js +146 -0
  130. package/dist/image/jpeg2000-codestream.cjs +309 -0
  131. package/dist/image/jpeg2000-codestream.d.cts +78 -0
  132. package/dist/image/jpeg2000-codestream.d.ts +78 -0
  133. package/dist/image/jpeg2000-codestream.js +308 -0
  134. package/dist/image/jpeg2000-dwt.cjs +148 -0
  135. package/dist/image/jpeg2000-dwt.d.cts +34 -0
  136. package/dist/image/jpeg2000-dwt.d.ts +34 -0
  137. package/dist/image/jpeg2000-dwt.js +145 -0
  138. package/dist/image/jpeg2000-errors.cjs +17 -0
  139. package/dist/image/jpeg2000-errors.d.cts +9 -0
  140. package/dist/image/jpeg2000-errors.d.ts +9 -0
  141. package/dist/image/jpeg2000-errors.js +15 -0
  142. package/dist/image/jpeg2000-t1.cjs +254 -0
  143. package/dist/image/jpeg2000-t1.d.cts +18 -0
  144. package/dist/image/jpeg2000-t1.d.ts +18 -0
  145. package/dist/image/jpeg2000-t1.js +253 -0
  146. package/dist/image/jpeg2000-t2.cjs +316 -0
  147. package/dist/image/jpeg2000-t2.d.cts +79 -0
  148. package/dist/image/jpeg2000-t2.d.ts +79 -0
  149. package/dist/image/jpeg2000-t2.js +311 -0
  150. package/dist/image/jpeg2000-tagtree.cjs +105 -0
  151. package/dist/image/jpeg2000-tagtree.d.cts +29 -0
  152. package/dist/image/jpeg2000-tagtree.d.ts +29 -0
  153. package/dist/image/jpeg2000-tagtree.js +103 -0
  154. package/dist/image/jpeg2000.cjs +322 -0
  155. package/dist/image/jpeg2000.d.cts +54 -0
  156. package/dist/image/jpeg2000.d.ts +54 -0
  157. package/dist/image/jpeg2000.js +320 -0
  158. package/dist/images-read.cjs +78 -1
  159. package/dist/images-read.js +78 -1
  160. package/dist/index.cjs +22 -0
  161. package/dist/index.d.cts +14 -3
  162. package/dist/index.d.ts +14 -3
  163. package/dist/index.js +9 -2
  164. package/dist/jbig2-errors-Bj6MPxrx.d.cts +9 -0
  165. package/dist/jbig2-errors-Bj6MPxrx.d.ts +9 -0
  166. package/dist/math-font-write.cjs +2 -31
  167. package/dist/math-font-write.js +1 -30
  168. package/dist/math-font.cjs +50 -23
  169. package/dist/math-font.d.cts +8 -1
  170. package/dist/math-font.d.ts +8 -1
  171. package/dist/math-font.js +48 -21
  172. package/dist/math-stretch-Bwk_bxh1.d.ts +24 -0
  173. package/dist/math-stretch-DXWjs8gL.d.cts +24 -0
  174. package/dist/math-stretch.cjs +96 -0
  175. package/dist/math-stretch.d.cts +2 -0
  176. package/dist/math-stretch.d.ts +2 -0
  177. package/dist/math-stretch.js +94 -0
  178. package/dist/math-table-DhPrNoPa.d.ts +70 -0
  179. package/dist/math-table-gFNB8DmQ.d.cts +70 -0
  180. package/dist/math-table.cjs +69 -1
  181. package/dist/math-table.d.cts +2 -45
  182. package/dist/math-table.d.ts +2 -45
  183. package/dist/math-table.js +69 -1
  184. package/dist/{math-types-Ba8qAf1p.d.cts → math-types-YSQUTviw.d.cts} +2 -0
  185. package/dist/{math-types-Ba8qAf1p.d.ts → math-types-YSQUTviw.d.ts} +2 -0
  186. package/dist/math-types.d.cts +1 -1
  187. package/dist/math-types.d.ts +1 -1
  188. package/dist/measure.cjs +65 -14
  189. package/dist/measure.d.cts +8 -1
  190. package/dist/measure.d.ts +8 -1
  191. package/dist/measure.js +64 -15
  192. package/dist/sfnt-V08bb5ut.d.cts +21 -0
  193. package/dist/sfnt-V08bb5ut.d.ts +21 -0
  194. package/dist/sfnt-subset.cjs +250 -0
  195. package/dist/sfnt-subset.d.cts +11 -0
  196. package/dist/sfnt-subset.d.ts +11 -0
  197. package/dist/sfnt-subset.js +249 -0
  198. package/dist/sfnt.cjs +50 -4
  199. package/dist/sfnt.d.cts +2 -17
  200. package/dist/sfnt.d.ts +2 -17
  201. package/dist/sfnt.js +47 -5
  202. package/dist/tounicode.cjs +34 -0
  203. package/dist/tounicode.d.cts +5 -0
  204. package/dist/tounicode.d.ts +5 -0
  205. package/dist/tounicode.js +33 -0
  206. package/dist/write.cjs +80 -10
  207. package/dist/write.d.cts +6 -0
  208. package/dist/write.d.ts +6 -0
  209. package/dist/write.js +81 -11
  210. package/package.json +1 -1
  211. package/dist/math-cmap.cjs +0 -81
  212. package/dist/math-cmap.d.cts +0 -6
  213. package/dist/math-cmap.d.ts +0 -6
  214. package/dist/math-cmap.js +0 -80
@@ -0,0 +1,42 @@
1
+ import { t as SfntFont } from "./sfnt-V08bb5ut.cjs";
2
+ //#region src/embedded-font.d.ts
3
+ declare const NOTDEF_GLYPH_ID = 0;
4
+ interface EmbeddedFaceMetrics {
5
+ readonly unitsPerEm: number;
6
+ readonly ascentGlyphSpace: number;
7
+ readonly descentGlyphSpace: number;
8
+ readonly lineGapGlyphSpace: number;
9
+ readonly typoAscentGlyphSpace: number | undefined;
10
+ readonly typoDescentGlyphSpace: number | undefined;
11
+ readonly typoLineGapGlyphSpace: number | undefined;
12
+ readonly winAscentGlyphSpace: number | undefined;
13
+ readonly winDescentGlyphSpace: number | undefined;
14
+ readonly capHeightGlyphSpace: number;
15
+ readonly xHeightGlyphSpace: number | undefined;
16
+ readonly bboxGlyphSpace: readonly [number, number, number, number];
17
+ readonly italicAngleDegrees: number;
18
+ readonly underlinePositionGlyphSpace: number;
19
+ readonly underlineThicknessGlyphSpace: number;
20
+ readonly serif: boolean;
21
+ }
22
+ interface EmbeddedFace {
23
+ readonly font: SfntFont;
24
+ readonly postScriptName: string;
25
+ readonly numGlyphs: number;
26
+ readonly metrics: EmbeddedFaceMetrics;
27
+ glyphId(codePoint: number): number | undefined;
28
+ glyphSpaceWidth(glyphId: number): number;
29
+ }
30
+ declare function loadEmbeddedFace(font: SfntFont): EmbeddedFace | undefined;
31
+ interface EmbeddedFaceSubstitution {
32
+ readonly from: string;
33
+ }
34
+ interface EmbeddedShow {
35
+ readonly codes: Uint8Array<ArrayBuffer>;
36
+ readonly width1000: number;
37
+ readonly substitutions: readonly EmbeddedFaceSubstitution[];
38
+ }
39
+ declare function encodeForShowEmbedded(text: string, face: EmbeddedFace): EmbeddedShow;
40
+ declare function collectEmbeddedGlyphs(texts: Iterable<string>, face: EmbeddedFace): ReadonlyMap<number, number>;
41
+ //#endregion
42
+ export { NOTDEF_GLYPH_ID as a, loadEmbeddedFace as c, EmbeddedShow as i, EmbeddedFaceMetrics as n, collectEmbeddedGlyphs as o, EmbeddedFaceSubstitution as r, encodeForShowEmbedded as s, EmbeddedFace as t };
@@ -0,0 +1,42 @@
1
+ import { t as SfntFont } from "./sfnt-V08bb5ut.js";
2
+ //#region src/embedded-font.d.ts
3
+ declare const NOTDEF_GLYPH_ID = 0;
4
+ interface EmbeddedFaceMetrics {
5
+ readonly unitsPerEm: number;
6
+ readonly ascentGlyphSpace: number;
7
+ readonly descentGlyphSpace: number;
8
+ readonly lineGapGlyphSpace: number;
9
+ readonly typoAscentGlyphSpace: number | undefined;
10
+ readonly typoDescentGlyphSpace: number | undefined;
11
+ readonly typoLineGapGlyphSpace: number | undefined;
12
+ readonly winAscentGlyphSpace: number | undefined;
13
+ readonly winDescentGlyphSpace: number | undefined;
14
+ readonly capHeightGlyphSpace: number;
15
+ readonly xHeightGlyphSpace: number | undefined;
16
+ readonly bboxGlyphSpace: readonly [number, number, number, number];
17
+ readonly italicAngleDegrees: number;
18
+ readonly underlinePositionGlyphSpace: number;
19
+ readonly underlineThicknessGlyphSpace: number;
20
+ readonly serif: boolean;
21
+ }
22
+ interface EmbeddedFace {
23
+ readonly font: SfntFont;
24
+ readonly postScriptName: string;
25
+ readonly numGlyphs: number;
26
+ readonly metrics: EmbeddedFaceMetrics;
27
+ glyphId(codePoint: number): number | undefined;
28
+ glyphSpaceWidth(glyphId: number): number;
29
+ }
30
+ declare function loadEmbeddedFace(font: SfntFont): EmbeddedFace | undefined;
31
+ interface EmbeddedFaceSubstitution {
32
+ readonly from: string;
33
+ }
34
+ interface EmbeddedShow {
35
+ readonly codes: Uint8Array<ArrayBuffer>;
36
+ readonly width1000: number;
37
+ readonly substitutions: readonly EmbeddedFaceSubstitution[];
38
+ }
39
+ declare function encodeForShowEmbedded(text: string, face: EmbeddedFace): EmbeddedShow;
40
+ declare function collectEmbeddedGlyphs(texts: Iterable<string>, face: EmbeddedFace): ReadonlyMap<number, number>;
41
+ //#endregion
42
+ export { NOTDEF_GLYPH_ID as a, loadEmbeddedFace as c, EmbeddedShow as i, EmbeddedFaceMetrics as n, collectEmbeddedGlyphs as o, EmbeddedFaceSubstitution as r, encodeForShowEmbedded as s, EmbeddedFace as t };
@@ -0,0 +1,92 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_bytes_crc32 = require("./bytes/crc32.cjs");
3
+ const require_objects = require("./objects.cjs");
4
+ const require_bytes_flate = require("./bytes/flate.cjs");
5
+ const require_tounicode = require("./tounicode.cjs");
6
+ //#region src/embedded-font-write.ts
7
+ const FLAG_SERIF = 2;
8
+ const FLAG_NONSYMBOLIC = 32;
9
+ const FLAG_ITALIC = 64;
10
+ const NOMINAL_STEM_V = 80;
11
+ const DEFAULT_WIDTH = 1e3;
12
+ const SUBSET_TAG_LENGTH = 6;
13
+ const SUBSET_TAG_ALPHABET_SIZE = 26;
14
+ const SUBSET_TAG_CODE_SPACE = SUBSET_TAG_ALPHABET_SIZE ** SUBSET_TAG_LENGTH;
15
+ const UPPERCASE_A_CHAR_CODE = 65;
16
+ function embeddedSubsetTag(postScriptName, glyphIds) {
17
+ let value = require_bytes_crc32.crc32(new TextEncoder().encode(`${postScriptName} ${glyphIds.join(",")}`)) % SUBSET_TAG_CODE_SPACE;
18
+ let tag = "";
19
+ for (let i = 0; i < SUBSET_TAG_LENGTH; i++) {
20
+ tag = String.fromCharCode(UPPERCASE_A_CHAR_CODE + value % SUBSET_TAG_ALPHABET_SIZE) + tag;
21
+ value = Math.floor(value / SUBSET_TAG_ALPHABET_SIZE);
22
+ }
23
+ return tag;
24
+ }
25
+ function computeFlags(face) {
26
+ let flags = FLAG_NONSYMBOLIC;
27
+ if (face.metrics.serif) flags |= FLAG_SERIF;
28
+ if (face.metrics.italicAngleDegrees !== 0) flags |= FLAG_ITALIC;
29
+ return flags;
30
+ }
31
+ function buildWidthsArray(face, glyphIds) {
32
+ const entries = [];
33
+ for (const glyphId of glyphIds) entries.push(require_objects.pdfNum(glyphId), require_objects.pdfArray([require_objects.pdfNum(face.glyphSpaceWidth(glyphId))]));
34
+ return require_objects.pdfArray(entries);
35
+ }
36
+ function buildFontDescriptor(face, baseFont, fontFileRef) {
37
+ const m = face.metrics;
38
+ const entries = /* @__PURE__ */ new Map([
39
+ ["Type", require_objects.pdfName("FontDescriptor")],
40
+ ["FontName", require_objects.pdfName(baseFont)],
41
+ ["Flags", require_objects.pdfNum(computeFlags(face))],
42
+ ["FontBBox", require_objects.pdfArray(m.bboxGlyphSpace.map((n) => require_objects.pdfNum(n)))],
43
+ ["ItalicAngle", require_objects.pdfNum(m.italicAngleDegrees)],
44
+ ["Ascent", require_objects.pdfNum(m.ascentGlyphSpace)],
45
+ ["Descent", require_objects.pdfNum(m.descentGlyphSpace)],
46
+ ["CapHeight", require_objects.pdfNum(m.capHeightGlyphSpace)],
47
+ ["StemV", require_objects.pdfNum(NOMINAL_STEM_V)]
48
+ ]);
49
+ if (m.xHeightGlyphSpace !== void 0) entries.set("XHeight", require_objects.pdfNum(m.xHeightGlyphSpace));
50
+ entries.set("FontFile2", fontFileRef);
51
+ return require_objects.pdfDict(entries);
52
+ }
53
+ function buildFontFileStream(subsetBytes, compress) {
54
+ const entries = /* @__PURE__ */ new Map([["Length1", require_objects.pdfNum(subsetBytes.length)]]);
55
+ if (compress) entries.set("Filter", require_objects.pdfName("FlateDecode"));
56
+ return require_objects.pdfStream(require_objects.pdfDict(entries), compress ? require_bytes_flate.deflate(subsetBytes) : subsetBytes);
57
+ }
58
+ function buildEmbeddedFontObjects(face, subset, usedGlyphs, refs, compress) {
59
+ const baseFont = `${embeddedSubsetTag(face.postScriptName, subset.glyphIds)}+${face.postScriptName}`;
60
+ const cidFont = require_objects.pdfDict({
61
+ Type: require_objects.pdfName("Font"),
62
+ Subtype: require_objects.pdfName("CIDFontType2"),
63
+ BaseFont: require_objects.pdfName(baseFont),
64
+ CIDSystemInfo: require_objects.pdfDict({
65
+ Registry: require_objects.pdfHexString(new TextEncoder().encode("Adobe")),
66
+ Ordering: require_objects.pdfHexString(new TextEncoder().encode("Identity")),
67
+ Supplement: require_objects.pdfNum(0)
68
+ }),
69
+ FontDescriptor: refs.descriptorRef,
70
+ DW: require_objects.pdfNum(DEFAULT_WIDTH),
71
+ W: buildWidthsArray(face, subset.glyphIds),
72
+ CIDToGIDMap: require_objects.pdfName("Identity")
73
+ });
74
+ return {
75
+ baseFont,
76
+ type0: require_objects.pdfDict({
77
+ Type: require_objects.pdfName("Font"),
78
+ Subtype: require_objects.pdfName("Type0"),
79
+ BaseFont: require_objects.pdfName(baseFont),
80
+ Encoding: require_objects.pdfName("Identity-H"),
81
+ DescendantFonts: require_objects.pdfArray([refs.cidFontRef]),
82
+ ToUnicode: refs.toUnicodeRef
83
+ }),
84
+ cidFont,
85
+ descriptor: buildFontDescriptor(face, baseFont, refs.fontFileRef),
86
+ fontFile: buildFontFileStream(subset.bytes, compress),
87
+ toUnicode: require_tounicode.buildToUnicodeCMap(usedGlyphs)
88
+ };
89
+ }
90
+ //#endregion
91
+ exports.buildEmbeddedFontObjects = buildEmbeddedFontObjects;
92
+ exports.embeddedSubsetTag = embeddedSubsetTag;
@@ -0,0 +1,22 @@
1
+ import { PdfDict, PdfObject } from "./objects.cjs";
2
+ import { t as EmbeddedFace } from "./embedded-font-CLedn-mT.cjs";
3
+ import { SfntSubsetResult } from "./sfnt-subset.cjs";
4
+ //#region src/embedded-font-write.d.ts
5
+ declare function embeddedSubsetTag(postScriptName: string, glyphIds: readonly number[]): string;
6
+ interface EmbeddedFontObjectRefs {
7
+ readonly cidFontRef: PdfObject;
8
+ readonly descriptorRef: PdfObject;
9
+ readonly fontFileRef: PdfObject;
10
+ readonly toUnicodeRef: PdfObject;
11
+ }
12
+ interface EmbeddedFontObjects {
13
+ readonly baseFont: string;
14
+ readonly type0: PdfDict;
15
+ readonly cidFont: PdfDict;
16
+ readonly descriptor: PdfDict;
17
+ readonly fontFile: PdfObject;
18
+ readonly toUnicode: PdfObject;
19
+ }
20
+ declare function buildEmbeddedFontObjects(face: EmbeddedFace, subset: SfntSubsetResult, usedGlyphs: ReadonlyMap<number, number>, refs: EmbeddedFontObjectRefs, compress: boolean): EmbeddedFontObjects;
21
+ //#endregion
22
+ export { EmbeddedFontObjectRefs, EmbeddedFontObjects, buildEmbeddedFontObjects, embeddedSubsetTag };
@@ -0,0 +1,22 @@
1
+ import { PdfDict, PdfObject } from "./objects.js";
2
+ import { t as EmbeddedFace } from "./embedded-font-CbqkKgBl.js";
3
+ import { SfntSubsetResult } from "./sfnt-subset.js";
4
+ //#region src/embedded-font-write.d.ts
5
+ declare function embeddedSubsetTag(postScriptName: string, glyphIds: readonly number[]): string;
6
+ interface EmbeddedFontObjectRefs {
7
+ readonly cidFontRef: PdfObject;
8
+ readonly descriptorRef: PdfObject;
9
+ readonly fontFileRef: PdfObject;
10
+ readonly toUnicodeRef: PdfObject;
11
+ }
12
+ interface EmbeddedFontObjects {
13
+ readonly baseFont: string;
14
+ readonly type0: PdfDict;
15
+ readonly cidFont: PdfDict;
16
+ readonly descriptor: PdfDict;
17
+ readonly fontFile: PdfObject;
18
+ readonly toUnicode: PdfObject;
19
+ }
20
+ declare function buildEmbeddedFontObjects(face: EmbeddedFace, subset: SfntSubsetResult, usedGlyphs: ReadonlyMap<number, number>, refs: EmbeddedFontObjectRefs, compress: boolean): EmbeddedFontObjects;
21
+ //#endregion
22
+ export { EmbeddedFontObjectRefs, EmbeddedFontObjects, buildEmbeddedFontObjects, embeddedSubsetTag };
@@ -0,0 +1,90 @@
1
+ import { crc32 } from "./bytes/crc32.js";
2
+ import { pdfArray, pdfDict, pdfHexString, pdfName, pdfNum, pdfStream } from "./objects.js";
3
+ import { deflate } from "./bytes/flate.js";
4
+ import { buildToUnicodeCMap } from "./tounicode.js";
5
+ //#region src/embedded-font-write.ts
6
+ const FLAG_SERIF = 2;
7
+ const FLAG_NONSYMBOLIC = 32;
8
+ const FLAG_ITALIC = 64;
9
+ const NOMINAL_STEM_V = 80;
10
+ const DEFAULT_WIDTH = 1e3;
11
+ const SUBSET_TAG_LENGTH = 6;
12
+ const SUBSET_TAG_ALPHABET_SIZE = 26;
13
+ const SUBSET_TAG_CODE_SPACE = SUBSET_TAG_ALPHABET_SIZE ** SUBSET_TAG_LENGTH;
14
+ const UPPERCASE_A_CHAR_CODE = 65;
15
+ function embeddedSubsetTag(postScriptName, glyphIds) {
16
+ let value = crc32(new TextEncoder().encode(`${postScriptName} ${glyphIds.join(",")}`)) % SUBSET_TAG_CODE_SPACE;
17
+ let tag = "";
18
+ for (let i = 0; i < SUBSET_TAG_LENGTH; i++) {
19
+ tag = String.fromCharCode(UPPERCASE_A_CHAR_CODE + value % SUBSET_TAG_ALPHABET_SIZE) + tag;
20
+ value = Math.floor(value / SUBSET_TAG_ALPHABET_SIZE);
21
+ }
22
+ return tag;
23
+ }
24
+ function computeFlags(face) {
25
+ let flags = FLAG_NONSYMBOLIC;
26
+ if (face.metrics.serif) flags |= FLAG_SERIF;
27
+ if (face.metrics.italicAngleDegrees !== 0) flags |= FLAG_ITALIC;
28
+ return flags;
29
+ }
30
+ function buildWidthsArray(face, glyphIds) {
31
+ const entries = [];
32
+ for (const glyphId of glyphIds) entries.push(pdfNum(glyphId), pdfArray([pdfNum(face.glyphSpaceWidth(glyphId))]));
33
+ return pdfArray(entries);
34
+ }
35
+ function buildFontDescriptor(face, baseFont, fontFileRef) {
36
+ const m = face.metrics;
37
+ const entries = /* @__PURE__ */ new Map([
38
+ ["Type", pdfName("FontDescriptor")],
39
+ ["FontName", pdfName(baseFont)],
40
+ ["Flags", pdfNum(computeFlags(face))],
41
+ ["FontBBox", pdfArray(m.bboxGlyphSpace.map((n) => pdfNum(n)))],
42
+ ["ItalicAngle", pdfNum(m.italicAngleDegrees)],
43
+ ["Ascent", pdfNum(m.ascentGlyphSpace)],
44
+ ["Descent", pdfNum(m.descentGlyphSpace)],
45
+ ["CapHeight", pdfNum(m.capHeightGlyphSpace)],
46
+ ["StemV", pdfNum(NOMINAL_STEM_V)]
47
+ ]);
48
+ if (m.xHeightGlyphSpace !== void 0) entries.set("XHeight", pdfNum(m.xHeightGlyphSpace));
49
+ entries.set("FontFile2", fontFileRef);
50
+ return pdfDict(entries);
51
+ }
52
+ function buildFontFileStream(subsetBytes, compress) {
53
+ const entries = /* @__PURE__ */ new Map([["Length1", pdfNum(subsetBytes.length)]]);
54
+ if (compress) entries.set("Filter", pdfName("FlateDecode"));
55
+ return pdfStream(pdfDict(entries), compress ? deflate(subsetBytes) : subsetBytes);
56
+ }
57
+ function buildEmbeddedFontObjects(face, subset, usedGlyphs, refs, compress) {
58
+ const baseFont = `${embeddedSubsetTag(face.postScriptName, subset.glyphIds)}+${face.postScriptName}`;
59
+ const cidFont = pdfDict({
60
+ Type: pdfName("Font"),
61
+ Subtype: pdfName("CIDFontType2"),
62
+ BaseFont: pdfName(baseFont),
63
+ CIDSystemInfo: pdfDict({
64
+ Registry: pdfHexString(new TextEncoder().encode("Adobe")),
65
+ Ordering: pdfHexString(new TextEncoder().encode("Identity")),
66
+ Supplement: pdfNum(0)
67
+ }),
68
+ FontDescriptor: refs.descriptorRef,
69
+ DW: pdfNum(DEFAULT_WIDTH),
70
+ W: buildWidthsArray(face, subset.glyphIds),
71
+ CIDToGIDMap: pdfName("Identity")
72
+ });
73
+ return {
74
+ baseFont,
75
+ type0: pdfDict({
76
+ Type: pdfName("Font"),
77
+ Subtype: pdfName("Type0"),
78
+ BaseFont: pdfName(baseFont),
79
+ Encoding: pdfName("Identity-H"),
80
+ DescendantFonts: pdfArray([refs.cidFontRef]),
81
+ ToUnicode: refs.toUnicodeRef
82
+ }),
83
+ cidFont,
84
+ descriptor: buildFontDescriptor(face, baseFont, refs.fontFileRef),
85
+ fontFile: buildFontFileStream(subset.bytes, compress),
86
+ toUnicode: buildToUnicodeCMap(usedGlyphs)
87
+ };
88
+ }
89
+ //#endregion
90
+ export { buildEmbeddedFontObjects, embeddedSubsetTag };
@@ -0,0 +1,142 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_sfnt = require("./sfnt.cjs");
3
+ const require_cmap_table = require("./cmap-table.cjs");
4
+ const require_font_tables = require("./font-tables.cjs");
5
+ const require_glyf = require("./glyf.cjs");
6
+ const require_hmtx_table = require("./hmtx-table.cjs");
7
+ //#region src/embedded-font.ts
8
+ const GLYPH_SPACE_UNITS_PER_EM = 1e3;
9
+ const NOTDEF_GLYPH_ID = 0;
10
+ const CAP_HEIGHT_REFERENCE_CODE_POINT = 72;
11
+ const PANOSE_FAMILY_KIND_OFFSET = 0;
12
+ const PANOSE_SERIF_STYLE_OFFSET = 1;
13
+ const PANOSE_FAMILY_KIND_LATIN_TEXT = 2;
14
+ const PANOSE_FIRST_SERIF_STYLE = 2;
15
+ const PANOSE_LAST_SERIF_STYLE = 10;
16
+ const cache = /* @__PURE__ */ new WeakMap();
17
+ function scaleToGlyphSpace(designUnits, unitsPerEm) {
18
+ return designUnits * GLYPH_SPACE_UNITS_PER_EM / unitsPerEm;
19
+ }
20
+ function resolveCapHeight(font, cmap, declared, ascent) {
21
+ if (declared !== void 0) return declared;
22
+ const head = require_font_tables.parseHead(font);
23
+ const maxp = require_font_tables.parseMaxp(font);
24
+ const glyphId = cmap(CAP_HEIGHT_REFERENCE_CODE_POINT);
25
+ if (head === void 0 || maxp === void 0 || glyphId === void 0) return ascent;
26
+ return require_glyf.parseGlyf(font, {
27
+ numGlyphs: maxp.numGlyphs,
28
+ indexToLocFormat: head.indexToLocFormat
29
+ })?.glyphHeader(glyphId)?.yMax ?? ascent;
30
+ }
31
+ function isSerifByPanose(panose) {
32
+ if (panose?.[PANOSE_FAMILY_KIND_OFFSET] !== PANOSE_FAMILY_KIND_LATIN_TEXT) return false;
33
+ const serifStyle = panose[PANOSE_SERIF_STYLE_OFFSET];
34
+ return serifStyle !== void 0 && serifStyle >= PANOSE_FIRST_SERIF_STYLE && serifStyle <= PANOSE_LAST_SERIF_STYLE;
35
+ }
36
+ function loadEmbeddedFace(font) {
37
+ const cached = cache.get(font.bytes);
38
+ if (cached !== void 0 || cache.has(font.bytes)) return cached;
39
+ const face = readEmbeddedFace(font);
40
+ cache.set(font.bytes, face);
41
+ return face;
42
+ }
43
+ function readEmbeddedFace(font) {
44
+ const head = require_font_tables.parseHead(font);
45
+ const maxp = require_font_tables.parseMaxp(font);
46
+ const name = require_font_tables.parseName(font);
47
+ const cmap = require_cmap_table.buildCmapLookup(font);
48
+ if (head === void 0 || maxp === void 0 || cmap === void 0 || name?.postScriptName === void 0) return;
49
+ const hhea = parseHhea(font);
50
+ const hmtxBytes = require_sfnt.sfntTableBytes(font, "hmtx");
51
+ if (hhea === void 0 || hmtxBytes === void 0 || !require_sfnt.hasBytes(hmtxBytes, 0, hhea.numberOfHMetrics * LONG_HOR_METRIC_SIZE)) return;
52
+ const hmtx = require_hmtx_table.parseHmtx(font);
53
+ const os2 = require_font_tables.parseOs2(font);
54
+ const post = require_font_tables.parsePost(font);
55
+ const { unitsPerEm } = head;
56
+ const scale = (designUnits) => scaleToGlyphSpace(designUnits, unitsPerEm);
57
+ const capHeight = resolveCapHeight(font, cmap, os2?.sCapHeight, hhea.ascent);
58
+ return {
59
+ font,
60
+ postScriptName: name.postScriptName,
61
+ numGlyphs: maxp.numGlyphs,
62
+ metrics: {
63
+ unitsPerEm,
64
+ ascentGlyphSpace: scale(hhea.ascent),
65
+ descentGlyphSpace: scale(hhea.descent),
66
+ lineGapGlyphSpace: scale(hhea.lineGap),
67
+ typoAscentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoAscender),
68
+ typoDescentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoDescender),
69
+ typoLineGapGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoLineGap),
70
+ winAscentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.usWinAscent),
71
+ winDescentGlyphSpace: os2 === void 0 ? void 0 : -scale(os2.usWinDescent),
72
+ capHeightGlyphSpace: scale(capHeight),
73
+ xHeightGlyphSpace: os2?.sxHeight === void 0 ? void 0 : scale(os2.sxHeight),
74
+ bboxGlyphSpace: [
75
+ scale(head.xMin),
76
+ scale(head.yMin),
77
+ scale(head.xMax),
78
+ scale(head.yMax)
79
+ ],
80
+ italicAngleDegrees: post?.italicAngle ?? 0,
81
+ underlinePositionGlyphSpace: scale(post?.underlinePosition ?? 0),
82
+ underlineThicknessGlyphSpace: scale(post?.underlineThickness ?? 0),
83
+ serif: isSerifByPanose(os2?.panose)
84
+ },
85
+ glyphId: (codePoint) => cmap(codePoint),
86
+ glyphSpaceWidth: (glyphId) => scale(hmtx.advanceWidth(glyphId))
87
+ };
88
+ }
89
+ const HHEA_TABLE_SIZE = 36;
90
+ const HHEA_ASCENDER_OFFSET = 4;
91
+ const HHEA_DESCENDER_OFFSET = 6;
92
+ const HHEA_LINE_GAP_OFFSET = 8;
93
+ const HHEA_NUMBER_OF_HMETRICS_OFFSET = 34;
94
+ const LONG_HOR_METRIC_SIZE = 4;
95
+ function parseHhea(font) {
96
+ const bytes = require_sfnt.sfntTableBytes(font, "hhea");
97
+ if (bytes === void 0 || !require_sfnt.hasBytes(bytes, 0, HHEA_TABLE_SIZE)) return;
98
+ const numberOfHMetrics = require_sfnt.u16(bytes, HHEA_NUMBER_OF_HMETRICS_OFFSET);
99
+ if (numberOfHMetrics === 0) return;
100
+ return {
101
+ ascent: require_sfnt.i16(bytes, HHEA_ASCENDER_OFFSET),
102
+ descent: require_sfnt.i16(bytes, HHEA_DESCENDER_OFFSET),
103
+ lineGap: require_sfnt.i16(bytes, HHEA_LINE_GAP_OFFSET),
104
+ numberOfHMetrics
105
+ };
106
+ }
107
+ function encodeForShowEmbedded(text, face) {
108
+ const glyphIds = [];
109
+ const substitutions = [];
110
+ let width1000 = 0;
111
+ for (const character of text) {
112
+ const glyphId = face.glyphId(character.codePointAt(0));
113
+ if (glyphId === void 0) substitutions.push({ from: character });
114
+ const shown = glyphId ?? 0;
115
+ glyphIds.push(shown);
116
+ width1000 += face.glyphSpaceWidth(shown);
117
+ }
118
+ const codes = new Uint8Array(glyphIds.length * 2);
119
+ glyphIds.forEach((glyphId, index) => {
120
+ codes[index * 2] = glyphId >> 8 & 255;
121
+ codes[index * 2 + 1] = glyphId & 255;
122
+ });
123
+ return {
124
+ codes,
125
+ width1000,
126
+ substitutions
127
+ };
128
+ }
129
+ function collectEmbeddedGlyphs(texts, face) {
130
+ const used = /* @__PURE__ */ new Map();
131
+ for (const text of texts) for (const character of text) {
132
+ const codePoint = character.codePointAt(0);
133
+ const glyphId = face.glyphId(codePoint);
134
+ if (glyphId !== void 0 && !used.has(glyphId)) used.set(glyphId, codePoint);
135
+ }
136
+ return used;
137
+ }
138
+ //#endregion
139
+ exports.NOTDEF_GLYPH_ID = NOTDEF_GLYPH_ID;
140
+ exports.collectEmbeddedGlyphs = collectEmbeddedGlyphs;
141
+ exports.encodeForShowEmbedded = encodeForShowEmbedded;
142
+ exports.loadEmbeddedFace = loadEmbeddedFace;
@@ -0,0 +1,2 @@
1
+ import { a as NOTDEF_GLYPH_ID, c as loadEmbeddedFace, i as EmbeddedShow, n as EmbeddedFaceMetrics, o as collectEmbeddedGlyphs, r as EmbeddedFaceSubstitution, s as encodeForShowEmbedded, t as EmbeddedFace } from "./embedded-font-CLedn-mT.cjs";
2
+ export { EmbeddedFace, EmbeddedFaceMetrics, EmbeddedFaceSubstitution, EmbeddedShow, NOTDEF_GLYPH_ID, collectEmbeddedGlyphs, encodeForShowEmbedded, loadEmbeddedFace };
@@ -0,0 +1,2 @@
1
+ import { a as NOTDEF_GLYPH_ID, c as loadEmbeddedFace, i as EmbeddedShow, n as EmbeddedFaceMetrics, o as collectEmbeddedGlyphs, r as EmbeddedFaceSubstitution, s as encodeForShowEmbedded, t as EmbeddedFace } from "./embedded-font-CbqkKgBl.js";
2
+ export { EmbeddedFace, EmbeddedFaceMetrics, EmbeddedFaceSubstitution, EmbeddedShow, NOTDEF_GLYPH_ID, collectEmbeddedGlyphs, encodeForShowEmbedded, loadEmbeddedFace };
@@ -0,0 +1,138 @@
1
+ import { hasBytes, i16, sfntTableBytes, u16 } from "./sfnt.js";
2
+ import { buildCmapLookup } from "./cmap-table.js";
3
+ import { parseHead, parseMaxp, parseName, parseOs2, parsePost } from "./font-tables.js";
4
+ import { parseGlyf } from "./glyf.js";
5
+ import { parseHmtx } from "./hmtx-table.js";
6
+ //#region src/embedded-font.ts
7
+ const GLYPH_SPACE_UNITS_PER_EM = 1e3;
8
+ const NOTDEF_GLYPH_ID = 0;
9
+ const CAP_HEIGHT_REFERENCE_CODE_POINT = 72;
10
+ const PANOSE_FAMILY_KIND_OFFSET = 0;
11
+ const PANOSE_SERIF_STYLE_OFFSET = 1;
12
+ const PANOSE_FAMILY_KIND_LATIN_TEXT = 2;
13
+ const PANOSE_FIRST_SERIF_STYLE = 2;
14
+ const PANOSE_LAST_SERIF_STYLE = 10;
15
+ const cache = /* @__PURE__ */ new WeakMap();
16
+ function scaleToGlyphSpace(designUnits, unitsPerEm) {
17
+ return designUnits * GLYPH_SPACE_UNITS_PER_EM / unitsPerEm;
18
+ }
19
+ function resolveCapHeight(font, cmap, declared, ascent) {
20
+ if (declared !== void 0) return declared;
21
+ const head = parseHead(font);
22
+ const maxp = parseMaxp(font);
23
+ const glyphId = cmap(CAP_HEIGHT_REFERENCE_CODE_POINT);
24
+ if (head === void 0 || maxp === void 0 || glyphId === void 0) return ascent;
25
+ return parseGlyf(font, {
26
+ numGlyphs: maxp.numGlyphs,
27
+ indexToLocFormat: head.indexToLocFormat
28
+ })?.glyphHeader(glyphId)?.yMax ?? ascent;
29
+ }
30
+ function isSerifByPanose(panose) {
31
+ if (panose?.[PANOSE_FAMILY_KIND_OFFSET] !== PANOSE_FAMILY_KIND_LATIN_TEXT) return false;
32
+ const serifStyle = panose[PANOSE_SERIF_STYLE_OFFSET];
33
+ return serifStyle !== void 0 && serifStyle >= PANOSE_FIRST_SERIF_STYLE && serifStyle <= PANOSE_LAST_SERIF_STYLE;
34
+ }
35
+ function loadEmbeddedFace(font) {
36
+ const cached = cache.get(font.bytes);
37
+ if (cached !== void 0 || cache.has(font.bytes)) return cached;
38
+ const face = readEmbeddedFace(font);
39
+ cache.set(font.bytes, face);
40
+ return face;
41
+ }
42
+ function readEmbeddedFace(font) {
43
+ const head = parseHead(font);
44
+ const maxp = parseMaxp(font);
45
+ const name = parseName(font);
46
+ const cmap = buildCmapLookup(font);
47
+ if (head === void 0 || maxp === void 0 || cmap === void 0 || name?.postScriptName === void 0) return;
48
+ const hhea = parseHhea(font);
49
+ const hmtxBytes = sfntTableBytes(font, "hmtx");
50
+ if (hhea === void 0 || hmtxBytes === void 0 || !hasBytes(hmtxBytes, 0, hhea.numberOfHMetrics * LONG_HOR_METRIC_SIZE)) return;
51
+ const hmtx = parseHmtx(font);
52
+ const os2 = parseOs2(font);
53
+ const post = parsePost(font);
54
+ const { unitsPerEm } = head;
55
+ const scale = (designUnits) => scaleToGlyphSpace(designUnits, unitsPerEm);
56
+ const capHeight = resolveCapHeight(font, cmap, os2?.sCapHeight, hhea.ascent);
57
+ return {
58
+ font,
59
+ postScriptName: name.postScriptName,
60
+ numGlyphs: maxp.numGlyphs,
61
+ metrics: {
62
+ unitsPerEm,
63
+ ascentGlyphSpace: scale(hhea.ascent),
64
+ descentGlyphSpace: scale(hhea.descent),
65
+ lineGapGlyphSpace: scale(hhea.lineGap),
66
+ typoAscentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoAscender),
67
+ typoDescentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoDescender),
68
+ typoLineGapGlyphSpace: os2 === void 0 ? void 0 : scale(os2.sTypoLineGap),
69
+ winAscentGlyphSpace: os2 === void 0 ? void 0 : scale(os2.usWinAscent),
70
+ winDescentGlyphSpace: os2 === void 0 ? void 0 : -scale(os2.usWinDescent),
71
+ capHeightGlyphSpace: scale(capHeight),
72
+ xHeightGlyphSpace: os2?.sxHeight === void 0 ? void 0 : scale(os2.sxHeight),
73
+ bboxGlyphSpace: [
74
+ scale(head.xMin),
75
+ scale(head.yMin),
76
+ scale(head.xMax),
77
+ scale(head.yMax)
78
+ ],
79
+ italicAngleDegrees: post?.italicAngle ?? 0,
80
+ underlinePositionGlyphSpace: scale(post?.underlinePosition ?? 0),
81
+ underlineThicknessGlyphSpace: scale(post?.underlineThickness ?? 0),
82
+ serif: isSerifByPanose(os2?.panose)
83
+ },
84
+ glyphId: (codePoint) => cmap(codePoint),
85
+ glyphSpaceWidth: (glyphId) => scale(hmtx.advanceWidth(glyphId))
86
+ };
87
+ }
88
+ const HHEA_TABLE_SIZE = 36;
89
+ const HHEA_ASCENDER_OFFSET = 4;
90
+ const HHEA_DESCENDER_OFFSET = 6;
91
+ const HHEA_LINE_GAP_OFFSET = 8;
92
+ const HHEA_NUMBER_OF_HMETRICS_OFFSET = 34;
93
+ const LONG_HOR_METRIC_SIZE = 4;
94
+ function parseHhea(font) {
95
+ const bytes = sfntTableBytes(font, "hhea");
96
+ if (bytes === void 0 || !hasBytes(bytes, 0, HHEA_TABLE_SIZE)) return;
97
+ const numberOfHMetrics = u16(bytes, HHEA_NUMBER_OF_HMETRICS_OFFSET);
98
+ if (numberOfHMetrics === 0) return;
99
+ return {
100
+ ascent: i16(bytes, HHEA_ASCENDER_OFFSET),
101
+ descent: i16(bytes, HHEA_DESCENDER_OFFSET),
102
+ lineGap: i16(bytes, HHEA_LINE_GAP_OFFSET),
103
+ numberOfHMetrics
104
+ };
105
+ }
106
+ function encodeForShowEmbedded(text, face) {
107
+ const glyphIds = [];
108
+ const substitutions = [];
109
+ let width1000 = 0;
110
+ for (const character of text) {
111
+ const glyphId = face.glyphId(character.codePointAt(0));
112
+ if (glyphId === void 0) substitutions.push({ from: character });
113
+ const shown = glyphId ?? 0;
114
+ glyphIds.push(shown);
115
+ width1000 += face.glyphSpaceWidth(shown);
116
+ }
117
+ const codes = new Uint8Array(glyphIds.length * 2);
118
+ glyphIds.forEach((glyphId, index) => {
119
+ codes[index * 2] = glyphId >> 8 & 255;
120
+ codes[index * 2 + 1] = glyphId & 255;
121
+ });
122
+ return {
123
+ codes,
124
+ width1000,
125
+ substitutions
126
+ };
127
+ }
128
+ function collectEmbeddedGlyphs(texts, face) {
129
+ const used = /* @__PURE__ */ new Map();
130
+ for (const text of texts) for (const character of text) {
131
+ const codePoint = character.codePointAt(0);
132
+ const glyphId = face.glyphId(codePoint);
133
+ if (glyphId !== void 0 && !used.has(glyphId)) used.set(glyphId, codePoint);
134
+ }
135
+ return used;
136
+ }
137
+ //#endregion
138
+ export { NOTDEF_GLYPH_ID, collectEmbeddedGlyphs, encodeForShowEmbedded, loadEmbeddedFace };
package/dist/filters.cjs CHANGED
@@ -3,9 +3,10 @@ const require_bytes_reader = require("./bytes/reader.cjs");
3
3
  const require_objects = require("./objects.cjs");
4
4
  const require_bytes_flate = require("./bytes/flate.cjs");
5
5
  const require_image_ccitt = require("./image/ccitt.cjs");
6
+ const require_image_jbig2 = require("./image/jbig2.cjs");
6
7
  const require_predictors = require("./predictors.cjs");
7
8
  //#region src/filters.ts
8
- function decodeStream(raw, dict, sink) {
9
+ function decodeStream(raw, dict, sink, resolve) {
9
10
  const filters = filterNames(dict);
10
11
  const parms = decodeParmsList(dict, filters.length);
11
12
  let bytes = raw;
@@ -20,10 +21,21 @@ function decodeStream(raw, dict, sink) {
20
21
  else if (filter === "ASCIIHexDecode" || filter === "AHx") bytes = asciiHexDecode(bytes);
21
22
  else if (filter === "RunLengthDecode" || filter === "RL") bytes = runLengthDecode(bytes);
22
23
  else if (filter === "CCITTFaxDecode" || filter === "CCF") bytes = ccittFaxDecode(bytes, parm, dict, sink);
23
- else if (filter === "DCTDecode" || filter === "DCT") return {
24
+ else if (filter === "JBIG2Decode") {
25
+ const decoded = jbig2Decode(bytes, parm, dict, sink, resolve);
26
+ if (decoded === void 0) return {
27
+ bytes,
28
+ remainingFilter: "JBIG2Decode"
29
+ };
30
+ bytes = decoded;
31
+ } else if (filter === "DCTDecode" || filter === "DCT") return {
24
32
  bytes,
25
33
  remainingFilter: "DCTDecode"
26
34
  };
35
+ else if (filter === "JPXDecode") return {
36
+ bytes,
37
+ remainingFilter: "JPXDecode"
38
+ };
27
39
  else {
28
40
  sink({
29
41
  code: "pdf/unsupported-filter",
@@ -57,6 +69,37 @@ function ccittFaxDecode(data, parm, dict, sink) {
57
69
  })
58
70
  }).bytes;
59
71
  }
72
+ function jbig2Decode(data, parm, dict, sink, resolve) {
73
+ const globalsObj = parm !== void 0 ? require_objects.dictGet(parm, "JBIG2Globals") : void 0;
74
+ const resolvedGlobals = resolve !== void 0 ? resolve(globalsObj) : globalsObj;
75
+ let globals;
76
+ if (resolvedGlobals?.kind === "stream") globals = decodeStream(resolvedGlobals.raw, resolvedGlobals.dict, sink, resolve).bytes;
77
+ else if (globalsObj !== void 0) sink({
78
+ code: "pdf/jbig2-degraded",
79
+ severity: "warning",
80
+ message: "a JBIG2Decode stream declares /JBIG2Globals but it could not be resolved to a stream; decoding without it, which will fail if the page refers to a shared symbol dictionary"
81
+ });
82
+ try {
83
+ const image = require_image_jbig2.decodeJbig2Embedded(data, {
84
+ globals,
85
+ width: require_objects.asNumber(require_objects.dictGet(dict, "Width") ?? require_objects.dictGet(dict, "W")),
86
+ height: require_objects.asNumber(require_objects.dictGet(dict, "Height") ?? require_objects.dictGet(dict, "H")),
87
+ onWarning: (message) => sink({
88
+ code: "pdf/jbig2-degraded",
89
+ severity: "warning",
90
+ message
91
+ })
92
+ });
93
+ return Uint8Array.from(image.bytes, (byte) => byte ^ 255);
94
+ } catch (error) {
95
+ sink({
96
+ code: "pdf/jbig2-undecodable",
97
+ severity: "warning",
98
+ message: `JBIG2Decode stream could not be decoded (${error instanceof Error ? error.message : String(error)}); leaving its bytes undecoded`
99
+ });
100
+ return;
101
+ }
102
+ }
60
103
  function filterNames(dict) {
61
104
  const filterObj = require_objects.dictGet(dict, "Filter") ?? require_objects.dictGet(dict, "F");
62
105
  if (filterObj === void 0) return [];