@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,1570 @@
|
|
|
1
|
+
import { BinaryReader } from './binary.js';
|
|
2
|
+
import { dataUrlFromBytes, escapeHtml, trimTrailingNulls } from './utils.js';
|
|
3
|
+
import { escapeXmlText } from './xml-text.js';
|
|
4
|
+
import { readEmfFont, readWmfFont } from './vector-font.js';
|
|
5
|
+
import { PptError } from './errors.js';
|
|
6
|
+
import { decodeBgraBitmap, encodeRgbaPng } from './raster.js';
|
|
7
|
+
const DEFAULT_VECTOR_LIMITS = { maxOutputBytes: 32 * 1024 * 1024, maxRecords: 100000, maxPoints: 500000, maxObjects: 65536, maxStateDepth: 1024 };
|
|
8
|
+
const DEFAULT_PEN = { color: '#000000', width: 1 };
|
|
9
|
+
const DEFAULT_BRUSH = { color: '#ffffff', none: true };
|
|
10
|
+
const DEFAULT_FONT = { family: 'sans-serif', size: 12 };
|
|
11
|
+
const IDENTITY_MATRIX = { m11: 1, m12: 0, m21: 0, m22: 1, dx: 0, dy: 0 };
|
|
12
|
+
const PLACEABLE_WMF_KEY = 0x9ac6cdd7;
|
|
13
|
+
const WMF_HEADER_SIZE = 18;
|
|
14
|
+
const WMF_RECORD = {
|
|
15
|
+
META_EOF: 0x0000,
|
|
16
|
+
META_SAVEDC: 0x001e,
|
|
17
|
+
META_RESTOREDC: 0x0127,
|
|
18
|
+
META_SETTEXTALIGN: 0x012e,
|
|
19
|
+
META_SETBKMODE: 0x0102,
|
|
20
|
+
META_SETPOLYFILLMODE: 0x0106,
|
|
21
|
+
META_SETBKCOLOR: 0x0201,
|
|
22
|
+
META_SETTEXTCOLOR: 0x0209,
|
|
23
|
+
META_SETWINDOWORG: 0x020b,
|
|
24
|
+
META_SETWINDOWEXT: 0x020c,
|
|
25
|
+
META_SETVIEWPORTORG: 0x020d,
|
|
26
|
+
META_SETVIEWPORTEXT: 0x020e,
|
|
27
|
+
META_OFFSETWINDOWORG: 0x020f,
|
|
28
|
+
META_OFFSETVIEWPORTORG: 0x0211,
|
|
29
|
+
META_LINETO: 0x0213,
|
|
30
|
+
META_MOVETO: 0x0214,
|
|
31
|
+
META_POLYGON: 0x0324,
|
|
32
|
+
META_POLYLINE: 0x0325,
|
|
33
|
+
META_TEXTOUT: 0x0521,
|
|
34
|
+
META_POLYPOLYGON: 0x0538,
|
|
35
|
+
META_ELLIPSE: 0x0418,
|
|
36
|
+
META_RECTANGLE: 0x041b,
|
|
37
|
+
META_ROUNDRECT: 0x061c,
|
|
38
|
+
META_ARC: 0x0817,
|
|
39
|
+
META_PIE: 0x081a,
|
|
40
|
+
META_CHORD: 0x0830,
|
|
41
|
+
META_INTERSECTCLIPRECT: 0x0416,
|
|
42
|
+
META_EXCLUDECLIPRECT: 0x0415,
|
|
43
|
+
META_EXTTEXTOUT: 0x0a32,
|
|
44
|
+
META_CREATEBRUSHINDIRECT: 0x02fc,
|
|
45
|
+
META_CREATEPENINDIRECT: 0x02fa,
|
|
46
|
+
META_CREATEFONTINDIRECT: 0x02fb,
|
|
47
|
+
META_SELECTOBJECT: 0x012d,
|
|
48
|
+
META_DELETEOBJECT: 0x01f0,
|
|
49
|
+
};
|
|
50
|
+
const EMF_RECORD = {
|
|
51
|
+
EMR_HEADER: 0x00000001,
|
|
52
|
+
EMR_POLYBEZIER: 0x00000002,
|
|
53
|
+
EMR_POLYGON: 0x00000003,
|
|
54
|
+
EMR_POLYLINE: 0x00000004,
|
|
55
|
+
EMR_POLYBEZIERTO: 0x00000005,
|
|
56
|
+
EMR_POLYLINETO: 0x00000006,
|
|
57
|
+
EMR_POLYPOLYLINE: 0x00000007,
|
|
58
|
+
EMR_POLYPOLYGON: 0x00000008,
|
|
59
|
+
EMR_SETWINDOWEXTEX: 0x00000009,
|
|
60
|
+
EMR_SETWINDOWORGEX: 0x0000000a,
|
|
61
|
+
EMR_SETVIEWPORTEXTEX: 0x0000000b,
|
|
62
|
+
EMR_SETVIEWPORTORGEX: 0x0000000c,
|
|
63
|
+
EMR_EOF: 0x0000000e,
|
|
64
|
+
EMR_SETBKMODE: 0x00000012,
|
|
65
|
+
EMR_SETPOLYFILLMODE: 0x00000013,
|
|
66
|
+
EMR_SETTEXTALIGN: 0x00000016,
|
|
67
|
+
EMR_SETTEXTCOLOR: 0x00000018,
|
|
68
|
+
EMR_SETBKCOLOR: 0x00000019,
|
|
69
|
+
EMR_MOVETOEX: 0x0000001b,
|
|
70
|
+
EMR_SAVEDC: 0x00000021,
|
|
71
|
+
EMR_RESTOREDC: 0x00000022,
|
|
72
|
+
EMR_SETWORLDTRANSFORM: 0x00000023,
|
|
73
|
+
EMR_MODIFYWORLDTRANSFORM: 0x00000024,
|
|
74
|
+
EMR_SELECTOBJECT: 0x00000025,
|
|
75
|
+
EMR_CREATEPEN: 0x00000026,
|
|
76
|
+
EMR_CREATEBRUSHINDIRECT: 0x00000027,
|
|
77
|
+
EMR_DELETEOBJECT: 0x00000028,
|
|
78
|
+
EMR_ELLIPSE: 0x0000002a,
|
|
79
|
+
EMR_RECTANGLE: 0x0000002b,
|
|
80
|
+
EMR_ROUNDRECT: 0x0000002c,
|
|
81
|
+
EMR_ARC: 0x0000002d,
|
|
82
|
+
EMR_CHORD: 0x0000002e,
|
|
83
|
+
EMR_PIE: 0x0000002f,
|
|
84
|
+
EMR_ARCTO: 0x00000037,
|
|
85
|
+
EMR_SETARCDIRECTION: 0x00000039,
|
|
86
|
+
EMR_INTERSECTCLIPRECT: 0x0000001e,
|
|
87
|
+
EMR_EXCLUDECLIPRECT: 0x0000001d,
|
|
88
|
+
EMR_BEGINPATH: 0x0000003b,
|
|
89
|
+
EMR_ENDPATH: 0x0000003c,
|
|
90
|
+
EMR_CLOSEFIGURE: 0x0000003d,
|
|
91
|
+
EMR_FILLPATH: 0x0000003e,
|
|
92
|
+
EMR_STROKEANDFILLPATH: 0x0000003f,
|
|
93
|
+
EMR_STROKEPATH: 0x00000040,
|
|
94
|
+
EMR_SELECTCLIPPATH: 0x00000043,
|
|
95
|
+
EMR_LINETO: 0x00000036,
|
|
96
|
+
EMR_BITBLT: 0x0000004c,
|
|
97
|
+
EMR_STRETCHBLT: 0x0000004d,
|
|
98
|
+
EMR_STRETCHDIBITS: 0x00000051,
|
|
99
|
+
EMR_EXTCREATEFONTINDIRECTW: 0x00000052,
|
|
100
|
+
EMR_EXTTEXTOUTA: 0x00000053,
|
|
101
|
+
EMR_EXTTEXTOUTW: 0x00000054,
|
|
102
|
+
EMR_POLYBEZIER16: 0x00000055,
|
|
103
|
+
EMR_POLYGON16: 0x00000056,
|
|
104
|
+
EMR_POLYLINE16: 0x00000057,
|
|
105
|
+
EMR_POLYBEZIERTO16: 0x00000058,
|
|
106
|
+
EMR_POLYLINETO16: 0x00000059,
|
|
107
|
+
EMR_POLYPOLYLINE16: 0x0000005a,
|
|
108
|
+
EMR_POLYPOLYGON16: 0x0000005b,
|
|
109
|
+
EMR_EXTCREATEPEN: 0x0000005f,
|
|
110
|
+
EMR_SMALLTEXTOUT: 0x0000006c,
|
|
111
|
+
EMR_ALPHABLEND: 0x00000072,
|
|
112
|
+
};
|
|
113
|
+
// Fixed prefixes accessed by the renderer; variable arrays receive their own size checks.
|
|
114
|
+
const WMF_MIN_BYTES = { 0: 6, 0x0102: 8, 0x0106: 8, 0x0201: 10, 0x0209: 10, 0x020b: 10, 0x020c: 10, 0x020d: 10, 0x020e: 10, 0x020f: 10, 0x0211: 10, 0x0213: 10, 0x0214: 10, 0x0324: 8, 0x0325: 8, 0x0521: 12, 0x0538: 8, 0x0418: 14, 0x041b: 14, 0x061c: 18, 0x02fc: 14, 0x02fa: 16, 0x02fb: 24, 0x012d: 8, 0x01f0: 8 };
|
|
115
|
+
const EMF_MIN_BYTES = { 1: 88, 2: 28, 3: 28, 4: 28, 5: 28, 6: 28, 7: 32, 8: 32, 9: 16, 10: 16, 11: 16, 12: 16, 14: 20, 18: 12, 19: 12, 22: 12, 24: 12, 25: 12, 27: 16, 33: 8, 34: 12, 35: 32, 36: 36, 37: 12, 38: 28, 39: 24, 40: 12, 42: 24, 43: 24, 44: 32, 54: 16, 59: 8, 60: 8, 61: 8, 62: 24, 63: 24, 64: 24, 67: 12, 76: 100, 77: 108, 81: 80, 82: 104, 83: 76, 84: 76, 85: 28, 86: 28, 87: 28, 88: 28, 89: 28, 90: 32, 91: 32, 95: 52, 108: 36, 114: 108 };
|
|
116
|
+
Object.assign(WMF_MIN_BYTES, { 0x001e: 6, 0x0127: 8, 0x012e: 8, 0x0817: 22, 0x081a: 22, 0x0830: 22, 0x0415: 14, 0x0416: 14, 0x0a32: 14 });
|
|
117
|
+
Object.assign(EMF_MIN_BYTES, { 29: 24, 30: 24, 45: 40, 46: 40, 47: 40, 55: 40, 57: 12, 83: 60, 84: 60 });
|
|
118
|
+
const STOCK_OBJECT_BASE = 0x80000000;
|
|
119
|
+
const STOCK_OBJECT = {
|
|
120
|
+
WHITE_BRUSH: STOCK_OBJECT_BASE,
|
|
121
|
+
LTGRAY_BRUSH: STOCK_OBJECT_BASE + 1,
|
|
122
|
+
GRAY_BRUSH: STOCK_OBJECT_BASE + 2,
|
|
123
|
+
DKGRAY_BRUSH: STOCK_OBJECT_BASE + 3,
|
|
124
|
+
BLACK_BRUSH: STOCK_OBJECT_BASE + 4,
|
|
125
|
+
NULL_BRUSH: STOCK_OBJECT_BASE + 5,
|
|
126
|
+
WHITE_PEN: STOCK_OBJECT_BASE + 6,
|
|
127
|
+
BLACK_PEN: STOCK_OBJECT_BASE + 7,
|
|
128
|
+
NULL_PEN: STOCK_OBJECT_BASE + 8,
|
|
129
|
+
};
|
|
130
|
+
const MODIFY_WORLD_TRANSFORM = {
|
|
131
|
+
MWT_IDENTITY: 1,
|
|
132
|
+
MWT_LEFTMULTIPLY: 2,
|
|
133
|
+
MWT_RIGHTMULTIPLY: 3,
|
|
134
|
+
MWT_SET: 4,
|
|
135
|
+
};
|
|
136
|
+
function numberCss(value) {
|
|
137
|
+
if (!Number.isFinite(value))
|
|
138
|
+
throw new PptError('METAFILE_COORDINATE', 'Metafile contains a non-finite transformed coordinate');
|
|
139
|
+
const rounded = Math.round(value * 100) / 100;
|
|
140
|
+
return Number.isInteger(rounded) ? String(rounded) : String(rounded);
|
|
141
|
+
}
|
|
142
|
+
function rectWidth(rect) {
|
|
143
|
+
return Math.max(1, rect.right - rect.left || 0);
|
|
144
|
+
}
|
|
145
|
+
function rectHeight(rect) {
|
|
146
|
+
return Math.max(1, rect.bottom - rect.top || 0);
|
|
147
|
+
}
|
|
148
|
+
function normalizeRect(rect) {
|
|
149
|
+
return {
|
|
150
|
+
left: Math.min(rect.left, rect.right),
|
|
151
|
+
top: Math.min(rect.top, rect.bottom),
|
|
152
|
+
right: Math.max(rect.left, rect.right),
|
|
153
|
+
bottom: Math.max(rect.top, rect.bottom),
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function updateBounds(ctx, points) {
|
|
157
|
+
for (const point of points) {
|
|
158
|
+
if (!Number.isFinite(point.x) || !Number.isFinite(point.y))
|
|
159
|
+
continue;
|
|
160
|
+
if (!ctx.contentBounds) {
|
|
161
|
+
ctx.contentBounds = { left: point.x, top: point.y, right: point.x, bottom: point.y };
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
ctx.contentBounds.left = Math.min(ctx.contentBounds.left, point.x);
|
|
165
|
+
ctx.contentBounds.top = Math.min(ctx.contentBounds.top, point.y);
|
|
166
|
+
ctx.contentBounds.right = Math.max(ctx.contentBounds.right, point.x);
|
|
167
|
+
ctx.contentBounds.bottom = Math.max(ctx.contentBounds.bottom, point.y);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
function colorRefToCss(colorRef) {
|
|
171
|
+
const red = colorRef & 0xff;
|
|
172
|
+
const green = (colorRef >>> 8) & 0xff;
|
|
173
|
+
const blue = (colorRef >>> 16) & 0xff;
|
|
174
|
+
return `#${red.toString(16).padStart(2, '0')}${green.toString(16).padStart(2, '0')}${blue.toString(16).padStart(2, '0')}`;
|
|
175
|
+
}
|
|
176
|
+
function createBitmapFileHeader(pixelOffset, fileSize) {
|
|
177
|
+
const header = new Uint8Array(14);
|
|
178
|
+
const view = new DataView(header.buffer);
|
|
179
|
+
header[0] = 0x42;
|
|
180
|
+
header[1] = 0x4d;
|
|
181
|
+
view.setUint32(2, fileSize >>> 0, true);
|
|
182
|
+
view.setUint32(10, pixelOffset >>> 0, true);
|
|
183
|
+
return header;
|
|
184
|
+
}
|
|
185
|
+
function bitmapInfoToBmp(bmi, bits) {
|
|
186
|
+
if (bmi.length < 12 || !bits.length)
|
|
187
|
+
return null;
|
|
188
|
+
const pixelOffset = 14 + bmi.length;
|
|
189
|
+
const fileSize = pixelOffset + bits.length;
|
|
190
|
+
const out = new Uint8Array(fileSize);
|
|
191
|
+
out.set(createBitmapFileHeader(pixelOffset, fileSize), 0);
|
|
192
|
+
out.set(bmi, 14);
|
|
193
|
+
out.set(bits, pixelOffset);
|
|
194
|
+
return out;
|
|
195
|
+
}
|
|
196
|
+
function penStyleFromCode(style, width, color) {
|
|
197
|
+
const normalizedWidth = Math.max(1, Math.abs(width) || 1);
|
|
198
|
+
const base = { color, width: normalizedWidth };
|
|
199
|
+
switch (style & 0x000f) {
|
|
200
|
+
case 1:
|
|
201
|
+
return { ...base, dasharray: `${normalizedWidth * 4} ${normalizedWidth * 2}` };
|
|
202
|
+
case 2:
|
|
203
|
+
return { ...base, dasharray: `${normalizedWidth} ${normalizedWidth * 2}` };
|
|
204
|
+
case 3:
|
|
205
|
+
return { ...base, dasharray: `${normalizedWidth * 4} ${normalizedWidth * 2} ${normalizedWidth} ${normalizedWidth * 2}` };
|
|
206
|
+
case 4:
|
|
207
|
+
return { ...base, dasharray: `${normalizedWidth * 4} ${normalizedWidth * 2} ${normalizedWidth} ${normalizedWidth * 2} ${normalizedWidth} ${normalizedWidth * 2}` };
|
|
208
|
+
case 5:
|
|
209
|
+
return { ...base, none: true };
|
|
210
|
+
default:
|
|
211
|
+
return base;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function brushStyleFromCode(style, color, hatch = 0) {
|
|
215
|
+
if (style === 1)
|
|
216
|
+
return { color, none: true };
|
|
217
|
+
if (style === 2 && hatch <= 5)
|
|
218
|
+
return { color, hatch };
|
|
219
|
+
return { color };
|
|
220
|
+
}
|
|
221
|
+
function cloneGraphicsState(state) {
|
|
222
|
+
return {
|
|
223
|
+
...state,
|
|
224
|
+
windowOrg: { ...state.windowOrg },
|
|
225
|
+
windowExt: { ...state.windowExt },
|
|
226
|
+
viewportOrg: { ...state.viewportOrg },
|
|
227
|
+
viewportExt: { ...state.viewportExt },
|
|
228
|
+
worldTransform: { ...state.worldTransform },
|
|
229
|
+
currentPosition: { ...state.currentPosition },
|
|
230
|
+
pen: { ...state.pen },
|
|
231
|
+
brush: { ...state.brush },
|
|
232
|
+
font: { ...state.font },
|
|
233
|
+
clips: state.clips.map(c => ({ ...c })),
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
function readXForm(reader, offset) {
|
|
237
|
+
const view = reader.view;
|
|
238
|
+
return {
|
|
239
|
+
m11: reader.ensure(offset, 4) ? view.getFloat32(offset, true) : 1,
|
|
240
|
+
m12: reader.ensure(offset + 4, 4) ? view.getFloat32(offset + 4, true) : 0,
|
|
241
|
+
m21: reader.ensure(offset + 8, 4) ? view.getFloat32(offset + 8, true) : 0,
|
|
242
|
+
m22: reader.ensure(offset + 12, 4) ? view.getFloat32(offset + 12, true) : 1,
|
|
243
|
+
dx: reader.ensure(offset + 16, 4) ? view.getFloat32(offset + 16, true) : 0,
|
|
244
|
+
dy: reader.ensure(offset + 20, 4) ? view.getFloat32(offset + 20, true) : 0,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
function multiplyMatrix(left, right) {
|
|
248
|
+
return {
|
|
249
|
+
m11: left.m11 * right.m11 + left.m12 * right.m21,
|
|
250
|
+
m12: left.m11 * right.m12 + left.m12 * right.m22,
|
|
251
|
+
m21: left.m21 * right.m11 + left.m22 * right.m21,
|
|
252
|
+
m22: left.m21 * right.m12 + left.m22 * right.m22,
|
|
253
|
+
dx: left.dx * right.m11 + left.dy * right.m21 + right.dx,
|
|
254
|
+
dy: left.dx * right.m12 + left.dy * right.m22 + right.dy,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
function transformPoint(matrix, point) {
|
|
258
|
+
return {
|
|
259
|
+
x: point.x * matrix.m11 + point.y * matrix.m21 + matrix.dx,
|
|
260
|
+
y: point.x * matrix.m12 + point.y * matrix.m22 + matrix.dy,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
function transformVector(matrix, vector) {
|
|
264
|
+
return {
|
|
265
|
+
x: vector.x * matrix.m11 + vector.y * matrix.m21,
|
|
266
|
+
y: vector.x * matrix.m12 + vector.y * matrix.m22,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
function defaultContext(width, height, viewBox, limits, objectCount) {
|
|
270
|
+
if (objectCount > limits.maxObjects)
|
|
271
|
+
throw new PptError('METAFILE_OBJECT_LIMIT', 'Metafile object table exceeds the configured handle limit');
|
|
272
|
+
return {
|
|
273
|
+
width,
|
|
274
|
+
height,
|
|
275
|
+
viewBox,
|
|
276
|
+
recordCount: 0,
|
|
277
|
+
unresolvedGlyphRuns: 0,
|
|
278
|
+
contentBounds: null,
|
|
279
|
+
nodes: [], defs: [], defSequence: 0, patternIds: new Map(),
|
|
280
|
+
stateStack: [],
|
|
281
|
+
capturePath: false,
|
|
282
|
+
pathData: [],
|
|
283
|
+
pathPoints: [],
|
|
284
|
+
objects: new Array(objectCount).fill(null),
|
|
285
|
+
limits, outputBytes: 0, pathBytes: 0, pointCount: 0,
|
|
286
|
+
freeObjectSlots: Array.from({ length: objectCount }, (_, index) => index),
|
|
287
|
+
unsupportedRecordTypes: new Set(), activeRecordType: 0,
|
|
288
|
+
state: {
|
|
289
|
+
windowOrg: { x: viewBox.left, y: viewBox.top },
|
|
290
|
+
windowExt: { x: rectWidth(viewBox), y: rectHeight(viewBox) },
|
|
291
|
+
viewportOrg: { x: viewBox.left, y: viewBox.top },
|
|
292
|
+
viewportExt: { x: rectWidth(viewBox), y: rectHeight(viewBox) },
|
|
293
|
+
worldTransform: { ...IDENTITY_MATRIX },
|
|
294
|
+
currentPosition: { x: 0, y: 0 },
|
|
295
|
+
pen: { ...DEFAULT_PEN },
|
|
296
|
+
brush: { ...DEFAULT_BRUSH },
|
|
297
|
+
font: { ...DEFAULT_FONT },
|
|
298
|
+
textColor: '#000000',
|
|
299
|
+
bkColor: '#ffffff',
|
|
300
|
+
bkMode: 1,
|
|
301
|
+
textAlign: 0,
|
|
302
|
+
polygonFillMode: 1,
|
|
303
|
+
arcDirection: 1, clips: [],
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
function mapPoint(ctx, point) {
|
|
308
|
+
const transformed = transformPoint(ctx.state.worldTransform, point);
|
|
309
|
+
const windowExtX = ctx.state.windowExt.x || rectWidth(ctx.viewBox) || 1;
|
|
310
|
+
const windowExtY = ctx.state.windowExt.y || rectHeight(ctx.viewBox) || 1;
|
|
311
|
+
const viewportExtX = ctx.state.viewportExt.x || rectWidth(ctx.viewBox) || 1;
|
|
312
|
+
const viewportExtY = ctx.state.viewportExt.y || rectHeight(ctx.viewBox) || 1;
|
|
313
|
+
return {
|
|
314
|
+
x: ctx.state.viewportOrg.x + ((transformed.x - ctx.state.windowOrg.x) * viewportExtX) / windowExtX,
|
|
315
|
+
y: ctx.state.viewportOrg.y + ((transformed.y - ctx.state.windowOrg.y) * viewportExtY) / windowExtY,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function mapVector(ctx, vector) {
|
|
319
|
+
const transformed = transformVector(ctx.state.worldTransform, vector);
|
|
320
|
+
const windowExtX = ctx.state.windowExt.x || rectWidth(ctx.viewBox) || 1;
|
|
321
|
+
const windowExtY = ctx.state.windowExt.y || rectHeight(ctx.viewBox) || 1;
|
|
322
|
+
const viewportExtX = ctx.state.viewportExt.x || rectWidth(ctx.viewBox) || 1;
|
|
323
|
+
const viewportExtY = ctx.state.viewportExt.y || rectHeight(ctx.viewBox) || 1;
|
|
324
|
+
return {
|
|
325
|
+
x: (transformed.x * viewportExtX) / windowExtX,
|
|
326
|
+
y: (transformed.y * viewportExtY) / windowExtY,
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
function mapRect(ctx, rect) {
|
|
330
|
+
const points = [
|
|
331
|
+
mapPoint(ctx, { x: rect.left, y: rect.top }),
|
|
332
|
+
mapPoint(ctx, { x: rect.right, y: rect.top }),
|
|
333
|
+
mapPoint(ctx, { x: rect.right, y: rect.bottom }),
|
|
334
|
+
mapPoint(ctx, { x: rect.left, y: rect.bottom }),
|
|
335
|
+
];
|
|
336
|
+
return normalizeRect({
|
|
337
|
+
left: Math.min(...points.map((point) => point.x)),
|
|
338
|
+
top: Math.min(...points.map((point) => point.y)),
|
|
339
|
+
right: Math.max(...points.map((point) => point.x)),
|
|
340
|
+
bottom: Math.max(...points.map((point) => point.y)),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
function pointAttr(point) {
|
|
344
|
+
return `${numberCss(point.x)},${numberCss(point.y)}`;
|
|
345
|
+
}
|
|
346
|
+
function effectivePenWidth(ctx, pen) {
|
|
347
|
+
const width = Math.max(1, Math.abs(pen.width) || 1);
|
|
348
|
+
const vx = mapVector(ctx, { x: width, y: 0 });
|
|
349
|
+
const vy = mapVector(ctx, { x: 0, y: width });
|
|
350
|
+
const sx = Math.hypot(vx.x, vx.y);
|
|
351
|
+
const sy = Math.hypot(vy.x, vy.y);
|
|
352
|
+
const scaled = (sx + sy) / 2;
|
|
353
|
+
if (!Number.isFinite(scaled) || scaled <= 0)
|
|
354
|
+
return width;
|
|
355
|
+
return Math.max(0.5, scaled);
|
|
356
|
+
}
|
|
357
|
+
function scaledDashArray(ctx, pen) {
|
|
358
|
+
if (!pen.dasharray)
|
|
359
|
+
return undefined;
|
|
360
|
+
const baseWidth = Math.max(1, Math.abs(pen.width) || 1);
|
|
361
|
+
const scale = effectivePenWidth(ctx, pen) / baseWidth;
|
|
362
|
+
return pen.dasharray
|
|
363
|
+
.split(/\s+/)
|
|
364
|
+
.map((part) => Number(part))
|
|
365
|
+
.filter((value) => Number.isFinite(value) && value > 0)
|
|
366
|
+
.map((value) => numberCss(value * scale))
|
|
367
|
+
.join(' ') || undefined;
|
|
368
|
+
}
|
|
369
|
+
function strokeAttributes(ctx, pen) {
|
|
370
|
+
if (pen.none)
|
|
371
|
+
return 'stroke="none"';
|
|
372
|
+
const attrs = [`stroke="${escapeHtml(pen.color)}"`, `stroke-width="${numberCss(effectivePenWidth(ctx, pen))}"`, 'fill="none"'];
|
|
373
|
+
const dasharray = scaledDashArray(ctx, pen);
|
|
374
|
+
if (dasharray)
|
|
375
|
+
attrs.push(`stroke-dasharray="${escapeHtml(dasharray)}"`);
|
|
376
|
+
return attrs.join(' ');
|
|
377
|
+
}
|
|
378
|
+
function fillAttributes(ctx) {
|
|
379
|
+
const attrs = [ctx.state.pen.none ? 'stroke="none"' : `stroke="${escapeHtml(ctx.state.pen.color)}"`, ctx.state.pen.none ? '' : `stroke-width="${numberCss(effectivePenWidth(ctx, ctx.state.pen))}"`].filter(Boolean);
|
|
380
|
+
const dasharray = scaledDashArray(ctx, ctx.state.pen);
|
|
381
|
+
if (dasharray && !ctx.state.pen.none)
|
|
382
|
+
attrs.push(`stroke-dasharray="${escapeHtml(dasharray)}"`);
|
|
383
|
+
attrs.push(brushFill(ctx));
|
|
384
|
+
attrs.push(`fill-rule="${ctx.state.polygonFillMode === 2 ? 'nonzero' : 'evenodd'}"`);
|
|
385
|
+
return attrs.join(' ');
|
|
386
|
+
}
|
|
387
|
+
function fillOnlyAttributes(ctx) {
|
|
388
|
+
const attrs = ['stroke="none"'];
|
|
389
|
+
attrs.push(brushFill(ctx));
|
|
390
|
+
attrs.push(`fill-rule="${ctx.state.polygonFillMode === 2 ? 'nonzero' : 'evenodd'}"`);
|
|
391
|
+
return attrs.join(' ');
|
|
392
|
+
}
|
|
393
|
+
function addDefinition(ctx, markup) {
|
|
394
|
+
const bytes = utf8Length(markup) + 1;
|
|
395
|
+
if (bytes > ctx.limits.maxOutputBytes - ctx.outputBytes)
|
|
396
|
+
throw new PptError('LIMIT_ASSETS', 'Metafile definitions exceed the SVG output budget');
|
|
397
|
+
ctx.outputBytes += bytes;
|
|
398
|
+
ctx.defs.push(markup);
|
|
399
|
+
}
|
|
400
|
+
function brushFill(ctx) {
|
|
401
|
+
const brush = ctx.state.brush;
|
|
402
|
+
if (brush.none)
|
|
403
|
+
return 'fill="none"';
|
|
404
|
+
if (brush.hatch === undefined)
|
|
405
|
+
return `fill="${escapeHtml(brush.color)}"`;
|
|
406
|
+
const key = `${brush.hatch}:${brush.color}:${ctx.state.bkMode}:${ctx.state.bkColor}`;
|
|
407
|
+
let id = ctx.patternIds.get(key);
|
|
408
|
+
if (!id) {
|
|
409
|
+
id = `mf-pattern-${++ctx.defSequence}`;
|
|
410
|
+
const paths = ['M0 4H8', 'M4 0V8', 'M-2 2L2 -2M0 8L8 0M6 10L10 6', 'M-2 6L2 10M0 0L8 8M6 -2L10 2', 'M0 4H8M4 0V8', 'M-2 2L2 -2M0 8L8 0M6 10L10 6M-2 6L2 10M0 0L8 8M6 -2L10 2'];
|
|
411
|
+
const background = ctx.state.bkMode === 2 ? `<rect width="8" height="8" fill="${ctx.state.bkColor}"/>` : '';
|
|
412
|
+
addDefinition(ctx, `<pattern id="${id}" patternUnits="userSpaceOnUse" width="8" height="8">${background}<path d="${paths[brush.hatch]}" fill="none" stroke="${brush.color}" stroke-width="1"/></pattern>`);
|
|
413
|
+
ctx.patternIds.set(key, id);
|
|
414
|
+
}
|
|
415
|
+
return `fill="url(#${id})"`;
|
|
416
|
+
}
|
|
417
|
+
function clipPath(ctx, path, mode) {
|
|
418
|
+
if (![1, 4, 5].includes(mode))
|
|
419
|
+
return false;
|
|
420
|
+
if (mode === 5)
|
|
421
|
+
ctx.state.clips = [];
|
|
422
|
+
if (ctx.state.clips.length >= Math.min(256, ctx.limits.maxStateDepth))
|
|
423
|
+
throw new PptError('METAFILE_CLIP_LIMIT', 'Metafile clipping stack exceeds its limit');
|
|
424
|
+
const id = `mf-clip-${++ctx.defSequence}`, mask = mode === 4;
|
|
425
|
+
if (mask) {
|
|
426
|
+
const r = ctx.viewBox;
|
|
427
|
+
addDefinition(ctx, `<mask id="${id}" maskUnits="userSpaceOnUse" x="${numberCss(r.left)}" y="${numberCss(r.top)}" width="${numberCss(rectWidth(r))}" height="${numberCss(rectHeight(r))}" style="mask-type:luminance"><rect x="${numberCss(r.left)}" y="${numberCss(r.top)}" width="${numberCss(rectWidth(r))}" height="${numberCss(rectHeight(r))}" fill="white"/><path d="${path}" fill="black" fill-rule="${ctx.state.polygonFillMode === 2 ? 'nonzero' : 'evenodd'}"/></mask>`);
|
|
428
|
+
}
|
|
429
|
+
else
|
|
430
|
+
addDefinition(ctx, `<clipPath id="${id}" clipPathUnits="userSpaceOnUse"><path d="${path}" clip-rule="${ctx.state.polygonFillMode === 2 ? 'nonzero' : 'evenodd'}"/></clipPath>`);
|
|
431
|
+
ctx.state.clips.push({ id, mask });
|
|
432
|
+
return true;
|
|
433
|
+
}
|
|
434
|
+
function rectanglePath(ctx, rect) {
|
|
435
|
+
const points = [{ x: rect.left, y: rect.top }, { x: rect.right, y: rect.top }, { x: rect.right, y: rect.bottom }, { x: rect.left, y: rect.bottom }].map(p => mapPoint(ctx, p));
|
|
436
|
+
return { path: `M${points.map(pointAttr).join('L')}Z`, points };
|
|
437
|
+
}
|
|
438
|
+
function saveState(ctx) { if (ctx.stateStack.length >= ctx.limits.maxStateDepth)
|
|
439
|
+
throw new PptError('METAFILE_STATE_LIMIT', 'Metafile graphics-state stack exceeds its depth limit'); ctx.stateStack.push(cloneGraphicsState(ctx.state)); }
|
|
440
|
+
function restoreState(ctx, saved) { const index = saved < 0 ? ctx.stateStack.length + saved : saved - 1; if (index < 0 || index >= ctx.stateStack.length)
|
|
441
|
+
return; const state = ctx.stateStack[index]; ctx.stateStack.length = index; ctx.state = cloneGraphicsState(state); }
|
|
442
|
+
function ensureObjectSlot(ctx) {
|
|
443
|
+
const heap = ctx.freeObjectSlots, first = heap[0];
|
|
444
|
+
if (first === undefined)
|
|
445
|
+
throw new PptError('METAFILE_OBJECT_LIMIT', 'Metafile creates more objects than its header declares');
|
|
446
|
+
const last = heap.pop();
|
|
447
|
+
if (heap.length) {
|
|
448
|
+
heap[0] = last;
|
|
449
|
+
let at = 0;
|
|
450
|
+
while (at * 2 + 1 < heap.length) {
|
|
451
|
+
let child = at * 2 + 1;
|
|
452
|
+
if (child + 1 < heap.length && heap[child + 1] < heap[child])
|
|
453
|
+
child++;
|
|
454
|
+
if (heap[at] <= heap[child])
|
|
455
|
+
break;
|
|
456
|
+
[heap[at], heap[child]] = [heap[child], heap[at]];
|
|
457
|
+
at = child;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
return first;
|
|
461
|
+
}
|
|
462
|
+
function setObject(ctx, index, value) {
|
|
463
|
+
if (!Number.isInteger(index) || index < 0 || index >= ctx.objects.length)
|
|
464
|
+
throw new PptError('METAFILE_OBJECT_HANDLE', 'Metafile object handle exceeds its declared table');
|
|
465
|
+
ctx.objects[index] = value;
|
|
466
|
+
}
|
|
467
|
+
function deleteObject(ctx, index) {
|
|
468
|
+
if (index < 0 || index >= ctx.objects.length || !ctx.objects[index])
|
|
469
|
+
return;
|
|
470
|
+
ctx.objects[index] = null;
|
|
471
|
+
const heap = ctx.freeObjectSlots;
|
|
472
|
+
let at = heap.length;
|
|
473
|
+
heap.push(index);
|
|
474
|
+
while (at > 0) {
|
|
475
|
+
const parent = (at - 1) >>> 1;
|
|
476
|
+
if (heap[parent] <= index)
|
|
477
|
+
break;
|
|
478
|
+
heap[at] = heap[parent];
|
|
479
|
+
at = parent;
|
|
480
|
+
heap[at] = index;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
function utf8Length(value) {
|
|
484
|
+
let bytes = 0;
|
|
485
|
+
for (let i = 0; i < value.length; i++) {
|
|
486
|
+
const c = value.charCodeAt(i);
|
|
487
|
+
if (c < 128)
|
|
488
|
+
bytes++;
|
|
489
|
+
else if (c < 2048)
|
|
490
|
+
bytes += 2;
|
|
491
|
+
else if (c >= 0xD800 && c <= 0xDBFF && i + 1 < value.length && value.charCodeAt(i + 1) >= 0xDC00 && value.charCodeAt(i + 1) <= 0xDFFF) {
|
|
492
|
+
bytes += 4;
|
|
493
|
+
i++;
|
|
494
|
+
}
|
|
495
|
+
else
|
|
496
|
+
bytes += 3;
|
|
497
|
+
}
|
|
498
|
+
return bytes;
|
|
499
|
+
}
|
|
500
|
+
function claimPoints(ctx, count) {
|
|
501
|
+
if (!Number.isSafeInteger(count) || count < 0 || count > ctx.limits.maxPoints - ctx.pointCount)
|
|
502
|
+
throw new PptError('METAFILE_POINT_LIMIT', 'Metafile exceeds its point budget');
|
|
503
|
+
ctx.pointCount += count;
|
|
504
|
+
}
|
|
505
|
+
function countRecord(ctx) {
|
|
506
|
+
if (++ctx.recordCount > ctx.limits.maxRecords)
|
|
507
|
+
throw new PptError('METAFILE_RECORD_LIMIT', 'Metafile exceeds its record budget');
|
|
508
|
+
}
|
|
509
|
+
function selectStockObject(ctx, index) {
|
|
510
|
+
switch (index >>> 0) {
|
|
511
|
+
case STOCK_OBJECT.WHITE_BRUSH:
|
|
512
|
+
ctx.state.brush = { color: '#ffffff' };
|
|
513
|
+
return true;
|
|
514
|
+
case STOCK_OBJECT.LTGRAY_BRUSH:
|
|
515
|
+
ctx.state.brush = { color: '#c0c0c0' };
|
|
516
|
+
return true;
|
|
517
|
+
case STOCK_OBJECT.GRAY_BRUSH:
|
|
518
|
+
ctx.state.brush = { color: '#808080' };
|
|
519
|
+
return true;
|
|
520
|
+
case STOCK_OBJECT.DKGRAY_BRUSH:
|
|
521
|
+
ctx.state.brush = { color: '#404040' };
|
|
522
|
+
return true;
|
|
523
|
+
case STOCK_OBJECT.BLACK_BRUSH:
|
|
524
|
+
ctx.state.brush = { color: '#000000' };
|
|
525
|
+
return true;
|
|
526
|
+
case STOCK_OBJECT.NULL_BRUSH:
|
|
527
|
+
ctx.state.brush = { color: 'none', none: true };
|
|
528
|
+
return true;
|
|
529
|
+
case STOCK_OBJECT.WHITE_PEN:
|
|
530
|
+
ctx.state.pen = { color: '#ffffff', width: 1 };
|
|
531
|
+
return true;
|
|
532
|
+
case STOCK_OBJECT.BLACK_PEN:
|
|
533
|
+
ctx.state.pen = { color: '#000000', width: 1 };
|
|
534
|
+
return true;
|
|
535
|
+
case STOCK_OBJECT.NULL_PEN:
|
|
536
|
+
ctx.state.pen = { color: 'none', width: 1, none: true };
|
|
537
|
+
return true;
|
|
538
|
+
default:
|
|
539
|
+
return false;
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
function selectObject(ctx, index) {
|
|
543
|
+
if (selectStockObject(ctx, index))
|
|
544
|
+
return;
|
|
545
|
+
const object = ctx.objects[index] || null;
|
|
546
|
+
if (!object)
|
|
547
|
+
return;
|
|
548
|
+
if (object.kind === 'pen')
|
|
549
|
+
ctx.state.pen = { ...object.value };
|
|
550
|
+
if (object.kind === 'brush')
|
|
551
|
+
ctx.state.brush = { ...object.value };
|
|
552
|
+
if (object.kind === 'font')
|
|
553
|
+
ctx.state.font = { ...object.value };
|
|
554
|
+
}
|
|
555
|
+
function addNode(ctx, svg, points) {
|
|
556
|
+
if (ctx.state.clips.length)
|
|
557
|
+
svg = ctx.state.clips.map(c => `<g ${c.mask ? 'mask' : 'clip-path'}="url(#${c.id})">`).join('') + svg + '</g>'.repeat(ctx.state.clips.length);
|
|
558
|
+
const bytes = utf8Length(svg) + 1;
|
|
559
|
+
if (bytes > ctx.limits.maxOutputBytes - ctx.outputBytes)
|
|
560
|
+
throw new PptError('LIMIT_ASSETS', 'Converted metafile exceeds the remaining asset byte budget');
|
|
561
|
+
ctx.outputBytes += bytes;
|
|
562
|
+
ctx.nodes.push(svg);
|
|
563
|
+
updateBounds(ctx, points);
|
|
564
|
+
}
|
|
565
|
+
function addPathFragment(ctx, fragment, points) {
|
|
566
|
+
if (!fragment)
|
|
567
|
+
return;
|
|
568
|
+
const bytes = utf8Length(fragment) + 1;
|
|
569
|
+
if (bytes > ctx.limits.maxOutputBytes - ctx.outputBytes - ctx.pathBytes)
|
|
570
|
+
throw new PptError('LIMIT_ASSETS', 'Metafile path exceeds the remaining asset byte budget');
|
|
571
|
+
ctx.pathBytes += bytes;
|
|
572
|
+
ctx.pathData.push(fragment);
|
|
573
|
+
for (const point of points)
|
|
574
|
+
ctx.pathPoints.push(point);
|
|
575
|
+
}
|
|
576
|
+
function flushPath(ctx, attrs) {
|
|
577
|
+
if (!ctx.pathData.length)
|
|
578
|
+
return;
|
|
579
|
+
addNode(ctx, `<path d="${ctx.pathData.join(' ')}" ${attrs} />`, ctx.pathPoints);
|
|
580
|
+
ctx.pathData = [];
|
|
581
|
+
ctx.pathPoints = [];
|
|
582
|
+
ctx.pathBytes = 0;
|
|
583
|
+
ctx.capturePath = false;
|
|
584
|
+
}
|
|
585
|
+
function addLine(ctx, start, end) {
|
|
586
|
+
const a = mapPoint(ctx, start);
|
|
587
|
+
const b = mapPoint(ctx, end);
|
|
588
|
+
ctx.state.currentPosition = { ...end };
|
|
589
|
+
if (ctx.capturePath) {
|
|
590
|
+
addPathFragment(ctx, `${ctx.pathData.length ? '' : `M ${pointAttr(a)}`} L ${pointAttr(b)}`.trim(), [a, b]);
|
|
591
|
+
return;
|
|
592
|
+
}
|
|
593
|
+
addNode(ctx, `<line x1="${numberCss(a.x)}" y1="${numberCss(a.y)}" x2="${numberCss(b.x)}" y2="${numberCss(b.y)}" ${strokeAttributes(ctx, ctx.state.pen)} />`, [a, b]);
|
|
594
|
+
}
|
|
595
|
+
function addPolyline(ctx, points, closed) {
|
|
596
|
+
if (!points.length)
|
|
597
|
+
return;
|
|
598
|
+
const mapped = points.map((point) => mapPoint(ctx, point));
|
|
599
|
+
ctx.state.currentPosition = { ...points[points.length - 1] };
|
|
600
|
+
const attr = mapped.map(pointAttr).join(' ');
|
|
601
|
+
if (ctx.capturePath) {
|
|
602
|
+
const commands = [`M ${pointAttr(mapped[0])}`, ...mapped.slice(1).map((point) => `L ${pointAttr(point)}`)];
|
|
603
|
+
if (closed)
|
|
604
|
+
commands.push('Z');
|
|
605
|
+
addPathFragment(ctx, commands.join(' '), mapped);
|
|
606
|
+
return;
|
|
607
|
+
}
|
|
608
|
+
const tag = closed ? 'polygon' : 'polyline';
|
|
609
|
+
const styleAttrs = closed ? fillAttributes(ctx) : strokeAttributes(ctx, ctx.state.pen);
|
|
610
|
+
addNode(ctx, `<${tag} points="${attr}" ${styleAttrs} />`, mapped);
|
|
611
|
+
}
|
|
612
|
+
function addCompoundPolygons(ctx, polygons) {
|
|
613
|
+
const points = [], parts = [];
|
|
614
|
+
for (const polygon of polygons) {
|
|
615
|
+
if (!polygon.length)
|
|
616
|
+
continue;
|
|
617
|
+
const mapped = polygon.map(p => mapPoint(ctx, p));
|
|
618
|
+
parts.push(`M${mapped.map(pointAttr).join('L')}Z`);
|
|
619
|
+
for (const point of mapped)
|
|
620
|
+
points.push(point);
|
|
621
|
+
}
|
|
622
|
+
if (!parts.length)
|
|
623
|
+
return;
|
|
624
|
+
if (ctx.capturePath)
|
|
625
|
+
addPathFragment(ctx, parts.join(' '), points);
|
|
626
|
+
else
|
|
627
|
+
addNode(ctx, `<path d="${parts.join(' ')}" ${fillAttributes(ctx)}/>`, points);
|
|
628
|
+
}
|
|
629
|
+
function addBezierPath(ctx, points, relativeToCurrent = false) {
|
|
630
|
+
const logical = relativeToCurrent ? [{ ...ctx.state.currentPosition }, ...points] : points;
|
|
631
|
+
if (logical.length < 4)
|
|
632
|
+
return;
|
|
633
|
+
const mapped = logical.map((point) => mapPoint(ctx, point));
|
|
634
|
+
let path = `M ${pointAttr(mapped[0])}`;
|
|
635
|
+
for (let i = 1; i + 2 < mapped.length; i += 3) {
|
|
636
|
+
path += ` C ${pointAttr(mapped[i])} ${pointAttr(mapped[i + 1])} ${pointAttr(mapped[i + 2])}`;
|
|
637
|
+
}
|
|
638
|
+
ctx.state.currentPosition = { ...logical[logical.length - 1] };
|
|
639
|
+
if (ctx.capturePath) {
|
|
640
|
+
addPathFragment(ctx, path, mapped);
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
addNode(ctx, `<path d="${path}" ${strokeAttributes(ctx, ctx.state.pen)} />`, mapped);
|
|
644
|
+
}
|
|
645
|
+
function addRect(ctx, rect, radius) {
|
|
646
|
+
if (ctx.capturePath || ctx.state.worldTransform.m12 !== 0 || ctx.state.worldTransform.m21 !== 0) {
|
|
647
|
+
const r = normalizeRect(rect), rx = Math.min((r.right - r.left) / 2, Math.abs(radius?.x ?? 0) / 2), ry = Math.min((r.bottom - r.top) / 2, Math.abs(radius?.y ?? 0) / 2), points = [], map = (x, y) => { const p = mapPoint(ctx, { x, y }); points.push(p); return pointAttr(p); };
|
|
648
|
+
let path;
|
|
649
|
+
if (rx && ry) {
|
|
650
|
+
const k = .5522847498307936;
|
|
651
|
+
path = `M${map(r.left + rx, r.top)} L${map(r.right - rx, r.top)} C${map(r.right - rx + k * rx, r.top)} ${map(r.right, r.top + ry - k * ry)} ${map(r.right, r.top + ry)} L${map(r.right, r.bottom - ry)} C${map(r.right, r.bottom - ry + k * ry)} ${map(r.right - rx + k * rx, r.bottom)} ${map(r.right - rx, r.bottom)} L${map(r.left + rx, r.bottom)} C${map(r.left + rx - k * rx, r.bottom)} ${map(r.left, r.bottom - ry + k * ry)} ${map(r.left, r.bottom - ry)} L${map(r.left, r.top + ry)} C${map(r.left, r.top + ry - k * ry)} ${map(r.left + rx - k * rx, r.top)} ${map(r.left + rx, r.top)} Z`;
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
const outline = rectanglePath(ctx, r);
|
|
655
|
+
path = outline.path;
|
|
656
|
+
points.push(...outline.points);
|
|
657
|
+
}
|
|
658
|
+
claimPoints(ctx, points.length);
|
|
659
|
+
if (ctx.capturePath)
|
|
660
|
+
addPathFragment(ctx, path, points);
|
|
661
|
+
else
|
|
662
|
+
addNode(ctx, `<path d="${path}" ${fillAttributes(ctx)}/>`, points);
|
|
663
|
+
return;
|
|
664
|
+
}
|
|
665
|
+
const mapped = mapRect(ctx, rect);
|
|
666
|
+
const attrs = [
|
|
667
|
+
`x="${numberCss(mapped.left)}"`,
|
|
668
|
+
`y="${numberCss(mapped.top)}"`,
|
|
669
|
+
`width="${numberCss(rectWidth(mapped))}"`,
|
|
670
|
+
`height="${numberCss(rectHeight(mapped))}"`,
|
|
671
|
+
];
|
|
672
|
+
if (radius) {
|
|
673
|
+
const rx = mapVector(ctx, { x: radius.x / 2, y: 0 }), ry = mapVector(ctx, { x: 0, y: radius.y / 2 });
|
|
674
|
+
attrs.push(`rx="${numberCss(Math.hypot(rx.x, rx.y))}"`);
|
|
675
|
+
attrs.push(`ry="${numberCss(Math.hypot(ry.x, ry.y))}"`);
|
|
676
|
+
}
|
|
677
|
+
if (ctx.capturePath) {
|
|
678
|
+
addPathFragment(ctx, `M ${numberCss(mapped.left)},${numberCss(mapped.top)} L ${numberCss(mapped.right)},${numberCss(mapped.top)} L ${numberCss(mapped.right)},${numberCss(mapped.bottom)} L ${numberCss(mapped.left)},${numberCss(mapped.bottom)} Z`, [
|
|
679
|
+
{ x: mapped.left, y: mapped.top },
|
|
680
|
+
{ x: mapped.right, y: mapped.bottom },
|
|
681
|
+
]);
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
addNode(ctx, `<rect ${attrs.join(' ')} ${fillAttributes(ctx)} />`, [
|
|
685
|
+
{ x: mapped.left, y: mapped.top },
|
|
686
|
+
{ x: mapped.right, y: mapped.bottom },
|
|
687
|
+
]);
|
|
688
|
+
}
|
|
689
|
+
function addEllipse(ctx, rect) {
|
|
690
|
+
if (ctx.capturePath || ctx.state.worldTransform.m12 !== 0 || ctx.state.worldTransform.m21 !== 0) {
|
|
691
|
+
addArc(ctx, rect, { x: rect.right, y: (rect.top + rect.bottom) / 2 }, { x: rect.right, y: (rect.top + rect.bottom) / 2 }, 'chord');
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
const mapped = mapRect(ctx, rect);
|
|
695
|
+
const cx = (mapped.left + mapped.right) / 2;
|
|
696
|
+
const cy = (mapped.top + mapped.bottom) / 2;
|
|
697
|
+
const rx = rectWidth(mapped) / 2;
|
|
698
|
+
const ry = rectHeight(mapped) / 2;
|
|
699
|
+
if (ctx.capturePath) {
|
|
700
|
+
addPathFragment(ctx, `M ${numberCss(cx - rx)},${numberCss(cy)} A ${numberCss(rx)},${numberCss(ry)} 0 1 0 ${numberCss(cx + rx)},${numberCss(cy)} A ${numberCss(rx)},${numberCss(ry)} 0 1 0 ${numberCss(cx - rx)},${numberCss(cy)} Z`, [
|
|
701
|
+
{ x: mapped.left, y: mapped.top },
|
|
702
|
+
{ x: mapped.right, y: mapped.bottom },
|
|
703
|
+
]);
|
|
704
|
+
return;
|
|
705
|
+
}
|
|
706
|
+
addNode(ctx, `<ellipse cx="${numberCss(cx)}" cy="${numberCss(cy)}" rx="${numberCss(rx)}" ry="${numberCss(ry)}" ${fillAttributes(ctx)} />`, [
|
|
707
|
+
{ x: mapped.left, y: mapped.top },
|
|
708
|
+
{ x: mapped.right, y: mapped.bottom },
|
|
709
|
+
]);
|
|
710
|
+
}
|
|
711
|
+
/** Cubic quarter-arcs commute with affine world transforms, unlike mapped bounding boxes. */
|
|
712
|
+
function addArc(ctx, rect, start, end, kind) {
|
|
713
|
+
const box = normalizeRect(rect), rx = (box.right - box.left) / 2, ry = (box.bottom - box.top) / 2, cx = (box.left + box.right) / 2, cy = (box.top + box.bottom) / 2;
|
|
714
|
+
if (!rx || !ry)
|
|
715
|
+
return;
|
|
716
|
+
const theta = Math.atan2((start.y - cy) / ry, (start.x - cx) / rx), finish = Math.atan2((end.y - cy) / ry, (end.x - cx) / rx), clockwise = ctx.state.arcDirection === 2, tau = Math.PI * 2;
|
|
717
|
+
let delta = ((clockwise ? finish - theta : theta - finish) % tau + tau) % tau;
|
|
718
|
+
if (delta < 1e-10)
|
|
719
|
+
delta = tau;
|
|
720
|
+
if (!clockwise)
|
|
721
|
+
delta = -delta;
|
|
722
|
+
const steps = Math.max(1, Math.ceil(Math.abs(delta) / (Math.PI / 2))), points = [], logical = (a) => ({ x: cx + rx * Math.cos(a), y: cy + ry * Math.sin(a) }), map = (p) => { const transformed = mapPoint(ctx, p); points.push(transformed); return pointAttr(transformed); }, first = logical(theta);
|
|
723
|
+
let path = kind === 'pie' ? `M${map({ x: cx, y: cy })} L${map(first)}` : kind === 'arcTo' ? `M${map(ctx.state.currentPosition)} L${map(first)}` : `M${map(first)}`;
|
|
724
|
+
for (let i = 0; i < steps; i++) {
|
|
725
|
+
const a = theta + delta * i / steps, b = theta + delta * (i + 1) / steps, k = 4 / 3 * Math.tan((b - a) / 4), p0 = logical(a), p3 = logical(b), p1 = { x: p0.x - k * rx * Math.sin(a), y: p0.y + k * ry * Math.cos(a) }, p2 = { x: p3.x + k * rx * Math.sin(b), y: p3.y - k * ry * Math.cos(b) };
|
|
726
|
+
path += ` C${map(p1)} ${map(p2)} ${map(p3)}`;
|
|
727
|
+
}
|
|
728
|
+
if (kind === 'chord' || kind === 'pie')
|
|
729
|
+
path += ' Z';
|
|
730
|
+
claimPoints(ctx, points.length);
|
|
731
|
+
if (ctx.capturePath)
|
|
732
|
+
addPathFragment(ctx, path, points);
|
|
733
|
+
else
|
|
734
|
+
addNode(ctx, `<path d="${path}" ${kind === 'arc' || kind === 'arcTo' ? strokeAttributes(ctx, ctx.state.pen) : fillAttributes(ctx)}/>`, points);
|
|
735
|
+
if (kind === 'arcTo')
|
|
736
|
+
ctx.state.currentPosition = logical(theta + delta);
|
|
737
|
+
}
|
|
738
|
+
/** Clip in the destination's unit square before its affine mapping. Mapping a
|
|
739
|
+
* bitmap's bounding box would discard rotation, shear and mirror directions. */
|
|
740
|
+
function addRasterImage(ctx, dest, image, source, sourceTransform = IDENTITY_MATRIX, opacity = 1) {
|
|
741
|
+
const sw = source.right - source.left, sh = source.bottom - source.top, dw = dest.right - dest.left, dh = dest.bottom - dest.top;
|
|
742
|
+
if (!sw || !sh || !dw || !dh || opacity === 0)
|
|
743
|
+
return;
|
|
744
|
+
const { m11: a, m12: b, m21: c, m22: d, dx: tx, dy: ty } = sourceTransform, det = a * d - b * c;
|
|
745
|
+
if (![a, b, c, d, tx, ty, det].every(Number.isFinite) || !det)
|
|
746
|
+
throw new PptError('METAFILE_BITMAP_TRANSFORM', 'Invalid or singular source bitmap transform');
|
|
747
|
+
const inverse = { m11: d / det, m12: -b / det, m21: -c / det, m22: a / det, dx: (c * ty - d * tx) / det, dy: (b * tx - a * ty) / det };
|
|
748
|
+
const sampling = [inverse.m11 / sw, inverse.m12 / sh, inverse.m21 / sw, inverse.m22 / sh, (inverse.dx - source.left) / sw, (inverse.dy - source.top) / sh];
|
|
749
|
+
const p = mapPoint(ctx, { x: dest.left, y: dest.top }), vx = mapVector(ctx, { x: dw, y: 0 }), vy = mapVector(ctx, { x: 0, y: dh });
|
|
750
|
+
if (Math.ceil(image.bytes.length / 3) * 4 + 700 > ctx.limits.maxOutputBytes - ctx.outputBytes)
|
|
751
|
+
throw new PptError('LIMIT_ASSETS', 'Metafile bitmap exceeds the remaining SVG output budget');
|
|
752
|
+
const href = dataUrlFromBytes(image.bytes, image.mime), transform = [vx.x, vx.y, vy.x, vy.y, p.x, p.y].map(numberCss).join(' ');
|
|
753
|
+
const svg = `<g transform="matrix(${transform})"${opacity < 1 ? ` opacity="${opacity}"` : ''}><svg x="0" y="0" width="1" height="1" viewBox="0 0 1 1" preserveAspectRatio="none" overflow="hidden"><image x="0" y="0" width="${image.width}" height="${image.height}" transform="matrix(${sampling.map(value => String(Number(value.toPrecision(12)))).join(' ')})" xlink:href="${href}" preserveAspectRatio="none" /></svg></g>`;
|
|
754
|
+
addNode(ctx, svg, [p, { x: p.x + vx.x, y: p.y + vx.y }, { x: p.x + vy.x, y: p.y + vy.y }, { x: p.x + vx.x + vy.x, y: p.y + vx.y + vy.y }]);
|
|
755
|
+
}
|
|
756
|
+
function decodeAnsi(bytes, charset = 0) {
|
|
757
|
+
const labels = { 0: 'windows-1252', 128: 'shift_jis', 129: 'euc-kr', 134: 'gbk', 136: 'big5', 161: 'windows-1253', 162: 'windows-1254', 163: 'windows-1258', 177: 'windows-1255', 178: 'windows-1256', 186: 'windows-1257', 204: 'windows-1251', 222: 'windows-874', 238: 'windows-1250' };
|
|
758
|
+
return trimTrailingNulls(new TextDecoder(labels[charset] ?? 'windows-1252').decode(bytes));
|
|
759
|
+
}
|
|
760
|
+
function decodeUtf16(bytes) {
|
|
761
|
+
return trimTrailingNulls(new TextDecoder('utf-16le').decode(bytes));
|
|
762
|
+
}
|
|
763
|
+
function textAnchor(textAlign) {
|
|
764
|
+
const horizontal = textAlign & 0x0006;
|
|
765
|
+
if (horizontal === 0x0002)
|
|
766
|
+
return 'end';
|
|
767
|
+
if (horizontal === 0x0006)
|
|
768
|
+
return 'middle';
|
|
769
|
+
return 'start';
|
|
770
|
+
}
|
|
771
|
+
function dominantBaseline(textAlign) {
|
|
772
|
+
const vertical = textAlign & 0x0018;
|
|
773
|
+
if (vertical === 0x0008)
|
|
774
|
+
return 'text-after-edge';
|
|
775
|
+
if (vertical === 0x0018)
|
|
776
|
+
return 'alphabetic';
|
|
777
|
+
return 'text-before-edge';
|
|
778
|
+
}
|
|
779
|
+
function addText(ctx, point, text, glyphIndices, textOptions = {}) {
|
|
780
|
+
if (!text && !glyphIndices?.length && !(textOptions.rectangle && ((textOptions.options ?? 0) & 2)))
|
|
781
|
+
return;
|
|
782
|
+
if (text.length * (textOptions.advances ? 96 : 6) + (glyphIndices?.length ?? 0) * 8 > ctx.limits.maxOutputBytes - ctx.outputBytes)
|
|
783
|
+
throw new PptError('LIMIT_ASSETS', 'Metafile text exceeds the remaining SVG output budget');
|
|
784
|
+
const logical = ctx.state.textAlign & 1 ? ctx.state.currentPosition : point, mapped = mapPoint(ctx, logical), angle = -(ctx.state.font.escapement ?? 0) * Math.PI / 1800, cos = Math.cos(angle), sin = Math.sin(angle), vx = mapVector(ctx, { x: cos, y: sin }), vy = mapVector(ctx, { x: -sin, y: cos });
|
|
785
|
+
const fontSize = Math.max(1, Math.abs(ctx.state.font.size) || 12), options = textOptions.options ?? 0;
|
|
786
|
+
const previousClips = [...ctx.state.clips];
|
|
787
|
+
if (textOptions.rectangle && (options & 2)) {
|
|
788
|
+
const r = rectanglePath(ctx, textOptions.rectangle);
|
|
789
|
+
addNode(ctx, `<path d="${r.path}" fill="${ctx.state.bkColor}" stroke="none"/>`, r.points);
|
|
790
|
+
}
|
|
791
|
+
if (textOptions.rectangle && (options & 4))
|
|
792
|
+
clipPath(ctx, rectanglePath(ctx, textOptions.rectangle).path, 1);
|
|
793
|
+
const attrs = [
|
|
794
|
+
'x="0"', 'y="0"',
|
|
795
|
+
`transform="matrix(${[vx.x, vx.y, vy.x, vy.y, mapped.x, mapped.y].map(numberCss).join(' ')})"`,
|
|
796
|
+
`fill="${escapeHtml(ctx.state.textColor)}"`,
|
|
797
|
+
`font-size="${numberCss(fontSize)}"`,
|
|
798
|
+
`font-family="${escapeXmlText(ctx.state.font.family)}"`,
|
|
799
|
+
`text-anchor="${textOptions.advances ? 'start' : textAnchor(ctx.state.textAlign)}"`,
|
|
800
|
+
`dominant-baseline="${dominantBaseline(ctx.state.textAlign)}"`,
|
|
801
|
+
];
|
|
802
|
+
if (options & 0x80)
|
|
803
|
+
attrs.push('direction="rtl" unicode-bidi="bidi-override"');
|
|
804
|
+
if (ctx.state.font.weight && ctx.state.font.weight >= 600)
|
|
805
|
+
attrs.push('font-weight="700"');
|
|
806
|
+
if (ctx.state.font.italic)
|
|
807
|
+
attrs.push('font-style="italic"');
|
|
808
|
+
const decorations = [ctx.state.font.underline ? 'underline' : '', ctx.state.font.strike ? 'line-through' : ''].filter(Boolean);
|
|
809
|
+
if (decorations.length)
|
|
810
|
+
attrs.push(`text-decoration="${decorations.join(' ')}"`);
|
|
811
|
+
if (glyphIndices?.length) {
|
|
812
|
+
// Font-specific glyph IDs are not Unicode; retain them without painting false text.
|
|
813
|
+
attrs.push(`data-msdoc-glyph-indices="${glyphIndices.join(' ')}"`);
|
|
814
|
+
ctx.unresolvedGlyphRuns += 1;
|
|
815
|
+
}
|
|
816
|
+
let body = glyphIndices ? '' : escapeXmlText(text), extent = { x: 0, y: 0 };
|
|
817
|
+
if (textOptions.advances) {
|
|
818
|
+
extent = textOptions.advances.reduce((p, d) => ({ x: p.x + d.x, y: p.y + d.y }), { x: 0, y: 0 });
|
|
819
|
+
const alignment = textAnchor(ctx.state.textAlign), factor = alignment === 'middle' ? .5 : alignment === 'end' ? 1 : 0;
|
|
820
|
+
let x = -extent.x * factor, y = -extent.y * factor, index = 0;
|
|
821
|
+
const spans = [];
|
|
822
|
+
for (const character of Array.from(text)) {
|
|
823
|
+
spans.push(`<tspan x="${numberCss(x)}" y="${numberCss(y)}">${escapeXmlText(character)}</tspan>`);
|
|
824
|
+
for (let k = 0; k < character.length; k++) {
|
|
825
|
+
const d = textOptions.advances[index++];
|
|
826
|
+
if (d) {
|
|
827
|
+
x += d.x;
|
|
828
|
+
y += d.y;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
}
|
|
832
|
+
if (!glyphIndices)
|
|
833
|
+
body = spans.join('');
|
|
834
|
+
}
|
|
835
|
+
else if (ctx.state.textAlign & 1) {
|
|
836
|
+
extent.x = Array.from(text).length * fontSize * .55;
|
|
837
|
+
ctx.unsupportedRecordTypes.add(ctx.activeRecordType);
|
|
838
|
+
}
|
|
839
|
+
if (ctx.state.font.charset === 2 || ctx.state.font.charset === 255 || ctx.state.font.charset === 130)
|
|
840
|
+
ctx.unsupportedRecordTypes.add(ctx.activeRecordType);
|
|
841
|
+
addNode(ctx, `<text ${attrs.join(' ')}>${body}</text>`, [mapped]);
|
|
842
|
+
ctx.state.clips = previousClips;
|
|
843
|
+
if (ctx.state.textAlign & 1)
|
|
844
|
+
ctx.state.currentPosition = { x: logical.x + extent.x * cos - extent.y * sin, y: logical.y + extent.x * sin + extent.y * cos };
|
|
845
|
+
}
|
|
846
|
+
function finalizeSvg(ctx, sourceMime) {
|
|
847
|
+
if (!ctx.nodes.length)
|
|
848
|
+
return null;
|
|
849
|
+
const fallback = ctx.contentBounds ? normalizeRect(ctx.contentBounds) : normalizeRect(ctx.viewBox);
|
|
850
|
+
const viewBox = normalizeRect({
|
|
851
|
+
left: Number.isFinite(ctx.viewBox.left) ? ctx.viewBox.left : fallback.left,
|
|
852
|
+
top: Number.isFinite(ctx.viewBox.top) ? ctx.viewBox.top : fallback.top,
|
|
853
|
+
right: Number.isFinite(ctx.viewBox.right) ? ctx.viewBox.right : fallback.right,
|
|
854
|
+
bottom: Number.isFinite(ctx.viewBox.bottom) ? ctx.viewBox.bottom : fallback.bottom,
|
|
855
|
+
});
|
|
856
|
+
const width = Math.max(1, Math.round(rectWidth(viewBox)));
|
|
857
|
+
const height = Math.max(1, Math.round(rectHeight(viewBox)));
|
|
858
|
+
const start = `<?xml version="1.0" encoding="UTF-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="${width}" height="${height}" viewBox="${numberCss(viewBox.left)} ${numberCss(viewBox.top)} ${numberCss(rectWidth(viewBox))} ${numberCss(rectHeight(viewBox))}" preserveAspectRatio="xMinYMin meet">\n`;
|
|
859
|
+
if (utf8Length(start) + ctx.outputBytes + 6 + (ctx.defs.length ? 14 : 0) > ctx.limits.maxOutputBytes)
|
|
860
|
+
throw new PptError('LIMIT_ASSETS', 'Converted SVG exceeds the remaining asset byte budget');
|
|
861
|
+
const svg = `${start}${ctx.defs.length ? `<defs>${ctx.defs.join('\n')}</defs>\n` : ''}${ctx.nodes.join('\n')}\n</svg>`;
|
|
862
|
+
const encoded = new TextEncoder().encode(svg);
|
|
863
|
+
return {
|
|
864
|
+
mime: 'image/svg+xml',
|
|
865
|
+
bytes: encoded,
|
|
866
|
+
dataUrl: dataUrlFromBytes(encoded, 'image/svg+xml'),
|
|
867
|
+
width,
|
|
868
|
+
height,
|
|
869
|
+
sourceMime,
|
|
870
|
+
recordCount: ctx.recordCount,
|
|
871
|
+
unresolvedGlyphRuns: ctx.unresolvedGlyphRuns,
|
|
872
|
+
unsupportedRecordTypes: [...ctx.unsupportedRecordTypes].sort((a, b) => a - b),
|
|
873
|
+
};
|
|
874
|
+
}
|
|
875
|
+
function parseWmf(bytes, limits) {
|
|
876
|
+
const reader = new BinaryReader(bytes);
|
|
877
|
+
let offset = 0;
|
|
878
|
+
let placeableBounds = null;
|
|
879
|
+
if (reader.u32(0) === PLACEABLE_WMF_KEY && bytes.length >= 22) {
|
|
880
|
+
placeableBounds = normalizeRect({
|
|
881
|
+
left: reader.i16(6),
|
|
882
|
+
top: reader.i16(8),
|
|
883
|
+
right: reader.i16(10),
|
|
884
|
+
bottom: reader.i16(12),
|
|
885
|
+
});
|
|
886
|
+
offset = 22;
|
|
887
|
+
}
|
|
888
|
+
if (offset + WMF_HEADER_SIZE > bytes.length)
|
|
889
|
+
return null;
|
|
890
|
+
const header = new BinaryReader(bytes.subarray(offset));
|
|
891
|
+
const headerSizeWords = header.u16(2);
|
|
892
|
+
const numberOfObjects = header.u16(10);
|
|
893
|
+
const recordOffset = offset + headerSizeWords * 2;
|
|
894
|
+
if (headerSizeWords < 9 || recordOffset > bytes.length)
|
|
895
|
+
throw new PptError('METAFILE_HEADER', 'Invalid WMF header length');
|
|
896
|
+
const viewBox = placeableBounds || { left: 0, top: 0, right: 1000, bottom: 1000 };
|
|
897
|
+
const ctx = defaultContext(rectWidth(viewBox), rectHeight(viewBox), viewBox, limits, numberOfObjects);
|
|
898
|
+
if (placeableBounds) {
|
|
899
|
+
ctx.state.windowOrg = { x: placeableBounds.left, y: placeableBounds.top };
|
|
900
|
+
ctx.state.windowExt = { x: rectWidth(placeableBounds), y: rectHeight(placeableBounds) };
|
|
901
|
+
ctx.state.viewportOrg = { x: placeableBounds.left, y: placeableBounds.top };
|
|
902
|
+
ctx.state.viewportExt = { x: rectWidth(placeableBounds), y: rectHeight(placeableBounds) };
|
|
903
|
+
}
|
|
904
|
+
let cursor = recordOffset;
|
|
905
|
+
while (cursor + 6 <= bytes.length) {
|
|
906
|
+
const sizeWords = reader.u32(cursor);
|
|
907
|
+
const recordSize = sizeWords * 2;
|
|
908
|
+
if (recordSize < 6 || cursor + recordSize > bytes.length)
|
|
909
|
+
throw new PptError('METAFILE_RECORD_LENGTH', 'WMF record exceeds its payload', cursor);
|
|
910
|
+
const recordReader = new BinaryReader(bytes.subarray(cursor, cursor + recordSize));
|
|
911
|
+
const fn = recordReader.u16(4);
|
|
912
|
+
if (recordSize < (WMF_MIN_BYTES[fn] ?? 6))
|
|
913
|
+
throw new PptError('METAFILE_RECORD_LENGTH', `WMF record 0x${fn.toString(16)} has a truncated fixed prefix`, cursor);
|
|
914
|
+
countRecord(ctx);
|
|
915
|
+
ctx.activeRecordType = fn;
|
|
916
|
+
switch (fn) {
|
|
917
|
+
case WMF_RECORD.META_EOF:
|
|
918
|
+
cursor += recordSize;
|
|
919
|
+
return finalizeSvg(ctx, 'image/wmf');
|
|
920
|
+
case WMF_RECORD.META_SAVEDC:
|
|
921
|
+
saveState(ctx);
|
|
922
|
+
break;
|
|
923
|
+
case WMF_RECORD.META_RESTOREDC:
|
|
924
|
+
restoreState(ctx, recordReader.i16(6));
|
|
925
|
+
break;
|
|
926
|
+
case WMF_RECORD.META_SETTEXTALIGN:
|
|
927
|
+
ctx.state.textAlign = recordReader.u16(6);
|
|
928
|
+
break;
|
|
929
|
+
case WMF_RECORD.META_INTERSECTCLIPRECT:
|
|
930
|
+
case WMF_RECORD.META_EXCLUDECLIPRECT: {
|
|
931
|
+
const r = { left: recordReader.i16(12), top: recordReader.i16(10), right: recordReader.i16(8), bottom: recordReader.i16(6) };
|
|
932
|
+
clipPath(ctx, rectanglePath(ctx, r).path, fn === WMF_RECORD.META_INTERSECTCLIPRECT ? 1 : 4);
|
|
933
|
+
break;
|
|
934
|
+
}
|
|
935
|
+
case WMF_RECORD.META_SETWINDOWORG:
|
|
936
|
+
ctx.state.windowOrg = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
937
|
+
break;
|
|
938
|
+
case WMF_RECORD.META_SETWINDOWEXT:
|
|
939
|
+
ctx.state.windowExt = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
940
|
+
break;
|
|
941
|
+
case WMF_RECORD.META_SETVIEWPORTORG:
|
|
942
|
+
ctx.state.viewportOrg = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
943
|
+
break;
|
|
944
|
+
case WMF_RECORD.META_SETVIEWPORTEXT:
|
|
945
|
+
ctx.state.viewportExt = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
946
|
+
break;
|
|
947
|
+
case WMF_RECORD.META_OFFSETWINDOWORG:
|
|
948
|
+
ctx.state.windowOrg = { x: ctx.state.windowOrg.x + recordReader.i16(8), y: ctx.state.windowOrg.y + recordReader.i16(6) };
|
|
949
|
+
break;
|
|
950
|
+
case WMF_RECORD.META_OFFSETVIEWPORTORG:
|
|
951
|
+
ctx.state.viewportOrg = { x: ctx.state.viewportOrg.x + recordReader.i16(8), y: ctx.state.viewportOrg.y + recordReader.i16(6) };
|
|
952
|
+
break;
|
|
953
|
+
case WMF_RECORD.META_SETBKMODE:
|
|
954
|
+
ctx.state.bkMode = recordReader.u16(6);
|
|
955
|
+
break;
|
|
956
|
+
case WMF_RECORD.META_SETPOLYFILLMODE:
|
|
957
|
+
ctx.state.polygonFillMode = recordReader.u16(6);
|
|
958
|
+
break;
|
|
959
|
+
case WMF_RECORD.META_SETBKCOLOR:
|
|
960
|
+
ctx.state.bkColor = colorRefToCss(recordReader.u32(6));
|
|
961
|
+
break;
|
|
962
|
+
case WMF_RECORD.META_SETTEXTCOLOR:
|
|
963
|
+
ctx.state.textColor = colorRefToCss(recordReader.u32(6));
|
|
964
|
+
break;
|
|
965
|
+
case WMF_RECORD.META_MOVETO:
|
|
966
|
+
ctx.state.currentPosition = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
967
|
+
break;
|
|
968
|
+
case WMF_RECORD.META_LINETO: {
|
|
969
|
+
const end = { x: recordReader.i16(8), y: recordReader.i16(6) };
|
|
970
|
+
addLine(ctx, ctx.state.currentPosition, end);
|
|
971
|
+
break;
|
|
972
|
+
}
|
|
973
|
+
case WMF_RECORD.META_POLYLINE:
|
|
974
|
+
case WMF_RECORD.META_POLYGON: {
|
|
975
|
+
const count = recordReader.u16(6);
|
|
976
|
+
if (!recordReader.ensure(8, count * 4))
|
|
977
|
+
throw new PptError('METAFILE_POINTS', 'WMF point array exceeds its own record', cursor);
|
|
978
|
+
claimPoints(ctx, count);
|
|
979
|
+
const points = [];
|
|
980
|
+
let pointOffset = 8;
|
|
981
|
+
for (let i = 0; i < count && pointOffset + 4 <= recordSize; i += 1) {
|
|
982
|
+
points.push({ x: recordReader.i16(pointOffset), y: recordReader.i16(pointOffset + 2) });
|
|
983
|
+
pointOffset += 4;
|
|
984
|
+
}
|
|
985
|
+
addPolyline(ctx, points, fn === WMF_RECORD.META_POLYGON);
|
|
986
|
+
break;
|
|
987
|
+
}
|
|
988
|
+
case WMF_RECORD.META_POLYPOLYGON: {
|
|
989
|
+
const polygons = recordReader.u16(6);
|
|
990
|
+
if (!recordReader.ensure(8, polygons * 2))
|
|
991
|
+
throw new PptError('METAFILE_POINTS', 'WMF polygon counts exceed their own record', cursor);
|
|
992
|
+
const counts = [];
|
|
993
|
+
let dataOffset = 8;
|
|
994
|
+
for (let i = 0; i < polygons && dataOffset + 2 <= recordSize; i += 1) {
|
|
995
|
+
counts.push(recordReader.u16(dataOffset));
|
|
996
|
+
dataOffset += 2;
|
|
997
|
+
}
|
|
998
|
+
const total = counts.reduce((sum, count) => sum + count, 0);
|
|
999
|
+
if (!recordReader.ensure(dataOffset, total * 4))
|
|
1000
|
+
throw new PptError('METAFILE_POINTS', 'WMF polygon points exceed their own record', cursor);
|
|
1001
|
+
claimPoints(ctx, total);
|
|
1002
|
+
const polygonPoints = [];
|
|
1003
|
+
for (const count of counts) {
|
|
1004
|
+
const points = [];
|
|
1005
|
+
for (let i = 0; i < count && dataOffset + 4 <= recordSize; i += 1) {
|
|
1006
|
+
points.push({ x: recordReader.i16(dataOffset), y: recordReader.i16(dataOffset + 2) });
|
|
1007
|
+
dataOffset += 4;
|
|
1008
|
+
}
|
|
1009
|
+
polygonPoints.push(points);
|
|
1010
|
+
}
|
|
1011
|
+
addCompoundPolygons(ctx, polygonPoints);
|
|
1012
|
+
break;
|
|
1013
|
+
}
|
|
1014
|
+
case WMF_RECORD.META_RECTANGLE: {
|
|
1015
|
+
addRect(ctx, {
|
|
1016
|
+
left: recordReader.i16(12),
|
|
1017
|
+
top: recordReader.i16(10),
|
|
1018
|
+
right: recordReader.i16(8),
|
|
1019
|
+
bottom: recordReader.i16(6),
|
|
1020
|
+
});
|
|
1021
|
+
break;
|
|
1022
|
+
}
|
|
1023
|
+
case WMF_RECORD.META_ELLIPSE: {
|
|
1024
|
+
addEllipse(ctx, {
|
|
1025
|
+
left: recordReader.i16(12),
|
|
1026
|
+
top: recordReader.i16(10),
|
|
1027
|
+
right: recordReader.i16(8),
|
|
1028
|
+
bottom: recordReader.i16(6),
|
|
1029
|
+
});
|
|
1030
|
+
break;
|
|
1031
|
+
}
|
|
1032
|
+
case WMF_RECORD.META_ROUNDRECT: {
|
|
1033
|
+
addRect(ctx, {
|
|
1034
|
+
left: recordReader.i16(16),
|
|
1035
|
+
top: recordReader.i16(14),
|
|
1036
|
+
right: recordReader.i16(12),
|
|
1037
|
+
bottom: recordReader.i16(10),
|
|
1038
|
+
}, {
|
|
1039
|
+
x: recordReader.i16(8),
|
|
1040
|
+
y: recordReader.i16(6),
|
|
1041
|
+
});
|
|
1042
|
+
break;
|
|
1043
|
+
}
|
|
1044
|
+
case WMF_RECORD.META_ARC:
|
|
1045
|
+
case WMF_RECORD.META_CHORD:
|
|
1046
|
+
case WMF_RECORD.META_PIE: {
|
|
1047
|
+
const end = { x: recordReader.i16(8), y: recordReader.i16(6) }, start = { x: recordReader.i16(12), y: recordReader.i16(10) }, box = { left: recordReader.i16(20), top: recordReader.i16(18), right: recordReader.i16(16), bottom: recordReader.i16(14) };
|
|
1048
|
+
addArc(ctx, box, start, end, fn === WMF_RECORD.META_CHORD ? 'chord' : fn === WMF_RECORD.META_PIE ? 'pie' : 'arc');
|
|
1049
|
+
break;
|
|
1050
|
+
}
|
|
1051
|
+
case WMF_RECORD.META_TEXTOUT: {
|
|
1052
|
+
const length = recordReader.u16(6);
|
|
1053
|
+
const textOffset = 8;
|
|
1054
|
+
const paddedLength = length + (length % 2);
|
|
1055
|
+
if (!recordReader.ensure(textOffset, paddedLength + 4))
|
|
1056
|
+
throw new PptError('METAFILE_TEXT', 'WMF text exceeds its own record', cursor);
|
|
1057
|
+
const text = decodeAnsi(recordReader.slice(textOffset, Math.min(length, Math.max(0, recordSize - textOffset - 4))), ctx.state.font.charset);
|
|
1058
|
+
const yOffset = textOffset + paddedLength;
|
|
1059
|
+
const y = recordReader.i16(yOffset);
|
|
1060
|
+
const x = recordReader.i16(yOffset + 2);
|
|
1061
|
+
addText(ctx, { x, y }, text);
|
|
1062
|
+
break;
|
|
1063
|
+
}
|
|
1064
|
+
case WMF_RECORD.META_EXTTEXTOUT: {
|
|
1065
|
+
const point = { x: recordReader.i16(8), y: recordReader.i16(6) }, length = recordReader.i16(10), options = recordReader.u16(12), hasRect = (options & 6) !== 0, textOffset = hasRect ? 22 : 14;
|
|
1066
|
+
if (length < 0 || !recordReader.ensure(textOffset, length + (length % 2)))
|
|
1067
|
+
throw new PptError('METAFILE_TEXT', 'WMF extended text exceeds its own record', cursor);
|
|
1068
|
+
const rectangle = hasRect ? { left: recordReader.i16(14), top: recordReader.i16(16), right: recordReader.i16(18), bottom: recordReader.i16(20) } : undefined;
|
|
1069
|
+
const dxOffset = textOffset + length + (length % 2);
|
|
1070
|
+
let advances;
|
|
1071
|
+
if (dxOffset < recordSize) {
|
|
1072
|
+
if (!recordReader.ensure(dxOffset, length * 2))
|
|
1073
|
+
throw new PptError('METAFILE_TEXT', 'WMF character spacing exceeds its own record', cursor);
|
|
1074
|
+
claimPoints(ctx, length);
|
|
1075
|
+
advances = Array.from({ length }, (_, i) => ({ x: recordReader.i16(dxOffset + i * 2), y: 0 }));
|
|
1076
|
+
}
|
|
1077
|
+
const text = decodeAnsi(recordReader.slice(textOffset, length), ctx.state.font.charset);
|
|
1078
|
+
addText(ctx, point, text, undefined, { advances, rectangle, options });
|
|
1079
|
+
break;
|
|
1080
|
+
}
|
|
1081
|
+
case WMF_RECORD.META_CREATEPENINDIRECT: {
|
|
1082
|
+
const style = recordReader.u16(6);
|
|
1083
|
+
const width = recordReader.i16(8);
|
|
1084
|
+
const color = colorRefToCss(recordReader.u32(12));
|
|
1085
|
+
const index = ensureObjectSlot(ctx);
|
|
1086
|
+
setObject(ctx, index, { kind: 'pen', value: penStyleFromCode(style, width, color) });
|
|
1087
|
+
break;
|
|
1088
|
+
}
|
|
1089
|
+
case WMF_RECORD.META_CREATEBRUSHINDIRECT: {
|
|
1090
|
+
const style = recordReader.u16(6);
|
|
1091
|
+
if (style > 2 || style === 2 && recordReader.u16(12) > 5)
|
|
1092
|
+
ctx.unsupportedRecordTypes.add(fn);
|
|
1093
|
+
const color = colorRefToCss(recordReader.u32(8));
|
|
1094
|
+
const index = ensureObjectSlot(ctx);
|
|
1095
|
+
setObject(ctx, index, { kind: 'brush', value: brushStyleFromCode(style, color, recordReader.u16(12)) });
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
case WMF_RECORD.META_CREATEFONTINDIRECT: {
|
|
1099
|
+
const font = readWmfFont(new BinaryReader(recordReader.slice(0, recordSize)));
|
|
1100
|
+
if (font)
|
|
1101
|
+
setObject(ctx, ensureObjectSlot(ctx), { kind: 'font', value: { ...font, charset: recordReader.u8(19) } });
|
|
1102
|
+
break;
|
|
1103
|
+
}
|
|
1104
|
+
case WMF_RECORD.META_SELECTOBJECT:
|
|
1105
|
+
selectObject(ctx, recordReader.u16(6));
|
|
1106
|
+
break;
|
|
1107
|
+
case WMF_RECORD.META_DELETEOBJECT: {
|
|
1108
|
+
const index = recordReader.u16(6);
|
|
1109
|
+
deleteObject(ctx, index);
|
|
1110
|
+
break;
|
|
1111
|
+
}
|
|
1112
|
+
default:
|
|
1113
|
+
ctx.unsupportedRecordTypes.add(fn);
|
|
1114
|
+
break;
|
|
1115
|
+
}
|
|
1116
|
+
cursor += recordSize;
|
|
1117
|
+
}
|
|
1118
|
+
return finalizeSvg(ctx, 'image/wmf');
|
|
1119
|
+
}
|
|
1120
|
+
function readEmfPointArray(ctx, reader, offset, count, pointSize) {
|
|
1121
|
+
if (!reader.ensure(offset, count * pointSize))
|
|
1122
|
+
throw new PptError('METAFILE_POINTS', 'EMF point array exceeds its own record');
|
|
1123
|
+
claimPoints(ctx, count);
|
|
1124
|
+
const points = [];
|
|
1125
|
+
for (let i = 0; i < count; i += 1) {
|
|
1126
|
+
const pointOffset = offset + i * pointSize;
|
|
1127
|
+
if (pointSize === 8) {
|
|
1128
|
+
points.push({ x: reader.i32(pointOffset), y: reader.i32(pointOffset + 4) });
|
|
1129
|
+
}
|
|
1130
|
+
else {
|
|
1131
|
+
points.push({ x: reader.i16(pointOffset), y: reader.i16(pointOffset + 2) });
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
return points;
|
|
1135
|
+
}
|
|
1136
|
+
function parseEmfText(ctx, reader, unicode) {
|
|
1137
|
+
if (!reader.ensure(36, 24))
|
|
1138
|
+
throw new PptError('METAFILE_TEXT', 'EMF text object exceeds its record');
|
|
1139
|
+
const point = { x: reader.i32(36), y: reader.i32(40) };
|
|
1140
|
+
const chars = reader.u32(44);
|
|
1141
|
+
const offString = reader.u32(48);
|
|
1142
|
+
const options = reader.u32(52), hasRect = (options & 0x100) === 0, offDxField = hasRect ? 72 : 56;
|
|
1143
|
+
if (!reader.ensure(offDxField, 4))
|
|
1144
|
+
throw new PptError('METAFILE_TEXT', 'EMF text rectangle or spacing offset exceeds its record');
|
|
1145
|
+
const rectangle = hasRect ? { left: reader.i32(56), top: reader.i32(60), right: reader.i32(64), bottom: reader.i32(68) } : undefined;
|
|
1146
|
+
const byteLength = unicode ? chars * 2 : chars, offDx = reader.u32(offDxField), stride = options & 0x2000 ? 8 : 4;
|
|
1147
|
+
if (chars && (!offString || !reader.ensure(offString, byteLength)))
|
|
1148
|
+
throw new PptError('METAFILE_TEXT', 'EMF text bytes exceed their own record');
|
|
1149
|
+
let advances;
|
|
1150
|
+
if (offDx && chars) {
|
|
1151
|
+
if (!reader.ensure(offDx, chars * stride))
|
|
1152
|
+
throw new PptError('METAFILE_TEXT', 'EMF character spacing exceeds its own record');
|
|
1153
|
+
claimPoints(ctx, chars);
|
|
1154
|
+
advances = Array.from({ length: chars }, (_, i) => ({ x: reader.i32(offDx + i * stride), y: stride === 8 ? reader.i32(offDx + i * stride + 4) : 0 }));
|
|
1155
|
+
}
|
|
1156
|
+
if (options & 0xC00)
|
|
1157
|
+
ctx.unsupportedRecordTypes.add(ctx.activeRecordType);
|
|
1158
|
+
const bytes = reader.slice(offString, byteLength);
|
|
1159
|
+
if (options & 0x0010) {
|
|
1160
|
+
claimPoints(ctx, chars);
|
|
1161
|
+
const glyphIndices = unicode
|
|
1162
|
+
? Array.from({ length: chars }, (_, index) => reader.u16(offString + index * 2)) : Array.from(bytes);
|
|
1163
|
+
return { point, text: '', glyphIndices, advances, options, rectangle };
|
|
1164
|
+
}
|
|
1165
|
+
const text = unicode ? decodeUtf16(bytes) : decodeAnsi(bytes, ctx.state.font.charset);
|
|
1166
|
+
return { point, text, advances, options, rectangle };
|
|
1167
|
+
}
|
|
1168
|
+
function parseSmallEmfText(ctx, reader) {
|
|
1169
|
+
if (!reader.ensure(8, 28))
|
|
1170
|
+
throw new PptError('METAFILE_TEXT', 'Small EMF text has a truncated prefix');
|
|
1171
|
+
const point = { x: reader.i32(8), y: reader.i32(12) };
|
|
1172
|
+
const chars = reader.u32(16);
|
|
1173
|
+
const options = reader.u32(20);
|
|
1174
|
+
const hasRect = (options & 0x100) === 0, textOffset = hasRect ? 52 : 36, small = (options & 0x200) !== 0;
|
|
1175
|
+
if (!reader.ensure(textOffset, chars * (small ? 1 : 2)))
|
|
1176
|
+
throw new PptError('METAFILE_TEXT', 'Small EMF text exceeds its own record');
|
|
1177
|
+
const rectangle = hasRect ? { left: reader.i32(36), top: reader.i32(40), right: reader.i32(44), bottom: reader.i32(48) } : undefined;
|
|
1178
|
+
if (options & 0x10) {
|
|
1179
|
+
claimPoints(ctx, chars);
|
|
1180
|
+
return { point, text: '', options, rectangle, glyphIndices: Array.from({ length: chars }, (_, i) => small ? reader.u8(textOffset + i) : reader.u16(textOffset + i * 2)) };
|
|
1181
|
+
}
|
|
1182
|
+
const text = small ? Array.from(reader.slice(textOffset, chars), value => String.fromCharCode(value)).join('') : decodeUtf16(reader.slice(textOffset, chars * 2));
|
|
1183
|
+
return { point, text, options, rectangle };
|
|
1184
|
+
}
|
|
1185
|
+
function readEmfEmbeddedBmp(record, offBmiOffset, cbBmiOffset, offBitsOffset, cbBitsOffset, remainingBytes, perPixelAlpha) {
|
|
1186
|
+
const offBmi = record.u32(offBmiOffset);
|
|
1187
|
+
const cbBmi = record.u32(cbBmiOffset);
|
|
1188
|
+
const offBits = record.u32(offBitsOffset);
|
|
1189
|
+
const cbBits = record.u32(cbBitsOffset);
|
|
1190
|
+
if (!offBmi && !cbBmi && !offBits && !cbBits)
|
|
1191
|
+
return null;
|
|
1192
|
+
if (!offBmi || !cbBmi || !offBits || !cbBits || !record.ensure(offBmi, cbBmi) || !record.ensure(offBits, cbBits))
|
|
1193
|
+
throw new PptError('METAFILE_BITMAP', 'Embedded bitmap exceeds its own record');
|
|
1194
|
+
if (Math.ceil((cbBmi + cbBits + 14) / 3) * 4 + 256 > remainingBytes)
|
|
1195
|
+
throw new PptError('LIMIT_ASSETS', 'Embedded metafile bitmap exceeds the SVG output budget');
|
|
1196
|
+
const bmi = record.slice(offBmi, cbBmi), bits = record.slice(offBits, cbBits), header = new BinaryReader(bmi), size = header.u32(0);
|
|
1197
|
+
if (size !== 12 && size < 40 || !header.ensure(0, size))
|
|
1198
|
+
throw new PptError('METAFILE_BITMAP', 'Invalid embedded bitmap header');
|
|
1199
|
+
const width = size === 12 ? header.u16(4) : header.i32(4), signedHeight = size === 12 ? header.u16(6) : header.i32(8), height = Math.abs(signedHeight), depth = header.u16(size === 12 ? 10 : 14), compression = size === 12 ? 0 : header.u32(16);
|
|
1200
|
+
if (width <= 0 || height <= 0 || width * height * 4 > remainingBytes)
|
|
1201
|
+
throw new PptError('LIMIT_ASSETS', 'Embedded bitmap dimensions exceed the decoded byte budget');
|
|
1202
|
+
let data, mime = 'image/bmp';
|
|
1203
|
+
if (perPixelAlpha !== undefined && depth === 32 && compression === 0) {
|
|
1204
|
+
data = encodeRgbaPng(width, height, decodeBgraBitmap(width, signedHeight, bits, perPixelAlpha, remainingBytes), remainingBytes);
|
|
1205
|
+
mime = 'image/png';
|
|
1206
|
+
}
|
|
1207
|
+
else if (perPixelAlpha || perPixelAlpha === false && (depth === 32 || compression === 5)) {
|
|
1208
|
+
return null;
|
|
1209
|
+
}
|
|
1210
|
+
else if (compression === 4 || compression === 5) {
|
|
1211
|
+
data = bits;
|
|
1212
|
+
mime = compression === 4 ? 'image/jpeg' : 'image/png';
|
|
1213
|
+
}
|
|
1214
|
+
else {
|
|
1215
|
+
const bmp = bitmapInfoToBmp(bmi, bits);
|
|
1216
|
+
if (!bmp)
|
|
1217
|
+
return null;
|
|
1218
|
+
data = bmp;
|
|
1219
|
+
}
|
|
1220
|
+
return { bytes: data, mime, width, height, bottomUp: signedHeight > 0 };
|
|
1221
|
+
}
|
|
1222
|
+
function addEmfStretchDibits(ctx, record) {
|
|
1223
|
+
if (!record.ensure(24, 56))
|
|
1224
|
+
return;
|
|
1225
|
+
const bmp = readEmfEmbeddedBmp(record, 48, 52, 56, 60, ctx.limits.maxOutputBytes - ctx.outputBytes);
|
|
1226
|
+
if (!bmp)
|
|
1227
|
+
return;
|
|
1228
|
+
const y = record.i32(36), cy = record.i32(44), sourceY = bmp.bottomUp ? bmp.height - y - cy : y;
|
|
1229
|
+
addRasterImage(ctx, {
|
|
1230
|
+
left: record.i32(24),
|
|
1231
|
+
top: record.i32(28),
|
|
1232
|
+
right: record.i32(24) + record.i32(72),
|
|
1233
|
+
bottom: record.i32(28) + record.i32(76),
|
|
1234
|
+
}, bmp, { left: record.i32(32), top: sourceY, right: record.i32(32) + record.i32(40), bottom: sourceY + cy });
|
|
1235
|
+
}
|
|
1236
|
+
function addEmfBltBitmap(ctx, record) {
|
|
1237
|
+
if (!record.ensure(24, 76))
|
|
1238
|
+
return;
|
|
1239
|
+
const alpha = ctx.activeRecordType === EMF_RECORD.EMR_ALPHABLEND;
|
|
1240
|
+
if (alpha && (record.u8(40) !== 0 || record.u8(43) > 1)) {
|
|
1241
|
+
ctx.unsupportedRecordTypes.add(ctx.activeRecordType);
|
|
1242
|
+
return;
|
|
1243
|
+
}
|
|
1244
|
+
const bmp = readEmfEmbeddedBmp(record, 84, 88, 92, 96, ctx.limits.maxOutputBytes - ctx.outputBytes, alpha ? record.u8(43) === 1 : undefined);
|
|
1245
|
+
if (!bmp) {
|
|
1246
|
+
ctx.unsupportedRecordTypes.add(ctx.activeRecordType);
|
|
1247
|
+
return;
|
|
1248
|
+
}
|
|
1249
|
+
const sw = ctx.activeRecordType === EMF_RECORD.EMR_BITBLT ? record.i32(32) : record.i32(100), sh = ctx.activeRecordType === EMF_RECORD.EMR_BITBLT ? record.i32(36) : record.i32(104);
|
|
1250
|
+
if (alpha && (sw <= 0 || sh <= 0 || record.i32(32) <= 0 || record.i32(36) <= 0))
|
|
1251
|
+
throw new PptError('METAFILE_BITMAP', 'AlphaBlend source and destination extents must be positive');
|
|
1252
|
+
addRasterImage(ctx, {
|
|
1253
|
+
left: record.i32(24),
|
|
1254
|
+
top: record.i32(28),
|
|
1255
|
+
right: record.i32(24) + record.i32(32),
|
|
1256
|
+
bottom: record.i32(28) + record.i32(36),
|
|
1257
|
+
}, bmp, { left: record.i32(44), top: record.i32(48), right: record.i32(44) + sw, bottom: record.i32(48) + sh }, readXForm(record, 52), alpha ? record.u8(42) / 255 : 1);
|
|
1258
|
+
}
|
|
1259
|
+
function closeCurrentPathFigure(ctx) {
|
|
1260
|
+
if (!ctx.pathData.length)
|
|
1261
|
+
return;
|
|
1262
|
+
addPathFragment(ctx, 'Z', []);
|
|
1263
|
+
}
|
|
1264
|
+
function parseEmf(bytes, limits) {
|
|
1265
|
+
const reader = new BinaryReader(bytes);
|
|
1266
|
+
if (!reader.ensure(0, 88) || reader.u32(0) !== EMF_RECORD.EMR_HEADER)
|
|
1267
|
+
return null;
|
|
1268
|
+
const bounds = normalizeRect({
|
|
1269
|
+
left: reader.i32(8),
|
|
1270
|
+
top: reader.i32(12),
|
|
1271
|
+
right: reader.i32(16),
|
|
1272
|
+
bottom: reader.i32(20),
|
|
1273
|
+
});
|
|
1274
|
+
const viewBox = rectWidth(bounds) > 0 && rectHeight(bounds) > 0 ? bounds : { left: 0, top: 0, right: 1000, bottom: 1000 };
|
|
1275
|
+
const ctx = defaultContext(rectWidth(viewBox), rectHeight(viewBox), viewBox, limits, reader.u16(56));
|
|
1276
|
+
ctx.state.windowOrg = { x: viewBox.left, y: viewBox.top };
|
|
1277
|
+
ctx.state.windowExt = { x: rectWidth(viewBox), y: rectHeight(viewBox) };
|
|
1278
|
+
ctx.state.viewportOrg = { x: viewBox.left, y: viewBox.top };
|
|
1279
|
+
ctx.state.viewportExt = { x: rectWidth(viewBox), y: rectHeight(viewBox) };
|
|
1280
|
+
let cursor = 0;
|
|
1281
|
+
while (cursor + 8 <= bytes.length) {
|
|
1282
|
+
const type = reader.u32(cursor);
|
|
1283
|
+
const size = reader.u32(cursor + 4);
|
|
1284
|
+
if (size < 8 || size % 4 !== 0 || cursor + size > bytes.length || size < (EMF_MIN_BYTES[type] ?? 8))
|
|
1285
|
+
throw new PptError('METAFILE_RECORD_LENGTH', `EMF record ${type} exceeds its payload or has a truncated fixed prefix`, cursor);
|
|
1286
|
+
const record = new BinaryReader(bytes.subarray(cursor, cursor + size));
|
|
1287
|
+
countRecord(ctx);
|
|
1288
|
+
ctx.activeRecordType = type;
|
|
1289
|
+
switch (type) {
|
|
1290
|
+
case EMF_RECORD.EMR_HEADER:
|
|
1291
|
+
break;
|
|
1292
|
+
case EMF_RECORD.EMR_EOF:
|
|
1293
|
+
cursor += size;
|
|
1294
|
+
return finalizeSvg(ctx, 'image/emf');
|
|
1295
|
+
case EMF_RECORD.EMR_SETWINDOWORGEX:
|
|
1296
|
+
ctx.state.windowOrg = { x: record.i32(8), y: record.i32(12) };
|
|
1297
|
+
break;
|
|
1298
|
+
case EMF_RECORD.EMR_SETWINDOWEXTEX:
|
|
1299
|
+
ctx.state.windowExt = { x: record.i32(8), y: record.i32(12) };
|
|
1300
|
+
break;
|
|
1301
|
+
case EMF_RECORD.EMR_SETVIEWPORTORGEX:
|
|
1302
|
+
ctx.state.viewportOrg = { x: record.i32(8), y: record.i32(12) };
|
|
1303
|
+
break;
|
|
1304
|
+
case EMF_RECORD.EMR_SETVIEWPORTEXTEX:
|
|
1305
|
+
ctx.state.viewportExt = { x: record.i32(8), y: record.i32(12) };
|
|
1306
|
+
break;
|
|
1307
|
+
case EMF_RECORD.EMR_SETBKMODE:
|
|
1308
|
+
ctx.state.bkMode = record.u32(8);
|
|
1309
|
+
break;
|
|
1310
|
+
case EMF_RECORD.EMR_SETPOLYFILLMODE:
|
|
1311
|
+
ctx.state.polygonFillMode = record.u32(8);
|
|
1312
|
+
break;
|
|
1313
|
+
case EMF_RECORD.EMR_SETTEXTALIGN:
|
|
1314
|
+
ctx.state.textAlign = record.u32(8);
|
|
1315
|
+
break;
|
|
1316
|
+
case EMF_RECORD.EMR_SETTEXTCOLOR:
|
|
1317
|
+
ctx.state.textColor = colorRefToCss(record.u32(8));
|
|
1318
|
+
break;
|
|
1319
|
+
case EMF_RECORD.EMR_SETBKCOLOR:
|
|
1320
|
+
ctx.state.bkColor = colorRefToCss(record.u32(8));
|
|
1321
|
+
break;
|
|
1322
|
+
case EMF_RECORD.EMR_SETARCDIRECTION:
|
|
1323
|
+
ctx.state.arcDirection = record.u32(8) === 2 ? 2 : 1;
|
|
1324
|
+
break;
|
|
1325
|
+
case EMF_RECORD.EMR_INTERSECTCLIPRECT:
|
|
1326
|
+
case EMF_RECORD.EMR_EXCLUDECLIPRECT: {
|
|
1327
|
+
const r = { left: record.i32(8), top: record.i32(12), right: record.i32(16), bottom: record.i32(20) };
|
|
1328
|
+
clipPath(ctx, rectanglePath(ctx, r).path, type === EMF_RECORD.EMR_INTERSECTCLIPRECT ? 1 : 4);
|
|
1329
|
+
break;
|
|
1330
|
+
}
|
|
1331
|
+
case EMF_RECORD.EMR_SAVEDC:
|
|
1332
|
+
saveState(ctx);
|
|
1333
|
+
break;
|
|
1334
|
+
case EMF_RECORD.EMR_RESTOREDC: {
|
|
1335
|
+
restoreState(ctx, record.i32(8));
|
|
1336
|
+
break;
|
|
1337
|
+
}
|
|
1338
|
+
case EMF_RECORD.EMR_SETWORLDTRANSFORM:
|
|
1339
|
+
ctx.state.worldTransform = readXForm(record, 8);
|
|
1340
|
+
break;
|
|
1341
|
+
case EMF_RECORD.EMR_MODIFYWORLDTRANSFORM: {
|
|
1342
|
+
const xform = readXForm(record, 8);
|
|
1343
|
+
const mode = record.u32(32);
|
|
1344
|
+
if (mode === MODIFY_WORLD_TRANSFORM.MWT_IDENTITY)
|
|
1345
|
+
ctx.state.worldTransform = { ...IDENTITY_MATRIX };
|
|
1346
|
+
else if (mode === MODIFY_WORLD_TRANSFORM.MWT_SET)
|
|
1347
|
+
ctx.state.worldTransform = xform;
|
|
1348
|
+
else if (mode === MODIFY_WORLD_TRANSFORM.MWT_LEFTMULTIPLY)
|
|
1349
|
+
ctx.state.worldTransform = multiplyMatrix(xform, ctx.state.worldTransform);
|
|
1350
|
+
else if (mode === MODIFY_WORLD_TRANSFORM.MWT_RIGHTMULTIPLY)
|
|
1351
|
+
ctx.state.worldTransform = multiplyMatrix(ctx.state.worldTransform, xform);
|
|
1352
|
+
break;
|
|
1353
|
+
}
|
|
1354
|
+
case EMF_RECORD.EMR_MOVETOEX:
|
|
1355
|
+
ctx.state.currentPosition = { x: record.i32(8), y: record.i32(12) };
|
|
1356
|
+
break;
|
|
1357
|
+
case EMF_RECORD.EMR_LINETO: {
|
|
1358
|
+
const end = { x: record.i32(8), y: record.i32(12) };
|
|
1359
|
+
addLine(ctx, ctx.state.currentPosition, end);
|
|
1360
|
+
break;
|
|
1361
|
+
}
|
|
1362
|
+
case EMF_RECORD.EMR_POLYLINE:
|
|
1363
|
+
case EMF_RECORD.EMR_POLYGON:
|
|
1364
|
+
case EMF_RECORD.EMR_POLYLINE16:
|
|
1365
|
+
case EMF_RECORD.EMR_POLYGON16: {
|
|
1366
|
+
const count = record.u32(24);
|
|
1367
|
+
const pointOffset = 28;
|
|
1368
|
+
const pointSize = (type === EMF_RECORD.EMR_POLYLINE16 || type === EMF_RECORD.EMR_POLYGON16) ? 4 : 8;
|
|
1369
|
+
const points = readEmfPointArray(ctx, record, pointOffset, count, pointSize);
|
|
1370
|
+
addPolyline(ctx, points, type === EMF_RECORD.EMR_POLYGON || type === EMF_RECORD.EMR_POLYGON16);
|
|
1371
|
+
break;
|
|
1372
|
+
}
|
|
1373
|
+
case EMF_RECORD.EMR_POLYBEZIER:
|
|
1374
|
+
case EMF_RECORD.EMR_POLYBEZIER16: {
|
|
1375
|
+
const count = record.u32(24);
|
|
1376
|
+
const pointOffset = 28;
|
|
1377
|
+
const pointSize = type === EMF_RECORD.EMR_POLYBEZIER16 ? 4 : 8;
|
|
1378
|
+
const points = readEmfPointArray(ctx, record, pointOffset, count, pointSize);
|
|
1379
|
+
addBezierPath(ctx, points, false);
|
|
1380
|
+
break;
|
|
1381
|
+
}
|
|
1382
|
+
case EMF_RECORD.EMR_POLYBEZIERTO:
|
|
1383
|
+
case EMF_RECORD.EMR_POLYBEZIERTO16: {
|
|
1384
|
+
const count = record.u32(24);
|
|
1385
|
+
const pointOffset = 28;
|
|
1386
|
+
const pointSize = type === EMF_RECORD.EMR_POLYBEZIERTO16 ? 4 : 8;
|
|
1387
|
+
const points = readEmfPointArray(ctx, record, pointOffset, count, pointSize);
|
|
1388
|
+
addBezierPath(ctx, points, true);
|
|
1389
|
+
break;
|
|
1390
|
+
}
|
|
1391
|
+
case EMF_RECORD.EMR_POLYLINETO:
|
|
1392
|
+
case EMF_RECORD.EMR_POLYLINETO16: {
|
|
1393
|
+
const count = record.u32(24);
|
|
1394
|
+
const pointOffset = 28;
|
|
1395
|
+
const pointSize = type === EMF_RECORD.EMR_POLYLINETO16 ? 4 : 8;
|
|
1396
|
+
const points = readEmfPointArray(ctx, record, pointOffset, count, pointSize);
|
|
1397
|
+
addPolyline(ctx, [{ ...ctx.state.currentPosition }, ...points], false);
|
|
1398
|
+
break;
|
|
1399
|
+
}
|
|
1400
|
+
case EMF_RECORD.EMR_POLYPOLYLINE:
|
|
1401
|
+
case EMF_RECORD.EMR_POLYPOLYGON:
|
|
1402
|
+
case EMF_RECORD.EMR_POLYPOLYLINE16:
|
|
1403
|
+
case EMF_RECORD.EMR_POLYPOLYGON16: {
|
|
1404
|
+
const polygonCount = record.u32(24);
|
|
1405
|
+
const pointCount = record.u32(28);
|
|
1406
|
+
const countsOffset = 32;
|
|
1407
|
+
const pointSize = (type === EMF_RECORD.EMR_POLYPOLYLINE16 || type === EMF_RECORD.EMR_POLYPOLYGON16) ? 4 : 8;
|
|
1408
|
+
const pointsOffset = countsOffset + polygonCount * 4;
|
|
1409
|
+
if (!record.ensure(countsOffset, polygonCount * 4) || !record.ensure(pointsOffset, pointCount * pointSize))
|
|
1410
|
+
throw new PptError('METAFILE_POINTS', 'EMF polygon counts or points exceed their own record', cursor);
|
|
1411
|
+
if (polygonCount > limits.maxPoints)
|
|
1412
|
+
throw new PptError('METAFILE_POINT_LIMIT', 'EMF polygon count exceeds the configured limit', cursor);
|
|
1413
|
+
const counts = [];
|
|
1414
|
+
for (let i = 0; i < polygonCount; i += 1)
|
|
1415
|
+
counts.push(record.u32(countsOffset + i * 4));
|
|
1416
|
+
if (counts.reduce((sum, count) => sum + count, 0) !== pointCount)
|
|
1417
|
+
throw new PptError('METAFILE_POINTS', 'EMF polygon counts disagree with the declared point count', cursor);
|
|
1418
|
+
const points = readEmfPointArray(ctx, record, pointsOffset, pointCount, pointSize);
|
|
1419
|
+
let index = 0;
|
|
1420
|
+
const polygons = [], closed = type === EMF_RECORD.EMR_POLYPOLYGON || type === EMF_RECORD.EMR_POLYPOLYGON16;
|
|
1421
|
+
for (const count of counts) {
|
|
1422
|
+
if (closed)
|
|
1423
|
+
polygons.push(points.slice(index, index + count));
|
|
1424
|
+
else
|
|
1425
|
+
addPolyline(ctx, points.slice(index, index + count), false);
|
|
1426
|
+
index += count;
|
|
1427
|
+
}
|
|
1428
|
+
if (closed)
|
|
1429
|
+
addCompoundPolygons(ctx, polygons);
|
|
1430
|
+
break;
|
|
1431
|
+
}
|
|
1432
|
+
case EMF_RECORD.EMR_RECTANGLE:
|
|
1433
|
+
addRect(ctx, { left: record.i32(8), top: record.i32(12), right: record.i32(16), bottom: record.i32(20) });
|
|
1434
|
+
break;
|
|
1435
|
+
case EMF_RECORD.EMR_ELLIPSE:
|
|
1436
|
+
addEllipse(ctx, { left: record.i32(8), top: record.i32(12), right: record.i32(16), bottom: record.i32(20) });
|
|
1437
|
+
break;
|
|
1438
|
+
case EMF_RECORD.EMR_ROUNDRECT:
|
|
1439
|
+
addRect(ctx, { left: record.i32(8), top: record.i32(12), right: record.i32(16), bottom: record.i32(20) }, { x: record.i32(24), y: record.i32(28) });
|
|
1440
|
+
break;
|
|
1441
|
+
case EMF_RECORD.EMR_ARC:
|
|
1442
|
+
case EMF_RECORD.EMR_ARCTO:
|
|
1443
|
+
case EMF_RECORD.EMR_CHORD:
|
|
1444
|
+
case EMF_RECORD.EMR_PIE:
|
|
1445
|
+
addArc(ctx, { left: record.i32(8), top: record.i32(12), right: record.i32(16), bottom: record.i32(20) }, { x: record.i32(24), y: record.i32(28) }, { x: record.i32(32), y: record.i32(36) }, type === EMF_RECORD.EMR_ARCTO ? 'arcTo' : type === EMF_RECORD.EMR_CHORD ? 'chord' : type === EMF_RECORD.EMR_PIE ? 'pie' : 'arc');
|
|
1446
|
+
break;
|
|
1447
|
+
case EMF_RECORD.EMR_BEGINPATH:
|
|
1448
|
+
ctx.capturePath = true;
|
|
1449
|
+
ctx.pathData = [];
|
|
1450
|
+
ctx.pathPoints = [];
|
|
1451
|
+
ctx.pathBytes = 0;
|
|
1452
|
+
break;
|
|
1453
|
+
case EMF_RECORD.EMR_ENDPATH:
|
|
1454
|
+
ctx.capturePath = false;
|
|
1455
|
+
break;
|
|
1456
|
+
case EMF_RECORD.EMR_CLOSEFIGURE:
|
|
1457
|
+
closeCurrentPathFigure(ctx);
|
|
1458
|
+
break;
|
|
1459
|
+
case EMF_RECORD.EMR_FILLPATH:
|
|
1460
|
+
flushPath(ctx, fillOnlyAttributes(ctx));
|
|
1461
|
+
break;
|
|
1462
|
+
case EMF_RECORD.EMR_STROKEANDFILLPATH:
|
|
1463
|
+
flushPath(ctx, fillAttributes(ctx));
|
|
1464
|
+
break;
|
|
1465
|
+
case EMF_RECORD.EMR_STROKEPATH:
|
|
1466
|
+
flushPath(ctx, strokeAttributes(ctx, ctx.state.pen));
|
|
1467
|
+
break;
|
|
1468
|
+
case EMF_RECORD.EMR_SELECTCLIPPATH:
|
|
1469
|
+
if (!ctx.pathData.length || !clipPath(ctx, ctx.pathData.join(' '), record.u32(8)))
|
|
1470
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1471
|
+
ctx.capturePath = false;
|
|
1472
|
+
ctx.pathData = [];
|
|
1473
|
+
ctx.pathPoints = [];
|
|
1474
|
+
ctx.pathBytes = 0;
|
|
1475
|
+
break;
|
|
1476
|
+
case EMF_RECORD.EMR_CREATEPEN: {
|
|
1477
|
+
const index = record.u32(8);
|
|
1478
|
+
const style = record.u32(12);
|
|
1479
|
+
const width = record.i32(16);
|
|
1480
|
+
const color = colorRefToCss(record.u32(24));
|
|
1481
|
+
setObject(ctx, index, { kind: 'pen', value: penStyleFromCode(style, width, color) });
|
|
1482
|
+
break;
|
|
1483
|
+
}
|
|
1484
|
+
case EMF_RECORD.EMR_CREATEBRUSHINDIRECT: {
|
|
1485
|
+
const index = record.u32(8);
|
|
1486
|
+
const style = record.u32(12);
|
|
1487
|
+
if (style > 2 || style === 2 && record.u32(20) > 5)
|
|
1488
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1489
|
+
const color = colorRefToCss(record.u32(16));
|
|
1490
|
+
setObject(ctx, index, { kind: 'brush', value: brushStyleFromCode(style, color, record.u32(20)) });
|
|
1491
|
+
break;
|
|
1492
|
+
}
|
|
1493
|
+
case EMF_RECORD.EMR_EXTCREATEPEN: {
|
|
1494
|
+
const index = record.u32(8);
|
|
1495
|
+
const style = record.u32(28);
|
|
1496
|
+
const width = record.i32(32);
|
|
1497
|
+
const brushStyle = record.u32(36);
|
|
1498
|
+
if (brushStyle > 1 || (style & 15) > 5)
|
|
1499
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1500
|
+
const color = brushStyle === 1 ? 'none' : colorRefToCss(record.u32(40));
|
|
1501
|
+
setObject(ctx, index, { kind: 'pen', value: brushStyle === 1 ? { color, width, none: true } : penStyleFromCode(style, width, color) });
|
|
1502
|
+
break;
|
|
1503
|
+
}
|
|
1504
|
+
case EMF_RECORD.EMR_EXTCREATEFONTINDIRECTW: {
|
|
1505
|
+
const index = record.u32(8);
|
|
1506
|
+
const font = readEmfFont(new BinaryReader(record.slice(0, size)));
|
|
1507
|
+
if (font)
|
|
1508
|
+
setObject(ctx, index, { kind: 'font', value: { ...font, charset: record.u8(35) } });
|
|
1509
|
+
break;
|
|
1510
|
+
}
|
|
1511
|
+
case EMF_RECORD.EMR_SELECTOBJECT:
|
|
1512
|
+
selectObject(ctx, record.u32(8));
|
|
1513
|
+
break;
|
|
1514
|
+
case EMF_RECORD.EMR_DELETEOBJECT: {
|
|
1515
|
+
const index = record.u32(8);
|
|
1516
|
+
deleteObject(ctx, index);
|
|
1517
|
+
break;
|
|
1518
|
+
}
|
|
1519
|
+
case EMF_RECORD.EMR_EXTTEXTOUTA: {
|
|
1520
|
+
const parsed = parseEmfText(ctx, record, false);
|
|
1521
|
+
if (parsed)
|
|
1522
|
+
addText(ctx, parsed.point, parsed.text, parsed.glyphIndices, parsed);
|
|
1523
|
+
break;
|
|
1524
|
+
}
|
|
1525
|
+
case EMF_RECORD.EMR_EXTTEXTOUTW: {
|
|
1526
|
+
const parsed = parseEmfText(ctx, record, true);
|
|
1527
|
+
if (parsed)
|
|
1528
|
+
addText(ctx, parsed.point, parsed.text, parsed.glyphIndices, parsed);
|
|
1529
|
+
break;
|
|
1530
|
+
}
|
|
1531
|
+
case EMF_RECORD.EMR_SMALLTEXTOUT: {
|
|
1532
|
+
const parsed = parseSmallEmfText(ctx, record);
|
|
1533
|
+
if (parsed)
|
|
1534
|
+
addText(ctx, parsed.point, parsed.text, parsed.glyphIndices, parsed);
|
|
1535
|
+
break;
|
|
1536
|
+
}
|
|
1537
|
+
case EMF_RECORD.EMR_STRETCHDIBITS:
|
|
1538
|
+
if (record.u32(68) !== 0x00CC0020 || record.u32(64) !== 0)
|
|
1539
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1540
|
+
addEmfStretchDibits(ctx, record);
|
|
1541
|
+
break;
|
|
1542
|
+
case EMF_RECORD.EMR_BITBLT:
|
|
1543
|
+
case EMF_RECORD.EMR_STRETCHBLT:
|
|
1544
|
+
case EMF_RECORD.EMR_ALPHABLEND:
|
|
1545
|
+
if (type !== EMF_RECORD.EMR_ALPHABLEND && record.u32(40) !== 0x00CC0020 || record.u32(80) !== 0)
|
|
1546
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1547
|
+
addEmfBltBitmap(ctx, record);
|
|
1548
|
+
break;
|
|
1549
|
+
default:
|
|
1550
|
+
ctx.unsupportedRecordTypes.add(type);
|
|
1551
|
+
break;
|
|
1552
|
+
}
|
|
1553
|
+
cursor += size;
|
|
1554
|
+
}
|
|
1555
|
+
return finalizeSvg(ctx, 'image/emf');
|
|
1556
|
+
}
|
|
1557
|
+
export function convertMetafileToSvg(mime, bytes, options = {}) {
|
|
1558
|
+
if (!bytes.length)
|
|
1559
|
+
return null;
|
|
1560
|
+
const limits = { ...DEFAULT_VECTOR_LIMITS, ...options };
|
|
1561
|
+
for (const value of Object.values(limits))
|
|
1562
|
+
if (!Number.isSafeInteger(value) || value < 0)
|
|
1563
|
+
throw new RangeError('Vector conversion limits must be nonnegative safe integers');
|
|
1564
|
+
limits.maxObjects = Math.min(limits.maxObjects, 65536);
|
|
1565
|
+
if (/image\/emf/i.test(mime))
|
|
1566
|
+
return parseEmf(bytes, limits);
|
|
1567
|
+
if (/image\/wmf/i.test(mime))
|
|
1568
|
+
return parseWmf(bytes, limits);
|
|
1569
|
+
return null;
|
|
1570
|
+
}
|