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