reamkit 0.1.0-alpha.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (175) hide show
  1. package/README.md +106 -30
  2. package/dist/esm/{bidi → core/bidi}/algorithm.js +1 -1
  3. package/dist/esm/{bidi → core/bidi}/char-types.js +1 -1
  4. package/dist/esm/{bidi → core/bidi}/index.js +1 -1
  5. package/dist/esm/core/bytes.d.ts +1 -0
  6. package/dist/esm/core/bytes.js +11 -0
  7. package/dist/esm/core/converter/facade.d.ts +45 -0
  8. package/dist/esm/core/converter/facade.js +89 -0
  9. package/dist/esm/core/converter/index.d.ts +4 -0
  10. package/dist/esm/core/converter/project.d.ts +4 -0
  11. package/dist/esm/core/converter/project.js +16 -0
  12. package/dist/esm/core/converter/ream.d.ts +35 -0
  13. package/dist/esm/core/converter/ream.js +100 -0
  14. package/dist/esm/{crypto → core/crypto}/asn1.js +1 -1
  15. package/dist/esm/{crypto → core/crypto}/cms.js +1 -1
  16. package/dist/esm/core/document-model/index.d.ts +1 -0
  17. package/dist/esm/{document-model → core/document-model}/types.d.ts +57 -41
  18. package/dist/esm/{ooxml → core}/drawingml/chart-parser.d.ts +1 -1
  19. package/dist/esm/{ooxml → core}/drawingml/chart-parser.js +6 -29
  20. package/dist/esm/{ooxml → core}/drawingml/colors.d.ts +3 -0
  21. package/dist/esm/{ooxml → core}/drawingml/colors.js +32 -2
  22. package/dist/esm/{ooxml → core}/drawingml/theme-parser.js +2 -2
  23. package/dist/esm/{font → core/font}/arabic-joining.js +1 -1
  24. package/dist/esm/{font → core/font}/binary-reader.js +1 -1
  25. package/dist/esm/{font → core/font}/font-registry.d.ts +1 -0
  26. package/dist/esm/{font → core/font}/font-registry.js +17 -16
  27. package/dist/esm/{font → core/font}/index.d.ts +1 -0
  28. package/dist/esm/{font → core/font}/opentype-layout.js +1 -1
  29. package/dist/esm/{font → core/font}/ttf-parser.js +1 -1
  30. package/dist/esm/{font → core/font}/ttf-subset.js +1 -1
  31. package/dist/esm/core/fonts/provider.d.ts +60 -0
  32. package/dist/esm/core/fonts/provider.js +163 -0
  33. package/dist/esm/{fonts → core/fonts}/remote-fonts.js +1 -1
  34. package/dist/esm/{hyphenation → core/hyphenation}/index.js +1 -1
  35. package/dist/esm/{hyphenation → core/hyphenation}/liang.js +1 -1
  36. package/dist/esm/{hyphenation → core/hyphenation}/patterns-en-us.js +1 -1
  37. package/dist/esm/{hyphenation → core/hyphenation}/patterns-ru.js +1 -1
  38. package/dist/esm/core/ir/adapters.d.ts +37 -0
  39. package/dist/esm/core/ir/features.d.ts +25 -0
  40. package/dist/esm/core/ir/features.js +24 -0
  41. package/dist/esm/core/ir/flow.d.ts +24 -0
  42. package/dist/esm/core/ir/index.d.ts +11 -0
  43. package/dist/esm/core/ir/loss.d.ts +22 -0
  44. package/dist/esm/core/ir/loss.js +15 -0
  45. package/dist/esm/core/ir/resources.d.ts +16 -0
  46. package/dist/esm/core/ir/resources.js +50 -0
  47. package/dist/esm/core/ir/units.d.ts +16 -0
  48. package/dist/esm/core/ir/units.js +41 -0
  49. package/dist/esm/core/line-breaker/index.d.ts +2 -0
  50. package/dist/esm/{line-breaker → core/line-breaker}/knuth-plass.js +1 -1
  51. package/dist/esm/{numbering → core/numbering}/state.js +1 -1
  52. package/dist/esm/{opc → core/opc}/core-properties.js +1 -1
  53. package/dist/esm/{opc → core/opc}/package.js +1 -1
  54. package/dist/esm/{opc → core/opc}/relationship-types.js +1 -1
  55. package/dist/esm/{opc → core/opc}/relationships.js +1 -1
  56. package/dist/esm/{ooxml/wordproc → core}/po-helpers.js +1 -1
  57. package/dist/esm/{style-cascade → core/style-cascade}/index.d.ts +2 -2
  58. package/dist/esm/core/style-cascade/index.js +2 -0
  59. package/dist/esm/{style-cascade → core/style-cascade}/resolver.d.ts +1 -0
  60. package/dist/esm/{style-cascade → core/style-cascade}/resolver.js +14 -9
  61. package/dist/esm/{style-cascade → core/style-cascade}/types.d.ts +8 -7
  62. package/dist/esm/{style-cascade → core/style-cascade}/types.js +9 -8
  63. package/dist/esm/{ooxml/spreadsheet → excel}/cell-reference.js +1 -1
  64. package/dist/esm/{ooxml/spreadsheet → excel}/defined-name-ref.js +1 -1
  65. package/dist/esm/{ooxml/spreadsheet → excel}/index.d.ts +1 -1
  66. package/dist/esm/{ooxml/spreadsheet → excel}/number-format.js +1 -1
  67. package/dist/esm/excel/print-model.d.ts +18 -0
  68. package/dist/esm/{converter/xlsx-to-pdf.js → excel/print-model.js} +39 -148
  69. package/dist/esm/{ooxml/spreadsheet → excel}/shared-strings-parser.js +1 -1
  70. package/dist/esm/{ooxml/spreadsheet → excel}/styles-parser.js +1 -1
  71. package/dist/esm/{ooxml/spreadsheet → excel}/workbook-parser.js +1 -1
  72. package/dist/esm/{ooxml/spreadsheet → excel}/worksheet-parser.js +1 -1
  73. package/dist/esm/excel/xlsx-reader.d.ts +4 -0
  74. package/dist/esm/excel/xlsx-reader.js +91 -0
  75. package/dist/esm/{converter → excel}/xlsx-to-pdf.d.ts +13 -2
  76. package/dist/esm/excel/xlsx-to-pdf.js +69 -0
  77. package/dist/esm/index.d.ts +23 -8
  78. package/dist/esm/index.js +18 -11
  79. package/dist/esm/pdf/chart-geometry.d.ts +1 -1
  80. package/dist/esm/pdf/cid-font.d.ts +7 -1
  81. package/dist/esm/pdf/cid-font.js +33 -22
  82. package/dist/esm/pdf/image-xobject.d.ts +13 -0
  83. package/dist/esm/pdf/image-xobject.js +55 -54
  84. package/dist/esm/pdf/index.d.ts +2 -2
  85. package/dist/esm/pdf/math-layout.d.ts +1 -1
  86. package/dist/esm/pdf/preset-geometry.d.ts +1 -1
  87. package/dist/esm/pdf/signature.js +4 -4
  88. package/dist/esm/pdf/styled-page-emitter.d.ts +5 -0
  89. package/dist/esm/pdf/styled-page-emitter.js +542 -0
  90. package/dist/esm/pdf/styled-page-renderer.d.ts +189 -14
  91. package/dist/esm/pdf/styled-page-renderer.js +168 -654
  92. package/dist/esm/pdf/text-page-renderer.d.ts +1 -1
  93. package/dist/esm/svg/svg-writer.d.ts +8 -0
  94. package/dist/esm/svg/svg-writer.js +138 -0
  95. package/dist/esm/word/document-parser.d.ts +16 -0
  96. package/dist/esm/{ooxml/wordproc → word}/document-parser.js +55 -47
  97. package/dist/esm/word/docx-reader.d.ts +4 -0
  98. package/dist/esm/word/docx-reader.js +190 -0
  99. package/dist/esm/word/docx-to-pdf.d.ts +34 -0
  100. package/dist/esm/word/docx-to-pdf.js +112 -0
  101. package/dist/esm/{ooxml/wordproc → word}/drawing-parser.d.ts +10 -9
  102. package/dist/esm/{ooxml/wordproc → word}/drawing-parser.js +15 -42
  103. package/dist/esm/{ooxml/wordproc → word}/font-table.d.ts +2 -2
  104. package/dist/esm/{ooxml/wordproc → word}/font-table.js +2 -2
  105. package/dist/esm/{ooxml/wordproc → word}/index.d.ts +3 -2
  106. package/dist/esm/{ooxml/wordproc → word}/numbering-parser.d.ts +1 -1
  107. package/dist/esm/{ooxml/wordproc → word}/numbering-parser.js +1 -1
  108. package/dist/esm/{ooxml/math → word}/omml-parser.d.ts +2 -2
  109. package/dist/esm/{ooxml/math → word}/omml-parser.js +2 -2
  110. package/dist/esm/{ooxml/wordproc → word}/paragraph-properties.d.ts +1 -1
  111. package/dist/esm/{ooxml/wordproc → word}/paragraph-properties.js +9 -8
  112. package/dist/esm/{ooxml/wordproc → word}/po-to-flat.d.ts +1 -1
  113. package/dist/esm/{ooxml/wordproc → word}/po-to-flat.js +2 -2
  114. package/dist/esm/{ooxml/wordproc → word}/run-properties.d.ts +1 -1
  115. package/dist/esm/{ooxml/wordproc → word}/run-properties.js +3 -2
  116. package/dist/esm/{ooxml/wordproc → word}/settings-parser.js +2 -2
  117. package/dist/esm/word/styles-parser.d.ts +2 -0
  118. package/dist/esm/{ooxml/wordproc → word}/styles-parser.js +4 -7
  119. package/dist/esm/word/table-parser.d.ts +4 -0
  120. package/dist/esm/{ooxml/wordproc → word}/table-parser.js +80 -26
  121. package/dist/esm/{ooxml/wordproc → word}/xml-helpers.js +1 -1
  122. package/package.json +5 -6
  123. package/dist/esm/converter/docx-to-pdf.d.ts +0 -15
  124. package/dist/esm/converter/docx-to-pdf.js +0 -227
  125. package/dist/esm/converter/index.d.ts +0 -4
  126. package/dist/esm/converter/index.js +0 -2
  127. package/dist/esm/document-model/index.d.ts +0 -1
  128. package/dist/esm/line-breaker/index.d.ts +0 -2
  129. package/dist/esm/ooxml/index.d.ts +0 -1
  130. package/dist/esm/ooxml/wordproc/document-parser.d.ts +0 -9
  131. package/dist/esm/ooxml/wordproc/styles-parser.d.ts +0 -3
  132. package/dist/esm/ooxml/wordproc/table-parser.d.ts +0 -3
  133. /package/dist/esm/{bidi → core/bidi}/algorithm.d.ts +0 -0
  134. /package/dist/esm/{bidi → core/bidi}/char-types.d.ts +0 -0
  135. /package/dist/esm/{bidi → core/bidi}/index.d.ts +0 -0
  136. /package/dist/esm/{crypto → core/crypto}/asn1.d.ts +0 -0
  137. /package/dist/esm/{crypto → core/crypto}/cms.d.ts +0 -0
  138. /package/dist/esm/{crypto → core/crypto}/index.d.ts +0 -0
  139. /package/dist/esm/{crypto → core/crypto}/index.js +0 -0
  140. /package/dist/esm/{document-model → core/document-model}/index.js +0 -0
  141. /package/dist/esm/{ooxml → core}/drawingml/theme-parser.d.ts +0 -0
  142. /package/dist/esm/{font → core/font}/arabic-joining.d.ts +0 -0
  143. /package/dist/esm/{font → core/font}/binary-reader.d.ts +0 -0
  144. /package/dist/esm/{font → core/font}/opentype-layout.d.ts +0 -0
  145. /package/dist/esm/{font → core/font}/ttf-parser.d.ts +0 -0
  146. /package/dist/esm/{font → core/font}/ttf-subset.d.ts +0 -0
  147. /package/dist/esm/{fonts → core/fonts}/index.d.ts +0 -0
  148. /package/dist/esm/{fonts → core/fonts}/remote-fonts.d.ts +0 -0
  149. /package/dist/esm/{hyphenation → core/hyphenation}/index.d.ts +0 -0
  150. /package/dist/esm/{hyphenation → core/hyphenation}/liang.d.ts +0 -0
  151. /package/dist/esm/{hyphenation → core/hyphenation}/patterns-en-us.d.ts +0 -0
  152. /package/dist/esm/{hyphenation → core/hyphenation}/patterns-ru.d.ts +0 -0
  153. /package/dist/esm/{line-breaker → core/line-breaker}/knuth-plass.d.ts +0 -0
  154. /package/dist/esm/{numbering → core/numbering}/index.d.ts +0 -0
  155. /package/dist/esm/{numbering → core/numbering}/state.d.ts +0 -0
  156. /package/dist/esm/{opc → core/opc}/core-properties.d.ts +0 -0
  157. /package/dist/esm/{opc → core/opc}/index.d.ts +0 -0
  158. /package/dist/esm/{opc → core/opc}/index.js +0 -0
  159. /package/dist/esm/{opc → core/opc}/package.d.ts +0 -0
  160. /package/dist/esm/{opc → core/opc}/relationship-types.d.ts +0 -0
  161. /package/dist/esm/{opc → core/opc}/relationships.d.ts +0 -0
  162. /package/dist/esm/{ooxml/wordproc → core}/po-helpers.d.ts +0 -0
  163. /package/dist/esm/{ooxml/spreadsheet → excel}/cell-reference.d.ts +0 -0
  164. /package/dist/esm/{ooxml/spreadsheet → excel}/defined-name-ref.d.ts +0 -0
  165. /package/dist/esm/{ooxml/spreadsheet → excel}/index.js +0 -0
  166. /package/dist/esm/{ooxml/spreadsheet → excel}/number-format.d.ts +0 -0
  167. /package/dist/esm/{ooxml/spreadsheet → excel}/shared-strings-parser.d.ts +0 -0
  168. /package/dist/esm/{ooxml/spreadsheet → excel}/styles-parser.d.ts +0 -0
  169. /package/dist/esm/{ooxml/spreadsheet → excel}/workbook-parser.d.ts +0 -0
  170. /package/dist/esm/{ooxml/spreadsheet → excel}/worksheet-parser.d.ts +0 -0
  171. /package/dist/esm/{ooxml/wordproc → word}/index.js +0 -0
  172. /package/dist/esm/{ooxml/wordproc → word}/settings-parser.d.ts +0 -0
  173. /package/dist/esm/{ooxml/wordproc → word}/text-extractor.d.ts +0 -0
  174. /package/dist/esm/{ooxml/wordproc → word}/text-extractor.js +0 -0
  175. /package/dist/esm/{ooxml/wordproc → word}/xml-helpers.d.ts +0 -0
@@ -0,0 +1,542 @@
1
+ import { dict, name, ref, stream, unicodeString } from "./objects.js";
2
+ import { embedTtfFont } from "./cid-font.js";
3
+ import { reorderVisual } from "../core/bidi/algorithm.js";
4
+ import { reverseByCodePoint } from "../core/bidi/index.js";
5
+ import { emitVectorShape } from "./vector-graphics.js";
6
+ import { addImage } from "./image-xobject.js";
7
+ import { embedAssociatedFile } from "./embedded-file.js";
8
+ import { buildSrgbIccProfile } from "./icc-profile.js";
9
+ import { addSignaturePlaceholder } from "./signature.js";
10
+ import { buildXmpPacket } from "./xmp.js";
11
+ import { paintPlan } from "./styled-page-renderer.js";
12
+ //#region src/pdf/styled-page-emitter.ts
13
+ var encoder = new TextEncoder();
14
+ function emitStyledPdf(laid, options, doc) {
15
+ const { pages: renderedPages, structBuilder } = laid;
16
+ const { sectionCtxs, pdfaProfile, tagged } = laid;
17
+ const embeddedFonts = embedFontResources(doc, laid);
18
+ const embeddedImages = embedImageResources(doc, laid);
19
+ const pagesDict = dict({
20
+ Type: name("Pages"),
21
+ Count: 0,
22
+ Kids: []
23
+ });
24
+ const pagesRef = doc.add(pagesDict);
25
+ let firstPageDict;
26
+ const fontResourceDict = buildFontResourceDict(laid.fontResources, embeddedFonts);
27
+ const xobjectResourceDict = buildXObjectResourceDict(laid.imageResources, embeddedImages);
28
+ const resourcesDict = dict({
29
+ Font: fontResourceDict,
30
+ ...xobjectResourceDict ? { XObject: xobjectResourceDict } : {}
31
+ });
32
+ let pdfaIccRef;
33
+ let transparencyGroup;
34
+ if (pdfaProfile) {
35
+ pdfaIccRef = doc.add(stream({
36
+ N: 3,
37
+ Alternate: name("DeviceRGB")
38
+ }, buildSrgbIccProfile()));
39
+ if (pdfaProfile.part >= 2) transparencyGroup = dict({
40
+ S: name("Transparency"),
41
+ CS: [name("ICCBased"), ref(pdfaIccRef.id)]
42
+ });
43
+ }
44
+ const pageRefs = [];
45
+ renderedPages.forEach((page, pageIndex) => {
46
+ let pageTagging;
47
+ if (structBuilder) {
48
+ const builder = structBuilder;
49
+ pageTagging = {
50
+ next: 0,
51
+ assigned: false,
52
+ record: (structId, mcid) => builder.addMcref(structId, pageIndex, mcid),
53
+ tagFor: (structId) => builder.node(structId).type
54
+ };
55
+ }
56
+ const contentBytes = emitPageContent(page.commands, pageTagging);
57
+ const contentsRef = doc.add(stream({}, contentBytes));
58
+ const pageEntries = {
59
+ Type: name("Page"),
60
+ Parent: ref(pagesRef.id),
61
+ MediaBox: [
62
+ 0,
63
+ 0,
64
+ page.width,
65
+ page.height
66
+ ],
67
+ Resources: resourcesDict,
68
+ Contents: ref(contentsRef.id)
69
+ };
70
+ if (transparencyGroup) pageEntries["Group"] = transparencyGroup;
71
+ if (pageTagging?.assigned) {
72
+ pageEntries["StructParents"] = pageIndex;
73
+ pageEntries["Tabs"] = name("S");
74
+ }
75
+ const pageDictObj = dict(pageEntries);
76
+ if (firstPageDict === void 0) firstPageDict = pageDictObj;
77
+ pageRefs.push(doc.add(pageDictObj));
78
+ });
79
+ if (pageRefs.length === 0) {
80
+ const fallback = sectionCtxs[0] ?? defaultPageCtx();
81
+ const contentsRef = doc.add(stream({}, new Uint8Array(0)));
82
+ const fallbackDict = dict({
83
+ Type: name("Page"),
84
+ Parent: ref(pagesRef.id),
85
+ MediaBox: [
86
+ 0,
87
+ 0,
88
+ fallback.pageWidth,
89
+ fallback.pageHeight
90
+ ],
91
+ Resources: resourcesDict,
92
+ Contents: ref(contentsRef.id),
93
+ ...transparencyGroup ? { Group: transparencyGroup } : {}
94
+ });
95
+ firstPageDict = fallbackDict;
96
+ pageRefs.push(doc.add(fallbackDict));
97
+ }
98
+ pagesDict.set("Count", pageRefs.length);
99
+ pagesDict.set("Kids", pageRefs);
100
+ const catalogEntries = {
101
+ Type: name("Catalog"),
102
+ Pages: ref(pagesRef.id)
103
+ };
104
+ if (pdfaProfile) {
105
+ catalogEntries["OutputIntents"] = [ref(doc.add(dict({
106
+ Type: name("OutputIntent"),
107
+ S: name("GTS_PDFA1"),
108
+ OutputConditionIdentifier: "sRGB",
109
+ Info: "sRGB IEC61966-2.1",
110
+ DestOutputProfile: ref(pdfaIccRef.id)
111
+ })).id)];
112
+ catalogEntries["Metadata"] = ref(doc.add(stream({
113
+ Type: name("Metadata"),
114
+ Subtype: name("XML")
115
+ }, buildXmpPacket(xmpFromInfo(options.info, pdfaProfile)))).id);
116
+ }
117
+ if (tagged && structBuilder) {
118
+ const structRootRef = structBuilder.emit(doc, pageRefs);
119
+ catalogEntries["MarkInfo"] = dict({ Marked: true });
120
+ catalogEntries["StructTreeRoot"] = ref(structRootRef.id);
121
+ catalogEntries["Lang"] = options.language ?? "en-US";
122
+ if (options.info?.title) catalogEntries["ViewerPreferences"] = dict({ DisplayDocTitle: true });
123
+ }
124
+ if (options.attachments?.length && (!pdfaProfile || pdfaProfile.part === 3)) {
125
+ const embedded = options.attachments.map((file) => ({
126
+ file,
127
+ ref: embedAssociatedFile(doc, file)
128
+ }));
129
+ catalogEntries["AF"] = embedded.map((e) => ref(e.ref.id));
130
+ const sorted = [...embedded].sort((a, b) => a.file.name < b.file.name ? -1 : a.file.name > b.file.name ? 1 : 0);
131
+ const names = [];
132
+ for (const e of sorted) names.push(e.file.name, ref(e.ref.id));
133
+ catalogEntries["Names"] = dict({ EmbeddedFiles: ref(doc.add(dict({ Names: names })).id) });
134
+ }
135
+ if (options.signaturePlaceholder && firstPageDict) {
136
+ const { fieldRef, acroForm } = addSignaturePlaceholder(doc, options.signaturePlaceholder, pageRefs[0]);
137
+ firstPageDict.set("Annots", [ref(fieldRef.id)]);
138
+ catalogEntries["AcroForm"] = acroForm;
139
+ }
140
+ const catalogRef = doc.add(dict(catalogEntries));
141
+ const infoRef = buildInfoDict(doc, options.info);
142
+ return doc.build(catalogRef, infoRef, { ...pdfaProfile ? {
143
+ version: pdfaProfile.version,
144
+ id: true
145
+ } : {} });
146
+ }
147
+ var DEFAULT_PRODUCER = "Ream";
148
+ function xmpFromInfo(info, p) {
149
+ return {
150
+ pdfaPart: p ? String(p.part) : "1",
151
+ pdfaConformance: p ? p.level.toUpperCase() : "B",
152
+ producer: info?.producer ?? DEFAULT_PRODUCER,
153
+ ...info?.title ? { title: info.title } : {},
154
+ ...info?.author ? { author: info.author } : {},
155
+ ...info?.subject ? { subject: info.subject } : {},
156
+ ...info?.keywords ? { keywords: info.keywords } : {},
157
+ ...info?.creator ? { creator: info.creator } : {},
158
+ ...info?.creationDate ? { createDate: info.creationDate } : {},
159
+ ...info?.modificationDate ? { modifyDate: info.modificationDate } : {}
160
+ };
161
+ }
162
+ function buildInfoDict(doc, info) {
163
+ const merged = {
164
+ ...info,
165
+ producer: info?.producer ?? DEFAULT_PRODUCER
166
+ };
167
+ const entries = {};
168
+ if (merged.title) entries["Title"] = unicodeString(merged.title);
169
+ if (merged.author) entries["Author"] = unicodeString(merged.author);
170
+ if (merged.subject) entries["Subject"] = unicodeString(merged.subject);
171
+ if (merged.keywords) entries["Keywords"] = unicodeString(merged.keywords);
172
+ if (merged.creator) entries["Creator"] = unicodeString(merged.creator);
173
+ entries["Producer"] = unicodeString(merged.producer);
174
+ if (merged.creationDate) entries["CreationDate"] = formatPdfDate(merged.creationDate);
175
+ if (merged.modificationDate) entries["ModDate"] = formatPdfDate(merged.modificationDate);
176
+ return doc.add(dict(entries));
177
+ }
178
+ function formatPdfDate(d) {
179
+ const pad = (n, w = 2) => String(n).padStart(w, "0");
180
+ return `D:${pad(d.getUTCFullYear(), 4)}${pad(d.getUTCMonth() + 1)}${pad(d.getUTCDate())}${pad(d.getUTCHours())}${pad(d.getUTCMinutes())}${pad(d.getUTCSeconds())}Z`;
181
+ }
182
+ function defaultPageCtx() {
183
+ return {
184
+ endIndex: 0,
185
+ properties: {
186
+ headers: [],
187
+ footers: []
188
+ },
189
+ pageWidth: 595,
190
+ pageHeight: 842,
191
+ marginLeft: 72,
192
+ marginTop: 72,
193
+ marginBottom: 72,
194
+ contentWidth: 451,
195
+ pageContentHeight: 698,
196
+ headerSet: {
197
+ default: [],
198
+ first: [],
199
+ even: []
200
+ },
201
+ footerSet: {
202
+ default: [],
203
+ first: [],
204
+ even: []
205
+ },
206
+ titlePg: false,
207
+ evenAndOddHeaders: false
208
+ };
209
+ }
210
+ function embedImageResources(doc, laid) {
211
+ const out = /* @__PURE__ */ new Map();
212
+ for (const [resourceId, res] of laid.imageResources) out.set(resourceId, addImage(doc, res.prepared).ref);
213
+ return out;
214
+ }
215
+ function embedFontResources(doc, laid) {
216
+ const cidSet = laid.pdfaProfile?.part === 1;
217
+ const out = /* @__PURE__ */ new Map();
218
+ for (const [variant, res] of laid.fontResources) out.set(variant, embedTtfFont(doc, res.parsed, {
219
+ usedGids: res.gids,
220
+ cidSet
221
+ }));
222
+ return out;
223
+ }
224
+ function buildFontResourceDict(resources, embedded) {
225
+ const entries = {};
226
+ for (const [variant, res] of resources) {
227
+ const emb = embedded.get(variant);
228
+ if (emb) entries[res.resourceName] = ref(emb.fontRef.id);
229
+ }
230
+ return dict(entries);
231
+ }
232
+ function buildXObjectResourceDict(resources, embedded) {
233
+ if (embedded.size === 0) return void 0;
234
+ const entries = {};
235
+ for (const [resourceId, res] of resources) {
236
+ const r = embedded.get(resourceId);
237
+ if (r) entries[res.resourceName] = ref(r.id);
238
+ }
239
+ return dict(entries);
240
+ }
241
+ function emitTaggedRuns(out, cmds, tagging, body) {
242
+ if (!tagging) {
243
+ for (const c of cmds) body(c);
244
+ return;
245
+ }
246
+ let openKey = null;
247
+ const close = () => {
248
+ if (openKey !== null) {
249
+ out.push("EMC");
250
+ openKey = null;
251
+ }
252
+ };
253
+ for (const c of cmds) {
254
+ const key = c.structId !== void 0 ? `f${c.structId}` : c.artifact === "pagination" ? "p" : "a";
255
+ if (key !== openKey) {
256
+ close();
257
+ if (c.structId !== void 0) {
258
+ const mcid = tagging.next++;
259
+ tagging.assigned = true;
260
+ tagging.record(c.structId, mcid);
261
+ out.push(`/${tagging.tagFor(c.structId)} <</MCID ${mcid}>> BDC`);
262
+ } else if (c.artifact === "pagination") out.push("/Artifact <</Type /Pagination>> BDC");
263
+ else out.push("/Artifact BMC");
264
+ openKey = key;
265
+ }
266
+ body(c);
267
+ }
268
+ close();
269
+ }
270
+ function emitPageContent(commands, tagging) {
271
+ if (commands.length === 0) return new Uint8Array(0);
272
+ const out = [];
273
+ const plan = paintPlan(commands);
274
+ const fills = plan.fills;
275
+ if (fills.length > 0) {
276
+ if (tagging) out.push("/Artifact BMC");
277
+ out.push("q");
278
+ let lastColor = "";
279
+ for (const f of fills) {
280
+ const color = f.fillColorHex;
281
+ if (color !== lastColor) {
282
+ const [r, g, b] = hexToRgb01(color);
283
+ out.push(`${formatNumber(r)} ${formatNumber(g)} ${formatNumber(b)} rg`);
284
+ lastColor = color;
285
+ }
286
+ out.push(`${formatNumber(f.x)} ${formatNumber(f.y)} ${formatNumber(f.width)} ${formatNumber(f.height)} re`);
287
+ out.push("f");
288
+ }
289
+ out.push("Q");
290
+ if (tagging) out.push("EMC");
291
+ }
292
+ emitTaggedRuns(out, plan.images.filter((c) => c.imageResourceName !== ""), tagging, (img) => {
293
+ out.push("q");
294
+ out.push(`${formatNumber(img.width)} 0 0 ${formatNumber(img.height)} ${formatNumber(img.x)} ${formatNumber(img.y)} cm`);
295
+ out.push(`/${img.imageResourceName} Do`);
296
+ out.push("Q");
297
+ });
298
+ const borders = plan.borders;
299
+ if (borders.length > 0) {
300
+ if (tagging) out.push("/Artifact BMC");
301
+ out.push("q");
302
+ let lastWidth = -1;
303
+ let lastColor = "";
304
+ for (const b of borders) {
305
+ const width = b.borderSizePt;
306
+ const color = b.borderColorHex;
307
+ if (width !== lastWidth) {
308
+ out.push(`${formatNumber(width)} w`);
309
+ lastWidth = width;
310
+ }
311
+ if (color !== lastColor) {
312
+ const [r, g, bl] = hexToRgb01(color);
313
+ out.push(`${formatNumber(r)} ${formatNumber(g)} ${formatNumber(bl)} RG`);
314
+ lastColor = color;
315
+ }
316
+ const x = b.x;
317
+ const y = b.y;
318
+ const w = b.width;
319
+ const h = b.height;
320
+ switch (b.side) {
321
+ case "top":
322
+ out.push(`${formatNumber(x)} ${formatNumber(y + h)} m`);
323
+ out.push(`${formatNumber(x + w)} ${formatNumber(y + h)} l`);
324
+ break;
325
+ case "bottom":
326
+ out.push(`${formatNumber(x)} ${formatNumber(y)} m`);
327
+ out.push(`${formatNumber(x + w)} ${formatNumber(y)} l`);
328
+ break;
329
+ case "left":
330
+ out.push(`${formatNumber(x)} ${formatNumber(y)} m`);
331
+ out.push(`${formatNumber(x)} ${formatNumber(y + h)} l`);
332
+ break;
333
+ case "right":
334
+ out.push(`${formatNumber(x + w)} ${formatNumber(y)} m`);
335
+ out.push(`${formatNumber(x + w)} ${formatNumber(y + h)} l`);
336
+ break;
337
+ }
338
+ out.push("S");
339
+ }
340
+ out.push("Q");
341
+ if (tagging) out.push("EMC");
342
+ }
343
+ const shapes = plan.shapes;
344
+ emitTaggedRuns(out, shapes, tagging, (sh) => {
345
+ for (const op of emitVectorShape(sh.shape)) out.push(op);
346
+ });
347
+ const lines = plan.lines;
348
+ if (lines.length > 0) {
349
+ let inBT = false;
350
+ let lastFont = "";
351
+ let lastSize = -1;
352
+ let lastColor = "";
353
+ const switchFontIfNeeded = (tok) => {
354
+ const fontKey = tok.font.resourceName;
355
+ if (fontKey !== lastFont || tok.fontSizePt !== lastSize) {
356
+ out.push(`/${fontKey} ${formatNumber(tok.fontSizePt)} Tf`);
357
+ lastFont = fontKey;
358
+ lastSize = tok.fontSizePt;
359
+ }
360
+ if (tok.resolvedRun.colorHex !== lastColor) {
361
+ const [r, g, b] = hexToRgb01(tok.resolvedRun.colorHex);
362
+ out.push(`${formatNumber(r)} ${formatNumber(g)} ${formatNumber(b)} rg`);
363
+ lastColor = tok.resolvedRun.colorHex;
364
+ }
365
+ };
366
+ const emitImageToken = (tok, x, baselineY) => {
367
+ if (!tok.imageResourceName) return;
368
+ if (inBT) {
369
+ out.push("ET");
370
+ inBT = false;
371
+ }
372
+ out.push("q");
373
+ out.push(`${formatNumber(tok.widthPt)} 0 0 ${formatNumber(tok.heightPt)} ${formatNumber(x)} ${formatNumber(baselineY)} cm`);
374
+ out.push(`/${tok.imageResourceName} Do`);
375
+ out.push("Q");
376
+ lastFont = "";
377
+ lastSize = -1;
378
+ lastColor = "";
379
+ };
380
+ const emitMathToken = (tok, originX, baselineY) => {
381
+ for (const it of tok.items) if (it.kind === "glyph") {
382
+ if (!inBT) {
383
+ out.push("BT");
384
+ inBT = true;
385
+ }
386
+ if (it.font.resourceName !== lastFont || it.sizePt !== lastSize) {
387
+ out.push(`/${it.font.resourceName} ${formatNumber(it.sizePt)} Tf`);
388
+ lastFont = it.font.resourceName;
389
+ lastSize = it.sizePt;
390
+ }
391
+ if (lastColor !== "000000") {
392
+ out.push("0 0 0 rg");
393
+ lastColor = "000000";
394
+ }
395
+ out.push(`1 0 0 1 ${formatNumber(originX + it.x)} ${formatNumber(baselineY + it.y)} Tm`);
396
+ out.push(`<${it.font.measure.encodeTextAsCidHex(it.text)}> Tj`);
397
+ } else if (it.kind === "rule") {
398
+ if (inBT) {
399
+ out.push("ET");
400
+ inBT = false;
401
+ }
402
+ out.push("q");
403
+ out.push("0 0 0 rg");
404
+ out.push(`${formatNumber(originX + it.x)} ${formatNumber(baselineY + it.y)} ${formatNumber(it.w)} ${formatNumber(it.h)} re`);
405
+ out.push("f");
406
+ out.push("Q");
407
+ lastFont = "";
408
+ lastSize = -1;
409
+ lastColor = "";
410
+ } else {
411
+ if (inBT) {
412
+ out.push("ET");
413
+ inBT = false;
414
+ }
415
+ const shape = {
416
+ paths: [{ segments: it.segments }],
417
+ ...it.fill ? { fillColorHex: "000000" } : {},
418
+ ...it.strokeWidthPt !== void 0 ? { stroke: {
419
+ colorHex: "000000",
420
+ widthPt: it.strokeWidthPt
421
+ } } : {},
422
+ transform: [
423
+ 1,
424
+ 0,
425
+ 0,
426
+ 1,
427
+ originX,
428
+ baselineY
429
+ ]
430
+ };
431
+ for (const op of emitVectorShape(shape)) out.push(op);
432
+ lastFont = "";
433
+ lastSize = -1;
434
+ lastColor = "";
435
+ }
436
+ };
437
+ const emitOneLine = (cmd) => {
438
+ const line = cmd.line;
439
+ const originX = cmd.originX;
440
+ const baselineY = cmd.baselineY;
441
+ const extraPerSpace = computeJustifyExtra(line);
442
+ const hasImageToken = line.tokens.some((t) => t.kind === "image");
443
+ const hasMathToken = line.tokens.some((t) => t.kind === "math");
444
+ const hasRtl = line.tokens.some((t) => t.bidiLevel % 2 === 1);
445
+ const encodeToken = (tok) => {
446
+ const text = tok.bidiLevel % 2 === 1 ? reverseByCodePoint(tok.text) : tok.text;
447
+ return tok.font.measure.encodeTextAsCidHex(text);
448
+ };
449
+ if (extraPerSpace > 0 || hasImageToken || hasMathToken || hasRtl) {
450
+ const order = hasRtl ? lineVisualOrder(line) : line.tokens.map((_, i) => i);
451
+ let x = originX;
452
+ for (const ti of order) {
453
+ const tok = line.tokens[ti];
454
+ if (tok.kind === "image") {
455
+ emitImageToken(tok, x, baselineY);
456
+ x += tok.widthPt;
457
+ continue;
458
+ }
459
+ if (tok.kind === "math") {
460
+ emitMathToken(tok, x, baselineY);
461
+ x += tok.widthPt;
462
+ continue;
463
+ }
464
+ if (!inBT) {
465
+ out.push("BT");
466
+ inBT = true;
467
+ }
468
+ switchFontIfNeeded(tok);
469
+ out.push(`1 0 0 1 ${formatNumber(x)} ${formatNumber(baselineY)} Tm`);
470
+ out.push(`<${encodeToken(tok)}> Tj`);
471
+ x += tok.widthPt;
472
+ if (tok.isSpace) x += extraPerSpace;
473
+ }
474
+ } else {
475
+ if (!inBT) {
476
+ out.push("BT");
477
+ inBT = true;
478
+ }
479
+ out.push(`1 0 0 1 ${formatNumber(originX)} ${formatNumber(baselineY)} Tm`);
480
+ for (const tok of line.tokens) {
481
+ if (tok.kind !== "text") continue;
482
+ switchFontIfNeeded(tok);
483
+ const hex = tok.font.measure.encodeTextAsCidHex(tok.text);
484
+ out.push(`<${hex}> Tj`);
485
+ }
486
+ }
487
+ };
488
+ if (tagging) for (const cmd of lines) {
489
+ if (inBT) {
490
+ out.push("ET");
491
+ inBT = false;
492
+ }
493
+ const sid = cmd.structId;
494
+ if (sid !== void 0) {
495
+ const mcid = tagging.next++;
496
+ tagging.assigned = true;
497
+ tagging.record(sid, mcid);
498
+ out.push(`/${tagging.tagFor(sid)} <</MCID ${mcid}>> BDC`);
499
+ } else if (cmd.artifact === "pagination") out.push("/Artifact <</Type /Pagination>> BDC");
500
+ else out.push("/Artifact BMC");
501
+ emitOneLine(cmd);
502
+ if (inBT) {
503
+ out.push("ET");
504
+ inBT = false;
505
+ }
506
+ out.push("EMC");
507
+ }
508
+ else {
509
+ out.push("BT");
510
+ inBT = true;
511
+ for (const cmd of lines) emitOneLine(cmd);
512
+ if (inBT) out.push("ET");
513
+ }
514
+ }
515
+ return encoder.encode(out.join("\n"));
516
+ }
517
+ function lineVisualOrder(line) {
518
+ return reorderVisual(line.tokens.map((t) => t.bidiLevel));
519
+ }
520
+ function computeJustifyExtra(line) {
521
+ if (line.resolved.alignment !== "both") return 0;
522
+ if (line.isLastInParagraph) return 0;
523
+ let spaces = 0;
524
+ for (const tok of line.tokens) if (tok.isSpace) spaces++;
525
+ if (spaces === 0) return 0;
526
+ const extra = line.availableWidthPt - line.contentWidthPt;
527
+ if (extra <= 0) return 0;
528
+ return extra / spaces;
529
+ }
530
+ function hexToRgb01(hex) {
531
+ return [
532
+ parseInt(hex.slice(0, 2), 16) / 255,
533
+ parseInt(hex.slice(2, 4), 16) / 255,
534
+ parseInt(hex.slice(4, 6), 16) / 255
535
+ ];
536
+ }
537
+ function formatNumber(n) {
538
+ if (Number.isInteger(n)) return String(n);
539
+ return n.toFixed(3).replace(/0+$/, "").replace(/\.$/, "");
540
+ }
541
+ //#endregion
542
+ export { emitStyledPdf };