pdf-codec 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +38 -0
- package/dist/afm-widths-BoeTOK2r.cjs +826 -0
- package/dist/afm-widths-Dxucrg7D.js +779 -0
- package/dist/afm-widths-DyDq56Ph.d.cts +19 -0
- package/dist/afm-widths-DyDq56Ph.d.ts +19 -0
- package/dist/afm-widths.cjs +4 -0
- package/dist/afm-widths.d.cts +2 -0
- package/dist/afm-widths.d.ts +2 -0
- package/dist/afm-widths.js +2 -0
- package/dist/assets/stix-two-math-font.cjs +5 -0
- package/dist/assets/stix-two-math-font.d.cts +4 -0
- package/dist/assets/stix-two-math-font.d.ts +4 -0
- package/dist/assets/stix-two-math-font.js +4 -0
- package/dist/bytes/crc32.cjs +21 -0
- package/dist/bytes/crc32.d.cts +4 -0
- package/dist/bytes/crc32.d.ts +4 -0
- package/dist/bytes/crc32.js +20 -0
- package/dist/bytes/flate.cjs +53 -0
- package/dist/bytes/flate.d.cts +12 -0
- package/dist/bytes/flate.d.ts +12 -0
- package/dist/bytes/flate.js +49 -0
- package/dist/bytes/reader.cjs +60 -0
- package/dist/bytes/reader.d.cts +20 -0
- package/dist/bytes/reader.d.ts +20 -0
- package/dist/bytes/reader.js +58 -0
- package/dist/bytes/writer.cjs +37 -0
- package/dist/bytes/writer.d.cts +13 -0
- package/dist/bytes/writer.d.ts +13 -0
- package/dist/bytes/writer.js +35 -0
- package/dist/cmap.cjs +110 -0
- package/dist/cmap.d.cts +8 -0
- package/dist/cmap.d.ts +8 -0
- package/dist/cmap.js +109 -0
- package/dist/codec.cjs +34 -0
- package/dist/codec.d.cts +167 -0
- package/dist/codec.d.ts +167 -0
- package/dist/codec.js +32 -0
- package/dist/content-read.cjs +126 -0
- package/dist/content-read.d.cts +21 -0
- package/dist/content-read.d.ts +21 -0
- package/dist/content-read.js +125 -0
- package/dist/content-write.cjs +138 -0
- package/dist/content-write.d.cts +24 -0
- package/dist/content-write.d.ts +24 -0
- package/dist/content-write.js +137 -0
- package/dist/diagnostics.cjs +21 -0
- package/dist/diagnostics.d.cts +19 -0
- package/dist/diagnostics.d.ts +19 -0
- package/dist/diagnostics.js +18 -0
- package/dist/document.cjs +162 -0
- package/dist/document.d.cts +12 -0
- package/dist/document.d.ts +12 -0
- package/dist/document.js +161 -0
- package/dist/encoding.cjs +5 -0
- package/dist/encoding.d.cts +6 -0
- package/dist/encoding.d.ts +6 -0
- package/dist/encoding.js +2 -0
- package/dist/filters.cjs +223 -0
- package/dist/filters.d.cts +14 -0
- package/dist/filters.d.ts +14 -0
- package/dist/filters.js +218 -0
- package/dist/font-read.cjs +199 -0
- package/dist/font-read.d.cts +23 -0
- package/dist/font-read.d.ts +23 -0
- package/dist/font-read.js +198 -0
- package/dist/font-style.cjs +35 -0
- package/dist/font-style.d.cts +14 -0
- package/dist/font-style.d.ts +14 -0
- package/dist/font-style.js +34 -0
- package/dist/fonts.cjs +124 -0
- package/dist/fonts.d.cts +15 -0
- package/dist/fonts.d.ts +15 -0
- package/dist/fonts.js +122 -0
- package/dist/formula.cjs +0 -0
- package/dist/formula.d.cts +10 -0
- package/dist/formula.d.ts +10 -0
- package/dist/formula.js +0 -0
- package/dist/image/jpeg-info.cjs +71 -0
- package/dist/image/jpeg-info.d.cts +12 -0
- package/dist/image/jpeg-info.d.ts +12 -0
- package/dist/image/jpeg-info.js +70 -0
- package/dist/image/png-decode.cjs +181 -0
- package/dist/image/png-decode.d.cts +14 -0
- package/dist/image/png-decode.d.ts +14 -0
- package/dist/image/png-decode.js +180 -0
- package/dist/image/png-encode.cjs +64 -0
- package/dist/image/png-encode.d.cts +8 -0
- package/dist/image/png-encode.d.ts +8 -0
- package/dist/image/png-encode.js +63 -0
- package/dist/image/png-filter.cjs +95 -0
- package/dist/image/png-filter.d.cts +6 -0
- package/dist/image/png-filter.d.ts +6 -0
- package/dist/image/png-filter.js +93 -0
- package/dist/images-read.cjs +250 -0
- package/dist/images-read.d.cts +13 -0
- package/dist/images-read.d.ts +13 -0
- package/dist/images-read.js +249 -0
- package/dist/index.cjs +45 -5912
- package/dist/index.d.cts +21 -483
- package/dist/index.d.ts +21 -483
- package/dist/index.js +18 -5885
- package/dist/interpret.cjs +587 -0
- package/dist/interpret.d.cts +84 -0
- package/dist/interpret.d.ts +84 -0
- package/dist/interpret.js +586 -0
- package/dist/lexer.cjs +180 -0
- package/dist/lexer.d.cts +29 -0
- package/dist/lexer.d.ts +29 -0
- package/dist/lexer.js +179 -0
- package/dist/math-cmap.cjs +81 -0
- package/dist/math-cmap.d.cts +6 -0
- package/dist/math-cmap.d.ts +6 -0
- package/dist/math-cmap.js +80 -0
- package/dist/math-content-write.cjs +83 -0
- package/dist/math-content-write.d.cts +11 -0
- package/dist/math-content-write.d.ts +11 -0
- package/dist/math-content-write.js +81 -0
- package/dist/math-font-write.cjs +100 -0
- package/dist/math-font-write.d.cts +19 -0
- package/dist/math-font-write.d.ts +19 -0
- package/dist/math-font-write.js +99 -0
- package/dist/math-font.cjs +103 -0
- package/dist/math-font.d.cts +25 -0
- package/dist/math-font.d.ts +25 -0
- package/dist/math-font.js +102 -0
- package/dist/math-hmtx.cjs +17 -0
- package/dist/math-hmtx.d.cts +8 -0
- package/dist/math-hmtx.d.ts +8 -0
- package/dist/math-hmtx.js +16 -0
- package/dist/math-table.cjs +117 -0
- package/dist/math-table.d.cts +45 -0
- package/dist/math-table.d.ts +45 -0
- package/dist/math-table.js +116 -0
- package/dist/math-types-Ba8qAf1p.d.cts +82 -0
- package/dist/math-types-Ba8qAf1p.d.ts +82 -0
- package/dist/math-types.cjs +0 -0
- package/dist/math-types.d.cts +2 -0
- package/dist/math-types.d.ts +2 -0
- package/dist/math-types.js +0 -0
- package/dist/matrix-B3c2_a2f.d.cts +18 -0
- package/dist/matrix-B3c2_a2f.d.ts +18 -0
- package/dist/matrix.cjs +93 -0
- package/dist/matrix.d.cts +2 -0
- package/dist/matrix.d.ts +2 -0
- package/dist/matrix.js +83 -0
- package/dist/measure.cjs +54 -0
- package/dist/measure.d.cts +20 -0
- package/dist/measure.d.ts +20 -0
- package/dist/measure.js +53 -0
- package/dist/objects.cjs +100 -0
- package/dist/objects.d.cts +58 -0
- package/dist/objects.d.ts +58 -0
- package/dist/objects.js +84 -0
- package/dist/parse.cjs +230 -0
- package/dist/parse.d.cts +13 -0
- package/dist/parse.d.ts +13 -0
- package/dist/parse.js +228 -0
- package/dist/predictors.cjs +64 -0
- package/dist/predictors.d.cts +13 -0
- package/dist/predictors.d.ts +13 -0
- package/dist/predictors.js +62 -0
- package/dist/read.cjs +418 -0
- package/dist/read.d.cts +21 -0
- package/dist/read.d.ts +21 -0
- package/dist/read.js +413 -0
- package/dist/serialize.cjs +69 -0
- package/dist/serialize.d.cts +8 -0
- package/dist/serialize.d.ts +8 -0
- package/dist/serialize.js +66 -0
- package/dist/sfnt.cjs +52 -0
- package/dist/sfnt.d.cts +17 -0
- package/dist/sfnt.d.ts +17 -0
- package/dist/sfnt.js +46 -0
- package/dist/text-layout.cjs +224 -0
- package/dist/text-layout.d.cts +37 -0
- package/dist/text-layout.d.ts +37 -0
- package/dist/text-layout.js +222 -0
- package/dist/util/abort.cjs +7 -0
- package/dist/util/abort.d.cts +4 -0
- package/dist/util/abort.d.ts +4 -0
- package/dist/util/abort.js +6 -0
- package/dist/util/base64.cjs +48 -0
- package/dist/util/base64.d.cts +5 -0
- package/dist/util/base64.d.ts +5 -0
- package/dist/util/base64.js +46 -0
- package/dist/winansi.cjs +5 -0
- package/dist/winansi.d.cts +20 -0
- package/dist/winansi.d.ts +20 -0
- package/dist/winansi.js +2 -0
- package/dist/write.cjs +421 -0
- package/dist/write.d.cts +16 -0
- package/dist/write.d.ts +16 -0
- package/dist/write.js +419 -0
- package/dist/xref.cjs +325 -0
- package/dist/xref.d.cts +21 -0
- package/dist/xref.d.ts +21 -0
- package/dist/xref.js +324 -0
- package/package.json +9 -1
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { PositionedFormula } from "./formula.js";
|
|
2
|
+
import { MathFont } from "./math-font.js";
|
|
3
|
+
//#region src/math-content-write.d.ts
|
|
4
|
+
interface MathContentWriteContext {
|
|
5
|
+
readonly font: MathFont;
|
|
6
|
+
readonly resourceName: string;
|
|
7
|
+
}
|
|
8
|
+
declare function writeFormulaContentStream(formulas: readonly PositionedFormula[], ctx: MathContentWriteContext): Uint8Array<ArrayBuffer>;
|
|
9
|
+
declare function collectUsedGlyphs(formulas: readonly PositionedFormula[], font: MathFont): ReadonlyMap<number, number>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { MathContentWriteContext, collectUsedGlyphs, writeFormulaContentStream };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { ByteWriter } from "./bytes/writer.js";
|
|
2
|
+
import { pdfHexString } from "./objects.js";
|
|
3
|
+
import { formatNumber, writeObject } from "./serialize.js";
|
|
4
|
+
//#region src/math-content-write.ts
|
|
5
|
+
function toPdfX(positioned, itemXPt) {
|
|
6
|
+
return positioned.xPt + itemXPt;
|
|
7
|
+
}
|
|
8
|
+
function toPdfY(positioned, itemYPt) {
|
|
9
|
+
return positioned.yPt + positioned.box.heightPt - itemYPt;
|
|
10
|
+
}
|
|
11
|
+
function writeRgbOperator(writer, color, operator) {
|
|
12
|
+
writer.writeAscii(`${formatNumber(color.r)} ${formatNumber(color.g)} ${formatNumber(color.b)} ${operator}\n`);
|
|
13
|
+
}
|
|
14
|
+
function encodeGlyphRunToCids(font, text) {
|
|
15
|
+
const gids = [];
|
|
16
|
+
for (const ch of text) {
|
|
17
|
+
const codePoint = ch.codePointAt(0);
|
|
18
|
+
if (codePoint === void 0) continue;
|
|
19
|
+
const glyphId = font.glyphId(codePoint);
|
|
20
|
+
if (glyphId !== void 0) gids.push(glyphId);
|
|
21
|
+
}
|
|
22
|
+
const bytes = new Uint8Array(gids.length * 2);
|
|
23
|
+
gids.forEach((gid, index) => {
|
|
24
|
+
bytes[index * 2] = gid >> 8 & 255;
|
|
25
|
+
bytes[index * 2 + 1] = gid & 255;
|
|
26
|
+
});
|
|
27
|
+
return bytes;
|
|
28
|
+
}
|
|
29
|
+
function writeGlyphRun(writer, positioned, item, ctx) {
|
|
30
|
+
const cidBytes = encodeGlyphRunToCids(ctx.font, item.text);
|
|
31
|
+
if (cidBytes.length === 0) return;
|
|
32
|
+
const x = toPdfX(positioned, item.xPt);
|
|
33
|
+
const y = toPdfY(positioned, item.yPt);
|
|
34
|
+
writer.writeAscii("BT\n");
|
|
35
|
+
writer.writeAscii(`/${ctx.resourceName} ${formatNumber(item.sizePt)} Tf\n`);
|
|
36
|
+
writeRgbOperator(writer, item.color, "rg");
|
|
37
|
+
writer.writeAscii(`1 0 0 1 ${formatNumber(x)} ${formatNumber(y)} Tm\n`);
|
|
38
|
+
writeObject(writer, pdfHexString(cidBytes));
|
|
39
|
+
writer.writeAscii(" Tj\n");
|
|
40
|
+
writer.writeAscii("ET\n");
|
|
41
|
+
}
|
|
42
|
+
function writeRule(writer, positioned, item) {
|
|
43
|
+
const x = toPdfX(positioned, item.xPt);
|
|
44
|
+
const topY = toPdfY(positioned, item.yPt);
|
|
45
|
+
writeRgbOperator(writer, item.color, "rg");
|
|
46
|
+
writer.writeAscii(`${formatNumber(x)} ${formatNumber(topY - item.heightPt)} ${formatNumber(item.widthPt)} ${formatNumber(item.heightPt)} re\n`);
|
|
47
|
+
writer.writeAscii("f\n");
|
|
48
|
+
}
|
|
49
|
+
function writeStroke(writer, positioned, item) {
|
|
50
|
+
if (item.points.length < 2) return;
|
|
51
|
+
writeRgbOperator(writer, item.color, "RG");
|
|
52
|
+
writer.writeAscii(`${formatNumber(item.widthPt)} w\n`);
|
|
53
|
+
item.points.forEach((point, index) => {
|
|
54
|
+
const x = toPdfX(positioned, point.xPt);
|
|
55
|
+
const y = toPdfY(positioned, point.yPt);
|
|
56
|
+
writer.writeAscii(`${formatNumber(x)} ${formatNumber(y)} ${index === 0 ? "m" : "l"}\n`);
|
|
57
|
+
});
|
|
58
|
+
writer.writeAscii("S\n");
|
|
59
|
+
}
|
|
60
|
+
function writeFormulaContentStream(formulas, ctx) {
|
|
61
|
+
const writer = new ByteWriter();
|
|
62
|
+
for (const positioned of formulas) for (const item of positioned.box.items) if (item.kind === "glyphs") writeGlyphRun(writer, positioned, item, ctx);
|
|
63
|
+
else if (item.kind === "rule") writeRule(writer, positioned, item);
|
|
64
|
+
else writeStroke(writer, positioned, item);
|
|
65
|
+
return writer.toBytes();
|
|
66
|
+
}
|
|
67
|
+
function collectUsedGlyphs(formulas, font) {
|
|
68
|
+
const used = /* @__PURE__ */ new Map();
|
|
69
|
+
for (const positioned of formulas) for (const item of positioned.box.items) {
|
|
70
|
+
if (item.kind !== "glyphs") continue;
|
|
71
|
+
for (const ch of item.text) {
|
|
72
|
+
const codePoint = ch.codePointAt(0);
|
|
73
|
+
if (codePoint === void 0) continue;
|
|
74
|
+
const glyphId = font.glyphId(codePoint);
|
|
75
|
+
if (glyphId !== void 0 && !used.has(glyphId)) used.set(glyphId, codePoint);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return used;
|
|
79
|
+
}
|
|
80
|
+
//#endregion
|
|
81
|
+
export { collectUsedGlyphs, writeFormulaContentStream };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_bytes_flate = require("./bytes/flate.cjs");
|
|
3
|
+
const require_objects = require("./objects.cjs");
|
|
4
|
+
//#region src/math-font-write.ts
|
|
5
|
+
const FLAG_SYMBOLIC = 4;
|
|
6
|
+
const NOMINAL_STEM_V = 80;
|
|
7
|
+
function buildWidthsArray(font, usedGlyphs) {
|
|
8
|
+
const entries = [];
|
|
9
|
+
for (const glyphId of [...usedGlyphs.keys()].sort((a, b) => a - b)) entries.push(require_objects.pdfNum(glyphId), require_objects.pdfArray([require_objects.pdfNum(font.glyphSpaceWidth(glyphId))]));
|
|
10
|
+
return require_objects.pdfArray(entries);
|
|
11
|
+
}
|
|
12
|
+
function buildFontDescriptor(font, fontFileRef) {
|
|
13
|
+
const d = font.descriptor;
|
|
14
|
+
const scale = 1e3 / d.unitsPerEm;
|
|
15
|
+
return require_objects.pdfDict({
|
|
16
|
+
Type: require_objects.pdfName("FontDescriptor"),
|
|
17
|
+
FontName: require_objects.pdfName("STIXTwoMath-Regular"),
|
|
18
|
+
Flags: require_objects.pdfNum(FLAG_SYMBOLIC),
|
|
19
|
+
FontBBox: require_objects.pdfArray([
|
|
20
|
+
d.bboxMin[0] * scale,
|
|
21
|
+
d.bboxMin[1] * scale,
|
|
22
|
+
d.bboxMax[0] * scale,
|
|
23
|
+
d.bboxMax[1] * scale
|
|
24
|
+
].map((n) => require_objects.pdfNum(n))),
|
|
25
|
+
ItalicAngle: require_objects.pdfNum(d.italicAngle),
|
|
26
|
+
Ascent: require_objects.pdfNum(d.ascent * scale),
|
|
27
|
+
Descent: require_objects.pdfNum(d.descent * scale),
|
|
28
|
+
CapHeight: require_objects.pdfNum(d.capHeight * scale),
|
|
29
|
+
StemV: require_objects.pdfNum(NOMINAL_STEM_V),
|
|
30
|
+
FontFile3: fontFileRef
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
function buildFontFileStream(font, compress) {
|
|
34
|
+
const dict = require_objects.pdfDict(compress ? {
|
|
35
|
+
Subtype: require_objects.pdfName("CIDFontType0C"),
|
|
36
|
+
Filter: require_objects.pdfName("FlateDecode")
|
|
37
|
+
} : { Subtype: require_objects.pdfName("CIDFontType0C") });
|
|
38
|
+
return require_objects.pdfStream(dict, compress ? require_bytes_flate.deflate(font.cffBytes) : font.cffBytes);
|
|
39
|
+
}
|
|
40
|
+
function codePointToUtf16BEHex(codePoint) {
|
|
41
|
+
const text = String.fromCodePoint(codePoint);
|
|
42
|
+
let hex = "";
|
|
43
|
+
for (let i = 0; i < text.length; i++) hex += text.charCodeAt(i).toString(16).padStart(4, "0");
|
|
44
|
+
return hex;
|
|
45
|
+
}
|
|
46
|
+
function buildToUnicodeCMap(usedGlyphs) {
|
|
47
|
+
const sortedGids = [...usedGlyphs.keys()].sort((a, b) => a - b);
|
|
48
|
+
const lines = [
|
|
49
|
+
"/CIDInit /ProcSet findresource begin",
|
|
50
|
+
"12 dict begin",
|
|
51
|
+
"begincmap",
|
|
52
|
+
"/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def",
|
|
53
|
+
"/CMapName /Adobe-Identity-UCS def",
|
|
54
|
+
"/CMapType 2 def",
|
|
55
|
+
"1 begincodespacerange",
|
|
56
|
+
"<0000> <FFFF>",
|
|
57
|
+
"endcodespacerange",
|
|
58
|
+
`${sortedGids.length} beginbfchar`
|
|
59
|
+
];
|
|
60
|
+
for (const gid of sortedGids) {
|
|
61
|
+
const codePoint = usedGlyphs.get(gid);
|
|
62
|
+
if (codePoint === void 0) continue;
|
|
63
|
+
lines.push(`<${gid.toString(16).padStart(4, "0")}> <${codePointToUtf16BEHex(codePoint)}>`);
|
|
64
|
+
}
|
|
65
|
+
lines.push("endbfchar", "endcmap", "CMapName currentdict /CMap defineresource pop", "end", "end");
|
|
66
|
+
const text = `${lines.join("\n")}\n`;
|
|
67
|
+
const bytes = new TextEncoder().encode(text);
|
|
68
|
+
return require_objects.pdfStream(require_objects.pdfDict({}), bytes);
|
|
69
|
+
}
|
|
70
|
+
function buildMathFontObjects(font, usedGlyphs, refs, compress) {
|
|
71
|
+
const cidFont = require_objects.pdfDict({
|
|
72
|
+
Type: require_objects.pdfName("Font"),
|
|
73
|
+
Subtype: require_objects.pdfName("CIDFontType0"),
|
|
74
|
+
BaseFont: require_objects.pdfName("STIXTwoMath-Regular"),
|
|
75
|
+
CIDSystemInfo: require_objects.pdfDict({
|
|
76
|
+
Registry: require_objects.pdfHexString(new TextEncoder().encode("Adobe")),
|
|
77
|
+
Ordering: require_objects.pdfHexString(new TextEncoder().encode("Identity")),
|
|
78
|
+
Supplement: require_objects.pdfNum(0)
|
|
79
|
+
}),
|
|
80
|
+
FontDescriptor: refs.descriptorRef,
|
|
81
|
+
DW: require_objects.pdfNum(0),
|
|
82
|
+
W: buildWidthsArray(font, usedGlyphs)
|
|
83
|
+
});
|
|
84
|
+
return {
|
|
85
|
+
type0: require_objects.pdfDict({
|
|
86
|
+
Type: require_objects.pdfName("Font"),
|
|
87
|
+
Subtype: require_objects.pdfName("Type0"),
|
|
88
|
+
BaseFont: require_objects.pdfName("STIXTwoMath-Regular"),
|
|
89
|
+
Encoding: require_objects.pdfName("Identity-H"),
|
|
90
|
+
DescendantFonts: require_objects.pdfArray([refs.cidFontRef]),
|
|
91
|
+
ToUnicode: refs.toUnicodeRef
|
|
92
|
+
}),
|
|
93
|
+
cidFont,
|
|
94
|
+
descriptor: buildFontDescriptor(font, refs.fontFileRef),
|
|
95
|
+
fontFile: buildFontFileStream(font, compress),
|
|
96
|
+
toUnicode: buildToUnicodeCMap(usedGlyphs)
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
//#endregion
|
|
100
|
+
exports.buildMathFontObjects = buildMathFontObjects;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PdfDict, PdfObject } from "./objects.cjs";
|
|
2
|
+
import { MathFont } from "./math-font.cjs";
|
|
3
|
+
//#region src/math-font-write.d.ts
|
|
4
|
+
interface MathFontObjectRefs {
|
|
5
|
+
readonly cidFontRef: PdfObject;
|
|
6
|
+
readonly descriptorRef: PdfObject;
|
|
7
|
+
readonly fontFileRef: PdfObject;
|
|
8
|
+
readonly toUnicodeRef: PdfObject;
|
|
9
|
+
}
|
|
10
|
+
interface MathFontObjects {
|
|
11
|
+
readonly type0: PdfDict;
|
|
12
|
+
readonly cidFont: PdfDict;
|
|
13
|
+
readonly descriptor: PdfDict;
|
|
14
|
+
readonly fontFile: PdfObject;
|
|
15
|
+
readonly toUnicode: PdfObject;
|
|
16
|
+
}
|
|
17
|
+
declare function buildMathFontObjects(font: MathFont, usedGlyphs: ReadonlyMap<number, number>, refs: MathFontObjectRefs, compress: boolean): MathFontObjects;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { MathFontObjectRefs, MathFontObjects, buildMathFontObjects };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { PdfDict, PdfObject } from "./objects.js";
|
|
2
|
+
import { MathFont } from "./math-font.js";
|
|
3
|
+
//#region src/math-font-write.d.ts
|
|
4
|
+
interface MathFontObjectRefs {
|
|
5
|
+
readonly cidFontRef: PdfObject;
|
|
6
|
+
readonly descriptorRef: PdfObject;
|
|
7
|
+
readonly fontFileRef: PdfObject;
|
|
8
|
+
readonly toUnicodeRef: PdfObject;
|
|
9
|
+
}
|
|
10
|
+
interface MathFontObjects {
|
|
11
|
+
readonly type0: PdfDict;
|
|
12
|
+
readonly cidFont: PdfDict;
|
|
13
|
+
readonly descriptor: PdfDict;
|
|
14
|
+
readonly fontFile: PdfObject;
|
|
15
|
+
readonly toUnicode: PdfObject;
|
|
16
|
+
}
|
|
17
|
+
declare function buildMathFontObjects(font: MathFont, usedGlyphs: ReadonlyMap<number, number>, refs: MathFontObjectRefs, compress: boolean): MathFontObjects;
|
|
18
|
+
//#endregion
|
|
19
|
+
export { MathFontObjectRefs, MathFontObjects, buildMathFontObjects };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { deflate } from "./bytes/flate.js";
|
|
2
|
+
import { pdfArray, pdfDict, pdfHexString, pdfName, pdfNum, pdfStream } from "./objects.js";
|
|
3
|
+
//#region src/math-font-write.ts
|
|
4
|
+
const FLAG_SYMBOLIC = 4;
|
|
5
|
+
const NOMINAL_STEM_V = 80;
|
|
6
|
+
function buildWidthsArray(font, usedGlyphs) {
|
|
7
|
+
const entries = [];
|
|
8
|
+
for (const glyphId of [...usedGlyphs.keys()].sort((a, b) => a - b)) entries.push(pdfNum(glyphId), pdfArray([pdfNum(font.glyphSpaceWidth(glyphId))]));
|
|
9
|
+
return pdfArray(entries);
|
|
10
|
+
}
|
|
11
|
+
function buildFontDescriptor(font, fontFileRef) {
|
|
12
|
+
const d = font.descriptor;
|
|
13
|
+
const scale = 1e3 / d.unitsPerEm;
|
|
14
|
+
return pdfDict({
|
|
15
|
+
Type: pdfName("FontDescriptor"),
|
|
16
|
+
FontName: pdfName("STIXTwoMath-Regular"),
|
|
17
|
+
Flags: pdfNum(FLAG_SYMBOLIC),
|
|
18
|
+
FontBBox: pdfArray([
|
|
19
|
+
d.bboxMin[0] * scale,
|
|
20
|
+
d.bboxMin[1] * scale,
|
|
21
|
+
d.bboxMax[0] * scale,
|
|
22
|
+
d.bboxMax[1] * scale
|
|
23
|
+
].map((n) => pdfNum(n))),
|
|
24
|
+
ItalicAngle: pdfNum(d.italicAngle),
|
|
25
|
+
Ascent: pdfNum(d.ascent * scale),
|
|
26
|
+
Descent: pdfNum(d.descent * scale),
|
|
27
|
+
CapHeight: pdfNum(d.capHeight * scale),
|
|
28
|
+
StemV: pdfNum(NOMINAL_STEM_V),
|
|
29
|
+
FontFile3: fontFileRef
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function buildFontFileStream(font, compress) {
|
|
33
|
+
const dict = pdfDict(compress ? {
|
|
34
|
+
Subtype: pdfName("CIDFontType0C"),
|
|
35
|
+
Filter: pdfName("FlateDecode")
|
|
36
|
+
} : { Subtype: pdfName("CIDFontType0C") });
|
|
37
|
+
return pdfStream(dict, compress ? deflate(font.cffBytes) : font.cffBytes);
|
|
38
|
+
}
|
|
39
|
+
function codePointToUtf16BEHex(codePoint) {
|
|
40
|
+
const text = String.fromCodePoint(codePoint);
|
|
41
|
+
let hex = "";
|
|
42
|
+
for (let i = 0; i < text.length; i++) hex += text.charCodeAt(i).toString(16).padStart(4, "0");
|
|
43
|
+
return hex;
|
|
44
|
+
}
|
|
45
|
+
function buildToUnicodeCMap(usedGlyphs) {
|
|
46
|
+
const sortedGids = [...usedGlyphs.keys()].sort((a, b) => a - b);
|
|
47
|
+
const lines = [
|
|
48
|
+
"/CIDInit /ProcSet findresource begin",
|
|
49
|
+
"12 dict begin",
|
|
50
|
+
"begincmap",
|
|
51
|
+
"/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def",
|
|
52
|
+
"/CMapName /Adobe-Identity-UCS def",
|
|
53
|
+
"/CMapType 2 def",
|
|
54
|
+
"1 begincodespacerange",
|
|
55
|
+
"<0000> <FFFF>",
|
|
56
|
+
"endcodespacerange",
|
|
57
|
+
`${sortedGids.length} beginbfchar`
|
|
58
|
+
];
|
|
59
|
+
for (const gid of sortedGids) {
|
|
60
|
+
const codePoint = usedGlyphs.get(gid);
|
|
61
|
+
if (codePoint === void 0) continue;
|
|
62
|
+
lines.push(`<${gid.toString(16).padStart(4, "0")}> <${codePointToUtf16BEHex(codePoint)}>`);
|
|
63
|
+
}
|
|
64
|
+
lines.push("endbfchar", "endcmap", "CMapName currentdict /CMap defineresource pop", "end", "end");
|
|
65
|
+
const text = `${lines.join("\n")}\n`;
|
|
66
|
+
const bytes = new TextEncoder().encode(text);
|
|
67
|
+
return pdfStream(pdfDict({}), bytes);
|
|
68
|
+
}
|
|
69
|
+
function buildMathFontObjects(font, usedGlyphs, refs, compress) {
|
|
70
|
+
const cidFont = pdfDict({
|
|
71
|
+
Type: pdfName("Font"),
|
|
72
|
+
Subtype: pdfName("CIDFontType0"),
|
|
73
|
+
BaseFont: pdfName("STIXTwoMath-Regular"),
|
|
74
|
+
CIDSystemInfo: pdfDict({
|
|
75
|
+
Registry: pdfHexString(new TextEncoder().encode("Adobe")),
|
|
76
|
+
Ordering: pdfHexString(new TextEncoder().encode("Identity")),
|
|
77
|
+
Supplement: pdfNum(0)
|
|
78
|
+
}),
|
|
79
|
+
FontDescriptor: refs.descriptorRef,
|
|
80
|
+
DW: pdfNum(0),
|
|
81
|
+
W: buildWidthsArray(font, usedGlyphs)
|
|
82
|
+
});
|
|
83
|
+
return {
|
|
84
|
+
type0: pdfDict({
|
|
85
|
+
Type: pdfName("Font"),
|
|
86
|
+
Subtype: pdfName("Type0"),
|
|
87
|
+
BaseFont: pdfName("STIXTwoMath-Regular"),
|
|
88
|
+
Encoding: pdfName("Identity-H"),
|
|
89
|
+
DescendantFonts: pdfArray([refs.cidFontRef]),
|
|
90
|
+
ToUnicode: refs.toUnicodeRef
|
|
91
|
+
}),
|
|
92
|
+
cidFont,
|
|
93
|
+
descriptor: buildFontDescriptor(font, refs.fontFileRef),
|
|
94
|
+
fontFile: buildFontFileStream(font, compress),
|
|
95
|
+
toUnicode: buildToUnicodeCMap(usedGlyphs)
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//#endregion
|
|
99
|
+
export { buildMathFontObjects };
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_util_base64 = require("./util/base64.cjs");
|
|
3
|
+
const require_assets_stix_two_math_font = require("./assets/stix-two-math-font.cjs");
|
|
4
|
+
const require_sfnt = require("./sfnt.cjs");
|
|
5
|
+
const require_math_cmap = require("./math-cmap.cjs");
|
|
6
|
+
const require_math_hmtx = require("./math-hmtx.cjs");
|
|
7
|
+
const require_math_table = require("./math-table.cjs");
|
|
8
|
+
//#region src/math-font.ts
|
|
9
|
+
function toPt(designUnits, unitsPerEm, sizePt) {
|
|
10
|
+
return designUnits / unitsPerEm * sizePt;
|
|
11
|
+
}
|
|
12
|
+
function metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, sizePt) {
|
|
13
|
+
const c = math.constants;
|
|
14
|
+
const pt = (designUnits) => toPt(designUnits, unitsPerEm, sizePt);
|
|
15
|
+
return {
|
|
16
|
+
ascentPerEm: ascentDesignUnits / unitsPerEm,
|
|
17
|
+
descentPerEm: -descentDesignUnits / unitsPerEm,
|
|
18
|
+
axisHeightPt: pt(c.axisHeight),
|
|
19
|
+
fractionRuleThicknessPt: pt(c.fractionRuleThickness),
|
|
20
|
+
fractionNumeratorShiftUpPt: pt(c.fractionNumeratorShiftUp),
|
|
21
|
+
fractionNumeratorDisplayShiftUpPt: pt(c.fractionNumeratorDisplayStyleShiftUp),
|
|
22
|
+
fractionDenominatorShiftDownPt: pt(c.fractionDenominatorShiftDown),
|
|
23
|
+
fractionDenominatorDisplayShiftDownPt: pt(c.fractionDenominatorDisplayStyleShiftDown),
|
|
24
|
+
fractionNumeratorGapMinPt: pt(c.fractionNumeratorGapMin),
|
|
25
|
+
fractionDenominatorGapMinPt: pt(c.fractionDenominatorGapMin),
|
|
26
|
+
radicalRuleThicknessPt: pt(c.radicalRuleThickness),
|
|
27
|
+
radicalExtraAscenderPt: pt(c.radicalExtraAscender),
|
|
28
|
+
radicalVerticalGapPt: pt(c.radicalVerticalGap),
|
|
29
|
+
radicalKernBeforeDegreePt: pt(c.radicalKernBeforeDegree),
|
|
30
|
+
radicalKernAfterDegreePt: pt(c.radicalKernAfterDegree),
|
|
31
|
+
radicalDegreeBottomRaisePercent: c.radicalDegreeBottomRaisePercent,
|
|
32
|
+
subscriptShiftDownPt: pt(c.subscriptShiftDown),
|
|
33
|
+
superscriptShiftUpPt: pt(c.superscriptShiftUp),
|
|
34
|
+
superscriptShiftUpCrampedPt: pt(c.superscriptShiftUpCramped),
|
|
35
|
+
subSuperscriptGapMinPt: pt(c.subSuperscriptGapMin),
|
|
36
|
+
superscriptBaselineDropMaxPt: pt(c.superscriptBaselineDropMax),
|
|
37
|
+
subscriptBaselineDropMinPt: pt(c.subscriptBaselineDropMin),
|
|
38
|
+
spaceAfterScriptPt: pt(c.spaceAfterScript),
|
|
39
|
+
upperLimitGapMinPt: pt(c.upperLimitGapMin),
|
|
40
|
+
upperLimitBaselineRiseMinPt: pt(c.upperLimitBaselineRiseMin),
|
|
41
|
+
lowerLimitGapMinPt: pt(c.lowerLimitGapMin),
|
|
42
|
+
lowerLimitBaselineDropMinPt: pt(c.lowerLimitBaselineDropMin),
|
|
43
|
+
stackTopShiftUpPt: pt(c.stackTopShiftUp),
|
|
44
|
+
stackBottomShiftDownPt: pt(c.stackBottomShiftDown),
|
|
45
|
+
stackGapMinPt: pt(c.stackGapMin),
|
|
46
|
+
scriptPercentScaleDown: c.scriptPercentScaleDown,
|
|
47
|
+
scriptScriptPercentScaleDown: c.scriptScriptPercentScaleDown,
|
|
48
|
+
defaultRuleThicknessPt: pt(c.fractionRuleThickness),
|
|
49
|
+
glyph(codePoint, glyphSizePt) {
|
|
50
|
+
const glyphId = cmap(codePoint);
|
|
51
|
+
if (glyphId === void 0) return;
|
|
52
|
+
const advanceWidthPt = toPt(hmtx.advanceWidth(glyphId), unitsPerEm, glyphSizePt);
|
|
53
|
+
const italicCorrectionDesignUnits = math.glyphInfo.italicsCorrection.get(glyphId) ?? 0;
|
|
54
|
+
const topAccentDesignUnits = math.glyphInfo.topAccentAttachment.get(glyphId);
|
|
55
|
+
return {
|
|
56
|
+
advanceWidthPt,
|
|
57
|
+
italicCorrectionPt: toPt(italicCorrectionDesignUnits, unitsPerEm, glyphSizePt),
|
|
58
|
+
topAccentXPt: topAccentDesignUnits === void 0 ? void 0 : toPt(topAccentDesignUnits, unitsPerEm, glyphSizePt)
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
let cached;
|
|
64
|
+
function loadMathFont() {
|
|
65
|
+
if (cached !== void 0) return cached;
|
|
66
|
+
const bytes = require_util_base64.base64ToBytes(require_assets_stix_two_math_font.STIX_TWO_MATH_FONT_BASE64);
|
|
67
|
+
const sfnt = require_sfnt.parseSfnt(bytes);
|
|
68
|
+
const headBytes = require_sfnt.sfntTableBytes(sfnt, "head");
|
|
69
|
+
const hheaBytes = require_sfnt.sfntTableBytes(sfnt, "hhea");
|
|
70
|
+
const os2Bytes = require_sfnt.sfntTableBytes(sfnt, "OS/2");
|
|
71
|
+
const cffBytes = require_sfnt.sfntTableBytes(sfnt, "CFF ");
|
|
72
|
+
if (headBytes === void 0 || hheaBytes === void 0 || cffBytes === void 0) throw new Error("embedded math font is missing a required sfnt table (head/hhea/CFF )");
|
|
73
|
+
const unitsPerEm = require_sfnt.u16(headBytes, 18);
|
|
74
|
+
const ascentDesignUnits = require_sfnt.i16(hheaBytes, 4);
|
|
75
|
+
const descentDesignUnits = require_sfnt.i16(hheaBytes, 6);
|
|
76
|
+
const bboxMin = [require_sfnt.i16(headBytes, 36), require_sfnt.i16(headBytes, 38)];
|
|
77
|
+
const bboxMax = [require_sfnt.i16(headBytes, 40), require_sfnt.i16(headBytes, 42)];
|
|
78
|
+
const capHeight = os2Bytes === void 0 ? ascentDesignUnits : require_sfnt.i16(os2Bytes, 88);
|
|
79
|
+
const cmap = require_math_cmap.buildCmapLookup(sfnt);
|
|
80
|
+
const hmtx = require_math_hmtx.parseHmtx(sfnt);
|
|
81
|
+
const math = require_math_table.parseMathTable(sfnt);
|
|
82
|
+
cached = {
|
|
83
|
+
font: {
|
|
84
|
+
metrics: metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, unitsPerEm),
|
|
85
|
+
cffBytes,
|
|
86
|
+
descriptor: {
|
|
87
|
+
unitsPerEm,
|
|
88
|
+
ascent: ascentDesignUnits,
|
|
89
|
+
descent: descentDesignUnits,
|
|
90
|
+
capHeight,
|
|
91
|
+
bboxMin,
|
|
92
|
+
bboxMax,
|
|
93
|
+
italicAngle: 0
|
|
94
|
+
},
|
|
95
|
+
glyphId: (codePoint) => cmap(codePoint),
|
|
96
|
+
glyphSpaceWidth: (glyphId) => hmtx.advanceWidth(glyphId) * 1e3 / unitsPerEm
|
|
97
|
+
},
|
|
98
|
+
metricsAt: (sizePt) => metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, sizePt)
|
|
99
|
+
};
|
|
100
|
+
return cached;
|
|
101
|
+
}
|
|
102
|
+
//#endregion
|
|
103
|
+
exports.loadMathFont = loadMathFont;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { r as MathFontMetrics } from "./math-types-Ba8qAf1p.cjs";
|
|
2
|
+
//#region src/math-font.d.ts
|
|
3
|
+
interface MathFontDescriptorMetrics {
|
|
4
|
+
readonly unitsPerEm: number;
|
|
5
|
+
readonly ascent: number;
|
|
6
|
+
readonly descent: number;
|
|
7
|
+
readonly capHeight: number;
|
|
8
|
+
readonly bboxMin: readonly [number, number];
|
|
9
|
+
readonly bboxMax: readonly [number, number];
|
|
10
|
+
readonly italicAngle: number;
|
|
11
|
+
}
|
|
12
|
+
interface MathFont {
|
|
13
|
+
readonly metrics: MathFontMetrics;
|
|
14
|
+
readonly cffBytes: Uint8Array<ArrayBuffer>;
|
|
15
|
+
readonly descriptor: MathFontDescriptorMetrics;
|
|
16
|
+
glyphId(codePoint: number): number | undefined;
|
|
17
|
+
glyphSpaceWidth(glyphId: number): number;
|
|
18
|
+
}
|
|
19
|
+
interface LoadedMathFont {
|
|
20
|
+
readonly font: MathFont;
|
|
21
|
+
readonly metricsAt: (sizePt: number) => MathFontMetrics;
|
|
22
|
+
}
|
|
23
|
+
declare function loadMathFont(): LoadedMathFont;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { LoadedMathFont, MathFont, MathFontDescriptorMetrics, loadMathFont };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { r as MathFontMetrics } from "./math-types-Ba8qAf1p.js";
|
|
2
|
+
//#region src/math-font.d.ts
|
|
3
|
+
interface MathFontDescriptorMetrics {
|
|
4
|
+
readonly unitsPerEm: number;
|
|
5
|
+
readonly ascent: number;
|
|
6
|
+
readonly descent: number;
|
|
7
|
+
readonly capHeight: number;
|
|
8
|
+
readonly bboxMin: readonly [number, number];
|
|
9
|
+
readonly bboxMax: readonly [number, number];
|
|
10
|
+
readonly italicAngle: number;
|
|
11
|
+
}
|
|
12
|
+
interface MathFont {
|
|
13
|
+
readonly metrics: MathFontMetrics;
|
|
14
|
+
readonly cffBytes: Uint8Array<ArrayBuffer>;
|
|
15
|
+
readonly descriptor: MathFontDescriptorMetrics;
|
|
16
|
+
glyphId(codePoint: number): number | undefined;
|
|
17
|
+
glyphSpaceWidth(glyphId: number): number;
|
|
18
|
+
}
|
|
19
|
+
interface LoadedMathFont {
|
|
20
|
+
readonly font: MathFont;
|
|
21
|
+
readonly metricsAt: (sizePt: number) => MathFontMetrics;
|
|
22
|
+
}
|
|
23
|
+
declare function loadMathFont(): LoadedMathFont;
|
|
24
|
+
//#endregion
|
|
25
|
+
export { LoadedMathFont, MathFont, MathFontDescriptorMetrics, loadMathFont };
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { base64ToBytes } from "./util/base64.js";
|
|
2
|
+
import { STIX_TWO_MATH_FONT_BASE64 } from "./assets/stix-two-math-font.js";
|
|
3
|
+
import { i16, parseSfnt, sfntTableBytes, u16 } from "./sfnt.js";
|
|
4
|
+
import { buildCmapLookup } from "./math-cmap.js";
|
|
5
|
+
import { parseHmtx } from "./math-hmtx.js";
|
|
6
|
+
import { parseMathTable } from "./math-table.js";
|
|
7
|
+
//#region src/math-font.ts
|
|
8
|
+
function toPt(designUnits, unitsPerEm, sizePt) {
|
|
9
|
+
return designUnits / unitsPerEm * sizePt;
|
|
10
|
+
}
|
|
11
|
+
function metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, sizePt) {
|
|
12
|
+
const c = math.constants;
|
|
13
|
+
const pt = (designUnits) => toPt(designUnits, unitsPerEm, sizePt);
|
|
14
|
+
return {
|
|
15
|
+
ascentPerEm: ascentDesignUnits / unitsPerEm,
|
|
16
|
+
descentPerEm: -descentDesignUnits / unitsPerEm,
|
|
17
|
+
axisHeightPt: pt(c.axisHeight),
|
|
18
|
+
fractionRuleThicknessPt: pt(c.fractionRuleThickness),
|
|
19
|
+
fractionNumeratorShiftUpPt: pt(c.fractionNumeratorShiftUp),
|
|
20
|
+
fractionNumeratorDisplayShiftUpPt: pt(c.fractionNumeratorDisplayStyleShiftUp),
|
|
21
|
+
fractionDenominatorShiftDownPt: pt(c.fractionDenominatorShiftDown),
|
|
22
|
+
fractionDenominatorDisplayShiftDownPt: pt(c.fractionDenominatorDisplayStyleShiftDown),
|
|
23
|
+
fractionNumeratorGapMinPt: pt(c.fractionNumeratorGapMin),
|
|
24
|
+
fractionDenominatorGapMinPt: pt(c.fractionDenominatorGapMin),
|
|
25
|
+
radicalRuleThicknessPt: pt(c.radicalRuleThickness),
|
|
26
|
+
radicalExtraAscenderPt: pt(c.radicalExtraAscender),
|
|
27
|
+
radicalVerticalGapPt: pt(c.radicalVerticalGap),
|
|
28
|
+
radicalKernBeforeDegreePt: pt(c.radicalKernBeforeDegree),
|
|
29
|
+
radicalKernAfterDegreePt: pt(c.radicalKernAfterDegree),
|
|
30
|
+
radicalDegreeBottomRaisePercent: c.radicalDegreeBottomRaisePercent,
|
|
31
|
+
subscriptShiftDownPt: pt(c.subscriptShiftDown),
|
|
32
|
+
superscriptShiftUpPt: pt(c.superscriptShiftUp),
|
|
33
|
+
superscriptShiftUpCrampedPt: pt(c.superscriptShiftUpCramped),
|
|
34
|
+
subSuperscriptGapMinPt: pt(c.subSuperscriptGapMin),
|
|
35
|
+
superscriptBaselineDropMaxPt: pt(c.superscriptBaselineDropMax),
|
|
36
|
+
subscriptBaselineDropMinPt: pt(c.subscriptBaselineDropMin),
|
|
37
|
+
spaceAfterScriptPt: pt(c.spaceAfterScript),
|
|
38
|
+
upperLimitGapMinPt: pt(c.upperLimitGapMin),
|
|
39
|
+
upperLimitBaselineRiseMinPt: pt(c.upperLimitBaselineRiseMin),
|
|
40
|
+
lowerLimitGapMinPt: pt(c.lowerLimitGapMin),
|
|
41
|
+
lowerLimitBaselineDropMinPt: pt(c.lowerLimitBaselineDropMin),
|
|
42
|
+
stackTopShiftUpPt: pt(c.stackTopShiftUp),
|
|
43
|
+
stackBottomShiftDownPt: pt(c.stackBottomShiftDown),
|
|
44
|
+
stackGapMinPt: pt(c.stackGapMin),
|
|
45
|
+
scriptPercentScaleDown: c.scriptPercentScaleDown,
|
|
46
|
+
scriptScriptPercentScaleDown: c.scriptScriptPercentScaleDown,
|
|
47
|
+
defaultRuleThicknessPt: pt(c.fractionRuleThickness),
|
|
48
|
+
glyph(codePoint, glyphSizePt) {
|
|
49
|
+
const glyphId = cmap(codePoint);
|
|
50
|
+
if (glyphId === void 0) return;
|
|
51
|
+
const advanceWidthPt = toPt(hmtx.advanceWidth(glyphId), unitsPerEm, glyphSizePt);
|
|
52
|
+
const italicCorrectionDesignUnits = math.glyphInfo.italicsCorrection.get(glyphId) ?? 0;
|
|
53
|
+
const topAccentDesignUnits = math.glyphInfo.topAccentAttachment.get(glyphId);
|
|
54
|
+
return {
|
|
55
|
+
advanceWidthPt,
|
|
56
|
+
italicCorrectionPt: toPt(italicCorrectionDesignUnits, unitsPerEm, glyphSizePt),
|
|
57
|
+
topAccentXPt: topAccentDesignUnits === void 0 ? void 0 : toPt(topAccentDesignUnits, unitsPerEm, glyphSizePt)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
let cached;
|
|
63
|
+
function loadMathFont() {
|
|
64
|
+
if (cached !== void 0) return cached;
|
|
65
|
+
const bytes = base64ToBytes(STIX_TWO_MATH_FONT_BASE64);
|
|
66
|
+
const sfnt = parseSfnt(bytes);
|
|
67
|
+
const headBytes = sfntTableBytes(sfnt, "head");
|
|
68
|
+
const hheaBytes = sfntTableBytes(sfnt, "hhea");
|
|
69
|
+
const os2Bytes = sfntTableBytes(sfnt, "OS/2");
|
|
70
|
+
const cffBytes = sfntTableBytes(sfnt, "CFF ");
|
|
71
|
+
if (headBytes === void 0 || hheaBytes === void 0 || cffBytes === void 0) throw new Error("embedded math font is missing a required sfnt table (head/hhea/CFF )");
|
|
72
|
+
const unitsPerEm = u16(headBytes, 18);
|
|
73
|
+
const ascentDesignUnits = i16(hheaBytes, 4);
|
|
74
|
+
const descentDesignUnits = i16(hheaBytes, 6);
|
|
75
|
+
const bboxMin = [i16(headBytes, 36), i16(headBytes, 38)];
|
|
76
|
+
const bboxMax = [i16(headBytes, 40), i16(headBytes, 42)];
|
|
77
|
+
const capHeight = os2Bytes === void 0 ? ascentDesignUnits : i16(os2Bytes, 88);
|
|
78
|
+
const cmap = buildCmapLookup(sfnt);
|
|
79
|
+
const hmtx = parseHmtx(sfnt);
|
|
80
|
+
const math = parseMathTable(sfnt);
|
|
81
|
+
cached = {
|
|
82
|
+
font: {
|
|
83
|
+
metrics: metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, unitsPerEm),
|
|
84
|
+
cffBytes,
|
|
85
|
+
descriptor: {
|
|
86
|
+
unitsPerEm,
|
|
87
|
+
ascent: ascentDesignUnits,
|
|
88
|
+
descent: descentDesignUnits,
|
|
89
|
+
capHeight,
|
|
90
|
+
bboxMin,
|
|
91
|
+
bboxMax,
|
|
92
|
+
italicAngle: 0
|
|
93
|
+
},
|
|
94
|
+
glyphId: (codePoint) => cmap(codePoint),
|
|
95
|
+
glyphSpaceWidth: (glyphId) => hmtx.advanceWidth(glyphId) * 1e3 / unitsPerEm
|
|
96
|
+
},
|
|
97
|
+
metricsAt: (sizePt) => metricsAtSize(cmap, hmtx, math, unitsPerEm, ascentDesignUnits, descentDesignUnits, sizePt)
|
|
98
|
+
};
|
|
99
|
+
return cached;
|
|
100
|
+
}
|
|
101
|
+
//#endregion
|
|
102
|
+
export { loadMathFont };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_sfnt = require("./sfnt.cjs");
|
|
3
|
+
//#region src/math-hmtx.ts
|
|
4
|
+
function parseHmtx(font) {
|
|
5
|
+
const hheaBytes = require_sfnt.sfntTableBytes(font, "hhea");
|
|
6
|
+
const hmtxBytes = require_sfnt.sfntTableBytes(font, "hmtx");
|
|
7
|
+
if (hheaBytes === void 0 || hmtxBytes === void 0) throw new Error("math font has no hhea/hmtx table");
|
|
8
|
+
const numberOfHMetrics = require_sfnt.u16(hheaBytes, 34);
|
|
9
|
+
if (numberOfHMetrics === 0) throw new Error("math font hhea numberOfHMetrics is zero");
|
|
10
|
+
const lastWidth = require_sfnt.u16(hmtxBytes, (numberOfHMetrics - 1) * 4);
|
|
11
|
+
return { advanceWidth(glyphId) {
|
|
12
|
+
if (glyphId < numberOfHMetrics) return require_sfnt.u16(hmtxBytes, glyphId * 4);
|
|
13
|
+
return lastWidth;
|
|
14
|
+
} };
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
exports.parseHmtx = parseHmtx;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { sfntTableBytes, u16 } from "./sfnt.js";
|
|
2
|
+
//#region src/math-hmtx.ts
|
|
3
|
+
function parseHmtx(font) {
|
|
4
|
+
const hheaBytes = sfntTableBytes(font, "hhea");
|
|
5
|
+
const hmtxBytes = sfntTableBytes(font, "hmtx");
|
|
6
|
+
if (hheaBytes === void 0 || hmtxBytes === void 0) throw new Error("math font has no hhea/hmtx table");
|
|
7
|
+
const numberOfHMetrics = u16(hheaBytes, 34);
|
|
8
|
+
if (numberOfHMetrics === 0) throw new Error("math font hhea numberOfHMetrics is zero");
|
|
9
|
+
const lastWidth = u16(hmtxBytes, (numberOfHMetrics - 1) * 4);
|
|
10
|
+
return { advanceWidth(glyphId) {
|
|
11
|
+
if (glyphId < numberOfHMetrics) return u16(hmtxBytes, glyphId * 4);
|
|
12
|
+
return lastWidth;
|
|
13
|
+
} };
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { parseHmtx };
|