@evotecit/officeimo 0.0.0-stage → 0.1.1
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/LICENSE +21 -0
- package/README.md +1222 -2
- package/bundles/officeimo-canopyx.js +11741 -0
- package/bundles/officeimo-canopyx.mjs +11639 -0
- package/bundles/officeimo-csv.js +1264 -0
- package/bundles/officeimo-csv.mjs +1239 -0
- package/bundles/officeimo-datatables.js +12018 -0
- package/bundles/officeimo-datatables.mjs +11915 -0
- package/bundles/officeimo-execution.js +1513 -0
- package/bundles/officeimo-execution.mjs +1489 -0
- package/bundles/officeimo-pdf.js +7009 -0
- package/bundles/officeimo-pdf.mjs +6946 -0
- package/bundles/officeimo-xlsx.js +5344 -0
- package/bundles/officeimo-xlsx.mjs +5290 -0
- package/bundles/officeimo.js +11468 -0
- package/bundles/officeimo.mjs +11369 -0
- package/dist/core/dates.d.ts +1 -0
- package/dist/core/dates.js +16 -0
- package/dist/core/errors.d.ts +12 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/grouped-report.d.ts +87 -0
- package/dist/core/grouped-report.js +252 -0
- package/dist/core/index.d.ts +74 -0
- package/dist/core/index.js +37 -0
- package/dist/core/iteration.d.ts +7 -0
- package/dist/core/iteration.js +149 -0
- package/dist/core/limits.d.ts +7 -0
- package/dist/core/limits.js +56 -0
- package/dist/core/links.d.ts +5 -0
- package/dist/core/links.js +14 -0
- package/dist/core/presentation.d.ts +29 -0
- package/dist/core/presentation.js +59 -0
- package/dist/core/progress.d.ts +1 -0
- package/dist/core/progress.js +10 -0
- package/dist/core/sinks.d.ts +33 -0
- package/dist/core/sinks.js +102 -0
- package/dist/core/table.d.ts +8 -0
- package/dist/core/table.js +1 -0
- package/dist/core/value-text.d.ts +7 -0
- package/dist/core/value-text.js +13 -0
- package/dist/csv/index.d.ts +44 -0
- package/dist/csv/index.js +133 -0
- package/dist/csv/options.d.ts +1 -0
- package/dist/csv/options.js +8 -0
- package/dist/csv/structured-rows.d.ts +3 -0
- package/dist/csv/structured-rows.js +15 -0
- package/dist/execution/host.d.ts +5 -0
- package/dist/execution/host.js +164 -0
- package/dist/execution/index.d.ts +3 -0
- package/dist/execution/index.js +2 -0
- package/dist/execution/protocol.d.ts +48 -0
- package/dist/execution/protocol.js +173 -0
- package/dist/execution/types.d.ts +42 -0
- package/dist/execution/types.js +1 -0
- package/dist/execution/worker.d.ts +3 -0
- package/dist/execution/worker.js +147 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +14 -0
- package/dist/integrations/canopyx/dates.d.ts +1 -0
- package/dist/integrations/canopyx/dates.js +34 -0
- package/dist/integrations/canopyx/index.d.ts +10 -0
- package/dist/integrations/canopyx/index.js +45 -0
- package/dist/integrations/canopyx/source.d.ts +3 -0
- package/dist/integrations/canopyx/source.js +122 -0
- package/dist/integrations/canopyx/types.d.ts +83 -0
- package/dist/integrations/canopyx/types.js +1 -0
- package/dist/integrations/datatables/api.d.ts +1 -0
- package/dist/integrations/datatables/api.js +27 -0
- package/dist/integrations/datatables/headings.d.ts +1 -0
- package/dist/integrations/datatables/headings.js +65 -0
- package/dist/integrations/datatables/index.d.ts +12 -0
- package/dist/integrations/datatables/index.js +167 -0
- package/dist/integrations/datatables/snapshot.d.ts +3 -0
- package/dist/integrations/datatables/snapshot.js +206 -0
- package/dist/integrations/datatables/types.d.ts +98 -0
- package/dist/integrations/datatables/types.js +1 -0
- package/dist/integrations/options.d.ts +13 -0
- package/dist/integrations/options.js +34 -0
- package/dist/internal/capture.d.ts +4 -0
- package/dist/internal/capture.js +14 -0
- package/dist/internal/columns.d.ts +27 -0
- package/dist/internal/columns.js +1 -0
- package/dist/internal/crc32.d.ts +6 -0
- package/dist/internal/crc32.js +17 -0
- package/dist/internal/export-values.d.ts +3 -0
- package/dist/internal/export-values.js +14 -0
- package/dist/internal/rows.d.ts +6 -0
- package/dist/internal/rows.js +77 -0
- package/dist/internal/table-spans.d.ts +11 -0
- package/dist/internal/table-spans.js +58 -0
- package/dist/internal/totals.d.ts +15 -0
- package/dist/internal/totals.js +42 -0
- package/dist/opc/index.d.ts +67 -0
- package/dist/opc/index.js +233 -0
- package/dist/opc/properties.d.ts +15 -0
- package/dist/opc/properties.js +18 -0
- package/dist/opc/uri.d.ts +5 -0
- package/dist/opc/uri.js +41 -0
- package/dist/pdf/annotations.d.ts +1 -0
- package/dist/pdf/annotations.js +38 -0
- package/dist/pdf/binary.d.ts +6 -0
- package/dist/pdf/binary.js +24 -0
- package/dist/pdf/document-drawings.d.ts +15 -0
- package/dist/pdf/document-drawings.js +237 -0
- package/dist/pdf/document-grouped.d.ts +1 -0
- package/dist/pdf/document-grouped.js +244 -0
- package/dist/pdf/document-images.d.ts +19 -0
- package/dist/pdf/document-images.js +125 -0
- package/dist/pdf/document-types.d.ts +160 -0
- package/dist/pdf/document-types.js +1 -0
- package/dist/pdf/document-vectors.d.ts +11 -0
- package/dist/pdf/document-vectors.js +77 -0
- package/dist/pdf/document.d.ts +9 -0
- package/dist/pdf/document.js +270 -0
- package/dist/pdf/drawing-types.d.ts +53 -0
- package/dist/pdf/drawing-types.js +1 -0
- package/dist/pdf/font-resources.d.ts +43 -0
- package/dist/pdf/font-resources.js +202 -0
- package/dist/pdf/font-support.d.ts +1 -0
- package/dist/pdf/font-support.js +60 -0
- package/dist/pdf/font.d.ts +17 -0
- package/dist/pdf/font.js +56 -0
- package/dist/pdf/grouped-table.d.ts +6 -0
- package/dist/pdf/grouped-table.js +40 -0
- package/dist/pdf/grouped-types.d.ts +34 -0
- package/dist/pdf/grouped-types.js +1 -0
- package/dist/pdf/grouped.d.ts +10 -0
- package/dist/pdf/grouped.js +37 -0
- package/dist/pdf/index.d.ts +22 -0
- package/dist/pdf/index.js +8 -0
- package/dist/pdf/inline-text.d.ts +30 -0
- package/dist/pdf/inline-text.js +183 -0
- package/dist/pdf/inline-types.d.ts +15 -0
- package/dist/pdf/inline-types.js +1 -0
- package/dist/pdf/list-layout.d.ts +6 -0
- package/dist/pdf/list-layout.js +49 -0
- package/dist/pdf/objects.d.ts +2 -0
- package/dist/pdf/objects.js +320 -0
- package/dist/pdf/outlines.d.ts +1 -0
- package/dist/pdf/outlines.js +55 -0
- package/dist/pdf/page-context.d.ts +1 -0
- package/dist/pdf/page-context.js +12 -0
- package/dist/pdf/pages.d.ts +1 -0
- package/dist/pdf/pages.js +678 -0
- package/dist/pdf/paragraph-layout.d.ts +21 -0
- package/dist/pdf/paragraph-layout.js +71 -0
- package/dist/pdf/paragraph-styles.d.ts +1 -0
- package/dist/pdf/paragraph-styles.js +66 -0
- package/dist/pdf/png.d.ts +9 -0
- package/dist/pdf/png.js +259 -0
- package/dist/pdf/preflight.d.ts +7 -0
- package/dist/pdf/preflight.js +32 -0
- package/dist/pdf/presentation-settings.d.ts +5 -0
- package/dist/pdf/presentation-settings.js +28 -0
- package/dist/pdf/providers/harfbuzz.d.ts +60 -0
- package/dist/pdf/providers/harfbuzz.js +112 -0
- package/dist/pdf/providers/unicode13-profile.d.ts +2 -0
- package/dist/pdf/providers/unicode13-profile.js +115 -0
- package/dist/pdf/section-types.d.ts +15 -0
- package/dist/pdf/section-types.js +40 -0
- package/dist/pdf/session.d.ts +25 -0
- package/dist/pdf/session.js +55 -0
- package/dist/pdf/settings.d.ts +17 -0
- package/dist/pdf/settings.js +196 -0
- package/dist/pdf/shaped-text.d.ts +5 -0
- package/dist/pdf/shaped-text.js +205 -0
- package/dist/pdf/standard-widths.d.ts +1 -0
- package/dist/pdf/standard-widths.js +317 -0
- package/dist/pdf/structure-parent-tree.d.ts +1 -0
- package/dist/pdf/structure-parent-tree.js +57 -0
- package/dist/pdf/structure.d.ts +6 -0
- package/dist/pdf/structure.js +302 -0
- package/dist/pdf/table-band-flow.d.ts +34 -0
- package/dist/pdf/table-band-flow.js +129 -0
- package/dist/pdf/table-band-plan.d.ts +10 -0
- package/dist/pdf/table-band-plan.js +83 -0
- package/dist/pdf/table-layout.d.ts +75 -0
- package/dist/pdf/table-layout.js +217 -0
- package/dist/pdf/table-structure.d.ts +1 -0
- package/dist/pdf/table-structure.js +108 -0
- package/dist/pdf/text-shaping-types.d.ts +39 -0
- package/dist/pdf/text-shaping-types.js +1 -0
- package/dist/pdf/text.d.ts +13 -0
- package/dist/pdf/text.js +101 -0
- package/dist/pdf/truetype-subset.d.ts +5 -0
- package/dist/pdf/truetype-subset.js +217 -0
- package/dist/pdf/truetype.d.ts +1 -0
- package/dist/pdf/truetype.js +235 -0
- package/dist/pdf/types.d.ts +171 -0
- package/dist/pdf/types.js +1 -0
- package/dist/pdf/write.d.ts +6 -0
- package/dist/pdf/write.js +57 -0
- package/dist/pdf/xref.d.ts +1 -0
- package/dist/pdf/xref.js +30 -0
- package/dist/xlsx/attachments.d.ts +30 -0
- package/dist/xlsx/attachments.js +67 -0
- package/dist/xlsx/chart-bindings.d.ts +1 -0
- package/dist/xlsx/chart-bindings.js +122 -0
- package/dist/xlsx/chart-types.d.ts +73 -0
- package/dist/xlsx/chart-types.js +1 -0
- package/dist/xlsx/chart-xml.d.ts +2 -0
- package/dist/xlsx/chart-xml.js +103 -0
- package/dist/xlsx/charts.d.ts +1 -0
- package/dist/xlsx/charts.js +159 -0
- package/dist/xlsx/conditional-formatting.d.ts +1 -0
- package/dist/xlsx/conditional-formatting.js +158 -0
- package/dist/xlsx/conditional-types.d.ts +64 -0
- package/dist/xlsx/conditional-types.js +1 -0
- package/dist/xlsx/data-row-ranges.d.ts +5 -0
- package/dist/xlsx/data-row-ranges.js +39 -0
- package/dist/xlsx/differential-styles.d.ts +1 -0
- package/dist/xlsx/differential-styles.js +102 -0
- package/dist/xlsx/formula.d.ts +8 -0
- package/dist/xlsx/formula.js +104 -0
- package/dist/xlsx/grouped-layout.d.ts +61 -0
- package/dist/xlsx/grouped-layout.js +79 -0
- package/dist/xlsx/grouped.d.ts +16 -0
- package/dist/xlsx/grouped.js +161 -0
- package/dist/xlsx/index.d.ts +24 -0
- package/dist/xlsx/index.js +10 -0
- package/dist/xlsx/layout.d.ts +1 -0
- package/dist/xlsx/layout.js +186 -0
- package/dist/xlsx/options.d.ts +1 -0
- package/dist/xlsx/options.js +16 -0
- package/dist/xlsx/portable.d.ts +27 -0
- package/dist/xlsx/portable.js +44 -0
- package/dist/xlsx/preservation.d.ts +1 -0
- package/dist/xlsx/preservation.js +60 -0
- package/dist/xlsx/ranges.d.ts +1 -0
- package/dist/xlsx/ranges.js +24 -0
- package/dist/xlsx/regions.d.ts +1 -0
- package/dist/xlsx/regions.js +97 -0
- package/dist/xlsx/structured-rows.d.ts +11 -0
- package/dist/xlsx/structured-rows.js +28 -0
- package/dist/xlsx/style-xml.d.ts +1 -0
- package/dist/xlsx/style-xml.js +6 -0
- package/dist/xlsx/styles.d.ts +70 -0
- package/dist/xlsx/styles.js +203 -0
- package/dist/xlsx/table.d.ts +12 -0
- package/dist/xlsx/table.js +35 -0
- package/dist/xlsx/types.d.ts +180 -0
- package/dist/xlsx/types.js +1 -0
- package/dist/xlsx/validation-types.d.ts +18 -0
- package/dist/xlsx/validation-types.js +1 -0
- package/dist/xlsx/validation.d.ts +1 -0
- package/dist/xlsx/validation.js +92 -0
- package/dist/xlsx/values.d.ts +13 -0
- package/dist/xlsx/values.js +87 -0
- package/dist/xlsx/workbook.d.ts +50 -0
- package/dist/xlsx/workbook.js +452 -0
- package/dist/xlsx/worksheet.d.ts +71 -0
- package/dist/xlsx/worksheet.js +700 -0
- package/dist/xlsx/write.d.ts +15 -0
- package/dist/xlsx/write.js +47 -0
- package/dist/xml/index.d.ts +24 -0
- package/dist/xml/index.js +74 -0
- package/dist/zip/entry.d.ts +39 -0
- package/dist/zip/entry.js +122 -0
- package/dist/zip/index.d.ts +44 -0
- package/dist/zip/index.js +248 -0
- package/package.json +104 -4
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { BlobByteSink, withDestination } from "../core/sinks.js";
|
|
2
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
3
|
+
import { beginTask, checkAbort, consumeRows, pause, taskYieldDue } from "../core/iteration.js";
|
|
4
|
+
import { notifyProgress } from "../core/progress.js";
|
|
5
|
+
import { copyColumns, createRowProjector } from "../internal/rows.js";
|
|
6
|
+
import { captureFields } from "../internal/capture.js";
|
|
7
|
+
import { settings, captureWideTable } from "./settings.js";
|
|
8
|
+
import { pdfNumber as n } from "./objects.js";
|
|
9
|
+
import { PdfTableLayout } from "./table-layout.js";
|
|
10
|
+
import { PdfWriteSession } from "./session.js";
|
|
11
|
+
import { PdfDocumentImages } from "./document-images.js";
|
|
12
|
+
import { vectorCommands } from "./document-vectors.js";
|
|
13
|
+
import { graphicAlternative } from "./structure.js";
|
|
14
|
+
import { checkDrawingBlock, prepareDrawing } from "./document-drawings.js";
|
|
15
|
+
import { captureParagraph, captureCaption, prepareParagraph, checkParagraphBuffer, paragraphStartHeight } from "./paragraph-layout.js";
|
|
16
|
+
import { captureListOptions, prepareListItem } from "./list-layout.js";
|
|
17
|
+
import { captureSection } from "./section-types.js";
|
|
18
|
+
import { renderGroupedPdfTable } from "./document-grouped.js";
|
|
19
|
+
const tableOptionFields = ["columns", "includeHeader", "headerRows", "footer", "headerPresentation", "footerPresentation", "alternateRowColor", "columnWidths", "wideTable", "formatValue", "formatRuns"];
|
|
20
|
+
/** Capture typed columns for a single-use table source in a composed document. */
|
|
21
|
+
export function pdfTable(rows, options) {
|
|
22
|
+
const captured = captureTableOptions(options);
|
|
23
|
+
return Object.freeze({ kind: "table", rows, options: Object.freeze({ ...captured, columns: Object.freeze(copyColumns(captured.columns)) }) });
|
|
24
|
+
}
|
|
25
|
+
/** Write sequential report blocks to a PDF Blob, retaining finished output proportional to file size. */
|
|
26
|
+
export async function writePdfDocument(blocks, options = {}) {
|
|
27
|
+
const sink = new BlobByteSink();
|
|
28
|
+
try {
|
|
29
|
+
await writePdfDocumentTo(blocks, sink, options);
|
|
30
|
+
return sink.toBlob("application/pdf");
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
sink.discard();
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/** Stream composed pages with backpressure. Rows sum table source rows; columns reports the widest table.
|
|
38
|
+
* Analytical subtotals/grand totals also consume the cumulative maxRows output budget. */
|
|
39
|
+
export async function writePdfDocumentTo(blocks, destination, configuration = {}) {
|
|
40
|
+
const captured = settings(configuration, true), controller = new AbortController();
|
|
41
|
+
const parentSignal = captured.options.signal, abort = () => controller.abort(parentSignal?.reason);
|
|
42
|
+
parentSignal?.addEventListener("abort", abort, { once: true });
|
|
43
|
+
if (parentSignal?.aborted)
|
|
44
|
+
abort();
|
|
45
|
+
const prepared = { ...captured, options: { ...captured.options, signal: controller.signal } }, { options, budget } = prepared;
|
|
46
|
+
try {
|
|
47
|
+
beginTask();
|
|
48
|
+
checkAbort(options.signal);
|
|
49
|
+
return await withDestination(destination, async (sink) => {
|
|
50
|
+
const session = new PdfWriteSession(sink, prepared), { objects, pages, fonts } = session, images = new PdfDocumentImages(objects, prepared);
|
|
51
|
+
let rows = 0, budgetRows = 0, columns = 0, commands = 0, drawingElements = 0, listItems = 0;
|
|
52
|
+
let pending = [];
|
|
53
|
+
const flushParagraphs = async (nextHeight = 0) => {
|
|
54
|
+
const paragraphs = [];
|
|
55
|
+
for (const paragraph of pending)
|
|
56
|
+
paragraphs.push(await prepareParagraph(paragraph, pages.settings, fonts));
|
|
57
|
+
pending = [];
|
|
58
|
+
await pages.paragraphs(paragraphs, nextHeight);
|
|
59
|
+
};
|
|
60
|
+
const enqueueParagraph = async (block) => {
|
|
61
|
+
const paragraph = captureParagraph(block, pages.settings, fonts);
|
|
62
|
+
if (!paragraph.text.text)
|
|
63
|
+
return;
|
|
64
|
+
if (pending.length || paragraph.keepWithNext) {
|
|
65
|
+
pending.push(paragraph);
|
|
66
|
+
checkParagraphBuffer(pending, pages.settings);
|
|
67
|
+
if (!paragraph.keepWithNext)
|
|
68
|
+
await flushParagraphs();
|
|
69
|
+
}
|
|
70
|
+
else
|
|
71
|
+
await pages.paragraphs([await prepareParagraph(paragraph, pages.settings, fonts)]);
|
|
72
|
+
};
|
|
73
|
+
const prepareCaption = async (captured) => captured === undefined ? undefined : prepareParagraph(captured, pages.settings, fonts);
|
|
74
|
+
const snapshotCaption = (caption) => caption === undefined ? undefined : captureCaption(caption, pages.settings, fonts);
|
|
75
|
+
await session.start();
|
|
76
|
+
await consumeRows(blocks, options.signal, async (block) => {
|
|
77
|
+
if (!block || typeof block !== "object")
|
|
78
|
+
throw new TypeError("PDF document blocks must be objects.");
|
|
79
|
+
switch (block.kind) {
|
|
80
|
+
case "section": {
|
|
81
|
+
const capturedSection = captureSection(block, prepared);
|
|
82
|
+
await flushParagraphs();
|
|
83
|
+
const section = await pages.beginSection(capturedSection);
|
|
84
|
+
if (section.includeHeading)
|
|
85
|
+
await enqueueParagraph({ kind: "paragraph", text: section.title,
|
|
86
|
+
headingLevel: section.level, ...(section.headingStyle === undefined ?
|
|
87
|
+
{ fontSize: pages.settings.fontSize * 1.5, presentation: { bold: true } } : { style: section.headingStyle }),
|
|
88
|
+
keepTogether: true, keepWithNext: true,
|
|
89
|
+
...(section.bookmark ? { bookmark: { title: section.title, level: section.level } } : {}) });
|
|
90
|
+
else if (section.bookmark)
|
|
91
|
+
pages.addBookmark({ title: section.title, level: section.level });
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case "paragraph":
|
|
95
|
+
await enqueueParagraph(block);
|
|
96
|
+
break;
|
|
97
|
+
case "list": {
|
|
98
|
+
const captured = captureListOptions(block, pages.settings);
|
|
99
|
+
let count = 0, owner;
|
|
100
|
+
await consumeRows(captured.items, options.signal, async (item) => {
|
|
101
|
+
if (++listItems > prepared.limits.maxListItems)
|
|
102
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxListItems exceeded.");
|
|
103
|
+
const laidOut = await prepareListItem(item, count++, captured, pages.settings, fonts);
|
|
104
|
+
if (count === 1) {
|
|
105
|
+
await flushParagraphs(laidOut.paragraph.keepTogether ? laidOut.paragraph.height : laidOut.paragraph.firstHeight);
|
|
106
|
+
owner = session.structure?.element("L");
|
|
107
|
+
}
|
|
108
|
+
await pages.listItem(laidOut, owner);
|
|
109
|
+
if (taskYieldDue())
|
|
110
|
+
await pause();
|
|
111
|
+
checkAbort(options.signal);
|
|
112
|
+
});
|
|
113
|
+
await session.structure?.complete(owner);
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
case "group": {
|
|
117
|
+
const blocks = block.blocks, keepTogether = block.keepTogether;
|
|
118
|
+
if (!Array.isArray(blocks))
|
|
119
|
+
throw new TypeError("PDF paragraph group blocks must be an array.");
|
|
120
|
+
if (keepTogether !== undefined && typeof keepTogether !== "boolean")
|
|
121
|
+
throw new TypeError("PDF paragraph group keepTogether must be boolean.");
|
|
122
|
+
if (blocks.length > prepared.limits.maxGroupBlocks)
|
|
123
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxGroupBlocks exceeded.");
|
|
124
|
+
const captured = [];
|
|
125
|
+
for (const paragraph of blocks) {
|
|
126
|
+
captured.push(captureParagraph(paragraph, pages.settings, fonts));
|
|
127
|
+
checkParagraphBuffer(captured, prepared);
|
|
128
|
+
}
|
|
129
|
+
if (captured.filter(paragraph => paragraph.text.text).at(-1)?.keepWithNext)
|
|
130
|
+
throw new TypeError("The last paragraph in a group cannot keepWithNext beyond the group's boundary.");
|
|
131
|
+
const paragraphs = [];
|
|
132
|
+
for (const paragraph of captured)
|
|
133
|
+
paragraphs.push(await prepareParagraph(paragraph, pages.settings, fonts));
|
|
134
|
+
if (!paragraphs.some(paragraph => paragraph.lines.length > 0))
|
|
135
|
+
break;
|
|
136
|
+
await flushParagraphs(keepTogether !== false ? paragraphs.reduce((height, paragraph) => height + paragraph.height, 0) : paragraphStartHeight(paragraphs));
|
|
137
|
+
await pages.paragraphs(paragraphs, 0, keepTogether !== false);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "pageBreak":
|
|
141
|
+
await flushParagraphs();
|
|
142
|
+
await pages.pageBreak();
|
|
143
|
+
break;
|
|
144
|
+
case "groupedTable":
|
|
145
|
+
await renderGroupedPdfTable(block, { prepared, pages, fonts, controller, flushParagraphs,
|
|
146
|
+
acceptColumns: count => { columns = Math.max(columns, count); },
|
|
147
|
+
acceptRow: source => {
|
|
148
|
+
budget.row(++budgetRows);
|
|
149
|
+
if (source && ++rows % 256 === 0)
|
|
150
|
+
notifyProgress(options.onProgress, { phase: "rows", rows, bytes: objects.bytes });
|
|
151
|
+
} });
|
|
152
|
+
break;
|
|
153
|
+
case "table": {
|
|
154
|
+
const { options: inputTableOptions, rows: source, caption: inputCaption, continuationCaption: inputContinuationCaption } = block;
|
|
155
|
+
const tableOptions = captureTableOptions(inputTableOptions);
|
|
156
|
+
const capturedCaption = snapshotCaption(inputCaption);
|
|
157
|
+
const capturedContinuationCaption = snapshotCaption(inputContinuationCaption);
|
|
158
|
+
const table = settings({ ...pages.settings.options, ...tableOptions, columns: tableOptions?.columns, includeHeader: tableOptions?.includeHeader ?? true }), layout = new PdfTableLayout({ ...table, budget }, fonts);
|
|
159
|
+
const caption = await prepareCaption(capturedCaption);
|
|
160
|
+
const continuationCaption = await prepareCaption(capturedContinuationCaption);
|
|
161
|
+
const project = createRowProjector(table.options.columns, undefined, options.signal);
|
|
162
|
+
let count = 0, started = false;
|
|
163
|
+
columns = Math.max(columns, table.options.columns.length);
|
|
164
|
+
const headings = await layout.headers(), headingHeight = await layout.blockHeight(headings);
|
|
165
|
+
await consumeRows(source, options.signal, async (row) => {
|
|
166
|
+
budget.row(++budgetRows);
|
|
167
|
+
const laidOut = await layout.data(project(row, count), count), keptHeading = !started && (pending.length > 0 || !!caption?.lines.length);
|
|
168
|
+
if (!started) {
|
|
169
|
+
const minimum = layout.minimumRowHeight(laidOut);
|
|
170
|
+
await flushParagraphs((caption?.height ?? 0) + headingHeight + minimum);
|
|
171
|
+
await pages.beginTable(layout, headings, minimum, caption, continuationCaption);
|
|
172
|
+
started = true;
|
|
173
|
+
}
|
|
174
|
+
await pages.row(laidOut, keptHeading);
|
|
175
|
+
count++;
|
|
176
|
+
rows++;
|
|
177
|
+
if (rows % 256 === 0)
|
|
178
|
+
notifyProgress(options.onProgress, { phase: "rows", rows, bytes: objects.bytes });
|
|
179
|
+
if (taskYieldDue())
|
|
180
|
+
await pause();
|
|
181
|
+
checkAbort(options.signal);
|
|
182
|
+
});
|
|
183
|
+
const footer = await layout.footer(count), keptFooterHeading = !started && (pending.length > 0 || !!caption?.lines.length);
|
|
184
|
+
if (!started) {
|
|
185
|
+
if (!headings.length && !footer?.length)
|
|
186
|
+
break;
|
|
187
|
+
const minimum = footer?.length ? table.options.footer?.rows ? await layout.blockHeight(footer) :
|
|
188
|
+
layout.minimumRowHeight(footer[0]) : 0;
|
|
189
|
+
await flushParagraphs((caption?.height ?? 0) + headingHeight + minimum);
|
|
190
|
+
await pages.beginTable(layout, headings, minimum, caption, continuationCaption);
|
|
191
|
+
}
|
|
192
|
+
if (footer) {
|
|
193
|
+
if (table.options.footer?.rows)
|
|
194
|
+
await pages.block(footer);
|
|
195
|
+
else
|
|
196
|
+
await pages.row(footer[0], keptFooterHeading);
|
|
197
|
+
}
|
|
198
|
+
await pages.endTable();
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "image": {
|
|
202
|
+
const { width: displayWidth, height: displayHeight, image: inputImage, caption: inputCaption } = block;
|
|
203
|
+
const capturedCaption = snapshotCaption(inputCaption), alternative = graphicAlternative(block, pages.settings), alignment = graphicAlignment(block.alignment);
|
|
204
|
+
if (capturedCaption && alternative === undefined)
|
|
205
|
+
throw new TypeError("A captioned PDF graphic requires alternativeText and cannot be decorative.");
|
|
206
|
+
const image = await images.prepare(inputImage), caption = await prepareCaption(capturedCaption);
|
|
207
|
+
const width = displayWidth ?? (displayHeight === undefined ? image.width * .75 : displayHeight * image.width / image.height);
|
|
208
|
+
const height = displayHeight ?? width * image.height / image.width;
|
|
209
|
+
if (!Number.isFinite(width) || width <= 0 || !Number.isFinite(height) || height <= 0)
|
|
210
|
+
throw new RangeError("Image display dimensions must be finite and positive.");
|
|
211
|
+
await flushParagraphs((caption?.height ?? 0) + pages.graphicHeight(width, height));
|
|
212
|
+
pages.registerXObject(image.name, image.id);
|
|
213
|
+
await pages.graphic(width, height, alignment, (x, y, w, h) => "q " + [w, 0, 0, h, x, y].map(n).join(" ") + " cm /" + image.name + " Do Q\n", alternative, caption);
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
case "vector": {
|
|
217
|
+
const capturedCaption = snapshotCaption(block.caption), alternative = graphicAlternative(block, pages.settings), alignment = graphicAlignment(block.alignment);
|
|
218
|
+
if (capturedCaption && alternative === undefined)
|
|
219
|
+
throw new TypeError("A captioned PDF graphic requires alternativeText and cannot be decorative.");
|
|
220
|
+
const path = await vectorCommands(block, pages.settings, commands), caption = await prepareCaption(capturedCaption);
|
|
221
|
+
commands += path.count;
|
|
222
|
+
await flushParagraphs((caption?.height ?? 0) + pages.graphicHeight(path.width, path.height));
|
|
223
|
+
await pages.graphic(path.width, path.height, alignment, (x, y, w, h) => "q " + [w / path.width, 0, 0, -h / path.height, x, y + h].map(n).join(" ") +
|
|
224
|
+
" cm 0 0 " + n(path.width) + " " + n(path.height) + " re W n\n" + path.content + "Q\n", alternative, caption);
|
|
225
|
+
break;
|
|
226
|
+
}
|
|
227
|
+
case "drawing": {
|
|
228
|
+
checkDrawingBlock(block);
|
|
229
|
+
const inputDrawing = block.drawing, capturedCaption = snapshotCaption(block.caption), alternative = graphicAlternative(block, pages.settings), alignment = graphicAlignment(block.alignment);
|
|
230
|
+
if (capturedCaption && alternative === undefined)
|
|
231
|
+
throw new TypeError("A captioned PDF graphic requires alternativeText and cannot be decorative.");
|
|
232
|
+
const drawing = await prepareDrawing(inputDrawing, pages.settings, fonts, { commands, elements: drawingElements }), caption = await prepareCaption(capturedCaption);
|
|
233
|
+
commands += drawing.commands;
|
|
234
|
+
drawingElements += drawing.elements;
|
|
235
|
+
await flushParagraphs((caption?.height ?? 0) + pages.graphicHeight(drawing.width, drawing.height));
|
|
236
|
+
await pages.graphic(drawing.width, drawing.height, alignment, (x, y, w, h) => "q " + [w / drawing.width, 0, 0, -h / drawing.height, x, y + h].map(n).join(" ") +
|
|
237
|
+
" cm 0 0 " + n(drawing.width) + " " + n(drawing.height) + " re W n\n" + drawing.content + "Q\n", alternative, caption);
|
|
238
|
+
break;
|
|
239
|
+
}
|
|
240
|
+
default: throw new TypeError("Unsupported PDF document block.");
|
|
241
|
+
}
|
|
242
|
+
if (taskYieldDue())
|
|
243
|
+
await pause();
|
|
244
|
+
checkAbort(options.signal);
|
|
245
|
+
});
|
|
246
|
+
await flushParagraphs();
|
|
247
|
+
if (options.messageBottom)
|
|
248
|
+
await pages.paragraph(options.messageBottom);
|
|
249
|
+
await session.finish();
|
|
250
|
+
notifyProgress(options.onProgress, { phase: "complete", rows, bytes: objects.bytes });
|
|
251
|
+
checkAbort(options.signal);
|
|
252
|
+
return { rows, columns, bytes: objects.bytes };
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
finally {
|
|
256
|
+
parentSignal?.removeEventListener("abort", abort);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function graphicAlignment(value) {
|
|
260
|
+
if (value !== undefined && !["left", "center", "right"].includes(value))
|
|
261
|
+
throw new TypeError("Invalid graphic alignment.");
|
|
262
|
+
return value ?? "left";
|
|
263
|
+
}
|
|
264
|
+
/** Per-table overrides cannot change the document's shared fonts, geometry or resource policy. */
|
|
265
|
+
function captureTableOptions(options) {
|
|
266
|
+
if (!options || typeof options !== "object" || Object.keys(options).some(key => !tableOptionFields.includes(key)))
|
|
267
|
+
throw new TypeError("Table options support columns, headings, footers, widths and presentation; document settings belong on writePdfDocument.");
|
|
268
|
+
const captured = captureFields(options, tableOptionFields);
|
|
269
|
+
return { ...captured, ...(captured.wideTable === undefined ? {} : { wideTable: captureWideTable(captured.wideTable) }) };
|
|
270
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import type { PdfCaption, PdfPathCommand } from "./document-types.js";
|
|
2
|
+
/** Versioned portable drawing. Lengths are points, with a top-left origin and downward y axis. */
|
|
3
|
+
export interface PdfDrawing {
|
|
4
|
+
readonly version: 1;
|
|
5
|
+
readonly width: number;
|
|
6
|
+
readonly height: number;
|
|
7
|
+
readonly elements: readonly PdfDrawingElement[];
|
|
8
|
+
}
|
|
9
|
+
/** One painted path; colors are six-digit RGB hex strings. */
|
|
10
|
+
export interface PdfDrawingPath {
|
|
11
|
+
readonly kind: "path";
|
|
12
|
+
readonly commands: readonly PdfPathCommand[];
|
|
13
|
+
readonly fill?: string;
|
|
14
|
+
readonly stroke?: string;
|
|
15
|
+
readonly strokeWidth?: number;
|
|
16
|
+
readonly fillRule?: "nonzero" | "evenodd";
|
|
17
|
+
readonly lineCap?: "butt" | "round" | "square";
|
|
18
|
+
readonly lineJoin?: "miter" | "round" | "bevel";
|
|
19
|
+
}
|
|
20
|
+
/** A face explicitly supplied in the PDF document's embedded static TrueType family. */
|
|
21
|
+
export type PdfDrawingFont = "regular" | "bold" | "italic" | "boldItalic";
|
|
22
|
+
/** Fixed baseline text. No wrapping, shaping, font lookup, synthetic emphasis or line breaks. */
|
|
23
|
+
export interface PdfDrawingText {
|
|
24
|
+
readonly kind: "text";
|
|
25
|
+
readonly text: string;
|
|
26
|
+
readonly x: number;
|
|
27
|
+
readonly y: number;
|
|
28
|
+
readonly font: PdfDrawingFont;
|
|
29
|
+
readonly fontSize: number;
|
|
30
|
+
readonly color: string;
|
|
31
|
+
}
|
|
32
|
+
export interface PdfDrawingClip {
|
|
33
|
+
readonly x: number;
|
|
34
|
+
readonly y: number;
|
|
35
|
+
readonly width: number;
|
|
36
|
+
readonly height: number;
|
|
37
|
+
}
|
|
38
|
+
/** Ordered children, optionally restricted by an axis-aligned rectangular clip. */
|
|
39
|
+
export interface PdfDrawingGroup {
|
|
40
|
+
readonly kind: "group";
|
|
41
|
+
readonly clip?: PdfDrawingClip;
|
|
42
|
+
readonly elements: readonly PdfDrawingElement[];
|
|
43
|
+
}
|
|
44
|
+
export type PdfDrawingElement = PdfDrawingPath | PdfDrawingText | PdfDrawingGroup;
|
|
45
|
+
/** An indivisible drawing, automatically scaled down to fit the printable page. */
|
|
46
|
+
export interface PdfDrawingBlock {
|
|
47
|
+
readonly kind: "drawing";
|
|
48
|
+
readonly drawing: PdfDrawing;
|
|
49
|
+
readonly alignment?: "left" | "center" | "right";
|
|
50
|
+
readonly alternativeText?: string;
|
|
51
|
+
readonly decorative?: boolean;
|
|
52
|
+
readonly caption?: PdfCaption;
|
|
53
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { PdfFont } from "./font.js";
|
|
2
|
+
import type { PdfObjects } from "./objects.js";
|
|
3
|
+
import type { PdfFonts } from "./types.js";
|
|
4
|
+
import type { TrueTypeFont } from "./truetype.js";
|
|
5
|
+
export declare class PdfFontResource {
|
|
6
|
+
readonly index: number;
|
|
7
|
+
readonly bold: boolean;
|
|
8
|
+
readonly italic: boolean;
|
|
9
|
+
readonly font: PdfFont | undefined;
|
|
10
|
+
private readonly shared?;
|
|
11
|
+
private readonly signal?;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly object: number;
|
|
14
|
+
readonly program: TrueTypeFont | undefined;
|
|
15
|
+
readonly syntheticBold: boolean;
|
|
16
|
+
readonly syntheticItalic: boolean;
|
|
17
|
+
private readonly mappings;
|
|
18
|
+
private readonly standardWidths;
|
|
19
|
+
constructor(index: number, bold: boolean, italic: boolean, font: PdfFont | undefined, objects: PdfObjects, syntheticBold: boolean, syntheticItalic: boolean, shared?: PdfFontResource | undefined, signal?: AbortSignal | undefined);
|
|
20
|
+
width(scalar: number): number;
|
|
21
|
+
lineHeight(size: number): number;
|
|
22
|
+
encode(text: string): string;
|
|
23
|
+
/** Register an already validated shaped glyph. A glyph may need distinct CIDs for different logical clusters. */
|
|
24
|
+
encodeGlyph(glyph: number, text: string): string;
|
|
25
|
+
write(objects: PdfObjects, compression: boolean): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
export declare class PdfFontResources {
|
|
28
|
+
private readonly objects;
|
|
29
|
+
private readonly fonts?;
|
|
30
|
+
private readonly maxBytes;
|
|
31
|
+
private readonly signal?;
|
|
32
|
+
readonly resources: PdfFontResource[];
|
|
33
|
+
private readonly selections;
|
|
34
|
+
private readonly programs;
|
|
35
|
+
private readonly explicitSelections;
|
|
36
|
+
private readonly registered;
|
|
37
|
+
private fontBytes;
|
|
38
|
+
constructor(objects: PdfObjects, fonts?: PdfFonts | undefined, maxBytes?: number, signal?: AbortSignal | undefined);
|
|
39
|
+
/** The same operation budget covers unused declared faces and every explicit run face, once per identity. */
|
|
40
|
+
registerFont(font: PdfFont): void;
|
|
41
|
+
select(bold?: boolean, italic?: boolean, explicit?: PdfFont): PdfFontResource;
|
|
42
|
+
dictionary(): string;
|
|
43
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { NotSupportedError, OfficeIMOError } from "../core/errors.js";
|
|
2
|
+
import { fontProgram } from "./font.js";
|
|
3
|
+
import { resolveScalar, selectFont, standardCode, validateFonts } from "./font-support.js";
|
|
4
|
+
import { pdfNumber, unicodeHex } from "./objects.js";
|
|
5
|
+
import { checkAbort, pause, taskYieldDue } from "../core/iteration.js";
|
|
6
|
+
const standardHexBytes = Object.freeze(Array.from({ length: 256 }, (_, byte) => byte.toString(16).padStart(2, "0")));
|
|
7
|
+
export class PdfFontResource {
|
|
8
|
+
index;
|
|
9
|
+
bold;
|
|
10
|
+
italic;
|
|
11
|
+
font;
|
|
12
|
+
shared;
|
|
13
|
+
signal;
|
|
14
|
+
name;
|
|
15
|
+
object;
|
|
16
|
+
program;
|
|
17
|
+
syntheticBold;
|
|
18
|
+
syntheticItalic;
|
|
19
|
+
mappings = new Map();
|
|
20
|
+
standardWidths = new Map();
|
|
21
|
+
constructor(index, bold, italic, font, objects, syntheticBold, syntheticItalic, shared, signal) {
|
|
22
|
+
this.index = index;
|
|
23
|
+
this.bold = bold;
|
|
24
|
+
this.italic = italic;
|
|
25
|
+
this.font = font;
|
|
26
|
+
this.shared = shared;
|
|
27
|
+
this.signal = signal;
|
|
28
|
+
this.name = shared?.name ?? "F" + index;
|
|
29
|
+
this.object = shared?.object ?? objects.reserve();
|
|
30
|
+
this.program = font ? fontProgram(font) : undefined;
|
|
31
|
+
this.syntheticBold = syntheticBold;
|
|
32
|
+
this.syntheticItalic = syntheticItalic;
|
|
33
|
+
}
|
|
34
|
+
width(scalar) {
|
|
35
|
+
if (this.shared)
|
|
36
|
+
return this.shared.width(scalar);
|
|
37
|
+
const existing = this.mappings.get(scalar);
|
|
38
|
+
if (existing)
|
|
39
|
+
return existing.width;
|
|
40
|
+
const standard = this.standardWidths.get(scalar);
|
|
41
|
+
if (standard !== undefined)
|
|
42
|
+
return standard;
|
|
43
|
+
const resolved = resolveScalar(scalar, this.program, this.bold);
|
|
44
|
+
if (!resolved.supported) {
|
|
45
|
+
if (resolved.code === "unpaired-surrogate")
|
|
46
|
+
throw new TypeError(resolved.message);
|
|
47
|
+
throw new NotSupportedError(resolved.message);
|
|
48
|
+
}
|
|
49
|
+
if (!this.program) {
|
|
50
|
+
this.standardWidths.set(scalar, resolved.width);
|
|
51
|
+
return resolved.width;
|
|
52
|
+
}
|
|
53
|
+
if (this.mappings.size >= 65535)
|
|
54
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "A PDF font supports at most 65,535 distinct Unicode scalars.");
|
|
55
|
+
const mapping = { cid: this.mappings.size + 1, glyph: resolved.glyph, width: resolved.width, text: String.fromCodePoint(scalar) };
|
|
56
|
+
this.mappings.set(scalar, mapping);
|
|
57
|
+
return mapping.width;
|
|
58
|
+
}
|
|
59
|
+
lineHeight(size) {
|
|
60
|
+
return Math.max(1.3, this.program ? (this.program.ascent - this.program.descent) / 1000 + .1 : 1.3) * size;
|
|
61
|
+
}
|
|
62
|
+
encode(text) {
|
|
63
|
+
if (this.shared)
|
|
64
|
+
return this.shared.encode(text);
|
|
65
|
+
let hex = "";
|
|
66
|
+
for (const char of text) {
|
|
67
|
+
const cp = char.codePointAt(0);
|
|
68
|
+
this.width(cp);
|
|
69
|
+
hex += this.program ? this.mappings.get(cp).cid.toString(16).padStart(4, "0") : standardHexBytes[standardCode(cp)];
|
|
70
|
+
}
|
|
71
|
+
return "<" + hex + ">";
|
|
72
|
+
}
|
|
73
|
+
/** Register an already validated shaped glyph. A glyph may need distinct CIDs for different logical clusters. */
|
|
74
|
+
encodeGlyph(glyph, text) {
|
|
75
|
+
if (this.shared)
|
|
76
|
+
return this.shared.encodeGlyph(glyph, text);
|
|
77
|
+
if (!this.program || !Number.isSafeInteger(glyph) || glyph <= 0 || glyph >= this.program.glyphCount)
|
|
78
|
+
throw new TypeError("Invalid shaped TrueType glyph.");
|
|
79
|
+
const key = glyph + ":" + text;
|
|
80
|
+
let mapping = this.mappings.get(key);
|
|
81
|
+
if (!mapping) {
|
|
82
|
+
if (this.mappings.size >= 65535)
|
|
83
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "A PDF font supports at most 65,535 distinct CID mappings.");
|
|
84
|
+
mapping = { cid: this.mappings.size + 1, glyph, width: this.program.widths[glyph], text };
|
|
85
|
+
this.mappings.set(key, mapping);
|
|
86
|
+
}
|
|
87
|
+
return "<" + mapping.cid.toString(16).padStart(4, "0") + ">";
|
|
88
|
+
}
|
|
89
|
+
async write(objects, compression) {
|
|
90
|
+
checkAbort(this.signal);
|
|
91
|
+
if (!this.program) {
|
|
92
|
+
const name = "Helvetica" + (this.bold && this.italic ? "-BoldOblique" : this.bold ? "-Bold" : this.italic ? "-Oblique" : "");
|
|
93
|
+
await objects.object(this.object, "<< /Type /Font /Subtype /Type1 /BaseFont /" + name + " /Encoding /WinAnsiEncoding >>");
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
const program = this.program, entries = [...this.mappings], glyphs = new Set(entries.map(([, m]) => m.glyph));
|
|
97
|
+
const fontName = (program.canSubset ? "OIMOAA+" : "") + "OfficeIMOFont" + this.index;
|
|
98
|
+
const file = objects.reserve(), descriptor = objects.reserve(), cidFont = objects.reserve(), map = objects.reserve(), unicode = objects.reserve();
|
|
99
|
+
const characters = new Map();
|
|
100
|
+
for (const [key, mapping] of entries) {
|
|
101
|
+
if (typeof key === "number")
|
|
102
|
+
characters.set(key, mapping.glyph);
|
|
103
|
+
else
|
|
104
|
+
for (const character of mapping.text) {
|
|
105
|
+
const scalar = character.codePointAt(0), glyph = program.glyph(scalar);
|
|
106
|
+
if (glyph) {
|
|
107
|
+
characters.set(scalar, glyph);
|
|
108
|
+
glyphs.add(glyph);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
const subset = await program.subsetCooperatively(glyphs, characters, this.signal);
|
|
113
|
+
await objects.stream(file, subset, "/Length1 " + subset.length, compression, true);
|
|
114
|
+
await objects.object(descriptor, "<< /Type /FontDescriptor /FontName /" + fontName + " /Flags 32 /FontBBox [" + program.bbox.map(pdfNumber).join(" ") +
|
|
115
|
+
"] /ItalicAngle 0 /Ascent " + pdfNumber(program.ascent) + " /Descent " + pdfNumber(program.descent) + " /CapHeight " + pdfNumber(program.ascent) + " /StemV 80 /FontFile2 " + file + " 0 R >>");
|
|
116
|
+
const mapping = new Uint8Array((entries.length + 1) * 2), view = new DataView(mapping.buffer);
|
|
117
|
+
for (let i = 0; i < entries.length; i++) {
|
|
118
|
+
const entry = entries[i][1];
|
|
119
|
+
view.setUint16(entry.cid * 2, entry.glyph);
|
|
120
|
+
if ((i & 1023) === 1023) {
|
|
121
|
+
if (taskYieldDue())
|
|
122
|
+
await pause();
|
|
123
|
+
checkAbort(this.signal);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
await objects.stream(map, mapping, "", compression, true);
|
|
127
|
+
await objects.object(cidFont, "<< /Type /Font /Subtype /CIDFontType2 /BaseFont /" + fontName +
|
|
128
|
+
" /CIDSystemInfo << /Registry (Adobe) /Ordering (Identity) /Supplement 0 >> /FontDescriptor " + descriptor + " 0 R /CIDToGIDMap " + map + " 0 R" +
|
|
129
|
+
(entries.length ? " /W [1 [" + entries.map(([, m]) => pdfNumber(m.width)).join(" ") + "]]" : "") + " >>");
|
|
130
|
+
let cmap = "/CIDInit /ProcSet findresource begin\n12 dict begin\nbegincmap\n/CIDSystemInfo << /Registry (Adobe) /Ordering (UCS) /Supplement 0 >> def\n/CMapName /OfficeIMOUnicode def\n/CMapType 2 def\n1 begincodespacerange\n<0000> <FFFF>\nendcodespacerange\n";
|
|
131
|
+
const mapped = entries.filter(([, mapping]) => mapping.text.length);
|
|
132
|
+
for (let i = 0; i < mapped.length; i += 100) {
|
|
133
|
+
const batch = mapped.slice(i, i + 100);
|
|
134
|
+
cmap += batch.length + " beginbfchar\n";
|
|
135
|
+
for (const [, m] of batch)
|
|
136
|
+
cmap += "<" + m.cid.toString(16).padStart(4, "0") + "> <" + unicodeHex(m.text) + ">\n";
|
|
137
|
+
cmap += "endbfchar\n";
|
|
138
|
+
if (taskYieldDue())
|
|
139
|
+
await pause();
|
|
140
|
+
checkAbort(this.signal);
|
|
141
|
+
}
|
|
142
|
+
cmap += "endcmap\nCMapName currentdict /CMap defineresource pop\nend\nend\n";
|
|
143
|
+
await objects.stream(unicode, cmap, "", compression);
|
|
144
|
+
await objects.object(this.object, "<< /Type /Font /Subtype /Type0 /BaseFont /" + fontName + " /Encoding /Identity-H /DescendantFonts [" + cidFont + " 0 R] /ToUnicode " + unicode + " 0 R >>");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
export class PdfFontResources {
|
|
148
|
+
objects;
|
|
149
|
+
fonts;
|
|
150
|
+
maxBytes;
|
|
151
|
+
signal;
|
|
152
|
+
resources = [];
|
|
153
|
+
selections = new Map();
|
|
154
|
+
programs = new Map();
|
|
155
|
+
explicitSelections = new Map();
|
|
156
|
+
registered = new Set();
|
|
157
|
+
fontBytes = 0;
|
|
158
|
+
constructor(objects, fonts, maxBytes = 16 * 1024 * 1024, signal) {
|
|
159
|
+
this.objects = objects;
|
|
160
|
+
this.fonts = fonts;
|
|
161
|
+
this.maxBytes = maxBytes;
|
|
162
|
+
this.signal = signal;
|
|
163
|
+
validateFonts(fonts, maxBytes);
|
|
164
|
+
for (const font of Object.values(fonts ?? {}))
|
|
165
|
+
this.registerFont(font);
|
|
166
|
+
}
|
|
167
|
+
/** The same operation budget covers unused declared faces and every explicit run face, once per identity. */
|
|
168
|
+
registerFont(font) {
|
|
169
|
+
if (this.registered.has(font))
|
|
170
|
+
return;
|
|
171
|
+
const bytes = fontProgram(font).bytes.length;
|
|
172
|
+
if (this.fontBytes + bytes > this.maxBytes)
|
|
173
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxFontBytes exceeded.");
|
|
174
|
+
this.fontBytes += bytes;
|
|
175
|
+
this.registered.add(font);
|
|
176
|
+
}
|
|
177
|
+
select(bold = false, italic = false, explicit) {
|
|
178
|
+
if (explicit)
|
|
179
|
+
this.registerFont(explicit);
|
|
180
|
+
let selections = this.selections;
|
|
181
|
+
if (explicit) {
|
|
182
|
+
const found = this.explicitSelections.get(explicit);
|
|
183
|
+
selections = found ?? new Map();
|
|
184
|
+
if (!found)
|
|
185
|
+
this.explicitSelections.set(explicit, selections);
|
|
186
|
+
}
|
|
187
|
+
const key = String(bold) + String(italic), existing = selections.get(key);
|
|
188
|
+
if (existing)
|
|
189
|
+
return existing;
|
|
190
|
+
const { font, syntheticBold, syntheticItalic } = explicit ? { font: explicit, syntheticBold: bold, syntheticItalic: italic } : selectFont(this.fonts, bold, italic);
|
|
191
|
+
const shared = font ? this.programs.get(font) : undefined;
|
|
192
|
+
const resource = new PdfFontResource(this.resources.length + 1, bold, italic, font, this.objects, syntheticBold, syntheticItalic, shared, this.signal);
|
|
193
|
+
if (!shared) {
|
|
194
|
+
this.resources.push(resource);
|
|
195
|
+
if (font)
|
|
196
|
+
this.programs.set(font, resource);
|
|
197
|
+
}
|
|
198
|
+
selections.set(key, resource);
|
|
199
|
+
return resource;
|
|
200
|
+
}
|
|
201
|
+
dictionary() { return "<< " + this.resources.map(f => "/" + f.name + " " + f.object + " 0 R").join(" ") + " >>"; }
|
|
202
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { OfficeIMOError } from "../core/errors.js";
|
|
2
|
+
import { PdfFont, fontProgram } from "./font.js";
|
|
3
|
+
import { helveticaWidths } from "./standard-widths.js";
|
|
4
|
+
const winAnsiExtras = new Map([0x20ac, 0, 0x201a, 0x192, 0x201e, 0x2026, 0x2020, 0x2021, 0x2c6, 0x2030, 0x160, 0x2039, 0x152, 0, 0x17d, 0, 0, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x2dc, 0x2122, 0x161, 0x203a, 0x153, 0, 0x17e, 0x178].map((cp, i) => [cp, i + 128]));
|
|
5
|
+
/** @internal Called after scalar support succeeds when encoding a standard-font run. */
|
|
6
|
+
export function standardCode(scalar) {
|
|
7
|
+
if ((scalar >= 32 && scalar <= 126) || (scalar >= 160 && scalar <= 255))
|
|
8
|
+
return scalar;
|
|
9
|
+
return scalar ? winAnsiExtras.get(scalar) : undefined;
|
|
10
|
+
}
|
|
11
|
+
/** @internal One encoding/glyph decision for the writer and public text preflight. */
|
|
12
|
+
export function resolveScalar(scalar, program, bold) {
|
|
13
|
+
if (scalar >= 0xd800 && scalar <= 0xdfff)
|
|
14
|
+
return { supported: false, code: "unpaired-surrogate", message: "PDF text contains an unpaired UTF-16 surrogate." };
|
|
15
|
+
if (scalar < 32 || (scalar >= 127 && scalar < 160))
|
|
16
|
+
return { supported: false, code: "unsupported-control", message: "PDF text contains an unsupported control character." };
|
|
17
|
+
const character = String.fromCodePoint(scalar);
|
|
18
|
+
if (/[\p{Mark}\p{Format}]/u.test(character) || !/[\p{Script=Latin}\p{Script=Greek}\p{Script=Cyrillic}\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Common}]/u.test(character) ||
|
|
19
|
+
(scalar >= 0x1100 && scalar <= 0x11ff) || (scalar >= 0x1f3fb && scalar <= 0x1f3ff))
|
|
20
|
+
return { supported: false, code: "unsupported-layout", message: "This PDF table writer supports Unicode scalar layout; complex shaping, combining sequences and bidirectional text require a shaping-capable writer." };
|
|
21
|
+
if (!program) {
|
|
22
|
+
if (standardCode(scalar) === undefined)
|
|
23
|
+
return { supported: false, code: "embedded-font-required", message: "Text U+" + scalar.toString(16).toUpperCase() + " needs an embedded Unicode TrueType font; supply fonts.regular." };
|
|
24
|
+
const metric = helveticaWidths.get(scalar === 160 ? 32 : scalar === 173 ? 45 : scalar);
|
|
25
|
+
if (!metric)
|
|
26
|
+
return { supported: false, code: "missing-glyph", message: "Standard font has no metric for this character." };
|
|
27
|
+
return { supported: true, width: metric[bold ? 1 : 0] };
|
|
28
|
+
}
|
|
29
|
+
let glyph = program.glyph(scalar);
|
|
30
|
+
// Reuse only an inkless thin-space glyph; the source scalar keeps its own CID/ToUnicode.
|
|
31
|
+
if (!glyph && scalar === 0x202f) {
|
|
32
|
+
const thin = program.glyph(0x2009);
|
|
33
|
+
if (thin && program.offsets[thin] === program.offsets[thin + 1])
|
|
34
|
+
glyph = thin;
|
|
35
|
+
}
|
|
36
|
+
if (!glyph)
|
|
37
|
+
return { supported: false, code: "missing-glyph", message: "The supplied font has no glyph for U+" + scalar.toString(16).toUpperCase() + "." };
|
|
38
|
+
return { supported: true, glyph, width: program.widths[glyph] };
|
|
39
|
+
}
|
|
40
|
+
/** @internal Family validation is shared; preflight does not impose a document's byte budget. */
|
|
41
|
+
export function validateFonts(fonts, maxBytes) {
|
|
42
|
+
if (!fonts)
|
|
43
|
+
return;
|
|
44
|
+
if (!(fonts.regular instanceof PdfFont))
|
|
45
|
+
throw new TypeError("fonts.regular must be a PdfFont.");
|
|
46
|
+
const unique = new Set();
|
|
47
|
+
for (const [key, font] of Object.entries(fonts)) {
|
|
48
|
+
if (!["regular", "bold", "italic", "boldItalic"].includes(key) || !(font instanceof PdfFont))
|
|
49
|
+
throw new TypeError("Invalid PDF font family.");
|
|
50
|
+
unique.add(font);
|
|
51
|
+
}
|
|
52
|
+
if (maxBytes !== undefined && [...unique].reduce((n, f) => n + fontProgram(f).bytes.length, 0) > maxBytes)
|
|
53
|
+
throw new OfficeIMOError("RESOURCE_LIMIT", "maxFontBytes exceeded.");
|
|
54
|
+
}
|
|
55
|
+
/** @internal Missing emphasis faces select regular glyphs with the writer's synthetic styling. */
|
|
56
|
+
export function selectFont(fonts, bold, italic) {
|
|
57
|
+
const exact = fonts && (bold && italic ? fonts.boldItalic : bold ? fonts.bold : italic ? fonts.italic : fonts.regular);
|
|
58
|
+
const font = exact ?? fonts?.regular;
|
|
59
|
+
return { font, syntheticBold: !!font && bold && !exact, syntheticItalic: !!font && italic && !exact };
|
|
60
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/** Intake limits for asynchronous font preparation; the source is copied before yielding. */
|
|
2
|
+
export interface PdfFontPreparationOptions {
|
|
3
|
+
readonly signal?: AbortSignal;
|
|
4
|
+
/** Defaults to 16 MiB; cannot exceed the constructor's 64 MiB hard ceiling. */
|
|
5
|
+
readonly maxBytes?: number;
|
|
6
|
+
}
|
|
7
|
+
/** Immutable, reusable static TrueType font. Bytes are copied; no fetch, DOM or installed-font lookup occurs. */
|
|
8
|
+
export declare class PdfFont {
|
|
9
|
+
static [Symbol.hasInstance](value: unknown): boolean;
|
|
10
|
+
private readonly brand;
|
|
11
|
+
constructor(bytes: Uint8Array);
|
|
12
|
+
/** Prepare the same immutable font with cooperative parsing and cancellation.
|
|
13
|
+
* The bounded source snapshot is synchronous so later host mutation cannot change the prepared font. */
|
|
14
|
+
static create(bytes: Uint8Array, options?: PdfFontPreparationOptions): Promise<PdfFont>;
|
|
15
|
+
/** Return an independent copy, for storage or delivery to another worker. */
|
|
16
|
+
toBytes(): Uint8Array;
|
|
17
|
+
}
|