@file-viewer/assets-ppt 3.3.1 → 3.3.4

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 (109) hide show
  1. package/README.en.md +3 -1
  2. package/README.md +3 -1
  3. package/README.zh-CN.md +3 -1
  4. package/package.json +96 -9
  5. package/viewer/THIRD_PARTY_LICENSES.assets.json +837 -54
  6. package/viewer/file-viewer-asset-pack.json +20 -31
  7. package/viewer/vendor/ppt/LICENSE +160 -122
  8. package/viewer/vendor/ppt/NOTICE.md +30 -0
  9. package/viewer/vendor/ppt/README.md +86 -0
  10. package/viewer/vendor/ppt/dist/core/binary.d.ts +31 -0
  11. package/viewer/vendor/ppt/dist/core/binary.js +102 -0
  12. package/viewer/vendor/ppt/dist/core/cfb.d.ts +38 -0
  13. package/viewer/vendor/ppt/dist/core/cfb.js +208 -0
  14. package/viewer/vendor/ppt/dist/core/crypto.d.ts +12 -0
  15. package/viewer/vendor/ppt/dist/core/crypto.js +64 -0
  16. package/viewer/vendor/ppt/dist/core/errors.d.ts +5 -0
  17. package/viewer/vendor/ppt/dist/core/errors.js +5 -0
  18. package/viewer/vendor/ppt/dist/core/inflate.d.ts +11 -0
  19. package/viewer/vendor/ppt/dist/core/inflate.js +337 -0
  20. package/viewer/vendor/ppt/dist/core/pict.d.ts +14 -0
  21. package/viewer/vendor/ppt/dist/core/pict.js +472 -0
  22. package/viewer/vendor/ppt/dist/core/raster.d.ts +7 -0
  23. package/viewer/vendor/ppt/dist/core/raster.js +101 -0
  24. package/viewer/vendor/ppt/dist/core/tiff.d.ts +20 -0
  25. package/viewer/vendor/ppt/dist/core/tiff.js +254 -0
  26. package/viewer/vendor/ppt/dist/core/utils.d.ts +9 -0
  27. package/viewer/vendor/ppt/dist/core/utils.js +13 -0
  28. package/viewer/vendor/ppt/dist/core/vector-font.d.ts +12 -0
  29. package/viewer/vendor/ppt/dist/core/vector-font.js +29 -0
  30. package/viewer/vendor/ppt/dist/core/vector.d.ts +19 -0
  31. package/viewer/vendor/ppt/dist/core/vector.js +1570 -0
  32. package/viewer/vendor/ppt/dist/core/xml-text.d.ts +1 -0
  33. package/viewer/vendor/ppt/dist/core/xml-text.js +6 -0
  34. package/viewer/vendor/ppt/dist/index.d.ts +12 -0
  35. package/viewer/vendor/ppt/dist/index.js +8 -0
  36. package/viewer/vendor/ppt/dist/layout/measure.d.ts +15 -0
  37. package/viewer/vendor/ppt/dist/layout/measure.js +54 -0
  38. package/viewer/vendor/ppt/dist/layout/text-layout.d.ts +48 -0
  39. package/viewer/vendor/ppt/dist/layout/text-layout.js +272 -0
  40. package/viewer/vendor/ppt/dist/manifest.json +13 -0
  41. package/viewer/vendor/ppt/dist/playback/media.d.ts +11 -0
  42. package/viewer/vendor/ppt/dist/playback/media.js +37 -0
  43. package/viewer/vendor/ppt/dist/playback/navigation.d.ts +2 -0
  44. package/viewer/vendor/ppt/dist/playback/navigation.js +14 -0
  45. package/viewer/vendor/ppt/dist/playback/timeline.d.ts +47 -0
  46. package/viewer/vendor/ppt/dist/playback/timeline.js +154 -0
  47. package/viewer/vendor/ppt/dist/playback/transitions.d.ts +14 -0
  48. package/viewer/vendor/ppt/dist/playback/transitions.js +41 -0
  49. package/viewer/vendor/ppt/dist/ppt/animation.d.ts +3 -0
  50. package/viewer/vendor/ppt/dist/ppt/animation.js +67 -0
  51. package/viewer/vendor/ppt/dist/ppt/constants.d.ts +72 -0
  52. package/viewer/vendor/ppt/dist/ppt/constants.js +21 -0
  53. package/viewer/vendor/ppt/dist/ppt/drawing.d.ts +15 -0
  54. package/viewer/vendor/ppt/dist/ppt/drawing.js +508 -0
  55. package/viewer/vendor/ppt/dist/ppt/encryption.d.ts +14 -0
  56. package/viewer/vendor/ppt/dist/ppt/encryption.js +241 -0
  57. package/viewer/vendor/ppt/dist/ppt/extras.d.ts +14 -0
  58. package/viewer/vendor/ppt/dist/ppt/extras.js +225 -0
  59. package/viewer/vendor/ppt/dist/ppt/geometry.d.ts +29 -0
  60. package/viewer/vendor/ppt/dist/ppt/geometry.js +448 -0
  61. package/viewer/vendor/ppt/dist/ppt/headers-footers.d.ts +11 -0
  62. package/viewer/vendor/ppt/dist/ppt/headers-footers.js +97 -0
  63. package/viewer/vendor/ppt/dist/ppt/interactions.d.ts +12 -0
  64. package/viewer/vendor/ppt/dist/ppt/interactions.js +104 -0
  65. package/viewer/vendor/ppt/dist/ppt/parser.d.ts +12 -0
  66. package/viewer/vendor/ppt/dist/ppt/parser.js +356 -0
  67. package/viewer/vendor/ppt/dist/ppt/pictures.d.ts +10 -0
  68. package/viewer/vendor/ppt/dist/ppt/pictures.js +215 -0
  69. package/viewer/vendor/ppt/dist/ppt/records.d.ts +13 -0
  70. package/viewer/vendor/ppt/dist/ppt/records.js +49 -0
  71. package/viewer/vendor/ppt/dist/ppt/text.d.ts +19 -0
  72. package/viewer/vendor/ppt/dist/ppt/text.js +789 -0
  73. package/viewer/vendor/ppt/dist/pptjs.browser.js +9651 -0
  74. package/viewer/vendor/ppt/dist/pptjs.mjs +9663 -0
  75. package/viewer/vendor/ppt/dist/pptjs.worker.js +9650 -0
  76. package/viewer/vendor/ppt/dist/render/effects.d.ts +23 -0
  77. package/viewer/vendor/ppt/dist/render/effects.js +125 -0
  78. package/viewer/vendor/ppt/dist/render/fields.d.ts +11 -0
  79. package/viewer/vendor/ppt/dist/render/fields.js +176 -0
  80. package/viewer/vendor/ppt/dist/render/geometry.d.ts +11 -0
  81. package/viewer/vendor/ppt/dist/render/geometry.js +131 -0
  82. package/viewer/vendor/ppt/dist/render/safe.d.ts +12 -0
  83. package/viewer/vendor/ppt/dist/render/safe.js +48 -0
  84. package/viewer/vendor/ppt/dist/render/svg.d.ts +4 -0
  85. package/viewer/vendor/ppt/dist/render/svg.js +279 -0
  86. package/viewer/vendor/ppt/dist/render/text.d.ts +9 -0
  87. package/viewer/vendor/ppt/dist/render/text.js +181 -0
  88. package/viewer/vendor/ppt/dist/render/three-d.d.ts +32 -0
  89. package/viewer/vendor/ppt/dist/render/three-d.js +219 -0
  90. package/viewer/vendor/ppt/dist/render/wordart.d.ts +16 -0
  91. package/viewer/vendor/ppt/dist/render/wordart.js +214 -0
  92. package/viewer/vendor/ppt/dist/runtime.d.ts +125 -0
  93. package/viewer/vendor/ppt/dist/runtime.js +226 -0
  94. package/viewer/vendor/ppt/dist/types.d.ts +546 -0
  95. package/viewer/vendor/ppt/dist/types.js +1 -0
  96. package/viewer/vendor/ppt/dist/viewer.d.ts +3 -0
  97. package/viewer/vendor/ppt/dist/viewer.js +264 -0
  98. package/viewer/vendor/ppt/dist/worker-client.d.ts +25 -0
  99. package/viewer/vendor/ppt/dist/worker-client.js +61 -0
  100. package/viewer/vendor/ppt/dist/worker.d.ts +1 -0
  101. package/viewer/vendor/ppt/dist/worker.js +25 -0
  102. package/viewer/vendor/ppt/package.json +40 -52
  103. package/viewer/vendor/ppt/NOTICE +0 -229
  104. package/viewer/vendor/ppt/frame-cache.mjs +0 -731
  105. package/viewer/vendor/ppt/index.mjs +0 -941
  106. package/viewer/vendor/ppt/manifest.json +0 -44
  107. package/viewer/vendor/ppt/ppt-font-cjk.otf +0 -0
  108. package/viewer/vendor/ppt/ppt-native.wasm +0 -0
  109. package/viewer/vendor/ppt/worker.mjs +0 -1720
@@ -0,0 +1,472 @@
1
+ /** Apple Inside Macintosh: Imaging With QuickDraw, Appendix A (picture opcodes).
2
+ * This interpreter emits browser-safe SVG/PNG and never executes embedded QuickTime code.
3
+ */
4
+ import { PptError } from './errors.js';
5
+ import { encodeRgbaPng, unpackBits } from './raster.js';
6
+ import { bytesToBase64, escapeXmlText } from './utils.js';
7
+ const f = (v) => String(Math.round(v * 10000) / 10000);
8
+ const legacyColors = { 30: '#ffffff', 33: '#000000', 69: '#ffff00', 137: '#ff00ff', 205: '#ff0000', 273: '#00ffff', 341: '#00ff00', 409: '#0000ff' };
9
+ export function pictToSvg(bytes, options = {}) {
10
+ const maxOutput = options.maxOutputBytes ?? 128 * 1024 * 1024, maxDecoded = options.maxDecodedBytes ?? 128 * 1024 * 1024, maxRecords = options.maxRecords ?? 200000;
11
+ if (![maxOutput, maxDecoded, maxRecords].every(x => Number.isSafeInteger(x) && x > 0))
12
+ throw new PptError('INVALID_LIMIT', 'Invalid PICT resource limit');
13
+ const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length);
14
+ let p = 0, records = 0, outputSize = 0;
15
+ const need = (n) => { if (!Number.isSafeInteger(n) || n < 0 || p + n > bytes.length)
16
+ throw new PptError('PICT_TRUNCATED', 'PICT operation exceeds its input', p); };
17
+ const skip = (n) => { need(n); p += n; }, u8 = () => { need(1); return bytes[p++]; }, i8 = () => { const v = u8(); return v & 128 ? v - 256 : v; }, u16 = () => { need(2); const v = view.getUint16(p); p += 2; return v; }, i16 = () => { const v = u16(); return v & 32768 ? v - 65536 : v; }, u32 = () => { need(4); const v = view.getUint32(p); p += 4; return v; };
18
+ const point = () => { const y = i16(), x = i16(); return { x, y }; }, rect = () => { const top = i16(), left = i16(), bottom = i16(), right = i16(); return { x: left, y: top, width: right - left, height: bottom - top }; };
19
+ const versionAt = (at) => at + 12 <= bytes.length && (bytes[at + 10] === 0 && bytes[at + 11] === 0x11 && bytes[at + 12] === 2 && bytes[at + 13] === 255 || bytes[at + 10] === 0x11 && bytes[at + 11] === 1);
20
+ if (!versionAt(0)) {
21
+ if (versionAt(512))
22
+ p = 512;
23
+ else
24
+ throw new PptError('PICT_HEADER', 'PICT version header is missing');
25
+ }
26
+ const start = p;
27
+ skip(2);
28
+ const frame = rect();
29
+ if (frame.width <= 0 || frame.height <= 0 || frame.width > 1e6 || frame.height > 1e6)
30
+ throw new PptError('PICT_SIZE', 'Invalid PICT frame');
31
+ const v2 = bytes[p] === 0;
32
+ if (v2)
33
+ skip(4);
34
+ else
35
+ skip(2);
36
+ let fg = '#000000', bg = '#ffffff', pen = { x: 1, y: 1 }, oval = { x: 16, y: 16 }, loc = { x: 0, y: 0 }, textLoc = { x: 0, y: 0 }, origin = { x: 0, y: 0 }, font = 'Arial', fontSize = 12, face = 0, mode = 8, clip = '', ended = false;
37
+ const defs = [], body = [], unsupported = new Set(), warnings = new Set(), lastRect = new Map();
38
+ let lastPoly = [], lastRegion = '';
39
+ const append = (value, target = body) => { outputSize += value.length; if (outputSize > maxOutput)
40
+ throw new PptError('LIMIT_ASSETS', 'PICT SVG exceeds the configured output budget'); target.push(value); };
41
+ const rgb = () => `#${[u16(), u16(), u16()].map(v => Math.round(v / 257).toString(16).padStart(2, '0')).join('')}`;
42
+ const oldColor = () => legacyColors[u32()] ?? '#000000';
43
+ const pattern = (data) => { if (data.every(x => x === 255))
44
+ return fg; if (data.every(x => x === 0))
45
+ return bg; const id = 'pict-pattern-' + defs.length; let pixels = ''; for (let y = 0; y < 8; y++)
46
+ for (let x = 0; x < 8; x++)
47
+ if (data[y] & (128 >> x))
48
+ pixels += `<rect x="${x}" y="${y}" width="1" height="1"/>`; append(`<pattern id="${id}" patternUnits="userSpaceOnUse" width="8" height="8"><rect width="8" height="8" fill="${bg}"/><g fill="${fg}">${pixels}</g></pattern>`, defs); return `url(#${id})`; };
49
+ let penPattern, fillPattern, bgPattern;
50
+ const attrs = (verb) => { const fill = verb === 0 ? 'none' : verb === 2 ? (bgPattern ? pattern(bgPattern) : bg) : verb === 4 ? (fillPattern ? pattern(fillPattern) : fg) : fg; if (verb === 3)
51
+ warnings.add('PICT_INVERT_APPROXIMATION'); if (mode !== 8 && mode !== 0)
52
+ warnings.add('PICT_TRANSFER_MODE_APPROXIMATION'); return `fill="${fill}" stroke="${verb === 0 ? (penPattern ? pattern(penPattern) : fg) : 'none'}" stroke-width="${f(Math.max(pen.x, pen.y))}"`; };
53
+ const draw = (content) => append(`<g${clip ? ` clip-path="url(#${clip})"` : ''} transform="translate(${origin.x} ${origin.y})">${content}</g>`);
54
+ function region() {
55
+ const begin = p, size = u16();
56
+ if (size < 10 || size % 2)
57
+ throw new PptError('PICT_REGION', 'Invalid QuickDraw region size', begin);
58
+ need(size - 2);
59
+ const end = begin + size, b = rect();
60
+ if (size === 10)
61
+ return `<rect x="${b.x}" y="${b.y}" width="${Math.max(0, b.width)}" height="${Math.max(0, b.height)}"/>`;
62
+ const active = new Set();
63
+ let previousY = b.y, out = '', done = false;
64
+ while (p < end) {
65
+ const y = i16();
66
+ if (y === 32767) {
67
+ done = true;
68
+ break;
69
+ }
70
+ if (y < previousY)
71
+ throw new PptError('PICT_REGION', 'QuickDraw region rows are unordered', p);
72
+ const edges = [...active].sort((a, b) => a - b);
73
+ if (edges.length % 2)
74
+ throw new PptError('PICT_REGION', 'QuickDraw region has unpaired scanline edges', p);
75
+ for (let i = 0; i < edges.length; i += 2)
76
+ if (y > previousY)
77
+ out += `<rect x="${edges[i]}" y="${previousY}" width="${edges[i + 1] - edges[i]}" height="${y - previousY}"/>`;
78
+ if (out.length > maxOutput)
79
+ throw new PptError('LIMIT_ASSETS', 'PICT region exceeds the output budget');
80
+ let terminated = false;
81
+ while (p < end) {
82
+ const x = i16();
83
+ if (x === 32767) {
84
+ terminated = true;
85
+ break;
86
+ }
87
+ if (active.has(x))
88
+ active.delete(x);
89
+ else
90
+ active.add(x);
91
+ }
92
+ if (!terminated)
93
+ throw new PptError('PICT_REGION', 'QuickDraw region row is unterminated', p);
94
+ previousY = y;
95
+ }
96
+ if (!done || p !== end)
97
+ throw new PptError('PICT_REGION', 'QuickDraw region terminator is invalid', p);
98
+ return out;
99
+ }
100
+ function pixmap(direct) {
101
+ if (direct)
102
+ skip(4);
103
+ const rowWord = u16(), rowBytes = rowWord & 0x3fff, bounds = rect(), indexed = !!(rowWord & 0x8000);
104
+ let depth = 1, pack = 0, components = 1, palette = [bg, fg];
105
+ if (indexed || direct) {
106
+ skip(2);
107
+ pack = u16();
108
+ skip(12);
109
+ const pixelType = u16();
110
+ depth = u16();
111
+ components = u16();
112
+ const componentBits = u16();
113
+ skip(12);
114
+ if (![0, 16].includes(pixelType) || ![1, 2, 4, 8, 16, 32].includes(depth) || depth >= 16 && !([3, 4].includes(components) && [5, 8].includes(componentBits)))
115
+ throw new PptError('PICT_PIXMAP', 'Unsupported QuickDraw pixel layout');
116
+ }
117
+ if (indexed && !direct) {
118
+ skip(4);
119
+ const flags = u16(), count = u16() + 1;
120
+ if (count > 256)
121
+ throw new PptError('PICT_PALETTE', 'QuickDraw palette is too large');
122
+ palette = [];
123
+ for (let i = 0; i < count; i++) {
124
+ const key = u16(), value = rgb();
125
+ palette[flags & 0x8000 ? i : key] = value;
126
+ }
127
+ }
128
+ if (!rowBytes || bounds.width <= 0 || bounds.height <= 0 || rowBytes < Math.ceil(bounds.width * depth / 8) || bounds.width * bounds.height * 4 > maxDecoded || rowBytes * bounds.height > maxDecoded)
129
+ throw new PptError('LIMIT_ASSETS', 'QuickDraw bitmap dimensions exceed the allocation budget');
130
+ return { rowBytes, bounds, depth, pack, components, palette };
131
+ }
132
+ function pixels(info, packed) {
133
+ const { rowBytes, bounds, depth, components, palette } = info;
134
+ let pack = info.pack;
135
+ if (!pack)
136
+ pack = depth === 16 ? 3 : depth === 32 ? 4 : packed ? 4 : 1;
137
+ if (rowBytes < 8)
138
+ pack = 1;
139
+ if (depth < 16)
140
+ pack = packed && rowBytes >= 8 ? 4 : 1;
141
+ if (![1, 2, 3, 4].includes(pack) || pack === 2 && depth !== 32 || pack === 3 && depth !== 16)
142
+ throw new PptError('PICT_PACKING', 'Unsupported QuickDraw pixel packing');
143
+ const rgba = new Uint8Array(bounds.width * bounds.height * 4);
144
+ for (let y = 0; y < bounds.height; y++) {
145
+ const expected = pack === 2 ? rowBytes / 4 * 3 : pack === 4 && depth === 32 ? rowBytes / 4 * components : rowBytes;
146
+ if (!Number.isInteger(expected))
147
+ throw new PptError('PICT_PIXMAP', 'Invalid row byte count');
148
+ let row;
149
+ if (pack === 1 || pack === 2) {
150
+ need(expected);
151
+ row = bytes.subarray(p, p + expected);
152
+ p += expected;
153
+ }
154
+ else {
155
+ const count = rowBytes > 250 ? u16() : u8();
156
+ need(count);
157
+ row = unpackBits(bytes.subarray(p, p + count), expected, pack === 3 ? 2 : 1);
158
+ p += count;
159
+ }
160
+ for (let x = 0; x < bounds.width; x++) {
161
+ const q = (y * bounds.width + x) * 4;
162
+ let r = 0, g = 0, b = 0, a = 255;
163
+ if (depth === 32) {
164
+ if (pack === 4) {
165
+ const span = rowBytes / 4, offset = components === 4 ? span : 0;
166
+ r = row[offset + x];
167
+ g = row[offset + span + x];
168
+ b = row[offset + span * 2 + x];
169
+ }
170
+ else {
171
+ const pos = x * (pack === 2 ? 3 : 4) + (pack === 2 ? 0 : 1);
172
+ r = row[pos];
173
+ g = row[pos + 1];
174
+ b = row[pos + 2];
175
+ }
176
+ }
177
+ else if (depth === 16) {
178
+ const v = (row[x * 2] << 8) | row[x * 2 + 1];
179
+ r = Math.round((v >>> 10 & 31) * 255 / 31);
180
+ g = Math.round((v >>> 5 & 31) * 255 / 31);
181
+ b = Math.round((v & 31) * 255 / 31);
182
+ }
183
+ else {
184
+ const bit = x * depth, value = (row[bit >>> 3] >>> (8 - depth - (bit & 7))) & ((1 << depth) - 1), c = palette[value];
185
+ if (!c)
186
+ throw new PptError('PICT_PALETTE', 'Bitmap references a missing palette entry');
187
+ r = parseInt(c.slice(1, 3), 16);
188
+ g = parseInt(c.slice(3, 5), 16);
189
+ b = parseInt(c.slice(5, 7), 16);
190
+ }
191
+ rgba.set([r, g, b, a], q);
192
+ }
193
+ }
194
+ return rgba;
195
+ }
196
+ function bitmap(op) {
197
+ const info = pixmap(op === 0x9a || op === 0x9b), src = rect(), dst = rect(), transfer = u16();
198
+ let mask = '';
199
+ if (op & 1)
200
+ mask = region();
201
+ const rgba = pixels(info, op >= 0x98);
202
+ if (src.width <= 0 || src.height <= 0 || dst.width <= 0 || dst.height <= 0)
203
+ return;
204
+ if (transfer !== 0 && transfer !== 64) {
205
+ unsupported.add(op);
206
+ warnings.add('PICT_TRANSFER_MODE_APPROXIMATION');
207
+ }
208
+ const png = encodeRgbaPng(info.bounds.width, info.bounds.height, rgba, maxOutput - outputSize), url = 'data:image/png;base64,' + bytesToBase64(png);
209
+ if (mask) {
210
+ const id = 'pict-mask-' + defs.length;
211
+ append(`<clipPath id="${id}">${mask}</clipPath>`, defs);
212
+ draw(`<g clip-path="url(#${id})"><svg x="${dst.x}" y="${dst.y}" width="${dst.width}" height="${dst.height}" viewBox="${src.x} ${src.y} ${src.width} ${src.height}" preserveAspectRatio="none"><image x="${info.bounds.x}" y="${info.bounds.y}" width="${info.bounds.width}" height="${info.bounds.height}" xlink:href="${url}"/></svg></g>`);
213
+ }
214
+ else
215
+ draw(`<svg x="${dst.x}" y="${dst.y}" width="${dst.width}" height="${dst.height}" viewBox="${src.x} ${src.y} ${src.width} ${src.height}" preserveAspectRatio="none"><image x="${info.bounds.x}" y="${info.bounds.y}" width="${info.bounds.width}" height="${info.bounds.height}" xlink:href="${url}"/></svg>`);
216
+ }
217
+ const textDecoder = new TextDecoder('macintosh');
218
+ while (p < bytes.length) {
219
+ if (++records > maxRecords)
220
+ throw new PptError('LIMIT_RECORDS', 'PICT operation count exceeds its configured budget');
221
+ if (v2 && (p - start) % 2)
222
+ skip(1);
223
+ const opcode = v2 ? u16() : u8();
224
+ if (opcode === 255) {
225
+ ended = true;
226
+ break;
227
+ }
228
+ switch (opcode) {
229
+ case 0:
230
+ case 0x1c:
231
+ case 0x1e: break;
232
+ case 1: {
233
+ const r = region();
234
+ clip = 'pict-clip-' + defs.length;
235
+ append(`<clipPath id="${clip}">${r}</clipPath>`, defs);
236
+ break;
237
+ }
238
+ case 2:
239
+ case 9:
240
+ case 10: {
241
+ need(8);
242
+ const data = new Uint8Array(bytes.subarray(p, p + 8));
243
+ p += 8;
244
+ if (opcode === 2)
245
+ bgPattern = data;
246
+ else if (opcode === 9)
247
+ penPattern = data;
248
+ else
249
+ fillPattern = data;
250
+ break;
251
+ }
252
+ case 3: {
253
+ const id = u16();
254
+ font = { 0: 'Chicago', 1: 'Geneva', 2: 'New York', 3: 'Geneva', 4: 'Monaco', 20: 'Times', 21: 'Helvetica', 22: 'Courier' }[id] ?? 'Arial';
255
+ break;
256
+ }
257
+ case 4:
258
+ face = u8();
259
+ break;
260
+ case 5:
261
+ case 0x15:
262
+ case 0x16:
263
+ skip(2);
264
+ unsupported.add(opcode);
265
+ break;
266
+ case 6:
267
+ skip(4);
268
+ unsupported.add(opcode);
269
+ break;
270
+ case 7:
271
+ pen = point();
272
+ break;
273
+ case 8:
274
+ mode = u16();
275
+ break;
276
+ case 11:
277
+ oval = point();
278
+ break;
279
+ case 12: {
280
+ origin.x += i16();
281
+ origin.y += i16();
282
+ warnings.add('PICT_ORIGIN_APPROXIMATION');
283
+ break;
284
+ }
285
+ case 13:
286
+ fontSize = u16();
287
+ break;
288
+ case 14:
289
+ fg = oldColor();
290
+ break;
291
+ case 15:
292
+ bg = oldColor();
293
+ break;
294
+ case 0x10:
295
+ skip(8);
296
+ unsupported.add(opcode);
297
+ break;
298
+ case 0x12:
299
+ case 0x13:
300
+ case 0x14: {
301
+ const type = u16();
302
+ skip(8);
303
+ if (type === 2) {
304
+ const value = rgb();
305
+ if (opcode === 0x12)
306
+ bg = value;
307
+ else
308
+ fg = value;
309
+ }
310
+ else if (type === 1) {
311
+ const info = pixmap(false);
312
+ pixels(info, true);
313
+ unsupported.add(opcode);
314
+ }
315
+ else
316
+ throw new PptError('PICT_PATTERN', 'Unsupported QuickDraw pixel pattern');
317
+ break;
318
+ }
319
+ case 0x1a:
320
+ fg = rgb();
321
+ break;
322
+ case 0x1b:
323
+ bg = rgb();
324
+ break;
325
+ case 0x1d:
326
+ case 0x1f:
327
+ skip(6);
328
+ unsupported.add(opcode);
329
+ break;
330
+ case 0x20:
331
+ case 0x21:
332
+ case 0x22:
333
+ case 0x23: {
334
+ if (opcode === 0x20 || opcode === 0x22)
335
+ loc = point();
336
+ const old = { ...loc };
337
+ if (opcode <= 0x21)
338
+ loc = point();
339
+ else {
340
+ loc.x += i8();
341
+ loc.y += i8();
342
+ }
343
+ draw(`<path d="M${old.x} ${old.y} L${loc.x} ${loc.y}" ${attrs(0)}/>`);
344
+ break;
345
+ }
346
+ case 0x28:
347
+ case 0x29:
348
+ case 0x2a:
349
+ case 0x2b: {
350
+ if (opcode === 0x28)
351
+ textLoc = point();
352
+ if (opcode === 0x29 || opcode === 0x2b)
353
+ textLoc.x += u8();
354
+ if (opcode === 0x2a || opcode === 0x2b)
355
+ textLoc.y += u8();
356
+ const len = u8();
357
+ need(len);
358
+ const text = textDecoder.decode(bytes.subarray(p, p + len));
359
+ p += len;
360
+ draw(`<text x="${textLoc.x}" y="${textLoc.y}" fill="${fg}" font-family="${escapeXmlText(font)}" font-size="${fontSize}"${face & 1 ? ' font-weight="bold"' : ''}${face & 2 ? ' font-style="italic"' : ''}${face & 4 ? ' text-decoration="underline"' : ''}>${escapeXmlText(text)}</text>`);
361
+ textLoc.x += text.length * fontSize * .6;
362
+ warnings.add('PICT_FONT_METRICS_APPROXIMATION');
363
+ break;
364
+ }
365
+ case 0x2c: {
366
+ const size = u16(), end = p + size;
367
+ need(size);
368
+ skip(2);
369
+ const n = u8();
370
+ need(n);
371
+ if (p + n > end)
372
+ throw new PptError('PICT_FONT', 'PICT font name exceeds its record');
373
+ font = textDecoder.decode(bytes.subarray(p, p + n));
374
+ p = end;
375
+ break;
376
+ }
377
+ case 0x90:
378
+ case 0x91:
379
+ case 0x98:
380
+ case 0x99:
381
+ case 0x9a:
382
+ case 0x9b:
383
+ bitmap(opcode);
384
+ break;
385
+ case 0xa0:
386
+ skip(2);
387
+ break;
388
+ case 0xa1:
389
+ skip(2);
390
+ skip(u16());
391
+ break;
392
+ default: {
393
+ if (opcode >= 0x30 && opcode < 0x70) {
394
+ const group = opcode & 0xf0, verb = opcode & 7, same = !!(opcode & 8);
395
+ let box = same ? lastRect.get(group) : rect();
396
+ if (box && !same)
397
+ lastRect.set(group, box);
398
+ let startAngle = 0, arc = 0;
399
+ if (group === 0x60) {
400
+ startAngle = i16();
401
+ arc = i16();
402
+ }
403
+ if (!box || verb > 4)
404
+ break;
405
+ const b = box, a = attrs(verb);
406
+ if (group === 0x50)
407
+ draw(`<ellipse cx="${f(b.x + b.width / 2)}" cy="${f(b.y + b.height / 2)}" rx="${f(Math.max(0, b.width / 2))}" ry="${f(Math.max(0, b.height / 2))}" ${a}/>`);
408
+ else if (group === 0x60) {
409
+ if (arc === 0 || b.width <= 0 || b.height <= 0)
410
+ break;
411
+ const cx = b.x + b.width / 2, cy = b.y + b.height / 2, rx = b.width / 2, ry = b.height / 2;
412
+ // QuickDraw's sweep is not reduced modulo 360. A full turn paints an oval;
413
+ // one SVG A segment with identical endpoints would instead paint nothing.
414
+ if (Math.abs(arc) >= 360)
415
+ draw(`<ellipse cx="${f(cx)}" cy="${f(cy)}" rx="${f(rx)}" ry="${f(ry)}" ${a}/>`);
416
+ else {
417
+ const r1 = (startAngle - 90) * Math.PI / 180, r2 = (startAngle + arc - 90) * Math.PI / 180, x1 = cx + rx * Math.cos(r1), y1 = cy + ry * Math.sin(r1), x2 = cx + rx * Math.cos(r2), y2 = cy + ry * Math.sin(r2);
418
+ draw(`<path d="${verb === 0 ? '' : `M${f(cx)} ${f(cy)} L`}${verb === 0 ? 'M' : ''}${f(x1)} ${f(y1)} A${f(rx)} ${f(ry)} 0 ${Math.abs(arc) > 180 ? 1 : 0} ${arc >= 0 ? 1 : 0} ${f(x2)} ${f(y2)}${verb === 0 ? '' : ' Z'}" ${a}/>`);
419
+ }
420
+ }
421
+ else
422
+ draw(`<rect x="${b.x}" y="${b.y}" width="${Math.max(0, b.width)}" height="${Math.max(0, b.height)}"${group === 0x40 ? ` rx="${f(oval.x / 2)}" ry="${f(oval.y / 2)}"` : ''} ${a}/>`);
423
+ }
424
+ else if (opcode >= 0x70 && opcode < 0x80) {
425
+ const verb = opcode & 7;
426
+ if (!(opcode & 8)) {
427
+ const begin = p, size = u16();
428
+ if (size < 10 || (size - 10) % 4)
429
+ throw new PptError('PICT_POLYGON', 'Invalid QuickDraw polygon');
430
+ need(size - 2);
431
+ rect();
432
+ lastPoly = [];
433
+ while (p < begin + size)
434
+ lastPoly.push(point());
435
+ }
436
+ if (verb <= 4 && lastPoly.length)
437
+ draw(`<polygon points="${lastPoly.map(q => q.x + ',' + q.y).join(' ')}" ${attrs(verb)}/>`);
438
+ }
439
+ else if (opcode >= 0x80 && opcode < 0x90) {
440
+ if (!(opcode & 8))
441
+ lastRegion = region();
442
+ const verb = opcode & 7;
443
+ if (verb <= 4)
444
+ draw(`<g ${attrs(verb)}>${lastRegion}</g>`);
445
+ }
446
+ else if (opcode === 0xc00)
447
+ skip(24);
448
+ else if (opcode >= 0x100 && opcode <= 0x7fff)
449
+ skip((opcode >>> 8) * 2);
450
+ else if (opcode >= 0x8000 && opcode <= 0x80ff || opcode >= 0xb0 && opcode <= 0xcf) { }
451
+ else if (opcode >= 0x8100 || opcode >= 0xd0 && opcode <= 0xfe) {
452
+ skip(u32());
453
+ if (opcode === 0x8200 || opcode === 0x8201)
454
+ unsupported.add(opcode);
455
+ }
456
+ else if (opcode >= 0x24 && opcode <= 0x27 || opcode >= 0x2d && opcode <= 0x2f || opcode >= 0x92 && opcode <= 0x97 || opcode >= 0x9c && opcode <= 0x9f || opcode >= 0xa2 && opcode <= 0xaf) {
457
+ skip(u16());
458
+ if (opcode === 0x2d || opcode === 0x2e)
459
+ unsupported.add(opcode);
460
+ }
461
+ else
462
+ throw new PptError('PICT_OPCODE', `PICT opcode 0x${opcode.toString(16)} has no safely known length`, p);
463
+ }
464
+ }
465
+ }
466
+ if (!ended)
467
+ throw new PptError('PICT_END', 'PICT end-of-picture opcode is missing');
468
+ const svg = `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${frame.width}" height="${frame.height}" viewBox="${frame.x} ${frame.y} ${frame.width} ${frame.height}"><defs>${defs.join('')}</defs>${body.join('')}</svg>`, out = new TextEncoder().encode(svg);
469
+ if (out.length > maxOutput)
470
+ throw new PptError('LIMIT_ASSETS', 'PICT SVG exceeds the configured byte budget');
471
+ return { bytes: out, mimeType: 'image/svg+xml', width: frame.width, height: frame.height, unsupportedOpcodes: [...unsupported], warnings: [...warnings] };
472
+ }
@@ -0,0 +1,7 @@
1
+ /** BI_RGB 32-bit scan lines used by EMR_ALPHABLEND. GDI stores B,G,R,A;
2
+ * AC_SRC_ALPHA channels are premultiplied, whereas PNG stores straight RGBA. */
3
+ export declare function decodeBgraBitmap(width: number, signedHeight: number, bits: Uint8Array, perPixelAlpha: boolean, maxBytes?: number): Uint8Array;
4
+ /** Bounded PackBits, also used by QuickDraw's 16-bit pixel packets. */
5
+ export declare function unpackBits(input: Uint8Array, expected: number, unit?: number): Uint8Array;
6
+ /** PNG (RGBA8, filter zero, zlib stored blocks), specified by ISO/IEC 15948. */
7
+ export declare function encodeRgbaPng(width: number, height: number, rgba: Uint8Array, maxBytes?: number): Uint8Array;
@@ -0,0 +1,101 @@
1
+ import { PptError } from './errors.js';
2
+ /** BI_RGB 32-bit scan lines used by EMR_ALPHABLEND. GDI stores B,G,R,A;
3
+ * AC_SRC_ALPHA channels are premultiplied, whereas PNG stores straight RGBA. */
4
+ export function decodeBgraBitmap(width, signedHeight, bits, perPixelAlpha, maxBytes = 128 * 1024 * 1024) {
5
+ const height = Math.abs(signedHeight), length = width * height * 4;
6
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(signedHeight) || width <= 0 || height <= 0 || !Number.isSafeInteger(length) || length > maxBytes)
7
+ throw new PptError('LIMIT_ASSETS', 'BGRA bitmap exceeds the decoded byte budget');
8
+ if (bits.length < length)
9
+ throw new PptError('METAFILE_BITMAP', 'Truncated BGRA scan lines');
10
+ const rgba = new Uint8Array(length);
11
+ for (let y = 0; y < height; y++)
12
+ for (let x = 0; x < width; x++) {
13
+ const p = ((signedHeight > 0 ? height - 1 - y : y) * width + x) * 4, q = (y * width + x) * 4, a = perPixelAlpha ? bits[p + 3] : 255;
14
+ for (let c = 0; c < 3; c++) {
15
+ const value = bits[p + 2 - c];
16
+ rgba[q + c] = perPixelAlpha ? (a ? Math.min(255, Math.round(value * 255 / a)) : 0) : value;
17
+ }
18
+ rgba[q + 3] = a;
19
+ }
20
+ return rgba;
21
+ }
22
+ /** Bounded PackBits, also used by QuickDraw's 16-bit pixel packets. */
23
+ export function unpackBits(input, expected, unit = 1) {
24
+ if (!Number.isSafeInteger(expected) || expected < 0 || expected > 256 * 1024 * 1024 || unit !== 1 && unit !== 2)
25
+ throw new PptError('RASTER_SIZE', 'Invalid PackBits output size');
26
+ const out = new Uint8Array(expected);
27
+ let p = 0, q = 0;
28
+ while (p < input.length) {
29
+ const control = input[p++];
30
+ if (control === 128)
31
+ continue;
32
+ const count = (control < 128 ? control + 1 : 257 - control) * unit;
33
+ if (q + count > expected)
34
+ throw new PptError('PACKBITS_LENGTH', 'PackBits expands beyond the declared row/strip');
35
+ if (control < 128) {
36
+ if (p + count > input.length)
37
+ throw new PptError('PACKBITS_TRUNCATED', 'Truncated PackBits literal');
38
+ out.set(input.subarray(p, p + count), q);
39
+ p += count;
40
+ }
41
+ else {
42
+ if (p + unit > input.length)
43
+ throw new PptError('PACKBITS_TRUNCATED', 'Truncated PackBits repeat');
44
+ for (let i = 0; i < count; i++)
45
+ out[q + i] = input[p + i % unit];
46
+ p += unit;
47
+ }
48
+ q += count;
49
+ }
50
+ if (q !== expected)
51
+ throw new PptError('PACKBITS_LENGTH', 'PackBits does not fill the declared row/strip');
52
+ return out;
53
+ }
54
+ const crcTable = Uint32Array.from({ length: 256 }, (_, i) => { let c = i; for (let b = 0; b < 8; b++)
55
+ c = c & 1 ? (c >>> 1) ^ 0xedb88320 : c >>> 1; return c >>> 0; });
56
+ function crc(bytes) { let c = 0xffffffff; for (const b of bytes)
57
+ c = crcTable[(c ^ b) & 255] ^ (c >>> 8); return (c ^ 0xffffffff) >>> 0; }
58
+ /** PNG (RGBA8, filter zero, zlib stored blocks), specified by ISO/IEC 15948. */
59
+ export function encodeRgbaPng(width, height, rgba, maxBytes = 128 * 1024 * 1024) {
60
+ const row = width * 4, rawLength = (row + 1) * height;
61
+ if (!Number.isSafeInteger(width) || !Number.isSafeInteger(height) || width <= 0 || height <= 0 || rgba.length !== row * height || rawLength > maxBytes)
62
+ throw new PptError('LIMIT_ASSETS', 'Raster dimensions exceed the configured byte budget');
63
+ const blocks = Math.ceil(rawLength / 65535), zlen = 2 + rawLength + blocks * 5 + 4, total = 57 + zlen;
64
+ if (total > maxBytes)
65
+ throw new PptError('LIMIT_ASSETS', 'PNG output exceeds the configured byte budget');
66
+ const raw = new Uint8Array(rawLength);
67
+ for (let y = 0; y < height; y++)
68
+ raw.set(rgba.subarray(y * row, (y + 1) * row), y * (row + 1) + 1);
69
+ const out = new Uint8Array(total), view = new DataView(out.buffer);
70
+ out.set([137, 80, 78, 71, 13, 10, 26, 10]);
71
+ let at = 8;
72
+ const chunk = (type, data) => { view.setUint32(at, data.length); for (let i = 0; i < 4; i++)
73
+ out[at + 4 + i] = type.charCodeAt(i); out.set(data, at + 8); view.setUint32(at + 8 + data.length, crc(out.subarray(at + 4, at + 8 + data.length))); at += data.length + 12; };
74
+ const header = new Uint8Array(13), hv = new DataView(header.buffer);
75
+ hv.setUint32(0, width);
76
+ hv.setUint32(4, height);
77
+ header[8] = 8;
78
+ header[9] = 6;
79
+ chunk('IHDR', header);
80
+ const z = new Uint8Array(zlen), zv = new DataView(z.buffer);
81
+ z[0] = 0x78;
82
+ z[1] = 1;
83
+ let p = 2, a = 1, b = 0;
84
+ for (let from = 0; from < raw.length; from += 65535) {
85
+ const size = Math.min(65535, raw.length - from);
86
+ z[p++] = from + size === raw.length ? 1 : 0;
87
+ zv.setUint16(p, size, true);
88
+ zv.setUint16(p + 2, (~size) & 0xffff, true);
89
+ p += 4;
90
+ z.set(raw.subarray(from, from + size), p);
91
+ p += size;
92
+ }
93
+ for (let i = 0; i < raw.length; i++) {
94
+ a = (a + raw[i]) % 65521;
95
+ b = (b + a) % 65521;
96
+ }
97
+ zv.setUint32(p, ((b << 16) | a) >>> 0);
98
+ chunk('IDAT', z);
99
+ chunk('IEND', new Uint8Array());
100
+ return out;
101
+ }
@@ -0,0 +1,20 @@
1
+ export interface TiffImage {
2
+ width: number;
3
+ height: number;
4
+ rgba: Uint8Array;
5
+ pageCount: number;
6
+ warnings: string[];
7
+ }
8
+ export interface TiffOptions {
9
+ maxDecodedBytes?: number;
10
+ maxOutputBytes?: number;
11
+ page?: number;
12
+ }
13
+ /** TIFF's MSB-first LZW with the early code-width change, unlike GIF. */
14
+ export declare function decodeTiffLzw(bytes: Uint8Array, expected: number): Uint8Array;
15
+ /** Decode a chosen IFD; all pixel/strip/tile allocations are preflighted. */
16
+ export declare function decodeTiff(bytes: Uint8Array, options?: TiffOptions): TiffImage;
17
+ export declare function tiffToPng(bytes: Uint8Array, options?: TiffOptions): Omit<TiffImage, 'rgba'> & {
18
+ bytes: Uint8Array;
19
+ mimeType: 'image/png';
20
+ };