@cj-tech-master/excelts 9.4.1 → 9.4.2
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/dist/browser/modules/pdf/builder/document-builder.js +4 -23
- package/dist/browser/modules/pdf/core/pdf-stream.d.ts +15 -0
- package/dist/browser/modules/pdf/core/pdf-stream.js +47 -3
- package/dist/browser/modules/pdf/font/font-manager.d.ts +37 -6
- package/dist/browser/modules/pdf/font/font-manager.js +129 -17
- package/dist/browser/modules/pdf/font/system-fonts.d.ts +41 -0
- package/dist/browser/modules/pdf/font/system-fonts.js +188 -0
- package/dist/browser/modules/pdf/font/ttf-parser.js +29 -1
- package/dist/browser/modules/pdf/font/type3-font.d.ts +35 -0
- package/dist/browser/modules/pdf/font/type3-font.js +228 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-extended.d.ts +33 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-extended.js +4164 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-extended2.d.ts +16 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-extended2.js +9649 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-fill.d.ts +17 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-fill.js +5438 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-quality.d.ts +28 -0
- package/dist/browser/modules/pdf/font/type3-glyphs-quality.js +5345 -0
- package/dist/browser/modules/pdf/font/type3-glyphs.d.ts +79 -0
- package/dist/browser/modules/pdf/font/type3-glyphs.js +2567 -0
- package/dist/browser/modules/pdf/render/layout-engine.js +36 -23
- package/dist/browser/modules/pdf/render/page-renderer.d.ts +9 -0
- package/dist/browser/modules/pdf/render/page-renderer.js +110 -78
- package/dist/browser/modules/pdf/render/pdf-exporter.js +73 -5
- package/dist/cjs/modules/pdf/builder/document-builder.js +3 -22
- package/dist/cjs/modules/pdf/core/pdf-stream.js +49 -3
- package/dist/cjs/modules/pdf/font/font-manager.js +129 -17
- package/dist/cjs/modules/pdf/font/system-fonts.js +194 -0
- package/dist/cjs/modules/pdf/font/ttf-parser.js +29 -1
- package/dist/cjs/modules/pdf/font/type3-font.js +231 -0
- package/dist/cjs/modules/pdf/font/type3-glyphs-extended.js +4167 -0
- package/dist/cjs/modules/pdf/font/type3-glyphs-extended2.js +9652 -0
- package/dist/cjs/modules/pdf/font/type3-glyphs-fill.js +5441 -0
- package/dist/cjs/modules/pdf/font/type3-glyphs-quality.js +5348 -0
- package/dist/cjs/modules/pdf/font/type3-glyphs.js +2573 -0
- package/dist/cjs/modules/pdf/render/layout-engine.js +36 -23
- package/dist/cjs/modules/pdf/render/page-renderer.js +111 -78
- package/dist/cjs/modules/pdf/render/pdf-exporter.js +71 -3
- package/dist/esm/modules/pdf/builder/document-builder.js +4 -23
- package/dist/esm/modules/pdf/core/pdf-stream.js +47 -3
- package/dist/esm/modules/pdf/font/font-manager.js +129 -17
- package/dist/esm/modules/pdf/font/system-fonts.js +188 -0
- package/dist/esm/modules/pdf/font/ttf-parser.js +29 -1
- package/dist/esm/modules/pdf/font/type3-font.js +228 -0
- package/dist/esm/modules/pdf/font/type3-glyphs-extended.js +4164 -0
- package/dist/esm/modules/pdf/font/type3-glyphs-extended2.js +9649 -0
- package/dist/esm/modules/pdf/font/type3-glyphs-fill.js +5438 -0
- package/dist/esm/modules/pdf/font/type3-glyphs-quality.js +5345 -0
- package/dist/esm/modules/pdf/font/type3-glyphs.js +2567 -0
- package/dist/esm/modules/pdf/render/layout-engine.js +36 -23
- package/dist/esm/modules/pdf/render/page-renderer.js +110 -78
- package/dist/esm/modules/pdf/render/pdf-exporter.js +73 -5
- package/dist/iife/excelts.iife.js +25445 -344
- package/dist/iife/excelts.iife.js.map +1 -1
- package/dist/iife/excelts.iife.min.js +48 -46
- package/dist/types/modules/pdf/core/pdf-stream.d.ts +15 -0
- package/dist/types/modules/pdf/font/font-manager.d.ts +37 -6
- package/dist/types/modules/pdf/font/system-fonts.d.ts +41 -0
- package/dist/types/modules/pdf/font/type3-font.d.ts +35 -0
- package/dist/types/modules/pdf/font/type3-glyphs-extended.d.ts +33 -0
- package/dist/types/modules/pdf/font/type3-glyphs-extended2.d.ts +16 -0
- package/dist/types/modules/pdf/font/type3-glyphs-fill.d.ts +17 -0
- package/dist/types/modules/pdf/font/type3-glyphs-quality.d.ts +28 -0
- package/dist/types/modules/pdf/font/type3-glyphs.d.ts +79 -0
- package/dist/types/modules/pdf/render/page-renderer.d.ts +9 -0
- package/package.json +1 -1
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Type3 fallback font builder.
|
|
4
|
+
*
|
|
5
|
+
* Produces a PDF Type3 font object that contains vector-drawn glyphs for
|
|
6
|
+
* Unicode characters that cannot be represented by standard Type1 fonts
|
|
7
|
+
* (WinAnsi encoding).
|
|
8
|
+
*
|
|
9
|
+
* A single Type3 font supports up to 256 glyphs (single-byte encoding).
|
|
10
|
+
* When more than 256 distinct non-WinAnsi characters appear in a document,
|
|
11
|
+
* multiple Type3 fonts are created automatically.
|
|
12
|
+
*
|
|
13
|
+
* @see PDF Reference 1.7, §5.5.4 — Type 3 Fonts
|
|
14
|
+
*/
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.writeType3Fonts = writeType3Fonts;
|
|
17
|
+
const pdf_object_1 = require("../core/pdf-object");
|
|
18
|
+
const pdf_stream_1 = require("../core/pdf-stream");
|
|
19
|
+
const type3_glyphs_1 = require("./type3-glyphs");
|
|
20
|
+
// =============================================================================
|
|
21
|
+
// Constants
|
|
22
|
+
// =============================================================================
|
|
23
|
+
/**
|
|
24
|
+
* Type3 fonts use a FontMatrix to map glyph coordinates to text space.
|
|
25
|
+
* With a 1000-unit em square, FontMatrix = [0.001 0 0 0.001 0 0].
|
|
26
|
+
*/
|
|
27
|
+
const UNITS_PER_EM = 1000;
|
|
28
|
+
/** Maximum glyphs per Type3 font (single-byte encoding limit). */
|
|
29
|
+
const MAX_GLYPHS_PER_FONT = 256;
|
|
30
|
+
// First usable encoding slot (0x00 = .notdef, start user glyphs at 0x01).
|
|
31
|
+
const FIRST_SLOT = 1;
|
|
32
|
+
/**
|
|
33
|
+
* Build and write Type3 fallback fonts to the PDF for the given code points.
|
|
34
|
+
*
|
|
35
|
+
* @param writer - The PdfWriter to add objects to.
|
|
36
|
+
* @param codePoints - Set of Unicode code points that need Type3 rendering.
|
|
37
|
+
* @param resourcePrefix - Prefix for resource names (e.g. "T3F").
|
|
38
|
+
* @returns Encoding and object info for use by FontManager.
|
|
39
|
+
*/
|
|
40
|
+
function writeType3Fonts(writer, codePoints, resourcePrefix = "T3F") {
|
|
41
|
+
const encoding = new Map();
|
|
42
|
+
const fontObjects = new Map();
|
|
43
|
+
const widths = new Map();
|
|
44
|
+
if (codePoints.size === 0) {
|
|
45
|
+
return { fontObjects, encoding, widths };
|
|
46
|
+
}
|
|
47
|
+
// Sort code points for deterministic output
|
|
48
|
+
const sorted = [...codePoints].sort((a, b) => a - b);
|
|
49
|
+
// Partition into chunks of MAX_GLYPHS_PER_FONT - 1 (slot 0 is .notdef)
|
|
50
|
+
const maxPerFont = MAX_GLYPHS_PER_FONT - FIRST_SLOT;
|
|
51
|
+
let fontIndex = 1;
|
|
52
|
+
for (let offset = 0; offset < sorted.length; offset += maxPerFont) {
|
|
53
|
+
const chunk = sorted.slice(offset, offset + maxPerFont);
|
|
54
|
+
const resourceName = `${resourcePrefix}${fontIndex}`;
|
|
55
|
+
fontIndex++;
|
|
56
|
+
const { objNum, fontWidths } = writeSingleType3Font(writer, chunk, resourceName);
|
|
57
|
+
fontObjects.set(resourceName, objNum);
|
|
58
|
+
widths.set(resourceName, fontWidths);
|
|
59
|
+
// Record encoding for each code point
|
|
60
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
61
|
+
encoding.set(chunk[i], { resourceName, charCode: FIRST_SLOT + i });
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return { fontObjects, encoding, widths };
|
|
65
|
+
}
|
|
66
|
+
// =============================================================================
|
|
67
|
+
// Internal — Single Type3 Font
|
|
68
|
+
// =============================================================================
|
|
69
|
+
function writeSingleType3Font(writer, codePoints, resourceName) {
|
|
70
|
+
// Build CharProcs: each glyph is a content stream
|
|
71
|
+
const charProcRefs = [];
|
|
72
|
+
const encodingNames = [];
|
|
73
|
+
const widthValues = new Array(FIRST_SLOT + codePoints.length).fill(0);
|
|
74
|
+
const fontWidths = new Map();
|
|
75
|
+
// Slot 0: .notdef
|
|
76
|
+
const notdefObj = writeGlyphStream(writer, type3_glyphs_1.NOTDEF_GLYPH);
|
|
77
|
+
charProcRefs.push({ name: ".notdef", objNum: notdefObj });
|
|
78
|
+
widthValues[0] = type3_glyphs_1.NOTDEF_GLYPH.width;
|
|
79
|
+
for (let i = 0; i < codePoints.length; i++) {
|
|
80
|
+
const cp = codePoints[i];
|
|
81
|
+
const glyphDef = (0, type3_glyphs_1.lookupGlyph)(cp) ?? type3_glyphs_1.NOTDEF_GLYPH;
|
|
82
|
+
const glyphName = `uni${cp.toString(16).toUpperCase().padStart(4, "0")}`;
|
|
83
|
+
const objNum = writeGlyphStream(writer, glyphDef);
|
|
84
|
+
charProcRefs.push({ name: glyphName, objNum });
|
|
85
|
+
encodingNames.push(glyphName);
|
|
86
|
+
const slot = FIRST_SLOT + i;
|
|
87
|
+
widthValues[slot] = glyphDef.width;
|
|
88
|
+
fontWidths.set(slot, glyphDef.width);
|
|
89
|
+
}
|
|
90
|
+
// Build CharProcs dictionary
|
|
91
|
+
const charProcsEntries = [];
|
|
92
|
+
for (const { name, objNum } of charProcRefs) {
|
|
93
|
+
charProcsEntries.push(`${(0, pdf_object_1.pdfName)(name)} ${(0, pdf_object_1.pdfRef)(objNum)}`);
|
|
94
|
+
}
|
|
95
|
+
const charProcsDict = `<<\n${charProcsEntries.join("\n")}\n>>`;
|
|
96
|
+
// Build Encoding dictionary
|
|
97
|
+
const differences = [];
|
|
98
|
+
differences.push(`${FIRST_SLOT}`);
|
|
99
|
+
for (const name of encodingNames) {
|
|
100
|
+
differences.push((0, pdf_object_1.pdfName)(name));
|
|
101
|
+
}
|
|
102
|
+
const encodingObjNum = writer.allocObject();
|
|
103
|
+
const encodingDict = new pdf_object_1.PdfDict()
|
|
104
|
+
.set("Type", "/Encoding")
|
|
105
|
+
.set("Differences", `[${differences.join(" ")}]`);
|
|
106
|
+
writer.addObject(encodingObjNum, encodingDict);
|
|
107
|
+
// Build ToUnicode CMap
|
|
108
|
+
const toUnicodeObjNum = writeToUnicodeCMap(writer, codePoints);
|
|
109
|
+
// Build the Type3 font dictionary
|
|
110
|
+
const fontObjNum = writer.allocObject();
|
|
111
|
+
const fontBBox = `[0 0 ${UNITS_PER_EM} ${UNITS_PER_EM}]`;
|
|
112
|
+
const fontMatrix = "[0.001 0 0 0.001 0 0]";
|
|
113
|
+
const fontDict = new pdf_object_1.PdfDict()
|
|
114
|
+
.set("Type", "/Font")
|
|
115
|
+
.set("Subtype", "/Type3")
|
|
116
|
+
.set("Name", (0, pdf_object_1.pdfName)(resourceName))
|
|
117
|
+
.set("FontBBox", fontBBox)
|
|
118
|
+
.set("FontMatrix", fontMatrix)
|
|
119
|
+
.set("FirstChar", (0, pdf_object_1.pdfNumber)(0))
|
|
120
|
+
.set("LastChar", (0, pdf_object_1.pdfNumber)(widthValues.length - 1))
|
|
121
|
+
.set("Widths", (0, pdf_object_1.pdfArray)(widthValues.map(w => (0, pdf_object_1.pdfNumber)(w))))
|
|
122
|
+
.set("CharProcs", charProcsDict)
|
|
123
|
+
.set("Encoding", (0, pdf_object_1.pdfRef)(encodingObjNum))
|
|
124
|
+
.set("ToUnicode", (0, pdf_object_1.pdfRef)(toUnicodeObjNum));
|
|
125
|
+
writer.addObject(fontObjNum, fontDict);
|
|
126
|
+
return { objNum: fontObjNum, fontWidths };
|
|
127
|
+
}
|
|
128
|
+
// =============================================================================
|
|
129
|
+
// Glyph Stream Writer
|
|
130
|
+
// =============================================================================
|
|
131
|
+
function writeGlyphStream(writer, glyph) {
|
|
132
|
+
const stream = new pdf_stream_1.PdfContentStream();
|
|
133
|
+
// d1 operator: wx wy llx lly urx ury — sets glyph width and bounding box
|
|
134
|
+
// This tells the PDF viewer the advance width and clip region
|
|
135
|
+
stream.raw(`${glyph.width} 0 0 0 ${UNITS_PER_EM} ${UNITS_PER_EM} d1`);
|
|
136
|
+
// Draw the glyph using the pen adapter
|
|
137
|
+
const pen = createPen(stream);
|
|
138
|
+
glyph.draw(pen);
|
|
139
|
+
const objNum = writer.allocObject();
|
|
140
|
+
writer.addStreamObject(objNum, new pdf_object_1.PdfDict(), stream);
|
|
141
|
+
return objNum;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Create a GlyphPen backed by a PdfContentStream.
|
|
145
|
+
*/
|
|
146
|
+
function createPen(stream) {
|
|
147
|
+
return {
|
|
148
|
+
M: (x, y) => {
|
|
149
|
+
stream.moveTo(x, y);
|
|
150
|
+
},
|
|
151
|
+
L: (x, y) => {
|
|
152
|
+
stream.lineTo(x, y);
|
|
153
|
+
},
|
|
154
|
+
C: (x1, y1, x2, y2, x3, y3) => {
|
|
155
|
+
stream.curveTo(x1, y1, x2, y2, x3, y3);
|
|
156
|
+
},
|
|
157
|
+
Z: () => {
|
|
158
|
+
stream.closePath();
|
|
159
|
+
},
|
|
160
|
+
rect: (x, y, w, h) => {
|
|
161
|
+
stream.rect(x, y, w, h);
|
|
162
|
+
},
|
|
163
|
+
circle: (cx, cy, r) => {
|
|
164
|
+
stream.circle(cx, cy, r);
|
|
165
|
+
},
|
|
166
|
+
ellipse: (cx, cy, rx, ry) => {
|
|
167
|
+
stream.ellipse(cx, cy, rx, ry);
|
|
168
|
+
},
|
|
169
|
+
stroke: () => {
|
|
170
|
+
stream.stroke();
|
|
171
|
+
},
|
|
172
|
+
fill: () => {
|
|
173
|
+
stream.fill();
|
|
174
|
+
},
|
|
175
|
+
fillStroke: () => {
|
|
176
|
+
stream.fillAndStroke();
|
|
177
|
+
},
|
|
178
|
+
lineWidth: w => {
|
|
179
|
+
stream.setLineWidth(w);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
// =============================================================================
|
|
184
|
+
// ToUnicode CMap
|
|
185
|
+
// =============================================================================
|
|
186
|
+
function writeToUnicodeCMap(writer, codePoints) {
|
|
187
|
+
const lines = [];
|
|
188
|
+
lines.push("/CIDInit /ProcSet findresource begin");
|
|
189
|
+
lines.push("12 dict begin");
|
|
190
|
+
lines.push("begincmap");
|
|
191
|
+
lines.push("/CIDSystemInfo");
|
|
192
|
+
lines.push("<< /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def");
|
|
193
|
+
lines.push("/CMapName /Adobe-Identity-UCS def");
|
|
194
|
+
lines.push("/CMapType 2 def");
|
|
195
|
+
lines.push("1 begincodespacerange");
|
|
196
|
+
lines.push("<00> <FF>");
|
|
197
|
+
lines.push("endcodespacerange");
|
|
198
|
+
// Write mappings in chunks of 100 (PDF limit per beginbfchar block)
|
|
199
|
+
for (let offset = 0; offset < codePoints.length; offset += 100) {
|
|
200
|
+
const chunk = codePoints.slice(offset, offset + 100);
|
|
201
|
+
lines.push(`${chunk.length} beginbfchar`);
|
|
202
|
+
for (let i = 0; i < chunk.length; i++) {
|
|
203
|
+
const slot = FIRST_SLOT + offset + i;
|
|
204
|
+
const cp = chunk[i];
|
|
205
|
+
const slotHex = slot.toString(16).toUpperCase().padStart(2, "0");
|
|
206
|
+
if (cp > 0xffff) {
|
|
207
|
+
// Supplementary character — encode as UTF-16 surrogate pair
|
|
208
|
+
const hi = Math.floor((cp - 0x10000) / 0x400) + 0xd800;
|
|
209
|
+
const lo = ((cp - 0x10000) % 0x400) + 0xdc00;
|
|
210
|
+
const hiHex = hi.toString(16).toUpperCase().padStart(4, "0");
|
|
211
|
+
const loHex = lo.toString(16).toUpperCase().padStart(4, "0");
|
|
212
|
+
lines.push(`<${slotHex}> <${hiHex}${loHex}>`);
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
const cpHex = cp.toString(16).toUpperCase().padStart(4, "0");
|
|
216
|
+
lines.push(`<${slotHex}> <${cpHex}>`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
lines.push("endbfchar");
|
|
220
|
+
}
|
|
221
|
+
lines.push("endcmap");
|
|
222
|
+
lines.push("CMapName currentdict /CMap defineresource pop");
|
|
223
|
+
lines.push("end");
|
|
224
|
+
lines.push("end");
|
|
225
|
+
const cmapStr = lines.join("\n");
|
|
226
|
+
const encoder = new TextEncoder();
|
|
227
|
+
const data = encoder.encode(cmapStr);
|
|
228
|
+
const objNum = writer.allocObject();
|
|
229
|
+
writer.addStreamObject(objNum, new pdf_object_1.PdfDict(), data);
|
|
230
|
+
return objNum;
|
|
231
|
+
}
|