pdf-codec 1.5.0 → 1.6.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 (118) hide show
  1. package/README.md +36 -3
  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-probe.cjs +133 -0
  35. package/dist/cff-probe.d.cts +10 -0
  36. package/dist/cff-probe.d.ts +10 -0
  37. package/dist/cff-probe.js +132 -0
  38. package/dist/cmap-table.cjs +108 -0
  39. package/dist/cmap-table.d.cts +6 -0
  40. package/dist/cmap-table.d.ts +6 -0
  41. package/dist/cmap-table.js +107 -0
  42. package/dist/content-write.cjs +34 -14
  43. package/dist/content-write.d.cts +9 -2
  44. package/dist/content-write.d.ts +9 -2
  45. package/dist/content-write.js +34 -14
  46. package/dist/embedded-font-CLedn-mT.d.cts +42 -0
  47. package/dist/embedded-font-CbqkKgBl.d.ts +42 -0
  48. package/dist/embedded-font-write.cjs +92 -0
  49. package/dist/embedded-font-write.d.cts +22 -0
  50. package/dist/embedded-font-write.d.ts +22 -0
  51. package/dist/embedded-font-write.js +90 -0
  52. package/dist/embedded-font.cjs +142 -0
  53. package/dist/embedded-font.d.cts +2 -0
  54. package/dist/embedded-font.d.ts +2 -0
  55. package/dist/embedded-font.js +138 -0
  56. package/dist/font-registry-Cn7P9lAh.d.ts +38 -0
  57. package/dist/font-registry-yby02snZ.d.cts +38 -0
  58. package/dist/font-registry.cjs +152 -0
  59. package/dist/font-registry.d.cts +2 -0
  60. package/dist/font-registry.d.ts +2 -0
  61. package/dist/font-registry.js +150 -0
  62. package/dist/font-substitutes.cjs +13 -0
  63. package/dist/font-substitutes.d.cts +5 -0
  64. package/dist/font-substitutes.d.ts +5 -0
  65. package/dist/font-substitutes.js +12 -0
  66. package/dist/font-tables.cjs +127 -0
  67. package/dist/font-tables.d.cts +43 -0
  68. package/dist/font-tables.d.ts +43 -0
  69. package/dist/font-tables.js +122 -0
  70. package/dist/fonts.cjs +1 -0
  71. package/dist/fonts.d.cts +2 -1
  72. package/dist/fonts.d.ts +2 -1
  73. package/dist/fonts.js +1 -1
  74. package/dist/glyf.cjs +127 -0
  75. package/dist/glyf.d.cts +31 -0
  76. package/dist/glyf.d.ts +31 -0
  77. package/dist/glyf.js +125 -0
  78. package/dist/{math-hmtx.cjs → hmtx-table.cjs} +3 -3
  79. package/dist/{math-hmtx.d.ts → hmtx-table.d.cts} +2 -2
  80. package/dist/{math-hmtx.d.cts → hmtx-table.d.ts} +2 -2
  81. package/dist/{math-hmtx.js → hmtx-table.js} +3 -3
  82. package/dist/index.cjs +5 -0
  83. package/dist/index.d.cts +4 -2
  84. package/dist/index.d.ts +4 -2
  85. package/dist/index.js +3 -2
  86. package/dist/math-font-write.cjs +2 -31
  87. package/dist/math-font-write.js +1 -30
  88. package/dist/math-font.cjs +7 -5
  89. package/dist/math-font.js +5 -3
  90. package/dist/math-table.d.cts +1 -1
  91. package/dist/math-table.d.ts +1 -1
  92. package/dist/measure.cjs +65 -14
  93. package/dist/measure.d.cts +8 -1
  94. package/dist/measure.d.ts +8 -1
  95. package/dist/measure.js +64 -15
  96. package/dist/sfnt-V08bb5ut.d.cts +21 -0
  97. package/dist/sfnt-V08bb5ut.d.ts +21 -0
  98. package/dist/sfnt-subset.cjs +250 -0
  99. package/dist/sfnt-subset.d.cts +11 -0
  100. package/dist/sfnt-subset.d.ts +11 -0
  101. package/dist/sfnt-subset.js +249 -0
  102. package/dist/sfnt.cjs +50 -4
  103. package/dist/sfnt.d.cts +2 -17
  104. package/dist/sfnt.d.ts +2 -17
  105. package/dist/sfnt.js +47 -5
  106. package/dist/tounicode.cjs +34 -0
  107. package/dist/tounicode.d.cts +5 -0
  108. package/dist/tounicode.d.ts +5 -0
  109. package/dist/tounicode.js +33 -0
  110. package/dist/write.cjs +80 -10
  111. package/dist/write.d.cts +6 -0
  112. package/dist/write.d.ts +6 -0
  113. package/dist/write.js +81 -11
  114. package/package.json +1 -1
  115. package/dist/math-cmap.cjs +0 -81
  116. package/dist/math-cmap.d.cts +0 -6
  117. package/dist/math-cmap.d.ts +0 -6
  118. package/dist/math-cmap.js +0 -80
@@ -2,8 +2,11 @@ import { o as encodeForShow } from "./afm-widths-Dxucrg7D.js";
2
2
  import { ByteWriter } from "./bytes/writer.js";
3
3
  import { pdfHexString } from "./objects.js";
4
4
  import { BEZIER_KAPPA, multiplyMatrices, rotationMatrix, scaleMatrix, translationMatrix } from "./matrix.js";
5
+ import { encodeForShowEmbedded } from "./embedded-font.js";
5
6
  import { formatNumber, writeObject } from "./serialize.js";
6
7
  //#region src/content-write.ts
8
+ const GLYPH_SPACE_UNITS_PER_EM = 1e3;
9
+ const EMBEDDED_HORIZONTAL_SCALE_PERCENT = 100;
7
10
  function writeRgbOperator(writer, color, operator) {
8
11
  writer.writeAscii(`${formatNumber(color.r)} ${formatNumber(color.g)} ${formatNumber(color.b)} ${operator}\n`);
9
12
  }
@@ -21,8 +24,7 @@ function anchorMatrix(xPt, yPt, rotationDeg) {
21
24
  yPt
22
25
  ];
23
26
  }
24
- function writeUnderline(writer, item, widthPt, measurer) {
25
- const underline = measurer.underlineAtSize(item.font, item.sizePt);
27
+ function writeUnderline(writer, item, widthPt, underline) {
26
28
  writer.writeAscii("q\n");
27
29
  writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "cm");
28
30
  writeRgbOperator(writer, item.color, "rg");
@@ -30,23 +32,39 @@ function writeUnderline(writer, item, widthPt, measurer) {
30
32
  writer.writeAscii("f\n");
31
33
  writer.writeAscii("Q\n");
32
34
  }
33
- function writeText(writer, item, context, substitutions) {
34
- const font = context.resolveFont(item.font);
35
- const encoded = encodeForShow(item.text, font.standardName);
36
- substitutions.push(...encoded.substitutions);
37
- const scalePercent = context.measurer.horizontalScaleFor(item.font) * 100;
35
+ function writeShowTextBlock(writer, item, resourceName, scalePercent, codes) {
38
36
  writer.writeAscii("BT\n");
39
- writer.writeAscii(`/${font.resourceName} ${formatNumber(item.sizePt)} Tf\n`);
37
+ writer.writeAscii(`/${resourceName} ${formatNumber(item.sizePt)} Tf\n`);
40
38
  writer.writeAscii(`${formatNumber(scalePercent)} Tz\n`);
41
39
  writeRgbOperator(writer, item.color, "rg");
42
40
  writeMatrixOperator(writer, anchorMatrix(item.xPt, item.yPt, item.rotationDeg), "Tm");
43
- writeObject(writer, pdfHexString(encoded.codes));
41
+ writeObject(writer, pdfHexString(codes));
44
42
  writer.writeAscii(" Tj\n");
45
43
  writer.writeAscii("ET\n");
46
- if (item.underline === true) {
47
- const scale = context.measurer.horizontalScaleFor(item.font);
48
- writeUnderline(writer, item, item.widthPt ?? encoded.width1000 / 1e3 * item.sizePt * scale, context.measurer);
44
+ }
45
+ function writeStandardText(writer, item, standardName, resourceName, measurer, substitutions) {
46
+ const encoded = encodeForShow(item.text, standardName);
47
+ substitutions.push(...encoded.substitutions);
48
+ const scale = measurer.horizontalScaleFor(item.font);
49
+ writeShowTextBlock(writer, item, resourceName, scale * 100, encoded.codes);
50
+ if (item.underline === true) writeUnderline(writer, item, item.widthPt ?? encoded.width1000 / GLYPH_SPACE_UNITS_PER_EM * item.sizePt * scale, measurer.underlineAtSize(item.font, item.sizePt));
51
+ }
52
+ function writeEmbeddedText(writer, item, face, resourceName, missingGlyphs) {
53
+ const encoded = encodeForShowEmbedded(item.text, face);
54
+ missingGlyphs.push(...encoded.substitutions);
55
+ writeShowTextBlock(writer, item, resourceName, EMBEDDED_HORIZONTAL_SCALE_PERCENT, encoded.codes);
56
+ if (item.underline === true) writeUnderline(writer, item, item.widthPt ?? encoded.width1000 / GLYPH_SPACE_UNITS_PER_EM * item.sizePt, {
57
+ offsetPt: face.metrics.underlinePositionGlyphSpace / GLYPH_SPACE_UNITS_PER_EM * item.sizePt,
58
+ thicknessPt: face.metrics.underlineThicknessGlyphSpace / GLYPH_SPACE_UNITS_PER_EM * item.sizePt
59
+ });
60
+ }
61
+ function writeText(writer, item, context, substitutions, missingGlyphs) {
62
+ const font = context.resolveFont(item.font);
63
+ if (font.kind === "embedded") {
64
+ writeEmbeddedText(writer, item, font.face, font.resourceName, missingGlyphs);
65
+ return;
49
66
  }
67
+ writeStandardText(writer, item, font.standardName, font.resourceName, context.measurer, substitutions);
50
68
  }
51
69
  function paintOperatorFor(fill, stroke, fillRule) {
52
70
  const evenOdd = fillRule === "evenodd";
@@ -121,7 +139,8 @@ function writeImage(writer, item, context) {
121
139
  function writeContentStream(items, context) {
122
140
  const writer = new ByteWriter();
123
141
  const substitutions = [];
124
- for (const item of items) if (item.kind === "text") writeText(writer, item, context, substitutions);
142
+ const missingGlyphs = [];
143
+ for (const item of items) if (item.kind === "text") writeText(writer, item, context, substitutions, missingGlyphs);
125
144
  else if (item.kind === "image") writeImage(writer, item, context);
126
145
  else if (item.kind === "rect") writeRect(writer, item);
127
146
  else if (item.kind === "line") writeLine(writer, item);
@@ -129,7 +148,8 @@ function writeContentStream(items, context) {
129
148
  else if (item.kind === "path") writePath(writer, item);
130
149
  return {
131
150
  bytes: writer.toBytes(),
132
- substitutions
151
+ substitutions,
152
+ missingGlyphs
133
153
  };
134
154
  }
135
155
  //#endregion
@@ -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 };