@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.
- 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,789 @@
|
|
|
1
|
+
import { PptError } from '../core/errors.js';
|
|
2
|
+
import { interactionHref } from './interactions.js';
|
|
3
|
+
import { RT } from './constants.js';
|
|
4
|
+
import { walkRecords } from './records.js';
|
|
5
|
+
/** Record-local reader: a malformed optional field can never consume another record. */
|
|
6
|
+
class Cursor {
|
|
7
|
+
view;
|
|
8
|
+
at;
|
|
9
|
+
end;
|
|
10
|
+
constructor(bytes, record) {
|
|
11
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
12
|
+
this.at = record.dataOffset;
|
|
13
|
+
this.end = record.end;
|
|
14
|
+
if (this.at < 0 || this.end > bytes.length || this.at > this.end)
|
|
15
|
+
throw new PptError('TEXT_BOUNDS', 'Text record lies outside its stream', record.offset);
|
|
16
|
+
}
|
|
17
|
+
need(size) {
|
|
18
|
+
if (!Number.isSafeInteger(size) || size < 0 || this.at + size > this.end)
|
|
19
|
+
throw new PptError('TEXT_TRUNCATED', 'Truncated text formatting field', this.at);
|
|
20
|
+
}
|
|
21
|
+
u16() { this.need(2); const n = this.view.getUint16(this.at, true); this.at += 2; return n; }
|
|
22
|
+
i16() { this.need(2); const n = this.view.getInt16(this.at, true); this.at += 2; return n; }
|
|
23
|
+
u32() { this.need(4); const n = this.view.getUint32(this.at, true); this.at += 4; return n; }
|
|
24
|
+
}
|
|
25
|
+
function warn(ctx, code, message, offset) {
|
|
26
|
+
ctx.diagnostics.push({ code, severity: 'warning', message, offset });
|
|
27
|
+
}
|
|
28
|
+
function failure(ctx, error, record) {
|
|
29
|
+
if (error instanceof PptError && error.code.startsWith('LIMIT_'))
|
|
30
|
+
throw error;
|
|
31
|
+
warn(ctx, error instanceof PptError ? error.code : 'TEXT_FORMAT', error instanceof Error ? error.message : String(error), error instanceof PptError ? error.offset : record.offset);
|
|
32
|
+
}
|
|
33
|
+
function rgb(value) {
|
|
34
|
+
return '#' + [value & 255, (value >>> 8) & 255, (value >>> 16) & 255].map(n => n.toString(16).padStart(2, '0')).join('');
|
|
35
|
+
}
|
|
36
|
+
/** Text ColorIndexStruct differs from OfficeArtCOLORREF: high byte 0xfe means RGB. */
|
|
37
|
+
function textColor(value, ctx) {
|
|
38
|
+
const index = value >>> 24;
|
|
39
|
+
if (index <= 7)
|
|
40
|
+
return { colorIndex: index, color: rgb(ctx.colors[index] ?? (index === 0 ? 0xffffff : 0)) };
|
|
41
|
+
if (index === 0xfe)
|
|
42
|
+
return { color: rgb(value) };
|
|
43
|
+
if (index !== 0xff)
|
|
44
|
+
warn(ctx, 'TEXT_COLOR_INDEX', `Invalid text color scheme index ${index}; using stored RGB components`);
|
|
45
|
+
return index === 0xff ? {} : { color: rgb(value) };
|
|
46
|
+
}
|
|
47
|
+
function fontName(id, ctx) {
|
|
48
|
+
return ctx.fonts.find(f => f.id === id)?.name;
|
|
49
|
+
}
|
|
50
|
+
function mergeCharacter(base, own) {
|
|
51
|
+
const merged = { ...base, ...Object.fromEntries(Object.entries(own).filter(([, value]) => value !== undefined)) };
|
|
52
|
+
if (own.color !== undefined && own.colorIndex === undefined)
|
|
53
|
+
delete merged.colorIndex;
|
|
54
|
+
return merged;
|
|
55
|
+
}
|
|
56
|
+
function mergeParagraph(base, own) {
|
|
57
|
+
const merged = { ...base, ...own };
|
|
58
|
+
// PFWrapFlags is a bitfield with three independent PFMasks selectors.
|
|
59
|
+
// Unselected bits in its stored word are not overrides of master properties.
|
|
60
|
+
if (own.wrapFlags !== undefined) {
|
|
61
|
+
const mask = (own.wrapFlagsMask ?? 7) & 7;
|
|
62
|
+
merged.wrapFlags = ((base.wrapFlags ?? 0) & ~mask) | (own.wrapFlags & mask);
|
|
63
|
+
merged.wrapFlagsMask = ((base.wrapFlagsMask ?? (base.wrapFlags === undefined ? 0 : 7)) | mask) & 7;
|
|
64
|
+
}
|
|
65
|
+
if (base.bullet || own.bullet) {
|
|
66
|
+
merged.bullet = { ...base.bullet, ...own.bullet, text: own.bullet?.text || base.bullet?.text || '•' };
|
|
67
|
+
if (own.bullet?.color !== undefined && own.bullet.colorIndex === undefined)
|
|
68
|
+
delete merged.bullet.colorIndex;
|
|
69
|
+
if (own.bullet?.size !== undefined)
|
|
70
|
+
delete merged.bullet.sizePercent;
|
|
71
|
+
if (own.bullet?.sizePercent !== undefined)
|
|
72
|
+
delete merged.bullet.size;
|
|
73
|
+
}
|
|
74
|
+
if (own.lineHeight !== undefined)
|
|
75
|
+
delete merged.lineHeightPoints;
|
|
76
|
+
if (own.lineHeightPoints !== undefined)
|
|
77
|
+
delete merged.lineHeight;
|
|
78
|
+
if (own.spaceBefore !== undefined)
|
|
79
|
+
delete merged.spaceBeforePercent;
|
|
80
|
+
if (own.spaceBeforePercent !== undefined)
|
|
81
|
+
delete merged.spaceBefore;
|
|
82
|
+
if (own.spaceAfter !== undefined)
|
|
83
|
+
delete merged.spaceAfterPercent;
|
|
84
|
+
if (own.spaceAfterPercent !== undefined)
|
|
85
|
+
delete merged.spaceAfter;
|
|
86
|
+
return merged;
|
|
87
|
+
}
|
|
88
|
+
/** Font-style flags only override attributes selected by their mask. */
|
|
89
|
+
function readCF(c, ctx) {
|
|
90
|
+
const maskOffset = c.at;
|
|
91
|
+
const mask = c.u32();
|
|
92
|
+
// The extension bits describe different record structures, not extra base fields.
|
|
93
|
+
if (mask & 0x07100000)
|
|
94
|
+
throw new PptError('TEXT_CF_EXTENSION', `Unsupported extension bits in base character formatting: 0x${mask.toString(16)}`, maskOffset);
|
|
95
|
+
// Unused/reserved mask bits have no payload. MS-PPT requires ignoring them;
|
|
96
|
+
// Windows writers commonly set the unused low bits in a full default mask.
|
|
97
|
+
const out = {};
|
|
98
|
+
if (mask & 0x3eb7) {
|
|
99
|
+
const flags = c.u16();
|
|
100
|
+
if (mask & 1)
|
|
101
|
+
out.bold = !!(flags & 1);
|
|
102
|
+
if (mask & 2)
|
|
103
|
+
out.italic = !!(flags & 2);
|
|
104
|
+
if (mask & 4)
|
|
105
|
+
out.underline = !!(flags & 4);
|
|
106
|
+
if (mask & 0x10)
|
|
107
|
+
out.shadow = !!(flags & 0x10);
|
|
108
|
+
if (mask & 0x200)
|
|
109
|
+
out.emboss = !!(flags & 0x200);
|
|
110
|
+
}
|
|
111
|
+
if (mask & 0x10000) {
|
|
112
|
+
out.fontId = c.u16();
|
|
113
|
+
out.fontFamily = fontName(out.fontId, ctx);
|
|
114
|
+
}
|
|
115
|
+
if (mask & 0x200000) {
|
|
116
|
+
out.eastAsianFontId = c.u16();
|
|
117
|
+
out.eastAsianFontFamily = fontName(out.eastAsianFontId, ctx);
|
|
118
|
+
}
|
|
119
|
+
if (mask & 0x400000) {
|
|
120
|
+
out.ansiFontId = c.u16();
|
|
121
|
+
out.ansiFontFamily = fontName(out.ansiFontId, ctx);
|
|
122
|
+
}
|
|
123
|
+
if (mask & 0x800000) {
|
|
124
|
+
out.symbolFontId = c.u16();
|
|
125
|
+
out.symbolFontFamily = fontName(out.symbolFontId, ctx);
|
|
126
|
+
}
|
|
127
|
+
if (mask & 0x20000) {
|
|
128
|
+
const size = c.i16();
|
|
129
|
+
if (size < 1 || size > 4000)
|
|
130
|
+
warn(ctx, 'TEXT_FONT_SIZE', `Invalid font size ${size} points`, c.at - 2);
|
|
131
|
+
else
|
|
132
|
+
out.fontSize = size * 4 / 3;
|
|
133
|
+
}
|
|
134
|
+
if (mask & 0x40000)
|
|
135
|
+
Object.assign(out, textColor(c.u32(), ctx));
|
|
136
|
+
if (mask & 0x80000)
|
|
137
|
+
out.baseline = c.i16();
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
function readTabs(c, ctx) {
|
|
141
|
+
const count = c.u16();
|
|
142
|
+
c.need(count * 4);
|
|
143
|
+
const tabs = [];
|
|
144
|
+
for (let i = 0; i < count; i++) {
|
|
145
|
+
const position = c.i16() / 6;
|
|
146
|
+
const kind = c.u16();
|
|
147
|
+
if (kind > 3)
|
|
148
|
+
warn(ctx, 'TEXT_TAB_ALIGNMENT', `Unknown tab alignment ${kind}`, c.at - 2);
|
|
149
|
+
tabs.push({ position, alignment: ['left', 'center', 'right', 'decimal'][kind] ?? 'left' });
|
|
150
|
+
}
|
|
151
|
+
return tabs;
|
|
152
|
+
}
|
|
153
|
+
function spacing(out, property, value) {
|
|
154
|
+
if (property === 'lineHeight') {
|
|
155
|
+
if (value < 0)
|
|
156
|
+
out.lineHeightPoints = -value / 6;
|
|
157
|
+
else
|
|
158
|
+
out.lineHeight = value / 100;
|
|
159
|
+
}
|
|
160
|
+
else if (property === 'spaceBefore') {
|
|
161
|
+
if (value < 0)
|
|
162
|
+
out.spaceBefore = -value / 6;
|
|
163
|
+
else
|
|
164
|
+
out.spaceBeforePercent = value / 100;
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
if (value < 0)
|
|
168
|
+
out.spaceAfter = -value / 6;
|
|
169
|
+
else
|
|
170
|
+
out.spaceAfterPercent = value / 100;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
function readPF(c, ctx) {
|
|
174
|
+
const maskOffset = c.at;
|
|
175
|
+
const mask = c.u32();
|
|
176
|
+
if (mask & 0x03800000)
|
|
177
|
+
throw new PptError('TEXT_PF_EXTENSION', `Unsupported extension bits in base paragraph formatting: 0x${mask.toString(16)}`, maskOffset);
|
|
178
|
+
// Ignore unused/reserved mask bits; only defined fields consume bytes.
|
|
179
|
+
const out = {};
|
|
180
|
+
const bullet = { text: '' };
|
|
181
|
+
if (mask & 15) {
|
|
182
|
+
const flags = c.u16();
|
|
183
|
+
if (mask & 1)
|
|
184
|
+
out.bulletEnabled = !!(flags & 1);
|
|
185
|
+
if (mask & 2)
|
|
186
|
+
bullet.useFont = !!(flags & 2);
|
|
187
|
+
if (mask & 4)
|
|
188
|
+
bullet.useColor = !!(flags & 4);
|
|
189
|
+
if (mask & 8)
|
|
190
|
+
bullet.useSize = !!(flags & 8);
|
|
191
|
+
}
|
|
192
|
+
if (mask & 0x80)
|
|
193
|
+
bullet.text = String.fromCharCode(c.u16());
|
|
194
|
+
if (mask & 0x10) {
|
|
195
|
+
bullet.fontId = c.u16();
|
|
196
|
+
bullet.fontFamily = fontName(bullet.fontId, ctx);
|
|
197
|
+
}
|
|
198
|
+
if (mask & 0x40) {
|
|
199
|
+
const size = c.i16();
|
|
200
|
+
if (size < 0)
|
|
201
|
+
bullet.size = -size * 4 / 3;
|
|
202
|
+
else
|
|
203
|
+
bullet.sizePercent = size / 100;
|
|
204
|
+
}
|
|
205
|
+
if (mask & 0x20)
|
|
206
|
+
Object.assign(bullet, textColor(c.u32(), ctx));
|
|
207
|
+
if (mask & 0xff)
|
|
208
|
+
out.bullet = bullet;
|
|
209
|
+
if (mask & 0x800) {
|
|
210
|
+
const align = c.u16();
|
|
211
|
+
out.alignment = ['left', 'center', 'right', 'justify', 'distributed', 'justify', 'justify'][align] ?? 'left';
|
|
212
|
+
if (align === 5 || align === 6)
|
|
213
|
+
warn(ctx, 'TEXT_DISTRIBUTED_ALIGNMENT', `Text alignment ${align} is approximated as justified`, c.at - 2);
|
|
214
|
+
else if (align > 6)
|
|
215
|
+
warn(ctx, 'TEXT_ALIGNMENT', `Unknown text alignment ${align}; using left alignment`, c.at - 2);
|
|
216
|
+
}
|
|
217
|
+
if (mask & 0x1000)
|
|
218
|
+
spacing(out, 'lineHeight', c.i16());
|
|
219
|
+
if (mask & 0x2000)
|
|
220
|
+
spacing(out, 'spaceBefore', c.i16());
|
|
221
|
+
if (mask & 0x4000)
|
|
222
|
+
spacing(out, 'spaceAfter', c.i16());
|
|
223
|
+
if (mask & 0x100)
|
|
224
|
+
out.marginLeft = c.i16() / 6;
|
|
225
|
+
if (mask & 0x400)
|
|
226
|
+
out.firstLinePosition = c.i16() / 6;
|
|
227
|
+
if (mask & 0x8000)
|
|
228
|
+
out.defaultTabSize = c.i16() / 6;
|
|
229
|
+
if (mask & 0x100000) {
|
|
230
|
+
out.tabStops = readTabs(c, ctx);
|
|
231
|
+
// [MS-PPT] TabStop: a TextPFException containing leftMargin makes its
|
|
232
|
+
// positions relative to that paragraph margin; TextRuler uses text origin.
|
|
233
|
+
if (mask & 0x100)
|
|
234
|
+
for (const stop of out.tabStops)
|
|
235
|
+
stop.origin = 'paragraph';
|
|
236
|
+
}
|
|
237
|
+
if (mask & 0x10000) {
|
|
238
|
+
out.fontAlignment = c.u16();
|
|
239
|
+
if (out.fontAlignment > 3)
|
|
240
|
+
warn(ctx, 'TEXT_FONT_ALIGNMENT', `Unknown font alignment ${out.fontAlignment}; using the font baseline`, c.at - 2);
|
|
241
|
+
}
|
|
242
|
+
if (mask & 0xe0000) {
|
|
243
|
+
out.wrapFlags = c.u16() & 7;
|
|
244
|
+
out.wrapFlagsMask = (mask >>> 17) & 7;
|
|
245
|
+
}
|
|
246
|
+
if (mask & 0x200000)
|
|
247
|
+
out.direction = c.u16() === 1 ? 'rtl' : 'ltr';
|
|
248
|
+
return out;
|
|
249
|
+
}
|
|
250
|
+
function readRuler(record, ctx) {
|
|
251
|
+
const c = new Cursor(ctx.bytes, record);
|
|
252
|
+
const flags = c.u32();
|
|
253
|
+
const result = { common: {}, levels: Array.from({ length: 5 }, () => ({})) };
|
|
254
|
+
if (flags & ~0x1fff)
|
|
255
|
+
warn(ctx, 'TEXT_RULER_RESERVED', 'Reserved text-ruler bits are set', record.offset);
|
|
256
|
+
if (flags & 2) {
|
|
257
|
+
const levels = c.i16();
|
|
258
|
+
if (levels < 0 || levels > 5)
|
|
259
|
+
warn(ctx, 'TEXT_RULER_LEVELS', `Invalid text-ruler level count ${levels}`, c.at - 2);
|
|
260
|
+
}
|
|
261
|
+
if (flags & 1)
|
|
262
|
+
result.common.defaultTabSize = c.i16() / 6;
|
|
263
|
+
if (flags & 4)
|
|
264
|
+
result.common.tabStops = readTabs(c, ctx);
|
|
265
|
+
for (let level = 0; level < 5; level++) {
|
|
266
|
+
if (flags & (1 << (3 + level)))
|
|
267
|
+
result.levels[level].marginLeft = c.i16() / 6;
|
|
268
|
+
if (flags & (1 << (8 + level)))
|
|
269
|
+
result.levels[level].firstLinePosition = c.i16() / 6;
|
|
270
|
+
}
|
|
271
|
+
return result;
|
|
272
|
+
}
|
|
273
|
+
function decodeChars(record, ctx) {
|
|
274
|
+
if (record.length / 2 > ctx.limits.maxTextLength)
|
|
275
|
+
throw new PptError('LIMIT_TEXT', 'Text body exceeds the configured character limit', record.offset);
|
|
276
|
+
if (record.length % 2 !== 0)
|
|
277
|
+
warn(ctx, 'TEXT_ODD_UTF16', 'An odd trailing byte in UTF-16 text was ignored', record.end - 1);
|
|
278
|
+
const count = Math.floor(record.length / 2);
|
|
279
|
+
const v = new DataView(ctx.bytes.buffer, ctx.bytes.byteOffset, ctx.bytes.byteLength);
|
|
280
|
+
const parts = [];
|
|
281
|
+
for (let i = 0; i < count; i += 8192) {
|
|
282
|
+
const chars = [];
|
|
283
|
+
for (let j = i; j < Math.min(count, i + 8192); j++)
|
|
284
|
+
chars.push(v.getUint16(record.dataOffset + j * 2, true));
|
|
285
|
+
parts.push(String.fromCharCode(...chars));
|
|
286
|
+
}
|
|
287
|
+
return parts.join('');
|
|
288
|
+
}
|
|
289
|
+
function decodeBytes(record, ctx) {
|
|
290
|
+
if (record.length > ctx.limits.maxTextLength)
|
|
291
|
+
throw new PptError('LIMIT_TEXT', 'Text body exceeds the configured character limit', record.offset);
|
|
292
|
+
// Do not use TextDecoder("latin1"): browsers map that alias to Windows-1252.
|
|
293
|
+
const data = ctx.bytes.subarray(record.dataOffset, record.end);
|
|
294
|
+
const parts = [];
|
|
295
|
+
for (let i = 0; i < data.length; i += 8192)
|
|
296
|
+
parts.push(String.fromCharCode(...data.subarray(i, i + 8192)));
|
|
297
|
+
return parts.join('');
|
|
298
|
+
}
|
|
299
|
+
function checkCount(count, covered, length, offset) {
|
|
300
|
+
if (count === 0 || count > length - covered)
|
|
301
|
+
throw new PptError('TEXT_RUN_COVERAGE', `Invalid text run length ${count}; ${length - covered} UTF-16 code units remain`, offset);
|
|
302
|
+
}
|
|
303
|
+
function parseRuns(record, length, ctx) {
|
|
304
|
+
const c = new Cursor(ctx.bytes, record);
|
|
305
|
+
const paragraphs = [], characters = [];
|
|
306
|
+
try {
|
|
307
|
+
let count = 0;
|
|
308
|
+
while (count < length) {
|
|
309
|
+
if (paragraphs.length >= ctx.limits.maxRecords)
|
|
310
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many paragraph formatting runs', c.at);
|
|
311
|
+
const start = count, size = c.u32();
|
|
312
|
+
checkCount(size, count, length, c.at - 4);
|
|
313
|
+
const rawLevel = c.u16();
|
|
314
|
+
if (rawLevel > 4)
|
|
315
|
+
warn(ctx, 'TEXT_INDENT_LEVEL', `Paragraph indent level ${rawLevel} exceeds 4`, c.at - 2);
|
|
316
|
+
const style = readPF(c, ctx);
|
|
317
|
+
count += size;
|
|
318
|
+
paragraphs.push({ start, end: count, level: Math.min(4, rawLevel), style });
|
|
319
|
+
}
|
|
320
|
+
count = 0;
|
|
321
|
+
while (count < length) {
|
|
322
|
+
if (characters.length >= ctx.limits.maxRecords)
|
|
323
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many character formatting runs', c.at);
|
|
324
|
+
const start = count, size = c.u32();
|
|
325
|
+
checkCount(size, count, length, c.at - 4);
|
|
326
|
+
const style = readCF(c, ctx);
|
|
327
|
+
count += size;
|
|
328
|
+
characters.push({ start, end: count, style });
|
|
329
|
+
}
|
|
330
|
+
if (c.at !== c.end)
|
|
331
|
+
warn(ctx, 'TEXT_STYLE_TAIL', `${c.end - c.at} unused bytes in text formatting`, c.at);
|
|
332
|
+
}
|
|
333
|
+
catch (error) {
|
|
334
|
+
failure(ctx, error, record);
|
|
335
|
+
}
|
|
336
|
+
return { paragraphs, characters };
|
|
337
|
+
}
|
|
338
|
+
/** [MS-PPT] TextSIException: unused mask bits do not add payload fields. */
|
|
339
|
+
function readSI(c, ctx) {
|
|
340
|
+
const mask = c.u32(), style = {};
|
|
341
|
+
if (mask & 1)
|
|
342
|
+
c.u16(); // Spelling status does not change displayed characters.
|
|
343
|
+
if (mask & 2)
|
|
344
|
+
style.language = c.u16();
|
|
345
|
+
if (mask & 4)
|
|
346
|
+
style.alternateLanguage = c.u16();
|
|
347
|
+
if (mask & 0x40)
|
|
348
|
+
c.u16(); // Contains bidi characters, not a paragraph direction.
|
|
349
|
+
if (mask & 0x20)
|
|
350
|
+
c.u32(); // PP10 run identifier and grammar status.
|
|
351
|
+
if (mask & 0x200) {
|
|
352
|
+
const count = c.u32();
|
|
353
|
+
if (count > ctx.limits.maxRecords)
|
|
354
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many smart-tag indices', c.at - 4);
|
|
355
|
+
c.need(count * 4);
|
|
356
|
+
c.at += count * 4;
|
|
357
|
+
}
|
|
358
|
+
for (const language of [style.language, style.alternateLanguage]) {
|
|
359
|
+
if (language !== undefined && language !== 0 && language !== 0x13 && language < 0x400)
|
|
360
|
+
warn(ctx, 'TEXT_LANGUAGE', `Invalid text language identifier 0x${language.toString(16)}`, c.at);
|
|
361
|
+
}
|
|
362
|
+
return style;
|
|
363
|
+
}
|
|
364
|
+
function parseSpecialInfo(record, length, ctx) {
|
|
365
|
+
const c = new Cursor(ctx.bytes, record), ranges = [];
|
|
366
|
+
let covered = 0;
|
|
367
|
+
try {
|
|
368
|
+
while (covered < length) {
|
|
369
|
+
if (ranges.length >= ctx.limits.maxRecords)
|
|
370
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many text language runs', c.at);
|
|
371
|
+
const start = covered, count = c.u32();
|
|
372
|
+
checkCount(count, covered, length, c.at - 4);
|
|
373
|
+
const style = readSI(c, ctx);
|
|
374
|
+
covered += count;
|
|
375
|
+
ranges.push({ start, end: covered, style });
|
|
376
|
+
}
|
|
377
|
+
if (c.at !== c.end)
|
|
378
|
+
warn(ctx, 'TEXT_SI_TAIL', `${c.end - c.at} unused bytes in text language properties`, c.at);
|
|
379
|
+
}
|
|
380
|
+
catch (error) {
|
|
381
|
+
failure(ctx, error, record);
|
|
382
|
+
}
|
|
383
|
+
return ranges;
|
|
384
|
+
}
|
|
385
|
+
/** Microsoft PPT 97–2007 specification, TxMasterStyleAtom: these are deltas. */
|
|
386
|
+
function baseTextType(textType) {
|
|
387
|
+
return textType === 6 ? 0 : textType === 5 || textType === 7 || textType === 8 ? 1 : undefined;
|
|
388
|
+
}
|
|
389
|
+
function masterLevel(ctx, textType, level) {
|
|
390
|
+
const defaultLevels = ctx.masterStyles?.get(4);
|
|
391
|
+
const defaults = defaultLevels?.[level] ?? defaultLevels?.[0];
|
|
392
|
+
const baseType = baseTextType(textType);
|
|
393
|
+
const baseLevels = baseType === undefined ? undefined : ctx.masterStyles?.get(baseType);
|
|
394
|
+
const base = baseLevels?.[level] ?? baseLevels?.[0];
|
|
395
|
+
const levels = ctx.masterStyles?.get(textType);
|
|
396
|
+
const selected = levels?.[level] ?? levels?.[0];
|
|
397
|
+
return {
|
|
398
|
+
paragraph: mergeParagraph(mergeParagraph(mergeParagraph({ bulletEnabled: false }, defaults?.paragraph ?? {}), base?.paragraph ?? {}), selected?.paragraph ?? {}),
|
|
399
|
+
character: mergeCharacter(mergeCharacter({ fontFamily: ctx.fonts[0]?.name ?? 'Arial', fontSize: 24, color: '#000000' }, ctx.defaultStyle ?? {}), mergeCharacter(mergeCharacter(defaults?.character ?? {}, base?.character ?? {}), selected?.character ?? {})),
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function resolveCharacter(style, ctx) {
|
|
403
|
+
const s = { ...style };
|
|
404
|
+
if (s.fontId !== undefined)
|
|
405
|
+
s.fontFamily = fontName(s.fontId, ctx) ?? s.fontFamily;
|
|
406
|
+
if (s.eastAsianFontId !== undefined)
|
|
407
|
+
s.eastAsianFontFamily = fontName(s.eastAsianFontId, ctx) ?? s.eastAsianFontFamily;
|
|
408
|
+
if (s.ansiFontId !== undefined)
|
|
409
|
+
s.ansiFontFamily = fontName(s.ansiFontId, ctx) ?? s.ansiFontFamily;
|
|
410
|
+
if (s.symbolFontId !== undefined)
|
|
411
|
+
s.symbolFontFamily = fontName(s.symbolFontId, ctx) ?? s.symbolFontFamily;
|
|
412
|
+
if (s.colorIndex !== undefined)
|
|
413
|
+
s.color = rgb(ctx.colors[s.colorIndex] ?? 0);
|
|
414
|
+
return s;
|
|
415
|
+
}
|
|
416
|
+
function resolveParagraph(style, firstStyle, ctx) {
|
|
417
|
+
const s = mergeParagraph({}, style);
|
|
418
|
+
if (s.firstLinePosition !== undefined)
|
|
419
|
+
s.indent = s.firstLinePosition - (s.marginLeft ?? 0);
|
|
420
|
+
if (s.bulletEnabled === false || (s.bulletEnabled === undefined && !s.bullet?.text))
|
|
421
|
+
delete s.bullet;
|
|
422
|
+
else if (s.bullet) {
|
|
423
|
+
const bullet = s.bullet;
|
|
424
|
+
bullet.text ||= '•';
|
|
425
|
+
if (bullet.useFont === false) {
|
|
426
|
+
delete bullet.fontId;
|
|
427
|
+
delete bullet.fontFamily;
|
|
428
|
+
}
|
|
429
|
+
else if (bullet.fontId !== undefined)
|
|
430
|
+
bullet.fontFamily = fontName(bullet.fontId, ctx) ?? bullet.fontFamily;
|
|
431
|
+
if (bullet.useColor === false) {
|
|
432
|
+
delete bullet.color;
|
|
433
|
+
delete bullet.colorIndex;
|
|
434
|
+
}
|
|
435
|
+
else if (bullet.colorIndex !== undefined)
|
|
436
|
+
bullet.color = rgb(ctx.colors[bullet.colorIndex] ?? 0);
|
|
437
|
+
if (bullet.useSize === false) {
|
|
438
|
+
delete bullet.size;
|
|
439
|
+
delete bullet.sizePercent;
|
|
440
|
+
}
|
|
441
|
+
else if (bullet.sizePercent !== undefined)
|
|
442
|
+
bullet.size = (firstStyle.fontSize ?? 24) * bullet.sizePercent;
|
|
443
|
+
}
|
|
444
|
+
return s;
|
|
445
|
+
}
|
|
446
|
+
const EAST_ASIAN = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Script=Bopomofo}\u3000-\u303f\uff01-\uffef]/u;
|
|
447
|
+
/** Honor distinct ANSI and East Asian typefaces without changing UTF-16 source indices. */
|
|
448
|
+
function splitScriptRun(text, style, start, maxRuns) {
|
|
449
|
+
if (maxRuns < 1)
|
|
450
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many rendered text runs');
|
|
451
|
+
if (!style.eastAsianFontFamily && !style.ansiFontFamily && !style.symbolFontFamily)
|
|
452
|
+
return [{ text: text.replace(/\u000b/g, '\n'), style, sourceStart: start, sourceEnd: start + text.length }];
|
|
453
|
+
const result = [];
|
|
454
|
+
let previousFamily;
|
|
455
|
+
let from = 0;
|
|
456
|
+
for (let i = 0; i <= text.length;) {
|
|
457
|
+
const code = text.codePointAt(i) ?? 0;
|
|
458
|
+
const char = i === text.length ? undefined : String.fromCodePoint(code);
|
|
459
|
+
const family = char === undefined ? undefined : code >= 0xf000 && code <= 0xf0ff && style.symbolFontFamily ? style.symbolFontFamily : EAST_ASIAN.test(char) ? style.eastAsianFontFamily ?? style.fontFamily : style.ansiFontFamily ?? style.fontFamily;
|
|
460
|
+
if (i === 0)
|
|
461
|
+
previousFamily = family;
|
|
462
|
+
if (i === text.length || family !== previousFamily) {
|
|
463
|
+
if (result.length >= maxRuns)
|
|
464
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many rendered text runs');
|
|
465
|
+
result.push({ text: text.slice(from, i).replace(/\u000b/g, '\n'), style: { ...style, fontFamily: previousFamily }, sourceStart: start + from, sourceEnd: start + i });
|
|
466
|
+
from = i;
|
|
467
|
+
previousFamily = family;
|
|
468
|
+
}
|
|
469
|
+
if (i === text.length)
|
|
470
|
+
break;
|
|
471
|
+
i += code > 0xffff ? 2 : 1;
|
|
472
|
+
}
|
|
473
|
+
return result;
|
|
474
|
+
}
|
|
475
|
+
/** Link range endpoints are UTF-16 offsets; TextRange.end is an exclusive cutoff. */
|
|
476
|
+
function parseHyperlinkRanges(records, length, ctx) {
|
|
477
|
+
const ranges = [];
|
|
478
|
+
let pending;
|
|
479
|
+
for (const record of records) {
|
|
480
|
+
// Instance 0 is a click; instance 1 is mouse-over and must not become a click.
|
|
481
|
+
if (record.type === 0x0ff2 && record.instance === 0) {
|
|
482
|
+
pending = undefined;
|
|
483
|
+
const atom = record.children.find(child => child.type === 0x0ff3);
|
|
484
|
+
if (!atom)
|
|
485
|
+
continue;
|
|
486
|
+
try {
|
|
487
|
+
const c = new Cursor(ctx.bytes, atom);
|
|
488
|
+
c.need(16);
|
|
489
|
+
pending = interactionHref(atom, ctx.bytes, ctx.hyperlinkTargets);
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
failure(ctx, error, atom);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
else if (record.type === 0x0fdf && record.instance === 0) {
|
|
496
|
+
const href = pending;
|
|
497
|
+
pending = undefined;
|
|
498
|
+
if (!href)
|
|
499
|
+
continue;
|
|
500
|
+
try {
|
|
501
|
+
const c = new Cursor(ctx.bytes, record);
|
|
502
|
+
const start = c.u32(), end = c.u32();
|
|
503
|
+
if (start >= end || end > length)
|
|
504
|
+
throw new PptError('TEXT_HYPERLINK_RANGE', `Invalid hyperlink range ${start}..${end} for ${length} UTF-16 code units`, record.offset);
|
|
505
|
+
if (ranges.length >= ctx.limits.maxRecords)
|
|
506
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many text hyperlink ranges', record.offset);
|
|
507
|
+
ranges.push({ start, end, href, offset: record.offset });
|
|
508
|
+
}
|
|
509
|
+
catch (error) {
|
|
510
|
+
failure(ctx, error, record);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
ranges.sort((a, b) => a.start - b.start || a.offset - b.offset);
|
|
515
|
+
const result = [];
|
|
516
|
+
for (const range of ranges) {
|
|
517
|
+
const previous = result[result.length - 1];
|
|
518
|
+
if (previous && range.start < previous.end) {
|
|
519
|
+
warn(ctx, 'TEXT_HYPERLINK_OVERLAP', 'Overlapping text hyperlink range was ignored', range.offset);
|
|
520
|
+
continue;
|
|
521
|
+
}
|
|
522
|
+
result.push(range);
|
|
523
|
+
}
|
|
524
|
+
return result;
|
|
525
|
+
}
|
|
526
|
+
/** Keep metacharacters symbolic so a master can be reused for multiple slides. */
|
|
527
|
+
function parseFields(records, text, ctx) {
|
|
528
|
+
const fields = [];
|
|
529
|
+
const positions = new Set();
|
|
530
|
+
for (const record of records) {
|
|
531
|
+
const type = record.type === 4056 ? 'slideNumber' : record.type === 4087 || record.type === 4117 ? 'dateTime' : record.type === 4088 ? 'genericDate' : record.type === 4089 ? 'header' : record.type === 4090 ? 'footer' : undefined;
|
|
532
|
+
if (!type)
|
|
533
|
+
continue;
|
|
534
|
+
try {
|
|
535
|
+
const c = new Cursor(ctx.bytes, record);
|
|
536
|
+
const position = c.u32();
|
|
537
|
+
if (position > text.length)
|
|
538
|
+
throw new PptError('TEXT_FIELD_POSITION', `Metacharacter position ${position} exceeds its text body`, record.offset);
|
|
539
|
+
if (positions.has(position))
|
|
540
|
+
throw new PptError('TEXT_FIELD_DUPLICATE', `More than one metacharacter occupies UTF-16 position ${position}`, record.offset);
|
|
541
|
+
const field = { position, type };
|
|
542
|
+
if (record.type === 4087) {
|
|
543
|
+
c.need(4);
|
|
544
|
+
field.format = c.view.getUint8(c.at);
|
|
545
|
+
c.at += 4; // Three bytes after the format index are unused.
|
|
546
|
+
if (field.format > 12)
|
|
547
|
+
warn(ctx, 'TEXT_DATE_FORMAT', `Unsupported date metacharacter format ${field.format}`, record.offset);
|
|
548
|
+
}
|
|
549
|
+
else if (record.type === 4117) {
|
|
550
|
+
c.need(128);
|
|
551
|
+
let format = '';
|
|
552
|
+
for (let i = 0; i < 64; i++) {
|
|
553
|
+
const unit = c.view.getUint16(c.at + i * 2, true);
|
|
554
|
+
if (!unit)
|
|
555
|
+
break;
|
|
556
|
+
format += String.fromCharCode(unit);
|
|
557
|
+
}
|
|
558
|
+
field.format = format;
|
|
559
|
+
c.at += 128;
|
|
560
|
+
}
|
|
561
|
+
if (c.at !== c.end)
|
|
562
|
+
warn(ctx, 'TEXT_FIELD_LENGTH', 'Extra bytes in a text metacharacter record were ignored', c.at);
|
|
563
|
+
if (position === text.length)
|
|
564
|
+
warn(ctx, 'TEXT_FIELD_TERMINAL', 'Metacharacter refers to the implicit terminal paragraph mark', record.offset);
|
|
565
|
+
if (fields.length >= ctx.limits.maxRecords)
|
|
566
|
+
throw new PptError('LIMIT_TEXT_FIELDS', 'Too many text metacharacters', record.offset);
|
|
567
|
+
positions.add(position);
|
|
568
|
+
fields.push(field);
|
|
569
|
+
}
|
|
570
|
+
catch (error) {
|
|
571
|
+
failure(ctx, error, record);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return fields.sort((a, b) => a.position - b.position);
|
|
575
|
+
}
|
|
576
|
+
function parseMasterIndent(record, length, ctx) {
|
|
577
|
+
const c = new Cursor(ctx.bytes, record), result = [];
|
|
578
|
+
let covered = 0;
|
|
579
|
+
try {
|
|
580
|
+
while (covered < length) {
|
|
581
|
+
if (result.length >= ctx.limits.maxRecords)
|
|
582
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many master indent runs', c.at);
|
|
583
|
+
const start = covered, count = c.u32();
|
|
584
|
+
checkCount(count, covered, length, c.at - 4);
|
|
585
|
+
const level = c.u16();
|
|
586
|
+
covered += count;
|
|
587
|
+
result.push({ start, end: covered, level: Math.min(level, 4), style: {} });
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
catch (error) {
|
|
591
|
+
failure(ctx, error, record);
|
|
592
|
+
}
|
|
593
|
+
return result;
|
|
594
|
+
}
|
|
595
|
+
function frameFromRecords(records, ctx) {
|
|
596
|
+
const header = records.find(r => r.type === RT.TextHeaderAtom);
|
|
597
|
+
const textRecord = records.find(r => r.type === RT.TextCharsAtom || r.type === RT.TextBytesAtom);
|
|
598
|
+
if (!header && !textRecord)
|
|
599
|
+
return undefined;
|
|
600
|
+
let textType = 4;
|
|
601
|
+
if (header) {
|
|
602
|
+
try {
|
|
603
|
+
textType = new Cursor(ctx.bytes, header).u32();
|
|
604
|
+
}
|
|
605
|
+
catch (error) {
|
|
606
|
+
failure(ctx, error, header);
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
const text = textRecord ? (textRecord.type === RT.TextCharsAtom ? decodeChars(textRecord, ctx) : decodeBytes(textRecord, ctx)) : '';
|
|
610
|
+
const length = text.length + 1;
|
|
611
|
+
if (length - 1 > ctx.limits.maxTextLength)
|
|
612
|
+
throw new PptError('LIMIT_TEXT', 'Text body exceeds configured limit', textRecord?.offset);
|
|
613
|
+
const styleRecord = records.find(r => r.type === RT.StyleTextPropAtom);
|
|
614
|
+
const ranges = styleRecord ? parseRuns(styleRecord, length, ctx) : { paragraphs: [], characters: [] };
|
|
615
|
+
const specialInfoRecord = records.find(r => r.type === RT.TextSpecInfoAtom);
|
|
616
|
+
const specialInfo = specialInfoRecord ? parseSpecialInfo(specialInfoRecord, length, ctx) : [];
|
|
617
|
+
const hyperlinks = parseHyperlinkRanges(records, length, ctx);
|
|
618
|
+
const fields = parseFields(records, text, ctx);
|
|
619
|
+
const indentRecord = records.find(r => r.type === 4002);
|
|
620
|
+
if (!ranges.paragraphs.length && indentRecord)
|
|
621
|
+
ranges.paragraphs = parseMasterIndent(indentRecord, length, ctx);
|
|
622
|
+
let ruler;
|
|
623
|
+
const rulerRecord = records.find(r => r.type === RT.TextRulerAtom);
|
|
624
|
+
if (rulerRecord) {
|
|
625
|
+
try {
|
|
626
|
+
ruler = readRuler(rulerRecord, ctx);
|
|
627
|
+
}
|
|
628
|
+
catch (error) {
|
|
629
|
+
failure(ctx, error, rulerRecord);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
const paragraphs = [];
|
|
633
|
+
let pfIndex = 0, cfIndex = 0, siIndex = 0, linkIndex = 0, outputRunCount = 0, from = 0;
|
|
634
|
+
// The implicit terminal CR ends the final paragraph but is not displayed.
|
|
635
|
+
for (let to = 0; to <= text.length; to++) {
|
|
636
|
+
if (to !== text.length && text.charCodeAt(to) !== 13)
|
|
637
|
+
continue;
|
|
638
|
+
if (paragraphs.length >= ctx.limits.maxRecords)
|
|
639
|
+
throw new PptError('LIMIT_TEXT_PARAGRAPHS', 'Too many text paragraphs', textRecord?.offset);
|
|
640
|
+
while (pfIndex + 1 < ranges.paragraphs.length && ranges.paragraphs[pfIndex].end <= from)
|
|
641
|
+
pfIndex++;
|
|
642
|
+
const pf = ranges.paragraphs[pfIndex];
|
|
643
|
+
const level = pf && pf.start <= from && pf.end > from ? pf.level : 0;
|
|
644
|
+
const inherited = masterLevel(ctx, textType, level);
|
|
645
|
+
let paragraphStyle = mergeParagraph(inherited.paragraph, pf && pf.start <= from && pf.end > from ? pf.style : {});
|
|
646
|
+
if (ruler)
|
|
647
|
+
paragraphStyle = mergeParagraph(paragraphStyle, mergeParagraph(ruler.common, ruler.levels[level] ?? {}));
|
|
648
|
+
const runs = [];
|
|
649
|
+
let at = from;
|
|
650
|
+
while (at < to) {
|
|
651
|
+
while (cfIndex < ranges.characters.length && ranges.characters[cfIndex].end <= at)
|
|
652
|
+
cfIndex++;
|
|
653
|
+
const cf = ranges.characters[cfIndex];
|
|
654
|
+
const active = cf && cf.start <= at && cf.end > at ? cf : undefined;
|
|
655
|
+
while (siIndex < specialInfo.length && specialInfo[siIndex].end <= at)
|
|
656
|
+
siIndex++;
|
|
657
|
+
const si = specialInfo[siIndex];
|
|
658
|
+
const activeSI = si && si.start <= at && si.end > at ? si : undefined;
|
|
659
|
+
while (linkIndex < hyperlinks.length && hyperlinks[linkIndex].end <= at)
|
|
660
|
+
linkIndex++;
|
|
661
|
+
const link = hyperlinks[linkIndex];
|
|
662
|
+
const activeLink = link && link.start <= at && link.end > at ? link : undefined;
|
|
663
|
+
const end = Math.min(to, active?.end ?? (cf && cf.start > at ? cf.start : to), activeSI?.end ?? (si && si.start > at ? si.start : to), activeLink?.end ?? (link && link.start > at ? link.start : to));
|
|
664
|
+
const style = resolveCharacter(mergeCharacter(mergeCharacter(inherited.character, active?.style ?? {}), activeSI?.style ?? {}), ctx);
|
|
665
|
+
if (activeLink)
|
|
666
|
+
style.hyperlink = activeLink.href;
|
|
667
|
+
const scriptRuns = splitScriptRun(text.slice(at, end), style, at, ctx.limits.maxRecords - outputRunCount);
|
|
668
|
+
outputRunCount += scriptRuns.length;
|
|
669
|
+
for (const run of scriptRuns)
|
|
670
|
+
runs.push(run);
|
|
671
|
+
at = end;
|
|
672
|
+
}
|
|
673
|
+
// Empty paragraphs still need their font metrics and paragraph formatting.
|
|
674
|
+
if (!runs.length) {
|
|
675
|
+
if (++outputRunCount > ctx.limits.maxRecords)
|
|
676
|
+
throw new PptError('LIMIT_TEXT_RUNS', 'Too many rendered text runs', textRecord?.offset);
|
|
677
|
+
while (cfIndex < ranges.characters.length && ranges.characters[cfIndex].end <= from)
|
|
678
|
+
cfIndex++;
|
|
679
|
+
const cf = ranges.characters[cfIndex];
|
|
680
|
+
while (siIndex < specialInfo.length && specialInfo[siIndex].end <= from)
|
|
681
|
+
siIndex++;
|
|
682
|
+
const si = specialInfo[siIndex];
|
|
683
|
+
const style = mergeCharacter(mergeCharacter(inherited.character, cf && cf.start <= from ? cf.style : {}), si && si.start <= from ? si.style : {});
|
|
684
|
+
runs.push({ text: '', style: resolveCharacter(style, ctx), sourceStart: from, sourceEnd: from });
|
|
685
|
+
}
|
|
686
|
+
paragraphs.push({ level, runs, style: resolveParagraph(paragraphStyle, runs[0].style, ctx) });
|
|
687
|
+
from = to + 1;
|
|
688
|
+
}
|
|
689
|
+
for (const r of records) {
|
|
690
|
+
if ([0x0fac, 0x0fb1, 0x0fb6].includes(r.type))
|
|
691
|
+
warn(ctx, 'TEXT_EXTENDED_STYLE', `Extended text formatting record 0x${r.type.toString(16)} is preserved only as a diagnostic`, r.offset);
|
|
692
|
+
}
|
|
693
|
+
return { paragraphs, margins: { left: 9.6, right: 9.6, top: 4.8, bottom: 4.8 }, verticalAlignment: 'top', wrap: true, textType, sourceText: text, ...(fields.length ? { fields } : {}) };
|
|
694
|
+
}
|
|
695
|
+
/** Parse immediate sibling text bodies; callers keep slide boundaries separate. */
|
|
696
|
+
export function parseTextFrames(records, ctx) {
|
|
697
|
+
const result = [];
|
|
698
|
+
let body = [];
|
|
699
|
+
const flush = () => {
|
|
700
|
+
if (!body.length)
|
|
701
|
+
return;
|
|
702
|
+
const frame = frameFromRecords(body, ctx);
|
|
703
|
+
if (frame)
|
|
704
|
+
result.push(frame);
|
|
705
|
+
body = [];
|
|
706
|
+
};
|
|
707
|
+
for (const record of records) {
|
|
708
|
+
if (record.type === RT.TextHeaderAtom || record.type === RT.SlidePersistAtom)
|
|
709
|
+
flush();
|
|
710
|
+
if (record.type !== RT.SlidePersistAtom)
|
|
711
|
+
body.push(record);
|
|
712
|
+
}
|
|
713
|
+
flush();
|
|
714
|
+
return result;
|
|
715
|
+
}
|
|
716
|
+
export const parseTextBodies = parseTextFrames;
|
|
717
|
+
/** Read the document's default text language before resolving body/master runs. */
|
|
718
|
+
export function parseDefaultTextStyle(records, ctx) {
|
|
719
|
+
for (const record of walkRecords(records)) {
|
|
720
|
+
if (record.type !== 0x0fa9)
|
|
721
|
+
continue;
|
|
722
|
+
try {
|
|
723
|
+
return readSI(new Cursor(ctx.bytes, record), ctx);
|
|
724
|
+
}
|
|
725
|
+
catch (error) {
|
|
726
|
+
failure(ctx, error, record);
|
|
727
|
+
return {};
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
return {};
|
|
731
|
+
}
|
|
732
|
+
/** Master style entries remain unflattened so slide palette/paragraph overrides work. */
|
|
733
|
+
export function parseMasterTextStyles(records, ctx) {
|
|
734
|
+
const result = new Map();
|
|
735
|
+
for (const [type, levels] of ctx.masterStyles ?? [])
|
|
736
|
+
result.set(type, levels.map(level => ({ paragraph: mergeParagraph({}, level.paragraph), character: { ...level.character } })));
|
|
737
|
+
// Preserve only this type's explicit properties across the master chain.
|
|
738
|
+
// Defaults/base styles are resolved by masterLevel when text is consumed.
|
|
739
|
+
// Flattening them here would turn an ancestor's fallback into an apparent
|
|
740
|
+
// derived override and hide a descendant's changed title/body base style.
|
|
741
|
+
for (const record of walkRecords(records)) {
|
|
742
|
+
if (record.type !== RT.TextMasterStyleAtom)
|
|
743
|
+
continue;
|
|
744
|
+
const levels = result.get(record.instance)?.slice() ?? [];
|
|
745
|
+
try {
|
|
746
|
+
const c = new Cursor(ctx.bytes, record);
|
|
747
|
+
const count = c.u16();
|
|
748
|
+
if (count > 5)
|
|
749
|
+
throw new PptError('TEXT_MASTER_LEVELS', `Master style has ${count} levels; at most five are valid`, record.offset);
|
|
750
|
+
for (let i = 0; i < count; i++) {
|
|
751
|
+
const level = record.instance >= 5 ? c.u16() : i;
|
|
752
|
+
if (level >= count)
|
|
753
|
+
throw new PptError('TEXT_MASTER_LEVEL', `Invalid master style level ${level}`, c.at - 2);
|
|
754
|
+
const paragraph = readPF(c, ctx), character = readCF(c, ctx);
|
|
755
|
+
const inherited = levels[level];
|
|
756
|
+
levels[level] = { paragraph: mergeParagraph(inherited?.paragraph ?? {}, paragraph), character: mergeCharacter(inherited?.character ?? {}, character) };
|
|
757
|
+
}
|
|
758
|
+
if (c.at !== c.end)
|
|
759
|
+
warn(ctx, 'TEXT_MASTER_TAIL', `${c.end - c.at} unused bytes in master text style`, c.at);
|
|
760
|
+
}
|
|
761
|
+
catch (error) {
|
|
762
|
+
failure(ctx, error, record);
|
|
763
|
+
}
|
|
764
|
+
result.set(record.instance, levels);
|
|
765
|
+
}
|
|
766
|
+
return result;
|
|
767
|
+
}
|
|
768
|
+
export function parseFonts(records, bytes) {
|
|
769
|
+
const fonts = [];
|
|
770
|
+
let fontIndex = 0;
|
|
771
|
+
for (const record of walkRecords(records)) {
|
|
772
|
+
if (record.type !== RT.FontEntityAtom)
|
|
773
|
+
continue;
|
|
774
|
+
const id = fontIndex++;
|
|
775
|
+
if (record.length < 68 || record.dataOffset < 0 || record.end > bytes.length)
|
|
776
|
+
continue;
|
|
777
|
+
const view = new DataView(bytes.buffer, bytes.byteOffset + record.dataOffset, record.length);
|
|
778
|
+
let name = '';
|
|
779
|
+
for (let i = 0; i < 32; i++) {
|
|
780
|
+
const unit = view.getUint16(i * 2, true);
|
|
781
|
+
if (!unit)
|
|
782
|
+
break;
|
|
783
|
+
name += String.fromCharCode(unit);
|
|
784
|
+
}
|
|
785
|
+
// FontIndexRef indexes FontCollection entries, not the record header instance.
|
|
786
|
+
fonts.push({ id, name: name || 'Arial', charset: view.getUint8(64) });
|
|
787
|
+
}
|
|
788
|
+
return fonts;
|
|
789
|
+
}
|