@officedk/pptxdk 0.0.0-stage → 0.1.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 (194) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/build.d.ts +94 -0
  4. package/dist/build.js +148 -0
  5. package/dist/capabilities/dist/all.js +32 -0
  6. package/dist/capabilities/dist/box/box-codegen.js +51 -0
  7. package/dist/capabilities/dist/box/box-declaration.js +31 -0
  8. package/dist/capabilities/dist/box/box-elements.js +17 -0
  9. package/dist/capabilities/dist/box/box.js +138 -0
  10. package/dist/capabilities/dist/chart/chart-codegen.js +77 -0
  11. package/dist/capabilities/dist/chart/chart-declaration.js +7 -0
  12. package/dist/capabilities/dist/chart/chart-elements.js +12 -0
  13. package/dist/capabilities/dist/chart/chart-import.js +2272 -0
  14. package/dist/capabilities/dist/chart/chart-labels.js +86 -0
  15. package/dist/capabilities/dist/chart/chart-model.js +9 -0
  16. package/dist/capabilities/dist/chart/chart-schemas.js +1078 -0
  17. package/dist/capabilities/dist/chart/chart-xml.js +788 -0
  18. package/dist/capabilities/dist/chart/chart.js +1124 -0
  19. package/dist/capabilities/dist/chart/preview/axes-and-grid.js +298 -0
  20. package/dist/capabilities/dist/chart/preview/bar-column.js +143 -0
  21. package/dist/capabilities/dist/chart/preview/data-labels.js +334 -0
  22. package/dist/capabilities/dist/chart/preview/error-bars.js +213 -0
  23. package/dist/capabilities/dist/chart/preview/index.js +190 -0
  24. package/dist/capabilities/dist/chart/preview/layout.js +519 -0
  25. package/dist/capabilities/dist/chart/preview/legend.js +273 -0
  26. package/dist/capabilities/dist/chart/preview/line-area.js +404 -0
  27. package/dist/capabilities/dist/chart/preview/markers.js +100 -0
  28. package/dist/capabilities/dist/chart/preview/number-format.js +247 -0
  29. package/dist/capabilities/dist/chart/preview/pie-doughnut.js +159 -0
  30. package/dist/capabilities/dist/chart/preview/scales.js +365 -0
  31. package/dist/capabilities/dist/chart/preview/scatter.js +131 -0
  32. package/dist/capabilities/dist/chart/preview/style.js +48 -0
  33. package/dist/capabilities/dist/chart/preview/text-render.js +116 -0
  34. package/dist/capabilities/dist/chart/preview/trendlines.js +537 -0
  35. package/dist/capabilities/dist/chart/preview/types.js +5 -0
  36. package/dist/capabilities/dist/document.js +148 -0
  37. package/dist/capabilities/dist/font/font-embedding.js +289 -0
  38. package/dist/capabilities/dist/font/font-family.js +10 -0
  39. package/dist/capabilities/dist/font/font-import.js +302 -0
  40. package/dist/capabilities/dist/font/font-subset.js +55 -0
  41. package/dist/capabilities/dist/font/font.js +25 -0
  42. package/dist/capabilities/dist/group/group-codegen.js +51 -0
  43. package/dist/capabilities/dist/group/group-declaration.js +42 -0
  44. package/dist/capabilities/dist/group/group-elements.js +12 -0
  45. package/dist/capabilities/dist/group/group-import.js +319 -0
  46. package/dist/capabilities/dist/group/group-xml.js +7 -0
  47. package/dist/capabilities/dist/group/group.js +211 -0
  48. package/dist/capabilities/dist/image/gif-decoder.js +244 -0
  49. package/dist/capabilities/dist/image/image-codegen.js +94 -0
  50. package/dist/capabilities/dist/image/image-declaration.js +8 -0
  51. package/dist/capabilities/dist/image/image-decode.js +221 -0
  52. package/dist/capabilities/dist/image/image-elements.js +12 -0
  53. package/dist/capabilities/dist/image/image-import.js +778 -0
  54. package/dist/capabilities/dist/image/image-schemas.js +149 -0
  55. package/dist/capabilities/dist/image/image-xml.js +12 -0
  56. package/dist/capabilities/dist/image/image.js +472 -0
  57. package/dist/capabilities/dist/masters/footer-placeholder.js +685 -0
  58. package/dist/capabilities/dist/masters/masters-codegen.js +21 -0
  59. package/dist/capabilities/dist/masters/masters.js +250 -0
  60. package/dist/capabilities/dist/motion/motion-api.js +527 -0
  61. package/dist/capabilities/dist/motion/motion-codegen.js +340 -0
  62. package/dist/capabilities/dist/motion/motion-elements.js +43 -0
  63. package/dist/capabilities/dist/motion/motion-import.js +1033 -0
  64. package/dist/capabilities/dist/motion/motion-plan.js +2189 -0
  65. package/dist/capabilities/dist/motion/motion-query.js +420 -0
  66. package/dist/capabilities/dist/motion/motion-schemas.js +216 -0
  67. package/dist/capabilities/dist/motion/motion.js +1372 -0
  68. package/dist/capabilities/dist/notes/notes-codegen.js +20 -0
  69. package/dist/capabilities/dist/notes/notes-declaration.js +8 -0
  70. package/dist/capabilities/dist/notes/notes-elements.js +12 -0
  71. package/dist/capabilities/dist/notes/notes-import.js +292 -0
  72. package/dist/capabilities/dist/notes/notes-schemas.js +35 -0
  73. package/dist/capabilities/dist/notes/notes-xml.js +38 -0
  74. package/dist/capabilities/dist/notes/notes.js +191 -0
  75. package/dist/capabilities/dist/placeholder/placeholder-codegen.js +23 -0
  76. package/dist/capabilities/dist/placeholder/placeholder-elements.js +12 -0
  77. package/dist/capabilities/dist/placeholder/placeholder.js +292 -0
  78. package/dist/capabilities/dist/shape/shape-codegen.js +166 -0
  79. package/dist/capabilities/dist/shape/shape-declaration.js +57 -0
  80. package/dist/capabilities/dist/shape/shape-elements.js +12 -0
  81. package/dist/capabilities/dist/shape/shape-import.js +579 -0
  82. package/dist/capabilities/dist/shape/shape-xml.js +6 -0
  83. package/dist/capabilities/dist/shape/shape.js +337 -0
  84. package/dist/capabilities/dist/shared/geometry.js +1423 -0
  85. package/dist/capabilities/dist/shared/identity.js +205 -0
  86. package/dist/capabilities/dist/shared/morph-api.js +11 -0
  87. package/dist/capabilities/dist/shared/placeholder-import.js +113 -0
  88. package/dist/capabilities/dist/shared/preset-definitions.js +21390 -0
  89. package/dist/capabilities/dist/shared/style-inheritance.js +166 -0
  90. package/dist/capabilities/dist/shared/style-parse.js +11 -0
  91. package/dist/capabilities/dist/shared/text-schemas.js +216 -0
  92. package/dist/capabilities/dist/shared/text-service.js +1883 -0
  93. package/dist/capabilities/dist/shared/visual.js +918 -0
  94. package/dist/capabilities/dist/slide-background/slide-background-codegen.js +44 -0
  95. package/dist/capabilities/dist/slide-background/slide-background-import.js +161 -0
  96. package/dist/capabilities/dist/slide-background/slide-background-xml.js +21 -0
  97. package/dist/capabilities/dist/slide-background/slide-background.js +221 -0
  98. package/dist/capabilities/dist/table/table-codegen.js +81 -0
  99. package/dist/capabilities/dist/table/table-declaration.js +15 -0
  100. package/dist/capabilities/dist/table/table-elements.js +27 -0
  101. package/dist/capabilities/dist/table/table-import.js +539 -0
  102. package/dist/capabilities/dist/table/table-schemas.js +279 -0
  103. package/dist/capabilities/dist/table/table-xml.js +37 -0
  104. package/dist/capabilities/dist/table/table.js +662 -0
  105. package/dist/capabilities/dist/text/text-codegen.js +129 -0
  106. package/dist/capabilities/dist/text/text-declaration.js +19 -0
  107. package/dist/capabilities/dist/text/text-elements.js +38 -0
  108. package/dist/capabilities/dist/text/text-import.js +1077 -0
  109. package/dist/capabilities/dist/text/text-xml.js +9 -0
  110. package/dist/capabilities/dist/text/text.js +335 -0
  111. package/dist/capabilities/dist/transition/transition-api.js +139 -0
  112. package/dist/capabilities/dist/transition/transition-codegen.js +45 -0
  113. package/dist/capabilities/dist/transition/transition-import.js +477 -0
  114. package/dist/capabilities/dist/transition/transition.js +260 -0
  115. package/dist/capability-D_xeNVIw.d.ts +29496 -0
  116. package/dist/cli.js +20 -0
  117. package/dist/codegen/dist/assets.js +78 -0
  118. package/dist/codegen/dist/codegen.js +611 -0
  119. package/dist/codegen/dist/identifiers.js +193 -0
  120. package/dist/codegen/dist/index.js +3 -0
  121. package/dist/codegen/dist/printer.js +143 -0
  122. package/dist/codegen/dist/types.js +49 -0
  123. package/dist/core/dist/author-ids.js +48 -0
  124. package/dist/core/dist/author-schemas.js +313 -0
  125. package/dist/core/dist/block-flex-layout.js +749 -0
  126. package/dist/core/dist/build.js +4 -0
  127. package/dist/core/dist/capability.js +13 -0
  128. package/dist/core/dist/declaration.js +15 -0
  129. package/dist/core/dist/diagnostics.js +21 -0
  130. package/dist/core/dist/document-internals.js +13 -0
  131. package/dist/core/dist/document.js +408 -0
  132. package/dist/core/dist/geometry-schemas.js +249 -0
  133. package/dist/core/dist/grid-layout.js +445 -0
  134. package/dist/core/dist/host.js +40 -0
  135. package/dist/core/dist/index.js +15 -0
  136. package/dist/core/dist/input-values.js +45 -0
  137. package/dist/core/dist/layout-protocol.js +66 -0
  138. package/dist/core/dist/layout-schemas.js +208 -0
  139. package/dist/core/dist/layout.js +299 -0
  140. package/dist/core/dist/masters.js +169 -0
  141. package/dist/core/dist/morph.js +30 -0
  142. package/dist/core/dist/placeholders.js +213 -0
  143. package/dist/core/dist/query.js +155 -0
  144. package/dist/core/dist/resolution.js +11 -0
  145. package/dist/core/dist/resolve.js +1827 -0
  146. package/dist/core/dist/stage-options.js +67 -0
  147. package/dist/core/dist/theme.js +931 -0
  148. package/dist/document-BHPUODPP.d.ts +27759 -0
  149. package/dist/emit/dist/emit.js +936 -0
  150. package/dist/emit/dist/index.js +5 -0
  151. package/dist/emit/dist/ooxml.js +606 -0
  152. package/dist/emit/dist/shape-tree.js +12 -0
  153. package/dist/import/dist/alternate-content.js +54 -0
  154. package/dist/import/dist/diagnostics.js +153 -0
  155. package/dist/import/dist/fonts.js +250 -0
  156. package/dist/import/dist/import.js +504 -0
  157. package/dist/import/dist/index.js +16 -0
  158. package/dist/import/dist/masters.js +1153 -0
  159. package/dist/import/dist/mechanism.js +10 -0
  160. package/dist/import/dist/namespaces.js +60 -0
  161. package/dist/import/dist/notes-masters.js +300 -0
  162. package/dist/import/dist/opc.js +233 -0
  163. package/dist/import/dist/resources.js +166 -0
  164. package/dist/import/dist/sections.js +139 -0
  165. package/dist/import/dist/themes.js +671 -0
  166. package/dist/import/dist/visual.js +761 -0
  167. package/dist/import/dist/xml-fragments.js +36 -0
  168. package/dist/import/dist/xml-reader.js +267 -0
  169. package/dist/index-B5m3hqjC.d.ts +7 -0
  170. package/dist/index-CCy-Yo8G.d.ts +1208 -0
  171. package/dist/index-DpxcZ4Gs.d.ts +725 -0
  172. package/dist/index.d.ts +7 -0
  173. package/dist/index.js +70 -0
  174. package/dist/node.d.ts +53 -0
  175. package/dist/node.js +194 -0
  176. package/dist/pptxdk/src/cli-commands.js +913 -0
  177. package/dist/pptxdk/src/compose.js +44 -0
  178. package/dist/pptxdk/src/slide.js +9 -0
  179. package/dist/preview/dist/index.js +240 -0
  180. package/dist/preview/dist/options.js +53 -0
  181. package/dist/preview-Cfw0QdMM.d.ts +86 -0
  182. package/dist/preview.d.ts +3 -0
  183. package/dist/preview.js +27 -0
  184. package/dist/react/dist/append.js +66 -0
  185. package/dist/react/dist/document-adapter.js +65 -0
  186. package/dist/react/dist/elements.js +59 -0
  187. package/dist/react/dist/index.js +68 -0
  188. package/dist/react/dist/reconciler.js +290 -0
  189. package/dist/react.d.ts +145 -0
  190. package/dist/react.js +27 -0
  191. package/dist/transition-Cips9wma.d.ts +82 -0
  192. package/dist/vanilla.d.ts +4 -0
  193. package/dist/vanilla.js +14 -0
  194. package/package.json +78 -3
@@ -0,0 +1,936 @@
1
+ import { BuildError, CapabilityError } from "../../core/dist/diagnostics.js";
2
+ import { buildOptionsSchema, formatBaseline, targets } from "../../core/dist/stage-options.js";
3
+ import "../../core/dist/build.js";
4
+ import { createBuildQuery } from "../../core/dist/query.js";
5
+ import { getInternalResolution } from "../../core/dist/resolution.js";
6
+ import { assertXml10Characters, countAllocatedMasterThemeParts, hasThemeOverrideContent, pptxParts } from "./ooxml.js";
7
+ import { createHash } from "node:crypto";
8
+ import { strToU8, zipSync } from "fflate";
9
+ // ../emit/dist/emit.js
10
+ function diagnostic(path, code, message) {
11
+ return {
12
+ severity: "error",
13
+ path,
14
+ code,
15
+ message
16
+ };
17
+ }
18
+ function allocateTextFieldId(document) {
19
+ return `{00000000-0000-4000-8000-${(document.nextTextFieldId++).toString(16).padStart(12, "0").toUpperCase()}}`;
20
+ }
21
+ function readValueAtPath(root, path) {
22
+ let current = root;
23
+ for (const key of path) {
24
+ if (typeof current !== "object" || current === null) return void 0;
25
+ current = Reflect.get(current, key);
26
+ }
27
+ return current;
28
+ }
29
+ function mapBuildOptionsIssue(rawOptions, issue) {
30
+ const [head, second, third] = issue.path;
31
+ if (head === void 0) {
32
+ if (issue.code === "unrecognized_keys") return diagnostic("build", "unknown-parameter", `Build options contain unknown parameter ${issue.keys[0]}`);
33
+ return diagnostic("build", "invalid-options", "Build options must be an object");
34
+ }
35
+ if (head === "embedFonts") return diagnostic("build.embedFonts", "unsupported-font-embedding", "embedFonts currently only supports 'full' or 'subset'");
36
+ if (head === "signal") return diagnostic("build.signal", "invalid-options", "Build options signal must be an AbortSignal");
37
+ if (head === "target") {
38
+ if (second === void 0) {
39
+ if (issue.code === "unrecognized_keys") return diagnostic("build.target", "unknown-parameter", `Target contains unknown parameter ${issue.keys[0]}`);
40
+ return diagnostic("build.target", "invalid-target", "Target must be an object");
41
+ }
42
+ if (second === "name") return diagnostic("build.target", "invalid-target", "Target requires a non-empty name");
43
+ if (second === "format") return diagnostic("build.target", "unsupported-format", `This build only emits ${formatBaseline}`);
44
+ if (second === "extensions") {
45
+ if (third === void 0) return diagnostic("build.target", "invalid-target", "Target extensions must be an array");
46
+ if (issue.code === "custom") return diagnostic("build.target", "duplicate-extension", issue.message);
47
+ const badExtension = readValueAtPath(rawOptions, issue.path);
48
+ return diagnostic("build.target", "unsupported-extension", `Unsupported target extension ${String(badExtension)}`);
49
+ }
50
+ }
51
+ return diagnostic("build", "invalid-options", issue.message);
52
+ }
53
+ /** Validates emit/build options (`target`, `embedFonts`, `signal`) and normalizes the target. */
54
+ function validateOptions(options) {
55
+ const parsed = buildOptionsSchema.safeParse(options);
56
+ if (!parsed.success) throw new BuildError(parsed.error.issues.map((issue) => mapBuildOptionsIssue(options, issue)));
57
+ return parsed.data.target ?? targets.transitional;
58
+ }
59
+ function addResource(resource, context, objectId) {
60
+ if ("externalTarget" in resource) {
61
+ if (typeof resource.externalTarget !== "string" || resource.externalTarget.trim() === "" || typeof resource.relationshipType !== "string" || resource.relationshipType.trim() === "") throw new CapabilityError("invalid-resource", "External resource target or relationship type is invalid");
62
+ try {
63
+ assertXml10Characters(resource.externalTarget);
64
+ assertXml10Characters(resource.relationshipType);
65
+ } catch {
66
+ throw new CapabilityError("invalid-resource", "External resource contains characters not allowed in XML");
67
+ }
68
+ const relationshipKey = `external\u0000${resource.relationshipType}\u0000${resource.externalTarget}`;
69
+ const existingRelationship = context.slide.relationshipIds.get(relationshipKey);
70
+ if (existingRelationship !== void 0) return existingRelationship;
71
+ const id = `rId${context.slide.relationships.length + context.slide.baseRelationshipCount + 1}`;
72
+ context.slide.relationshipIds.set(relationshipKey, id);
73
+ context.slide.relationships.push({
74
+ id,
75
+ type: resource.relationshipType,
76
+ target: resource.externalTarget,
77
+ targetMode: "External"
78
+ });
79
+ return id;
80
+ }
81
+ if (!(resource.bytes instanceof Uint8Array) || resource.bytes.byteLength === 0) throw new CapabilityError("invalid-resource", "Resource must contain non-empty binary content");
82
+ if (typeof resource.extension !== "string" || !/^[a-z0-9]+$/iu.test(resource.extension) || typeof resource.contentType !== "string" || resource.contentType.trim() === "" || typeof resource.relationshipType !== "string" || resource.relationshipType.trim() === "") throw new CapabilityError("invalid-resource", "Resource format or relationship type is invalid");
83
+ const extension = resource.extension.toLowerCase();
84
+ if (extension === "xml" || extension === "rels") throw new CapabilityError("invalid-resource", `Resource extension .${extension} conflicts with core PPTX parts`);
85
+ const knownType = context.document.resourceTypes.get(extension);
86
+ if (knownType !== void 0 && knownType !== resource.contentType) throw new CapabilityError("conflicting-resource-type", `Resource extension .${extension} corresponds to multiple content types`);
87
+ context.document.resourceTypes.set(extension, resource.contentType);
88
+ try {
89
+ assertXml10Characters(resource.contentType);
90
+ assertXml10Characters(resource.relationshipType);
91
+ } catch {
92
+ throw new CapabilityError("invalid-resource", "Resource relationship contains characters not allowed in XML");
93
+ }
94
+ const digest = createHash("sha256").update(resource.bytes).digest("hex");
95
+ const assetKey = `${extension}\u0000${resource.contentType}\u0000${digest}`;
96
+ let asset = context.document.resources.get(assetKey);
97
+ if (asset === void 0) {
98
+ asset = {
99
+ name: `resource${context.document.resources.size + 1}.${extension}`,
100
+ bytes: new Uint8Array(resource.bytes),
101
+ extension,
102
+ contentType: resource.contentType
103
+ };
104
+ context.document.resources.set(assetKey, asset);
105
+ }
106
+ if (objectId !== void 0) recordResourceObservation(context.slide, objectId, {
107
+ part: `ppt/media/${asset.name}`,
108
+ contentType: asset.contentType,
109
+ byteLength: asset.bytes.byteLength
110
+ });
111
+ const relationshipKey = `${assetKey}\u0000${resource.relationshipType}`;
112
+ const existingRelationship = context.slide.relationshipIds.get(relationshipKey);
113
+ if (existingRelationship !== void 0) return existingRelationship;
114
+ const id = `rId${context.slide.relationships.length + context.slide.baseRelationshipCount + 1}`;
115
+ context.slide.relationshipIds.set(relationshipKey, id);
116
+ context.slide.relationships.push({
117
+ id,
118
+ type: resource.relationshipType,
119
+ target: relationshipTarget(context.slide.partPath, `ppt/media/${asset.name}`)
120
+ });
121
+ return id;
122
+ }
123
+ function relationshipTarget(sourcePath, targetPath) {
124
+ const sourceSegments = sourcePath.split("/").slice(0, -1);
125
+ const targetSegments = targetPath.split("/");
126
+ while (sourceSegments.length > 0 && targetSegments.length > 0 && sourceSegments[0] === targetSegments[0]) {
127
+ sourceSegments.shift();
128
+ targetSegments.shift();
129
+ }
130
+ return [...sourceSegments.map(() => ".."), ...targetSegments].join("/");
131
+ }
132
+ function recordResourceObservation(slide, objectId, resource) {
133
+ if (slide.resourceObservations.some((existing) => existing.objectId === objectId && existing.part === resource.part)) return;
134
+ slide.resourceObservations.push({
135
+ objectId,
136
+ ...resource
137
+ });
138
+ }
139
+ function packagePartPath(template, sequence) {
140
+ if ((template.match(/\{n\}/gu) ?? []).length !== 1 || !/^(?:[a-z0-9_-]+\/)*[a-z0-9_-]+\.[a-z0-9]+$/iu.test(template.replace("{n}", String(sequence)))) throw new CapabilityError("invalid-part", "Native part path must be a safe package-relative path containing a single part number");
141
+ return template.replace("{n}", String(sequence));
142
+ }
143
+ /**
144
+ * Allocates a shared part path without colliding with generated deck parts.
145
+ *
146
+ * Shared part templates normally start at a free sequence number. A template such as
147
+ * `ppt/theme/theme{n}.xml` must nevertheless skip the theme part generated from the deck theme,
148
+ * which is written outside shared part registration but already reserved for the package.
149
+ */
150
+ function allocateSharedPackagePartPath(template, document) {
151
+ for (;;) {
152
+ const path = packagePartPath(template, document.nextPackagePartId++);
153
+ if (!document.packageParts.has(path) && !document.presentationPartPaths.has(path)) return path;
154
+ }
155
+ }
156
+ function addPackagePart(packagePart, context, objectId) {
157
+ const { part, relatedParts } = packagePart;
158
+ if (typeof part.pathTemplate !== "string" || typeof part.contentType !== "string" || part.contentType.trim() === "" || typeof packagePart.relationshipType !== "string" || packagePart.relationshipType.trim() === "" || typeof packagePart.content !== "function" || !Array.isArray(relatedParts) || relatedParts.some((related) => typeof related.pathTemplate !== "string" || typeof related.contentType !== "string" || related.contentType.trim() === "" || typeof related.relationshipType !== "string" || related.relationshipType.trim() === "" || !(related.bytes instanceof Uint8Array) || related.bytes.byteLength === 0)) throw new CapabilityError("invalid-part", "Native part or its related parts format and relationships are invalid");
159
+ try {
160
+ for (const value of [part.contentType, packagePart.relationshipType]) assertXml10Characters(value);
161
+ for (const related of relatedParts) for (const value of [related.contentType, related.relationshipType]) assertXml10Characters(value);
162
+ } catch {
163
+ throw new CapabilityError("invalid-part", "Native part content type or relationship contains characters not allowed in XML 1.0");
164
+ }
165
+ const sequence = context.document.nextPackagePartId++;
166
+ const partPath = packagePartPath(part.pathTemplate, sequence);
167
+ const related = relatedParts.map((item, index) => ({
168
+ path: packagePartPath(item.pathTemplate, sequence),
169
+ relationship: {
170
+ id: `rId${index + 1}`,
171
+ type: item.relationshipType,
172
+ target: ""
173
+ },
174
+ descriptor: item
175
+ }));
176
+ for (const item of related) item.relationship.target = relationshipTarget(partPath, item.path);
177
+ const relatedIds = related.map(({ relationship }) => relationship.id);
178
+ const content = packagePart.content(relatedIds);
179
+ if (!(typeof content === "string" ? content.length > 0 : content instanceof Uint8Array && content.byteLength > 0)) throw new CapabilityError("invalid-part", "Native part must contain content");
180
+ const relatedPaths = new Set(related.map(({ path }) => path));
181
+ if (context.document.packageParts.has(partPath) || relatedPaths.size !== related.length || relatedPaths.has(partPath) || related.some(({ path }) => context.document.packageParts.has(path))) throw new CapabilityError("duplicate-part", "Duplicate PPTX part path");
182
+ if (objectId !== void 0) {
183
+ recordResourceObservation(context.slide, objectId, {
184
+ part: partPath,
185
+ contentType: part.contentType,
186
+ byteLength: typeof content === "string" ? strToU8(content).byteLength : content.byteLength
187
+ });
188
+ for (const item of related) recordResourceObservation(context.slide, objectId, {
189
+ part: item.path,
190
+ contentType: item.descriptor.contentType,
191
+ byteLength: item.descriptor.bytes.byteLength
192
+ });
193
+ }
194
+ context.document.packageParts.set(partPath, {
195
+ path: partPath,
196
+ content,
197
+ contentType: part.contentType
198
+ });
199
+ for (const item of related) context.document.packageParts.set(item.path, {
200
+ path: item.path,
201
+ content: new Uint8Array(item.descriptor.bytes),
202
+ contentType: item.descriptor.contentType
203
+ });
204
+ if (related.length > 0) context.document.packageRelationships.set(partPath, related.map(({ relationship }) => relationship));
205
+ const relationshipId = `rId${context.slide.relationships.length + context.slide.baseRelationshipCount + 1}`;
206
+ context.slide.relationships.push({
207
+ id: relationshipId,
208
+ type: packagePart.relationshipType,
209
+ target: relationshipTarget(context.slide.partPath, partPath)
210
+ });
211
+ return relationshipId;
212
+ }
213
+ function packagePath(value, label) {
214
+ if (typeof value !== "string" || !/^(?:[a-z0-9_-]+\/)*[a-z0-9_-]+\.[a-z0-9]+$/iu.test(value)) throw new CapabilityError("invalid-part", `${label} must be a safe package-relative path`);
215
+ return value;
216
+ }
217
+ function packagePartDescriptor(part, relationshipType, relationships, content, label, allowsSlideTarget = false) {
218
+ if (typeof part.pathTemplate !== "string" || typeof part.contentType !== "string" || part.contentType.trim() === "" || relationshipType !== void 0 && (typeof relationshipType !== "string" || relationshipType.trim() === "") || typeof content !== "function" || relationships !== void 0 && !Array.isArray(relationships) || Array.isArray(relationships) && relationships.some((relationship) => typeof relationship !== "object" || relationship === null || typeof relationship.relationshipType !== "string" || relationship.relationshipType.trim() === "" || typeof relationship.target !== "object" || relationship.target === null || ![
219
+ "slide",
220
+ "part",
221
+ "path"
222
+ ].includes(relationship.target.kind) || relationship.target.kind === "part" && (typeof relationship.target.key !== "string" || relationship.target.key === "") || relationship.target.kind === "path" && typeof relationship.target.path !== "string" || relationship.target.kind === "slide" && !allowsSlideTarget)) throw new CapabilityError("invalid-part", `${label} declaration is invalid`);
223
+ try {
224
+ assertXml10Characters(part.contentType);
225
+ if (typeof relationshipType === "string") assertXml10Characters(relationshipType);
226
+ for (const relationship of relationships ?? []) {
227
+ assertXml10Characters(relationship.relationshipType);
228
+ if (relationship.target.kind === "part") assertXml10Characters(relationship.target.key);
229
+ else if (relationship.target.kind === "path") assertXml10Characters(relationship.target.path);
230
+ }
231
+ } catch {
232
+ throw new CapabilityError("invalid-part", `${label} contains characters not allowed in XML 1.0`);
233
+ }
234
+ return {
235
+ pathTemplate: part.pathTemplate,
236
+ contentType: part.contentType,
237
+ ...typeof relationshipType === "string" ? { relationshipType } : {},
238
+ relationships: relationships ?? []
239
+ };
240
+ }
241
+ function targetPartPath(target, context) {
242
+ if (target.kind === "slide") return `ppt/slides/slide${context.slide.index}.xml`;
243
+ if (target.kind === "part") {
244
+ const shared = context.document.sharedParts.get(target.key);
245
+ if (shared === void 0) throw new CapabilityError("missing-part-target", `Package part relationship references unregistered shared part ${JSON.stringify(target.key)}`);
246
+ return shared.path;
247
+ }
248
+ return packagePath(target.path, "Package part relationship target");
249
+ }
250
+ function partContent(content, label) {
251
+ if (!(typeof content === "string" ? content.length > 0 : content instanceof Uint8Array && content.byteLength > 0)) throw new CapabilityError("invalid-part", `${label} must contain content`);
252
+ if (typeof content === "string") assertXml10Characters(content);
253
+ return content;
254
+ }
255
+ function addSlidePackagePart(part, context) {
256
+ const descriptor = packagePartDescriptor(part.part, part.slideRelationshipType, part.relationships, part.content, "slide part", true);
257
+ const slideRelationshipType = descriptor.relationshipType;
258
+ if (slideRelationshipType === void 0) throw new CapabilityError("invalid-part", "Slide part relationship type is required");
259
+ const sequence = context.document.nextPackagePartId++;
260
+ const partPath = packagePartPath(descriptor.pathTemplate, sequence);
261
+ if (context.document.packageParts.has(partPath) || context.document.presentationPartPaths.has(partPath)) throw new CapabilityError("duplicate-part", "Duplicate PPTX part path");
262
+ const relationships = descriptor.relationships.map((relationship, index) => ({
263
+ id: `rId${index + 1}`,
264
+ type: relationship.relationshipType,
265
+ target: relationshipTarget(partPath, targetPartPath(relationship.target, context))
266
+ }));
267
+ const content = partContent(part.content(relationships.map(({ id }) => id)), "Slide part");
268
+ context.document.presentationPartPaths.add(partPath);
269
+ context.document.packageParts.set(partPath, {
270
+ path: partPath,
271
+ content,
272
+ contentType: descriptor.contentType
273
+ });
274
+ if (relationships.length > 0) context.document.packageRelationships.set(partPath, relationships);
275
+ const relationshipId = `rId${context.slide.relationships.length + context.slide.baseRelationshipCount + 1}`;
276
+ context.slide.relationships.push({
277
+ id: relationshipId,
278
+ type: slideRelationshipType,
279
+ target: relationshipTarget(context.slide.partPath, partPath)
280
+ });
281
+ return relationshipId;
282
+ }
283
+ function addSharedPackagePart(key, part, context) {
284
+ if (typeof key !== "string" || key.trim() === "") throw new CapabilityError("invalid-part", "Shared part key must be a non-empty string");
285
+ const descriptor = packagePartDescriptor(part.part, part.presentationRelationshipType, part.relationships, part.content, "shared part");
286
+ const presentationRelationshipType = descriptor.relationshipType;
287
+ if (part.presentationElement !== void 0 && presentationRelationshipType === void 0) throw new CapabilityError("invalid-part", "Shared part presentation element requires a presentation relationship type");
288
+ const existing = context.document.sharedParts.get(key);
289
+ if (existing !== void 0) {
290
+ if (!(existing.descriptor.part.pathTemplate === descriptor.pathTemplate && existing.descriptor.part.contentType === descriptor.contentType && existing.descriptor.presentationRelationshipType === descriptor.relationshipType && JSON.stringify(existing.descriptor.relationships ?? []) === JSON.stringify(descriptor.relationships))) throw new CapabilityError("conflicting-shared-part", `Shared part ${JSON.stringify(key)} has conflicting declarations`);
291
+ return {
292
+ path: existing.path,
293
+ ...existing.relationshipId === void 0 ? {} : { relationshipId: existing.relationshipId }
294
+ };
295
+ }
296
+ const partPath = allocateSharedPackagePartPath(descriptor.pathTemplate, context.document);
297
+ const relationships = descriptor.relationships.map((relationship, index) => ({
298
+ id: `rId${index + 1}`,
299
+ type: relationship.relationshipType,
300
+ target: relationshipTarget(partPath, targetPartPath(relationship.target, context))
301
+ }));
302
+ const content = partContent(part.content(relationships.map(({ id }) => id)), "Shared part");
303
+ context.document.presentationPartPaths.add(partPath);
304
+ context.document.packageParts.set(partPath, {
305
+ path: partPath,
306
+ content,
307
+ contentType: descriptor.contentType
308
+ });
309
+ if (relationships.length > 0) context.document.packageRelationships.set(partPath, relationships);
310
+ let relationshipId;
311
+ if (presentationRelationshipType !== void 0) {
312
+ relationshipId = `rId${context.document.nextPresentationRelationshipId++}`;
313
+ const presentationRelationships = context.document.packageRelationships.get("ppt/presentation.xml") ?? [];
314
+ presentationRelationships.push({
315
+ id: relationshipId,
316
+ type: presentationRelationshipType,
317
+ target: relationshipTarget("ppt/presentation.xml", partPath)
318
+ });
319
+ context.document.packageRelationships.set("ppt/presentation.xml", presentationRelationships);
320
+ const element = part.presentationElement;
321
+ if (element !== void 0) {
322
+ if (element.position !== "before-slides" && element.position !== "after-notes-size" || typeof element.content !== "function") throw new CapabilityError("invalid-part", "Shared part presentation element declaration is invalid");
323
+ const xml = element.content(relationshipId);
324
+ if (typeof xml !== "string" || xml.trim() === "") throw new CapabilityError("invalid-part", "Shared part presentation element must generate content");
325
+ assertXml10Characters(xml);
326
+ context.document.presentationElements.push({
327
+ position: element.position,
328
+ content: xml
329
+ });
330
+ }
331
+ }
332
+ context.document.sharedParts.set(key, {
333
+ path: partPath,
334
+ ...relationshipId === void 0 ? {} : { relationshipId },
335
+ descriptor: {
336
+ part: {
337
+ pathTemplate: descriptor.pathTemplate,
338
+ contentType: descriptor.contentType
339
+ },
340
+ ...presentationRelationshipType === void 0 ? {} : { presentationRelationshipType },
341
+ ...part.relationships === void 0 ? {} : { relationships: descriptor.relationships },
342
+ ...part.presentationElement === void 0 ? {} : { presentationElement: part.presentationElement },
343
+ content: part.content
344
+ }
345
+ });
346
+ return {
347
+ path: partPath,
348
+ ...relationshipId === void 0 ? {} : { relationshipId }
349
+ };
350
+ }
351
+ function xmlName(value) {
352
+ return /^(?:[A-Za-z_][A-Za-z0-9_.-]*:)?[A-Za-z_][A-Za-z0-9_.-]*$/u.test(value);
353
+ }
354
+ function addPresentationContribution(contribution, context, path, objectId) {
355
+ const { collection, record, item } = contribution;
356
+ if (!xmlName(collection) || typeof record.key !== "string" || record.key === "" || typeof record.identity !== "string" || typeof record.tagName !== "string" || !xmlName(record.tagName) || typeof record.content !== "string" || typeof item.key !== "string" || item.key === "" || typeof item.identity !== "string" || !Number.isInteger(item.order) || item.order < 0 || typeof item.relationshipType !== "string" || item.relationshipType.trim() === "" || typeof item.pathTemplate !== "string" || typeof item.contentType !== "string" || item.contentType.trim() === "" || item.bytes !== void 0 && (!(item.bytes instanceof Uint8Array) || item.bytes.byteLength === 0) || item.bytes === void 0 && item.resolveBytes === void 0 || item.resourceClaim === void 0 !== (item.resolveBytes === void 0) || item.resourceClaim !== void 0 && (typeof item.resourceClaim.kind !== "string" || item.resourceClaim.kind === "" || typeof item.resourceClaim.key !== "string" || item.resourceClaim.key === "") || item.resolveBytes !== void 0 && typeof item.resolveBytes !== "function" || typeof item.content !== "function") throw new CapabilityError("invalid-presentation-contribution", "Presentation-level contribution record, relationship, or resource is invalid");
357
+ try {
358
+ assertXml10Characters(collection);
359
+ assertXml10Characters(record.content);
360
+ assertXml10Characters(item.relationshipType);
361
+ assertXml10Characters(item.contentType);
362
+ for (const [name, value] of Object.entries(contribution.presentationAttributes ?? {})) {
363
+ if (!xmlName(name) || typeof value !== "string") throw new Error("invalid presentation attribute");
364
+ assertXml10Characters(value);
365
+ }
366
+ } catch {
367
+ throw new CapabilityError("invalid-presentation-contribution", "Presentation-level contribution contains invalid XML name or characters");
368
+ }
369
+ for (const [name, value] of Object.entries(contribution.presentationAttributes ?? {})) {
370
+ const existing = context.document.presentationAttributes.get(name);
371
+ if (existing !== void 0 && existing !== value) context.slide.diagnostics.push(diagnostic(path, "conflicting-presentation-attribute", `Conflicting contribution value for presentation attribute ${name}`));
372
+ else context.document.presentationAttributes.set(name, value);
373
+ }
374
+ let targetCollection = context.document.presentationCollections.get(collection);
375
+ if (targetCollection === void 0) {
376
+ targetCollection = {
377
+ tagName: collection,
378
+ records: /* @__PURE__ */ new Map()
379
+ };
380
+ context.document.presentationCollections.set(collection, targetCollection);
381
+ }
382
+ let targetRecord = targetCollection.records.get(record.key);
383
+ if (targetRecord === void 0) {
384
+ targetRecord = {
385
+ identity: record.identity,
386
+ tagName: record.tagName,
387
+ content: record.content,
388
+ items: /* @__PURE__ */ new Map()
389
+ };
390
+ targetCollection.records.set(record.key, targetRecord);
391
+ } else if (targetRecord.identity !== record.identity || targetRecord.tagName !== record.tagName || targetRecord.content !== record.content) {
392
+ context.slide.diagnostics.push(diagnostic(path, "conflicting-presentation-record", `Conflicting declaration for presentation-level record ${record.key}`));
393
+ return;
394
+ }
395
+ let preview;
396
+ try {
397
+ preview = item.content("rId0");
398
+ assertXml10Characters(preview);
399
+ } catch {
400
+ throw new CapabilityError("invalid-presentation-contribution", "Presentation-level contribution failed to generate XML content");
401
+ }
402
+ if (preview.trim() === "") throw new CapabilityError("invalid-presentation-contribution", "Presentation-level contribution must generate content");
403
+ const existingItem = targetRecord.items.get(item.key);
404
+ if (existingItem !== void 0) {
405
+ if (existingItem.identity !== item.identity || existingItem.order !== item.order || existingItem.preview !== preview || existingItem.resourceClaim?.kind !== item.resourceClaim?.kind || existingItem.resourceClaim?.key !== item.resourceClaim?.key) context.slide.diagnostics.push(diagnostic(path, "conflicting-presentation-item", `Presentation-level record ${record.key} item ${item.key} uses conflicting resources`));
406
+ else {
407
+ const objectIds = existingItem.resourceOwners.get(context.slide) ?? /* @__PURE__ */ new Set();
408
+ if (objectId !== void 0) objectIds.add(objectId);
409
+ existingItem.resourceOwners.set(context.slide, objectIds);
410
+ }
411
+ return;
412
+ }
413
+ let sequence = 1;
414
+ let partPath = packagePartPath(item.pathTemplate, sequence);
415
+ while (context.document.packageParts.has(partPath) || context.document.presentationPartPaths.has(partPath)) {
416
+ sequence += 1;
417
+ partPath = packagePartPath(item.pathTemplate, sequence);
418
+ }
419
+ context.document.presentationPartPaths.add(partPath);
420
+ const relationshipId = `rId${context.document.nextPresentationRelationshipId++}`;
421
+ const content = item.content(relationshipId);
422
+ targetRecord.items.set(item.key, {
423
+ identity: item.identity,
424
+ order: item.order,
425
+ preview,
426
+ content,
427
+ partPath,
428
+ relationshipId,
429
+ relationshipType: item.relationshipType,
430
+ contentType: item.contentType,
431
+ path,
432
+ resourceOwners: /* @__PURE__ */ new Map([[context.slide, objectId === void 0 ? /* @__PURE__ */ new Set() : /* @__PURE__ */ new Set([objectId])]]),
433
+ ...item.bytes === void 0 ? {} : { bytes: item.bytes },
434
+ ...item.resourceClaim === void 0 ? {} : { resourceClaim: item.resourceClaim },
435
+ ...item.resolveBytes === void 0 ? {} : { resolveBytes: item.resolveBytes }
436
+ });
437
+ }
438
+ function finalizePresentationContributions(document) {
439
+ for (const collection of document.presentationCollections.values()) for (const record of collection.records.values()) for (const item of record.items.values()) {
440
+ let bytes = item.bytes;
441
+ if (item.resourceClaim !== void 0) {
442
+ const claimKey = `${item.resourceClaim.kind}\u0000${item.resourceClaim.key}`;
443
+ const claim = document.resourceClaims.get(claimKey);
444
+ if (claim === void 0 || claim.identity !== item.identity) throw new BuildError([diagnostic(item.path, "missing-resource-claim", `Presentation resource ${item.resourceClaim.key} is missing matching character claims`)]);
445
+ try {
446
+ bytes = item.resolveBytes(claim.characters);
447
+ } catch (error) {
448
+ if (error instanceof CapabilityError) throw new BuildError([diagnostic(error.path ?? item.path, error.code, error.message)]);
449
+ throw new BuildError([diagnostic(item.path, "resource-contribution-failed", error instanceof Error ? error.message : String(error))]);
450
+ }
451
+ }
452
+ if (!(bytes instanceof Uint8Array) || bytes.byteLength === 0) throw new BuildError([diagnostic(item.path, "invalid-presentation-contribution", "Presentation-level contribution did not produce valid resource bytes")]);
453
+ document.packageParts.set(item.partPath, {
454
+ path: item.partPath,
455
+ content: new Uint8Array(bytes),
456
+ contentType: item.contentType
457
+ });
458
+ const relationships = document.packageRelationships.get("ppt/presentation.xml") ?? [];
459
+ relationships.push({
460
+ id: item.relationshipId,
461
+ type: item.relationshipType,
462
+ target: relationshipTarget("ppt/presentation.xml", item.partPath)
463
+ });
464
+ document.packageRelationships.set("ppt/presentation.xml", relationships);
465
+ for (const [slide, objectIds] of item.resourceOwners) for (const objectId of objectIds) recordResourceObservation(slide, objectId, {
466
+ part: item.partPath,
467
+ contentType: item.contentType,
468
+ byteLength: bytes.byteLength
469
+ });
470
+ }
471
+ }
472
+ function presentationContent(document) {
473
+ const rendered = [...document.presentationCollections.values()].map(({ tagName, records }) => `<${tagName}>${[...records.values()].map((record) => `<${record.tagName}>${record.content}${[...record.items.values()].sort((left, right) => left.order - right.order).map(({ content }) => content).join("")}</${record.tagName}>`).join("")}</${tagName}>`).join("");
474
+ const elements = (position) => document.presentationElements.filter((element) => element.position === position).map(({ content }) => content).join("");
475
+ return {
476
+ beforeSlides: elements("before-slides"),
477
+ afterNotesSize: rendered + elements("after-notes-size")
478
+ };
479
+ }
480
+ function registerLinkRelationship(link, slideIndexById, slide) {
481
+ const { target } = link;
482
+ if ("id" in target) {
483
+ const targetIndex = slideIndexById.get(target.id);
484
+ if (targetIndex === void 0) throw new Error(`Missing validated target slide ${JSON.stringify(target.id)}`);
485
+ const relationshipKey = `link:slide:${target.id}`;
486
+ let relationshipId = slide.relationshipIds.get(relationshipKey);
487
+ if (relationshipId === void 0) {
488
+ relationshipId = `rId${slide.relationships.length + slide.baseRelationshipCount + 1}`;
489
+ slide.relationshipIds.set(relationshipKey, relationshipId);
490
+ slide.relationships.push({
491
+ id: relationshipId,
492
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide",
493
+ target: relationshipTarget(slide.partPath, `ppt/slides/slide${targetIndex}.xml`)
494
+ });
495
+ }
496
+ return {
497
+ relationshipId,
498
+ action: "ppaction://hlinksldjump"
499
+ };
500
+ }
501
+ if ("url" in target) {
502
+ const relationshipKey = `link:url:${target.url}`;
503
+ let relationshipId = slide.relationshipIds.get(relationshipKey);
504
+ if (relationshipId === void 0) {
505
+ relationshipId = `rId${slide.relationships.length + slide.baseRelationshipCount + 1}`;
506
+ slide.relationshipIds.set(relationshipKey, relationshipId);
507
+ slide.relationships.push({
508
+ id: relationshipId,
509
+ type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink",
510
+ target: target.url,
511
+ targetMode: "External"
512
+ });
513
+ }
514
+ return { relationshipId };
515
+ }
516
+ return {
517
+ relationshipId: "",
518
+ action: "ppaction://hlinkshowjump?jump=firstslide"
519
+ };
520
+ }
521
+ function slideSlotOrder(slot) {
522
+ return slot === "transition" ? 0 : 1;
523
+ }
524
+ function createDocumentEmitState(internal) {
525
+ const themePartCount = countAllocatedMasterThemeParts(internal.masters, internal.theme.theme);
526
+ return {
527
+ usedExtensions: /* @__PURE__ */ new Set(),
528
+ warnings: [...internal.warnings],
529
+ resourceClaims: internal.resourceClaims,
530
+ resources: /* @__PURE__ */ new Map(),
531
+ resourceTypes: /* @__PURE__ */ new Map(),
532
+ packageParts: /* @__PURE__ */ new Map(),
533
+ packageRelationships: /* @__PURE__ */ new Map(),
534
+ presentationAttributes: /* @__PURE__ */ new Map(),
535
+ presentationPartPaths: new Set(Array.from({ length: themePartCount }, (_, index) => `ppt/theme/theme${index + 1}.xml`)),
536
+ presentationCollections: /* @__PURE__ */ new Map(),
537
+ sharedParts: /* @__PURE__ */ new Map(),
538
+ presentationElements: [],
539
+ nextPackagePartId: 1,
540
+ nextPresentationRelationshipId: (internal.masters?.length ?? 1) + internal.slides.length + 1,
541
+ nextTextFieldId: 1
542
+ };
543
+ }
544
+ function checkExtensions(extensions, target, usedExtensions, diagnostics, path) {
545
+ for (const extension of extensions) if (!target.extensions.includes(extension)) diagnostics.push(diagnostic(path, "forbidden-mechanism", `Target forbids ${extension}`));
546
+ else usedExtensions.add(extension);
547
+ }
548
+ function emitLayer(layer, slideIndexById, docEmit, slideEmit, target, options) {
549
+ const packageCtx = {
550
+ document: docEmit,
551
+ slide: slideEmit
552
+ };
553
+ checkExtensions(layer.extensions, target, docEmit.usedExtensions, slideEmit.diagnostics, layer.path);
554
+ const childrenXml = layer.childComposition === "members" ? layer.memberLayers.map((child) => emitLayer(child, slideIndexById, docEmit, slideEmit, target, options)).join("") : "";
555
+ const siblingXml = layer.childComposition === "siblings" ? layer.siblingLayers.map((child) => emitLayer(child, slideIndexById, docEmit, slideEmit, target, options)).join("") : "";
556
+ let ownXml = "";
557
+ try {
558
+ const nativeLink = layer.resolvedLink === void 0 ? void 0 : registerLinkRelationship(layer.resolvedLink, slideIndexById, slideEmit);
559
+ const emitContext = {
560
+ id: layer.objectId,
561
+ path: layer.path,
562
+ theme: layer.theme,
563
+ options,
564
+ slideNumber: slideEmit.index,
565
+ ...nativeLink === void 0 ? {} : { nativeLink },
566
+ childrenXml,
567
+ createTextFieldId: () => allocateTextFieldId(docEmit),
568
+ registerResource: (resource) => addResource(resource, packageCtx, layer.objectId),
569
+ registerPackagePart: (part) => addPackagePart(part, packageCtx, layer.objectId)
570
+ };
571
+ const output = layer.emit(emitContext);
572
+ ownXml = output.xml;
573
+ if (layer.morphKey !== void 0 && layer.emitsObject && ownXml === "") slideEmit.diagnostics.push(diagnostic(layer.path, "morph-needs-object", "morphKey can only be used with capabilities that produce native objects"));
574
+ for (const contribution of output.presentationContributions ?? []) addPresentationContribution(contribution, {
575
+ document: docEmit,
576
+ slide: slideEmit
577
+ }, layer.path, layer.objectId);
578
+ } catch (error) {
579
+ slideEmit.diagnostics.push(error instanceof CapabilityError ? diagnostic(error.path === void 0 ? layer.path : `${layer.path}/${error.path}`, error.code, error.message) : diagnostic(layer.path, "capability-failure", error instanceof Error ? error.message : String(error)));
580
+ }
581
+ return layer.childComposition === "members" ? ownXml : ownXml + siblingXml;
582
+ }
583
+ const emittedQueries = /* @__PURE__ */ new WeakMap();
584
+ function maxResolvedObjectId(layers) {
585
+ let maxId = 1;
586
+ for (const layer of layers) {
587
+ if (layer.objectId > maxId) maxId = layer.objectId;
588
+ const memberMax = maxResolvedObjectId(layer.memberLayers);
589
+ if (memberMax > maxId) maxId = memberMax;
590
+ const siblingMax = maxResolvedObjectId(layer.siblingLayers);
591
+ if (siblingMax > maxId) maxId = siblingMax;
592
+ }
593
+ return maxId;
594
+ }
595
+ /**
596
+ * Returns the BuildQuery enriched with emitted package resources for a BuildReport produced by `emit()`.
597
+ */
598
+ function emittedBuildQuery(report, fallbackQuery) {
599
+ return emittedQueries.get(report) ?? fallbackQuery;
600
+ }
601
+ function emitMasterOrLayoutCapabilities(capabilities, internal, docEmit, partEmit, target, options) {
602
+ if (capabilities === void 0 || capabilities.length === 0) return {
603
+ transitionXml: "",
604
+ timingXml: ""
605
+ };
606
+ const packageCtx = {
607
+ document: docEmit,
608
+ slide: partEmit
609
+ };
610
+ const transitionParts = [];
611
+ const timingParts = [];
612
+ for (const cap of capabilities) {
613
+ checkExtensions(cap.extensions, target, docEmit.usedExtensions, partEmit.diagnostics, cap.path);
614
+ try {
615
+ const output = cap.emit({
616
+ path: cap.path,
617
+ dimensions: internal.dimensions,
618
+ theme: cap.theme,
619
+ options,
620
+ slideNumber: 0,
621
+ createTextFieldId: () => allocateTextFieldId(docEmit),
622
+ registerResource: (resource) => addResource(resource, packageCtx),
623
+ registerPackagePart: (part) => addSlidePackagePart(part, packageCtx),
624
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, packageCtx)
625
+ });
626
+ if (cap.slot === "transition" && output.xml !== "") transitionParts.push(output.xml);
627
+ else if (cap.slot === "timing" && output.xml !== "") timingParts.push(output.xml);
628
+ } catch (error) {
629
+ partEmit.diagnostics.push(error instanceof CapabilityError ? diagnostic(error.path ?? cap.path, error.code, error.message) : diagnostic(cap.path, "capability-failure", error instanceof Error ? error.message : String(error)));
630
+ }
631
+ }
632
+ return {
633
+ transitionXml: transitionParts.join(""),
634
+ timingXml: timingParts.join("")
635
+ };
636
+ }
637
+ function emitOwnerPlaceholderVisuals(placeholderVisuals, internal, docEmit, ownerEmit, ownerCtx, target, options) {
638
+ if (placeholderVisuals === void 0) return void 0;
639
+ checkExtensions(placeholderVisuals.extensions, target, docEmit.usedExtensions, ownerEmit.diagnostics, placeholderVisuals.path);
640
+ return placeholderVisuals.emit({
641
+ path: placeholderVisuals.path,
642
+ dimensions: internal.dimensions,
643
+ theme: placeholderVisuals.theme,
644
+ options,
645
+ slideNumber: 1,
646
+ createTextFieldId: () => allocateTextFieldId(docEmit),
647
+ registerResource: (resource) => addResource(resource, ownerCtx),
648
+ registerPackagePart: (part) => addSlidePackagePart(part, ownerCtx),
649
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, ownerCtx)
650
+ }).placeholderSpPrByKey;
651
+ }
652
+ /**
653
+ * Emits a PPTX archive and BuildReport from an existing Resolution without re-resolving the document.
654
+ */
655
+ function emit(resolution, options = {}) {
656
+ const target = validateOptions(options);
657
+ if (options.signal?.aborted) throw options.signal.reason;
658
+ const internal = getInternalResolution(resolution);
659
+ const docEmit = createDocumentEmitState(internal);
660
+ let sectionXml = "";
661
+ if (internal.sectionsEmitter !== void 0) {
662
+ if (!internal.sectionsEmitter.extensions.every((ext) => target.extensions.includes(ext))) throw new BuildError([diagnostic("deck/sections", "forbidden-mechanism", "Current target forbids p14 section mechanism")]);
663
+ for (const ext of internal.sectionsEmitter.extensions) docEmit.usedExtensions.add(ext);
664
+ sectionXml = internal.sectionsEmitter.emit().xml;
665
+ }
666
+ const mastersEmitDriver = internal.mastersEmitDriver;
667
+ const slideIndexById = new Map(internal.slides.map((slide) => [slide.id, slide.index]));
668
+ let globalLayoutNumber = 0;
669
+ const emittedMasters = internal.masters?.map((master, masterIndex) => {
670
+ const masterEmit = {
671
+ partPath: `ppt/slideMasters/slideMaster${masterIndex + 1}.xml`,
672
+ baseRelationshipCount: master.layouts.length + 1,
673
+ index: 1,
674
+ diagnostics: [],
675
+ relationships: [],
676
+ relationshipIds: /* @__PURE__ */ new Map(),
677
+ resourceObservations: []
678
+ };
679
+ const masterCtx = {
680
+ document: docEmit,
681
+ slide: masterEmit
682
+ };
683
+ const masterCapXml = emitMasterOrLayoutCapabilities(master.slideCapabilities, internal, docEmit, masterEmit, target, options);
684
+ let masterBackgroundXml = "";
685
+ if (master.background !== void 0) {
686
+ checkExtensions(master.background.extensions, target, docEmit.usedExtensions, masterEmit.diagnostics, master.background.path);
687
+ masterBackgroundXml = master.background.emit({
688
+ path: master.background.path,
689
+ dimensions: internal.dimensions,
690
+ theme: master.background.theme,
691
+ options,
692
+ slideNumber: 1,
693
+ createTextFieldId: () => allocateTextFieldId(docEmit),
694
+ registerResource: (resource) => addResource(resource, masterCtx),
695
+ registerPackagePart: (part) => addSlidePackagePart(part, masterCtx),
696
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, masterCtx)
697
+ }).xml;
698
+ }
699
+ let nextMasterObjectId = Math.max(maxResolvedObjectId(master.layers ?? []), 1 + Object.keys(master.placeholders ?? {}).length) + 1;
700
+ const masterShapeFragments = (master.layers ?? []).map((layer) => emitLayer(layer, slideIndexById, docEmit, masterEmit, target, options));
701
+ if (master.footerPlaceholders !== void 0) {
702
+ checkExtensions(master.footerPlaceholders.extensions, target, docEmit.usedExtensions, masterEmit.diagnostics, master.footerPlaceholders.path);
703
+ const fpOut = master.footerPlaceholders.emit({
704
+ path: master.footerPlaceholders.path,
705
+ dimensions: internal.dimensions,
706
+ theme: master.footerPlaceholders.theme,
707
+ options,
708
+ slideNumber: 1,
709
+ createTextFieldId: () => allocateTextFieldId(docEmit),
710
+ allocateObjectId: () => nextMasterObjectId++,
711
+ registerResource: (resource) => addResource(resource, masterCtx),
712
+ registerPackagePart: (part) => addSlidePackagePart(part, masterCtx),
713
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, masterCtx)
714
+ });
715
+ masterShapeFragments.push(fpOut.xml);
716
+ for (const contribution of fpOut.presentationContributions ?? []) addPresentationContribution(contribution, {
717
+ document: docEmit,
718
+ slide: masterEmit
719
+ }, master.footerPlaceholders.path);
720
+ }
721
+ const masterShapeXml = mastersEmitDriver !== void 0 ? mastersEmitDriver.emitSharedCanvas(masterShapeFragments) : masterShapeFragments.join("");
722
+ const masterPlaceholderSpPrByKey = emitOwnerPlaceholderVisuals(master.placeholderVisuals, internal, docEmit, masterEmit, masterCtx, target, options);
723
+ if (masterEmit.diagnostics.length > 0) throw new BuildError(masterEmit.diagnostics);
724
+ const emittedLayouts = master.layouts.map((layout) => {
725
+ const layoutEmit = {
726
+ partPath: `ppt/slideLayouts/slideLayout${++globalLayoutNumber}.xml`,
727
+ baseRelationshipCount: hasThemeOverrideContent(layout.themeOverride) ? 2 : 1,
728
+ index: 1,
729
+ diagnostics: [],
730
+ relationships: [],
731
+ relationshipIds: /* @__PURE__ */ new Map(),
732
+ resourceObservations: []
733
+ };
734
+ const layoutCtx = {
735
+ document: docEmit,
736
+ slide: layoutEmit
737
+ };
738
+ const layoutCapXml = emitMasterOrLayoutCapabilities(layout.slideCapabilities, internal, docEmit, layoutEmit, target, options);
739
+ let layoutBackgroundXml = "";
740
+ if (layout.background !== void 0) {
741
+ checkExtensions(layout.background.extensions, target, docEmit.usedExtensions, layoutEmit.diagnostics, layout.background.path);
742
+ layoutBackgroundXml = layout.background.emit({
743
+ path: layout.background.path,
744
+ dimensions: internal.dimensions,
745
+ theme: layout.background.theme,
746
+ options,
747
+ slideNumber: 1,
748
+ createTextFieldId: () => allocateTextFieldId(docEmit),
749
+ registerResource: (resource) => addResource(resource, layoutCtx),
750
+ registerPackagePart: (part) => addSlidePackagePart(part, layoutCtx),
751
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, layoutCtx)
752
+ }).xml;
753
+ }
754
+ let nextLayoutObjectId = Math.max(maxResolvedObjectId(layout.layers ?? []), 1 + Object.keys(layout.placeholders ?? {}).length) + 1;
755
+ const layoutShapeFragments = (layout.layers ?? []).map((layer) => emitLayer(layer, slideIndexById, docEmit, layoutEmit, target, options));
756
+ if (layout.footerPlaceholders !== void 0) {
757
+ checkExtensions(layout.footerPlaceholders.extensions, target, docEmit.usedExtensions, layoutEmit.diagnostics, layout.footerPlaceholders.path);
758
+ const fpOut = layout.footerPlaceholders.emit({
759
+ path: layout.footerPlaceholders.path,
760
+ dimensions: internal.dimensions,
761
+ theme: layout.footerPlaceholders.theme,
762
+ options,
763
+ slideNumber: 1,
764
+ createTextFieldId: () => allocateTextFieldId(docEmit),
765
+ allocateObjectId: () => nextLayoutObjectId++,
766
+ registerResource: (resource) => addResource(resource, layoutCtx),
767
+ registerPackagePart: (part) => addSlidePackagePart(part, layoutCtx),
768
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, layoutCtx)
769
+ });
770
+ layoutShapeFragments.push(fpOut.xml);
771
+ for (const contribution of fpOut.presentationContributions ?? []) addPresentationContribution(contribution, {
772
+ document: docEmit,
773
+ slide: layoutEmit
774
+ }, layout.footerPlaceholders.path);
775
+ }
776
+ const layoutShapeXml = mastersEmitDriver !== void 0 ? mastersEmitDriver.emitSharedCanvas(layoutShapeFragments) : layoutShapeFragments.join("");
777
+ const layoutPlaceholderSpPrByKey = emitOwnerPlaceholderVisuals(layout.placeholderVisuals, internal, docEmit, layoutEmit, layoutCtx, target, options);
778
+ if (layoutEmit.diagnostics.length > 0) throw new BuildError(layoutEmit.diagnostics);
779
+ return {
780
+ key: layout.key,
781
+ ...layout.name === void 0 ? {} : { name: layout.name },
782
+ ...layout.type === void 0 ? {} : { type: layout.type },
783
+ ...layout.preserve === void 0 ? {} : { preserve: layout.preserve },
784
+ ...layout.matchingName === void 0 ? {} : { matchingName: layout.matchingName },
785
+ ...layout.userDrawn === void 0 ? {} : { userDrawn: layout.userDrawn },
786
+ ...layout.showMasterShapes === void 0 ? {} : { showMasterShapes: layout.showMasterShapes },
787
+ ...layout.showMasterPlaceholderAnimations === void 0 ? {} : { showMasterPlaceholderAnimations: layout.showMasterPlaceholderAnimations },
788
+ ...layout.placeholders === void 0 ? {} : { placeholders: layout.placeholders },
789
+ ...layoutPlaceholderSpPrByKey === void 0 ? {} : { placeholderSpPrByKey: layoutPlaceholderSpPrByKey },
790
+ ...layout.colorMapOverride === void 0 ? {} : { colorMapOverride: layout.colorMapOverride },
791
+ ...layout.themeOverride === void 0 ? {} : { themeOverride: layout.themeOverride },
792
+ ...layout.headerFooter === void 0 ? {} : { headerFooter: layout.headerFooter },
793
+ ...layoutBackgroundXml === "" ? {} : { backgroundXml: layoutBackgroundXml },
794
+ ...layoutShapeXml === "" ? {} : { shapeXml: layoutShapeXml },
795
+ ...layoutEmit.relationships.length === 0 ? {} : { extraRelationships: layoutEmit.relationships },
796
+ ...layoutCapXml.transitionXml === "" ? {} : { transitionXml: layoutCapXml.transitionXml },
797
+ ...layoutCapXml.timingXml === "" ? {} : { timingXml: layoutCapXml.timingXml }
798
+ };
799
+ });
800
+ return {
801
+ key: master.key,
802
+ ...master.name === void 0 ? {} : { name: master.name },
803
+ ...master.preserve === void 0 ? {} : { preserve: master.preserve },
804
+ theme: master.theme,
805
+ themeContext: master.themeContext,
806
+ colorMap: master.colorMap,
807
+ ...master.placeholders === void 0 ? {} : { placeholders: master.placeholders },
808
+ ...masterPlaceholderSpPrByKey === void 0 ? {} : { placeholderSpPrByKey: masterPlaceholderSpPrByKey },
809
+ ...master.textStyles === void 0 ? {} : { textStyles: master.textStyles },
810
+ ...master.headerFooter === void 0 ? {} : { headerFooter: master.headerFooter },
811
+ ...masterBackgroundXml === "" ? {} : { backgroundXml: masterBackgroundXml },
812
+ ...masterShapeXml === "" ? {} : { shapeXml: masterShapeXml },
813
+ ...masterEmit.relationships.length === 0 ? {} : { extraRelationships: masterEmit.relationships },
814
+ ...masterCapXml.transitionXml === "" ? {} : { transitionXml: masterCapXml.transitionXml },
815
+ ...masterCapXml.timingXml === "" ? {} : { timingXml: masterCapXml.timingXml },
816
+ layouts: emittedLayouts
817
+ };
818
+ });
819
+ const emittedSlides = internal.slides.map((resolvedSlide) => {
820
+ const slideEmit = {
821
+ partPath: `ppt/slides/slide${resolvedSlide.index}.xml`,
822
+ baseRelationshipCount: 1,
823
+ index: resolvedSlide.index,
824
+ diagnostics: [],
825
+ relationships: [],
826
+ relationshipIds: /* @__PURE__ */ new Map(),
827
+ resourceObservations: []
828
+ };
829
+ const packageCtx = {
830
+ document: docEmit,
831
+ slide: slideEmit
832
+ };
833
+ let nextSlideObjectId = maxResolvedObjectId(resolvedSlide.layers) + 1;
834
+ let shapeXml = resolvedSlide.layers.map((layer) => emitLayer(layer, slideIndexById, docEmit, slideEmit, target, options)).join("");
835
+ let backgroundXml = "";
836
+ const slideXmlParts = [];
837
+ for (const slideCap of resolvedSlide.slideCapabilities) {
838
+ checkExtensions(slideCap.extensions, target, docEmit.usedExtensions, slideEmit.diagnostics, slideCap.path);
839
+ try {
840
+ const slideEmitContext = {
841
+ path: slideCap.path,
842
+ dimensions: internal.dimensions,
843
+ theme: slideCap.theme,
844
+ options,
845
+ slideNumber: resolvedSlide.index,
846
+ createTextFieldId: () => allocateTextFieldId(docEmit),
847
+ allocateObjectId: () => nextSlideObjectId++,
848
+ registerResource: (resource) => addResource(resource, packageCtx),
849
+ registerPackagePart: (part) => addSlidePackagePart(part, packageCtx),
850
+ registerSharedPart: (key, part) => addSharedPackagePart(key, part, packageCtx)
851
+ };
852
+ const output = slideCap.emit(slideEmitContext);
853
+ if (slideCap.slot === "background") backgroundXml += output.xml;
854
+ else if (slideCap.slot === "shape-tree") shapeXml += output.xml;
855
+ else slideXmlParts.push({
856
+ slot: slideCap.slot,
857
+ xml: output.xml
858
+ });
859
+ for (const contribution of output.presentationContributions ?? []) addPresentationContribution(contribution, {
860
+ document: docEmit,
861
+ slide: slideEmit
862
+ }, slideCap.path);
863
+ } catch (error) {
864
+ slideEmit.diagnostics.push(error instanceof CapabilityError ? diagnostic(error.path ?? slideCap.path, error.code, error.message) : diagnostic(slideCap.path, "capability-failure", error instanceof Error ? error.message : String(error)));
865
+ }
866
+ }
867
+ if (slideEmit.diagnostics.length > 0) throw new BuildError(slideEmit.diagnostics);
868
+ return {
869
+ id: resolvedSlide.id,
870
+ ...resolvedSlide.name === void 0 ? {} : { name: resolvedSlide.name },
871
+ ...resolvedSlide.layoutKey === void 0 ? {} : { layoutKey: resolvedSlide.layoutKey },
872
+ ...resolvedSlide.colorMapOverride === void 0 ? {} : { colorMapOverride: resolvedSlide.colorMapOverride },
873
+ ...resolvedSlide.themeOverride === void 0 ? {} : { themeOverride: resolvedSlide.themeOverride },
874
+ hidden: resolvedSlide.hidden,
875
+ ...resolvedSlide.showMasterPlaceholderAnimations === void 0 ? {} : { showMasterPlaceholderAnimations: resolvedSlide.showMasterPlaceholderAnimations },
876
+ ...resolvedSlide.showMasterShapes === void 0 ? {} : { showMasterShapes: resolvedSlide.showMasterShapes },
877
+ shapeXml,
878
+ backgroundXml,
879
+ slideXml: slideXmlParts.map((item, index) => ({
880
+ item,
881
+ index
882
+ })).sort((left, right) => slideSlotOrder(left.item.slot) - slideSlotOrder(right.item.slot) || left.index - right.index).map(({ item }) => item.xml).join(""),
883
+ relationships: slideEmit.relationships,
884
+ resourceObservations: slideEmit.resourceObservations
885
+ };
886
+ });
887
+ finalizePresentationContributions(docEmit);
888
+ const parts = pptxParts(internal.dimensions, emittedSlides.map(({ id, name, layoutKey, colorMapOverride, themeOverride, hidden, showMasterPlaceholderAnimations, showMasterShapes, shapeXml, backgroundXml, slideXml, relationships }) => ({
889
+ id,
890
+ ...name === void 0 ? {} : { name },
891
+ ...layoutKey === void 0 ? {} : { layoutKey },
892
+ ...colorMapOverride === void 0 ? {} : { colorMapOverride },
893
+ ...themeOverride === void 0 ? {} : { themeOverride },
894
+ hidden,
895
+ ...showMasterPlaceholderAnimations === void 0 ? {} : { showMasterPlaceholderAnimations },
896
+ ...showMasterShapes === void 0 ? {} : { showMasterShapes },
897
+ shapeXml,
898
+ backgroundXml,
899
+ slideXml,
900
+ relationships
901
+ })), [...docEmit.resources.values()], [...docEmit.packageParts.values()], [...docEmit.packageRelationships].map(([sourcePath, relationships]) => ({
902
+ sourcePath,
903
+ relationships
904
+ })), presentationContent(docEmit), [...docEmit.presentationAttributes].map(([name, value]) => ({
905
+ name,
906
+ value
907
+ })), internal.theme.theme, internal.title, sectionXml, emittedMasters, mastersEmitDriver);
908
+ const pptx = zipSync(Object.fromEntries(Object.entries(parts).map(([partPath, content]) => [partPath, typeof content === "string" ? strToU8(content) : content])), {
909
+ level: 6,
910
+ mtime: /* @__PURE__ */ new Date("2020-01-01T00:00:00Z")
911
+ });
912
+ const report = Object.freeze({
913
+ evidence: "generation",
914
+ ...typeof internal.title === "string" ? { title: internal.title } : {},
915
+ target: Object.freeze({
916
+ name: target.name,
917
+ format: target.format,
918
+ extensions: Object.freeze([...target.extensions])
919
+ }),
920
+ extensions: Object.freeze([...docEmit.usedExtensions]),
921
+ ...resolution.report.masters === void 0 ? {} : { masters: resolution.report.masters },
922
+ slides: resolution.report.slides,
923
+ diagnostics: Object.freeze(docEmit.warnings.map((warning) => Object.freeze({ ...warning })))
924
+ });
925
+ const query = createBuildQuery(internal.slides.map((slide, index) => ({
926
+ observation: report.slides[index],
927
+ content: slide.content,
928
+ resources: emittedSlides[index].resourceObservations
929
+ })));
930
+ emittedQueries.set(report, query);
931
+ return Object.freeze({
932
+ pptx,
933
+ report
934
+ });
935
+ }
936
+ export { emit, emittedBuildQuery, validateOptions };