@file-viewer/assets-ppt 3.3.1 → 3.3.3
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/README.en.md +3 -1
- package/README.md +3 -1
- package/README.zh-CN.md +3 -1
- package/package.json +96 -9
- package/viewer/THIRD_PARTY_LICENSES.assets.json +837 -54
- package/viewer/file-viewer-asset-pack.json +20 -31
- package/viewer/vendor/ppt/LICENSE +160 -122
- package/viewer/vendor/ppt/NOTICE.md +30 -0
- package/viewer/vendor/ppt/README.md +86 -0
- package/viewer/vendor/ppt/dist/core/binary.d.ts +31 -0
- package/viewer/vendor/ppt/dist/core/binary.js +102 -0
- package/viewer/vendor/ppt/dist/core/cfb.d.ts +38 -0
- package/viewer/vendor/ppt/dist/core/cfb.js +208 -0
- package/viewer/vendor/ppt/dist/core/crypto.d.ts +12 -0
- package/viewer/vendor/ppt/dist/core/crypto.js +64 -0
- package/viewer/vendor/ppt/dist/core/errors.d.ts +5 -0
- package/viewer/vendor/ppt/dist/core/errors.js +5 -0
- package/viewer/vendor/ppt/dist/core/inflate.d.ts +11 -0
- package/viewer/vendor/ppt/dist/core/inflate.js +337 -0
- package/viewer/vendor/ppt/dist/core/pict.d.ts +14 -0
- package/viewer/vendor/ppt/dist/core/pict.js +472 -0
- package/viewer/vendor/ppt/dist/core/raster.d.ts +7 -0
- package/viewer/vendor/ppt/dist/core/raster.js +101 -0
- package/viewer/vendor/ppt/dist/core/tiff.d.ts +20 -0
- package/viewer/vendor/ppt/dist/core/tiff.js +254 -0
- package/viewer/vendor/ppt/dist/core/utils.d.ts +9 -0
- package/viewer/vendor/ppt/dist/core/utils.js +13 -0
- package/viewer/vendor/ppt/dist/core/vector-font.d.ts +12 -0
- package/viewer/vendor/ppt/dist/core/vector-font.js +29 -0
- package/viewer/vendor/ppt/dist/core/vector.d.ts +19 -0
- package/viewer/vendor/ppt/dist/core/vector.js +1570 -0
- package/viewer/vendor/ppt/dist/core/xml-text.d.ts +1 -0
- package/viewer/vendor/ppt/dist/core/xml-text.js +6 -0
- package/viewer/vendor/ppt/dist/index.d.ts +12 -0
- package/viewer/vendor/ppt/dist/index.js +8 -0
- package/viewer/vendor/ppt/dist/layout/measure.d.ts +15 -0
- package/viewer/vendor/ppt/dist/layout/measure.js +54 -0
- package/viewer/vendor/ppt/dist/layout/text-layout.d.ts +48 -0
- package/viewer/vendor/ppt/dist/layout/text-layout.js +272 -0
- package/viewer/vendor/ppt/dist/manifest.json +13 -0
- package/viewer/vendor/ppt/dist/playback/media.d.ts +11 -0
- package/viewer/vendor/ppt/dist/playback/media.js +37 -0
- package/viewer/vendor/ppt/dist/playback/navigation.d.ts +2 -0
- package/viewer/vendor/ppt/dist/playback/navigation.js +14 -0
- package/viewer/vendor/ppt/dist/playback/timeline.d.ts +47 -0
- package/viewer/vendor/ppt/dist/playback/timeline.js +154 -0
- package/viewer/vendor/ppt/dist/playback/transitions.d.ts +14 -0
- package/viewer/vendor/ppt/dist/playback/transitions.js +41 -0
- package/viewer/vendor/ppt/dist/ppt/animation.d.ts +3 -0
- package/viewer/vendor/ppt/dist/ppt/animation.js +67 -0
- package/viewer/vendor/ppt/dist/ppt/constants.d.ts +72 -0
- package/viewer/vendor/ppt/dist/ppt/constants.js +21 -0
- package/viewer/vendor/ppt/dist/ppt/drawing.d.ts +15 -0
- package/viewer/vendor/ppt/dist/ppt/drawing.js +508 -0
- package/viewer/vendor/ppt/dist/ppt/encryption.d.ts +14 -0
- package/viewer/vendor/ppt/dist/ppt/encryption.js +241 -0
- package/viewer/vendor/ppt/dist/ppt/extras.d.ts +14 -0
- package/viewer/vendor/ppt/dist/ppt/extras.js +225 -0
- package/viewer/vendor/ppt/dist/ppt/geometry.d.ts +29 -0
- package/viewer/vendor/ppt/dist/ppt/geometry.js +448 -0
- package/viewer/vendor/ppt/dist/ppt/headers-footers.d.ts +11 -0
- package/viewer/vendor/ppt/dist/ppt/headers-footers.js +97 -0
- package/viewer/vendor/ppt/dist/ppt/interactions.d.ts +12 -0
- package/viewer/vendor/ppt/dist/ppt/interactions.js +104 -0
- package/viewer/vendor/ppt/dist/ppt/parser.d.ts +12 -0
- package/viewer/vendor/ppt/dist/ppt/parser.js +356 -0
- package/viewer/vendor/ppt/dist/ppt/pictures.d.ts +10 -0
- package/viewer/vendor/ppt/dist/ppt/pictures.js +215 -0
- package/viewer/vendor/ppt/dist/ppt/records.d.ts +13 -0
- package/viewer/vendor/ppt/dist/ppt/records.js +49 -0
- package/viewer/vendor/ppt/dist/ppt/text.d.ts +19 -0
- package/viewer/vendor/ppt/dist/ppt/text.js +789 -0
- package/viewer/vendor/ppt/dist/pptjs.browser.js +9651 -0
- package/viewer/vendor/ppt/dist/pptjs.mjs +9663 -0
- package/viewer/vendor/ppt/dist/pptjs.worker.js +9650 -0
- package/viewer/vendor/ppt/dist/render/effects.d.ts +23 -0
- package/viewer/vendor/ppt/dist/render/effects.js +125 -0
- package/viewer/vendor/ppt/dist/render/fields.d.ts +11 -0
- package/viewer/vendor/ppt/dist/render/fields.js +176 -0
- package/viewer/vendor/ppt/dist/render/geometry.d.ts +11 -0
- package/viewer/vendor/ppt/dist/render/geometry.js +131 -0
- package/viewer/vendor/ppt/dist/render/safe.d.ts +12 -0
- package/viewer/vendor/ppt/dist/render/safe.js +48 -0
- package/viewer/vendor/ppt/dist/render/svg.d.ts +4 -0
- package/viewer/vendor/ppt/dist/render/svg.js +279 -0
- package/viewer/vendor/ppt/dist/render/text.d.ts +9 -0
- package/viewer/vendor/ppt/dist/render/text.js +181 -0
- package/viewer/vendor/ppt/dist/render/three-d.d.ts +32 -0
- package/viewer/vendor/ppt/dist/render/three-d.js +219 -0
- package/viewer/vendor/ppt/dist/render/wordart.d.ts +16 -0
- package/viewer/vendor/ppt/dist/render/wordart.js +214 -0
- package/viewer/vendor/ppt/dist/runtime.d.ts +125 -0
- package/viewer/vendor/ppt/dist/runtime.js +226 -0
- package/viewer/vendor/ppt/dist/types.d.ts +546 -0
- package/viewer/vendor/ppt/dist/types.js +1 -0
- package/viewer/vendor/ppt/dist/viewer.d.ts +3 -0
- package/viewer/vendor/ppt/dist/viewer.js +264 -0
- package/viewer/vendor/ppt/dist/worker-client.d.ts +25 -0
- package/viewer/vendor/ppt/dist/worker-client.js +61 -0
- package/viewer/vendor/ppt/dist/worker.d.ts +1 -0
- package/viewer/vendor/ppt/dist/worker.js +25 -0
- package/viewer/vendor/ppt/package.json +40 -52
- package/viewer/vendor/ppt/NOTICE +0 -229
- package/viewer/vendor/ppt/frame-cache.mjs +0 -731
- package/viewer/vendor/ppt/index.mjs +0 -941
- package/viewer/vendor/ppt/manifest.json +0 -44
- package/viewer/vendor/ppt/ppt-font-cjk.otf +0 -0
- package/viewer/vendor/ppt/ppt-native.wasm +0 -0
- package/viewer/vendor/ppt/worker.mjs +0 -1720
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { BinaryInput, ParseOptions, PptDocument } from '../types.js';
|
|
2
|
+
import { type RecordContext } from './records.js';
|
|
3
|
+
export declare const VERSION = "1.0.2";
|
|
4
|
+
export interface PersistDirectory {
|
|
5
|
+
objects: Map<number, number>;
|
|
6
|
+
documentId: number;
|
|
7
|
+
editCount: number;
|
|
8
|
+
}
|
|
9
|
+
/** Resolve newest-to-oldest save transactions; old versions never override newer IDs. [MS-PPT] 2.1.2. */
|
|
10
|
+
export declare function readPersistDirectory(bytes: Uint8Array, currentUser: Uint8Array, ctx: RecordContext): PersistDirectory;
|
|
11
|
+
/** Parse a real legacy .ppt file into a serializable model. No DOM, external URLs, native code or WASM. */
|
|
12
|
+
export declare function parsePpt(input: BinaryInput, options?: ParseOptions): PptDocument;
|
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
import { CompoundFile, toBytes } from '../core/cfb.js';
|
|
2
|
+
import { PptError } from '../core/errors.js';
|
|
3
|
+
import { trimTrailingNulls } from '../core/utils.js';
|
|
4
|
+
import { DEFAULT_LIMITS, RT } from './constants.js';
|
|
5
|
+
import { readRecord } from './records.js';
|
|
6
|
+
import { parseDefaultTextStyle, parseFonts, parseMasterTextStyles, parseTextFrames } from './text.js';
|
|
7
|
+
import { parseDrawing, readDrawingDefaults } from './drawing.js';
|
|
8
|
+
import { parsePictures } from './pictures.js';
|
|
9
|
+
import { parseDocumentExtras, parseSlideExtras, extractOleStorage } from './extras.js';
|
|
10
|
+
import { parseHeaderFooter, resolveHeaderFooter } from './headers-footers.js';
|
|
11
|
+
import { parseLegacyAnimations } from './animation.js';
|
|
12
|
+
import { decryptPptStreams } from './encryption.js';
|
|
13
|
+
import { bindShapeInteractions, resolveSlideLinks } from './interactions.js';
|
|
14
|
+
export const VERSION = '1.0.2';
|
|
15
|
+
const now = () => typeof performance === 'undefined' ? Date.now() : performance.now();
|
|
16
|
+
function dataView(bytes) { return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); }
|
|
17
|
+
/** Resolve newest-to-oldest save transactions; old versions never override newer IDs. [MS-PPT] 2.1.2. */
|
|
18
|
+
export function readPersistDirectory(bytes, currentUser, ctx) {
|
|
19
|
+
const cctx = { ...ctx, bytes: currentUser, count: 0 };
|
|
20
|
+
const current = readRecord(cctx, 0);
|
|
21
|
+
if (current.type !== RT.CurrentUserAtom || current.length < 20)
|
|
22
|
+
throw new PptError('CURRENT_USER', 'Invalid Current User stream');
|
|
23
|
+
const cv = dataView(currentUser), token = cv.getUint32(current.dataOffset + 4, true);
|
|
24
|
+
if (token === 0xf3d1c4df)
|
|
25
|
+
throw new PptError('ENCRYPTED_PPT', 'Decrypt this encrypted presentation with its supplied password before reading its persist graph');
|
|
26
|
+
if (token !== 0xe391c05f)
|
|
27
|
+
throw new PptError('CURRENT_USER', 'Unrecognized PowerPoint Current User token');
|
|
28
|
+
let edit = cv.getUint32(current.dataOffset + 8, true), documentId = 0, editCount = 0;
|
|
29
|
+
const objects = new Map(), seen = new Set();
|
|
30
|
+
const view = dataView(bytes);
|
|
31
|
+
while (edit !== 0) {
|
|
32
|
+
if (seen.has(edit))
|
|
33
|
+
throw new PptError('PERSIST_CYCLE', 'Cycle in UserEdit chain', edit);
|
|
34
|
+
seen.add(edit);
|
|
35
|
+
const user = readRecord(ctx, edit);
|
|
36
|
+
if (user.type !== RT.UserEditAtom || user.length < 28)
|
|
37
|
+
throw new PptError('USER_EDIT', 'Invalid UserEditAtom', edit);
|
|
38
|
+
const p = user.dataOffset;
|
|
39
|
+
if (user.length >= 32 && view.getUint32(p + 28, true) !== 0)
|
|
40
|
+
throw new PptError('ENCRYPTED_PPT', 'Encrypted persist objects must be decrypted before reading the graph', edit);
|
|
41
|
+
if (editCount++ === 0)
|
|
42
|
+
documentId = view.getUint32(p + 16, true);
|
|
43
|
+
const directory = readRecord(ctx, view.getUint32(p + 12, true));
|
|
44
|
+
if (directory.type !== RT.PersistDirectoryAtom && directory.type !== RT.PersistDirectoryFullBlock)
|
|
45
|
+
throw new PptError('PERSIST_DIRECTORY', 'UserEdit references an invalid persist directory', directory.offset);
|
|
46
|
+
let at = directory.dataOffset;
|
|
47
|
+
while (at < directory.end) {
|
|
48
|
+
if (at + 4 > directory.end)
|
|
49
|
+
throw new PptError('PERSIST_DIRECTORY', 'Truncated persist directory entry', at);
|
|
50
|
+
const packed = view.getUint32(at, true), start = packed & 0xfffff, count = packed >>> 20;
|
|
51
|
+
at += 4;
|
|
52
|
+
if (count === 0 || start === 0 || start + count > 0x100000 || at + count * 4 > directory.end)
|
|
53
|
+
throw new PptError('PERSIST_DIRECTORY', 'Invalid persist directory range', at - 4);
|
|
54
|
+
for (let i = 0; i < count; i++) {
|
|
55
|
+
const offset = view.getUint32(at + i * 4, true);
|
|
56
|
+
if (offset + 8 > bytes.length)
|
|
57
|
+
throw new PptError('PERSIST_OFFSET', 'Persist object offset is outside PowerPoint Document', offset);
|
|
58
|
+
if (!objects.has(start + i))
|
|
59
|
+
objects.set(start + i, offset);
|
|
60
|
+
}
|
|
61
|
+
at += count * 4;
|
|
62
|
+
}
|
|
63
|
+
edit = view.getUint32(p + 8, true);
|
|
64
|
+
}
|
|
65
|
+
if (!documentId || !objects.has(documentId))
|
|
66
|
+
throw new PptError('PERSIST_DOCUMENT', 'Current document persist object is missing');
|
|
67
|
+
return { objects, documentId, editCount };
|
|
68
|
+
}
|
|
69
|
+
function slideReferences(list, bytes) {
|
|
70
|
+
if (!list)
|
|
71
|
+
return [];
|
|
72
|
+
const v = dataView(bytes), out = [];
|
|
73
|
+
let ref;
|
|
74
|
+
for (const record of list.children) {
|
|
75
|
+
if (record.type === RT.SlidePersistAtom) {
|
|
76
|
+
if (record.length < 20)
|
|
77
|
+
throw new PptError('SLIDE_PERSIST', 'Truncated SlidePersistAtom', record.offset);
|
|
78
|
+
ref = { persistId: v.getUint32(record.dataOffset, true), id: v.getUint32(record.dataOffset + 12, true), textCount: v.getUint32(record.dataOffset + 8, true), texts: [] };
|
|
79
|
+
out.push(ref);
|
|
80
|
+
}
|
|
81
|
+
else if (ref)
|
|
82
|
+
ref.texts.push(record);
|
|
83
|
+
}
|
|
84
|
+
return out;
|
|
85
|
+
}
|
|
86
|
+
function scheme(records, bytes, fallback) {
|
|
87
|
+
const choices = records.filter(r => r.type === RT.ColorSchemeAtom && r.length >= 32);
|
|
88
|
+
const r = choices.find(r => r.instance === 1) ?? choices[0];
|
|
89
|
+
if (!r)
|
|
90
|
+
return fallback;
|
|
91
|
+
const v = dataView(bytes);
|
|
92
|
+
return Array.from({ length: 8 }, (_, i) => v.getUint32(r.dataOffset + i * 4, true) & 0xffffff);
|
|
93
|
+
}
|
|
94
|
+
function rgb(value) { return '#' + [value & 255, (value >>> 8) & 255, (value >>> 16) & 255].map(x => x.toString(16).padStart(2, '0')).join(''); }
|
|
95
|
+
function frames(shapes) { const out = []; for (const shape of shapes) {
|
|
96
|
+
if (shape.text)
|
|
97
|
+
out.push(shape.text);
|
|
98
|
+
if (shape.children)
|
|
99
|
+
out.push(...frames(shape.children));
|
|
100
|
+
} return out; }
|
|
101
|
+
function validateLimits(options) { const limits = { ...DEFAULT_LIMITS, ...options.limits }; for (const [key, value] of Object.entries(limits))
|
|
102
|
+
if (!Number.isSafeInteger(value) || value <= 0)
|
|
103
|
+
throw new PptError('INVALID_LIMIT', `Invalid parse limit ${key}`); return limits; }
|
|
104
|
+
function summary(bytes) {
|
|
105
|
+
const result = {};
|
|
106
|
+
if (!bytes || bytes.length < 48)
|
|
107
|
+
return result;
|
|
108
|
+
try {
|
|
109
|
+
const v = dataView(bytes);
|
|
110
|
+
if (v.getUint16(0, true) !== 0xfffe)
|
|
111
|
+
return result;
|
|
112
|
+
const p = v.getUint32(44, true);
|
|
113
|
+
if (p + 8 > bytes.length)
|
|
114
|
+
return result;
|
|
115
|
+
const size = v.getUint32(p, true), count = v.getUint32(p + 4, true);
|
|
116
|
+
if (p + size > bytes.length || 8 + count * 8 > size)
|
|
117
|
+
return result;
|
|
118
|
+
const map = new Map();
|
|
119
|
+
for (let i = 0; i < count; i++)
|
|
120
|
+
map.set(v.getUint32(p + 8 + i * 8, true), p + v.getUint32(p + 12 + i * 8, true));
|
|
121
|
+
let encoding = 'windows-1252';
|
|
122
|
+
const cp = map.get(1);
|
|
123
|
+
if (cp !== undefined && cp + 6 <= p + size && v.getUint32(cp, true) === 2) {
|
|
124
|
+
const code = v.getUint16(cp + 4, true);
|
|
125
|
+
encoding = code === 65001 ? 'utf-8' : code === 936 ? 'gbk' : code === 932 ? 'shift_jis' : code === 949 ? 'euc-kr' : 'windows-1252';
|
|
126
|
+
}
|
|
127
|
+
for (const [id, key] of [[2, 'title'], [3, 'subject'], [4, 'author']]) {
|
|
128
|
+
const at = map.get(id);
|
|
129
|
+
if (at === undefined || at + 8 > p + size)
|
|
130
|
+
continue;
|
|
131
|
+
const type = v.getUint32(at, true), n = v.getUint32(at + 4, true), length = type === 31 ? n * 2 : n;
|
|
132
|
+
if ((type !== 30 && type !== 31) || at + 8 + length > p + size)
|
|
133
|
+
continue;
|
|
134
|
+
result[key] = trimTrailingNulls(new TextDecoder(type === 31 ? 'utf-16le' : encoding).decode(bytes.subarray(at + 8, at + 8 + length)));
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
catch { /* Optional metadata never prevents rendering a valid slide stream. */ }
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
/** Parse a real legacy .ppt file into a serializable model. No DOM, external URLs, native code or WASM. */
|
|
141
|
+
export function parsePpt(input, options = {}) {
|
|
142
|
+
const started = now(), inputBytes = toBytes(input), limits = validateLimits(options), diagnostics = [];
|
|
143
|
+
if (inputBytes[0] === 0x50 && inputBytes[1] === 0x4b)
|
|
144
|
+
throw new PptError('UNSUPPORTED_PPTX', 'This parser handles binary .ppt files. Use a separate OOXML parser for .pptx.');
|
|
145
|
+
const cfb = new CompoundFile(inputBytes, limits, diagnostics, options.strict);
|
|
146
|
+
const decrypted = decryptPptStreams(cfb, options.password, limits, diagnostics);
|
|
147
|
+
const bytes = decrypted?.document ?? cfb.getStream('PowerPoint Document'), currentUser = decrypted?.currentUser ?? cfb.getStream('Current User');
|
|
148
|
+
if (!bytes || !currentUser)
|
|
149
|
+
throw new PptError('NOT_PPT', 'Required PowerPoint Document or Current User stream is missing');
|
|
150
|
+
const rc = { bytes, limits, diagnostics, count: 0, strict: options.strict };
|
|
151
|
+
const persist = readPersistDirectory(bytes, currentUser, rc);
|
|
152
|
+
const cache = new Map();
|
|
153
|
+
const recordCount = { get value() { return rc.count; }, set value(value) { rc.count = value; } };
|
|
154
|
+
const object = (id) => { let node = cache.get(id); if (node)
|
|
155
|
+
return node; const offset = persist.objects.get(id); if (offset === undefined)
|
|
156
|
+
throw new PptError('PERSIST_MISSING', `Persist object ${id} is missing`); node = readRecord(rc, offset); cache.set(id, node); return node; };
|
|
157
|
+
const document = object(persist.documentId);
|
|
158
|
+
if (document.type !== RT.Document)
|
|
159
|
+
throw new PptError('DOCUMENT', 'Document persist ID does not refer to a DocumentContainer');
|
|
160
|
+
const da = document.children.find(r => r.type === RT.DocumentAtom);
|
|
161
|
+
if (!da || da.length < 40)
|
|
162
|
+
throw new PptError('DOCUMENT_ATOM', 'Required DocumentAtom is missing or truncated');
|
|
163
|
+
const dv = dataView(bytes);
|
|
164
|
+
const size = { width: dv.getInt32(da.dataOffset, true) / 6, height: dv.getInt32(da.dataOffset + 4, true) / 6 };
|
|
165
|
+
const notesSize = { width: dv.getInt32(da.dataOffset + 8, true) / 6, height: dv.getInt32(da.dataOffset + 12, true) / 6 }, notesMasterPersistId = dv.getUint32(da.dataOffset + 24, true);
|
|
166
|
+
const firstSlideNumber = dv.getUint16(da.dataOffset + 32, true), omitTitlePlace = !!dv.getUint8(da.dataOffset + 37);
|
|
167
|
+
if (firstSlideNumber >= 10000)
|
|
168
|
+
diagnostics.push({ code: 'SLIDE_NUMBER_RANGE', severity: 'warning', message: 'Document first-slide number exceeds the specification range', offset: da.offset });
|
|
169
|
+
const hfContext = { bytes, limits, diagnostics }, documentHeadersFooters = parseHeaderFooter(document.children, hfContext, 3), notesHeadersFooters = parseHeaderFooter(document.children, hfContext, 4);
|
|
170
|
+
if (!(size.width > 0 && size.height > 0 && size.width <= 1e6 && size.height <= 1e6))
|
|
171
|
+
throw new PptError('SLIDE_SIZE', 'Invalid slide dimensions');
|
|
172
|
+
const lists = document.children.filter(r => r.type === RT.SlideListWithText);
|
|
173
|
+
const slideRefs = slideReferences(lists.find(r => r.instance === 0), bytes), masterRefs = slideReferences(lists.find(r => r.instance === 1), bytes), noteRefs = slideReferences(lists.find(r => r.instance === 2), bytes);
|
|
174
|
+
if (slideRefs.length > limits.maxSlides || masterRefs.length > limits.maxSlides || noteRefs.length > limits.maxSlides)
|
|
175
|
+
throw new PptError('LIMIT_SLIDES', 'Presentation exceeds the slide count limit');
|
|
176
|
+
const drawingGroup = document.children.find(r => r.type === RT.PPDrawingGroup);
|
|
177
|
+
const picturesBytes = decrypted?.pictures ?? cfb.getStream('Pictures') ?? new Uint8Array(0);
|
|
178
|
+
const pictureResult = parsePictures(drawingGroup, bytes, picturesBytes, diagnostics, limits);
|
|
179
|
+
const extras = { hyperlinks: [], sounds: [], media: [], oleObjects: [], comments: [], animationRecordTypes: [] };
|
|
180
|
+
const hyperlinkTargets = new Map(), oleStorage = new Map();
|
|
181
|
+
let assetCursor = 0, totalAssetBytes = 0;
|
|
182
|
+
const assetSize = () => { while (assetCursor < pictureResult.assets.length)
|
|
183
|
+
totalAssetBytes += pictureResult.assets[assetCursor++].bytes.length; return totalAssetBytes; };
|
|
184
|
+
const remainingLimits = () => ({ ...limits, maxAssetBytes: Math.max(0, limits.maxAssetBytes - assetSize()), maxRecords: Math.max(0, limits.maxRecords - rc.count) });
|
|
185
|
+
function mergeExtras(part) {
|
|
186
|
+
rc.count += part.additionalRecordCount;
|
|
187
|
+
if (rc.count > limits.maxRecords)
|
|
188
|
+
throw new PptError('LIMIT_RECORDS', 'Presentation exceeds the aggregate record limit');
|
|
189
|
+
pictureResult.assets.push(...part.assets);
|
|
190
|
+
if (assetSize() > limits.maxAssetBytes)
|
|
191
|
+
throw new PptError('LIMIT_ASSETS', 'Presentation exceeds the aggregate asset limit');
|
|
192
|
+
resolveSlideLinks(part.hyperlinks, slideRefs, part.hyperlinkTargets, diagnostics);
|
|
193
|
+
for (const [id, href] of part.hyperlinkTargets)
|
|
194
|
+
if (!hyperlinkTargets.has(id))
|
|
195
|
+
hyperlinkTargets.set(id, href);
|
|
196
|
+
extras.hyperlinks.push(...part.hyperlinks);
|
|
197
|
+
extras.sounds.push(...part.sounds);
|
|
198
|
+
extras.media.push(...part.media);
|
|
199
|
+
extras.comments.push(...part.comments);
|
|
200
|
+
for (const type of part.animationRecordTypes)
|
|
201
|
+
if (!extras.animationRecordTypes.includes(type))
|
|
202
|
+
extras.animationRecordTypes.push(type);
|
|
203
|
+
for (const embedded of part.oleObjects) {
|
|
204
|
+
const id = embedded.storagePersistId;
|
|
205
|
+
if (id) {
|
|
206
|
+
const known = oleStorage.get(id);
|
|
207
|
+
if (known)
|
|
208
|
+
embedded.assetId = known;
|
|
209
|
+
else if (persist.objects.has(id)) {
|
|
210
|
+
const storage = extractOleStorage(object(id), bytes, limits.maxAssetBytes - assetSize(), diagnostics);
|
|
211
|
+
if (storage) {
|
|
212
|
+
pictureResult.assets.push(storage);
|
|
213
|
+
embedded.assetId = storage.id;
|
|
214
|
+
oleStorage.set(id, storage.id);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
else
|
|
218
|
+
diagnostics.push({ code: 'OLE_STORAGE_MISSING', severity: 'warning', message: 'The embedded object storage persist ID is missing', offset: embedded.sourceOffset, details: { persistId: id } });
|
|
219
|
+
}
|
|
220
|
+
extras.oleObjects.push(embedded);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
mergeExtras(parseDocumentExtras(document.children, bytes, remainingLimits(), diagnostics));
|
|
224
|
+
const fonts = parseFonts(document.children, bytes);
|
|
225
|
+
const baseColors = [0xffffff, 0x000000, 0x808080, 0x000000, 0xffffff, 0xff0000, 0x0000ff, 0x800080];
|
|
226
|
+
const tc = { bytes, fonts, colors: baseColors, diagnostics, limits, hyperlinkTargets };
|
|
227
|
+
const environment = document.children.find(r => r.type === RT.Environment);
|
|
228
|
+
tc.defaultStyle = parseDefaultTextStyle(environment?.children ?? [], tc);
|
|
229
|
+
const defaultStyles = parseMasterTextStyles(environment?.children ?? [], tc);
|
|
230
|
+
const drawingDefaults = readDrawingDefaults(drawingGroup, bytes, diagnostics, limits, recordCount);
|
|
231
|
+
const shapeCount = { value: 0 };
|
|
232
|
+
const common = { ...tc, assets: pictureResult.assets, pictureIds: pictureResult.pictureIds, slideTexts: [], shapeCount, drawingDefaults, recordCount };
|
|
233
|
+
const masters = [];
|
|
234
|
+
const masterStyles = new Map();
|
|
235
|
+
const notes = new Map();
|
|
236
|
+
let notesMaster, notesMasterTextStyles;
|
|
237
|
+
const masterReferences = new Map(masterRefs.map((ref, index) => [ref.id, { ref, index }]));
|
|
238
|
+
const masterById = new Map(), resolvingMasters = new Set();
|
|
239
|
+
function resolveMaster(id) {
|
|
240
|
+
if (!id)
|
|
241
|
+
return undefined;
|
|
242
|
+
const cached = masterById.get(id);
|
|
243
|
+
if (cached)
|
|
244
|
+
return cached;
|
|
245
|
+
const entry = masterReferences.get(id);
|
|
246
|
+
if (!entry)
|
|
247
|
+
return undefined;
|
|
248
|
+
if (resolvingMasters.has(id))
|
|
249
|
+
throw new PptError('MASTER_CYCLE', 'Cycle in slide master inheritance');
|
|
250
|
+
if (resolvingMasters.size >= limits.maxDepth)
|
|
251
|
+
throw new PptError('LIMIT_DEPTH', 'Slide master inheritance exceeds the configured nesting limit');
|
|
252
|
+
resolvingMasters.add(id);
|
|
253
|
+
const master = parseOne(entry.ref, entry.index, true);
|
|
254
|
+
resolvingMasters.delete(id);
|
|
255
|
+
masters.push(master);
|
|
256
|
+
masterById.set(id, master);
|
|
257
|
+
return master;
|
|
258
|
+
}
|
|
259
|
+
function parseOne(ref, index, isMaster = false, isNotesMaster = false) {
|
|
260
|
+
const diagnosticStart = diagnostics.length;
|
|
261
|
+
const node = object(ref.persistId);
|
|
262
|
+
if (node.type !== RT.Slide && node.type !== RT.MainMaster && node.type !== RT.Notes)
|
|
263
|
+
throw new PptError('SLIDE_OBJECT', 'Slide list references a non-slide object', node.offset);
|
|
264
|
+
const atom = node.children.find(r => r.type === RT.SlideAtom), masterId = atom && atom.length >= 20 ? dv.getUint32(atom.dataOffset + 12, true) : 0;
|
|
265
|
+
const noteAtom = node.type === RT.Notes ? node.children.find(r => r.type === RT.NotesAtom) : undefined;
|
|
266
|
+
const flags = noteAtom && noteAtom.length >= 6 ? dv.getUint16(noteAtom.dataOffset + 4, true) : atom && atom.length >= 22 ? dv.getUint16(atom.dataOffset + 20, true) : 7;
|
|
267
|
+
const master = node.type === RT.Notes ? (isNotesMaster ? undefined : notesMaster) : resolveMaster(masterId);
|
|
268
|
+
const colors = ((flags & 2) && master?.colorScheme) ? master.colorScheme : scheme(node.children, bytes, master?.colorScheme ?? baseColors);
|
|
269
|
+
const slideExtras = parseSlideExtras(node.children, bytes, remainingLimits(), diagnostics, ref.id);
|
|
270
|
+
mergeExtras(slideExtras);
|
|
271
|
+
const localContext = { ...tc, colors, masterStyles: (node.type === RT.Notes ? notesMasterTextStyles : masterStyles.get(masterId)) ?? defaultStyles };
|
|
272
|
+
if (isMaster) {
|
|
273
|
+
const own = parseMasterTextStyles(node.children, localContext);
|
|
274
|
+
const merged = new Map(localContext.masterStyles);
|
|
275
|
+
for (const [key, value] of own)
|
|
276
|
+
merged.set(key, value);
|
|
277
|
+
localContext.masterStyles = merged;
|
|
278
|
+
if (isNotesMaster)
|
|
279
|
+
notesMasterTextStyles = merged;
|
|
280
|
+
else
|
|
281
|
+
masterStyles.set(ref.id, merged);
|
|
282
|
+
}
|
|
283
|
+
const slideTexts = parseTextFrames(ref.texts, localContext);
|
|
284
|
+
const drawing = node.children.find(r => r.type === RT.PPDrawing);
|
|
285
|
+
const shapes = drawing ? parseDrawing(drawing, { ...common, ...localContext, slideTexts, masterShapes: master?.shapes }) : [];
|
|
286
|
+
bindShapeInteractions(node.children, bytes, shapes, extras, hyperlinkTargets, diagnostics, ref.id);
|
|
287
|
+
let background = (flags & 4) && master ? master.background : { kind: 'solid', color: rgb(colors[0] ?? 0xffffff) };
|
|
288
|
+
const visibleShapes = [];
|
|
289
|
+
for (const shape of shapes) {
|
|
290
|
+
if ((shape.source?.properties?.fspFlags ?? 0) & 0x400) {
|
|
291
|
+
if (!(flags & 4) || !master)
|
|
292
|
+
background = shape.fill;
|
|
293
|
+
}
|
|
294
|
+
else
|
|
295
|
+
visibleShapes.push(shape);
|
|
296
|
+
}
|
|
297
|
+
if (!drawing && slideTexts.length)
|
|
298
|
+
diagnostics.push({ code: 'SLIDE_NO_DRAWING', severity: 'warning', message: 'Text exists but the slide has no drawing container; original positioning is unavailable.', slideId: ref.id });
|
|
299
|
+
const name = slideTexts.map(f => f.sourceText ?? f.paragraphs.flatMap(p => p.runs.map(r => r.text)).join('')).find(t => t.trim())?.slice(0, 200);
|
|
300
|
+
const layoutType = atom && atom.length >= 4 ? dv.getUint32(atom.dataOffset, true) : undefined;
|
|
301
|
+
const headersFooters = resolveHeaderFooter(node.type === RT.Notes ? notesHeadersFooters : documentHeadersFooters, master?.headersFooters, parseHeaderFooter(node.children, hfContext));
|
|
302
|
+
const slide = { id: ref.id, persistId: ref.persistId, index, name, size: node.type === RT.Notes ? notesSize : size, masterId: masterId || undefined, shapes: visibleShapes, background, showMasterShapes: !!(flags & 1), colorScheme: colors, headersFooters, displayNumber: firstSlideNumber + index, layoutType, suppressHeaderFooter: !isMaster && node.type === RT.Slide && layoutType === 0 && omitTitlePlace };
|
|
303
|
+
const hasModernTiming = slideExtras.animationRecordTypes.some(t => t >= 0xf125 && t <= 0xf145);
|
|
304
|
+
const effects = parseLegacyAnimations(node.children, bytes, shapes, diagnostics, ref.id, hasModernTiming);
|
|
305
|
+
if (effects.length)
|
|
306
|
+
slide.timeline = { effects };
|
|
307
|
+
const show = node.children.find(r => r.type === RT.SSSlideInfoAtom);
|
|
308
|
+
if (show && show.length >= 16) {
|
|
309
|
+
const p = show.dataOffset, showFlags = dv.getUint16(p + 10, true);
|
|
310
|
+
slide.transition = { type: dv.getUint8(p + 9), direction: dv.getUint8(p + 8), duration: [750, 500, 250][dv.getUint8(p + 12)], autoAdvanceAfterMs: (showFlags & 0x400) ? dv.getUint32(p, true) : undefined, manualAdvance: !!(showFlags & 1), soundId: dv.getUint32(p + 4, true), soundEnabled: !!(showFlags & 16), loopSound: !!(showFlags & 64) };
|
|
311
|
+
slide.hidden = !!(showFlags & 4);
|
|
312
|
+
}
|
|
313
|
+
if (slideExtras.animationRecordTypes.length && (hasModernTiming || diagnostics.slice(diagnosticStart).some(d => d.slideId === ref.id && d.severity === 'warning' && d.code.startsWith('ANIMATION_'))))
|
|
314
|
+
slide.unsupportedRecordTypes = slideExtras.animationRecordTypes;
|
|
315
|
+
if (atom && atom.length >= 20) {
|
|
316
|
+
const noteId = dv.getUint32(atom.dataOffset + 16, true);
|
|
317
|
+
if (noteId && notes.has(noteId))
|
|
318
|
+
slide.notes = notes.get(noteId);
|
|
319
|
+
}
|
|
320
|
+
for (let i = diagnosticStart; i < diagnostics.length; i++)
|
|
321
|
+
if (diagnostics[i].slideId == null)
|
|
322
|
+
diagnostics[i].slideId = ref.id;
|
|
323
|
+
return slide;
|
|
324
|
+
}
|
|
325
|
+
for (const ref of masterRefs)
|
|
326
|
+
resolveMaster(ref.id);
|
|
327
|
+
masters.sort((a, b) => a.index - b.index);
|
|
328
|
+
// Notes text type 2 comes from the first main master, while its palette and
|
|
329
|
+
// drawing defaults come from the separate notes-master persist object.
|
|
330
|
+
notesMasterTextStyles = masterRefs[0] ? masterStyles.get(masterRefs[0].id) : defaultStyles;
|
|
331
|
+
if (notesMasterPersistId) {
|
|
332
|
+
if (!persist.objects.has(notesMasterPersistId))
|
|
333
|
+
diagnostics.push({ code: 'NOTES_MASTER_MISSING', severity: 'warning', message: 'The notes master persist ID is missing', details: { persistId: notesMasterPersistId } });
|
|
334
|
+
else if (object(notesMasterPersistId).type !== RT.Notes)
|
|
335
|
+
diagnostics.push({ code: 'NOTES_MASTER_TYPE', severity: 'warning', message: 'The notes master persist ID does not refer to a NotesContainer', offset: object(notesMasterPersistId).offset });
|
|
336
|
+
else
|
|
337
|
+
notesMaster = parseOne({ persistId: notesMasterPersistId, id: 0, texts: [], textCount: 0 }, -1, true, true);
|
|
338
|
+
}
|
|
339
|
+
for (let i = 0; i < noteRefs.length; i++) {
|
|
340
|
+
const note = parseOne(noteRefs[i], i);
|
|
341
|
+
notes.set(note.id, frames(note.shapes));
|
|
342
|
+
}
|
|
343
|
+
const slides = slideRefs.map((ref, index) => parseOne(ref, index));
|
|
344
|
+
if (slides.length === 0)
|
|
345
|
+
diagnostics.push({ code: 'EMPTY_PRESENTATION', severity: 'info', message: 'The current document slide list is empty.' });
|
|
346
|
+
// Each asset owns exactly its bytes, avoiding accidental transfer/retention of a full compound stream.
|
|
347
|
+
let assetBytes = 0;
|
|
348
|
+
for (const asset of pictureResult.assets) {
|
|
349
|
+
if (!(asset.bytes.buffer instanceof ArrayBuffer) || asset.bytes.byteOffset !== 0 || asset.bytes.byteLength !== asset.bytes.buffer.byteLength)
|
|
350
|
+
asset.bytes = new Uint8Array(asset.bytes);
|
|
351
|
+
assetBytes += asset.bytes.length;
|
|
352
|
+
}
|
|
353
|
+
if (assetBytes > limits.maxAssetBytes)
|
|
354
|
+
throw new PptError('LIMIT_ASSETS', 'Presentation exceeds the aggregate asset limit');
|
|
355
|
+
return { format: 'ppt', version: VERSION, size, slides, masters, fonts, assets: pictureResult.assets, extras, diagnostics, metadata: { ...summary(cfb.getStream('\u0005SummaryInformation')), firstSlideNumber, omitTitlePlace, slideCount: slides.length, source: 'Microsoft PowerPoint 97–2003 binary format ([MS-PPT])' }, stats: { inputBytes: inputBytes.length, streamBytes: bytes.length + picturesBytes.length, recordCount: rc.count, shapeCount: shapeCount.value, parseTimeMs: now() - started, assetBytes } };
|
|
356
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Diagnostic, ParseLimits, PptAsset, RecordNode } from '../types.js';
|
|
2
|
+
/** DIBs omit BITMAPFILEHEADER. The pixel offset includes palette and bit masks. */
|
|
3
|
+
export declare function dibToBmp(bytes: Uint8Array): Uint8Array;
|
|
4
|
+
/** Complex pib/fillBlip values contain a complete OfficeArtBlip, not a BStore index. */
|
|
5
|
+
export declare function parseInlinePicture(bytes: Uint8Array, offset: number, length: number, id: string, diagnostics: Diagnostic[], remainingBytes: number): PptAsset;
|
|
6
|
+
/** BStore order is authoritative. Empty entries retain their one-based slot. */
|
|
7
|
+
export declare function parsePictures(drawingGroup: RecordNode | undefined, documentBytes: Uint8Array, picturesBytes: Uint8Array, diagnostics: Diagnostic[], limits: ParseLimits): {
|
|
8
|
+
assets: PptAsset[];
|
|
9
|
+
pictureIds: Map<number, string>;
|
|
10
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { PptError } from '../core/errors.js';
|
|
2
|
+
import { inflateZlib } from '../core/inflate.js';
|
|
3
|
+
import { convertMetafileToSvg } from '../core/vector.js';
|
|
4
|
+
import { pictToSvg } from '../core/pict.js';
|
|
5
|
+
import { tiffToPng } from '../core/tiff.js';
|
|
6
|
+
import { readRecords, walkRecords } from './records.js';
|
|
7
|
+
import { RT } from './constants.js';
|
|
8
|
+
function header(bytes, offset, end = bytes.length) {
|
|
9
|
+
if (!Number.isSafeInteger(offset) || offset < 0 || offset + 8 > end)
|
|
10
|
+
throw new PptError('BLIP_HEADER', 'Truncated BLIP header', offset);
|
|
11
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + offset, 8);
|
|
12
|
+
const vi = view.getUint16(0, true), length = view.getUint32(4, true);
|
|
13
|
+
if (offset + 8 + length > end)
|
|
14
|
+
throw new PptError('BLIP_LENGTH', 'BLIP exceeds its containing stream', offset);
|
|
15
|
+
return { offset, dataOffset: offset + 8, end: offset + 8 + length, length, type: view.getUint16(2, true), version: vi & 15, instance: vi >>> 4, children: [] };
|
|
16
|
+
}
|
|
17
|
+
/** DIBs omit BITMAPFILEHEADER. The pixel offset includes palette and bit masks. */
|
|
18
|
+
export function dibToBmp(bytes) {
|
|
19
|
+
if (bytes.length < 12)
|
|
20
|
+
throw new PptError('DIB_HEADER', 'Truncated DIB');
|
|
21
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
22
|
+
const size = view.getUint32(0, true);
|
|
23
|
+
if (size !== 12 && size < 40 || size > bytes.length)
|
|
24
|
+
throw new PptError('DIB_HEADER', 'Invalid DIB header size');
|
|
25
|
+
const bits = view.getUint16(size === 12 ? 10 : 14, true);
|
|
26
|
+
const compression = size >= 40 ? view.getUint32(16, true) : 0;
|
|
27
|
+
const used = size >= 40 ? view.getUint32(32, true) : 0;
|
|
28
|
+
const palette = (used || (bits <= 8 ? 2 ** bits : 0)) * (size === 12 ? 3 : 4);
|
|
29
|
+
const masks = size === 40 && (compression === 3 || compression === 6) ? (compression === 6 ? 16 : 12) : 0;
|
|
30
|
+
const pixelOffset = 14 + size + palette + masks;
|
|
31
|
+
if (pixelOffset > bytes.length + 14)
|
|
32
|
+
throw new PptError('DIB_PALETTE', 'DIB palette exceeds the image');
|
|
33
|
+
const out = new Uint8Array(bytes.length + 14), dest = new DataView(out.buffer);
|
|
34
|
+
dest.setUint16(0, 0x4D42, true);
|
|
35
|
+
dest.setUint32(2, out.length, true);
|
|
36
|
+
dest.setUint32(10, pixelOffset, true);
|
|
37
|
+
out.set(bytes, 14);
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
function pngDimensions(bytes) {
|
|
41
|
+
if (bytes.length < 24 || bytes[0] !== 137 || bytes[1] !== 80 || bytes[2] !== 78 || bytes[3] !== 71)
|
|
42
|
+
return {};
|
|
43
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
44
|
+
return { width: view.getUint32(16), height: view.getUint32(20) };
|
|
45
|
+
}
|
|
46
|
+
function decodeBlip(bytes, record, id, diagnostics, limit) {
|
|
47
|
+
const base = record.dataOffset;
|
|
48
|
+
// All supported raster and metafile record instances use the low bit for a second UID.
|
|
49
|
+
const uidBytes = (record.instance & 1) ? 32 : 16;
|
|
50
|
+
if (record.length < uidBytes)
|
|
51
|
+
throw new PptError('BLIP_UID', 'Truncated BLIP identifier', record.offset);
|
|
52
|
+
let data, mimeType, unsupported = false, width, height;
|
|
53
|
+
if (record.type >= 0xF01A && record.type <= 0xF01C) {
|
|
54
|
+
if (record.length < uidBytes + 34)
|
|
55
|
+
throw new PptError('METAFILE_HEADER', 'Truncated OfficeArt metafile header', record.offset);
|
|
56
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + base + uidBytes, 34);
|
|
57
|
+
const expected = view.getUint32(0, true), stored = view.getUint32(28, true), compression = view.getUint8(32);
|
|
58
|
+
if (expected > limit || stored > limit)
|
|
59
|
+
throw new PptError('LIMIT_ASSETS', 'Metafile exceeds the asset byte budget', record.offset);
|
|
60
|
+
if (base + uidBytes + 34 + stored > record.end)
|
|
61
|
+
throw new PptError('METAFILE_LENGTH', 'Metafile payload exceeds the BLIP', record.offset);
|
|
62
|
+
const raw = bytes.subarray(base + uidBytes + 34, base + uidBytes + 34 + stored);
|
|
63
|
+
if (compression === 0) {
|
|
64
|
+
const inflated = inflateZlib(raw, expected);
|
|
65
|
+
if (!inflated || inflated.length !== expected)
|
|
66
|
+
throw new PptError('METAFILE_DEFLATE', 'Cannot decompress OfficeArt metafile', record.offset);
|
|
67
|
+
data = inflated;
|
|
68
|
+
}
|
|
69
|
+
else if (compression === 0xFE) {
|
|
70
|
+
if (raw.byteLength !== expected)
|
|
71
|
+
throw new PptError('METAFILE_LENGTH', 'Uncompressed metafile length differs from its declared size', record.offset);
|
|
72
|
+
data = raw;
|
|
73
|
+
}
|
|
74
|
+
else
|
|
75
|
+
throw new PptError('METAFILE_COMPRESSION', `Unsupported metafile compression ${compression}`, record.offset);
|
|
76
|
+
width = Math.abs(view.getInt32(20, true)) / 9525;
|
|
77
|
+
height = Math.abs(view.getInt32(24, true)) / 9525;
|
|
78
|
+
mimeType = record.type === 0xF01A ? 'image/emf' : record.type === 0xF01B ? 'image/wmf' : 'image/x-pict';
|
|
79
|
+
if (record.type === 0xF01C) {
|
|
80
|
+
try {
|
|
81
|
+
const pict = pictToSvg(data, { maxOutputBytes: limit, maxDecodedBytes: limit });
|
|
82
|
+
data = pict.bytes;
|
|
83
|
+
mimeType = pict.mimeType;
|
|
84
|
+
width = pict.width;
|
|
85
|
+
height = pict.height;
|
|
86
|
+
if (pict.unsupportedOpcodes.length || pict.warnings.length)
|
|
87
|
+
diagnostics.push({ code: 'PICT_PARTIAL', severity: 'warning', message: 'PICT preview contains operations that require a documented approximation or are preserved without display', offset: record.offset, details: { assetId: id, opcodes: pict.unsupportedOpcodes, warnings: pict.warnings } });
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
if (error instanceof PptError && error.code.startsWith('LIMIT_'))
|
|
91
|
+
throw error;
|
|
92
|
+
unsupported = true;
|
|
93
|
+
diagnostics.push({ code: error instanceof PptError ? error.code : 'PICT_INVALID', severity: 'warning', message: error instanceof Error ? error.message : String(error), offset: record.offset, details: { assetId: id } });
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const converted = record.type === 0xF01C ? null : convertMetafileToSvg(mimeType, data, { maxOutputBytes: limit });
|
|
97
|
+
if (converted) {
|
|
98
|
+
data = converted.bytes;
|
|
99
|
+
mimeType = 'image/svg+xml';
|
|
100
|
+
width = converted.width;
|
|
101
|
+
height = converted.height;
|
|
102
|
+
if (converted.unresolvedGlyphRuns)
|
|
103
|
+
diagnostics.push({ code: 'METAFILE_GLYPHS', severity: 'warning', message: 'Metafile contains glyph-index text whose source font cannot be resolved', offset: record.offset, details: { assetId: id, runs: converted.unresolvedGlyphRuns } });
|
|
104
|
+
if (converted.unsupportedRecordTypes.length)
|
|
105
|
+
diagnostics.push({ code: 'METAFILE_PARTIAL', severity: 'warning', message: 'Metafile preview includes the supported drawing subset; some records require an unsupported or approximated operation', offset: record.offset, details: { assetId: id, sourceMime: converted.sourceMime, recordTypes: converted.unsupportedRecordTypes } });
|
|
106
|
+
}
|
|
107
|
+
else if (record.type !== 0xF01C)
|
|
108
|
+
unsupported = true;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
if (record.length < uidBytes + 1)
|
|
112
|
+
throw new PptError('BLIP_DATA', 'Truncated raster BLIP', record.offset);
|
|
113
|
+
data = bytes.subarray(base + uidBytes + 1, record.end);
|
|
114
|
+
if (data.byteLength + (record.type === 0xF01F ? 14 : 0) > limit)
|
|
115
|
+
throw new PptError('LIMIT_ASSETS', 'Raster image exceeds the asset byte budget', record.offset);
|
|
116
|
+
switch (record.type) {
|
|
117
|
+
case 0xF01D:
|
|
118
|
+
case 0xF02A:
|
|
119
|
+
mimeType = 'image/jpeg';
|
|
120
|
+
break;
|
|
121
|
+
case 0xF01E:
|
|
122
|
+
mimeType = 'image/png';
|
|
123
|
+
({ width, height } = pngDimensions(data));
|
|
124
|
+
break;
|
|
125
|
+
case 0xF01F:
|
|
126
|
+
data = dibToBmp(data);
|
|
127
|
+
mimeType = 'image/bmp';
|
|
128
|
+
break;
|
|
129
|
+
case 0xF029:
|
|
130
|
+
mimeType = 'image/tiff';
|
|
131
|
+
try {
|
|
132
|
+
const tiff = tiffToPng(data, { maxOutputBytes: limit, maxDecodedBytes: limit });
|
|
133
|
+
data = tiff.bytes;
|
|
134
|
+
mimeType = tiff.mimeType;
|
|
135
|
+
width = tiff.width;
|
|
136
|
+
height = tiff.height;
|
|
137
|
+
for (const code of tiff.warnings)
|
|
138
|
+
diagnostics.push({ code, severity: 'warning', message: code === 'TIFF_MULTIPAGE' ? 'The first TIFF image directory is displayed; remaining pages stay in the source package' : 'TIFF CMYK colors are converted without an ICC color-management engine', offset: record.offset, details: { assetId: id, pages: tiff.pageCount } });
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
if (error instanceof PptError && error.code.startsWith('LIMIT_'))
|
|
142
|
+
throw error;
|
|
143
|
+
unsupported = true;
|
|
144
|
+
diagnostics.push({ code: error instanceof PptError ? error.code : 'TIFF_INVALID', severity: 'warning', message: error instanceof Error ? error.message : String(error), offset: record.offset, details: { assetId: id } });
|
|
145
|
+
}
|
|
146
|
+
break;
|
|
147
|
+
default:
|
|
148
|
+
mimeType = 'application/octet-stream';
|
|
149
|
+
unsupported = true;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (data.byteLength > limit)
|
|
153
|
+
throw new PptError('LIMIT_ASSETS', 'Decoded image exceeds the asset byte budget', record.offset);
|
|
154
|
+
if (unsupported)
|
|
155
|
+
diagnostics.push({ code: 'IMAGE_FORMAT_UNSUPPORTED', severity: 'warning', message: `Image ${mimeType} is preserved but not browser-renderable`, offset: record.offset, details: { assetId: id } });
|
|
156
|
+
return { id, mimeType, bytes: data, width, height, sourceOffset: record.offset, unsupported };
|
|
157
|
+
}
|
|
158
|
+
/** Complex pib/fillBlip values contain a complete OfficeArtBlip, not a BStore index. */
|
|
159
|
+
export function parseInlinePicture(bytes, offset, length, id, diagnostics, remainingBytes) {
|
|
160
|
+
return decodeBlip(bytes, header(bytes, offset, offset + length), id, diagnostics, remainingBytes);
|
|
161
|
+
}
|
|
162
|
+
/** BStore order is authoritative. Empty entries retain their one-based slot. */
|
|
163
|
+
export function parsePictures(drawingGroup, documentBytes, picturesBytes, diagnostics, limits) {
|
|
164
|
+
const assets = [], pictureIds = new Map();
|
|
165
|
+
if (!drawingGroup)
|
|
166
|
+
return { assets, pictureIds };
|
|
167
|
+
const ctx = { bytes: documentBytes, diagnostics, limits, count: 0 };
|
|
168
|
+
const roots = drawingGroup.children.length ? drawingGroup.children : readRecords(ctx, drawingGroup.dataOffset, drawingGroup.end);
|
|
169
|
+
const store = [...walkRecords(roots)].find(r => r.type === RT.OfficeArtBStoreContainer);
|
|
170
|
+
if (!store)
|
|
171
|
+
return { assets, pictureIds };
|
|
172
|
+
const seen = new Map();
|
|
173
|
+
let budget = limits.maxAssetBytes;
|
|
174
|
+
for (let i = 0; i < store.children.length; i++) {
|
|
175
|
+
const entry = store.children[i];
|
|
176
|
+
let record = entry, bytes = documentBytes, key = `document:${entry.offset}`;
|
|
177
|
+
try {
|
|
178
|
+
if (entry.type === RT.OfficeArtFBSE) {
|
|
179
|
+
if (entry.length < 36)
|
|
180
|
+
throw new PptError('FBSE_HEADER', 'Truncated File BLIP Store Entry', entry.offset);
|
|
181
|
+
const view = new DataView(documentBytes.buffer, documentBytes.byteOffset + entry.dataOffset, entry.length);
|
|
182
|
+
if (!view.getUint32(24, true))
|
|
183
|
+
continue;
|
|
184
|
+
const embedded = entry.dataOffset + 36 + view.getUint8(33), delayed = view.getUint32(28, true);
|
|
185
|
+
if (embedded + 8 <= entry.end)
|
|
186
|
+
record = header(documentBytes, embedded, entry.end);
|
|
187
|
+
else if (delayed !== 0xFFFFFFFF) {
|
|
188
|
+
bytes = picturesBytes;
|
|
189
|
+
record = header(bytes, delayed);
|
|
190
|
+
key = `pictures:${delayed}`;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
diagnostics.push({ code: 'BLIP_MISSING', severity: 'warning', message: 'Referenced image has no embedded or delayed BLIP', offset: entry.offset });
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const duplicate = seen.get(key);
|
|
198
|
+
if (duplicate) {
|
|
199
|
+
pictureIds.set(i + 1, duplicate);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
const id = `image-${i + 1}`, asset = decodeBlip(bytes, record, id, diagnostics, budget);
|
|
203
|
+
budget -= asset.bytes.length;
|
|
204
|
+
assets.push(asset);
|
|
205
|
+
pictureIds.set(i + 1, id);
|
|
206
|
+
seen.set(key, id);
|
|
207
|
+
}
|
|
208
|
+
catch (error) {
|
|
209
|
+
if (error instanceof PptError && error.code === 'LIMIT_ASSETS')
|
|
210
|
+
throw error;
|
|
211
|
+
diagnostics.push({ code: error instanceof PptError ? error.code : 'BLIP_INVALID', severity: 'warning', message: error instanceof Error ? error.message : String(error), offset: entry.offset, details: { pictureIndex: i + 1 } });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return { assets, pictureIds };
|
|
215
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Diagnostic, ParseLimits, RecordNode } from '../types.js';
|
|
2
|
+
export interface RecordContext {
|
|
3
|
+
bytes: Uint8Array;
|
|
4
|
+
limits: ParseLimits;
|
|
5
|
+
diagnostics: Diagnostic[];
|
|
6
|
+
count: number;
|
|
7
|
+
strict?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function readRecord(ctx: RecordContext, offset: number, end?: number, depth?: number): RecordNode;
|
|
10
|
+
export declare function readRecords(ctx: RecordContext, start: number, end: number, depth?: number): RecordNode[];
|
|
11
|
+
export declare function walkRecords(records: readonly RecordNode[]): Generator<RecordNode>;
|
|
12
|
+
export declare function findRecord(records: readonly RecordNode[], type: number): RecordNode | undefined;
|
|
13
|
+
export declare function childrenOf(record: RecordNode, type: number): RecordNode[];
|