@microlee666/dom-to-pptx 1.1.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/.prettierrc +6 -0
- package/CHANGELOG.md +96 -0
- package/CONTRIBUTING.md +72 -0
- package/LICENSE +21 -0
- package/Readme.md +310 -0
- package/SUPPORTED.md +50 -0
- package/USAGE_CN.md +324 -0
- package/cli/.claude/settings.local.json +8 -0
- package/cli/.dockerignore +8 -0
- package/cli/Dockerfile +54 -0
- package/cli/Dockerfile.cn +22 -0
- package/cli/HTML_PPT_SPEC.md +360 -0
- package/cli/README.md +196 -0
- package/cli/docker-compose.yml +20 -0
- package/cli/dom-to-pptx.bundle.js +64731 -0
- package/cli/html2pptx.js +214 -0
- package/cli/output.pptx +1 -0
- package/cli/package-lock.json +1171 -0
- package/cli/package.json +20 -0
- package/cli/server.js +808 -0
- package/dist/dom-to-pptx.bundle.js +64917 -0
- package/dist/dom-to-pptx.cjs +2735 -0
- package/dist/dom-to-pptx.cjs.map +1 -0
- package/dist/dom-to-pptx.mjs +2705 -0
- package/dist/dom-to-pptx.mjs.map +1 -0
- package/eslint.config.js +17 -0
- package/package.json +86 -0
- package/rollup.config.js +96 -0
- package/src/font-embedder.js +163 -0
- package/src/font-utils.js +32 -0
- package/src/image-processor.js +118 -0
- package/src/index.js +1376 -0
- package/src/utils.js +1032 -0
|
@@ -0,0 +1,2705 @@
|
|
|
1
|
+
import * as PptxGenJSImport from 'pptxgenjs';
|
|
2
|
+
import html2canvas from 'html2canvas';
|
|
3
|
+
import opentype from 'opentype.js';
|
|
4
|
+
import { Font } from 'fonteditor-core';
|
|
5
|
+
import pako from 'pako';
|
|
6
|
+
import JSZip from 'jszip';
|
|
7
|
+
|
|
8
|
+
// src/font-utils.js
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Converts various font formats to EOT (Embedded OpenType),
|
|
12
|
+
* which is highly compatible with PowerPoint embedding.
|
|
13
|
+
* @param {string} type - 'ttf', 'woff', or 'otf'
|
|
14
|
+
* @param {ArrayBuffer} fontBuffer - The raw font data
|
|
15
|
+
*/
|
|
16
|
+
async function fontToEot(type, fontBuffer) {
|
|
17
|
+
const options = {
|
|
18
|
+
type,
|
|
19
|
+
hinting: true,
|
|
20
|
+
// inflate is required for WOFF decoding
|
|
21
|
+
inflate: type === 'woff' ? pako.inflate : undefined,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const font = Font.create(fontBuffer, options);
|
|
25
|
+
|
|
26
|
+
const eotBuffer = font.write({
|
|
27
|
+
type: 'eot',
|
|
28
|
+
toBuffer: true,
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
if (eotBuffer instanceof ArrayBuffer) {
|
|
32
|
+
return eotBuffer;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Ensure we return an ArrayBuffer
|
|
36
|
+
return eotBuffer.buffer.slice(eotBuffer.byteOffset, eotBuffer.byteOffset + eotBuffer.byteLength);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// src/font-embedder.js
|
|
40
|
+
|
|
41
|
+
const START_RID = 201314;
|
|
42
|
+
|
|
43
|
+
class PPTXEmbedFonts {
|
|
44
|
+
constructor() {
|
|
45
|
+
this.zip = null;
|
|
46
|
+
this.rId = START_RID;
|
|
47
|
+
this.fonts = []; // { name, data, rid }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async loadZip(zip) {
|
|
51
|
+
this.zip = zip;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Reads the font name from the buffer using opentype.js
|
|
56
|
+
*/
|
|
57
|
+
getFontInfo(fontBuffer) {
|
|
58
|
+
try {
|
|
59
|
+
const font = opentype.parse(fontBuffer);
|
|
60
|
+
const names = font.names;
|
|
61
|
+
// Prefer English name, fallback to others
|
|
62
|
+
const fontFamily = names.fontFamily.en || Object.values(names.fontFamily)[0];
|
|
63
|
+
return { name: fontFamily };
|
|
64
|
+
} catch (e) {
|
|
65
|
+
console.warn('Could not parse font info', e);
|
|
66
|
+
return { name: 'Unknown' };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async addFont(fontFace, fontBuffer, type) {
|
|
71
|
+
// Convert to EOT/fntdata for PPTX compatibility
|
|
72
|
+
const eotData = await fontToEot(type, fontBuffer);
|
|
73
|
+
const rid = this.rId++;
|
|
74
|
+
this.fonts.push({ name: fontFace, data: eotData, rid });
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async updateFiles() {
|
|
78
|
+
await this.updateContentTypesXML();
|
|
79
|
+
await this.updatePresentationXML();
|
|
80
|
+
await this.updateRelsPresentationXML();
|
|
81
|
+
this.updateFontFiles();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async generateBlob() {
|
|
85
|
+
if (!this.zip) throw new Error('Zip not loaded');
|
|
86
|
+
return this.zip.generateAsync({
|
|
87
|
+
type: 'blob',
|
|
88
|
+
compression: 'DEFLATE',
|
|
89
|
+
compressionOptions: { level: 6 },
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// --- XML Manipulation Methods ---
|
|
94
|
+
|
|
95
|
+
async updateContentTypesXML() {
|
|
96
|
+
const file = this.zip.file('[Content_Types].xml');
|
|
97
|
+
if (!file) throw new Error('[Content_Types].xml not found');
|
|
98
|
+
|
|
99
|
+
const xmlStr = await file.async('string');
|
|
100
|
+
const parser = new DOMParser();
|
|
101
|
+
const doc = parser.parseFromString(xmlStr, 'text/xml');
|
|
102
|
+
|
|
103
|
+
const types = doc.getElementsByTagName('Types')[0];
|
|
104
|
+
const defaults = Array.from(doc.getElementsByTagName('Default'));
|
|
105
|
+
|
|
106
|
+
const hasFntData = defaults.some((el) => el.getAttribute('Extension') === 'fntdata');
|
|
107
|
+
|
|
108
|
+
if (!hasFntData) {
|
|
109
|
+
const el = doc.createElement('Default');
|
|
110
|
+
el.setAttribute('Extension', 'fntdata');
|
|
111
|
+
el.setAttribute('ContentType', 'application/x-fontdata');
|
|
112
|
+
types.insertBefore(el, types.firstChild);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
this.zip.file('[Content_Types].xml', new XMLSerializer().serializeToString(doc));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
async updatePresentationXML() {
|
|
119
|
+
const file = this.zip.file('ppt/presentation.xml');
|
|
120
|
+
if (!file) throw new Error('ppt/presentation.xml not found');
|
|
121
|
+
|
|
122
|
+
const xmlStr = await file.async('string');
|
|
123
|
+
const parser = new DOMParser();
|
|
124
|
+
const doc = parser.parseFromString(xmlStr, 'text/xml');
|
|
125
|
+
const presentation = doc.getElementsByTagName('p:presentation')[0];
|
|
126
|
+
|
|
127
|
+
// Enable embedding flags
|
|
128
|
+
presentation.setAttribute('saveSubsetFonts', 'true');
|
|
129
|
+
presentation.setAttribute('embedTrueTypeFonts', 'true');
|
|
130
|
+
|
|
131
|
+
// Find or create embeddedFontLst
|
|
132
|
+
let embeddedFontLst = presentation.getElementsByTagName('p:embeddedFontLst')[0];
|
|
133
|
+
|
|
134
|
+
if (!embeddedFontLst) {
|
|
135
|
+
embeddedFontLst = doc.createElement('p:embeddedFontLst');
|
|
136
|
+
|
|
137
|
+
// Insert before defaultTextStyle or at end
|
|
138
|
+
const defaultTextStyle = presentation.getElementsByTagName('p:defaultTextStyle')[0];
|
|
139
|
+
if (defaultTextStyle) {
|
|
140
|
+
presentation.insertBefore(embeddedFontLst, defaultTextStyle);
|
|
141
|
+
} else {
|
|
142
|
+
presentation.appendChild(embeddedFontLst);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Add font references
|
|
147
|
+
this.fonts.forEach((font) => {
|
|
148
|
+
// Check if already exists
|
|
149
|
+
const existing = Array.from(embeddedFontLst.getElementsByTagName('p:font')).find(
|
|
150
|
+
(node) => node.getAttribute('typeface') === font.name
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
if (!existing) {
|
|
154
|
+
const embedFont = doc.createElement('p:embeddedFont');
|
|
155
|
+
|
|
156
|
+
const fontNode = doc.createElement('p:font');
|
|
157
|
+
fontNode.setAttribute('typeface', font.name);
|
|
158
|
+
embedFont.appendChild(fontNode);
|
|
159
|
+
|
|
160
|
+
const regular = doc.createElement('p:regular');
|
|
161
|
+
regular.setAttribute('r:id', `rId${font.rid}`);
|
|
162
|
+
embedFont.appendChild(regular);
|
|
163
|
+
|
|
164
|
+
embeddedFontLst.appendChild(embedFont);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
this.zip.file('ppt/presentation.xml', new XMLSerializer().serializeToString(doc));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async updateRelsPresentationXML() {
|
|
172
|
+
const file = this.zip.file('ppt/_rels/presentation.xml.rels');
|
|
173
|
+
if (!file) throw new Error('presentation.xml.rels not found');
|
|
174
|
+
|
|
175
|
+
const xmlStr = await file.async('string');
|
|
176
|
+
const parser = new DOMParser();
|
|
177
|
+
const doc = parser.parseFromString(xmlStr, 'text/xml');
|
|
178
|
+
const relationships = doc.getElementsByTagName('Relationships')[0];
|
|
179
|
+
|
|
180
|
+
this.fonts.forEach((font) => {
|
|
181
|
+
const rel = doc.createElement('Relationship');
|
|
182
|
+
rel.setAttribute('Id', `rId${font.rid}`);
|
|
183
|
+
rel.setAttribute('Target', `fonts/${font.rid}.fntdata`);
|
|
184
|
+
rel.setAttribute(
|
|
185
|
+
'Type',
|
|
186
|
+
'http://schemas.openxmlformats.org/officeDocument/2006/relationships/font'
|
|
187
|
+
);
|
|
188
|
+
relationships.appendChild(rel);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
this.zip.file('ppt/_rels/presentation.xml.rels', new XMLSerializer().serializeToString(doc));
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
updateFontFiles() {
|
|
195
|
+
this.fonts.forEach((font) => {
|
|
196
|
+
this.zip.file(`ppt/fonts/${font.rid}.fntdata`, font.data);
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/utils.js
|
|
202
|
+
|
|
203
|
+
// canvas context for color normalization
|
|
204
|
+
let _ctx;
|
|
205
|
+
function getCtx() {
|
|
206
|
+
if (!_ctx) _ctx = document.createElement('canvas').getContext('2d', { willReadFrequently: true });
|
|
207
|
+
return _ctx;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function getTableBorder(style, side, scale) {
|
|
211
|
+
const widthStr = style[`border${side}Width`];
|
|
212
|
+
const styleStr = style[`border${side}Style`];
|
|
213
|
+
const colorStr = style[`border${side}Color`];
|
|
214
|
+
|
|
215
|
+
const width = parseFloat(widthStr) || 0;
|
|
216
|
+
if (width === 0 || styleStr === 'none' || styleStr === 'hidden') {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const color = parseColor(colorStr);
|
|
221
|
+
if (!color.hex || color.opacity === 0) return null;
|
|
222
|
+
|
|
223
|
+
let dash = 'solid';
|
|
224
|
+
if (styleStr === 'dashed') dash = 'dash';
|
|
225
|
+
if (styleStr === 'dotted') dash = 'dot';
|
|
226
|
+
|
|
227
|
+
return {
|
|
228
|
+
pt: width * 0.75 * scale, // Convert px to pt
|
|
229
|
+
color: color.hex,
|
|
230
|
+
style: dash,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Extracts native table data for PptxGenJS.
|
|
236
|
+
*/
|
|
237
|
+
function extractTableData(node, scale, options = {}) {
|
|
238
|
+
const rows = [];
|
|
239
|
+
const colWidths = [];
|
|
240
|
+
const root = options.root || null;
|
|
241
|
+
|
|
242
|
+
// 1. Calculate Column Widths based on the first row of cells
|
|
243
|
+
// We look at the first <tr>'s children to determine visual column widths.
|
|
244
|
+
// Note: This assumes a fixed grid. Complex colspan/rowspan on the first row
|
|
245
|
+
// might skew widths, but getBoundingClientRect captures the rendered result.
|
|
246
|
+
const firstRow = node.querySelector('tr');
|
|
247
|
+
if (firstRow) {
|
|
248
|
+
const cells = Array.from(firstRow.children);
|
|
249
|
+
cells.forEach((cell) => {
|
|
250
|
+
const rect = cell.getBoundingClientRect();
|
|
251
|
+
const wIn = rect.width * (1 / 96) * scale;
|
|
252
|
+
colWidths.push(wIn);
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// 2. Iterate Rows
|
|
257
|
+
const trList = node.querySelectorAll('tr');
|
|
258
|
+
trList.forEach((tr) => {
|
|
259
|
+
const rowData = [];
|
|
260
|
+
const cellList = Array.from(tr.children).filter((c) =>
|
|
261
|
+
['TD', 'TH'].includes(c.tagName)
|
|
262
|
+
);
|
|
263
|
+
|
|
264
|
+
cellList.forEach((cell) => {
|
|
265
|
+
const style = window.getComputedStyle(cell);
|
|
266
|
+
const cellText = cell.innerText.replace(/[\n\r\t]+/g, ' ').trim();
|
|
267
|
+
|
|
268
|
+
// A. Text Style
|
|
269
|
+
const textStyle = getTextStyle(style, scale, cellText, options);
|
|
270
|
+
|
|
271
|
+
// B. Cell Background
|
|
272
|
+
const fill = computeTableCellFill(style, cell, root, options);
|
|
273
|
+
|
|
274
|
+
// C. Alignment
|
|
275
|
+
let align = 'left';
|
|
276
|
+
if (style.textAlign === 'center') align = 'center';
|
|
277
|
+
if (style.textAlign === 'right' || style.textAlign === 'end') align = 'right';
|
|
278
|
+
|
|
279
|
+
let valign = 'top';
|
|
280
|
+
if (style.verticalAlign === 'middle') valign = 'middle';
|
|
281
|
+
if (style.verticalAlign === 'bottom') valign = 'bottom';
|
|
282
|
+
|
|
283
|
+
// D. Padding (Margins in PPTX)
|
|
284
|
+
// CSS Padding px -> PPTX Margin pt
|
|
285
|
+
const padding = getPadding(style, scale);
|
|
286
|
+
// getPadding returns [top, right, bottom, left] in inches relative to scale
|
|
287
|
+
// PptxGenJS expects points (pt) for margin: [t, r, b, l]
|
|
288
|
+
// or discrete properties. Let's use discrete for clarity.
|
|
289
|
+
const margin = [
|
|
290
|
+
padding[0] * 72, // top
|
|
291
|
+
padding[1] * 72, // right
|
|
292
|
+
padding[2] * 72, // bottom
|
|
293
|
+
padding[3] * 72 // left
|
|
294
|
+
];
|
|
295
|
+
|
|
296
|
+
// E. Borders
|
|
297
|
+
const borderTop = getTableBorder(style, 'Top', scale);
|
|
298
|
+
const borderRight = getTableBorder(style, 'Right', scale);
|
|
299
|
+
const borderBottom = getTableBorder(style, 'Bottom', scale);
|
|
300
|
+
const borderLeft = getTableBorder(style, 'Left', scale);
|
|
301
|
+
|
|
302
|
+
// F. Construct Cell Object
|
|
303
|
+
rowData.push({
|
|
304
|
+
text: cellText,
|
|
305
|
+
options: {
|
|
306
|
+
color: textStyle.color,
|
|
307
|
+
fontFace: textStyle.fontFace,
|
|
308
|
+
fontSize: textStyle.fontSize,
|
|
309
|
+
bold: textStyle.bold,
|
|
310
|
+
italic: textStyle.italic,
|
|
311
|
+
underline: textStyle.underline,
|
|
312
|
+
|
|
313
|
+
fill: fill,
|
|
314
|
+
align: align,
|
|
315
|
+
valign: valign,
|
|
316
|
+
margin: margin,
|
|
317
|
+
|
|
318
|
+
rowspan: parseInt(cell.getAttribute('rowspan')) || null,
|
|
319
|
+
colspan: parseInt(cell.getAttribute('colspan')) || null,
|
|
320
|
+
|
|
321
|
+
border: {
|
|
322
|
+
pt: null, // trigger explicit object structure
|
|
323
|
+
top: borderTop,
|
|
324
|
+
right: borderRight,
|
|
325
|
+
bottom: borderBottom,
|
|
326
|
+
left: borderLeft
|
|
327
|
+
}
|
|
328
|
+
},
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
if (rowData.length > 0) {
|
|
333
|
+
rows.push(rowData);
|
|
334
|
+
}
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
return { rows, colWidths };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Checks if any parent element has overflow: hidden which would clip this element
|
|
341
|
+
function isClippedByParent(node) {
|
|
342
|
+
let parent = node.parentElement;
|
|
343
|
+
while (parent && parent !== document.body) {
|
|
344
|
+
const style = window.getComputedStyle(parent);
|
|
345
|
+
const overflow = style.overflow;
|
|
346
|
+
if (overflow === 'hidden' || overflow === 'clip') {
|
|
347
|
+
return true;
|
|
348
|
+
}
|
|
349
|
+
parent = parent.parentElement;
|
|
350
|
+
}
|
|
351
|
+
return false;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Helper to save gradient text
|
|
355
|
+
// Helper to save gradient text: extracts the first color from a gradient string
|
|
356
|
+
function getGradientFallbackColor(bgImage) {
|
|
357
|
+
if (!bgImage || bgImage === 'none') return null;
|
|
358
|
+
|
|
359
|
+
// 1. Extract content inside function(...)
|
|
360
|
+
// Handles linear-gradient(...), radial-gradient(...), repeating-linear-gradient(...)
|
|
361
|
+
const match = bgImage.match(/gradient\((.*)\)/);
|
|
362
|
+
if (!match) return null;
|
|
363
|
+
|
|
364
|
+
const content = match[1];
|
|
365
|
+
|
|
366
|
+
// 2. Split by comma, respecting parentheses (to avoid splitting inside rgb(), oklch(), etc.)
|
|
367
|
+
const parts = [];
|
|
368
|
+
let current = '';
|
|
369
|
+
let parenDepth = 0;
|
|
370
|
+
|
|
371
|
+
for (const char of content) {
|
|
372
|
+
if (char === '(') parenDepth++;
|
|
373
|
+
if (char === ')') parenDepth--;
|
|
374
|
+
if (char === ',' && parenDepth === 0) {
|
|
375
|
+
parts.push(current.trim());
|
|
376
|
+
current = '';
|
|
377
|
+
} else {
|
|
378
|
+
current += char;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
if (current) parts.push(current.trim());
|
|
382
|
+
|
|
383
|
+
// 3. Find first part that is a color (skip angle/direction)
|
|
384
|
+
for (const part of parts) {
|
|
385
|
+
// Ignore directions (to right) or angles (90deg, 0.5turn)
|
|
386
|
+
if (/^(to\s|[\d\.]+(deg|rad|turn|grad))/.test(part)) continue;
|
|
387
|
+
|
|
388
|
+
// Extract color: Remove trailing position (e.g. "red 50%" -> "red")
|
|
389
|
+
// Regex matches whitespace + number + unit at end of string
|
|
390
|
+
const colorPart = part.replace(/\s+(-?[\d\.]+(%|px|em|rem|ch|vh|vw)?)$/, '');
|
|
391
|
+
|
|
392
|
+
// Check if it's not just a number (some gradients might have bare numbers? unlikely in standard syntax)
|
|
393
|
+
if (colorPart) return colorPart;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
return null;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
function mapDashType(style) {
|
|
400
|
+
if (style === 'dashed') return 'dash';
|
|
401
|
+
if (style === 'dotted') return 'dot';
|
|
402
|
+
return 'solid';
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function hexToRgb(hex) {
|
|
406
|
+
const clean = (hex || '').replace('#', '').trim();
|
|
407
|
+
if (clean.length !== 6) return null;
|
|
408
|
+
const num = parseInt(clean, 16);
|
|
409
|
+
if (Number.isNaN(num)) return null;
|
|
410
|
+
return {
|
|
411
|
+
r: (num >> 16) & 255,
|
|
412
|
+
g: (num >> 8) & 255,
|
|
413
|
+
b: num & 255,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function rgbToHex(rgb) {
|
|
418
|
+
const toHex = (v) => v.toString(16).padStart(2, '0').toUpperCase();
|
|
419
|
+
return `${toHex(rgb.r)}${toHex(rgb.g)}${toHex(rgb.b)}`;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function blendHex(baseHex, overlayHex, alpha) {
|
|
423
|
+
const base = hexToRgb(baseHex);
|
|
424
|
+
const over = hexToRgb(overlayHex);
|
|
425
|
+
if (!base || !over) return overlayHex;
|
|
426
|
+
const a = Math.max(0, Math.min(1, alpha));
|
|
427
|
+
const r = Math.round(base.r * (1 - a) + over.r * a);
|
|
428
|
+
const g = Math.round(base.g * (1 - a) + over.g * a);
|
|
429
|
+
const b = Math.round(base.b * (1 - a) + over.b * a);
|
|
430
|
+
return rgbToHex({ r, g, b });
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function resolveTableBaseColor(node, root) {
|
|
434
|
+
// Start from parent to avoid picking the cell's own semi-transparent fill
|
|
435
|
+
let el = node?.parentElement || null;
|
|
436
|
+
while (el && el !== document.body) {
|
|
437
|
+
const style = window.getComputedStyle(el);
|
|
438
|
+
const bg = parseColor(style.backgroundColor);
|
|
439
|
+
if (bg.hex && bg.opacity > 0) return bg.hex;
|
|
440
|
+
|
|
441
|
+
const bgClip = style.webkitBackgroundClip || style.backgroundClip;
|
|
442
|
+
const bgImage = style.backgroundImage;
|
|
443
|
+
if (bgClip !== 'text' && bgImage && bgImage.includes('gradient')) {
|
|
444
|
+
const fallback = getGradientFallbackColor(bgImage);
|
|
445
|
+
if (fallback) {
|
|
446
|
+
const parsed = parseColor(fallback);
|
|
447
|
+
if (parsed.hex) return parsed.hex;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (el === root) break;
|
|
452
|
+
el = el.parentElement;
|
|
453
|
+
}
|
|
454
|
+
return null;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function computeTableCellFill(style, cell, root, options = {}) {
|
|
458
|
+
const bg = parseColor(style.backgroundColor);
|
|
459
|
+
if (!bg.hex || bg.opacity <= 0) return null;
|
|
460
|
+
|
|
461
|
+
const flatten = options.tableConfig?.flattenTransparentFill !== false;
|
|
462
|
+
if (bg.opacity < 1 && flatten) {
|
|
463
|
+
const baseHex = resolveTableBaseColor(cell, root) || 'FFFFFF';
|
|
464
|
+
const blended = blendHex(baseHex, bg.hex, bg.opacity);
|
|
465
|
+
return { color: blended };
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const transparency = Math.max(0, Math.min(100, (1 - bg.opacity) * 100));
|
|
469
|
+
return transparency > 0 ? { color: bg.hex, transparency } : { color: bg.hex };
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Analyzes computed border styles and determines the rendering strategy.
|
|
474
|
+
*/
|
|
475
|
+
function getBorderInfo(style, scale) {
|
|
476
|
+
const top = {
|
|
477
|
+
width: parseFloat(style.borderTopWidth) || 0,
|
|
478
|
+
style: style.borderTopStyle,
|
|
479
|
+
color: parseColor(style.borderTopColor).hex,
|
|
480
|
+
};
|
|
481
|
+
const right = {
|
|
482
|
+
width: parseFloat(style.borderRightWidth) || 0,
|
|
483
|
+
style: style.borderRightStyle,
|
|
484
|
+
color: parseColor(style.borderRightColor).hex,
|
|
485
|
+
};
|
|
486
|
+
const bottom = {
|
|
487
|
+
width: parseFloat(style.borderBottomWidth) || 0,
|
|
488
|
+
style: style.borderBottomStyle,
|
|
489
|
+
color: parseColor(style.borderBottomColor).hex,
|
|
490
|
+
};
|
|
491
|
+
const left = {
|
|
492
|
+
width: parseFloat(style.borderLeftWidth) || 0,
|
|
493
|
+
style: style.borderLeftStyle,
|
|
494
|
+
color: parseColor(style.borderLeftColor).hex,
|
|
495
|
+
};
|
|
496
|
+
|
|
497
|
+
const hasAnyBorder = top.width > 0 || right.width > 0 || bottom.width > 0 || left.width > 0;
|
|
498
|
+
if (!hasAnyBorder) return { type: 'none' };
|
|
499
|
+
|
|
500
|
+
// Check if all sides are uniform
|
|
501
|
+
const isUniform =
|
|
502
|
+
top.width === right.width &&
|
|
503
|
+
top.width === bottom.width &&
|
|
504
|
+
top.width === left.width &&
|
|
505
|
+
top.style === right.style &&
|
|
506
|
+
top.style === bottom.style &&
|
|
507
|
+
top.style === left.style &&
|
|
508
|
+
top.color === right.color &&
|
|
509
|
+
top.color === bottom.color &&
|
|
510
|
+
top.color === left.color;
|
|
511
|
+
|
|
512
|
+
if (isUniform) {
|
|
513
|
+
return {
|
|
514
|
+
type: 'uniform',
|
|
515
|
+
options: {
|
|
516
|
+
width: top.width * 0.75 * scale,
|
|
517
|
+
color: top.color,
|
|
518
|
+
transparency: (1 - parseColor(style.borderTopColor).opacity) * 100,
|
|
519
|
+
dashType: mapDashType(top.style),
|
|
520
|
+
},
|
|
521
|
+
};
|
|
522
|
+
} else {
|
|
523
|
+
return {
|
|
524
|
+
type: 'composite',
|
|
525
|
+
sides: { top, right, bottom, left },
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
/**
|
|
531
|
+
* Generates an SVG image for composite borders that respects border-radius.
|
|
532
|
+
*/
|
|
533
|
+
function generateCompositeBorderSVG(w, h, radius, sides) {
|
|
534
|
+
radius = radius / 2; // Adjust for SVG rendering
|
|
535
|
+
const clipId = 'clip_' + Math.random().toString(36).substr(2, 9);
|
|
536
|
+
let borderRects = '';
|
|
537
|
+
|
|
538
|
+
if (sides.top.width > 0 && sides.top.color) {
|
|
539
|
+
borderRects += `<rect x="0" y="0" width="${w}" height="${sides.top.width}" fill="#${sides.top.color}" />`;
|
|
540
|
+
}
|
|
541
|
+
if (sides.right.width > 0 && sides.right.color) {
|
|
542
|
+
borderRects += `<rect x="${w - sides.right.width}" y="0" width="${sides.right.width}" height="${h}" fill="#${sides.right.color}" />`;
|
|
543
|
+
}
|
|
544
|
+
if (sides.bottom.width > 0 && sides.bottom.color) {
|
|
545
|
+
borderRects += `<rect x="0" y="${h - sides.bottom.width}" width="${w}" height="${sides.bottom.width}" fill="#${sides.bottom.color}" />`;
|
|
546
|
+
}
|
|
547
|
+
if (sides.left.width > 0 && sides.left.color) {
|
|
548
|
+
borderRects += `<rect x="0" y="0" width="${sides.left.width}" height="${h}" fill="#${sides.left.color}" />`;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
const svg = `
|
|
552
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
|
|
553
|
+
<defs>
|
|
554
|
+
<clipPath id="${clipId}">
|
|
555
|
+
<rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" />
|
|
556
|
+
</clipPath>
|
|
557
|
+
</defs>
|
|
558
|
+
<g clip-path="url(#${clipId})">
|
|
559
|
+
${borderRects}
|
|
560
|
+
</g>
|
|
561
|
+
</svg>`;
|
|
562
|
+
|
|
563
|
+
return 'data:image/svg+xml;base64,' + btoa(svg);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/**
|
|
567
|
+
* Generates an SVG data URL for a solid shape with non-uniform corner radii.
|
|
568
|
+
*/
|
|
569
|
+
function generateCustomShapeSVG(w, h, color, opacity, radii) {
|
|
570
|
+
let { tl, tr, br, bl } = radii;
|
|
571
|
+
|
|
572
|
+
// Clamp radii using CSS spec logic (avoid overlap)
|
|
573
|
+
const factor = Math.min(
|
|
574
|
+
w / (tl + tr) || Infinity,
|
|
575
|
+
h / (tr + br) || Infinity,
|
|
576
|
+
w / (br + bl) || Infinity,
|
|
577
|
+
h / (bl + tl) || Infinity
|
|
578
|
+
);
|
|
579
|
+
|
|
580
|
+
if (factor < 1) {
|
|
581
|
+
tl *= factor;
|
|
582
|
+
tr *= factor;
|
|
583
|
+
br *= factor;
|
|
584
|
+
bl *= factor;
|
|
585
|
+
}
|
|
586
|
+
|
|
587
|
+
const path = `
|
|
588
|
+
M ${tl} 0
|
|
589
|
+
L ${w - tr} 0
|
|
590
|
+
A ${tr} ${tr} 0 0 1 ${w} ${tr}
|
|
591
|
+
L ${w} ${h - br}
|
|
592
|
+
A ${br} ${br} 0 0 1 ${w - br} ${h}
|
|
593
|
+
L ${bl} ${h}
|
|
594
|
+
A ${bl} ${bl} 0 0 1 0 ${h - bl}
|
|
595
|
+
L 0 ${tl}
|
|
596
|
+
A ${tl} ${tl} 0 0 1 ${tl} 0
|
|
597
|
+
Z
|
|
598
|
+
`;
|
|
599
|
+
|
|
600
|
+
const svg = `
|
|
601
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
|
|
602
|
+
<path d="${path}" fill="#${color}" fill-opacity="${opacity}" />
|
|
603
|
+
</svg>`;
|
|
604
|
+
|
|
605
|
+
return 'data:image/svg+xml;base64,' + btoa(svg);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
// --- REPLACE THE EXISTING parseColor FUNCTION ---
|
|
609
|
+
function parseColor(str) {
|
|
610
|
+
if (!str || str === 'transparent' || str.trim() === 'rgba(0, 0, 0, 0)') {
|
|
611
|
+
return { hex: null, opacity: 0 };
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
const ctx = getCtx();
|
|
615
|
+
ctx.fillStyle = str;
|
|
616
|
+
const computed = ctx.fillStyle;
|
|
617
|
+
|
|
618
|
+
// 1. Handle Hex Output (e.g. #ff0000) - Fast Path
|
|
619
|
+
if (computed.startsWith('#')) {
|
|
620
|
+
let hex = computed.slice(1);
|
|
621
|
+
let opacity = 1;
|
|
622
|
+
if (hex.length === 3) hex = hex.split('').map(c => c + c).join('');
|
|
623
|
+
if (hex.length === 4) hex = hex.split('').map(c => c + c).join('');
|
|
624
|
+
if (hex.length === 8) {
|
|
625
|
+
opacity = parseInt(hex.slice(6), 16) / 255;
|
|
626
|
+
hex = hex.slice(0, 6);
|
|
627
|
+
}
|
|
628
|
+
return { hex: hex.toUpperCase(), opacity };
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// 2. Handle RGB/RGBA Output (standard) - Fast Path
|
|
632
|
+
if (computed.startsWith('rgb')) {
|
|
633
|
+
const match = computed.match(/[\d.]+/g);
|
|
634
|
+
if (match && match.length >= 3) {
|
|
635
|
+
const r = parseInt(match[0]);
|
|
636
|
+
const g = parseInt(match[1]);
|
|
637
|
+
const b = parseInt(match[2]);
|
|
638
|
+
const a = match.length > 3 ? parseFloat(match[3]) : 1;
|
|
639
|
+
const hex = ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
|
|
640
|
+
return { hex, opacity: a };
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// 3. Fallback: Browser returned a format we don't parse (oklch, lab, color(srgb...), etc.)
|
|
645
|
+
// Use Canvas API to convert to sRGB
|
|
646
|
+
ctx.clearRect(0, 0, 1, 1);
|
|
647
|
+
ctx.fillRect(0, 0, 1, 1);
|
|
648
|
+
const data = ctx.getImageData(0, 0, 1, 1).data;
|
|
649
|
+
// data = [r, g, b, a]
|
|
650
|
+
const r = data[0];
|
|
651
|
+
const g = data[1];
|
|
652
|
+
const b = data[2];
|
|
653
|
+
const a = data[3] / 255;
|
|
654
|
+
|
|
655
|
+
if (a === 0) return { hex: null, opacity: 0 };
|
|
656
|
+
|
|
657
|
+
const hex = ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
|
|
658
|
+
return { hex, opacity: a };
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
function getPadding(style, scale) {
|
|
662
|
+
const pxToInch = 1 / 96;
|
|
663
|
+
return [
|
|
664
|
+
(parseFloat(style.paddingTop) || 0) * pxToInch * scale,
|
|
665
|
+
(parseFloat(style.paddingRight) || 0) * pxToInch * scale,
|
|
666
|
+
(parseFloat(style.paddingBottom) || 0) * pxToInch * scale,
|
|
667
|
+
(parseFloat(style.paddingLeft) || 0) * pxToInch * scale,
|
|
668
|
+
];
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
function getSoftEdges(filterStr, scale) {
|
|
672
|
+
if (!filterStr || filterStr === 'none') return null;
|
|
673
|
+
const match = filterStr.match(/blur\(([\d.]+)px\)/);
|
|
674
|
+
if (match) return parseFloat(match[1]) * 0.75 * scale;
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
const DEFAULT_CJK_FONTS = [
|
|
679
|
+
'PingFang SC',
|
|
680
|
+
'Hiragino Sans GB',
|
|
681
|
+
'Microsoft YaHei',
|
|
682
|
+
'Noto Sans CJK SC',
|
|
683
|
+
'Source Han Sans SC',
|
|
684
|
+
'WenQuanYi Micro Hei',
|
|
685
|
+
'SimHei',
|
|
686
|
+
'SimSun',
|
|
687
|
+
'STHeiti'
|
|
688
|
+
];
|
|
689
|
+
|
|
690
|
+
function normalizeFontList(fontFamily) {
|
|
691
|
+
if (!fontFamily || typeof fontFamily !== 'string') return [];
|
|
692
|
+
return fontFamily
|
|
693
|
+
.split(',')
|
|
694
|
+
.map((f) => f.trim().replace(/['"]/g, ''))
|
|
695
|
+
.filter(Boolean);
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
function containsCjk(text) {
|
|
699
|
+
if (!text) return false;
|
|
700
|
+
return /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/.test(text);
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
function normalizeCjkFallbacks(options) {
|
|
704
|
+
if (!options) return [];
|
|
705
|
+
const raw =
|
|
706
|
+
options.fontFallbacks?.cjk ??
|
|
707
|
+
options.cjkFonts ??
|
|
708
|
+
options.cjkFont ??
|
|
709
|
+
[];
|
|
710
|
+
const list = Array.isArray(raw) ? raw : [raw];
|
|
711
|
+
return list
|
|
712
|
+
.map((f) => (typeof f === 'string' ? f.trim() : String(f)))
|
|
713
|
+
.filter(Boolean);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
function pickFontFace(fontFamily, text, options) {
|
|
717
|
+
const fontList = normalizeFontList(fontFamily);
|
|
718
|
+
const primary = fontList[0] || 'Arial';
|
|
719
|
+
|
|
720
|
+
if (!containsCjk(text)) return primary;
|
|
721
|
+
|
|
722
|
+
const lowered = fontList.map((f) => f.toLowerCase());
|
|
723
|
+
const configured = normalizeCjkFallbacks(options);
|
|
724
|
+
if (configured.length > 0) {
|
|
725
|
+
const match = configured.find((f) => lowered.includes(f.toLowerCase()));
|
|
726
|
+
return match || configured[0];
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
const autoMatch = DEFAULT_CJK_FONTS.find((f) => lowered.includes(f.toLowerCase()));
|
|
730
|
+
return autoMatch || primary;
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
function getTextStyle(style, scale, text = '', options = {}) {
|
|
734
|
+
let colorObj = parseColor(style.color);
|
|
735
|
+
|
|
736
|
+
const bgClip = style.webkitBackgroundClip || style.backgroundClip;
|
|
737
|
+
if (colorObj.opacity === 0 && bgClip === 'text') {
|
|
738
|
+
const fallback = getGradientFallbackColor(style.backgroundImage);
|
|
739
|
+
if (fallback) colorObj = parseColor(fallback);
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
let lineSpacing = null;
|
|
743
|
+
const fontSizePx = parseFloat(style.fontSize);
|
|
744
|
+
const lhStr = style.lineHeight;
|
|
745
|
+
|
|
746
|
+
if (lhStr && lhStr !== 'normal') {
|
|
747
|
+
let lhPx = parseFloat(lhStr);
|
|
748
|
+
|
|
749
|
+
// Edge Case: If browser returns a raw multiplier (e.g. "1.5")
|
|
750
|
+
// we must multiply by font size to get the height in pixels.
|
|
751
|
+
// (Note: getComputedStyle usually returns 'px', but inline styles might differ)
|
|
752
|
+
if (/^[0-9.]+$/.test(lhStr)) {
|
|
753
|
+
lhPx = lhPx * fontSizePx;
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
if (!isNaN(lhPx) && lhPx > 0) {
|
|
757
|
+
// Convert Pixel Height to Point Height (1px = 0.75pt)
|
|
758
|
+
// And apply the global layout scale.
|
|
759
|
+
lineSpacing = lhPx * 0.75 * scale;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
// --- Spacing (Margins) ---
|
|
764
|
+
// Convert CSS margins (px) to PPTX Paragraph Spacing (pt).
|
|
765
|
+
let paraSpaceBefore = 0;
|
|
766
|
+
let paraSpaceAfter = 0;
|
|
767
|
+
|
|
768
|
+
const mt = parseFloat(style.marginTop) || 0;
|
|
769
|
+
const mb = parseFloat(style.marginBottom) || 0;
|
|
770
|
+
|
|
771
|
+
if (mt > 0) paraSpaceBefore = mt * 0.75 * scale;
|
|
772
|
+
if (mb > 0) paraSpaceAfter = mb * 0.75 * scale;
|
|
773
|
+
|
|
774
|
+
const fontFace = pickFontFace(style.fontFamily, text, options);
|
|
775
|
+
|
|
776
|
+
return {
|
|
777
|
+
color: colorObj.hex || '000000',
|
|
778
|
+
fontFace: fontFace,
|
|
779
|
+
fontSize: Math.floor(fontSizePx * 0.75 * scale),
|
|
780
|
+
bold: parseInt(style.fontWeight) >= 600,
|
|
781
|
+
italic: style.fontStyle === 'italic',
|
|
782
|
+
underline: style.textDecoration.includes('underline'),
|
|
783
|
+
// Only add if we have a valid value
|
|
784
|
+
...(lineSpacing && { lineSpacing }),
|
|
785
|
+
...(paraSpaceBefore > 0 && { paraSpaceBefore }),
|
|
786
|
+
...(paraSpaceAfter > 0 && { paraSpaceAfter }),
|
|
787
|
+
// Map background color to highlight if present
|
|
788
|
+
...(parseColor(style.backgroundColor).hex ? { highlight: parseColor(style.backgroundColor).hex } : {}),
|
|
789
|
+
};
|
|
790
|
+
}
|
|
791
|
+
|
|
792
|
+
/**
|
|
793
|
+
* Determines if a given DOM node is primarily a text container.
|
|
794
|
+
* Updated to correctly reject Icon elements so they are rendered as images.
|
|
795
|
+
*/
|
|
796
|
+
function isTextContainer(node) {
|
|
797
|
+
const hasText = node.textContent.trim().length > 0;
|
|
798
|
+
if (!hasText) return false;
|
|
799
|
+
|
|
800
|
+
const children = Array.from(node.children);
|
|
801
|
+
if (children.length === 0) return true;
|
|
802
|
+
|
|
803
|
+
const isSafeInline = (el) => {
|
|
804
|
+
// 1. Reject Web Components / Custom Elements
|
|
805
|
+
if (el.tagName.includes('-')) return false;
|
|
806
|
+
// 2. Reject Explicit Images/SVGs
|
|
807
|
+
if (el.tagName === 'IMG' || el.tagName === 'SVG') return false;
|
|
808
|
+
|
|
809
|
+
// 3. Reject Class-based Icons (FontAwesome, Material, Bootstrap, etc.)
|
|
810
|
+
// If an <i> or <span> has icon classes, it is a visual object, not text.
|
|
811
|
+
if (el.tagName === 'I' || el.tagName === 'SPAN') {
|
|
812
|
+
const cls = el.getAttribute('class') || '';
|
|
813
|
+
if (
|
|
814
|
+
cls.includes('fa-') ||
|
|
815
|
+
cls.includes('fas') ||
|
|
816
|
+
cls.includes('far') ||
|
|
817
|
+
cls.includes('fab') ||
|
|
818
|
+
cls.includes('material-icons') ||
|
|
819
|
+
cls.includes('bi-') ||
|
|
820
|
+
cls.includes('icon')
|
|
821
|
+
) {
|
|
822
|
+
return false;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
|
|
826
|
+
const style = window.getComputedStyle(el);
|
|
827
|
+
const display = style.display;
|
|
828
|
+
|
|
829
|
+
// 4. Standard Inline Tag Check
|
|
830
|
+
const isInlineTag = ['SPAN', 'B', 'STRONG', 'EM', 'I', 'A', 'SMALL', 'MARK'].includes(
|
|
831
|
+
el.tagName
|
|
832
|
+
);
|
|
833
|
+
const isInlineDisplay = display.includes('inline');
|
|
834
|
+
|
|
835
|
+
if (!isInlineTag && !isInlineDisplay) return false;
|
|
836
|
+
|
|
837
|
+
// 5. Structural Styling Check
|
|
838
|
+
// If a child has a background or border, it's a layout block, not a simple text span.
|
|
839
|
+
const bgColor = parseColor(style.backgroundColor);
|
|
840
|
+
const hasVisibleBg = bgColor.hex && bgColor.opacity > 0;
|
|
841
|
+
const hasBorder =
|
|
842
|
+
parseFloat(style.borderWidth) > 0 && parseColor(style.borderColor).opacity > 0;
|
|
843
|
+
|
|
844
|
+
// 4. Check for empty shapes (visual objects without text, like dots)
|
|
845
|
+
const hasContent = el.textContent.trim().length > 0;
|
|
846
|
+
if (!hasContent && (hasVisibleBg || hasBorder)) {
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// If element has background/border and is not inline, treat as layout block
|
|
851
|
+
if ((hasVisibleBg || hasBorder) && !isInlineDisplay) {
|
|
852
|
+
return false;
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
return true;
|
|
856
|
+
};
|
|
857
|
+
|
|
858
|
+
return children.every(isSafeInline);
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function getRotation(transformStr) {
|
|
862
|
+
if (!transformStr || transformStr === 'none') return 0;
|
|
863
|
+
const values = transformStr.split('(')[1].split(')')[0].split(',');
|
|
864
|
+
if (values.length < 4) return 0;
|
|
865
|
+
const a = parseFloat(values[0]);
|
|
866
|
+
const b = parseFloat(values[1]);
|
|
867
|
+
return Math.round(Math.atan2(b, a) * (180 / Math.PI));
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function svgToPng(node) {
|
|
871
|
+
return new Promise((resolve) => {
|
|
872
|
+
const clone = node.cloneNode(true);
|
|
873
|
+
const rect = node.getBoundingClientRect();
|
|
874
|
+
const width = rect.width || 300;
|
|
875
|
+
const height = rect.height || 150;
|
|
876
|
+
|
|
877
|
+
function inlineStyles(source, target) {
|
|
878
|
+
const computed = window.getComputedStyle(source);
|
|
879
|
+
const properties = [
|
|
880
|
+
'fill',
|
|
881
|
+
'stroke',
|
|
882
|
+
'stroke-width',
|
|
883
|
+
'stroke-linecap',
|
|
884
|
+
'stroke-linejoin',
|
|
885
|
+
'opacity',
|
|
886
|
+
'font-family',
|
|
887
|
+
'font-size',
|
|
888
|
+
'font-weight',
|
|
889
|
+
];
|
|
890
|
+
|
|
891
|
+
if (computed.fill === 'none') target.setAttribute('fill', 'none');
|
|
892
|
+
else if (computed.fill) target.style.fill = computed.fill;
|
|
893
|
+
|
|
894
|
+
if (computed.stroke === 'none') target.setAttribute('stroke', 'none');
|
|
895
|
+
else if (computed.stroke) target.style.stroke = computed.stroke;
|
|
896
|
+
|
|
897
|
+
properties.forEach((prop) => {
|
|
898
|
+
if (prop !== 'fill' && prop !== 'stroke') {
|
|
899
|
+
const val = computed[prop];
|
|
900
|
+
if (val && val !== 'auto') target.style[prop] = val;
|
|
901
|
+
}
|
|
902
|
+
});
|
|
903
|
+
|
|
904
|
+
for (let i = 0; i < source.children.length; i++) {
|
|
905
|
+
if (target.children[i]) inlineStyles(source.children[i], target.children[i]);
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
inlineStyles(node, clone);
|
|
910
|
+
clone.setAttribute('width', width);
|
|
911
|
+
clone.setAttribute('height', height);
|
|
912
|
+
clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
|
|
913
|
+
|
|
914
|
+
const xml = new XMLSerializer().serializeToString(clone);
|
|
915
|
+
const svgUrl = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xml)}`;
|
|
916
|
+
const img = new Image();
|
|
917
|
+
img.crossOrigin = 'Anonymous';
|
|
918
|
+
img.onload = () => {
|
|
919
|
+
const canvas = document.createElement('canvas');
|
|
920
|
+
const scale = 3;
|
|
921
|
+
canvas.width = width * scale;
|
|
922
|
+
canvas.height = height * scale;
|
|
923
|
+
const ctx = canvas.getContext('2d');
|
|
924
|
+
ctx.scale(scale, scale);
|
|
925
|
+
ctx.drawImage(img, 0, 0, width, height);
|
|
926
|
+
resolve(canvas.toDataURL('image/png'));
|
|
927
|
+
};
|
|
928
|
+
img.onerror = () => resolve(null);
|
|
929
|
+
img.src = svgUrl;
|
|
930
|
+
});
|
|
931
|
+
}
|
|
932
|
+
|
|
933
|
+
function getVisibleShadow(shadowStr, scale) {
|
|
934
|
+
if (!shadowStr || shadowStr === 'none') return null;
|
|
935
|
+
const shadows = shadowStr.split(/,(?![^()]*\))/);
|
|
936
|
+
for (let s of shadows) {
|
|
937
|
+
s = s.trim();
|
|
938
|
+
if (s.startsWith('rgba(0, 0, 0, 0)')) continue;
|
|
939
|
+
const match = s.match(
|
|
940
|
+
/(rgba?\([^)]+\)|#[0-9a-fA-F]+)\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px/
|
|
941
|
+
);
|
|
942
|
+
if (match) {
|
|
943
|
+
const colorStr = match[1];
|
|
944
|
+
const x = parseFloat(match[2]);
|
|
945
|
+
const y = parseFloat(match[3]);
|
|
946
|
+
const blur = parseFloat(match[4]);
|
|
947
|
+
const distance = Math.sqrt(x * x + y * y);
|
|
948
|
+
let angle = Math.atan2(y, x) * (180 / Math.PI);
|
|
949
|
+
if (angle < 0) angle += 360;
|
|
950
|
+
const colorObj = parseColor(colorStr);
|
|
951
|
+
return {
|
|
952
|
+
type: 'outer',
|
|
953
|
+
angle: angle,
|
|
954
|
+
blur: blur * 0.75 * scale,
|
|
955
|
+
offset: distance * 0.75 * scale,
|
|
956
|
+
color: colorObj.hex || '000000',
|
|
957
|
+
opacity: colorObj.opacity,
|
|
958
|
+
};
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
return null;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* Generates an SVG image for gradients, supporting degrees and keywords.
|
|
966
|
+
*/
|
|
967
|
+
function generateGradientSVG(w, h, bgString, radius, border) {
|
|
968
|
+
try {
|
|
969
|
+
const match = bgString.match(/linear-gradient\((.*)\)/);
|
|
970
|
+
if (!match) return null;
|
|
971
|
+
const content = match[1];
|
|
972
|
+
|
|
973
|
+
// Split by comma, ignoring commas inside parentheses (e.g. rgba())
|
|
974
|
+
const parts = content.split(/,(?![^()]*\))/).map((p) => p.trim());
|
|
975
|
+
if (parts.length < 2) return null;
|
|
976
|
+
|
|
977
|
+
let x1 = '0%',
|
|
978
|
+
y1 = '0%',
|
|
979
|
+
x2 = '0%',
|
|
980
|
+
y2 = '100%';
|
|
981
|
+
let stopsStartIndex = 0;
|
|
982
|
+
const firstPart = parts[0].toLowerCase();
|
|
983
|
+
|
|
984
|
+
// 1. Check for Keywords (to right, etc.)
|
|
985
|
+
if (firstPart.startsWith('to ')) {
|
|
986
|
+
stopsStartIndex = 1;
|
|
987
|
+
const direction = firstPart.replace('to ', '').trim();
|
|
988
|
+
switch (direction) {
|
|
989
|
+
case 'top':
|
|
990
|
+
y1 = '100%';
|
|
991
|
+
y2 = '0%';
|
|
992
|
+
break;
|
|
993
|
+
case 'bottom':
|
|
994
|
+
y1 = '0%';
|
|
995
|
+
y2 = '100%';
|
|
996
|
+
break;
|
|
997
|
+
case 'left':
|
|
998
|
+
x1 = '100%';
|
|
999
|
+
x2 = '0%';
|
|
1000
|
+
break;
|
|
1001
|
+
case 'right':
|
|
1002
|
+
x2 = '100%';
|
|
1003
|
+
break;
|
|
1004
|
+
case 'top right':
|
|
1005
|
+
x1 = '0%';
|
|
1006
|
+
y1 = '100%';
|
|
1007
|
+
x2 = '100%';
|
|
1008
|
+
y2 = '0%';
|
|
1009
|
+
break;
|
|
1010
|
+
case 'top left':
|
|
1011
|
+
x1 = '100%';
|
|
1012
|
+
y1 = '100%';
|
|
1013
|
+
x2 = '0%';
|
|
1014
|
+
y2 = '0%';
|
|
1015
|
+
break;
|
|
1016
|
+
case 'bottom right':
|
|
1017
|
+
x2 = '100%';
|
|
1018
|
+
y2 = '100%';
|
|
1019
|
+
break;
|
|
1020
|
+
case 'bottom left':
|
|
1021
|
+
x1 = '100%';
|
|
1022
|
+
y2 = '100%';
|
|
1023
|
+
break;
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
// 2. Check for Degrees (45deg, 90deg, etc.)
|
|
1027
|
+
else if (firstPart.match(/^-?[\d.]+(deg|rad|turn|grad)$/)) {
|
|
1028
|
+
stopsStartIndex = 1;
|
|
1029
|
+
const val = parseFloat(firstPart);
|
|
1030
|
+
// CSS 0deg is Top (North), 90deg is Right (East), 180deg is Bottom (South)
|
|
1031
|
+
// We convert this to SVG coordinates on a unit square (0-100%).
|
|
1032
|
+
// Formula: Map angle to perimeter coordinates.
|
|
1033
|
+
if (!isNaN(val)) {
|
|
1034
|
+
const deg = firstPart.includes('rad') ? val * (180 / Math.PI) : val;
|
|
1035
|
+
const cssRad = ((deg - 90) * Math.PI) / 180; // Correct CSS angle offset
|
|
1036
|
+
|
|
1037
|
+
// Calculate standard vector for rectangle center (50, 50)
|
|
1038
|
+
const scale = 50; // Distance from center to edge (approx)
|
|
1039
|
+
const cos = Math.cos(cssRad); // Y component (reversed in SVG)
|
|
1040
|
+
const sin = Math.sin(cssRad); // X component
|
|
1041
|
+
|
|
1042
|
+
// Invert Y for SVG coordinate system
|
|
1043
|
+
x1 = (50 - sin * scale).toFixed(1) + '%';
|
|
1044
|
+
y1 = (50 + cos * scale).toFixed(1) + '%';
|
|
1045
|
+
x2 = (50 + sin * scale).toFixed(1) + '%';
|
|
1046
|
+
y2 = (50 - cos * scale).toFixed(1) + '%';
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
// 3. Process Color Stops
|
|
1051
|
+
let stopsXML = '';
|
|
1052
|
+
const stopParts = parts.slice(stopsStartIndex);
|
|
1053
|
+
|
|
1054
|
+
stopParts.forEach((part, idx) => {
|
|
1055
|
+
// Parse "Color Position" (e.g., "red 50%")
|
|
1056
|
+
// Regex looks for optional space + number + unit at the end of the string
|
|
1057
|
+
let color = part;
|
|
1058
|
+
let offset = Math.round((idx / (stopParts.length - 1)) * 100) + '%';
|
|
1059
|
+
|
|
1060
|
+
const posMatch = part.match(/^(.*?)\s+(-?[\d.]+(?:%|px)?)$/);
|
|
1061
|
+
if (posMatch) {
|
|
1062
|
+
color = posMatch[1];
|
|
1063
|
+
offset = posMatch[2];
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
// Handle RGBA/RGB for SVG compatibility
|
|
1067
|
+
let opacity = 1;
|
|
1068
|
+
if (color.includes('rgba')) {
|
|
1069
|
+
const rgbaMatch = color.match(/[\d.]+/g);
|
|
1070
|
+
if (rgbaMatch && rgbaMatch.length >= 4) {
|
|
1071
|
+
opacity = rgbaMatch[3];
|
|
1072
|
+
color = `rgb(${rgbaMatch[0]},${rgbaMatch[1]},${rgbaMatch[2]})`;
|
|
1073
|
+
}
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
stopsXML += `<stop offset="${offset}" stop-color="${color.trim()}" stop-opacity="${opacity}"/>`;
|
|
1077
|
+
});
|
|
1078
|
+
|
|
1079
|
+
let strokeAttr = '';
|
|
1080
|
+
if (border) {
|
|
1081
|
+
strokeAttr = `stroke="#${border.color}" stroke-width="${border.width}"`;
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
const svg = `
|
|
1085
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
|
|
1086
|
+
<defs>
|
|
1087
|
+
<linearGradient id="grad" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">
|
|
1088
|
+
${stopsXML}
|
|
1089
|
+
</linearGradient>
|
|
1090
|
+
</defs>
|
|
1091
|
+
<rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="url(#grad)" ${strokeAttr} />
|
|
1092
|
+
</svg>`;
|
|
1093
|
+
|
|
1094
|
+
return 'data:image/svg+xml;base64,' + btoa(svg);
|
|
1095
|
+
} catch (e) {
|
|
1096
|
+
console.warn('Gradient generation failed:', e);
|
|
1097
|
+
return null;
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
function generateBlurredSVG(w, h, color, radius, blurPx) {
|
|
1102
|
+
const padding = blurPx * 3;
|
|
1103
|
+
const fullW = w + padding * 2;
|
|
1104
|
+
const fullH = h + padding * 2;
|
|
1105
|
+
const x = padding;
|
|
1106
|
+
const y = padding;
|
|
1107
|
+
let shapeTag = '';
|
|
1108
|
+
const isCircle = radius >= Math.min(w, h) / 2 - 1 && Math.abs(w - h) < 2;
|
|
1109
|
+
|
|
1110
|
+
if (isCircle) {
|
|
1111
|
+
const cx = x + w / 2;
|
|
1112
|
+
const cy = y + h / 2;
|
|
1113
|
+
const rx = w / 2;
|
|
1114
|
+
const ry = h / 2;
|
|
1115
|
+
shapeTag = `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" fill="#${color}" filter="url(#f1)" />`;
|
|
1116
|
+
} else {
|
|
1117
|
+
shapeTag = `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="#${color}" filter="url(#f1)" />`;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
const svg = `
|
|
1121
|
+
<svg xmlns="http://www.w3.org/2000/svg" width="${fullW}" height="${fullH}" viewBox="0 0 ${fullW} ${fullH}">
|
|
1122
|
+
<defs>
|
|
1123
|
+
<filter id="f1" x="-50%" y="-50%" width="200%" height="200%">
|
|
1124
|
+
<feGaussianBlur in="SourceGraphic" stdDeviation="${blurPx}" />
|
|
1125
|
+
</filter>
|
|
1126
|
+
</defs>
|
|
1127
|
+
${shapeTag}
|
|
1128
|
+
</svg>`;
|
|
1129
|
+
|
|
1130
|
+
return {
|
|
1131
|
+
data: 'data:image/svg+xml;base64,' + btoa(svg),
|
|
1132
|
+
padding: padding,
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
|
|
1136
|
+
// src/utils.js
|
|
1137
|
+
|
|
1138
|
+
// ... (keep all existing exports) ...
|
|
1139
|
+
|
|
1140
|
+
/**
|
|
1141
|
+
* Traverses the target DOM and collects all unique font-family names used.
|
|
1142
|
+
*/
|
|
1143
|
+
function getUsedFontFamilies(root) {
|
|
1144
|
+
const families = new Set();
|
|
1145
|
+
|
|
1146
|
+
function scan(node) {
|
|
1147
|
+
if (node.nodeType === 1) {
|
|
1148
|
+
// Element
|
|
1149
|
+
const style = window.getComputedStyle(node);
|
|
1150
|
+
const fontList = normalizeFontList(style.fontFamily);
|
|
1151
|
+
fontList.forEach((f) => families.add(f));
|
|
1152
|
+
}
|
|
1153
|
+
for (const child of node.childNodes) {
|
|
1154
|
+
scan(child);
|
|
1155
|
+
}
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
// Handle array of roots or single root
|
|
1159
|
+
const elements = Array.isArray(root) ? root : [root];
|
|
1160
|
+
elements.forEach((el) => {
|
|
1161
|
+
const node = typeof el === 'string' ? document.querySelector(el) : el;
|
|
1162
|
+
if (node) scan(node);
|
|
1163
|
+
});
|
|
1164
|
+
|
|
1165
|
+
return families;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
/**
|
|
1169
|
+
* Scans document.styleSheets to find @font-face URLs for the requested families.
|
|
1170
|
+
* Returns an array of { name, url } objects.
|
|
1171
|
+
*/
|
|
1172
|
+
async function getAutoDetectedFonts(usedFamilies) {
|
|
1173
|
+
const foundFonts = [];
|
|
1174
|
+
const processedUrls = new Set();
|
|
1175
|
+
|
|
1176
|
+
// Helper to extract clean URL from CSS src string
|
|
1177
|
+
const extractUrl = (srcStr) => {
|
|
1178
|
+
// Look for url("...") or url('...') or url(...)
|
|
1179
|
+
// Prioritize woff, ttf, otf. Avoid woff2 if possible as handling is harder,
|
|
1180
|
+
// but if it's the only one, take it (convert logic handles it best effort).
|
|
1181
|
+
const matches = srcStr.match(/url\((['"]?)(.*?)\1\)/g);
|
|
1182
|
+
if (!matches) return null;
|
|
1183
|
+
|
|
1184
|
+
// Filter for preferred formats
|
|
1185
|
+
let chosenUrl = null;
|
|
1186
|
+
for (const match of matches) {
|
|
1187
|
+
const urlRaw = match.replace(/url\((['"]?)(.*?)\1\)/, '$2');
|
|
1188
|
+
// Skip data URIs for now (unless you want to support base64 embedding)
|
|
1189
|
+
if (urlRaw.startsWith('data:')) continue;
|
|
1190
|
+
|
|
1191
|
+
if (urlRaw.includes('.ttf') || urlRaw.includes('.otf') || urlRaw.includes('.woff')) {
|
|
1192
|
+
chosenUrl = urlRaw;
|
|
1193
|
+
break; // Found a good one
|
|
1194
|
+
}
|
|
1195
|
+
// Fallback
|
|
1196
|
+
if (!chosenUrl) chosenUrl = urlRaw;
|
|
1197
|
+
}
|
|
1198
|
+
return chosenUrl;
|
|
1199
|
+
};
|
|
1200
|
+
|
|
1201
|
+
for (const sheet of Array.from(document.styleSheets)) {
|
|
1202
|
+
try {
|
|
1203
|
+
// Accessing cssRules on cross-origin sheets (like Google Fonts) might fail
|
|
1204
|
+
// if CORS headers aren't set. We wrap in try/catch.
|
|
1205
|
+
const rules = sheet.cssRules || sheet.rules;
|
|
1206
|
+
if (!rules) continue;
|
|
1207
|
+
|
|
1208
|
+
for (const rule of Array.from(rules)) {
|
|
1209
|
+
if (rule.constructor.name === 'CSSFontFaceRule' || rule.type === 5) {
|
|
1210
|
+
const familyName = rule.style.getPropertyValue('font-family').replace(/['"]/g, '').trim();
|
|
1211
|
+
|
|
1212
|
+
if (usedFamilies.has(familyName)) {
|
|
1213
|
+
const src = rule.style.getPropertyValue('src');
|
|
1214
|
+
const url = extractUrl(src);
|
|
1215
|
+
|
|
1216
|
+
if (url && !processedUrls.has(url)) {
|
|
1217
|
+
processedUrls.add(url);
|
|
1218
|
+
foundFonts.push({ name: familyName, url: url });
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
} catch (e) {
|
|
1224
|
+
// SecurityError is common for external stylesheets (CORS).
|
|
1225
|
+
// We cannot scan those automatically via CSSOM.
|
|
1226
|
+
console.warn('error:', e);
|
|
1227
|
+
console.warn('Cannot scan stylesheet for fonts (CORS restriction):', sheet.href);
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
return foundFonts;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
// src/image-processor.js
|
|
1235
|
+
|
|
1236
|
+
async function getProcessedImage(
|
|
1237
|
+
src,
|
|
1238
|
+
targetW,
|
|
1239
|
+
targetH,
|
|
1240
|
+
radius,
|
|
1241
|
+
objectFit = 'fill',
|
|
1242
|
+
objectPosition = '50% 50%'
|
|
1243
|
+
) {
|
|
1244
|
+
return new Promise((resolve) => {
|
|
1245
|
+
const img = new Image();
|
|
1246
|
+
img.crossOrigin = 'Anonymous';
|
|
1247
|
+
|
|
1248
|
+
img.onload = () => {
|
|
1249
|
+
const canvas = document.createElement('canvas');
|
|
1250
|
+
const scale = 2; // Double resolution
|
|
1251
|
+
canvas.width = targetW * scale;
|
|
1252
|
+
canvas.height = targetH * scale;
|
|
1253
|
+
const ctx = canvas.getContext('2d');
|
|
1254
|
+
ctx.scale(scale, scale);
|
|
1255
|
+
|
|
1256
|
+
// Normalize radius
|
|
1257
|
+
let r = { tl: 0, tr: 0, br: 0, bl: 0 };
|
|
1258
|
+
if (typeof radius === 'number') {
|
|
1259
|
+
r = { tl: radius, tr: radius, br: radius, bl: radius };
|
|
1260
|
+
} else if (typeof radius === 'object' && radius !== null) {
|
|
1261
|
+
r = { ...r, ...radius };
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
// 1. Draw Mask
|
|
1265
|
+
ctx.beginPath();
|
|
1266
|
+
// ... (radius clamping logic remains the same) ...
|
|
1267
|
+
const factor = Math.min(
|
|
1268
|
+
targetW / (r.tl + r.tr) || Infinity,
|
|
1269
|
+
targetH / (r.tr + r.br) || Infinity,
|
|
1270
|
+
targetW / (r.br + r.bl) || Infinity,
|
|
1271
|
+
targetH / (r.bl + r.tl) || Infinity
|
|
1272
|
+
);
|
|
1273
|
+
|
|
1274
|
+
if (factor < 1) {
|
|
1275
|
+
r.tl *= factor;
|
|
1276
|
+
r.tr *= factor;
|
|
1277
|
+
r.br *= factor;
|
|
1278
|
+
r.bl *= factor;
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
ctx.moveTo(r.tl, 0);
|
|
1282
|
+
ctx.lineTo(targetW - r.tr, 0);
|
|
1283
|
+
ctx.arcTo(targetW, 0, targetW, r.tr, r.tr);
|
|
1284
|
+
ctx.lineTo(targetW, targetH - r.br);
|
|
1285
|
+
ctx.arcTo(targetW, targetH, targetW - r.br, targetH, r.br);
|
|
1286
|
+
ctx.lineTo(r.bl, targetH);
|
|
1287
|
+
ctx.arcTo(0, targetH, 0, targetH - r.bl, r.bl);
|
|
1288
|
+
ctx.lineTo(0, r.tl);
|
|
1289
|
+
ctx.arcTo(0, 0, r.tl, 0, r.tl);
|
|
1290
|
+
ctx.closePath();
|
|
1291
|
+
ctx.fillStyle = '#000';
|
|
1292
|
+
ctx.fill();
|
|
1293
|
+
|
|
1294
|
+
// 2. Composite Source-In
|
|
1295
|
+
ctx.globalCompositeOperation = 'source-in';
|
|
1296
|
+
|
|
1297
|
+
// 3. Draw Image with Object Fit logic
|
|
1298
|
+
const wRatio = targetW / img.width;
|
|
1299
|
+
const hRatio = targetH / img.height;
|
|
1300
|
+
let renderW, renderH;
|
|
1301
|
+
|
|
1302
|
+
if (objectFit === 'contain') {
|
|
1303
|
+
const fitScale = Math.min(wRatio, hRatio);
|
|
1304
|
+
renderW = img.width * fitScale;
|
|
1305
|
+
renderH = img.height * fitScale;
|
|
1306
|
+
} else if (objectFit === 'cover') {
|
|
1307
|
+
const coverScale = Math.max(wRatio, hRatio);
|
|
1308
|
+
renderW = img.width * coverScale;
|
|
1309
|
+
renderH = img.height * coverScale;
|
|
1310
|
+
} else if (objectFit === 'none') {
|
|
1311
|
+
renderW = img.width;
|
|
1312
|
+
renderH = img.height;
|
|
1313
|
+
} else if (objectFit === 'scale-down') {
|
|
1314
|
+
const scaleDown = Math.min(1, Math.min(wRatio, hRatio));
|
|
1315
|
+
renderW = img.width * scaleDown;
|
|
1316
|
+
renderH = img.height * scaleDown;
|
|
1317
|
+
} else {
|
|
1318
|
+
// 'fill' (default)
|
|
1319
|
+
renderW = targetW;
|
|
1320
|
+
renderH = targetH;
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
// Handle Object Position (simplified parsing for "x% y%" or keywords)
|
|
1324
|
+
let posX = 0.5; // Default center
|
|
1325
|
+
let posY = 0.5;
|
|
1326
|
+
|
|
1327
|
+
const posParts = objectPosition.split(' ');
|
|
1328
|
+
if (posParts.length > 0) {
|
|
1329
|
+
const parsePos = (val) => {
|
|
1330
|
+
if (val === 'left' || val === 'top') return 0;
|
|
1331
|
+
if (val === 'center') return 0.5;
|
|
1332
|
+
if (val === 'right' || val === 'bottom') return 1;
|
|
1333
|
+
if (val.includes('%')) return parseFloat(val) / 100;
|
|
1334
|
+
return 0.5; // fallback
|
|
1335
|
+
};
|
|
1336
|
+
posX = parsePos(posParts[0]);
|
|
1337
|
+
posY = posParts.length > 1 ? parsePos(posParts[1]) : 0.5;
|
|
1338
|
+
}
|
|
1339
|
+
|
|
1340
|
+
const renderX = (targetW - renderW) * posX;
|
|
1341
|
+
const renderY = (targetH - renderH) * posY;
|
|
1342
|
+
|
|
1343
|
+
ctx.drawImage(img, renderX, renderY, renderW, renderH);
|
|
1344
|
+
|
|
1345
|
+
resolve(canvas.toDataURL('image/png'));
|
|
1346
|
+
};
|
|
1347
|
+
|
|
1348
|
+
img.onerror = () => resolve(null);
|
|
1349
|
+
img.src = src;
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
// src/index.js
|
|
1354
|
+
|
|
1355
|
+
// Normalize import
|
|
1356
|
+
const PptxGenJS = PptxGenJSImport?.default ?? PptxGenJSImport;
|
|
1357
|
+
|
|
1358
|
+
const PPI = 96;
|
|
1359
|
+
const PX_TO_INCH = 1 / PPI;
|
|
1360
|
+
|
|
1361
|
+
/**
|
|
1362
|
+
* Main export function.
|
|
1363
|
+
* @param {HTMLElement | string | Array<HTMLElement | string>} target
|
|
1364
|
+
* @param {Object} options
|
|
1365
|
+
* @param {string} [options.fileName]
|
|
1366
|
+
* @param {boolean} [options.skipDownload=false] - If true, prevents automatic download
|
|
1367
|
+
* @param {Object} [options.listConfig] - Config for bullets
|
|
1368
|
+
* @returns {Promise<Blob>} - Returns the generated PPTX Blob
|
|
1369
|
+
*/
|
|
1370
|
+
async function exportToPptx(target, options = {}) {
|
|
1371
|
+
const resolvePptxConstructor = (pkg) => {
|
|
1372
|
+
if (!pkg) return null;
|
|
1373
|
+
if (typeof pkg === 'function') return pkg;
|
|
1374
|
+
if (pkg && typeof pkg.default === 'function') return pkg.default;
|
|
1375
|
+
if (pkg && typeof pkg.PptxGenJS === 'function') return pkg.PptxGenJS;
|
|
1376
|
+
if (pkg && pkg.PptxGenJS && typeof pkg.PptxGenJS.default === 'function')
|
|
1377
|
+
return pkg.PptxGenJS.default;
|
|
1378
|
+
return null;
|
|
1379
|
+
};
|
|
1380
|
+
|
|
1381
|
+
const PptxConstructor = resolvePptxConstructor(PptxGenJS);
|
|
1382
|
+
if (!PptxConstructor) throw new Error('PptxGenJS constructor not found.');
|
|
1383
|
+
const pptx = new PptxConstructor();
|
|
1384
|
+
pptx.layout = 'LAYOUT_16x9';
|
|
1385
|
+
|
|
1386
|
+
const elements = Array.isArray(target) ? target : [target];
|
|
1387
|
+
|
|
1388
|
+
for (const el of elements) {
|
|
1389
|
+
const root = typeof el === 'string' ? document.querySelector(el) : el;
|
|
1390
|
+
if (!root) {
|
|
1391
|
+
console.warn('Element not found, skipping slide:', el);
|
|
1392
|
+
continue;
|
|
1393
|
+
}
|
|
1394
|
+
const slide = pptx.addSlide();
|
|
1395
|
+
await processSlide(root, slide, pptx, options);
|
|
1396
|
+
}
|
|
1397
|
+
|
|
1398
|
+
// 3. Font Embedding Logic
|
|
1399
|
+
let finalBlob;
|
|
1400
|
+
let fontsToEmbed = options.fonts || [];
|
|
1401
|
+
|
|
1402
|
+
if (options.autoEmbedFonts) {
|
|
1403
|
+
// A. Scan DOM for used font families
|
|
1404
|
+
const usedFamilies = getUsedFontFamilies(elements);
|
|
1405
|
+
|
|
1406
|
+
// B. Scan CSS for URLs matches
|
|
1407
|
+
const detectedFonts = await getAutoDetectedFonts(usedFamilies);
|
|
1408
|
+
|
|
1409
|
+
// C. Merge (Avoid duplicates)
|
|
1410
|
+
const explicitNames = new Set(fontsToEmbed.map((f) => f.name));
|
|
1411
|
+
for (const autoFont of detectedFonts) {
|
|
1412
|
+
if (!explicitNames.has(autoFont.name)) {
|
|
1413
|
+
fontsToEmbed.push(autoFont);
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
if (detectedFonts.length > 0) {
|
|
1418
|
+
console.log(
|
|
1419
|
+
'Auto-detected fonts:',
|
|
1420
|
+
detectedFonts.map((f) => f.name)
|
|
1421
|
+
);
|
|
1422
|
+
}
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
if (fontsToEmbed.length > 0) {
|
|
1426
|
+
// Generate initial PPTX
|
|
1427
|
+
const initialBlob = await pptx.write({ outputType: 'blob' });
|
|
1428
|
+
|
|
1429
|
+
// Load into Embedder
|
|
1430
|
+
const zip = await JSZip.loadAsync(initialBlob);
|
|
1431
|
+
const embedder = new PPTXEmbedFonts();
|
|
1432
|
+
await embedder.loadZip(zip);
|
|
1433
|
+
|
|
1434
|
+
// Fetch and Embed
|
|
1435
|
+
for (const fontCfg of fontsToEmbed) {
|
|
1436
|
+
try {
|
|
1437
|
+
const response = await fetch(fontCfg.url);
|
|
1438
|
+
if (!response.ok) throw new Error(`Failed to fetch ${fontCfg.url}`);
|
|
1439
|
+
const buffer = await response.arrayBuffer();
|
|
1440
|
+
|
|
1441
|
+
// Infer type
|
|
1442
|
+
const ext = fontCfg.url.split('.').pop().split(/[?#]/)[0].toLowerCase();
|
|
1443
|
+
let type = 'ttf';
|
|
1444
|
+
if (['woff', 'otf'].includes(ext)) type = ext;
|
|
1445
|
+
|
|
1446
|
+
await embedder.addFont(fontCfg.name, buffer, type);
|
|
1447
|
+
} catch (e) {
|
|
1448
|
+
console.warn(`Failed to embed font: ${fontCfg.name} (${fontCfg.url})`, e);
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
|
|
1452
|
+
await embedder.updateFiles();
|
|
1453
|
+
finalBlob = await embedder.generateBlob();
|
|
1454
|
+
} else {
|
|
1455
|
+
// No fonts to embed
|
|
1456
|
+
finalBlob = await pptx.write({ outputType: 'blob' });
|
|
1457
|
+
}
|
|
1458
|
+
|
|
1459
|
+
// 4. Output Handling
|
|
1460
|
+
// If skipDownload is NOT true, proceed with browser download
|
|
1461
|
+
if (!options.skipDownload) {
|
|
1462
|
+
const fileName = options.fileName || 'export.pptx';
|
|
1463
|
+
const url = URL.createObjectURL(finalBlob);
|
|
1464
|
+
const a = document.createElement('a');
|
|
1465
|
+
a.href = url;
|
|
1466
|
+
a.download = fileName;
|
|
1467
|
+
document.body.appendChild(a);
|
|
1468
|
+
a.click();
|
|
1469
|
+
document.body.removeChild(a);
|
|
1470
|
+
URL.revokeObjectURL(url);
|
|
1471
|
+
}
|
|
1472
|
+
|
|
1473
|
+
// Always return the blob so the caller can use it (e.g. upload to server)
|
|
1474
|
+
return finalBlob;
|
|
1475
|
+
}
|
|
1476
|
+
|
|
1477
|
+
/**
|
|
1478
|
+
* Worker function to process a single DOM element into a single PPTX slide.
|
|
1479
|
+
* @param {HTMLElement} root - The root element for this slide.
|
|
1480
|
+
* @param {PptxGenJS.Slide} slide - The PPTX slide object to add content to.
|
|
1481
|
+
* @param {PptxGenJS} pptx - The main PPTX instance.
|
|
1482
|
+
*/
|
|
1483
|
+
async function processSlide(root, slide, pptx, globalOptions = {}) {
|
|
1484
|
+
const rootRect = root.getBoundingClientRect();
|
|
1485
|
+
const PPTX_WIDTH_IN = 10;
|
|
1486
|
+
const PPTX_HEIGHT_IN = 5.625;
|
|
1487
|
+
|
|
1488
|
+
const contentWidthIn = rootRect.width * PX_TO_INCH;
|
|
1489
|
+
const contentHeightIn = rootRect.height * PX_TO_INCH;
|
|
1490
|
+
const scale = Math.min(PPTX_WIDTH_IN / contentWidthIn, PPTX_HEIGHT_IN / contentHeightIn);
|
|
1491
|
+
|
|
1492
|
+
const layoutConfig = {
|
|
1493
|
+
rootX: rootRect.x,
|
|
1494
|
+
rootY: rootRect.y,
|
|
1495
|
+
scale: scale,
|
|
1496
|
+
offX: (PPTX_WIDTH_IN - contentWidthIn * scale) / 2,
|
|
1497
|
+
offY: (PPTX_HEIGHT_IN - contentHeightIn * scale) / 2,
|
|
1498
|
+
};
|
|
1499
|
+
|
|
1500
|
+
const renderQueue = [];
|
|
1501
|
+
const asyncTasks = []; // Queue for heavy operations (Images, Canvas)
|
|
1502
|
+
let domOrderCounter = 0;
|
|
1503
|
+
|
|
1504
|
+
// Sync Traversal Function
|
|
1505
|
+
function collect(node, parentZIndex) {
|
|
1506
|
+
const order = domOrderCounter++;
|
|
1507
|
+
|
|
1508
|
+
let currentZ = parentZIndex;
|
|
1509
|
+
let nodeStyle = null;
|
|
1510
|
+
const nodeType = node.nodeType;
|
|
1511
|
+
|
|
1512
|
+
if (nodeType === 1) {
|
|
1513
|
+
nodeStyle = window.getComputedStyle(node);
|
|
1514
|
+
// Optimization: Skip completely hidden elements immediately
|
|
1515
|
+
if (
|
|
1516
|
+
nodeStyle.display === 'none' ||
|
|
1517
|
+
nodeStyle.visibility === 'hidden' ||
|
|
1518
|
+
nodeStyle.opacity === '0'
|
|
1519
|
+
) {
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
if (nodeStyle.zIndex !== 'auto') {
|
|
1523
|
+
currentZ = parseInt(nodeStyle.zIndex);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
// Prepare the item. If it needs async work, it returns a 'job'
|
|
1528
|
+
const result = prepareRenderItem(
|
|
1529
|
+
node,
|
|
1530
|
+
{ ...layoutConfig, root },
|
|
1531
|
+
order,
|
|
1532
|
+
pptx,
|
|
1533
|
+
currentZ,
|
|
1534
|
+
nodeStyle,
|
|
1535
|
+
globalOptions
|
|
1536
|
+
);
|
|
1537
|
+
|
|
1538
|
+
if (result) {
|
|
1539
|
+
if (result.items) {
|
|
1540
|
+
// Push items immediately to queue (data might be missing but filled later)
|
|
1541
|
+
renderQueue.push(...result.items);
|
|
1542
|
+
}
|
|
1543
|
+
if (result.job) {
|
|
1544
|
+
// Push the promise-returning function to the task list
|
|
1545
|
+
asyncTasks.push(result.job);
|
|
1546
|
+
}
|
|
1547
|
+
if (result.stopRecursion) return;
|
|
1548
|
+
}
|
|
1549
|
+
|
|
1550
|
+
// Recurse children synchronously
|
|
1551
|
+
const childNodes = node.childNodes;
|
|
1552
|
+
for (let i = 0; i < childNodes.length; i++) {
|
|
1553
|
+
collect(childNodes[i], currentZ);
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
|
|
1557
|
+
// 1. Traverse and build the structure (Fast)
|
|
1558
|
+
collect(root, 0);
|
|
1559
|
+
|
|
1560
|
+
// 2. Execute all heavy tasks in parallel (Fast)
|
|
1561
|
+
if (asyncTasks.length > 0) {
|
|
1562
|
+
await Promise.all(asyncTasks.map((task) => task()));
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
// 3. Cleanup and Sort
|
|
1566
|
+
// Remove items that failed to generate data (marked with skip)
|
|
1567
|
+
const finalQueue = renderQueue.filter(
|
|
1568
|
+
(item) => !item.skip && (item.type !== 'image' || item.options.data)
|
|
1569
|
+
);
|
|
1570
|
+
|
|
1571
|
+
finalQueue.sort((a, b) => {
|
|
1572
|
+
if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
|
|
1573
|
+
return a.domOrder - b.domOrder;
|
|
1574
|
+
});
|
|
1575
|
+
|
|
1576
|
+
// 4. Add to Slide
|
|
1577
|
+
for (const item of finalQueue) {
|
|
1578
|
+
if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
|
|
1579
|
+
if (item.type === 'image') slide.addImage(item.options);
|
|
1580
|
+
if (item.type === 'text') slide.addText(item.textParts, item.options);
|
|
1581
|
+
if (item.type === 'table') {
|
|
1582
|
+
slide.addTable(item.tableData.rows, {
|
|
1583
|
+
x: item.options.x,
|
|
1584
|
+
y: item.options.y,
|
|
1585
|
+
w: item.options.w,
|
|
1586
|
+
colW: item.tableData.colWidths, // Essential for correct layout
|
|
1587
|
+
autoPage: false,
|
|
1588
|
+
// Remove default table styles so our extracted CSS applies cleanly
|
|
1589
|
+
border: { type: "none" },
|
|
1590
|
+
fill: { color: "FFFFFF", transparency: 100 }
|
|
1591
|
+
});
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
/**
|
|
1597
|
+
* Optimized html2canvas wrapper
|
|
1598
|
+
* Includes fix for cropped icons by adjusting styles in the cloned document.
|
|
1599
|
+
*/
|
|
1600
|
+
async function elementToCanvasImage(node, widthPx, heightPx) {
|
|
1601
|
+
return new Promise((resolve) => {
|
|
1602
|
+
// 1. Assign a temp ID to locate the node inside the cloned document
|
|
1603
|
+
const originalId = node.id;
|
|
1604
|
+
const tempId = 'pptx-capture-' + Math.random().toString(36).substr(2, 9);
|
|
1605
|
+
node.id = tempId;
|
|
1606
|
+
|
|
1607
|
+
const width = Math.max(Math.ceil(widthPx), 1);
|
|
1608
|
+
const height = Math.max(Math.ceil(heightPx), 1);
|
|
1609
|
+
const style = window.getComputedStyle(node);
|
|
1610
|
+
|
|
1611
|
+
html2canvas(node, {
|
|
1612
|
+
backgroundColor: null,
|
|
1613
|
+
logging: false,
|
|
1614
|
+
scale: 3, // Higher scale for sharper icons
|
|
1615
|
+
useCORS: true, // critical for external fonts/images
|
|
1616
|
+
onclone: (clonedDoc) => {
|
|
1617
|
+
const clonedNode = clonedDoc.getElementById(tempId);
|
|
1618
|
+
if (clonedNode) {
|
|
1619
|
+
// --- FIX: PREVENT ICON CLIPPING ---
|
|
1620
|
+
// 1. Force overflow visible so glyphs bleeding out aren't cut
|
|
1621
|
+
clonedNode.style.overflow = 'visible';
|
|
1622
|
+
|
|
1623
|
+
// 2. Adjust alignment for Icons to prevent baseline clipping
|
|
1624
|
+
// (Applies to <i>, <span>, or standard icon classes)
|
|
1625
|
+
const tag = clonedNode.tagName;
|
|
1626
|
+
if (tag === 'I' || tag === 'SPAN' || clonedNode.className.includes('fa-')) {
|
|
1627
|
+
// Flex center helps align the glyph exactly in the middle of the box
|
|
1628
|
+
// preventing top/bottom cropping due to line-height mismatches.
|
|
1629
|
+
clonedNode.style.display = 'inline-flex';
|
|
1630
|
+
clonedNode.style.justifyContent = 'center';
|
|
1631
|
+
clonedNode.style.alignItems = 'center';
|
|
1632
|
+
|
|
1633
|
+
// Remove margins that might offset the capture
|
|
1634
|
+
clonedNode.style.margin = '0';
|
|
1635
|
+
|
|
1636
|
+
// Ensure the font fits
|
|
1637
|
+
clonedNode.style.lineHeight = '1';
|
|
1638
|
+
clonedNode.style.verticalAlign = 'middle';
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
},
|
|
1642
|
+
})
|
|
1643
|
+
.then((canvas) => {
|
|
1644
|
+
// Restore the original ID
|
|
1645
|
+
if (originalId) node.id = originalId;
|
|
1646
|
+
else node.removeAttribute('id');
|
|
1647
|
+
|
|
1648
|
+
const destCanvas = document.createElement('canvas');
|
|
1649
|
+
destCanvas.width = width;
|
|
1650
|
+
destCanvas.height = height;
|
|
1651
|
+
const ctx = destCanvas.getContext('2d');
|
|
1652
|
+
|
|
1653
|
+
// Draw captured canvas.
|
|
1654
|
+
// We simply draw it to fill the box. Since we centered it in 'onclone',
|
|
1655
|
+
// the glyph should now be visible within the bounds.
|
|
1656
|
+
ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, width, height);
|
|
1657
|
+
|
|
1658
|
+
// --- Border Radius Clipping (Existing Logic) ---
|
|
1659
|
+
let tl = parseFloat(style.borderTopLeftRadius) || 0;
|
|
1660
|
+
let tr = parseFloat(style.borderTopRightRadius) || 0;
|
|
1661
|
+
let br = parseFloat(style.borderBottomRightRadius) || 0;
|
|
1662
|
+
let bl = parseFloat(style.borderBottomLeftRadius) || 0;
|
|
1663
|
+
|
|
1664
|
+
const f = Math.min(
|
|
1665
|
+
width / (tl + tr) || Infinity,
|
|
1666
|
+
height / (tr + br) || Infinity,
|
|
1667
|
+
width / (br + bl) || Infinity,
|
|
1668
|
+
height / (bl + tl) || Infinity
|
|
1669
|
+
);
|
|
1670
|
+
|
|
1671
|
+
if (f < 1) {
|
|
1672
|
+
tl *= f;
|
|
1673
|
+
tr *= f;
|
|
1674
|
+
br *= f;
|
|
1675
|
+
bl *= f;
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
if (tl + tr + br + bl > 0) {
|
|
1679
|
+
ctx.globalCompositeOperation = 'destination-in';
|
|
1680
|
+
ctx.beginPath();
|
|
1681
|
+
ctx.moveTo(tl, 0);
|
|
1682
|
+
ctx.lineTo(width - tr, 0);
|
|
1683
|
+
ctx.arcTo(width, 0, width, tr, tr);
|
|
1684
|
+
ctx.lineTo(width, height - br);
|
|
1685
|
+
ctx.arcTo(width, height, width - br, height, br);
|
|
1686
|
+
ctx.lineTo(bl, height);
|
|
1687
|
+
ctx.arcTo(0, height, 0, height - bl, bl);
|
|
1688
|
+
ctx.lineTo(0, tl);
|
|
1689
|
+
ctx.arcTo(0, 0, tl, 0, tl);
|
|
1690
|
+
ctx.closePath();
|
|
1691
|
+
ctx.fill();
|
|
1692
|
+
}
|
|
1693
|
+
|
|
1694
|
+
resolve(destCanvas.toDataURL('image/png'));
|
|
1695
|
+
})
|
|
1696
|
+
.catch((e) => {
|
|
1697
|
+
if (originalId) node.id = originalId;
|
|
1698
|
+
else node.removeAttribute('id');
|
|
1699
|
+
console.warn('Canvas capture failed for node', node, e);
|
|
1700
|
+
resolve(null);
|
|
1701
|
+
});
|
|
1702
|
+
});
|
|
1703
|
+
}
|
|
1704
|
+
|
|
1705
|
+
/**
|
|
1706
|
+
* Helper to identify elements that should be rendered as icons (Images).
|
|
1707
|
+
* Detects Custom Elements AND generic tags (<i>, <span>) with icon classes/pseudo-elements.
|
|
1708
|
+
*/
|
|
1709
|
+
function isIconElement(node) {
|
|
1710
|
+
// 1. Custom Elements (hyphenated tags) or Explicit Library Tags
|
|
1711
|
+
const tag = node.tagName.toUpperCase();
|
|
1712
|
+
if (
|
|
1713
|
+
tag.includes('-') ||
|
|
1714
|
+
[
|
|
1715
|
+
'MATERIAL-ICON',
|
|
1716
|
+
'ICONIFY-ICON',
|
|
1717
|
+
'REMIX-ICON',
|
|
1718
|
+
'ION-ICON',
|
|
1719
|
+
'EVA-ICON',
|
|
1720
|
+
'BOX-ICON',
|
|
1721
|
+
'FA-ICON',
|
|
1722
|
+
].includes(tag)
|
|
1723
|
+
) {
|
|
1724
|
+
return true;
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
// 2. Class-based Icons (FontAwesome, Bootstrap, Material symbols) on <i> or <span>
|
|
1728
|
+
if (tag === 'I' || tag === 'SPAN') {
|
|
1729
|
+
const cls = node.getAttribute('class') || '';
|
|
1730
|
+
if (
|
|
1731
|
+
typeof cls === 'string' &&
|
|
1732
|
+
(cls.includes('fa-') ||
|
|
1733
|
+
cls.includes('fas') ||
|
|
1734
|
+
cls.includes('far') ||
|
|
1735
|
+
cls.includes('fab') ||
|
|
1736
|
+
cls.includes('bi-') ||
|
|
1737
|
+
cls.includes('material-icons') ||
|
|
1738
|
+
cls.includes('icon'))
|
|
1739
|
+
) {
|
|
1740
|
+
// Double-check: Must have pseudo-element content to be a CSS icon
|
|
1741
|
+
const before = window.getComputedStyle(node, '::before').content;
|
|
1742
|
+
const after = window.getComputedStyle(node, '::after').content;
|
|
1743
|
+
const hasContent = (c) => c && c !== 'none' && c !== 'normal' && c !== '""';
|
|
1744
|
+
|
|
1745
|
+
if (hasContent(before) || hasContent(after)) return true;
|
|
1746
|
+
}
|
|
1747
|
+
}
|
|
1748
|
+
|
|
1749
|
+
return false;
|
|
1750
|
+
}
|
|
1751
|
+
|
|
1752
|
+
/**
|
|
1753
|
+
* Replaces createRenderItem.
|
|
1754
|
+
* Returns { items: [], job: () => Promise, stopRecursion: boolean }
|
|
1755
|
+
*/
|
|
1756
|
+
function prepareRenderItem(
|
|
1757
|
+
node,
|
|
1758
|
+
config,
|
|
1759
|
+
domOrder,
|
|
1760
|
+
pptx,
|
|
1761
|
+
effectiveZIndex,
|
|
1762
|
+
computedStyle,
|
|
1763
|
+
globalOptions = {}
|
|
1764
|
+
) {
|
|
1765
|
+
// 1. Text Node Handling
|
|
1766
|
+
if (node.nodeType === 3) {
|
|
1767
|
+
const textContent = node.nodeValue.trim();
|
|
1768
|
+
if (!textContent) return null;
|
|
1769
|
+
|
|
1770
|
+
const parent = node.parentElement;
|
|
1771
|
+
if (!parent) return null;
|
|
1772
|
+
|
|
1773
|
+
if (isTextContainer(parent)) return null; // Parent handles it
|
|
1774
|
+
|
|
1775
|
+
const range = document.createRange();
|
|
1776
|
+
range.selectNode(node);
|
|
1777
|
+
const rect = range.getBoundingClientRect();
|
|
1778
|
+
range.detach();
|
|
1779
|
+
|
|
1780
|
+
const style = window.getComputedStyle(parent);
|
|
1781
|
+
const widthPx = rect.width;
|
|
1782
|
+
const heightPx = rect.height;
|
|
1783
|
+
const unrotatedW = widthPx * PX_TO_INCH * config.scale;
|
|
1784
|
+
const unrotatedH = heightPx * PX_TO_INCH * config.scale;
|
|
1785
|
+
|
|
1786
|
+
const x = config.offX + (rect.left - config.rootX) * PX_TO_INCH * config.scale;
|
|
1787
|
+
const y = config.offY + (rect.top - config.rootY) * PX_TO_INCH * config.scale;
|
|
1788
|
+
|
|
1789
|
+
return {
|
|
1790
|
+
items: [
|
|
1791
|
+
{
|
|
1792
|
+
type: 'text',
|
|
1793
|
+
zIndex: effectiveZIndex,
|
|
1794
|
+
domOrder,
|
|
1795
|
+
textParts: [
|
|
1796
|
+
{
|
|
1797
|
+
text: textContent,
|
|
1798
|
+
options: (() => {
|
|
1799
|
+
const opts = getTextStyle(style, config.scale, textContent, globalOptions);
|
|
1800
|
+
const bg = parseColor(style.backgroundColor);
|
|
1801
|
+
if (opts.highlight && bg.hex && bg.opacity > 0 && !isTextContainer(parent)) {
|
|
1802
|
+
delete opts.highlight;
|
|
1803
|
+
}
|
|
1804
|
+
return opts;
|
|
1805
|
+
})(),
|
|
1806
|
+
},
|
|
1807
|
+
],
|
|
1808
|
+
options: { x, y, w: unrotatedW, h: unrotatedH, margin: 0, autoFit: false },
|
|
1809
|
+
},
|
|
1810
|
+
],
|
|
1811
|
+
stopRecursion: false,
|
|
1812
|
+
};
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
if (node.nodeType !== 1) return null;
|
|
1816
|
+
const style = computedStyle; // Use pre-computed style
|
|
1817
|
+
|
|
1818
|
+
const rect = node.getBoundingClientRect();
|
|
1819
|
+
if (rect.width < 0.5 || rect.height < 0.5) return null;
|
|
1820
|
+
|
|
1821
|
+
const zIndex = effectiveZIndex;
|
|
1822
|
+
const rotation = getRotation(style.transform);
|
|
1823
|
+
const elementOpacity = parseFloat(style.opacity);
|
|
1824
|
+
const safeOpacity = isNaN(elementOpacity) ? 1 : elementOpacity;
|
|
1825
|
+
|
|
1826
|
+
const widthPx = node.offsetWidth || rect.width;
|
|
1827
|
+
const heightPx = node.offsetHeight || rect.height;
|
|
1828
|
+
const unrotatedW = widthPx * PX_TO_INCH * config.scale;
|
|
1829
|
+
const unrotatedH = heightPx * PX_TO_INCH * config.scale;
|
|
1830
|
+
const centerX = rect.left + rect.width / 2;
|
|
1831
|
+
const centerY = rect.top + rect.height / 2;
|
|
1832
|
+
|
|
1833
|
+
let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
|
|
1834
|
+
let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
|
|
1835
|
+
let w = unrotatedW;
|
|
1836
|
+
let h = unrotatedH;
|
|
1837
|
+
|
|
1838
|
+
const items = [];
|
|
1839
|
+
|
|
1840
|
+
if (node.tagName === 'TABLE') {
|
|
1841
|
+
const tableData = extractTableData(node, config.scale, { ...globalOptions, root: config.root });
|
|
1842
|
+
const cellBgItems = [];
|
|
1843
|
+
const renderCellBg = globalOptions.tableConfig?.renderCellBackgrounds !== false;
|
|
1844
|
+
|
|
1845
|
+
if (renderCellBg) {
|
|
1846
|
+
const trList = node.querySelectorAll('tr');
|
|
1847
|
+
trList.forEach((tr) => {
|
|
1848
|
+
const cellList = Array.from(tr.children).filter((c) => ['TD', 'TH'].includes(c.tagName));
|
|
1849
|
+
cellList.forEach((cell) => {
|
|
1850
|
+
const style = window.getComputedStyle(cell);
|
|
1851
|
+
const fill = computeTableCellFill(style, cell, config.root, globalOptions);
|
|
1852
|
+
if (!fill) return;
|
|
1853
|
+
|
|
1854
|
+
const rect = cell.getBoundingClientRect();
|
|
1855
|
+
const wIn = rect.width * PX_TO_INCH * config.scale;
|
|
1856
|
+
const hIn = rect.height * PX_TO_INCH * config.scale;
|
|
1857
|
+
const xIn = config.offX + (rect.left - config.rootX) * PX_TO_INCH * config.scale;
|
|
1858
|
+
const yIn = config.offY + (rect.top - config.rootY) * PX_TO_INCH * config.scale;
|
|
1859
|
+
|
|
1860
|
+
cellBgItems.push({
|
|
1861
|
+
type: 'shape',
|
|
1862
|
+
zIndex: effectiveZIndex - 0.5,
|
|
1863
|
+
domOrder,
|
|
1864
|
+
shapeType: 'rect',
|
|
1865
|
+
options: { x: xIn, y: yIn, w: wIn, h: hIn, fill: fill, line: { color: 'FFFFFF', transparency: 100 } }
|
|
1866
|
+
});
|
|
1867
|
+
});
|
|
1868
|
+
});
|
|
1869
|
+
}
|
|
1870
|
+
|
|
1871
|
+
// Calculate total table width to ensure X position is correct
|
|
1872
|
+
// (Though x calculation above usually handles it, tables can be finicky)
|
|
1873
|
+
return {
|
|
1874
|
+
items: [
|
|
1875
|
+
...cellBgItems,
|
|
1876
|
+
{
|
|
1877
|
+
type: 'table',
|
|
1878
|
+
zIndex: effectiveZIndex,
|
|
1879
|
+
domOrder,
|
|
1880
|
+
tableData: tableData,
|
|
1881
|
+
options: { x, y, w: unrotatedW, h: unrotatedH }
|
|
1882
|
+
}
|
|
1883
|
+
],
|
|
1884
|
+
stopRecursion: true // Important: Don't process TR/TD as separate shapes
|
|
1885
|
+
};
|
|
1886
|
+
}
|
|
1887
|
+
|
|
1888
|
+
if ((node.tagName === 'UL' || node.tagName === 'OL') && !isComplexHierarchy(node)) {
|
|
1889
|
+
const listItems = [];
|
|
1890
|
+
const liChildren = Array.from(node.children).filter((c) => c.tagName === 'LI');
|
|
1891
|
+
|
|
1892
|
+
liChildren.forEach((child, index) => {
|
|
1893
|
+
const liStyle = window.getComputedStyle(child);
|
|
1894
|
+
const liRect = child.getBoundingClientRect();
|
|
1895
|
+
const parentRect = node.getBoundingClientRect(); // node is UL/OL
|
|
1896
|
+
|
|
1897
|
+
// 1. Determine Bullet Config
|
|
1898
|
+
let bullet = { type: 'bullet' };
|
|
1899
|
+
const listStyleType = liStyle.listStyleType || 'disc';
|
|
1900
|
+
|
|
1901
|
+
if (node.tagName === 'OL' || listStyleType === 'decimal') {
|
|
1902
|
+
bullet = { type: 'number' };
|
|
1903
|
+
} else if (listStyleType === 'none') {
|
|
1904
|
+
bullet = false;
|
|
1905
|
+
} else {
|
|
1906
|
+
let code = '2022'; // disc
|
|
1907
|
+
if (listStyleType === 'circle') code = '25CB';
|
|
1908
|
+
if (listStyleType === 'square') code = '25A0';
|
|
1909
|
+
|
|
1910
|
+
// --- CHANGE: Color & Size Logic (Option > ::marker > CSS color) ---
|
|
1911
|
+
let finalHex = '000000';
|
|
1912
|
+
let markerFontSize = null;
|
|
1913
|
+
|
|
1914
|
+
// A. Check Global Option override
|
|
1915
|
+
if (globalOptions?.listConfig?.color) {
|
|
1916
|
+
finalHex = parseColor(globalOptions.listConfig.color).hex || '000000';
|
|
1917
|
+
}
|
|
1918
|
+
// B. Check ::marker pseudo element (supported in modern browsers)
|
|
1919
|
+
else {
|
|
1920
|
+
const markerStyle = window.getComputedStyle(child, '::marker');
|
|
1921
|
+
const markerColor = parseColor(markerStyle.color);
|
|
1922
|
+
if (markerColor.hex) {
|
|
1923
|
+
finalHex = markerColor.hex;
|
|
1924
|
+
} else {
|
|
1925
|
+
// C. Fallback to LI text color
|
|
1926
|
+
const colorObj = parseColor(liStyle.color);
|
|
1927
|
+
if (colorObj.hex) finalHex = colorObj.hex;
|
|
1928
|
+
}
|
|
1929
|
+
|
|
1930
|
+
// Check ::marker font-size
|
|
1931
|
+
const markerFs = parseFloat(markerStyle.fontSize);
|
|
1932
|
+
if (!isNaN(markerFs) && markerFs > 0) {
|
|
1933
|
+
// Convert px->pt for PPTX
|
|
1934
|
+
markerFontSize = markerFs * 0.75 * config.scale;
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
bullet = { code, color: finalHex };
|
|
1939
|
+
if (markerFontSize) {
|
|
1940
|
+
bullet.fontSize = markerFontSize;
|
|
1941
|
+
}
|
|
1942
|
+
}
|
|
1943
|
+
|
|
1944
|
+
// 2. Calculate Dynamic Indent (Respects padding-left)
|
|
1945
|
+
// Visual Indent = Distance from UL left edge to LI Content left edge.
|
|
1946
|
+
// PptxGenJS 'indent' = Space between bullet and text?
|
|
1947
|
+
// Actually PptxGenJS 'indent' allows setting the hanging indent.
|
|
1948
|
+
// We calculate the TOTAL visual offset from the parent container.
|
|
1949
|
+
// 1 px = 0.75 pt (approx, standard DTP).
|
|
1950
|
+
// We must scale it by config.scale.
|
|
1951
|
+
const visualIndentPx = liRect.left - parentRect.left;
|
|
1952
|
+
/*
|
|
1953
|
+
Standard indent in PPT is ~27pt.
|
|
1954
|
+
If visualIndentPx is small (e.g. 10px padding), we want small indent.
|
|
1955
|
+
If visualIndentPx is large (40px padding), we want large indent.
|
|
1956
|
+
We treat 'indent' as the value to pass to PptxGenJS.
|
|
1957
|
+
*/
|
|
1958
|
+
const computedIndentPt = visualIndentPx * 0.75 * config.scale;
|
|
1959
|
+
|
|
1960
|
+
if (bullet && computedIndentPt > 0) {
|
|
1961
|
+
bullet.indent = computedIndentPt;
|
|
1962
|
+
// Also support custom margin between bullet and text if provided in listConfig?
|
|
1963
|
+
// For now, computedIndentPt covers the visual placement.
|
|
1964
|
+
}
|
|
1965
|
+
|
|
1966
|
+
// 3. Extract Text Parts
|
|
1967
|
+
const parts = collectListParts(child, liStyle, config.scale, globalOptions);
|
|
1968
|
+
|
|
1969
|
+
if (parts.length > 0) {
|
|
1970
|
+
parts.forEach((p) => {
|
|
1971
|
+
if (!p.options) p.options = {};
|
|
1972
|
+
});
|
|
1973
|
+
|
|
1974
|
+
// A. Apply Bullet
|
|
1975
|
+
// Workaround: pptxgenjs bullets inherit the style of the text run they are attached to.
|
|
1976
|
+
// To support ::marker styles (color, size) that differ from the text, we create
|
|
1977
|
+
// a "dummy" text run at the start of the list item that carries the bullet configuration.
|
|
1978
|
+
if (bullet) {
|
|
1979
|
+
const firstPartInfo = parts[0].options;
|
|
1980
|
+
|
|
1981
|
+
// Create a dummy run. We use a Zero Width Space to ensure it's rendered but invisible.
|
|
1982
|
+
// This "run" will hold the bullet and its specific color/size.
|
|
1983
|
+
const bulletRun = {
|
|
1984
|
+
text: '\u200B',
|
|
1985
|
+
options: {
|
|
1986
|
+
...firstPartInfo, // Inherit base props (fontFace, etc.)
|
|
1987
|
+
color: bullet.color || firstPartInfo.color,
|
|
1988
|
+
fontSize: bullet.fontSize || firstPartInfo.fontSize,
|
|
1989
|
+
bullet: bullet
|
|
1990
|
+
}
|
|
1991
|
+
};
|
|
1992
|
+
|
|
1993
|
+
// Don't duplicate transparent or empty color from firstPart if bullet has one
|
|
1994
|
+
if (bullet.color) bulletRun.options.color = bullet.color;
|
|
1995
|
+
if (bullet.fontSize) bulletRun.options.fontSize = bullet.fontSize;
|
|
1996
|
+
|
|
1997
|
+
// Prepend
|
|
1998
|
+
parts.unshift(bulletRun);
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
// B. Apply Spacing
|
|
2002
|
+
let ptBefore = 0;
|
|
2003
|
+
let ptAfter = 0;
|
|
2004
|
+
|
|
2005
|
+
// A. Check Global Options (Expected in Points)
|
|
2006
|
+
if (globalOptions.listConfig?.spacing) {
|
|
2007
|
+
if (typeof globalOptions.listConfig.spacing.before === 'number') {
|
|
2008
|
+
ptBefore = globalOptions.listConfig.spacing.before;
|
|
2009
|
+
}
|
|
2010
|
+
if (typeof globalOptions.listConfig.spacing.after === 'number') {
|
|
2011
|
+
ptAfter = globalOptions.listConfig.spacing.after;
|
|
2012
|
+
}
|
|
2013
|
+
}
|
|
2014
|
+
// B. Fallback to CSS Margins (Convert px -> pt)
|
|
2015
|
+
else {
|
|
2016
|
+
const mt = parseFloat(liStyle.marginTop) || 0;
|
|
2017
|
+
const mb = parseFloat(liStyle.marginBottom) || 0;
|
|
2018
|
+
if (mt > 0) ptBefore = mt * 0.75 * config.scale;
|
|
2019
|
+
if (mb > 0) ptAfter = mb * 0.75 * config.scale;
|
|
2020
|
+
}
|
|
2021
|
+
|
|
2022
|
+
if (ptBefore > 0) parts[0].options.paraSpaceBefore = ptBefore;
|
|
2023
|
+
if (ptAfter > 0) parts[0].options.paraSpaceAfter = ptAfter;
|
|
2024
|
+
|
|
2025
|
+
if (index < liChildren.length - 1) {
|
|
2026
|
+
parts[parts.length - 1].options.breakLine = true;
|
|
2027
|
+
}
|
|
2028
|
+
|
|
2029
|
+
listItems.push(...parts);
|
|
2030
|
+
}
|
|
2031
|
+
});
|
|
2032
|
+
|
|
2033
|
+
if (listItems.length > 0) {
|
|
2034
|
+
// Add background if exists
|
|
2035
|
+
const bgColorObj = parseColor(style.backgroundColor);
|
|
2036
|
+
if (bgColorObj.hex && bgColorObj.opacity > 0) {
|
|
2037
|
+
items.push({
|
|
2038
|
+
type: 'shape',
|
|
2039
|
+
zIndex,
|
|
2040
|
+
domOrder,
|
|
2041
|
+
shapeType: 'rect',
|
|
2042
|
+
options: { x, y, w, h, fill: { color: bgColorObj.hex } },
|
|
2043
|
+
});
|
|
2044
|
+
}
|
|
2045
|
+
|
|
2046
|
+
items.push({
|
|
2047
|
+
type: 'text',
|
|
2048
|
+
zIndex: zIndex + 1,
|
|
2049
|
+
domOrder,
|
|
2050
|
+
textParts: listItems,
|
|
2051
|
+
options: {
|
|
2052
|
+
x,
|
|
2053
|
+
y,
|
|
2054
|
+
w,
|
|
2055
|
+
h,
|
|
2056
|
+
align: 'left',
|
|
2057
|
+
valign: 'top',
|
|
2058
|
+
margin: 0,
|
|
2059
|
+
autoFit: false,
|
|
2060
|
+
wrap: true,
|
|
2061
|
+
},
|
|
2062
|
+
});
|
|
2063
|
+
|
|
2064
|
+
return { items, stopRecursion: true };
|
|
2065
|
+
}
|
|
2066
|
+
}
|
|
2067
|
+
|
|
2068
|
+
if (node.tagName === 'CANVAS') {
|
|
2069
|
+
const item = {
|
|
2070
|
+
type: 'image',
|
|
2071
|
+
zIndex,
|
|
2072
|
+
domOrder,
|
|
2073
|
+
options: { x, y, w, h, rotate: rotation, data: null }
|
|
2074
|
+
};
|
|
2075
|
+
|
|
2076
|
+
const job = async () => {
|
|
2077
|
+
try {
|
|
2078
|
+
// Direct data extraction from the canvas element
|
|
2079
|
+
// This preserves the exact current state of the chart
|
|
2080
|
+
const dataUrl = node.toDataURL('image/png');
|
|
2081
|
+
|
|
2082
|
+
// Basic validation
|
|
2083
|
+
if (dataUrl && dataUrl.length > 10) {
|
|
2084
|
+
item.options.data = dataUrl;
|
|
2085
|
+
} else {
|
|
2086
|
+
item.skip = true;
|
|
2087
|
+
}
|
|
2088
|
+
} catch (e) {
|
|
2089
|
+
// Tainted canvas (CORS issues) will throw here
|
|
2090
|
+
console.warn('Failed to capture canvas content:', e);
|
|
2091
|
+
item.skip = true;
|
|
2092
|
+
}
|
|
2093
|
+
};
|
|
2094
|
+
|
|
2095
|
+
return { items: [item], job, stopRecursion: true };
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
// --- ASYNC JOB: SVG Tags ---
|
|
2099
|
+
if (node.nodeName.toUpperCase() === 'SVG') {
|
|
2100
|
+
const item = {
|
|
2101
|
+
type: 'image',
|
|
2102
|
+
zIndex,
|
|
2103
|
+
domOrder,
|
|
2104
|
+
options: { data: null, x, y, w, h, rotate: rotation },
|
|
2105
|
+
};
|
|
2106
|
+
|
|
2107
|
+
const job = async () => {
|
|
2108
|
+
const processed = await svgToPng(node);
|
|
2109
|
+
if (processed) item.options.data = processed;
|
|
2110
|
+
else item.skip = true;
|
|
2111
|
+
};
|
|
2112
|
+
|
|
2113
|
+
return { items: [item], job, stopRecursion: true };
|
|
2114
|
+
}
|
|
2115
|
+
|
|
2116
|
+
// --- ASYNC JOB: IMG Tags ---
|
|
2117
|
+
if (node.tagName === 'IMG') {
|
|
2118
|
+
let radii = {
|
|
2119
|
+
tl: parseFloat(style.borderTopLeftRadius) || 0,
|
|
2120
|
+
tr: parseFloat(style.borderTopRightRadius) || 0,
|
|
2121
|
+
br: parseFloat(style.borderBottomRightRadius) || 0,
|
|
2122
|
+
bl: parseFloat(style.borderBottomLeftRadius) || 0,
|
|
2123
|
+
};
|
|
2124
|
+
|
|
2125
|
+
const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
|
|
2126
|
+
if (!hasAnyRadius) {
|
|
2127
|
+
const parent = node.parentElement;
|
|
2128
|
+
const parentStyle = window.getComputedStyle(parent);
|
|
2129
|
+
if (parentStyle.overflow !== 'visible') {
|
|
2130
|
+
const pRadii = {
|
|
2131
|
+
tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
|
|
2132
|
+
tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
|
|
2133
|
+
br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
|
|
2134
|
+
bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
|
|
2135
|
+
};
|
|
2136
|
+
const pRect = parent.getBoundingClientRect();
|
|
2137
|
+
if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
|
|
2138
|
+
radii = pRadii;
|
|
2139
|
+
}
|
|
2140
|
+
}
|
|
2141
|
+
}
|
|
2142
|
+
|
|
2143
|
+
const objectFit = style.objectFit || 'fill'; // default CSS behavior is fill
|
|
2144
|
+
const objectPosition = style.objectPosition || '50% 50%';
|
|
2145
|
+
|
|
2146
|
+
const item = {
|
|
2147
|
+
type: 'image',
|
|
2148
|
+
zIndex,
|
|
2149
|
+
domOrder,
|
|
2150
|
+
options: { x, y, w, h, rotate: rotation, data: null },
|
|
2151
|
+
};
|
|
2152
|
+
|
|
2153
|
+
const job = async () => {
|
|
2154
|
+
const processed = await getProcessedImage(
|
|
2155
|
+
node.src,
|
|
2156
|
+
widthPx,
|
|
2157
|
+
heightPx,
|
|
2158
|
+
radii,
|
|
2159
|
+
objectFit,
|
|
2160
|
+
objectPosition
|
|
2161
|
+
);
|
|
2162
|
+
if (processed) item.options.data = processed;
|
|
2163
|
+
else item.skip = true;
|
|
2164
|
+
};
|
|
2165
|
+
|
|
2166
|
+
return { items: [item], job, stopRecursion: true };
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
// --- ASYNC JOB: Icons and Other Elements ---
|
|
2170
|
+
if (isIconElement(node)) {
|
|
2171
|
+
const item = {
|
|
2172
|
+
type: 'image',
|
|
2173
|
+
zIndex,
|
|
2174
|
+
domOrder,
|
|
2175
|
+
options: { x, y, w, h, rotate: rotation, data: null },
|
|
2176
|
+
};
|
|
2177
|
+
const job = async () => {
|
|
2178
|
+
const pngData = await elementToCanvasImage(node, widthPx, heightPx);
|
|
2179
|
+
if (pngData) item.options.data = pngData;
|
|
2180
|
+
else item.skip = true;
|
|
2181
|
+
};
|
|
2182
|
+
return { items: [item], job, stopRecursion: true };
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
// Radii logic
|
|
2186
|
+
const borderRadiusValue = parseFloat(style.borderRadius) || 0;
|
|
2187
|
+
const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
|
|
2188
|
+
const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
|
|
2189
|
+
const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
|
|
2190
|
+
const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
|
|
2191
|
+
|
|
2192
|
+
const hasPartialBorderRadius =
|
|
2193
|
+
(borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
|
|
2194
|
+
(borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
|
|
2195
|
+
(borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
|
|
2196
|
+
(borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
|
|
2197
|
+
(borderRadiusValue === 0 &&
|
|
2198
|
+
(borderBottomLeftRadius ||
|
|
2199
|
+
borderBottomRightRadius ||
|
|
2200
|
+
borderTopLeftRadius ||
|
|
2201
|
+
borderTopRightRadius));
|
|
2202
|
+
|
|
2203
|
+
// --- PRIORITY SVG: Solid Fill with Partial Border Radius (Vector Cone/Tab) ---
|
|
2204
|
+
// Fix for "missing cone": Prioritize SVG vector generation over Raster Canvas for simple shapes with partial radii.
|
|
2205
|
+
// This avoids html2canvas failures on empty divs.
|
|
2206
|
+
const tempBg = parseColor(style.backgroundColor);
|
|
2207
|
+
const isTxt = isTextContainer(node);
|
|
2208
|
+
|
|
2209
|
+
if (hasPartialBorderRadius && tempBg.hex && !isTxt) {
|
|
2210
|
+
const shapeSvg = generateCustomShapeSVG(
|
|
2211
|
+
widthPx,
|
|
2212
|
+
heightPx,
|
|
2213
|
+
tempBg.hex,
|
|
2214
|
+
tempBg.opacity,
|
|
2215
|
+
{
|
|
2216
|
+
tl: parseFloat(style.borderTopLeftRadius) || 0,
|
|
2217
|
+
tr: parseFloat(style.borderTopRightRadius) || 0,
|
|
2218
|
+
br: parseFloat(style.borderBottomRightRadius) || 0,
|
|
2219
|
+
bl: parseFloat(style.borderBottomLeftRadius) || 0,
|
|
2220
|
+
}
|
|
2221
|
+
);
|
|
2222
|
+
|
|
2223
|
+
return {
|
|
2224
|
+
items: [{
|
|
2225
|
+
type: 'image',
|
|
2226
|
+
zIndex,
|
|
2227
|
+
domOrder,
|
|
2228
|
+
options: { data: shapeSvg, x, y, w, h, rotate: rotation },
|
|
2229
|
+
}],
|
|
2230
|
+
stopRecursion: true // Treat as leaf
|
|
2231
|
+
};
|
|
2232
|
+
}
|
|
2233
|
+
|
|
2234
|
+
// --- ASYNC JOB: Clipped Divs via Canvas ---
|
|
2235
|
+
if (hasPartialBorderRadius && isClippedByParent(node)) {
|
|
2236
|
+
const marginLeft = parseFloat(style.marginLeft) || 0;
|
|
2237
|
+
const marginTop = parseFloat(style.marginTop) || 0;
|
|
2238
|
+
x += marginLeft * PX_TO_INCH * config.scale;
|
|
2239
|
+
y += marginTop * PX_TO_INCH * config.scale;
|
|
2240
|
+
|
|
2241
|
+
const item = {
|
|
2242
|
+
type: 'image',
|
|
2243
|
+
zIndex,
|
|
2244
|
+
domOrder,
|
|
2245
|
+
options: { x, y, w, h, rotate: rotation, data: null },
|
|
2246
|
+
};
|
|
2247
|
+
|
|
2248
|
+
const job = async () => {
|
|
2249
|
+
const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx);
|
|
2250
|
+
if (canvasImageData) item.options.data = canvasImageData;
|
|
2251
|
+
else item.skip = true;
|
|
2252
|
+
};
|
|
2253
|
+
|
|
2254
|
+
return { items: [item], job, stopRecursion: true };
|
|
2255
|
+
}
|
|
2256
|
+
|
|
2257
|
+
// --- SYNC: Standard CSS Extraction ---
|
|
2258
|
+
const bgColorObj = parseColor(style.backgroundColor);
|
|
2259
|
+
const bgClip = style.webkitBackgroundClip || style.backgroundClip;
|
|
2260
|
+
const isBgClipText = bgClip === 'text';
|
|
2261
|
+
const hasGradient =
|
|
2262
|
+
!isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
|
|
2263
|
+
|
|
2264
|
+
const borderColorObj = parseColor(style.borderColor);
|
|
2265
|
+
const borderWidth = parseFloat(style.borderWidth);
|
|
2266
|
+
const hasBorder = borderWidth > 0 && borderColorObj.hex;
|
|
2267
|
+
|
|
2268
|
+
const borderInfo = getBorderInfo(style, config.scale);
|
|
2269
|
+
const hasUniformBorder = borderInfo.type === 'uniform';
|
|
2270
|
+
const hasCompositeBorder = borderInfo.type === 'composite';
|
|
2271
|
+
|
|
2272
|
+
const shadowStr = style.boxShadow;
|
|
2273
|
+
const hasShadow = shadowStr && shadowStr !== 'none';
|
|
2274
|
+
const softEdge = getSoftEdges(style.filter, config.scale);
|
|
2275
|
+
|
|
2276
|
+
let isImageWrapper = false;
|
|
2277
|
+
const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
|
|
2278
|
+
if (imgChild) {
|
|
2279
|
+
const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
|
|
2280
|
+
const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
|
|
2281
|
+
if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
|
|
2282
|
+
}
|
|
2283
|
+
|
|
2284
|
+
let textPayload = null;
|
|
2285
|
+
const isText = isTextContainer(node);
|
|
2286
|
+
|
|
2287
|
+
if (isText) {
|
|
2288
|
+
const textParts = [];
|
|
2289
|
+
let trimNextLeading = false;
|
|
2290
|
+
|
|
2291
|
+
node.childNodes.forEach((child, index) => {
|
|
2292
|
+
// Handle <br> tags
|
|
2293
|
+
if (child.tagName === 'BR') {
|
|
2294
|
+
// 1. Trim trailing space from the *previous* text part to prevent double wrapping
|
|
2295
|
+
if (textParts.length > 0) {
|
|
2296
|
+
const lastPart = textParts[textParts.length - 1];
|
|
2297
|
+
if (lastPart.text && typeof lastPart.text === 'string') {
|
|
2298
|
+
lastPart.text = lastPart.text.trimEnd();
|
|
2299
|
+
}
|
|
2300
|
+
}
|
|
2301
|
+
|
|
2302
|
+
textParts.push({ text: '', options: { breakLine: true } });
|
|
2303
|
+
|
|
2304
|
+
// 2. Signal to trim leading space from the *next* text part
|
|
2305
|
+
trimNextLeading = true;
|
|
2306
|
+
return;
|
|
2307
|
+
}
|
|
2308
|
+
|
|
2309
|
+
let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
|
|
2310
|
+
let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
|
|
2311
|
+
textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
|
|
2312
|
+
|
|
2313
|
+
// Trimming logic
|
|
2314
|
+
if (index === 0) textVal = textVal.trimStart();
|
|
2315
|
+
if (trimNextLeading) {
|
|
2316
|
+
textVal = textVal.trimStart();
|
|
2317
|
+
trimNextLeading = false;
|
|
2318
|
+
}
|
|
2319
|
+
|
|
2320
|
+
if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
|
|
2321
|
+
if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
|
|
2322
|
+
if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
|
|
2323
|
+
|
|
2324
|
+
if (textVal.length > 0) {
|
|
2325
|
+
const textOpts = getTextStyle(nodeStyle, config.scale, textVal, globalOptions);
|
|
2326
|
+
|
|
2327
|
+
// BUG FIX: Numbers 1 and 2 having background.
|
|
2328
|
+
// If this is a naked Text Node (nodeType 3), it inherits style from the parent container.
|
|
2329
|
+
// The parent container's background is already rendered as the Shape Fill.
|
|
2330
|
+
// We must NOT render it again as a Text Highlight, otherwise it looks like a solid marker on top of the shape.
|
|
2331
|
+
if (child.nodeType === 3 && textOpts.highlight) {
|
|
2332
|
+
delete textOpts.highlight;
|
|
2333
|
+
}
|
|
2334
|
+
|
|
2335
|
+
textParts.push({ text: textVal, options: textOpts });
|
|
2336
|
+
}
|
|
2337
|
+
});
|
|
2338
|
+
|
|
2339
|
+
if (textParts.length > 0) {
|
|
2340
|
+
let align = style.textAlign || 'left';
|
|
2341
|
+
if (align === 'start') align = 'left';
|
|
2342
|
+
if (align === 'end') align = 'right';
|
|
2343
|
+
let valign = 'top';
|
|
2344
|
+
if (style.alignItems === 'center') valign = 'middle';
|
|
2345
|
+
if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
|
|
2346
|
+
|
|
2347
|
+
const pt = parseFloat(style.paddingTop) || 0;
|
|
2348
|
+
const pb = parseFloat(style.paddingBottom) || 0;
|
|
2349
|
+
if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
|
|
2350
|
+
|
|
2351
|
+
let padding = getPadding(style, config.scale);
|
|
2352
|
+
if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
|
|
2353
|
+
|
|
2354
|
+
textPayload = { text: textParts, align, valign, inset: padding };
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
|
|
2359
|
+
let bgData = null;
|
|
2360
|
+
let padIn = 0;
|
|
2361
|
+
if (softEdge) {
|
|
2362
|
+
const svgInfo = generateBlurredSVG(
|
|
2363
|
+
widthPx,
|
|
2364
|
+
heightPx,
|
|
2365
|
+
bgColorObj.hex,
|
|
2366
|
+
borderRadiusValue,
|
|
2367
|
+
softEdge
|
|
2368
|
+
);
|
|
2369
|
+
bgData = svgInfo.data;
|
|
2370
|
+
padIn = svgInfo.padding * PX_TO_INCH * config.scale;
|
|
2371
|
+
} else {
|
|
2372
|
+
bgData = generateGradientSVG(
|
|
2373
|
+
widthPx,
|
|
2374
|
+
heightPx,
|
|
2375
|
+
style.backgroundImage,
|
|
2376
|
+
borderRadiusValue,
|
|
2377
|
+
hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
|
|
2378
|
+
);
|
|
2379
|
+
}
|
|
2380
|
+
|
|
2381
|
+
if (bgData) {
|
|
2382
|
+
items.push({
|
|
2383
|
+
type: 'image',
|
|
2384
|
+
zIndex,
|
|
2385
|
+
domOrder,
|
|
2386
|
+
options: {
|
|
2387
|
+
data: bgData,
|
|
2388
|
+
x: x - padIn,
|
|
2389
|
+
y: y - padIn,
|
|
2390
|
+
w: w + padIn * 2,
|
|
2391
|
+
h: h + padIn * 2,
|
|
2392
|
+
rotate: rotation,
|
|
2393
|
+
},
|
|
2394
|
+
});
|
|
2395
|
+
}
|
|
2396
|
+
|
|
2397
|
+
if (textPayload) {
|
|
2398
|
+
textPayload.text[0].options.fontSize =
|
|
2399
|
+
Math.floor(textPayload.text[0]?.options?.fontSize) || 12;
|
|
2400
|
+
items.push({
|
|
2401
|
+
type: 'text',
|
|
2402
|
+
zIndex: zIndex + 1,
|
|
2403
|
+
domOrder,
|
|
2404
|
+
textParts: textPayload.text,
|
|
2405
|
+
options: {
|
|
2406
|
+
x,
|
|
2407
|
+
y,
|
|
2408
|
+
w,
|
|
2409
|
+
h,
|
|
2410
|
+
align: textPayload.align,
|
|
2411
|
+
valign: textPayload.valign,
|
|
2412
|
+
inset: textPayload.inset,
|
|
2413
|
+
rotate: rotation,
|
|
2414
|
+
margin: 0,
|
|
2415
|
+
wrap: true,
|
|
2416
|
+
autoFit: false,
|
|
2417
|
+
},
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
if (hasCompositeBorder) {
|
|
2421
|
+
const borderItems = createCompositeBorderItems(
|
|
2422
|
+
borderInfo.sides,
|
|
2423
|
+
x,
|
|
2424
|
+
y,
|
|
2425
|
+
w,
|
|
2426
|
+
h,
|
|
2427
|
+
config.scale,
|
|
2428
|
+
zIndex,
|
|
2429
|
+
domOrder
|
|
2430
|
+
);
|
|
2431
|
+
items.push(...borderItems);
|
|
2432
|
+
}
|
|
2433
|
+
} else if (
|
|
2434
|
+
(bgColorObj.hex && !isImageWrapper) ||
|
|
2435
|
+
hasUniformBorder ||
|
|
2436
|
+
hasCompositeBorder ||
|
|
2437
|
+
hasShadow ||
|
|
2438
|
+
textPayload
|
|
2439
|
+
) {
|
|
2440
|
+
const finalAlpha = safeOpacity * bgColorObj.opacity;
|
|
2441
|
+
const transparency = (1 - finalAlpha) * 100;
|
|
2442
|
+
const useSolidFill = bgColorObj.hex && !isImageWrapper;
|
|
2443
|
+
|
|
2444
|
+
if (hasPartialBorderRadius && useSolidFill && !textPayload) {
|
|
2445
|
+
const shapeSvg = generateCustomShapeSVG(
|
|
2446
|
+
widthPx,
|
|
2447
|
+
heightPx,
|
|
2448
|
+
bgColorObj.hex,
|
|
2449
|
+
bgColorObj.opacity,
|
|
2450
|
+
{
|
|
2451
|
+
tl: parseFloat(style.borderTopLeftRadius) || 0,
|
|
2452
|
+
tr: parseFloat(style.borderTopRightRadius) || 0,
|
|
2453
|
+
br: parseFloat(style.borderBottomRightRadius) || 0,
|
|
2454
|
+
bl: parseFloat(style.borderBottomLeftRadius) || 0,
|
|
2455
|
+
}
|
|
2456
|
+
);
|
|
2457
|
+
|
|
2458
|
+
items.push({
|
|
2459
|
+
type: 'image',
|
|
2460
|
+
zIndex,
|
|
2461
|
+
domOrder,
|
|
2462
|
+
options: { data: shapeSvg, x, y, w, h, rotate: rotation },
|
|
2463
|
+
});
|
|
2464
|
+
} else {
|
|
2465
|
+
const shapeOpts = {
|
|
2466
|
+
x,
|
|
2467
|
+
y,
|
|
2468
|
+
w,
|
|
2469
|
+
h,
|
|
2470
|
+
rotate: rotation,
|
|
2471
|
+
fill: useSolidFill
|
|
2472
|
+
? { color: bgColorObj.hex, transparency: transparency }
|
|
2473
|
+
: { type: 'none' },
|
|
2474
|
+
line: hasUniformBorder ? borderInfo.options : null,
|
|
2475
|
+
};
|
|
2476
|
+
|
|
2477
|
+
if (hasShadow) shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
|
|
2478
|
+
|
|
2479
|
+
// 1. Calculate dimensions first
|
|
2480
|
+
const minDimension = Math.min(widthPx, heightPx);
|
|
2481
|
+
|
|
2482
|
+
let rawRadius = parseFloat(style.borderRadius) || 0;
|
|
2483
|
+
const isPercentage = style.borderRadius && style.borderRadius.toString().includes('%');
|
|
2484
|
+
|
|
2485
|
+
// 2. Normalize radius to pixels
|
|
2486
|
+
let radiusPx = rawRadius;
|
|
2487
|
+
if (isPercentage) {
|
|
2488
|
+
radiusPx = (rawRadius / 100) * minDimension;
|
|
2489
|
+
}
|
|
2490
|
+
|
|
2491
|
+
let shapeType = pptx.ShapeType.rect;
|
|
2492
|
+
|
|
2493
|
+
// 3. Determine Shape Logic
|
|
2494
|
+
const isSquare = Math.abs(widthPx - heightPx) < 1;
|
|
2495
|
+
const isFullyRound = radiusPx >= minDimension / 2;
|
|
2496
|
+
|
|
2497
|
+
// CASE A: It is an Ellipse if:
|
|
2498
|
+
// 1. It is explicitly "50%" (standard CSS way to make ovals/circles)
|
|
2499
|
+
// 2. OR it is a perfect square and fully rounded (a circle)
|
|
2500
|
+
if (isFullyRound && (isPercentage || isSquare)) {
|
|
2501
|
+
shapeType = pptx.ShapeType.ellipse;
|
|
2502
|
+
}
|
|
2503
|
+
// CASE B: It is a Rounded Rectangle (including "Pill" shapes)
|
|
2504
|
+
else if (radiusPx > 0) {
|
|
2505
|
+
shapeType = pptx.ShapeType.roundRect;
|
|
2506
|
+
let r = radiusPx / minDimension;
|
|
2507
|
+
if (r > 0.5) r = 0.5;
|
|
2508
|
+
if (minDimension < 100) r = r * 0.25; // Small size adjustment for small shapes
|
|
2509
|
+
|
|
2510
|
+
shapeOpts.rectRadius = r;
|
|
2511
|
+
}
|
|
2512
|
+
|
|
2513
|
+
if (textPayload) {
|
|
2514
|
+
textPayload.text[0].options.fontSize =
|
|
2515
|
+
Math.floor(textPayload.text[0]?.options?.fontSize) || 12;
|
|
2516
|
+
const textOptions = {
|
|
2517
|
+
shape: shapeType,
|
|
2518
|
+
...shapeOpts,
|
|
2519
|
+
rotate: rotation,
|
|
2520
|
+
align: textPayload.align,
|
|
2521
|
+
valign: textPayload.valign,
|
|
2522
|
+
inset: textPayload.inset,
|
|
2523
|
+
margin: 0,
|
|
2524
|
+
wrap: true,
|
|
2525
|
+
autoFit: false,
|
|
2526
|
+
};
|
|
2527
|
+
items.push({
|
|
2528
|
+
type: 'text',
|
|
2529
|
+
zIndex,
|
|
2530
|
+
domOrder,
|
|
2531
|
+
textParts: textPayload.text,
|
|
2532
|
+
options: textOptions,
|
|
2533
|
+
});
|
|
2534
|
+
} else if (!hasPartialBorderRadius) {
|
|
2535
|
+
items.push({
|
|
2536
|
+
type: 'shape',
|
|
2537
|
+
zIndex,
|
|
2538
|
+
domOrder,
|
|
2539
|
+
shapeType,
|
|
2540
|
+
options: shapeOpts,
|
|
2541
|
+
});
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
|
|
2545
|
+
if (hasCompositeBorder) {
|
|
2546
|
+
const borderSvgData = generateCompositeBorderSVG(
|
|
2547
|
+
widthPx,
|
|
2548
|
+
heightPx,
|
|
2549
|
+
borderRadiusValue,
|
|
2550
|
+
borderInfo.sides
|
|
2551
|
+
);
|
|
2552
|
+
if (borderSvgData) {
|
|
2553
|
+
items.push({
|
|
2554
|
+
type: 'image',
|
|
2555
|
+
zIndex: zIndex + 1,
|
|
2556
|
+
domOrder,
|
|
2557
|
+
options: { data: borderSvgData, x, y, w, h, rotate: rotation },
|
|
2558
|
+
});
|
|
2559
|
+
}
|
|
2560
|
+
}
|
|
2561
|
+
}
|
|
2562
|
+
|
|
2563
|
+
return { items, stopRecursion: !!textPayload };
|
|
2564
|
+
}
|
|
2565
|
+
|
|
2566
|
+
function isComplexHierarchy(root) {
|
|
2567
|
+
// Use a simple tree traversal to find forbidden elements in the list structure
|
|
2568
|
+
if (root?.getAttribute?.('data-pptx-list') === 'complex') return true;
|
|
2569
|
+
|
|
2570
|
+
const stack = [root];
|
|
2571
|
+
while (stack.length > 0) {
|
|
2572
|
+
const el = stack.pop();
|
|
2573
|
+
|
|
2574
|
+
// 1. Layouts: Flex/Grid on LIs
|
|
2575
|
+
if (el.tagName === 'LI') {
|
|
2576
|
+
const s = window.getComputedStyle(el);
|
|
2577
|
+
if (s.display === 'flex' || s.display === 'grid' || s.display === 'inline-flex') return true;
|
|
2578
|
+
|
|
2579
|
+
// Custom list items (e.g., list-style: none + structured children)
|
|
2580
|
+
const listStyleType = s.listStyleType || s.listStyle;
|
|
2581
|
+
if (listStyleType === 'none') {
|
|
2582
|
+
const hasStructuredChild = Array.from(el.children).some((child) => {
|
|
2583
|
+
const cs = window.getComputedStyle(child);
|
|
2584
|
+
const display = cs.display || '';
|
|
2585
|
+
if (!display.includes('inline')) return true;
|
|
2586
|
+
|
|
2587
|
+
const bg = parseColor(cs.backgroundColor);
|
|
2588
|
+
if (bg.hex && bg.opacity > 0) return true;
|
|
2589
|
+
|
|
2590
|
+
const bw = parseFloat(cs.borderWidth) || 0;
|
|
2591
|
+
const bc = parseColor(cs.borderColor);
|
|
2592
|
+
if (bw > 0 && bc.opacity > 0) return true;
|
|
2593
|
+
|
|
2594
|
+
return false;
|
|
2595
|
+
});
|
|
2596
|
+
|
|
2597
|
+
if (hasStructuredChild) return true;
|
|
2598
|
+
}
|
|
2599
|
+
}
|
|
2600
|
+
|
|
2601
|
+
// 2. Media / Icons
|
|
2602
|
+
if (['IMG', 'SVG', 'CANVAS', 'VIDEO', 'IFRAME'].includes(el.tagName)) return true;
|
|
2603
|
+
if (isIconElement(el)) return true;
|
|
2604
|
+
|
|
2605
|
+
// 3. Nested Lists (Flattening logic doesn't support nested bullets well yet)
|
|
2606
|
+
if (el !== root && (el.tagName === 'UL' || el.tagName === 'OL')) return true;
|
|
2607
|
+
|
|
2608
|
+
// Recurse, but don't go too deep if not needed
|
|
2609
|
+
for (let i = 0; i < el.children.length; i++) {
|
|
2610
|
+
stack.push(el.children[i]);
|
|
2611
|
+
}
|
|
2612
|
+
}
|
|
2613
|
+
return false;
|
|
2614
|
+
}
|
|
2615
|
+
|
|
2616
|
+
function collectListParts(node, parentStyle, scale, globalOptions) {
|
|
2617
|
+
const parts = [];
|
|
2618
|
+
|
|
2619
|
+
// Check for CSS Content (::before) - often used for icons
|
|
2620
|
+
if (node.nodeType === 1) {
|
|
2621
|
+
const beforeStyle = window.getComputedStyle(node, '::before');
|
|
2622
|
+
const content = beforeStyle.content;
|
|
2623
|
+
if (content && content !== 'none' && content !== 'normal' && content !== '""') {
|
|
2624
|
+
// Strip quotes
|
|
2625
|
+
const cleanContent = content.replace(/^['"]|['"]$/g, '');
|
|
2626
|
+
if (cleanContent.trim()) {
|
|
2627
|
+
parts.push({
|
|
2628
|
+
text: cleanContent + ' ', // Add space after icon
|
|
2629
|
+
options: getTextStyle(window.getComputedStyle(node), scale, cleanContent, globalOptions),
|
|
2630
|
+
});
|
|
2631
|
+
}
|
|
2632
|
+
}
|
|
2633
|
+
}
|
|
2634
|
+
|
|
2635
|
+
node.childNodes.forEach((child) => {
|
|
2636
|
+
if (child.nodeType === 3) {
|
|
2637
|
+
// Text
|
|
2638
|
+
const val = child.nodeValue.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
|
|
2639
|
+
if (val) {
|
|
2640
|
+
// Use parent style if child is text node, otherwise current style
|
|
2641
|
+
const styleToUse = node.nodeType === 1 ? window.getComputedStyle(node) : parentStyle;
|
|
2642
|
+
parts.push({
|
|
2643
|
+
text: val,
|
|
2644
|
+
options: getTextStyle(styleToUse, scale, val, globalOptions),
|
|
2645
|
+
});
|
|
2646
|
+
}
|
|
2647
|
+
} else if (child.nodeType === 1) {
|
|
2648
|
+
// Element (span, i, b)
|
|
2649
|
+
// Recurse
|
|
2650
|
+
parts.push(...collectListParts(child, parentStyle, scale, globalOptions));
|
|
2651
|
+
}
|
|
2652
|
+
});
|
|
2653
|
+
|
|
2654
|
+
return parts;
|
|
2655
|
+
}
|
|
2656
|
+
|
|
2657
|
+
function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
|
|
2658
|
+
const items = [];
|
|
2659
|
+
const pxToInch = 1 / 96;
|
|
2660
|
+
const common = { zIndex: zIndex + 1, domOrder, shapeType: 'rect' };
|
|
2661
|
+
|
|
2662
|
+
if (sides.top.width > 0)
|
|
2663
|
+
items.push({
|
|
2664
|
+
...common,
|
|
2665
|
+
options: { x, y, w, h: sides.top.width * pxToInch * scale, fill: { color: sides.top.color } },
|
|
2666
|
+
});
|
|
2667
|
+
if (sides.right.width > 0)
|
|
2668
|
+
items.push({
|
|
2669
|
+
...common,
|
|
2670
|
+
options: {
|
|
2671
|
+
x: x + w - sides.right.width * pxToInch * scale,
|
|
2672
|
+
y,
|
|
2673
|
+
w: sides.right.width * pxToInch * scale,
|
|
2674
|
+
h,
|
|
2675
|
+
fill: { color: sides.right.color },
|
|
2676
|
+
},
|
|
2677
|
+
});
|
|
2678
|
+
if (sides.bottom.width > 0)
|
|
2679
|
+
items.push({
|
|
2680
|
+
...common,
|
|
2681
|
+
options: {
|
|
2682
|
+
x,
|
|
2683
|
+
y: y + h - sides.bottom.width * pxToInch * scale,
|
|
2684
|
+
w,
|
|
2685
|
+
h: sides.bottom.width * pxToInch * scale,
|
|
2686
|
+
fill: { color: sides.bottom.color },
|
|
2687
|
+
},
|
|
2688
|
+
});
|
|
2689
|
+
if (sides.left.width > 0)
|
|
2690
|
+
items.push({
|
|
2691
|
+
...common,
|
|
2692
|
+
options: {
|
|
2693
|
+
x,
|
|
2694
|
+
y,
|
|
2695
|
+
w: sides.left.width * pxToInch * scale,
|
|
2696
|
+
h,
|
|
2697
|
+
fill: { color: sides.left.color },
|
|
2698
|
+
},
|
|
2699
|
+
});
|
|
2700
|
+
|
|
2701
|
+
return items;
|
|
2702
|
+
}
|
|
2703
|
+
|
|
2704
|
+
export { exportToPptx };
|
|
2705
|
+
//# sourceMappingURL=dom-to-pptx.mjs.map
|