dom-to-pptx 1.0.6 → 1.0.8

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/src/index.js CHANGED
@@ -1,657 +1,755 @@
1
- // src/index.js
2
- import * as PptxGenJSImport from 'pptxgenjs';
3
- import html2canvas from 'html2canvas';
4
-
5
- // Normalize import
6
- const PptxGenJS = PptxGenJSImport?.default ?? PptxGenJSImport;
7
-
8
- import {
9
- parseColor,
10
- getTextStyle,
11
- isTextContainer,
12
- getVisibleShadow,
13
- generateGradientSVG,
14
- getRotation,
15
- svgToPng,
16
- getPadding,
17
- getSoftEdges,
18
- generateBlurredSVG,
19
- getBorderInfo,
20
- generateCompositeBorderSVG,
21
- isClippedByParent,
22
- generateCustomShapeSVG,
23
- } from './utils.js';
24
- import { getProcessedImage } from './image-processor.js';
25
-
26
- const PPI = 96;
27
- const PX_TO_INCH = 1 / PPI;
28
-
29
- /**
30
- * Main export function. Accepts single element or an array.
31
- * @param {HTMLElement | string | Array<HTMLElement | string>} target - The root element(s) to convert.
32
- * @param {Object} options - { fileName: string }
33
- */
34
- export async function exportToPptx(target, options = {}) {
35
- const resolvePptxConstructor = (pkg) => {
36
- if (!pkg) return null;
37
- if (typeof pkg === 'function') return pkg;
38
- if (pkg && typeof pkg.default === 'function') return pkg.default;
39
- if (pkg && typeof pkg.PptxGenJS === 'function') return pkg.PptxGenJS;
40
- if (pkg && pkg.PptxGenJS && typeof pkg.PptxGenJS.default === 'function')
41
- return pkg.PptxGenJS.default;
42
- return null;
43
- };
44
-
45
- const PptxConstructor = resolvePptxConstructor(PptxGenJS);
46
- if (!PptxConstructor) throw new Error('PptxGenJS constructor not found.');
47
- const pptx = new PptxConstructor();
48
- pptx.layout = 'LAYOUT_16x9';
49
-
50
- const elements = Array.isArray(target) ? target : [target];
51
-
52
- for (const el of elements) {
53
- const root = typeof el === 'string' ? document.querySelector(el) : el;
54
- if (!root) {
55
- console.warn('Element not found, skipping slide:', el);
56
- continue;
57
- }
58
- const slide = pptx.addSlide();
59
- await processSlide(root, slide, pptx);
60
- }
61
-
62
- const fileName = options.fileName || 'export.pptx';
63
- pptx.writeFile({ fileName });
64
- }
65
-
66
- /**
67
- * Worker function to process a single DOM element into a single PPTX slide.
68
- * @param {HTMLElement} root - The root element for this slide.
69
- * @param {PptxGenJS.Slide} slide - The PPTX slide object to add content to.
70
- * @param {PptxGenJS} pptx - The main PPTX instance.
71
- */
72
- async function processSlide(root, slide, pptx) {
73
- const rootRect = root.getBoundingClientRect();
74
- const PPTX_WIDTH_IN = 10;
75
- const PPTX_HEIGHT_IN = 5.625;
76
-
77
- const contentWidthIn = rootRect.width * PX_TO_INCH;
78
- const contentHeightIn = rootRect.height * PX_TO_INCH;
79
- const scale = Math.min(PPTX_WIDTH_IN / contentWidthIn, PPTX_HEIGHT_IN / contentHeightIn);
80
-
81
- const layoutConfig = {
82
- rootX: rootRect.x,
83
- rootY: rootRect.y,
84
- scale: scale,
85
- offX: (PPTX_WIDTH_IN - contentWidthIn * scale) / 2,
86
- offY: (PPTX_HEIGHT_IN - contentHeightIn * scale) / 2,
87
- };
88
-
89
- const renderQueue = [];
90
- let domOrderCounter = 0;
91
-
92
- async function collect(node) {
93
- const order = domOrderCounter++;
94
- const result = await createRenderItem(node, { ...layoutConfig, root }, order, pptx);
95
- if (result) {
96
- if (result.items) renderQueue.push(...result.items);
97
- if (result.stopRecursion) return;
98
- }
99
- for (const child of node.children) await collect(child);
100
- }
101
-
102
- await collect(root);
103
-
104
- renderQueue.sort((a, b) => {
105
- if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
106
- return a.domOrder - b.domOrder;
107
- });
108
-
109
- for (const item of renderQueue) {
110
- if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
111
- if (item.type === 'image') slide.addImage(item.options);
112
- if (item.type === 'text') slide.addText(item.textParts, item.options);
113
- }
114
- }
115
-
116
- async function elementToCanvasImage(node, widthPx, heightPx, root) {
117
- return new Promise((resolve) => {
118
- const width = Math.ceil(widthPx);
119
- const height = Math.ceil(heightPx);
120
-
121
- if (width <= 0 || height <= 0) {
122
- resolve(null);
123
- return;
124
- }
125
-
126
- const style = window.getComputedStyle(node);
127
-
128
- html2canvas(root, {
129
- width: root.scrollWidth,
130
- height: root.scrollHeight,
131
- useCORS: true,
132
- allowTaint: true,
133
- backgroundColor: null,
134
- })
135
- .then((canvas) => {
136
- const rootCanvas = canvas;
137
- const nodeRect = node.getBoundingClientRect();
138
- const rootRect = root.getBoundingClientRect();
139
- const sourceX = nodeRect.left - rootRect.left;
140
- const sourceY = nodeRect.top - rootRect.top;
141
-
142
- const destCanvas = document.createElement('canvas');
143
- destCanvas.width = width;
144
- destCanvas.height = height;
145
- const ctx = destCanvas.getContext('2d');
146
-
147
- ctx.drawImage(rootCanvas, sourceX, sourceY, width, height, 0, 0, width, height);
148
-
149
- // Parse radii
150
- let tl = parseFloat(style.borderTopLeftRadius) || 0;
151
- let tr = parseFloat(style.borderTopRightRadius) || 0;
152
- let br = parseFloat(style.borderBottomRightRadius) || 0;
153
- let bl = parseFloat(style.borderBottomLeftRadius) || 0;
154
-
155
- const f = Math.min(
156
- width / (tl + tr) || Infinity,
157
- height / (tr + br) || Infinity,
158
- width / (br + bl) || Infinity,
159
- height / (bl + tl) || Infinity
160
- );
161
-
162
- if (f < 1) {
163
- tl *= f;
164
- tr *= f;
165
- br *= f;
166
- bl *= f;
167
- }
168
-
169
- ctx.globalCompositeOperation = 'destination-in';
170
- ctx.beginPath();
171
- ctx.moveTo(tl, 0);
172
- ctx.lineTo(width - tr, 0);
173
- ctx.arcTo(width, 0, width, tr, tr);
174
- ctx.lineTo(width, height - br);
175
- ctx.arcTo(width, height, width - br, height, br);
176
- ctx.lineTo(bl, height);
177
- ctx.arcTo(0, height, 0, height - bl, bl);
178
- ctx.lineTo(0, tl);
179
- ctx.arcTo(0, 0, tl, 0, tl);
180
- ctx.closePath();
181
- ctx.fill();
182
-
183
- resolve(destCanvas.toDataURL('image/png'));
184
- })
185
- .catch(() => resolve(null));
186
- });
187
- }
188
-
189
- async function createRenderItem(node, config, domOrder, pptx) {
190
- if (node.nodeType !== 1) return null;
191
- const style = window.getComputedStyle(node);
192
- if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')
193
- return null;
194
-
195
- const rect = node.getBoundingClientRect();
196
- if (rect.width < 0.5 || rect.height < 0.5) return null;
197
-
198
- const zIndex = style.zIndex !== 'auto' ? parseInt(style.zIndex) : 0;
199
- const rotation = getRotation(style.transform);
200
- const elementOpacity = parseFloat(style.opacity);
201
-
202
- const widthPx = node.offsetWidth || rect.width;
203
- const heightPx = node.offsetHeight || rect.height;
204
- const unrotatedW = widthPx * PX_TO_INCH * config.scale;
205
- const unrotatedH = heightPx * PX_TO_INCH * config.scale;
206
- const centerX = rect.left + rect.width / 2;
207
- const centerY = rect.top + rect.height / 2;
208
-
209
- let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
210
- let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
211
- let w = unrotatedW;
212
- let h = unrotatedH;
213
-
214
- const items = [];
215
-
216
- if (node.nodeName.toUpperCase() === 'SVG') {
217
- const pngData = await svgToPng(node);
218
- if (pngData)
219
- items.push({
220
- type: 'image',
221
- zIndex,
222
- domOrder,
223
- options: { data: pngData, x, y, w, h, rotate: rotation },
224
- });
225
- return { items, stopRecursion: true };
226
- }
227
-
228
- // --- UPDATED IMG BLOCK START ---
229
- if (node.tagName === 'IMG') {
230
- // Extract individual corner radii
231
- let radii = {
232
- tl: parseFloat(style.borderTopLeftRadius) || 0,
233
- tr: parseFloat(style.borderTopRightRadius) || 0,
234
- br: parseFloat(style.borderBottomRightRadius) || 0,
235
- bl: parseFloat(style.borderBottomLeftRadius) || 0,
236
- };
237
-
238
- const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
239
-
240
- // Fallback: Check parent if image has no specific radius but parent clips it
241
- if (!hasAnyRadius) {
242
- const parent = node.parentElement;
243
- const parentStyle = window.getComputedStyle(parent);
244
- if (parentStyle.overflow !== 'visible') {
245
- const pRadii = {
246
- tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
247
- tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
248
- br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
249
- bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
250
- };
251
- // Simple heuristic: If image takes up full size of parent, inherit radii.
252
- // For complex grids (like slide-1), this blindly applies parent radius.
253
- // In a perfect world, we'd calculate intersection, but for now we apply parent radius
254
- // if the image is close to the parent's size, effectively masking it.
255
- const pRect = parent.getBoundingClientRect();
256
- if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
257
- radii = pRadii;
258
- }
259
- }
260
- }
261
-
262
- const processed = await getProcessedImage(node.src, widthPx, heightPx, radii);
263
- if (processed)
264
- items.push({
265
- type: 'image',
266
- zIndex,
267
- domOrder,
268
- options: { data: processed, x, y, w, h, rotate: rotation },
269
- });
270
- return { items, stopRecursion: true };
271
- }
272
- // --- UPDATED IMG BLOCK END ---
273
-
274
- // Radii processing for Divs/Shapes
275
- const borderRadiusValue = parseFloat(style.borderRadius) || 0;
276
- const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
277
- const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
278
- const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
279
- const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
280
-
281
- const hasPartialBorderRadius =
282
- (borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
283
- (borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
284
- (borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
285
- (borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
286
- (borderRadiusValue === 0 &&
287
- (borderBottomLeftRadius ||
288
- borderBottomRightRadius ||
289
- borderTopLeftRadius ||
290
- borderTopRightRadius));
291
-
292
- // Allow clipped elements to be rendered via canvas
293
- if (hasPartialBorderRadius && isClippedByParent(node)) {
294
- const marginLeft = parseFloat(style.marginLeft) || 0;
295
- const marginTop = parseFloat(style.marginTop) || 0;
296
- x += marginLeft * PX_TO_INCH * config.scale;
297
- y += marginTop * PX_TO_INCH * config.scale;
298
-
299
- const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx, config.root);
300
- if (canvasImageData) {
301
- items.push({
302
- type: 'image',
303
- zIndex,
304
- domOrder,
305
- options: { data: canvasImageData, x, y, w, h, rotate: rotation },
306
- });
307
- return { items, stopRecursion: true };
308
- }
309
- }
310
-
311
- const bgColorObj = parseColor(style.backgroundColor);
312
- const bgClip = style.webkitBackgroundClip || style.backgroundClip;
313
- const isBgClipText = bgClip === 'text';
314
- const hasGradient =
315
- !isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
316
-
317
- const borderColorObj = parseColor(style.borderColor);
318
- const borderWidth = parseFloat(style.borderWidth);
319
- const hasBorder = borderWidth > 0 && borderColorObj.hex;
320
-
321
- const borderInfo = getBorderInfo(style, config.scale);
322
- const hasUniformBorder = borderInfo.type === 'uniform';
323
- const hasCompositeBorder = borderInfo.type === 'composite';
324
-
325
- const shadowStr = style.boxShadow;
326
- const hasShadow = shadowStr && shadowStr !== 'none';
327
- const softEdge = getSoftEdges(style.filter, config.scale);
328
-
329
- let isImageWrapper = false;
330
- const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
331
- if (imgChild) {
332
- const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
333
- const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
334
- if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
335
- }
336
-
337
- let textPayload = null;
338
- const isText = isTextContainer(node);
339
-
340
- if (isText) {
341
- const textParts = [];
342
- const isList = style.display === 'list-item';
343
- if (isList) {
344
- const fontSizePt = parseFloat(style.fontSize) * 0.75 * config.scale;
345
- const bulletShift = (parseFloat(style.fontSize) || 16) * PX_TO_INCH * config.scale * 1.5;
346
- x -= bulletShift;
347
- w += bulletShift;
348
- textParts.push({
349
- text: '• ',
350
- options: {
351
- color: parseColor(style.color).hex || '000000',
352
- fontSize: fontSizePt,
353
- },
354
- });
355
- }
356
-
357
- node.childNodes.forEach((child, index) => {
358
- let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
359
- let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
360
- textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
361
- if (index === 0 && !isList) textVal = textVal.trimStart();
362
- else if (index === 0) textVal = textVal.trimStart();
363
- if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
364
- if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
365
- if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
366
-
367
- if (textVal.length > 0) {
368
- textParts.push({
369
- text: textVal,
370
- options: getTextStyle(nodeStyle, config.scale),
371
- });
372
- }
373
- });
374
-
375
- if (textParts.length > 0) {
376
- let align = style.textAlign || 'left';
377
- if (align === 'start') align = 'left';
378
- if (align === 'end') align = 'right';
379
- let valign = 'top';
380
- if (style.alignItems === 'center') valign = 'middle';
381
- if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
382
-
383
- const pt = parseFloat(style.paddingTop) || 0;
384
- const pb = parseFloat(style.paddingBottom) || 0;
385
- if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
386
-
387
- let padding = getPadding(style, config.scale);
388
- if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
389
-
390
- textPayload = { text: textParts, align, valign, inset: padding };
391
- }
392
- }
393
-
394
- if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
395
- let bgData = null;
396
- let padIn = 0;
397
- if (softEdge) {
398
- const svgInfo = generateBlurredSVG(
399
- widthPx,
400
- heightPx,
401
- bgColorObj.hex,
402
- borderRadiusValue,
403
- softEdge
404
- );
405
- bgData = svgInfo.data;
406
- padIn = svgInfo.padding * PX_TO_INCH * config.scale;
407
- } else {
408
- bgData = generateGradientSVG(
409
- widthPx,
410
- heightPx,
411
- style.backgroundImage,
412
- borderRadiusValue,
413
- hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
414
- );
415
- }
416
-
417
- if (bgData) {
418
- items.push({
419
- type: 'image',
420
- zIndex,
421
- domOrder,
422
- options: {
423
- data: bgData,
424
- x: x - padIn,
425
- y: y - padIn,
426
- w: w + padIn * 2,
427
- h: h + padIn * 2,
428
- rotate: rotation,
429
- },
430
- });
431
- }
432
-
433
- if (textPayload) {
434
- items.push({
435
- type: 'text',
436
- zIndex: zIndex + 1,
437
- domOrder,
438
- textParts: textPayload.text,
439
- options: {
440
- x,
441
- y,
442
- w,
443
- h,
444
- align: textPayload.align,
445
- valign: textPayload.valign,
446
- inset: textPayload.inset,
447
- rotate: rotation,
448
- margin: 0,
449
- wrap: true,
450
- autoFit: false,
451
- },
452
- });
453
- }
454
- if (hasCompositeBorder) {
455
- // Add border shapes after the main background
456
- const borderItems = createCompositeBorderItems(
457
- borderInfo.sides,
458
- x,
459
- y,
460
- w,
461
- h,
462
- config.scale,
463
- zIndex,
464
- domOrder
465
- );
466
- items.push(...borderItems);
467
- }
468
- } else if (
469
- (bgColorObj.hex && !isImageWrapper) ||
470
- hasUniformBorder ||
471
- hasCompositeBorder ||
472
- hasShadow ||
473
- textPayload
474
- ) {
475
- const finalAlpha = elementOpacity * bgColorObj.opacity;
476
- const transparency = (1 - finalAlpha) * 100;
477
- const useSolidFill = bgColorObj.hex && !isImageWrapper;
478
-
479
- if (hasPartialBorderRadius && useSolidFill && !textPayload) {
480
- const shapeSvg = generateCustomShapeSVG(
481
- widthPx,
482
- heightPx,
483
- bgColorObj.hex,
484
- bgColorObj.opacity,
485
- {
486
- tl: parseFloat(style.borderTopLeftRadius) || 0,
487
- tr: parseFloat(style.borderTopRightRadius) || 0,
488
- br: parseFloat(style.borderBottomRightRadius) || 0,
489
- bl: parseFloat(style.borderBottomLeftRadius) || 0,
490
- }
491
- );
492
-
493
- items.push({
494
- type: 'image',
495
- zIndex,
496
- domOrder,
497
- options: {
498
- data: shapeSvg,
499
- x,
500
- y,
501
- w,
502
- h,
503
- rotate: rotation,
504
- },
505
- });
506
- } else {
507
- const shapeOpts = {
508
- x,
509
- y,
510
- w,
511
- h,
512
- rotate: rotation,
513
- fill: useSolidFill
514
- ? { color: bgColorObj.hex, transparency: transparency }
515
- : { type: 'none' },
516
- line: hasUniformBorder ? borderInfo.options : null,
517
- };
518
-
519
- if (hasShadow) {
520
- shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
521
- }
522
-
523
- const borderRadius = parseFloat(style.borderRadius) || 0;
524
- const aspectRatio = Math.max(widthPx, heightPx) / Math.min(widthPx, heightPx);
525
- const isCircle = aspectRatio < 1.1 && borderRadius >= Math.min(widthPx, heightPx) / 2 - 1;
526
-
527
- let shapeType = pptx.ShapeType.rect;
528
- if (isCircle) shapeType = pptx.ShapeType.ellipse;
529
- else if (borderRadius > 0) {
530
- shapeType = pptx.ShapeType.roundRect;
531
- shapeOpts.rectRadius = Math.min(0.5, borderRadius / Math.min(widthPx, heightPx));
532
- }
533
-
534
- if (textPayload) {
535
- const textOptions = {
536
- shape: shapeType,
537
- ...shapeOpts,
538
- align: textPayload.align,
539
- valign: textPayload.valign,
540
- inset: textPayload.inset,
541
- margin: 0,
542
- wrap: true,
543
- autoFit: false,
544
- };
545
- items.push({
546
- type: 'text',
547
- zIndex,
548
- domOrder,
549
- textParts: textPayload.text,
550
- options: textOptions,
551
- });
552
- } else if (!hasPartialBorderRadius) {
553
- items.push({
554
- type: 'shape',
555
- zIndex,
556
- domOrder,
557
- shapeType,
558
- options: shapeOpts,
559
- });
560
- }
561
- }
562
-
563
- if (hasCompositeBorder) {
564
- const borderSvgData = generateCompositeBorderSVG(
565
- widthPx,
566
- heightPx,
567
- borderRadiusValue,
568
- borderInfo.sides
569
- );
570
- if (borderSvgData) {
571
- items.push({
572
- type: 'image',
573
- zIndex: zIndex + 1,
574
- domOrder,
575
- options: { data: borderSvgData, x, y, w, h, rotate: rotation },
576
- });
577
- }
578
- }
579
- }
580
-
581
- return { items, stopRecursion: !!textPayload };
582
- }
583
-
584
- /**
585
- * Helper function to create individual border shapes
586
- */
587
- function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
588
- const items = [];
589
- const pxToInch = 1 / 96;
590
-
591
- // TOP BORDER
592
- if (sides.top.width > 0) {
593
- items.push({
594
- type: 'shape',
595
- zIndex: zIndex + 1,
596
- domOrder,
597
- shapeType: 'rect',
598
- options: {
599
- x: x,
600
- y: y,
601
- w: w,
602
- h: sides.top.width * pxToInch * scale,
603
- fill: { color: sides.top.color },
604
- },
605
- });
606
- }
607
- // RIGHT BORDER
608
- if (sides.right.width > 0) {
609
- items.push({
610
- type: 'shape',
611
- zIndex: zIndex + 1,
612
- domOrder,
613
- shapeType: 'rect',
614
- options: {
615
- x: x + w - sides.right.width * pxToInch * scale,
616
- y: y,
617
- w: sides.right.width * pxToInch * scale,
618
- h: h,
619
- fill: { color: sides.right.color },
620
- },
621
- });
622
- }
623
- // BOTTOM BORDER
624
- if (sides.bottom.width > 0) {
625
- items.push({
626
- type: 'shape',
627
- zIndex: zIndex + 1,
628
- domOrder,
629
- shapeType: 'rect',
630
- options: {
631
- x: x,
632
- y: y + h - sides.bottom.width * pxToInch * scale,
633
- w: w,
634
- h: sides.bottom.width * pxToInch * scale,
635
- fill: { color: sides.bottom.color },
636
- },
637
- });
638
- }
639
- // LEFT BORDER
640
- if (sides.left.width > 0) {
641
- items.push({
642
- type: 'shape',
643
- zIndex: zIndex + 1,
644
- domOrder,
645
- shapeType: 'rect',
646
- options: {
647
- x: x,
648
- y: y,
649
- w: sides.left.width * pxToInch * scale,
650
- h: h,
651
- fill: { color: sides.left.color },
652
- },
653
- });
654
- }
655
-
656
- return items;
657
- }
1
+ // src/index.js
2
+ import * as PptxGenJSImport from 'pptxgenjs';
3
+ import html2canvas from 'html2canvas';
4
+
5
+ // Normalize import
6
+ const PptxGenJS = PptxGenJSImport?.default ?? PptxGenJSImport;
7
+
8
+ import {
9
+ parseColor,
10
+ getTextStyle,
11
+ isTextContainer,
12
+ getVisibleShadow,
13
+ generateGradientSVG,
14
+ getRotation,
15
+ svgToPng,
16
+ getPadding,
17
+ getSoftEdges,
18
+ generateBlurredSVG,
19
+ getBorderInfo,
20
+ generateCompositeBorderSVG,
21
+ isClippedByParent,
22
+ generateCustomShapeSVG,
23
+ } from './utils.js';
24
+ import { getProcessedImage } from './image-processor.js';
25
+
26
+ const PPI = 96;
27
+ const PX_TO_INCH = 1 / PPI;
28
+
29
+ /**
30
+ * Main export function. Accepts single element or an array.
31
+ * @param {HTMLElement | string | Array<HTMLElement | string>} target - The root element(s) to convert.
32
+ * @param {Object} options - { fileName: string }
33
+ */
34
+ export async function exportToPptx(target, options = {}) {
35
+ const resolvePptxConstructor = (pkg) => {
36
+ if (!pkg) return null;
37
+ if (typeof pkg === 'function') return pkg;
38
+ if (pkg && typeof pkg.default === 'function') return pkg.default;
39
+ if (pkg && typeof pkg.PptxGenJS === 'function') return pkg.PptxGenJS;
40
+ if (pkg && pkg.PptxGenJS && typeof pkg.PptxGenJS.default === 'function')
41
+ return pkg.PptxGenJS.default;
42
+ return null;
43
+ };
44
+
45
+ const PptxConstructor = resolvePptxConstructor(PptxGenJS);
46
+ if (!PptxConstructor) throw new Error('PptxGenJS constructor not found.');
47
+ const pptx = new PptxConstructor();
48
+ pptx.layout = 'LAYOUT_16x9';
49
+
50
+ const elements = Array.isArray(target) ? target : [target];
51
+
52
+ for (const el of elements) {
53
+ const root = typeof el === 'string' ? document.querySelector(el) : el;
54
+ if (!root) {
55
+ console.warn('Element not found, skipping slide:', el);
56
+ continue;
57
+ }
58
+ const slide = pptx.addSlide();
59
+ await processSlide(root, slide, pptx);
60
+ }
61
+
62
+ const fileName = options.fileName || 'export.pptx';
63
+ pptx.writeFile({ fileName });
64
+ }
65
+
66
+ /**
67
+ * Worker function to process a single DOM element into a single PPTX slide.
68
+ * @param {HTMLElement} root - The root element for this slide.
69
+ * @param {PptxGenJS.Slide} slide - The PPTX slide object to add content to.
70
+ * @param {PptxGenJS} pptx - The main PPTX instance.
71
+ */
72
+ async function processSlide(root, slide, pptx) {
73
+ const rootRect = root.getBoundingClientRect();
74
+ const PPTX_WIDTH_IN = 10;
75
+ const PPTX_HEIGHT_IN = 5.625;
76
+
77
+ const contentWidthIn = rootRect.width * PX_TO_INCH;
78
+ const contentHeightIn = rootRect.height * PX_TO_INCH;
79
+ const scale = Math.min(PPTX_WIDTH_IN / contentWidthIn, PPTX_HEIGHT_IN / contentHeightIn);
80
+
81
+ const layoutConfig = {
82
+ rootX: rootRect.x,
83
+ rootY: rootRect.y,
84
+ scale: scale,
85
+ offX: (PPTX_WIDTH_IN - contentWidthIn * scale) / 2,
86
+ offY: (PPTX_HEIGHT_IN - contentHeightIn * scale) / 2,
87
+ };
88
+
89
+ const renderQueue = [];
90
+ const asyncTasks = []; // Queue for heavy operations (Images, Canvas)
91
+ let domOrderCounter = 0;
92
+
93
+ // Sync Traversal Function
94
+ function collect(node, parentZIndex) {
95
+ const order = domOrderCounter++;
96
+
97
+ let currentZ = parentZIndex;
98
+ let nodeStyle = null;
99
+ const nodeType = node.nodeType;
100
+
101
+ if (nodeType === 1) {
102
+ nodeStyle = window.getComputedStyle(node);
103
+ // Optimization: Skip completely hidden elements immediately
104
+ if (
105
+ nodeStyle.display === 'none' ||
106
+ nodeStyle.visibility === 'hidden' ||
107
+ nodeStyle.opacity === '0'
108
+ ) {
109
+ return;
110
+ }
111
+ if (nodeStyle.zIndex !== 'auto') {
112
+ currentZ = parseInt(nodeStyle.zIndex);
113
+ }
114
+ }
115
+
116
+ // Prepare the item. If it needs async work, it returns a 'job'
117
+ const result = prepareRenderItem(
118
+ node,
119
+ { ...layoutConfig, root },
120
+ order,
121
+ pptx,
122
+ currentZ,
123
+ nodeStyle
124
+ );
125
+
126
+ if (result) {
127
+ if (result.items) {
128
+ // Push items immediately to queue (data might be missing but filled later)
129
+ renderQueue.push(...result.items);
130
+ }
131
+ if (result.job) {
132
+ // Push the promise-returning function to the task list
133
+ asyncTasks.push(result.job);
134
+ }
135
+ if (result.stopRecursion) return;
136
+ }
137
+
138
+ // Recurse children synchronously
139
+ const childNodes = node.childNodes;
140
+ for (let i = 0; i < childNodes.length; i++) {
141
+ collect(childNodes[i], currentZ);
142
+ }
143
+ }
144
+
145
+ // 1. Traverse and build the structure (Fast)
146
+ collect(root, 0);
147
+
148
+ // 2. Execute all heavy tasks in parallel (Fast)
149
+ if (asyncTasks.length > 0) {
150
+ await Promise.all(asyncTasks.map((task) => task()));
151
+ }
152
+
153
+ // 3. Cleanup and Sort
154
+ // Remove items that failed to generate data (marked with skip)
155
+ const finalQueue = renderQueue.filter(
156
+ (item) => !item.skip && (item.type !== 'image' || item.options.data)
157
+ );
158
+
159
+ finalQueue.sort((a, b) => {
160
+ if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
161
+ return a.domOrder - b.domOrder;
162
+ });
163
+
164
+ // 4. Add to Slide
165
+ for (const item of finalQueue) {
166
+ if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
167
+ if (item.type === 'image') slide.addImage(item.options);
168
+ if (item.type === 'text') slide.addText(item.textParts, item.options);
169
+ }
170
+ }
171
+
172
+ /**
173
+ * Optimized html2canvas wrapper
174
+ * Now strictly captures the node itself, not the root.
175
+ */
176
+ async function elementToCanvasImage(node, widthPx, heightPx) {
177
+ return new Promise((resolve) => {
178
+ const width = Math.max(Math.ceil(widthPx), 1);
179
+ const height = Math.max(Math.ceil(heightPx), 1);
180
+ const style = window.getComputedStyle(node);
181
+
182
+ // Optimized: Capture ONLY the specific node
183
+ html2canvas(node, {
184
+ backgroundColor: null,
185
+ logging: false,
186
+ scale: 2, // Slight quality boost
187
+ })
188
+ .then((canvas) => {
189
+ const destCanvas = document.createElement('canvas');
190
+ destCanvas.width = width;
191
+ destCanvas.height = height;
192
+ const ctx = destCanvas.getContext('2d');
193
+
194
+ // Draw the captured canvas into our sized canvas
195
+ // html2canvas might return a larger canvas if scale > 1, so we fit it
196
+ ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, width, height);
197
+
198
+ // Apply border radius clipping
199
+ let tl = parseFloat(style.borderTopLeftRadius) || 0;
200
+ let tr = parseFloat(style.borderTopRightRadius) || 0;
201
+ let br = parseFloat(style.borderBottomRightRadius) || 0;
202
+ let bl = parseFloat(style.borderBottomLeftRadius) || 0;
203
+
204
+ const f = Math.min(
205
+ width / (tl + tr) || Infinity,
206
+ height / (tr + br) || Infinity,
207
+ width / (br + bl) || Infinity,
208
+ height / (bl + tl) || Infinity
209
+ );
210
+
211
+ if (f < 1) {
212
+ tl *= f;
213
+ tr *= f;
214
+ br *= f;
215
+ bl *= f;
216
+ }
217
+
218
+ if (tl + tr + br + bl > 0) {
219
+ ctx.globalCompositeOperation = 'destination-in';
220
+ ctx.beginPath();
221
+ ctx.moveTo(tl, 0);
222
+ ctx.lineTo(width - tr, 0);
223
+ ctx.arcTo(width, 0, width, tr, tr);
224
+ ctx.lineTo(width, height - br);
225
+ ctx.arcTo(width, height, width - br, height, br);
226
+ ctx.lineTo(bl, height);
227
+ ctx.arcTo(0, height, 0, height - bl, bl);
228
+ ctx.lineTo(0, tl);
229
+ ctx.arcTo(0, 0, tl, 0, tl);
230
+ ctx.closePath();
231
+ ctx.fill();
232
+ }
233
+
234
+ resolve(destCanvas.toDataURL('image/png'));
235
+ })
236
+ .catch((e) => {
237
+ console.warn('Canvas capture failed for node', node, e);
238
+ resolve(null);
239
+ });
240
+ });
241
+ }
242
+
243
+ /**
244
+ * Replaces createRenderItem.
245
+ * Returns { items: [], job: () => Promise, stopRecursion: boolean }
246
+ */
247
+ function prepareRenderItem(node, config, domOrder, pptx, effectiveZIndex, computedStyle) {
248
+ // 1. Text Node Handling
249
+ if (node.nodeType === 3) {
250
+ const textContent = node.nodeValue.trim();
251
+ if (!textContent) return null;
252
+
253
+ const parent = node.parentElement;
254
+ if (!parent) return null;
255
+
256
+ if (isTextContainer(parent)) return null; // Parent handles it
257
+
258
+ const range = document.createRange();
259
+ range.selectNode(node);
260
+ const rect = range.getBoundingClientRect();
261
+ range.detach();
262
+
263
+ const style = window.getComputedStyle(parent);
264
+ const widthPx = rect.width;
265
+ const heightPx = rect.height;
266
+ const unrotatedW = widthPx * PX_TO_INCH * config.scale;
267
+ const unrotatedH = heightPx * PX_TO_INCH * config.scale;
268
+
269
+ const x = config.offX + (rect.left - config.rootX) * PX_TO_INCH * config.scale;
270
+ const y = config.offY + (rect.top - config.rootY) * PX_TO_INCH * config.scale;
271
+
272
+ return {
273
+ items: [
274
+ {
275
+ type: 'text',
276
+ zIndex: effectiveZIndex,
277
+ domOrder,
278
+ textParts: [
279
+ {
280
+ text: textContent,
281
+ options: getTextStyle(style, config.scale),
282
+ },
283
+ ],
284
+ options: { x, y, w: unrotatedW, h: unrotatedH, margin: 0, autoFit: false },
285
+ },
286
+ ],
287
+ stopRecursion: false,
288
+ };
289
+ }
290
+
291
+ if (node.nodeType !== 1) return null;
292
+ const style = computedStyle; // Use pre-computed style
293
+
294
+ const rect = node.getBoundingClientRect();
295
+ if (rect.width < 0.5 || rect.height < 0.5) return null;
296
+
297
+ const zIndex = effectiveZIndex;
298
+ const rotation = getRotation(style.transform);
299
+ const elementOpacity = parseFloat(style.opacity);
300
+ const safeOpacity = isNaN(elementOpacity) ? 1 : elementOpacity;
301
+
302
+ const widthPx = node.offsetWidth || rect.width;
303
+ const heightPx = node.offsetHeight || rect.height;
304
+ const unrotatedW = widthPx * PX_TO_INCH * config.scale;
305
+ const unrotatedH = heightPx * PX_TO_INCH * config.scale;
306
+ const centerX = rect.left + rect.width / 2;
307
+ const centerY = rect.top + rect.height / 2;
308
+
309
+ let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
310
+ let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
311
+ let w = unrotatedW;
312
+ let h = unrotatedH;
313
+
314
+ const items = [];
315
+
316
+ // --- ASYNC JOB: SVG Tags ---
317
+ if (node.nodeName.toUpperCase() === 'SVG') {
318
+ const item = {
319
+ type: 'image',
320
+ zIndex,
321
+ domOrder,
322
+ options: { data: null, x, y, w, h, rotate: rotation },
323
+ };
324
+
325
+ const job = async () => {
326
+ const processed = await svgToPng(node);
327
+ if (processed) item.options.data = processed;
328
+ else item.skip = true;
329
+ };
330
+
331
+ return { items: [item], job, stopRecursion: true };
332
+ }
333
+
334
+ // --- ASYNC JOB: IMG Tags ---
335
+ if (node.tagName === 'IMG') {
336
+ let radii = {
337
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
338
+ tr: parseFloat(style.borderTopRightRadius) || 0,
339
+ br: parseFloat(style.borderBottomRightRadius) || 0,
340
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
341
+ };
342
+
343
+ const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
344
+ if (!hasAnyRadius) {
345
+ const parent = node.parentElement;
346
+ const parentStyle = window.getComputedStyle(parent);
347
+ if (parentStyle.overflow !== 'visible') {
348
+ const pRadii = {
349
+ tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
350
+ tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
351
+ br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
352
+ bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
353
+ };
354
+ const pRect = parent.getBoundingClientRect();
355
+ if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
356
+ radii = pRadii;
357
+ }
358
+ }
359
+ }
360
+
361
+ const item = {
362
+ type: 'image',
363
+ zIndex,
364
+ domOrder,
365
+ options: { x, y, w, h, rotate: rotation, data: null },
366
+ };
367
+
368
+ const job = async () => {
369
+ const processed = await getProcessedImage(node.src, widthPx, heightPx, radii);
370
+ if (processed) item.options.data = processed;
371
+ else item.skip = true;
372
+ };
373
+
374
+ return { items: [item], job, stopRecursion: true };
375
+ }
376
+
377
+ // --- ASYNC JOB: Icons and Other Elements ---
378
+ if (
379
+ node.tagName.toUpperCase() === 'MATERIAL-ICON' ||
380
+ node.tagName.toUpperCase() === 'ICONIFY-ICON' ||
381
+ node.tagName.toUpperCase() === 'REMIX-ICON' ||
382
+ node.tagName.toUpperCase() === 'ION-ICON' ||
383
+ node.tagName.toUpperCase() === 'EVA-ICON' ||
384
+ node.tagName.toUpperCase() === 'BOX-ICON' ||
385
+ node.tagName.toUpperCase() === 'FA-ICON' ||
386
+ node.tagName.includes('-')
387
+ ) {
388
+ const item = {
389
+ type: 'image',
390
+ zIndex,
391
+ domOrder,
392
+ options: { x, y, w, h, rotate: rotation, data: null }, // Data null initially
393
+ };
394
+
395
+ // Create Job
396
+ const job = async () => {
397
+ const pngData = await elementToCanvasImage(node, widthPx, heightPx);
398
+ if (pngData) item.options.data = pngData;
399
+ else item.skip = true;
400
+ };
401
+
402
+ return { items: [item], job, stopRecursion: true };
403
+ }
404
+
405
+ // Radii logic
406
+ const borderRadiusValue = parseFloat(style.borderRadius) || 0;
407
+ const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
408
+ const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
409
+ const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
410
+ const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
411
+
412
+ const hasPartialBorderRadius =
413
+ (borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
414
+ (borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
415
+ (borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
416
+ (borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
417
+ (borderRadiusValue === 0 &&
418
+ (borderBottomLeftRadius ||
419
+ borderBottomRightRadius ||
420
+ borderTopLeftRadius ||
421
+ borderTopRightRadius));
422
+
423
+ // --- ASYNC JOB: Clipped Divs via Canvas ---
424
+ if (hasPartialBorderRadius && isClippedByParent(node)) {
425
+ const marginLeft = parseFloat(style.marginLeft) || 0;
426
+ const marginTop = parseFloat(style.marginTop) || 0;
427
+ x += marginLeft * PX_TO_INCH * config.scale;
428
+ y += marginTop * PX_TO_INCH * config.scale;
429
+
430
+ const item = {
431
+ type: 'image',
432
+ zIndex,
433
+ domOrder,
434
+ options: { x, y, w, h, rotate: rotation, data: null },
435
+ };
436
+
437
+ const job = async () => {
438
+ const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx);
439
+ if (canvasImageData) item.options.data = canvasImageData;
440
+ else item.skip = true;
441
+ };
442
+
443
+ return { items: [item], job, stopRecursion: true };
444
+ }
445
+
446
+ // --- SYNC: Standard CSS Extraction ---
447
+ const bgColorObj = parseColor(style.backgroundColor);
448
+ const bgClip = style.webkitBackgroundClip || style.backgroundClip;
449
+ const isBgClipText = bgClip === 'text';
450
+ const hasGradient =
451
+ !isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
452
+
453
+ const borderColorObj = parseColor(style.borderColor);
454
+ const borderWidth = parseFloat(style.borderWidth);
455
+ const hasBorder = borderWidth > 0 && borderColorObj.hex;
456
+
457
+ const borderInfo = getBorderInfo(style, config.scale);
458
+ const hasUniformBorder = borderInfo.type === 'uniform';
459
+ const hasCompositeBorder = borderInfo.type === 'composite';
460
+
461
+ const shadowStr = style.boxShadow;
462
+ const hasShadow = shadowStr && shadowStr !== 'none';
463
+ const softEdge = getSoftEdges(style.filter, config.scale);
464
+
465
+ let isImageWrapper = false;
466
+ const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
467
+ if (imgChild) {
468
+ const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
469
+ const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
470
+ if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
471
+ }
472
+
473
+ let textPayload = null;
474
+ const isText = isTextContainer(node);
475
+
476
+ if (isText) {
477
+ const textParts = [];
478
+ const isList = style.display === 'list-item';
479
+ if (isList) {
480
+ const fontSizePt = parseFloat(style.fontSize) * 0.75 * config.scale;
481
+ const bulletShift = (parseFloat(style.fontSize) || 16) * PX_TO_INCH * config.scale * 1.5;
482
+ x -= bulletShift;
483
+ w += bulletShift;
484
+ textParts.push({
485
+ text: ' ',
486
+ options: {
487
+ color: parseColor(style.color).hex || '000000',
488
+ fontSize: fontSizePt,
489
+ },
490
+ });
491
+ }
492
+
493
+ node.childNodes.forEach((child, index) => {
494
+ let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
495
+ let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
496
+ textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
497
+ if (index === 0 && !isList) textVal = textVal.trimStart();
498
+ else if (index === 0) textVal = textVal.trimStart();
499
+ if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
500
+ if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
501
+ if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
502
+
503
+ if (textVal.length > 0) {
504
+ textParts.push({
505
+ text: textVal,
506
+ options: getTextStyle(nodeStyle, config.scale),
507
+ });
508
+ }
509
+ });
510
+
511
+ if (textParts.length > 0) {
512
+ let align = style.textAlign || 'left';
513
+ if (align === 'start') align = 'left';
514
+ if (align === 'end') align = 'right';
515
+ let valign = 'top';
516
+ if (style.alignItems === 'center') valign = 'middle';
517
+ if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
518
+
519
+ const pt = parseFloat(style.paddingTop) || 0;
520
+ const pb = parseFloat(style.paddingBottom) || 0;
521
+ if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
522
+
523
+ let padding = getPadding(style, config.scale);
524
+ if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
525
+
526
+ textPayload = { text: textParts, align, valign, inset: padding };
527
+ }
528
+ }
529
+
530
+ if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
531
+ let bgData = null;
532
+ let padIn = 0;
533
+ if (softEdge) {
534
+ const svgInfo = generateBlurredSVG(
535
+ widthPx,
536
+ heightPx,
537
+ bgColorObj.hex,
538
+ borderRadiusValue,
539
+ softEdge
540
+ );
541
+ bgData = svgInfo.data;
542
+ padIn = svgInfo.padding * PX_TO_INCH * config.scale;
543
+ } else {
544
+ bgData = generateGradientSVG(
545
+ widthPx,
546
+ heightPx,
547
+ style.backgroundImage,
548
+ borderRadiusValue,
549
+ hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
550
+ );
551
+ }
552
+
553
+ if (bgData) {
554
+ items.push({
555
+ type: 'image',
556
+ zIndex,
557
+ domOrder,
558
+ options: {
559
+ data: bgData,
560
+ x: x - padIn,
561
+ y: y - padIn,
562
+ w: w + padIn * 2,
563
+ h: h + padIn * 2,
564
+ rotate: rotation,
565
+ },
566
+ });
567
+ }
568
+
569
+ if (textPayload) {
570
+ items.push({
571
+ type: 'text',
572
+ zIndex: zIndex + 1,
573
+ domOrder,
574
+ textParts: textPayload.text,
575
+ options: {
576
+ x,
577
+ y,
578
+ w,
579
+ h,
580
+ align: textPayload.align,
581
+ valign: textPayload.valign,
582
+ inset: textPayload.inset,
583
+ rotate: rotation,
584
+ margin: 0,
585
+ wrap: true,
586
+ autoFit: false,
587
+ },
588
+ });
589
+ }
590
+ if (hasCompositeBorder) {
591
+ const borderItems = createCompositeBorderItems(
592
+ borderInfo.sides,
593
+ x,
594
+ y,
595
+ w,
596
+ h,
597
+ config.scale,
598
+ zIndex,
599
+ domOrder
600
+ );
601
+ items.push(...borderItems);
602
+ }
603
+ } else if (
604
+ (bgColorObj.hex && !isImageWrapper) ||
605
+ hasUniformBorder ||
606
+ hasCompositeBorder ||
607
+ hasShadow ||
608
+ textPayload
609
+ ) {
610
+ const finalAlpha = safeOpacity * bgColorObj.opacity;
611
+ const transparency = (1 - finalAlpha) * 100;
612
+ const useSolidFill = bgColorObj.hex && !isImageWrapper;
613
+
614
+ if (hasPartialBorderRadius && useSolidFill && !textPayload) {
615
+ const shapeSvg = generateCustomShapeSVG(
616
+ widthPx,
617
+ heightPx,
618
+ bgColorObj.hex,
619
+ bgColorObj.opacity,
620
+ {
621
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
622
+ tr: parseFloat(style.borderTopRightRadius) || 0,
623
+ br: parseFloat(style.borderBottomRightRadius) || 0,
624
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
625
+ }
626
+ );
627
+
628
+ items.push({
629
+ type: 'image',
630
+ zIndex,
631
+ domOrder,
632
+ options: { data: shapeSvg, x, y, w, h, rotate: rotation },
633
+ });
634
+ } else {
635
+ const shapeOpts = {
636
+ x,
637
+ y,
638
+ w,
639
+ h,
640
+ rotate: rotation,
641
+ fill: useSolidFill
642
+ ? { color: bgColorObj.hex, transparency: transparency }
643
+ : { type: 'none' },
644
+ line: hasUniformBorder ? borderInfo.options : null,
645
+ };
646
+
647
+ if (hasShadow) shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
648
+
649
+ const borderRadius = parseFloat(style.borderRadius) || 0;
650
+ const aspectRatio = Math.max(widthPx, heightPx) / Math.min(widthPx, heightPx);
651
+ const isCircle = aspectRatio < 1.1 && borderRadius >= Math.min(widthPx, heightPx) / 2 - 1;
652
+
653
+ let shapeType = pptx.ShapeType.rect;
654
+ if (isCircle) shapeType = pptx.ShapeType.ellipse;
655
+ else if (borderRadius > 0) {
656
+ shapeType = pptx.ShapeType.roundRect;
657
+ shapeOpts.rectRadius = Math.min(0.5, borderRadius / Math.min(widthPx, heightPx));
658
+ }
659
+
660
+ if (textPayload) {
661
+ const textOptions = {
662
+ shape: shapeType,
663
+ ...shapeOpts,
664
+ align: textPayload.align,
665
+ valign: textPayload.valign,
666
+ inset: textPayload.inset,
667
+ margin: 0,
668
+ wrap: true,
669
+ autoFit: false,
670
+ };
671
+ items.push({
672
+ type: 'text',
673
+ zIndex,
674
+ domOrder,
675
+ textParts: textPayload.text,
676
+ options: textOptions,
677
+ });
678
+ } else if (!hasPartialBorderRadius) {
679
+ items.push({
680
+ type: 'shape',
681
+ zIndex,
682
+ domOrder,
683
+ shapeType,
684
+ options: shapeOpts,
685
+ });
686
+ }
687
+ }
688
+
689
+ if (hasCompositeBorder) {
690
+ const borderSvgData = generateCompositeBorderSVG(
691
+ widthPx,
692
+ heightPx,
693
+ borderRadiusValue,
694
+ borderInfo.sides
695
+ );
696
+ if (borderSvgData) {
697
+ items.push({
698
+ type: 'image',
699
+ zIndex: zIndex + 1,
700
+ domOrder,
701
+ options: { data: borderSvgData, x, y, w, h, rotate: rotation },
702
+ });
703
+ }
704
+ }
705
+ }
706
+
707
+ return { items, stopRecursion: !!textPayload };
708
+ }
709
+
710
+ function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
711
+ const items = [];
712
+ const pxToInch = 1 / 96;
713
+ const common = { zIndex: zIndex + 1, domOrder, shapeType: 'rect' };
714
+
715
+ if (sides.top.width > 0)
716
+ items.push({
717
+ ...common,
718
+ options: { x, y, w, h: sides.top.width * pxToInch * scale, fill: { color: sides.top.color } },
719
+ });
720
+ if (sides.right.width > 0)
721
+ items.push({
722
+ ...common,
723
+ options: {
724
+ x: x + w - sides.right.width * pxToInch * scale,
725
+ y,
726
+ w: sides.right.width * pxToInch * scale,
727
+ h,
728
+ fill: { color: sides.right.color },
729
+ },
730
+ });
731
+ if (sides.bottom.width > 0)
732
+ items.push({
733
+ ...common,
734
+ options: {
735
+ x,
736
+ y: y + h - sides.bottom.width * pxToInch * scale,
737
+ w,
738
+ h: sides.bottom.width * pxToInch * scale,
739
+ fill: { color: sides.bottom.color },
740
+ },
741
+ });
742
+ if (sides.left.width > 0)
743
+ items.push({
744
+ ...common,
745
+ options: {
746
+ x,
747
+ y,
748
+ w: sides.left.width * pxToInch * scale,
749
+ h,
750
+ fill: { color: sides.left.color },
751
+ },
752
+ });
753
+
754
+ return items;
755
+ }