documents.js 1.32.0 → 1.33.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -2,154 +2,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  let ooxml_js = require("ooxml.js");
3
3
  let zod = require("zod");
4
4
  let fflate = require("fflate");
5
- //#region src/model/color.ts
6
- const LayoutColorSchema = zod.z.object({
7
- r: zod.z.number().min(0).max(1),
8
- g: zod.z.number().min(0).max(1),
9
- b: zod.z.number().min(0).max(1)
10
- });
11
- const COLOR_BLACK = {
12
- r: 0,
13
- g: 0,
14
- b: 0
15
- };
16
- const HEX_COLOR_PATTERN = /^#?([0-9a-fA-F]{6})$/;
17
- const HEX_BYTE_MAX = 255;
18
- function rgbHexToColor(hex) {
19
- const match = HEX_COLOR_PATTERN.exec(hex);
20
- if (match === null) throw new Error(`not a 6-digit hex colour: ${hex}`);
21
- const digits = match[1];
22
- if (digits === void 0) throw new Error(`not a 6-digit hex colour: ${hex}`);
23
- const r = Number.parseInt(digits.slice(0, 2), 16);
24
- const g = Number.parseInt(digits.slice(2, 4), 16);
25
- const b = Number.parseInt(digits.slice(4, 6), 16);
26
- return {
27
- r: r / HEX_BYTE_MAX,
28
- g: g / HEX_BYTE_MAX,
29
- b: b / HEX_BYTE_MAX
30
- };
31
- }
32
- function toHexByte(component) {
33
- return Math.round(component * HEX_BYTE_MAX).toString(16).padStart(2, "0");
34
- }
35
- function colorToRgbHex(color) {
36
- return `${toHexByte(color.r)}${toHexByte(color.g)}${toHexByte(color.b)}`;
37
- }
38
- const OOXML_PERCENT_SCALE = 1e5;
39
- function clamp01(x) {
40
- return Math.max(0, Math.min(1, x));
41
- }
42
- function srgbToLinear(c) {
43
- return c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4;
44
- }
45
- function linearToSrgb(c) {
46
- return c <= .0031308 ? c * 12.92 : 1.055 * c ** (1 / 2.4) - .055;
47
- }
48
- function applyShadeOrTint(color, kind, value) {
49
- const pct = value / OOXML_PERCENT_SCALE;
50
- const transform = kind === "shade" ? (linear) => linear * pct : (linear) => 1 - (1 - linear) * pct;
51
- return {
52
- r: clamp01(linearToSrgb(transform(srgbToLinear(color.r)))),
53
- g: clamp01(linearToSrgb(transform(srgbToLinear(color.g)))),
54
- b: clamp01(linearToSrgb(transform(srgbToLinear(color.b))))
55
- };
56
- }
57
- function rgbToHsl(color) {
58
- const { r, g, b } = color;
59
- const max = Math.max(r, g, b);
60
- const min = Math.min(r, g, b);
61
- const l = (max + min) / 2;
62
- if (max === min) return {
63
- h: 0,
64
- s: 0,
65
- l
66
- };
67
- const d = max - min;
68
- const s = l > .5 ? d / (2 - max - min) : d / (max + min);
69
- let h;
70
- if (max === r) h = (g - b) / d + (g < b ? 6 : 0);
71
- else if (max === g) h = (b - r) / d + 2;
72
- else h = (r - g) / d + 4;
73
- return {
74
- h: h * 60,
75
- s,
76
- l
77
- };
78
- }
79
- function hueToRgbComponent(p, q, hue) {
80
- let t = hue;
81
- if (t < 0) t += 1;
82
- if (t > 1) t -= 1;
83
- if (t < 1 / 6) return p + (q - p) * 6 * t;
84
- if (t < 1 / 2) return q;
85
- if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6;
86
- return p;
87
- }
88
- function hslToRgb(hsl) {
89
- const { h, s, l } = hsl;
90
- if (s === 0) return {
91
- r: l,
92
- g: l,
93
- b: l
94
- };
95
- const q = l < .5 ? l * (1 + s) : l + s - l * s;
96
- const p = 2 * l - q;
97
- const hk = h / 360;
98
- return {
99
- r: clamp01(hueToRgbComponent(p, q, hk + 1 / 3)),
100
- g: clamp01(hueToRgbComponent(p, q, hk)),
101
- b: clamp01(hueToRgbComponent(p, q, hk - 1 / 3))
102
- };
103
- }
104
- function applyLumModOrOff(color, kind, value) {
105
- const hsl = rgbToHsl(color);
106
- const pct = value / OOXML_PERCENT_SCALE;
107
- const l = clamp01(kind === "lumMod" ? hsl.l * pct : hsl.l + pct);
108
- return hslToRgb({
109
- ...hsl,
110
- l
111
- });
112
- }
113
- function applyColorTransforms(base, transforms) {
114
- let color = base;
115
- for (const t of transforms) if (t.kind === "shade" || t.kind === "tint") color = applyShadeOrTint(color, t.kind, t.value);
116
- for (const t of transforms) if (t.kind === "lumMod" || t.kind === "lumOff") color = applyLumModOrOff(color, t.kind, t.value);
117
- return color;
118
- }
119
- //#endregion
120
5
  //#region src/model/geometry.ts
121
- const BoxSchema = zod.z.object({
122
- xPt: zod.z.number(),
123
- yPt: zod.z.number(),
124
- widthPt: zod.z.number().nonnegative(),
125
- heightPt: zod.z.number().nonnegative()
126
- });
127
- const PageSizeSchema = zod.z.object({
128
- widthPt: zod.z.number().positive(),
129
- heightPt: zod.z.number().positive()
130
- });
131
- const MarginsSchema = zod.z.object({
132
- topPt: zod.z.number().nonnegative(),
133
- rightPt: zod.z.number().nonnegative(),
134
- bottomPt: zod.z.number().nonnegative(),
135
- leftPt: zod.z.number().nonnegative()
136
- });
137
- const PAGE_SIZE_LETTER = {
138
- widthPt: 612,
139
- heightPt: 792
140
- };
141
- const PAGE_SIZE_A4 = {
142
- widthPt: 595.28,
143
- heightPt: 841.89
144
- };
145
- const SLIDE_SIZE_WIDESCREEN = {
146
- widthPt: 960,
147
- heightPt: 540
148
- };
149
- const SLIDE_SIZE_STANDARD = {
150
- widthPt: 720,
151
- heightPt: 540
152
- };
153
6
  function flipY(box, containerHeightPt) {
154
7
  return {
155
8
  xPt: box.xPt,
@@ -170,12 +23,6 @@ const DEFAULT_LAYOUT_FONT = {
170
23
  weight: "normal",
171
24
  style: "normal"
172
25
  };
173
- const AlignmentSchema = zod.z.enum([
174
- "left",
175
- "center",
176
- "right",
177
- "justify"
178
- ]);
179
26
  //#endregion
180
27
  //#region src/model/layout.ts
181
28
  const LAYOUT_FORMAT_VERSION = 1;
@@ -186,7 +33,7 @@ const LayoutTextSchema = zod.z.object({
186
33
  yPt: zod.z.number(),
187
34
  font: LayoutFontSchema,
188
35
  sizePt: zod.z.number().positive(),
189
- color: LayoutColorSchema,
36
+ color: ooxml_js.ColorSchema,
190
37
  widthPt: zod.z.number().nonnegative().optional(),
191
38
  rotationDeg: zod.z.number().optional(),
192
39
  underline: zod.z.boolean().optional()
@@ -206,9 +53,9 @@ const LayoutRectSchema = zod.z.object({
206
53
  yPt: zod.z.number(),
207
54
  widthPt: zod.z.number().nonnegative(),
208
55
  heightPt: zod.z.number().nonnegative(),
209
- fill: LayoutColorSchema.optional(),
56
+ fill: ooxml_js.ColorSchema.optional(),
210
57
  stroke: zod.z.object({
211
- color: LayoutColorSchema,
58
+ color: ooxml_js.ColorSchema,
212
59
  widthPt: zod.z.number().positive()
213
60
  }).optional()
214
61
  });
@@ -218,7 +65,7 @@ const LayoutLineSchema = zod.z.object({
218
65
  y1Pt: zod.z.number(),
219
66
  x2Pt: zod.z.number(),
220
67
  y2Pt: zod.z.number(),
221
- color: LayoutColorSchema,
68
+ color: ooxml_js.ColorSchema,
222
69
  widthPt: zod.z.number().positive()
223
70
  });
224
71
  const LayoutEllipseSchema = zod.z.object({
@@ -227,9 +74,9 @@ const LayoutEllipseSchema = zod.z.object({
227
74
  yPt: zod.z.number(),
228
75
  widthPt: zod.z.number().positive(),
229
76
  heightPt: zod.z.number().positive(),
230
- fill: LayoutColorSchema.optional(),
77
+ fill: ooxml_js.ColorSchema.optional(),
231
78
  stroke: zod.z.object({
232
- color: LayoutColorSchema,
79
+ color: ooxml_js.ColorSchema,
233
80
  widthPt: zod.z.number().positive()
234
81
  }).optional()
235
82
  });
@@ -270,7 +117,7 @@ const LayoutMetadataSchema = zod.z.object({
270
117
  createdIso: zod.z.string().optional(),
271
118
  modifiedIso: zod.z.string().optional()
272
119
  });
273
- zod.z.object({
120
+ const LayoutDocumentSchema = zod.z.object({
274
121
  formatVersion: zod.z.literal(1),
275
122
  metadata: LayoutMetadataSchema,
276
123
  pages: zod.z.array(LayoutPageSchema),
@@ -287,7 +134,7 @@ const ContentRunSchema = zod.z.object({
287
134
  strike: zod.z.boolean().optional(),
288
135
  fontFamily: zod.z.string().optional(),
289
136
  sizePt: zod.z.number().positive().optional(),
290
- color: LayoutColorSchema.optional(),
137
+ color: ooxml_js.ColorSchema.optional(),
291
138
  hyperlink: zod.z.string().optional()
292
139
  });
293
140
  const ContentListMembershipSchema = zod.z.object({
@@ -298,7 +145,7 @@ const ContentParagraphSchema = zod.z.object({
298
145
  kind: zod.z.literal("paragraph"),
299
146
  runs: zod.z.array(ContentRunSchema),
300
147
  styleId: zod.z.string().optional(),
301
- alignment: AlignmentSchema.optional(),
148
+ alignment: ooxml_js.AlignmentSchema.optional(),
302
149
  list: ContentListMembershipSchema.optional(),
303
150
  spacingBeforePt: zod.z.number().optional(),
304
151
  spacingAfterPt: zod.z.number().optional(),
@@ -341,7 +188,7 @@ const ContentTableCellSchema = zod.z.object({
341
188
  blocks: zod.z.array(ContentBlockSchema),
342
189
  colSpan: zod.z.number().int().positive().optional(),
343
190
  rowSpan: zod.z.number().int().positive().optional(),
344
- background: LayoutColorSchema.optional()
191
+ background: ooxml_js.ColorSchema.optional()
345
192
  });
346
193
  const ContentTableRowSchema = zod.z.object({
347
194
  cells: zod.z.array(ContentTableCellSchema),
@@ -353,13 +200,13 @@ const ContentTableSchema = zod.z.object({
353
200
  columnWidthsPt: zod.z.array(zod.z.number().positive())
354
201
  });
355
202
  const ContentSectionSchema = zod.z.object({
356
- pageSize: PageSizeSchema,
357
- margins: MarginsSchema,
203
+ pageSize: ooxml_js.PageSizeSchema,
204
+ margins: ooxml_js.MarginsSchema,
358
205
  blocks: zod.z.array(ContentBlockSchema)
359
206
  });
360
207
  const ContentShapeSchema = zod.z.object({
361
208
  name: zod.z.string().optional(),
362
- frame: BoxSchema,
209
+ frame: ooxml_js.BoxSchema,
363
210
  rotationDeg: zod.z.number().optional(),
364
211
  insetLeftPt: zod.z.number().nonnegative(),
365
212
  insetTopPt: zod.z.number().nonnegative(),
@@ -370,7 +217,7 @@ const ContentShapeSchema = zod.z.object({
370
217
  blocks: zod.z.array(ContentBlockSchema)
371
218
  });
372
219
  const ContentSlideSchema = zod.z.object({
373
- size: PageSizeSchema,
220
+ size: ooxml_js.PageSizeSchema,
374
221
  shapes: zod.z.array(ContentShapeSchema),
375
222
  notes: zod.z.string()
376
223
  });
@@ -386,6 +233,45 @@ const ContentDocumentSchema = zod.z.discriminatedUnion("kind", [zod.z.object({
386
233
  slides: zod.z.array(ContentSlideSchema)
387
234
  })]);
388
235
  //#endregion
236
+ //#region src/model/bytes.ts
237
+ const ZIP_LOCAL_FILE_HEADER = [
238
+ 80,
239
+ 75,
240
+ 3,
241
+ 4
242
+ ];
243
+ const PDF_HEADER = [
244
+ 37,
245
+ 80,
246
+ 68,
247
+ 70,
248
+ 45
249
+ ];
250
+ const PDF_HEADER_SEARCH_WINDOW = 1024;
251
+ function startsWithBytes(bytes, signature) {
252
+ if (bytes.length < signature.length) return false;
253
+ for (let i = 0; i < signature.length; i++) if (bytes[i] !== signature[i]) return false;
254
+ return true;
255
+ }
256
+ function containsBytesWithin(bytes, signature, window) {
257
+ const limit = Math.min(bytes.length - signature.length, window);
258
+ for (let start = 0; start <= limit; start++) {
259
+ let matched = true;
260
+ for (let i = 0; i < signature.length; i++) if (bytes[start + i] !== signature[i]) {
261
+ matched = false;
262
+ break;
263
+ }
264
+ if (matched) return true;
265
+ }
266
+ return false;
267
+ }
268
+ function zipBytesSchema(label) {
269
+ return zod.z.instanceof(Uint8Array).refine((bytes) => startsWithBytes(bytes, ZIP_LOCAL_FILE_HEADER), { message: `not a valid ${label} file: missing the ZIP local-file-header signature` });
270
+ }
271
+ const DocxBytesSchema = zipBytesSchema("docx");
272
+ const PptxBytesSchema = zipBytesSchema("pptx");
273
+ const PdfBytesSchema = zod.z.instanceof(Uint8Array).refine((bytes) => containsBytesWithin(bytes, PDF_HEADER, PDF_HEADER_SEARCH_WINDOW), { message: "not a valid PDF file: missing the %PDF- header" });
274
+ //#endregion
389
275
  //#region src/xml/fragment.ts
390
276
  function el(tag, attrs = {}, children = []) {
391
277
  return {
@@ -463,9 +349,6 @@ function emuToPt(emu) {
463
349
  function ptToEmu(pt) {
464
350
  return Math.round(pt * EMU_PER_POINT);
465
351
  }
466
- function twipsToPt(twips) {
467
- return twips / 20;
468
- }
469
352
  function ptToTwips(pt) {
470
353
  return Math.round(pt * 20);
471
354
  }
@@ -475,12 +358,6 @@ function halfPointsToPt(halfPoints) {
475
358
  function ptToHalfPoints(pt) {
476
359
  return Math.round(pt * 2);
477
360
  }
478
- function drawingMlFontSizeToPt(hundredths) {
479
- return hundredths / 100;
480
- }
481
- function lineUnitsToMultiplier(lineUnits) {
482
- return lineUnits / 240;
483
- }
484
361
  //#endregion
485
362
  //#region src/opc/paths.ts
486
363
  function relsPathFor(partPath) {
@@ -775,10 +652,10 @@ function getColor(rPr) {
775
652
  if (color === void 0) return;
776
653
  const val = (0, ooxml_js.attr)(color, "w:val");
777
654
  if (val === void 0 || val.toLowerCase() === "auto") return;
778
- return rgbHexToColor(val);
655
+ return (0, ooxml_js.rgbHexToColor)(val);
779
656
  }
780
657
  function setColor(rPr, color) {
781
- setAttr(getOrCreateChildElement(rPr, "w:color", RPR_ORDER, () => el("w:color")), "w:val", colorToRgbHex(color));
658
+ setAttr(getOrCreateChildElement(rPr, "w:color", RPR_ORDER, () => el("w:color")), "w:val", (0, ooxml_js.colorToRgbHex)(color));
782
659
  }
783
660
  function getStyleId(propsElement, tag) {
784
661
  if (propsElement === void 0) return;
@@ -931,7 +808,7 @@ function buildRun(init = {}) {
931
808
  const half = String(Math.round(init.sizePt * 2));
932
809
  rPrChildren.push(el("w:sz", { "w:val": half }), el("w:szCs", { "w:val": half }));
933
810
  }
934
- if (init.color !== void 0) rPrChildren.push(el("w:color", { "w:val": colorToRgbHex(init.color) }));
811
+ if (init.color !== void 0) rPrChildren.push(el("w:color", { "w:val": (0, ooxml_js.colorToRgbHex)(init.color) }));
935
812
  const run = el("w:r");
936
813
  if (rPrChildren.length > 0 || init.underline === true) {
937
814
  const rPr = el("w:rPr");
@@ -1253,16 +1130,16 @@ function buildTable(init) {
1253
1130
  }
1254
1131
  //#endregion
1255
1132
  //#region src/edit/docx/editor.ts
1256
- const DOCUMENT_PART_PATH$1 = "word/document.xml";
1133
+ const DOCUMENT_PART_PATH = "word/document.xml";
1257
1134
  const MEDIA_DIR$1 = "word/media";
1258
1135
  function findDocumentRoot(pkg) {
1259
- const root = (0, ooxml_js.rootElement)(pkg.parts[DOCUMENT_PART_PATH$1]);
1260
- if (root === void 0) throw new Error(`package has no root element at ${DOCUMENT_PART_PATH$1}`);
1136
+ const root = (0, ooxml_js.rootElement)(pkg.parts[DOCUMENT_PART_PATH]);
1137
+ if (root === void 0) throw new Error(`package has no root element at ${DOCUMENT_PART_PATH}`);
1261
1138
  return root;
1262
1139
  }
1263
1140
  function findBody(documentRoot) {
1264
1141
  for (const child of documentRoot.children) if (child.type === "element" && child.tag === "w:body") return child;
1265
- throw new Error(`${DOCUMENT_PART_PATH$1} has no w:body element`);
1142
+ throw new Error(`${DOCUMENT_PART_PATH} has no w:body element`);
1266
1143
  }
1267
1144
  function bodyInsertionPoint(body) {
1268
1145
  const sectPrIndex = body.children.findIndex((c) => c.type === "element" && c.tag === "w:sectPr");
@@ -1316,7 +1193,7 @@ var DocxEditor = class {
1316
1193
  documentRoot,
1317
1194
  media: {
1318
1195
  pkg,
1319
- partPath: DOCUMENT_PART_PATH$1,
1196
+ partPath: DOCUMENT_PART_PATH,
1320
1197
  mediaDir: MEDIA_DIR$1
1321
1198
  }
1322
1199
  };
@@ -1330,7 +1207,7 @@ var DocxEditor = class {
1330
1207
  documentRoot,
1331
1208
  media: {
1332
1209
  pkg: this.pkg,
1333
- partPath: DOCUMENT_PART_PATH$1,
1210
+ partPath: DOCUMENT_PART_PATH,
1334
1211
  mediaDir: MEDIA_DIR$1
1335
1212
  }
1336
1213
  };
@@ -4734,7 +4611,7 @@ function createFontResolver(context) {
4734
4611
  }
4735
4612
  //#endregion
4736
4613
  //#region src/image/png-encode.ts
4737
- const PNG_SIGNATURE$2 = new Uint8Array([
4614
+ const PNG_SIGNATURE$1 = new Uint8Array([
4738
4615
  137,
4739
4616
  80,
4740
4617
  78,
@@ -4783,7 +4660,7 @@ function encodePng(image, options = {}) {
4783
4660
  ihdr[11] = 0;
4784
4661
  ihdr[12] = 0;
4785
4662
  const writer = new ByteWriter();
4786
- writer.writeBytes(PNG_SIGNATURE$2);
4663
+ writer.writeBytes(PNG_SIGNATURE$1);
4787
4664
  writeChunk(writer, "IHDR", ihdr);
4788
4665
  writeChunk(writer, "IDAT", compressed);
4789
4666
  writeChunk(writer, "IEND", /* @__PURE__ */ new Uint8Array(0));
@@ -5398,8 +5275,8 @@ function interpretContentStream(bytes, resources, context) {
5398
5275
  const items = [];
5399
5276
  runContentStream(bytes, resources, {
5400
5277
  ctm: IDENTITY_MATRIX,
5401
- fillColor: COLOR_BLACK,
5402
- strokeColor: COLOR_BLACK
5278
+ fillColor: ooxml_js.COLOR_BLACK,
5279
+ strokeColor: ooxml_js.COLOR_BLACK
5403
5280
  }, context, items, 0);
5404
5281
  return items;
5405
5282
  }
@@ -6016,7 +5893,7 @@ function readMetadata(trailer, resolver) {
6016
5893
  }
6017
5894
  //#endregion
6018
5895
  //#region src/image/png-decode.ts
6019
- const PNG_SIGNATURE$1 = [
5896
+ const PNG_SIGNATURE = [
6020
5897
  137,
6021
5898
  80,
6022
5899
  78,
@@ -6032,7 +5909,7 @@ function requireDataView(bytes) {
6032
5909
  function readChunks(bytes, onWarning) {
6033
5910
  const chunks = [];
6034
5911
  const view = requireDataView(bytes);
6035
- let offset = PNG_SIGNATURE$1.length;
5912
+ let offset = PNG_SIGNATURE.length;
6036
5913
  while (offset + 8 <= bytes.length) {
6037
5914
  const length = view.getUint32(offset);
6038
5915
  const typeBytes = bytes.subarray(offset + 4, offset + 8);
@@ -6107,7 +5984,7 @@ function scaleToByte(sample, bitDepth) {
6107
5984
  return Math.round(sample * 255 / maxSample);
6108
5985
  }
6109
5986
  function decodePng(bytes, options = {}) {
6110
- for (let i = 0; i < PNG_SIGNATURE$1.length; i++) if (bytes[i] !== PNG_SIGNATURE$1[i]) throw new Error("not a valid PNG file: bad signature");
5987
+ for (let i = 0; i < PNG_SIGNATURE.length; i++) if (bytes[i] !== PNG_SIGNATURE[i]) throw new Error("not a valid PNG file: bad signature");
6111
5988
  const chunks = readChunks(bytes, options.onWarning);
6112
5989
  const ihdrChunk = chunks[0];
6113
5990
  if (ihdrChunk?.type !== "IHDR") throw new Error("PNG file does not begin with an IHDR chunk");
@@ -6737,1066 +6614,31 @@ function writePdf(doc, options = {}) {
6737
6614
  return writer.toBytes();
6738
6615
  }
6739
6616
  //#endregion
6740
- //#region src/ooxml/core-properties.ts
6741
- const CORE_PROPERTIES_PATH = "docProps/core.xml";
6742
- const APP_PROPERTIES_PATH = "docProps/app.xml";
6743
- function firstElementText(root, tag) {
6744
- if (root === void 0) return;
6745
- const element = (0, ooxml_js.childrenWithTag)(root, tag)[0];
6746
- if (element === void 0) return;
6747
- const text = (0, ooxml_js.textContent)(element);
6748
- return text.length > 0 ? text : void 0;
6749
- }
6750
- function readKeywords(core) {
6751
- const raw = firstElementText(core, "cp:keywords");
6752
- if (raw === void 0) return;
6753
- const parts = raw.split(",").map((part) => part.trim()).filter((part) => part.length > 0);
6754
- return parts.length > 0 ? parts : void 0;
6755
- }
6756
- function readCoreProperties(pkg) {
6757
- const core = (0, ooxml_js.rootElement)(pkg.parts[CORE_PROPERTIES_PATH]);
6758
- const app = (0, ooxml_js.rootElement)(pkg.parts[APP_PROPERTIES_PATH]);
6759
- return {
6760
- title: firstElementText(core, "dc:title"),
6761
- author: firstElementText(core, "dc:creator"),
6762
- subject: firstElementText(core, "dc:subject"),
6763
- keywords: readKeywords(core),
6764
- creator: firstElementText(app, "Application"),
6765
- createdIso: firstElementText(core, "dcterms:created"),
6766
- modifiedIso: firstElementText(core, "dcterms:modified")
6767
- };
6768
- }
6769
- //#endregion
6770
- //#region src/ooxml/drawingml.ts
6771
- const ROTATION_UNITS_PER_DEGREE = 6e4;
6772
- function readXfrm(xfrm) {
6773
- if (xfrm === void 0) return;
6774
- const off = (0, ooxml_js.childrenWithTag)(xfrm, "a:off")[0];
6775
- const ext = (0, ooxml_js.childrenWithTag)(xfrm, "a:ext")[0];
6776
- if (off === void 0 || ext === void 0) return;
6777
- const x = (0, ooxml_js.attr)(off, "x");
6778
- const y = (0, ooxml_js.attr)(off, "y");
6779
- const cx = (0, ooxml_js.attr)(ext, "cx");
6780
- const cy = (0, ooxml_js.attr)(ext, "cy");
6781
- if (x === void 0 || y === void 0 || cx === void 0 || cy === void 0) return;
6782
- const rot = (0, ooxml_js.attr)(xfrm, "rot");
6783
- return {
6784
- xPt: emuToPt(Number(x)),
6785
- yPt: emuToPt(Number(y)),
6786
- widthPt: emuToPt(Number(cx)),
6787
- heightPt: emuToPt(Number(cy)),
6788
- rotationDeg: rot === void 0 ? 0 : Number(rot) / ROTATION_UNITS_PER_DEGREE,
6789
- flipH: (0, ooxml_js.attr)(xfrm, "flipH") === "1",
6790
- flipV: (0, ooxml_js.attr)(xfrm, "flipV") === "1"
6791
- };
6792
- }
6793
- const CLR_SCHEME_SLOTS = [
6794
- "dk1",
6795
- "lt1",
6796
- "dk2",
6797
- "lt2",
6798
- "accent1",
6799
- "accent2",
6800
- "accent3",
6801
- "accent4",
6802
- "accent5",
6803
- "accent6",
6804
- "hlink",
6805
- "folHlink"
6806
- ];
6807
- function readThemeSlotColor(colorEl) {
6808
- if (colorEl.tag === "a:srgbClr") {
6809
- const val = (0, ooxml_js.attr)(colorEl, "val");
6810
- return val === void 0 ? void 0 : rgbHexToColor(val);
6811
- }
6812
- if (colorEl.tag === "a:sysClr") {
6813
- const lastClr = (0, ooxml_js.attr)(colorEl, "lastClr");
6814
- if (lastClr !== void 0) return rgbHexToColor(lastClr);
6815
- return (0, ooxml_js.attr)(colorEl, "val") === "window" ? {
6816
- r: 1,
6817
- g: 1,
6818
- b: 1
6819
- } : {
6820
- r: 0,
6821
- g: 0,
6822
- b: 0
6823
- };
6824
- }
6825
- }
6826
- function readClrScheme(clrSchemeEl) {
6827
- const map = /* @__PURE__ */ new Map();
6828
- for (const slot of CLR_SCHEME_SLOTS) {
6829
- const wrapper = (0, ooxml_js.childrenWithTag)(clrSchemeEl, `a:${slot}`)[0];
6830
- if (wrapper === void 0) continue;
6831
- const colorEl = wrapper.children.find((c) => c.type === "element");
6832
- if (colorEl === void 0) continue;
6833
- const color = readThemeSlotColor(colorEl);
6834
- if (color !== void 0) map.set(slot, color);
6835
- }
6836
- return map;
6837
- }
6838
- function readSchemeFont(fontSchemeEl, tag) {
6839
- if (fontSchemeEl === void 0) return;
6840
- const fontEl = (0, ooxml_js.childrenWithTag)(fontSchemeEl, tag)[0];
6841
- const latin = fontEl === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(fontEl, "a:latin")[0];
6842
- return latin === void 0 ? void 0 : (0, ooxml_js.attr)(latin, "typeface");
6843
- }
6844
- const DEFAULT_THEME_FONT = "Calibri";
6845
- const EMPTY_THEME = {
6846
- colorScheme: /* @__PURE__ */ new Map(),
6847
- majorFont: DEFAULT_THEME_FONT,
6848
- minorFont: DEFAULT_THEME_FONT
6849
- };
6850
- function readTheme(themeRoot) {
6851
- const clrSchemeEl = (0, ooxml_js.elementsWithTag)([themeRoot], "a:clrScheme")[0];
6852
- const colorScheme = clrSchemeEl === void 0 ? /* @__PURE__ */ new Map() : readClrScheme(clrSchemeEl);
6853
- const fontSchemeEl = (0, ooxml_js.elementsWithTag)([themeRoot], "a:fontScheme")[0];
6854
- return {
6855
- colorScheme,
6856
- majorFont: readSchemeFont(fontSchemeEl, "a:majorFont") ?? DEFAULT_THEME_FONT,
6857
- minorFont: readSchemeFont(fontSchemeEl, "a:minorFont") ?? DEFAULT_THEME_FONT
6858
- };
6859
- }
6860
- function resolveThemeFontReference(typeface, theme) {
6861
- if (typeface === "+mj-lt") return theme.majorFont;
6862
- if (typeface === "+mn-lt") return theme.minorFont;
6863
- return typeface;
6864
- }
6865
- function readColorMap(clrMapEl) {
6866
- const map = /* @__PURE__ */ new Map();
6867
- if (clrMapEl === void 0) return map;
6868
- for (const a of clrMapEl.attributes) map.set(a.name, a.value);
6869
- return map;
6870
- }
6871
- function resolveSchemeColorSlot(schemeVal, colorMap) {
6872
- return colorMap.get(schemeVal) ?? schemeVal;
6873
- }
6874
- const COLOR_TRANSFORM_TAGS = /* @__PURE__ */ new Map([
6875
- ["a:shade", "shade"],
6876
- ["a:tint", "tint"],
6877
- ["a:lumMod", "lumMod"],
6878
- ["a:lumOff", "lumOff"]
6879
- ]);
6880
- function readColorTransforms(container) {
6881
- const transforms = [];
6882
- for (const child of container.children) {
6883
- if (child.type !== "element") continue;
6884
- const kind = COLOR_TRANSFORM_TAGS.get(child.tag);
6885
- if (kind === void 0) continue;
6886
- const val = (0, ooxml_js.attr)(child, "val");
6887
- if (val === void 0) continue;
6888
- transforms.push({
6889
- kind,
6890
- value: Number(val)
6891
- });
6892
- }
6893
- return transforms;
6894
- }
6895
- function readSchemeColor(schemeClrEl, colorMap, theme) {
6896
- const val = (0, ooxml_js.attr)(schemeClrEl, "val");
6897
- if (val === void 0) return;
6898
- const base = theme.colorScheme.get(resolveSchemeColorSlot(val, colorMap));
6899
- return base === void 0 ? void 0 : applyColorTransforms(base, readColorTransforms(schemeClrEl));
6900
- }
6901
- function readSrgbColor(srgbClrEl) {
6902
- const val = (0, ooxml_js.attr)(srgbClrEl, "val");
6903
- return val === void 0 ? void 0 : applyColorTransforms(rgbHexToColor(val), readColorTransforms(srgbClrEl));
6904
- }
6905
- function readSolidFillColor(solidFillEl, colorMap, theme) {
6906
- if (solidFillEl === void 0) return;
6907
- const schemeClr = (0, ooxml_js.childrenWithTag)(solidFillEl, "a:schemeClr")[0];
6908
- if (schemeClr !== void 0) return readSchemeColor(schemeClr, colorMap, theme);
6909
- const srgbClr = (0, ooxml_js.childrenWithTag)(solidFillEl, "a:srgbClr")[0];
6910
- return srgbClr === void 0 ? void 0 : readSrgbColor(srgbClr);
6911
- }
6912
- function readGroupXfrm(xfrm) {
6913
- const base = readXfrm(xfrm);
6914
- if (base === void 0 || xfrm === void 0) return;
6915
- const chOff = (0, ooxml_js.childrenWithTag)(xfrm, "a:chOff")[0];
6916
- const chExt = (0, ooxml_js.childrenWithTag)(xfrm, "a:chExt")[0];
6917
- if (chOff === void 0 || chExt === void 0) return;
6918
- const cx = (0, ooxml_js.attr)(chOff, "x");
6919
- const cy = (0, ooxml_js.attr)(chOff, "y");
6920
- const ccx = (0, ooxml_js.attr)(chExt, "cx");
6921
- const ccy = (0, ooxml_js.attr)(chExt, "cy");
6922
- if (cx === void 0 || cy === void 0 || ccx === void 0 || ccy === void 0) return;
6923
- return {
6924
- offXPt: base.xPt,
6925
- offYPt: base.yPt,
6926
- extWidthPt: base.widthPt,
6927
- extHeightPt: base.heightPt,
6928
- childOffXPt: emuToPt(Number(cx)),
6929
- childOffYPt: emuToPt(Number(cy)),
6930
- childExtWidthPt: emuToPt(Number(ccx)),
6931
- childExtHeightPt: emuToPt(Number(ccy))
6932
- };
6933
- }
6934
- function applyGroupTransform(group, childFrame) {
6935
- const scaleX = group.childExtWidthPt === 0 ? 1 : group.extWidthPt / group.childExtWidthPt;
6936
- const scaleY = group.childExtHeightPt === 0 ? 1 : group.extHeightPt / group.childExtHeightPt;
6937
- return {
6938
- xPt: group.offXPt + (childFrame.xPt - group.childOffXPt) * scaleX,
6939
- yPt: group.offYPt + (childFrame.yPt - group.childOffYPt) * scaleY,
6940
- widthPt: childFrame.widthPt * scaleX,
6941
- heightPt: childFrame.heightPt * scaleY
6942
- };
6943
- }
6944
- //#endregion
6945
- //#region src/ooxml/docx/styles.ts
6946
- function mergeParagraphLayer(base, layer) {
6947
- return {
6948
- alignment: layer.alignment ?? base.alignment,
6949
- spacingBeforePt: layer.spacingBeforePt ?? base.spacingBeforePt,
6950
- spacingAfterPt: layer.spacingAfterPt ?? base.spacingAfterPt,
6951
- lineSpacing: layer.lineSpacing ?? base.lineSpacing,
6952
- indentLeftPt: layer.indentLeftPt ?? base.indentLeftPt,
6953
- indentFirstLinePt: layer.indentFirstLinePt ?? base.indentFirstLinePt
6954
- };
6955
- }
6956
- function mergeRunLayer(base, layer) {
6957
- return {
6958
- bold: layer.bold ?? base.bold,
6959
- italic: layer.italic ?? base.italic,
6960
- underline: layer.underline ?? base.underline,
6961
- strike: layer.strike ?? base.strike,
6962
- fontFamily: layer.fontFamily ?? base.fontFamily,
6963
- sizePt: layer.sizePt ?? base.sizePt,
6964
- color: layer.color ?? base.color
6965
- };
6966
- }
6967
- function readToggle$1(el) {
6968
- if (el === void 0) return;
6969
- const val = (0, ooxml_js.attr)(el, "w:val");
6970
- return val === void 0 || val !== "0" && val !== "false" && val !== "off";
6971
- }
6972
- function readUnderline(u) {
6973
- if (u === void 0) return;
6974
- const val = (0, ooxml_js.attr)(u, "w:val");
6975
- return val !== void 0 && val !== "none";
6976
- }
6977
- function readRunColor(colorEl) {
6978
- if (colorEl === void 0) return;
6979
- const val = (0, ooxml_js.attr)(colorEl, "w:val");
6980
- return val === void 0 || val === "auto" ? void 0 : rgbHexToColor(val);
6981
- }
6982
- function readRunFontFamily(rFonts, theme) {
6983
- if (rFonts === void 0) return;
6984
- const ascii = (0, ooxml_js.attr)(rFonts, "w:ascii");
6985
- if (ascii !== void 0) return ascii;
6986
- const asciiTheme = (0, ooxml_js.attr)(rFonts, "w:asciiTheme");
6987
- if (asciiTheme === "majorHAnsi" || asciiTheme === "majorAscii") return theme.majorFont;
6988
- if (asciiTheme === "minorHAnsi" || asciiTheme === "minorAscii") return theme.minorFont;
6989
- }
6990
- function readRunPropertiesLayer(rPr, theme) {
6991
- if (rPr === void 0) return {};
6992
- const sz = (0, ooxml_js.childrenWithTag)(rPr, "w:sz")[0];
6993
- const szVal = sz === void 0 ? void 0 : (0, ooxml_js.attr)(sz, "w:val");
6994
- return {
6995
- bold: readToggle$1((0, ooxml_js.childrenWithTag)(rPr, "w:b")[0]),
6996
- italic: readToggle$1((0, ooxml_js.childrenWithTag)(rPr, "w:i")[0]),
6997
- underline: readUnderline((0, ooxml_js.childrenWithTag)(rPr, "w:u")[0]),
6998
- strike: readToggle$1((0, ooxml_js.childrenWithTag)(rPr, "w:strike")[0]),
6999
- fontFamily: readRunFontFamily((0, ooxml_js.childrenWithTag)(rPr, "w:rFonts")[0], theme),
7000
- sizePt: szVal === void 0 ? void 0 : halfPointsToPt(Number(szVal)),
7001
- color: readRunColor((0, ooxml_js.childrenWithTag)(rPr, "w:color")[0])
7002
- };
7003
- }
7004
- function readAlignment$1(jc) {
7005
- const val = jc === void 0 ? void 0 : (0, ooxml_js.attr)(jc, "w:val");
7006
- if (val === "left" || val === "start") return "left";
7007
- if (val === "center") return "center";
7008
- if (val === "right" || val === "end") return "right";
7009
- if (val === "both" || val === "distribute") return "justify";
7010
- }
7011
- function readParagraphPropertiesLayer(pPr) {
7012
- if (pPr === void 0) return {};
7013
- const spacing = (0, ooxml_js.childrenWithTag)(pPr, "w:spacing")[0];
7014
- const before = spacing === void 0 ? void 0 : (0, ooxml_js.attr)(spacing, "w:before");
7015
- const after = spacing === void 0 ? void 0 : (0, ooxml_js.attr)(spacing, "w:after");
7016
- const line = spacing === void 0 ? void 0 : (0, ooxml_js.attr)(spacing, "w:line");
7017
- const lineRule = spacing === void 0 ? void 0 : (0, ooxml_js.attr)(spacing, "w:lineRule");
7018
- const ind = (0, ooxml_js.childrenWithTag)(pPr, "w:ind")[0];
7019
- const left = ind === void 0 ? void 0 : (0, ooxml_js.attr)(ind, "w:left") ?? (0, ooxml_js.attr)(ind, "w:start");
7020
- const firstLine = ind === void 0 ? void 0 : (0, ooxml_js.attr)(ind, "w:firstLine");
7021
- const hanging = ind === void 0 ? void 0 : (0, ooxml_js.attr)(ind, "w:hanging");
7022
- return {
7023
- alignment: readAlignment$1((0, ooxml_js.childrenWithTag)(pPr, "w:jc")[0]),
7024
- spacingBeforePt: before === void 0 ? void 0 : twipsToPt(Number(before)),
7025
- spacingAfterPt: after === void 0 ? void 0 : twipsToPt(Number(after)),
7026
- lineSpacing: line === void 0 || lineRule === "exact" || lineRule === "atLeast" ? void 0 : lineUnitsToMultiplier(Number(line)),
7027
- indentLeftPt: left === void 0 ? void 0 : twipsToPt(Number(left)),
7028
- indentFirstLinePt: firstLine !== void 0 ? twipsToPt(Number(firstLine)) : hanging !== void 0 ? -twipsToPt(Number(hanging)) : void 0
7029
- };
7030
- }
7031
- function findStyle(stylesRoot, styleId, type) {
7032
- return (0, ooxml_js.elementsWithTag)([stylesRoot], "w:style").find((s) => (0, ooxml_js.attr)(s, "w:type") === type && (0, ooxml_js.attr)(s, "w:styleId") === styleId);
7033
- }
7034
- function findDefaultStyle(stylesRoot, type) {
7035
- return (0, ooxml_js.elementsWithTag)([stylesRoot], "w:style").find((s) => (0, ooxml_js.attr)(s, "w:type") === type && (0, ooxml_js.attr)(s, "w:default") === "1");
7036
- }
7037
- function resolveBasedOnChain(stylesRoot, styleId, type) {
7038
- const chain = [];
7039
- const visited = /* @__PURE__ */ new Set();
7040
- let currentId = styleId;
7041
- while (currentId !== void 0 && !visited.has(currentId)) {
7042
- visited.add(currentId);
7043
- const style = findStyle(stylesRoot, currentId, type);
7044
- if (style === void 0) break;
7045
- chain.unshift(style);
7046
- const basedOn = (0, ooxml_js.childrenWithTag)(style, "w:basedOn")[0];
7047
- currentId = basedOn === void 0 ? void 0 : (0, ooxml_js.attr)(basedOn, "w:val");
7048
- }
7049
- return chain;
7050
- }
7051
- function docDefaultsElement(stylesRoot, wrapperTag, innerTag) {
7052
- const docDefaults = stylesRoot === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(stylesRoot, "w:docDefaults")[0];
7053
- const wrapper = docDefaults === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(docDefaults, wrapperTag)[0];
7054
- return wrapper === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(wrapper, innerTag)[0];
7055
- }
7056
- function resolveParagraphProperties(paragraph, context) {
7057
- const pPr = (0, ooxml_js.childrenWithTag)(paragraph, "w:pPr")[0];
7058
- const pStyleEl = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:pStyle")[0];
7059
- const styleId = pStyleEl === void 0 ? void 0 : (0, ooxml_js.attr)(pStyleEl, "w:val");
7060
- let resolved = readParagraphPropertiesLayer(docDefaultsElement(context.stylesRoot, "w:pPrDefault", "w:pPr"));
7061
- if (context.stylesRoot !== void 0) {
7062
- const defaultStyle = findDefaultStyle(context.stylesRoot, "paragraph");
7063
- if (defaultStyle !== void 0) resolved = mergeParagraphLayer(resolved, readParagraphPropertiesLayer((0, ooxml_js.childrenWithTag)(defaultStyle, "w:pPr")[0]));
7064
- if (styleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, styleId, "paragraph")) resolved = mergeParagraphLayer(resolved, readParagraphPropertiesLayer((0, ooxml_js.childrenWithTag)(style, "w:pPr")[0]));
7065
- }
7066
- return mergeParagraphLayer(resolved, readParagraphPropertiesLayer(pPr));
7067
- }
7068
- function resolveRunProperties(run, paragraph, context) {
7069
- const pPr = (0, ooxml_js.childrenWithTag)(paragraph, "w:pPr")[0];
7070
- const pStyleEl = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:pStyle")[0];
7071
- const pStyleId = pStyleEl === void 0 ? void 0 : (0, ooxml_js.attr)(pStyleEl, "w:val");
7072
- let resolved = readRunPropertiesLayer(docDefaultsElement(context.stylesRoot, "w:rPrDefault", "w:rPr"), context.theme);
7073
- if (context.stylesRoot !== void 0) {
7074
- const defaultStyle = findDefaultStyle(context.stylesRoot, "paragraph");
7075
- if (defaultStyle !== void 0) resolved = mergeRunLayer(resolved, readRunPropertiesLayer((0, ooxml_js.childrenWithTag)(defaultStyle, "w:rPr")[0], context.theme));
7076
- if (pStyleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, pStyleId, "paragraph")) resolved = mergeRunLayer(resolved, readRunPropertiesLayer((0, ooxml_js.childrenWithTag)(style, "w:rPr")[0], context.theme));
7077
- }
7078
- const paragraphMarkRPr = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:rPr")[0];
7079
- resolved = mergeRunLayer(resolved, readRunPropertiesLayer(paragraphMarkRPr, context.theme));
7080
- const rPr = (0, ooxml_js.childrenWithTag)(run, "w:rPr")[0];
7081
- const rStyleEl = rPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(rPr, "w:rStyle")[0];
7082
- const rStyleId = rStyleEl === void 0 ? void 0 : (0, ooxml_js.attr)(rStyleEl, "w:val");
7083
- if (context.stylesRoot !== void 0 && rStyleId !== void 0) for (const style of resolveBasedOnChain(context.stylesRoot, rStyleId, "character")) resolved = mergeRunLayer(resolved, readRunPropertiesLayer((0, ooxml_js.childrenWithTag)(style, "w:rPr")[0], context.theme));
7084
- return mergeRunLayer(resolved, readRunPropertiesLayer(rPr, context.theme));
7085
- }
6617
+ //#region src/pdf/codec.ts
6618
+ const pdfCodec = zod.z.codec(PdfBytesSchema, LayoutDocumentSchema, {
6619
+ decode: (bytes) => readPdf(bytes),
6620
+ encode: (doc) => writePdf(doc)
6621
+ });
7086
6622
  //#endregion
7087
6623
  //#region src/ooxml/docx/read.ts
7088
- const DOCUMENT_PART_PATH = "word/document.xml";
7089
- const STYLES_PART_PATH = "word/styles.xml";
7090
- const THEME_REL_SUFFIX$1 = "/theme";
7091
- const DEFAULT_MARGIN_PT = 72;
7092
- const DEFAULT_MARGINS = {
7093
- topPt: DEFAULT_MARGIN_PT,
7094
- rightPt: DEFAULT_MARGIN_PT,
7095
- bottomPt: DEFAULT_MARGIN_PT,
7096
- leftPt: DEFAULT_MARGIN_PT
7097
- };
7098
- function readPageSize(sectPr) {
7099
- const pgSz = (0, ooxml_js.childrenWithTag)(sectPr, "w:pgSz")[0];
7100
- const w = pgSz === void 0 ? void 0 : (0, ooxml_js.attr)(pgSz, "w:w");
7101
- const h = pgSz === void 0 ? void 0 : (0, ooxml_js.attr)(pgSz, "w:h");
7102
- return w === void 0 || h === void 0 ? PAGE_SIZE_LETTER : {
7103
- widthPt: twipsToPt(Number(w)),
7104
- heightPt: twipsToPt(Number(h))
7105
- };
7106
- }
7107
- function readMargins(sectPr) {
7108
- const pgMar = (0, ooxml_js.childrenWithTag)(sectPr, "w:pgMar")[0];
7109
- if (pgMar === void 0) return DEFAULT_MARGINS;
7110
- const top = (0, ooxml_js.attr)(pgMar, "w:top");
7111
- const right = (0, ooxml_js.attr)(pgMar, "w:right");
7112
- const bottom = (0, ooxml_js.attr)(pgMar, "w:bottom");
7113
- const left = (0, ooxml_js.attr)(pgMar, "w:left");
7114
- return {
7115
- topPt: top === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(top)),
7116
- rightPt: right === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(right)),
7117
- bottomPt: bottom === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(bottom)),
7118
- leftPt: left === void 0 ? DEFAULT_MARGIN_PT : twipsToPt(Number(left))
7119
- };
7120
- }
7121
- function readListMembership(pPr) {
7122
- const numPr = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:numPr")[0];
7123
- if (numPr === void 0) return;
7124
- const numIdEl = (0, ooxml_js.childrenWithTag)(numPr, "w:numId")[0];
7125
- const numId = numIdEl === void 0 ? void 0 : (0, ooxml_js.attr)(numIdEl, "w:val");
7126
- if (numId === void 0) return;
7127
- const ilvlEl = (0, ooxml_js.childrenWithTag)(numPr, "w:ilvl")[0];
7128
- const ilvlVal = ilvlEl === void 0 ? void 0 : (0, ooxml_js.attr)(ilvlEl, "w:val");
7129
- return {
7130
- numId,
7131
- level: ilvlVal === void 0 ? 0 : Number(ilvlVal)
7132
- };
7133
- }
7134
- function readToggle(el) {
7135
- if (el === void 0) return false;
7136
- const val = (0, ooxml_js.attr)(el, "w:val");
7137
- return val === void 0 || val !== "0" && val !== "false" && val !== "off";
7138
- }
7139
- function hasPageBreakBefore(paragraph) {
7140
- const pPr = (0, ooxml_js.childrenWithTag)(paragraph, "w:pPr")[0];
7141
- return readToggle(pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:pageBreakBefore")[0]);
7142
- }
7143
- function readRunText(run) {
7144
- let text = "";
7145
- for (const child of run.children) {
7146
- if (child.type !== "element") continue;
7147
- if (child.tag === "w:t") text += (0, ooxml_js.textContent)(child);
7148
- else if (child.tag === "w:tab") text += " ";
7149
- else if (child.tag === "w:br" || child.tag === "w:cr") text += "\n";
7150
- }
7151
- return text;
7152
- }
7153
- function readRun$1(run, paragraph, context) {
7154
- const props = resolveRunProperties(run, paragraph, context);
7155
- return {
7156
- text: readRunText(run),
7157
- bold: props.bold,
7158
- italic: props.italic,
7159
- underline: props.underline,
7160
- strike: props.strike,
7161
- fontFamily: props.fontFamily,
7162
- sizePt: props.sizePt,
7163
- color: props.color
7164
- };
7165
- }
7166
- function readParagraphRuns(paragraph, context, rels) {
7167
- const runs = [];
7168
- let fieldState = "none";
7169
- function walk(nodes, hyperlinkTarget) {
7170
- for (const node of nodes) {
7171
- if (node.type !== "element") continue;
7172
- if (node.tag === "w:r") {
7173
- const fldChar = (0, ooxml_js.childrenWithTag)(node, "w:fldChar")[0];
7174
- if (fldChar !== void 0) {
7175
- const type = (0, ooxml_js.attr)(fldChar, "w:fldCharType");
7176
- if (type === "begin") fieldState = "code";
7177
- else if (type === "separate") fieldState = "result";
7178
- else if (type === "end") fieldState = "none";
7179
- continue;
7180
- }
7181
- if (fieldState === "code") continue;
7182
- const run = readRun$1(node, paragraph, context);
7183
- runs.push(hyperlinkTarget === void 0 ? run : {
7184
- ...run,
7185
- hyperlink: hyperlinkTarget
7186
- });
7187
- } else if (node.tag === "w:fldSimple") walk(node.children, hyperlinkTarget);
7188
- else if (node.tag === "w:hyperlink") {
7189
- const rId = (0, ooxml_js.attr)(node, "r:id");
7190
- const target = rId === void 0 ? void 0 : rels.get(rId)?.target;
7191
- walk(node.children, target ?? hyperlinkTarget);
7192
- } else if (node.tag === "w:ins") walk(node.children, hyperlinkTarget);
7193
- }
7194
- }
7195
- walk(paragraph.children, void 0);
7196
- return runs;
7197
- }
7198
- function readParagraph$1(paragraph, context, rels) {
7199
- const pPr = (0, ooxml_js.childrenWithTag)(paragraph, "w:pPr")[0];
7200
- const pStyleEl = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:pStyle")[0];
7201
- const props = resolveParagraphProperties(paragraph, context);
7202
- return {
7203
- kind: "paragraph",
7204
- runs: readParagraphRuns(paragraph, context, rels),
7205
- styleId: pStyleEl === void 0 ? void 0 : (0, ooxml_js.attr)(pStyleEl, "w:val"),
7206
- alignment: props.alignment,
7207
- list: readListMembership(pPr),
7208
- spacingBeforePt: props.spacingBeforePt,
7209
- spacingAfterPt: props.spacingAfterPt,
7210
- lineSpacing: props.lineSpacing,
7211
- indentLeftPt: props.indentLeftPt,
7212
- indentFirstLinePt: props.indentFirstLinePt
7213
- };
7214
- }
7215
- function readCellShading(tcPr) {
7216
- const shd = tcPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(tcPr, "w:shd")[0];
7217
- const fill = shd === void 0 ? void 0 : (0, ooxml_js.attr)(shd, "w:fill");
7218
- return fill === void 0 || fill === "auto" || fill === "none" ? void 0 : rgbHexToColor(fill);
7219
- }
7220
- function readRawCell(tc, context, rels) {
7221
- const tcPr = (0, ooxml_js.childrenWithTag)(tc, "w:tcPr")[0];
7222
- const gridSpanEl = tcPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(tcPr, "w:gridSpan")[0];
7223
- const gridSpanVal = gridSpanEl === void 0 ? void 0 : (0, ooxml_js.attr)(gridSpanEl, "w:val");
7224
- const vMerge = tcPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(tcPr, "w:vMerge")[0];
7225
- const vMergeVal = vMerge === void 0 ? void 0 : (0, ooxml_js.attr)(vMerge, "w:val") ?? "continue";
7226
- return {
7227
- gridSpan: gridSpanVal === void 0 ? 1 : Number(gridSpanVal),
7228
- isVMergeContinuation: vMergeVal === "continue",
7229
- background: readCellShading(tcPr),
7230
- blocks: readBodyBlocks(tc.children, context, rels)
7231
- };
7232
- }
7233
- function readTable$1(tbl, context, rels) {
7234
- const tblGrid = (0, ooxml_js.childrenWithTag)(tbl, "w:tblGrid")[0];
7235
- const columnWidthsPt = tblGrid === void 0 ? [] : (0, ooxml_js.childrenWithTag)(tblGrid, "w:gridCol").map((col) => twipsToPt(Number((0, ooxml_js.attr)(col, "w:w") ?? "0")));
7236
- const rawRows = (0, ooxml_js.childrenWithTag)(tbl, "w:tr").map((tr) => (0, ooxml_js.childrenWithTag)(tr, "w:tc").map((tc) => readRawCell(tc, context, rels)));
7237
- const rowColumnIndices = rawRows.map((row) => {
7238
- const indices = [];
7239
- let col = 0;
7240
- for (const cell of row) {
7241
- indices.push(col);
7242
- col += cell.gridSpan;
7243
- }
7244
- return indices;
7245
- });
7246
- return {
7247
- kind: "table",
7248
- columnWidthsPt,
7249
- rows: rawRows.map((row, rowIndex) => ({ cells: row.map((cell, cellIndex) => {
7250
- if (cell.isVMergeContinuation) return { blocks: [] };
7251
- const colIndex = rowColumnIndices[rowIndex][cellIndex];
7252
- let rowSpan = 1;
7253
- for (let r = rowIndex + 1; r < rawRows.length; r++) {
7254
- const matchIndex = rowColumnIndices[r].indexOf(colIndex);
7255
- if (!(matchIndex === -1 ? void 0 : rawRows[r][matchIndex])?.isVMergeContinuation) break;
7256
- rowSpan++;
7257
- }
7258
- return {
7259
- blocks: cell.blocks,
7260
- colSpan: cell.gridSpan > 1 ? cell.gridSpan : void 0,
7261
- rowSpan: rowSpan > 1 ? rowSpan : void 0,
7262
- background: cell.background
7263
- };
7264
- }) }))
7265
- };
7266
- }
7267
- function readBodyBlocks(nodes, context, rels) {
7268
- const blocks = [];
7269
- for (const node of nodes) {
7270
- if (node.type !== "element") continue;
7271
- if (node.tag === "w:p") {
7272
- if (hasPageBreakBefore(node)) blocks.push({ kind: "pageBreak" });
7273
- blocks.push(readParagraph$1(node, context, rels));
7274
- } else if (node.tag === "w:tbl") blocks.push(readTable$1(node, context, rels));
7275
- else if (node.tag === "w:sdt") {
7276
- const sdtContent = (0, ooxml_js.childrenWithTag)(node, "w:sdtContent")[0];
7277
- if (sdtContent !== void 0) blocks.push(...readBodyBlocks(sdtContent.children, context, rels));
7278
- } else if (node.tag === "w:ins") blocks.push(...readBodyBlocks(node.children, context, rels));
7279
- else if (node.tag === "mc:AlternateContent") {
7280
- const target = (0, ooxml_js.childrenWithTag)(node, "mc:Fallback")[0] ?? (0, ooxml_js.childrenWithTag)(node, "mc:Choice")[0];
7281
- if (target !== void 0) blocks.push(...readBodyBlocks(target.children, context, rels));
7282
- }
7283
- }
7284
- return blocks;
7285
- }
7286
- function readSections(body, context, rels) {
7287
- const sections = [];
7288
- let currentBlocks = [];
7289
- for (const node of body.children) {
7290
- if (node.type !== "element") continue;
7291
- if (node.tag === "w:sectPr") {
7292
- sections.push({
7293
- pageSize: readPageSize(node),
7294
- margins: readMargins(node),
7295
- blocks: currentBlocks
7296
- });
7297
- currentBlocks = [];
7298
- continue;
7299
- }
7300
- if (node.tag === "w:p") {
7301
- const pPr = (0, ooxml_js.childrenWithTag)(node, "w:pPr")[0];
7302
- const sectPr = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "w:sectPr")[0];
7303
- if (hasPageBreakBefore(node)) currentBlocks.push({ kind: "pageBreak" });
7304
- currentBlocks.push(readParagraph$1(node, context, rels));
7305
- if (sectPr !== void 0) {
7306
- sections.push({
7307
- pageSize: readPageSize(sectPr),
7308
- margins: readMargins(sectPr),
7309
- blocks: currentBlocks
7310
- });
7311
- currentBlocks = [];
7312
- }
7313
- continue;
7314
- }
7315
- currentBlocks.push(...readBodyBlocks([node], context, rels));
7316
- }
7317
- if (currentBlocks.length > 0 || sections.length === 0) sections.push({
7318
- pageSize: PAGE_SIZE_LETTER,
7319
- margins: DEFAULT_MARGINS,
7320
- blocks: currentBlocks
7321
- });
7322
- return sections;
7323
- }
7324
- function readDocumentTheme(pkg, docRels) {
7325
- for (const rel of docRels.values()) if (rel.type.endsWith(THEME_REL_SUFFIX$1)) {
7326
- const themeRoot = (0, ooxml_js.rootElement)(pkg.parts[rel.target]);
7327
- if (themeRoot !== void 0) return readTheme(themeRoot);
7328
- }
7329
- return EMPTY_THEME;
7330
- }
7331
6624
  function readDocxContent(pkg) {
7332
- const documentRoot = (0, ooxml_js.rootElement)(pkg.parts[DOCUMENT_PART_PATH]);
7333
- if (documentRoot === void 0) throw new Error(`package has no ${DOCUMENT_PART_PATH} part`);
7334
- const body = (0, ooxml_js.childrenWithTag)(documentRoot, "w:body")[0];
7335
- if (body === void 0) throw new Error(`${DOCUMENT_PART_PATH} has no w:body element`);
7336
- const docRels = (0, ooxml_js.resolveRelationships)(pkg, DOCUMENT_PART_PATH);
7337
- const context = {
7338
- stylesRoot: (0, ooxml_js.rootElement)(pkg.parts[STYLES_PART_PATH]),
7339
- theme: readDocumentTheme(pkg, docRels)
7340
- };
6625
+ const docxDoc = (0, ooxml_js.readDocx)(pkg);
7341
6626
  return {
7342
6627
  kind: "wordprocessing",
7343
6628
  formatVersion: 1,
7344
- metadata: readCoreProperties(pkg),
7345
- sections: readSections(body, context, docRels)
6629
+ metadata: { ...docxDoc.metadata },
6630
+ sections: docxDoc.sections
7346
6631
  };
7347
6632
  }
7348
6633
  //#endregion
7349
- //#region src/image/sniff.ts
7350
- const PNG_SIGNATURE = [
7351
- 137,
7352
- 80,
7353
- 78,
7354
- 71,
7355
- 13,
7356
- 10,
7357
- 26,
7358
- 10
7359
- ];
7360
- const JPEG_SIGNATURE = [
7361
- 255,
7362
- 216,
7363
- 255
7364
- ];
7365
- function startsWith(bytes, signature) {
7366
- if (bytes.length < signature.length) return false;
7367
- for (let i = 0; i < signature.length; i++) if (bytes[i] !== signature[i]) return false;
7368
- return true;
7369
- }
7370
- function sniffImageFormat(bytes) {
7371
- if (startsWith(bytes, PNG_SIGNATURE)) return "png";
7372
- if (startsWith(bytes, JPEG_SIGNATURE)) return "jpeg";
7373
- }
7374
- //#endregion
7375
- //#region src/ooxml/pptx/inherit.ts
7376
- const SLIDE_LAYOUT_REL_SUFFIX = "/slideLayout";
7377
- const SLIDE_MASTER_REL_SUFFIX = "/slideMaster";
7378
- const THEME_REL_SUFFIX = "/theme";
7379
- function findRelTarget(pkg, partPath, typeSuffix) {
7380
- for (const rel of (0, ooxml_js.resolveRelationships)(pkg, partPath).values()) if (rel.type.endsWith(typeSuffix)) return rel.target;
7381
- }
7382
- function resolveSlideInheritance(pkg, slidePath) {
7383
- const layoutPath = findRelTarget(pkg, slidePath, SLIDE_LAYOUT_REL_SUFFIX);
7384
- const layoutRoot = layoutPath === void 0 ? void 0 : (0, ooxml_js.rootElement)(pkg.parts[layoutPath]);
7385
- const masterPath = layoutPath === void 0 ? void 0 : findRelTarget(pkg, layoutPath, SLIDE_MASTER_REL_SUFFIX);
7386
- const masterRoot = masterPath === void 0 ? void 0 : (0, ooxml_js.rootElement)(pkg.parts[masterPath]);
7387
- const themePath = masterPath === void 0 ? void 0 : findRelTarget(pkg, masterPath, THEME_REL_SUFFIX);
7388
- const themeRoot = themePath === void 0 ? void 0 : (0, ooxml_js.rootElement)(pkg.parts[themePath]);
7389
- return {
7390
- layoutRoot,
7391
- masterRoot,
7392
- theme: themeRoot === void 0 ? EMPTY_THEME : readTheme(themeRoot),
7393
- colorMap: readColorMap(masterRoot === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(masterRoot, "p:clrMap")[0])
7394
- };
7395
- }
7396
- const TYPE_NORMALIZATION = /* @__PURE__ */ new Map([["ctrTitle", "title"], ["subTitle", "body"]]);
7397
- function normalizePlaceholderType(type) {
7398
- return type === void 0 ? void 0 : TYPE_NORMALIZATION.get(type) ?? type;
7399
- }
7400
- function readPlaceholderKey(shape) {
7401
- const ph = (0, ooxml_js.elementsWithTag)([shape], "p:ph")[0];
7402
- return ph === void 0 ? void 0 : {
7403
- type: (0, ooxml_js.attr)(ph, "type"),
7404
- idx: (0, ooxml_js.attr)(ph, "idx")
7405
- };
7406
- }
7407
- function shapesOf(root) {
7408
- return root === void 0 ? [] : (0, ooxml_js.elementsWithTag)([root], "p:sp");
7409
- }
7410
- function findMatchingPlaceholder(root, key) {
7411
- const shapes = shapesOf(root);
7412
- if (key.idx !== void 0) {
7413
- const byIdx = shapes.find((shape) => readPlaceholderKey(shape)?.idx === key.idx);
7414
- if (byIdx !== void 0) return byIdx;
7415
- }
7416
- const normalizedTarget = normalizePlaceholderType(key.type);
7417
- if (normalizedTarget === void 0) return;
7418
- return shapes.find((shape) => normalizePlaceholderType(readPlaceholderKey(shape)?.type) === normalizedTarget);
7419
- }
7420
- function shapeXfrm(shape) {
7421
- if (shape === void 0) return;
7422
- const spPr = (0, ooxml_js.childrenWithTag)(shape, "p:spPr")[0];
7423
- return spPr === void 0 ? void 0 : readXfrm((0, ooxml_js.childrenWithTag)(spPr, "a:xfrm")[0]);
7424
- }
7425
- function resolvePlaceholderXfrm(key, context) {
7426
- return shapeXfrm(findMatchingPlaceholder(context.layoutRoot, key)) ?? shapeXfrm(findMatchingPlaceholder(context.masterRoot, key));
7427
- }
7428
- function readRunPropertiesFromElement(rPr, context) {
7429
- const sz = (0, ooxml_js.attr)(rPr, "sz");
7430
- const latin = (0, ooxml_js.childrenWithTag)(rPr, "a:latin")[0];
7431
- const typeface = latin === void 0 ? void 0 : (0, ooxml_js.attr)(latin, "typeface");
7432
- const solidFill = (0, ooxml_js.childrenWithTag)(rPr, "a:solidFill")[0];
7433
- const bold = (0, ooxml_js.attr)(rPr, "b");
7434
- const italic = (0, ooxml_js.attr)(rPr, "i");
7435
- return {
7436
- fontFamily: typeface === void 0 ? void 0 : resolveThemeFontReference(typeface, context.theme),
7437
- sizePt: sz === void 0 ? void 0 : drawingMlFontSizeToPt(Number(sz)),
7438
- bold: bold === void 0 ? void 0 : bold === "1",
7439
- italic: italic === void 0 ? void 0 : italic === "1",
7440
- color: readSolidFillColor(solidFill, context.colorMap, context.theme)
7441
- };
7442
- }
7443
- function txStyleTagFor(placeholderType) {
7444
- const normalized = normalizePlaceholderType(placeholderType);
7445
- if (normalized === "title") return "p:titleStyle";
7446
- if (normalized === "body") return "p:bodyStyle";
7447
- return "p:otherStyle";
7448
- }
7449
- function levelTag(level) {
7450
- return `a:lvl${Math.min(Math.max(level, 0), 8) + 1}pPr`;
7451
- }
7452
- function resolveDefaultRunProperties(placeholderType, level, context) {
7453
- if (context.masterRoot === void 0) return {};
7454
- const txStyles = (0, ooxml_js.childrenWithTag)(context.masterRoot, "p:txStyles")[0];
7455
- const styleEl = txStyles === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(txStyles, txStyleTagFor(placeholderType))[0];
7456
- const lvlPPr = styleEl === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(styleEl, levelTag(level))[0];
7457
- const defRPr = lvlPPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(lvlPPr, "a:defRPr")[0];
7458
- return defRPr === void 0 ? {} : readRunPropertiesFromElement(defRPr, context);
7459
- }
7460
- //#endregion
7461
6634
  //#region src/ooxml/pptx/read.ts
7462
- const PRESENTATION_PATH = "ppt/presentation.xml";
7463
- const TABLE_GRAPHIC_URI = "http://schemas.openxmlformats.org/drawingml/2006/table";
7464
- function readSlideSize(presentationRoot) {
7465
- const sldSz = presentationRoot === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(presentationRoot, "p:sldSz")[0];
7466
- const cx = sldSz === void 0 ? void 0 : (0, ooxml_js.attr)(sldSz, "cx");
7467
- const cy = sldSz === void 0 ? void 0 : (0, ooxml_js.attr)(sldSz, "cy");
7468
- return cx === void 0 || cy === void 0 ? SLIDE_SIZE_WIDESCREEN : {
7469
- widthPt: emuToPt(Number(cx)),
7470
- heightPt: emuToPt(Number(cy))
7471
- };
7472
- }
7473
- function readSlidePathsInOrder(pkg, presentationRoot) {
7474
- if (presentationRoot === void 0) return [];
7475
- const sldIdLst = (0, ooxml_js.childrenWithTag)(presentationRoot, "p:sldIdLst")[0];
7476
- if (sldIdLst === void 0) return [];
7477
- const presentationRels = (0, ooxml_js.resolveRelationships)(pkg, PRESENTATION_PATH);
7478
- const paths = [];
7479
- for (const sldId of (0, ooxml_js.childrenWithTag)(sldIdLst, "p:sldId")) {
7480
- const rId = (0, ooxml_js.attr)(sldId, "r:id");
7481
- const rel = rId === void 0 ? void 0 : presentationRels.get(rId);
7482
- if (rel !== void 0) paths.push(rel.target);
7483
- }
7484
- return paths;
7485
- }
7486
- function shapeName(shape) {
7487
- const cNvPr = (0, ooxml_js.elementsWithTag)([shape], "p:cNvPr")[0];
7488
- return cNvPr === void 0 ? void 0 : (0, ooxml_js.attr)(cNvPr, "name");
7489
- }
7490
- function mergeRunProperties(base, override) {
7491
- return {
7492
- fontFamily: override.fontFamily ?? base.fontFamily,
7493
- sizePt: override.sizePt ?? base.sizePt,
7494
- bold: override.bold ?? base.bold,
7495
- italic: override.italic ?? base.italic,
7496
- color: override.color ?? base.color
7497
- };
7498
- }
7499
- function isUnderlined(rPr) {
7500
- if (rPr === void 0) return;
7501
- const u = (0, ooxml_js.attr)(rPr, "u");
7502
- return u === void 0 ? void 0 : u !== "none";
7503
- }
7504
- function isStrikethrough(rPr) {
7505
- if (rPr === void 0) return;
7506
- const strike = (0, ooxml_js.attr)(rPr, "strike");
7507
- return strike === void 0 ? void 0 : strike !== "noStrike";
7508
- }
7509
- function readHyperlink(rPr, slideRels) {
7510
- if (rPr === void 0) return;
7511
- const hlink = (0, ooxml_js.childrenWithTag)(rPr, "a:hlinkClick")[0];
7512
- const rId = hlink === void 0 ? void 0 : (0, ooxml_js.attr)(hlink, "r:id");
7513
- const rel = rId === void 0 ? void 0 : slideRels.get(rId);
7514
- return rel?.targetMode === "External" ? rel.target : void 0;
7515
- }
7516
- function readRun(runEl, cascadeBase, context, slideRels) {
7517
- const rPr = (0, ooxml_js.childrenWithTag)(runEl, "a:rPr")[0];
7518
- const merged = mergeRunProperties(cascadeBase, rPr === void 0 ? {} : readRunPropertiesFromElement(rPr, context));
7519
- const tEl = (0, ooxml_js.childrenWithTag)(runEl, "a:t")[0];
7520
- return {
7521
- text: tEl === void 0 ? "" : (0, ooxml_js.textContent)(tEl),
7522
- bold: merged.bold,
7523
- italic: merged.italic,
7524
- underline: isUnderlined(rPr),
7525
- strike: isStrikethrough(rPr),
7526
- fontFamily: merged.fontFamily,
7527
- sizePt: merged.sizePt,
7528
- color: merged.color,
7529
- hyperlink: readHyperlink(rPr, slideRels)
7530
- };
7531
- }
7532
- function readAlignment(algn) {
7533
- if (algn === "l") return "left";
7534
- if (algn === "ctr") return "center";
7535
- if (algn === "r") return "right";
7536
- if (algn === "just" || algn === "justLow") return "justify";
7537
- }
7538
- function readAbsoluteSpacingPt(spc) {
7539
- if (spc === void 0) return;
7540
- const pts = (0, ooxml_js.childrenWithTag)(spc, "a:spcPts")[0];
7541
- const val = pts === void 0 ? void 0 : (0, ooxml_js.attr)(pts, "val");
7542
- return val === void 0 ? void 0 : drawingMlFontSizeToPt(Number(val));
7543
- }
7544
- function readLineSpacingMultiplier(pPr) {
7545
- const lnSpc = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "a:lnSpc")[0];
7546
- const pct = lnSpc === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(lnSpc, "a:spcPct")[0];
7547
- const val = pct === void 0 ? void 0 : (0, ooxml_js.attr)(pct, "val");
7548
- return val === void 0 ? void 0 : Number(val) / 1e5;
7549
- }
7550
- function readParagraph(pEl, placeholderType, context, slideRels) {
7551
- const pPr = (0, ooxml_js.childrenWithTag)(pEl, "a:pPr")[0];
7552
- const masterDefaults = resolveDefaultRunProperties(placeholderType, pPr === void 0 ? 0 : Number((0, ooxml_js.attr)(pPr, "lvl") ?? "0"), context);
7553
- const pPrDefRPr = pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "a:defRPr")[0];
7554
- const paragraphDefaults = pPrDefRPr === void 0 ? masterDefaults : mergeRunProperties(masterDefaults, readRunPropertiesFromElement(pPrDefRPr, context));
7555
- const runs = [];
7556
- for (const child of pEl.children) {
7557
- if (child.type !== "element") continue;
7558
- if (child.tag === "a:r" || child.tag === "a:fld") runs.push(readRun(child, paragraphDefaults, context, slideRels));
7559
- else if (child.tag === "a:br") runs.push({ text: "\n" });
7560
- }
7561
- const marL = pPr === void 0 ? void 0 : (0, ooxml_js.attr)(pPr, "marL");
7562
- const indent = pPr === void 0 ? void 0 : (0, ooxml_js.attr)(pPr, "indent");
7563
- return {
7564
- kind: "paragraph",
7565
- runs,
7566
- alignment: pPr === void 0 ? void 0 : readAlignment((0, ooxml_js.attr)(pPr, "algn")),
7567
- spacingBeforePt: readAbsoluteSpacingPt(pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "a:spcBef")[0]),
7568
- spacingAfterPt: readAbsoluteSpacingPt(pPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(pPr, "a:spcAft")[0]),
7569
- lineSpacing: readLineSpacingMultiplier(pPr),
7570
- indentLeftPt: marL === void 0 ? void 0 : emuToPt(Number(marL)),
7571
- indentFirstLinePt: indent === void 0 ? void 0 : emuToPt(Number(indent))
7572
- };
7573
- }
7574
- function textBodyParagraphs(txBody, placeholderType, context, slideRels) {
7575
- return txBody === void 0 ? [] : (0, ooxml_js.childrenWithTag)(txBody, "a:p").map((p) => readParagraph(p, placeholderType, context, slideRels));
7576
- }
7577
- const DEFAULT_INSET_LEFT_RIGHT_EMU = 91440;
7578
- const DEFAULT_INSET_TOP_BOTTOM_EMU = 45720;
7579
- const NO_TEXT_BODY_EXTRAS = {
7580
- insetLeftPt: 0,
7581
- insetTopPt: 0,
7582
- insetRightPt: 0,
7583
- insetBottomPt: 0,
7584
- fontScale: void 0,
7585
- lineSpacingReduction: void 0
7586
- };
7587
- function readShapeTextExtras(txBody) {
7588
- if (txBody === void 0) return NO_TEXT_BODY_EXTRAS;
7589
- const bodyPr = (0, ooxml_js.childrenWithTag)(txBody, "a:bodyPr")[0];
7590
- const lIns = bodyPr === void 0 ? void 0 : (0, ooxml_js.attr)(bodyPr, "lIns");
7591
- const tIns = bodyPr === void 0 ? void 0 : (0, ooxml_js.attr)(bodyPr, "tIns");
7592
- const rIns = bodyPr === void 0 ? void 0 : (0, ooxml_js.attr)(bodyPr, "rIns");
7593
- const bIns = bodyPr === void 0 ? void 0 : (0, ooxml_js.attr)(bodyPr, "bIns");
7594
- const normAutofit = bodyPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(bodyPr, "a:normAutofit")[0];
7595
- const fontScale = normAutofit === void 0 ? void 0 : (0, ooxml_js.attr)(normAutofit, "fontScale");
7596
- const lnSpcReduction = normAutofit === void 0 ? void 0 : (0, ooxml_js.attr)(normAutofit, "lnSpcReduction");
7597
- return {
7598
- insetLeftPt: emuToPt(lIns === void 0 ? DEFAULT_INSET_LEFT_RIGHT_EMU : Number(lIns)),
7599
- insetTopPt: emuToPt(tIns === void 0 ? DEFAULT_INSET_TOP_BOTTOM_EMU : Number(tIns)),
7600
- insetRightPt: emuToPt(rIns === void 0 ? DEFAULT_INSET_LEFT_RIGHT_EMU : Number(rIns)),
7601
- insetBottomPt: emuToPt(bIns === void 0 ? DEFAULT_INSET_TOP_BOTTOM_EMU : Number(bIns)),
7602
- fontScale: fontScale === void 0 ? void 0 : Number(fontScale) / 1e5,
7603
- lineSpacingReduction: lnSpcReduction === void 0 ? void 0 : Number(lnSpcReduction) / 1e5
7604
- };
7605
- }
7606
- function resolveShapeFrame(shape, context, parentTransform) {
7607
- const key = readPlaceholderKey(shape);
7608
- const spPr = (0, ooxml_js.childrenWithTag)(shape, "p:spPr")[0];
7609
- const xfrm = (spPr === void 0 ? void 0 : readXfrm((0, ooxml_js.childrenWithTag)(spPr, "a:xfrm")[0])) ?? (key === void 0 ? void 0 : resolvePlaceholderXfrm(key, context));
7610
- if (xfrm === void 0) return;
7611
- const localFrame = {
7612
- xPt: xfrm.xPt,
7613
- yPt: xfrm.yPt,
7614
- widthPt: xfrm.widthPt,
7615
- heightPt: xfrm.heightPt
7616
- };
7617
- return {
7618
- frame: parentTransform === void 0 ? localFrame : applyGroupTransform(parentTransform, localFrame),
7619
- rotationDeg: xfrm.rotationDeg === 0 ? void 0 : xfrm.rotationDeg
7620
- };
7621
- }
7622
- function readSpShape(sp, context, slideRels, parentTransform) {
7623
- const resolved = resolveShapeFrame(sp, context, parentTransform);
7624
- if (resolved === void 0) return;
7625
- const key = readPlaceholderKey(sp);
7626
- const txBody = (0, ooxml_js.childrenWithTag)(sp, "p:txBody")[0];
7627
- const blocks = textBodyParagraphs(txBody, key?.type, context, slideRels);
7628
- const extras = readShapeTextExtras(txBody);
7629
- return {
7630
- name: shapeName(sp),
7631
- frame: resolved.frame,
7632
- rotationDeg: resolved.rotationDeg,
7633
- ...extras,
7634
- blocks
7635
- };
7636
- }
7637
- function readPicShape(pic, context, slideRels, pkg, parentTransform) {
7638
- const resolved = resolveShapeFrame(pic, context, parentTransform);
7639
- if (resolved === void 0) return;
7640
- const blipFill = (0, ooxml_js.childrenWithTag)(pic, "p:blipFill")[0];
7641
- const blip = blipFill === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(blipFill, "a:blip")[0];
7642
- const rId = blip === void 0 ? void 0 : (0, ooxml_js.attr)(blip, "r:embed");
7643
- const rel = rId === void 0 ? void 0 : slideRels.get(rId);
7644
- const mediaPart = rel === void 0 ? void 0 : pkg.parts[rel.target];
7645
- const blocks = [];
7646
- if (mediaPart?.kind === "binary") {
7647
- const format = sniffImageFormat((0, ooxml_js.base64ToBytes)(mediaPart.base64));
7648
- if (format !== void 0) {
7649
- const image = {
7650
- kind: "image",
7651
- format,
7652
- base64: mediaPart.base64,
7653
- widthPt: resolved.frame.widthPt,
7654
- heightPt: resolved.frame.heightPt
7655
- };
7656
- blocks.push(image);
7657
- }
7658
- }
7659
- return {
7660
- name: shapeName(pic),
7661
- frame: resolved.frame,
7662
- rotationDeg: resolved.rotationDeg,
7663
- ...NO_TEXT_BODY_EXTRAS,
7664
- blocks
7665
- };
7666
- }
7667
- function readTableCell(tc, context, slideRels) {
7668
- const hMerge = (0, ooxml_js.attr)(tc, "hMerge");
7669
- const vMerge = (0, ooxml_js.attr)(tc, "vMerge");
7670
- if (hMerge === "1" || vMerge === "1") return { blocks: [] };
7671
- const tcPr = (0, ooxml_js.childrenWithTag)(tc, "a:tcPr")[0];
7672
- const background = readSolidFillColor(tcPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(tcPr, "a:solidFill")[0], context.colorMap, context.theme);
7673
- const txBody = (0, ooxml_js.childrenWithTag)(tc, "a:txBody")[0];
7674
- const gridSpan = (0, ooxml_js.attr)(tc, "gridSpan");
7675
- const rowSpan = (0, ooxml_js.attr)(tc, "rowSpan");
7676
- return {
7677
- blocks: textBodyParagraphs(txBody, void 0, context, slideRels),
7678
- colSpan: gridSpan === void 0 ? void 0 : Number(gridSpan),
7679
- rowSpan: rowSpan === void 0 ? void 0 : Number(rowSpan),
7680
- background
7681
- };
7682
- }
7683
- function readTable(tbl, context, slideRels) {
7684
- const tblGrid = (0, ooxml_js.childrenWithTag)(tbl, "a:tblGrid")[0];
7685
- const columnWidthsPt = tblGrid === void 0 ? [] : (0, ooxml_js.childrenWithTag)(tblGrid, "a:gridCol").map((col) => emuToPt(Number((0, ooxml_js.attr)(col, "w") ?? "0")));
7686
- return {
7687
- kind: "table",
7688
- rows: (0, ooxml_js.childrenWithTag)(tbl, "a:tr").map((tr) => {
7689
- const h = (0, ooxml_js.attr)(tr, "h");
7690
- return {
7691
- cells: (0, ooxml_js.childrenWithTag)(tr, "a:tc").map((tc) => readTableCell(tc, context, slideRels)),
7692
- heightPt: h === void 0 ? void 0 : emuToPt(Number(h))
7693
- };
7694
- }),
7695
- columnWidthsPt
7696
- };
7697
- }
7698
- function readGraphicFrameShape(gf, context, slideRels, parentTransform) {
7699
- const xfrm = readXfrm((0, ooxml_js.childrenWithTag)(gf, "p:xfrm")[0]);
7700
- if (xfrm === void 0) return;
7701
- const localFrame = {
7702
- xPt: xfrm.xPt,
7703
- yPt: xfrm.yPt,
7704
- widthPt: xfrm.widthPt,
7705
- heightPt: xfrm.heightPt
7706
- };
7707
- const frame = parentTransform === void 0 ? localFrame : applyGroupTransform(parentTransform, localFrame);
7708
- const rotationDeg = xfrm.rotationDeg === 0 ? void 0 : xfrm.rotationDeg;
7709
- const graphic = (0, ooxml_js.childrenWithTag)(gf, "a:graphic")[0];
7710
- const graphicData = graphic === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(graphic, "a:graphicData")[0];
7711
- const tbl = (graphicData === void 0 ? void 0 : (0, ooxml_js.attr)(graphicData, "uri")) === TABLE_GRAPHIC_URI && graphicData !== void 0 ? (0, ooxml_js.childrenWithTag)(graphicData, "a:tbl")[0] : void 0;
7712
- const blocks = tbl === void 0 ? [] : [readTable(tbl, context, slideRels)];
7713
- return {
7714
- name: shapeName(gf),
7715
- frame,
7716
- rotationDeg,
7717
- ...NO_TEXT_BODY_EXTRAS,
7718
- blocks
7719
- };
7720
- }
7721
- function walkShapeTreeChildren(children, parentTransform, context, slideRels, pkg, out) {
7722
- for (const node of children) {
7723
- if (node.type !== "element") continue;
7724
- if (node.tag === "p:sp") {
7725
- const shape = readSpShape(node, context, slideRels, parentTransform);
7726
- if (shape !== void 0) out.push(shape);
7727
- } else if (node.tag === "p:pic") {
7728
- const shape = readPicShape(node, context, slideRels, pkg, parentTransform);
7729
- if (shape !== void 0) out.push(shape);
7730
- } else if (node.tag === "p:graphicFrame") {
7731
- const shape = readGraphicFrameShape(node, context, slideRels, parentTransform);
7732
- if (shape !== void 0) out.push(shape);
7733
- } else if (node.tag === "p:grpSp") {
7734
- const grpSpPr = (0, ooxml_js.childrenWithTag)(node, "p:grpSpPr")[0];
7735
- const composed = composeGroupTransform(readGroupXfrm(grpSpPr === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(grpSpPr, "a:xfrm")[0]), parentTransform);
7736
- walkShapeTreeChildren(node.children, composed, context, slideRels, pkg, out);
7737
- }
7738
- }
7739
- }
7740
- function composeGroupTransform(own, parent) {
7741
- if (own === void 0) return;
7742
- if (parent === void 0) return own;
7743
- const absolute = applyGroupTransform(parent, {
7744
- xPt: own.offXPt,
7745
- yPt: own.offYPt,
7746
- widthPt: own.extWidthPt,
7747
- heightPt: own.extHeightPt
7748
- });
7749
- return {
7750
- ...own,
7751
- offXPt: absolute.xPt,
7752
- offYPt: absolute.yPt,
7753
- extWidthPt: absolute.widthPt,
7754
- extHeightPt: absolute.heightPt
7755
- };
7756
- }
7757
- const NOTES_SLIDE_REL_SUFFIX = "/notesSlide";
7758
- function readNotes(pkg, slidePath) {
7759
- let notesPath;
7760
- for (const rel of (0, ooxml_js.resolveRelationships)(pkg, slidePath).values()) if (rel.type.endsWith(NOTES_SLIDE_REL_SUFFIX)) {
7761
- notesPath = rel.target;
7762
- break;
7763
- }
7764
- if (notesPath === void 0) return "";
7765
- const notesRoot = (0, ooxml_js.rootElement)(pkg.parts[notesPath]);
7766
- if (notesRoot === void 0) return "";
7767
- const bodyShape = (0, ooxml_js.elementsWithTag)([notesRoot], "p:sp").find((shape) => {
7768
- const key = readPlaceholderKey(shape);
7769
- return key !== void 0 && (key.type === void 0 || key.type === "body");
7770
- });
7771
- if (bodyShape !== void 0) {
7772
- const txBody = (0, ooxml_js.childrenWithTag)(bodyShape, "p:txBody")[0];
7773
- if (txBody !== void 0) return (0, ooxml_js.elementsWithTag)(txBody.children, "a:t").map(ooxml_js.textContent).join("");
7774
- }
7775
- return (0, ooxml_js.elementsWithTag)([notesRoot], "a:t").map(ooxml_js.textContent).join("");
7776
- }
7777
- function readSlide(pkg, slidePath, size) {
7778
- const slideRoot = (0, ooxml_js.rootElement)(pkg.parts[slidePath]);
7779
- const context = resolveSlideInheritance(pkg, slidePath);
7780
- const slideRels = (0, ooxml_js.resolveRelationships)(pkg, slidePath);
7781
- const cSld = slideRoot === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(slideRoot, "p:cSld")[0];
7782
- const spTree = cSld === void 0 ? void 0 : (0, ooxml_js.childrenWithTag)(cSld, "p:spTree")[0];
7783
- const shapes = [];
7784
- if (spTree !== void 0) walkShapeTreeChildren(spTree.children, void 0, context, slideRels, pkg, shapes);
7785
- return {
7786
- size,
7787
- shapes,
7788
- notes: readNotes(pkg, slidePath)
7789
- };
7790
- }
7791
6635
  function readPptxContent(pkg) {
7792
- const presentationRoot = (0, ooxml_js.rootElement)(pkg.parts[PRESENTATION_PATH]);
7793
- const size = readSlideSize(presentationRoot);
7794
- const slides = readSlidePathsInOrder(pkg, presentationRoot).map((slidePath) => readSlide(pkg, slidePath, size));
6636
+ const pptxDoc = (0, ooxml_js.readPptx)(pkg);
7795
6637
  return {
7796
6638
  kind: "presentation",
7797
6639
  formatVersion: 1,
7798
- metadata: readCoreProperties(pkg),
7799
- slides
6640
+ metadata: { ...pptxDoc.metadata },
6641
+ slides: pptxDoc.slides
7800
6642
  };
7801
6643
  }
7802
6644
  //#endregion
@@ -8024,7 +6866,7 @@ function toStyledRuns(runs, fontScale = 1) {
8024
6866
  text: run.text,
8025
6867
  font: runFont(run),
8026
6868
  sizePt: (run.sizePt ?? 18) * fontScale,
8027
- color: run.color ?? COLOR_BLACK,
6869
+ color: run.color ?? ooxml_js.COLOR_BLACK,
8028
6870
  underline: run.underline,
8029
6871
  hyperlink: run.hyperlink
8030
6872
  }));
@@ -8035,7 +6877,7 @@ function effectiveStyledRuns(runs, fontScale = 1) {
8035
6877
  text: "",
8036
6878
  font: DEFAULT_LAYOUT_FONT,
8037
6879
  sizePt: 18 * fontScale,
8038
- color: COLOR_BLACK
6880
+ color: ooxml_js.COLOR_BLACK
8039
6881
  }];
8040
6882
  }
8041
6883
  function lineNaturalHeightPt(line, measurer, fallback) {
@@ -8782,6 +7624,16 @@ function pdfToPptx(bytes, options) {
8782
7624
  return (0, ooxml_js.encodePackage)(buildPptxPackage(content));
8783
7625
  }
8784
7626
  //#endregion
7627
+ //#region src/convert/codec.ts
7628
+ const docxPdfCodec = zod.z.codec(DocxBytesSchema, PdfBytesSchema, {
7629
+ decode: (docxBytes) => docxToPdf(docxBytes),
7630
+ encode: (pdfBytes) => pdfToDocx(pdfBytes)
7631
+ });
7632
+ const pptxPdfCodec = zod.z.codec(PptxBytesSchema, PdfBytesSchema, {
7633
+ decode: (pptxBytes) => pptxToPdf(pptxBytes),
7634
+ encode: (pdfBytes) => pdfToPptx(pdfBytes)
7635
+ });
7636
+ //#endregion
8785
7637
  //#region src/convert/local.ts
8786
7638
  const SUPPORTED_CONVERSIONS = [
8787
7639
  {
@@ -8899,7 +7751,12 @@ Object.defineProperty(exports, "BinaryPartSchema", {
8899
7751
  return ooxml_js.BinaryPartSchema;
8900
7752
  }
8901
7753
  });
8902
- exports.COLOR_BLACK = COLOR_BLACK;
7754
+ Object.defineProperty(exports, "COLOR_BLACK", {
7755
+ enumerable: true,
7756
+ get: function() {
7757
+ return ooxml_js.COLOR_BLACK;
7758
+ }
7759
+ });
8903
7760
  exports.CONTENT_FORMAT_VERSION = CONTENT_FORMAT_VERSION;
8904
7761
  Object.defineProperty(exports, "CommentSchema", {
8905
7762
  enumerable: true,
@@ -8944,6 +7801,7 @@ Object.defineProperty(exports, "DefinedNameSchema", {
8944
7801
  return ooxml_js.DefinedNameSchema;
8945
7802
  }
8946
7803
  });
7804
+ exports.DocxBytesSchema = DocxBytesSchema;
8947
7805
  exports.DocxEditor = DocxEditor;
8948
7806
  exports.DocxParagraph = DocxParagraph;
8949
7807
  exports.DocxRun = DocxRun;
@@ -8952,8 +7810,18 @@ exports.DocxTableCell = DocxTableCell;
8952
7810
  exports.DocxTableRow = DocxTableRow;
8953
7811
  exports.LAYOUT_FORMAT_VERSION = LAYOUT_FORMAT_VERSION;
8954
7812
  exports.NOOP_DIAGNOSTIC_SINK = NOOP_DIAGNOSTIC_SINK;
8955
- exports.PAGE_SIZE_A4 = PAGE_SIZE_A4;
8956
- exports.PAGE_SIZE_LETTER = PAGE_SIZE_LETTER;
7813
+ Object.defineProperty(exports, "PAGE_SIZE_A4", {
7814
+ enumerable: true,
7815
+ get: function() {
7816
+ return ooxml_js.PAGE_SIZE_A4;
7817
+ }
7818
+ });
7819
+ Object.defineProperty(exports, "PAGE_SIZE_LETTER", {
7820
+ enumerable: true,
7821
+ get: function() {
7822
+ return ooxml_js.PAGE_SIZE_LETTER;
7823
+ }
7824
+ });
8957
7825
  Object.defineProperty(exports, "PackageSchema", {
8958
7826
  enumerable: true,
8959
7827
  get: function() {
@@ -8966,13 +7834,25 @@ Object.defineProperty(exports, "PartSchema", {
8966
7834
  return ooxml_js.PartSchema;
8967
7835
  }
8968
7836
  });
7837
+ exports.PdfBytesSchema = PdfBytesSchema;
8969
7838
  exports.PdfEncryptedError = PdfEncryptedError;
8970
7839
  exports.PdfParseError = PdfParseError;
7840
+ exports.PptxBytesSchema = PptxBytesSchema;
8971
7841
  exports.PptxEditor = PptxEditor;
8972
7842
  exports.PptxShape = PptxShape;
8973
7843
  exports.PptxSlide = PptxSlide;
8974
- exports.SLIDE_SIZE_STANDARD = SLIDE_SIZE_STANDARD;
8975
- exports.SLIDE_SIZE_WIDESCREEN = SLIDE_SIZE_WIDESCREEN;
7844
+ Object.defineProperty(exports, "SLIDE_SIZE_STANDARD", {
7845
+ enumerable: true,
7846
+ get: function() {
7847
+ return ooxml_js.SLIDE_SIZE_STANDARD;
7848
+ }
7849
+ });
7850
+ Object.defineProperty(exports, "SLIDE_SIZE_WIDESCREEN", {
7851
+ enumerable: true,
7852
+ get: function() {
7853
+ return ooxml_js.SLIDE_SIZE_WIDESCREEN;
7854
+ }
7855
+ });
8976
7856
  Object.defineProperty(exports, "XmlCdataSchema", {
8977
7857
  enumerable: true,
8978
7858
  get: function() {
@@ -9088,6 +7968,7 @@ Object.defineProperty(exports, "decodePackage", {
9088
7968
  return ooxml_js.decodePackage;
9089
7969
  }
9090
7970
  });
7971
+ exports.docxPdfCodec = docxPdfCodec;
9091
7972
  exports.docxToPdf = docxToPdf;
9092
7973
  Object.defineProperty(exports, "elementsWithTag", {
9093
7974
  enumerable: true,
@@ -9148,8 +8029,10 @@ Object.defineProperty(exports, "parseXml", {
9148
8029
  return ooxml_js.parseXml;
9149
8030
  }
9150
8031
  });
8032
+ exports.pdfCodec = pdfCodec;
9151
8033
  exports.pdfToDocx = pdfToDocx;
9152
8034
  exports.pdfToPptx = pdfToPptx;
8035
+ exports.pptxPdfCodec = pptxPdfCodec;
9153
8036
  exports.pptxToPdf = pptxToPdf;
9154
8037
  exports.readDocxContent = readDocxContent;
9155
8038
  exports.readPdf = readPdf;
@@ -9162,7 +8045,12 @@ Object.defineProperty(exports, "resolveRelationships", {
9162
8045
  return ooxml_js.resolveRelationships;
9163
8046
  }
9164
8047
  });
9165
- exports.rgbHexToColor = rgbHexToColor;
8048
+ Object.defineProperty(exports, "rgbHexToColor", {
8049
+ enumerable: true,
8050
+ get: function() {
8051
+ return ooxml_js.rgbHexToColor;
8052
+ }
8053
+ });
9166
8054
  Object.defineProperty(exports, "rootElement", {
9167
8055
  enumerable: true,
9168
8056
  get: function() {