dom-to-pptx 1.0.5 → 1.0.6

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.
@@ -4,7 +4,8 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.domToPptx = {}, global.PptxGenJS));
5
5
  })(this, (function (exports, PptxGenJSImport) { 'use strict';
6
6
 
7
- function _interopNamespaceDefault(e) {
7
+ function _interopNamespace(e) {
8
+ if (e && e.__esModule) return e;
8
9
  var n = Object.create(null);
9
10
  if (e) {
10
11
  Object.keys(e).forEach(function (k) {
@@ -17,11 +18,11 @@
17
18
  }
18
19
  });
19
20
  }
20
- n.default = e;
21
+ n["default"] = e;
21
22
  return Object.freeze(n);
22
23
  }
23
24
 
24
- var PptxGenJSImport__namespace = /*#__PURE__*/_interopNamespaceDefault(PptxGenJSImport);
25
+ var PptxGenJSImport__namespace = /*#__PURE__*/_interopNamespace(PptxGenJSImport);
25
26
 
26
27
  /*!
27
28
  * html2canvas 1.4.1 <https://html2canvas.hertzen.com>
@@ -76,7 +77,7 @@
76
77
  function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
77
78
  function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
78
79
  function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
79
- step((generator = generator.apply(thisArg, [])).next());
80
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
80
81
  });
81
82
  }
82
83
 
@@ -109,7 +110,7 @@
109
110
  }
110
111
 
111
112
  function __spreadArray(to, from, pack) {
112
- if (arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
113
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
113
114
  if (ar || !(i in from)) {
114
115
  if (!ar) ar = Array.prototype.slice.call(from, 0, i);
115
116
  ar[i] = from[i];
@@ -7843,1172 +7844,1202 @@
7843
7844
  : defaultBackgroundColor;
7844
7845
  };
7845
7846
 
7846
- // src/utils.js
7847
-
7848
- /**
7849
- * Checks if any parent element has overflow: hidden which would clip this element
7850
- * @param {HTMLElement} node - The DOM node to check
7851
- * @returns {boolean} - True if a parent has overflow-hidden or overflow-clip
7852
- */
7853
- function isClippedByParent(node) {
7854
- let parent = node.parentElement;
7855
- while (parent && parent !== document.body) {
7856
- const style = window.getComputedStyle(parent);
7857
- const overflow = style.overflow;
7858
- if (overflow === 'hidden' || overflow === 'clip') {
7859
- return true;
7860
- }
7861
- parent = parent.parentElement;
7862
- }
7863
- return false;
7864
- }
7865
-
7866
- // Helper to save gradient text
7867
- function getGradientFallbackColor(bgImage) {
7868
- if (!bgImage) return null;
7869
- const hexMatch = bgImage.match(/#(?:[0-9a-fA-F]{3}){1,2}/);
7870
- if (hexMatch) return hexMatch[0];
7871
- const rgbMatch = bgImage.match(/rgba?\(.*?\)/);
7872
- if (rgbMatch) return rgbMatch[0];
7873
- return null;
7874
- }
7875
-
7876
- function mapDashType(style) {
7877
- if (style === 'dashed') return 'dash';
7878
- if (style === 'dotted') return 'dot';
7879
- return 'solid';
7880
- }
7881
-
7882
- /**
7883
- * Analyzes computed border styles and determines the rendering strategy.
7884
- */
7885
- function getBorderInfo(style, scale) {
7886
- const top = {
7887
- width: parseFloat(style.borderTopWidth) || 0,
7888
- style: style.borderTopStyle,
7889
- color: parseColor(style.borderTopColor).hex,
7890
- };
7891
- const right = {
7892
- width: parseFloat(style.borderRightWidth) || 0,
7893
- style: style.borderRightStyle,
7894
- color: parseColor(style.borderRightColor).hex,
7895
- };
7896
- const bottom = {
7897
- width: parseFloat(style.borderBottomWidth) || 0,
7898
- style: style.borderBottomStyle,
7899
- color: parseColor(style.borderBottomColor).hex,
7900
- };
7901
- const left = {
7902
- width: parseFloat(style.borderLeftWidth) || 0,
7903
- style: style.borderLeftStyle,
7904
- color: parseColor(style.borderLeftColor).hex,
7905
- };
7906
-
7907
- const hasAnyBorder = top.width > 0 || right.width > 0 || bottom.width > 0 || left.width > 0;
7908
- if (!hasAnyBorder) return { type: 'none' };
7909
-
7910
- // Check if all sides are uniform
7911
- const isUniform =
7912
- top.width === right.width &&
7913
- top.width === bottom.width &&
7914
- top.width === left.width &&
7915
- top.style === right.style &&
7916
- top.style === bottom.style &&
7917
- top.style === left.style &&
7918
- top.color === right.color &&
7919
- top.color === bottom.color &&
7920
- top.color === left.color;
7921
-
7922
- if (isUniform) {
7923
- return {
7924
- type: 'uniform',
7925
- options: {
7926
- width: top.width * 0.75 * scale,
7927
- color: top.color,
7928
- transparency: (1 - parseColor(style.borderTopColor).opacity) * 100,
7929
- dashType: mapDashType(top.style),
7930
- },
7931
- };
7932
- } else {
7933
- return {
7934
- type: 'composite',
7935
- sides: { top, right, bottom, left },
7936
- };
7937
- }
7938
- }
7939
-
7940
- /**
7941
- * Generates an SVG image for composite borders that respects border-radius.
7942
- */
7943
- function generateCompositeBorderSVG(w, h, radius, sides) {
7944
- radius = radius / 2; // Adjust for SVG rendering
7945
- const clipId = 'clip_' + Math.random().toString(36).substr(2, 9);
7946
- let borderRects = '';
7947
-
7948
- if (sides.top.width > 0 && sides.top.color) {
7949
- borderRects += `<rect x="0" y="0" width="${w}" height="${sides.top.width}" fill="#${sides.top.color}" />`;
7950
- }
7951
- if (sides.right.width > 0 && sides.right.color) {
7952
- borderRects += `<rect x="${w - sides.right.width}" y="0" width="${sides.right.width}" height="${h}" fill="#${sides.right.color}" />`;
7953
- }
7954
- if (sides.bottom.width > 0 && sides.bottom.color) {
7955
- borderRects += `<rect x="0" y="${h - sides.bottom.width}" width="${w}" height="${sides.bottom.width}" fill="#${sides.bottom.color}" />`;
7956
- }
7957
- if (sides.left.width > 0 && sides.left.color) {
7958
- borderRects += `<rect x="0" y="0" width="${sides.left.width}" height="${h}" fill="#${sides.left.color}" />`;
7959
- }
7960
-
7961
- const svg = `
7962
- <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
7963
- <defs>
7964
- <clipPath id="${clipId}">
7965
- <rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" />
7966
- </clipPath>
7967
- </defs>
7968
- <g clip-path="url(#${clipId})">
7969
- ${borderRects}
7970
- </g>
7971
- </svg>`;
7972
-
7973
- return 'data:image/svg+xml;base64,' + btoa(svg);
7974
- }
7975
-
7976
- /**
7977
- * Generates an SVG data URL for a solid shape with non-uniform corner radii.
7978
- */
7979
- function generateCustomShapeSVG(w, h, color, opacity, radii) {
7980
- let { tl, tr, br, bl } = radii;
7981
-
7982
- // Clamp radii using CSS spec logic (avoid overlap)
7983
- const factor = Math.min(
7984
- (w / (tl + tr)) || Infinity,
7985
- (h / (tr + br)) || Infinity,
7986
- (w / (br + bl)) || Infinity,
7987
- (h / (bl + tl)) || Infinity
7988
- );
7989
-
7990
- if (factor < 1) {
7991
- tl *= factor; tr *= factor; br *= factor; bl *= factor;
7992
- }
7993
-
7994
- const path = `
7995
- M ${tl} 0
7996
- L ${w - tr} 0
7997
- A ${tr} ${tr} 0 0 1 ${w} ${tr}
7998
- L ${w} ${h - br}
7999
- A ${br} ${br} 0 0 1 ${w - br} ${h}
8000
- L ${bl} ${h}
8001
- A ${bl} ${bl} 0 0 1 0 ${h - bl}
8002
- L 0 ${tl}
8003
- A ${tl} ${tl} 0 0 1 ${tl} 0
8004
- Z
8005
- `;
8006
-
8007
- const svg = `
8008
- <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
8009
- <path d="${path}" fill="#${color}" fill-opacity="${opacity}" />
8010
- </svg>`;
8011
-
8012
- return 'data:image/svg+xml;base64,' + btoa(svg);
8013
- }
8014
-
8015
- function parseColor(str) {
8016
- if (!str || str === 'transparent' || str.startsWith('rgba(0, 0, 0, 0)')) {
8017
- return { hex: null, opacity: 0 };
8018
- }
8019
- if (str.startsWith('#')) {
8020
- let hex = str.slice(1);
8021
- if (hex.length === 3)
8022
- hex = hex.split('').map((c) => c + c).join('');
8023
- return { hex: hex.toUpperCase(), opacity: 1 };
8024
- }
8025
- const match = str.match(/[\d.]+/g);
8026
- if (match && match.length >= 3) {
8027
- const r = parseInt(match[0]);
8028
- const g = parseInt(match[1]);
8029
- const b = parseInt(match[2]);
8030
- const a = match.length > 3 ? parseFloat(match[3]) : 1;
8031
- const hex = ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
8032
- return { hex, opacity: a };
8033
- }
8034
- return { hex: null, opacity: 0 };
8035
- }
8036
-
8037
- function getPadding(style, scale) {
8038
- const pxToInch = 1 / 96;
8039
- return [
8040
- (parseFloat(style.paddingTop) || 0) * pxToInch * scale,
8041
- (parseFloat(style.paddingRight) || 0) * pxToInch * scale,
8042
- (parseFloat(style.paddingBottom) || 0) * pxToInch * scale,
8043
- (parseFloat(style.paddingLeft) || 0) * pxToInch * scale,
8044
- ];
8045
- }
8046
-
8047
- function getSoftEdges(filterStr, scale) {
8048
- if (!filterStr || filterStr === 'none') return null;
8049
- const match = filterStr.match(/blur\(([\d.]+)px\)/);
8050
- if (match) return parseFloat(match[1]) * 0.75 * scale;
8051
- return null;
8052
- }
8053
-
8054
- function getTextStyle(style, scale) {
8055
- let colorObj = parseColor(style.color);
8056
-
8057
- const bgClip = style.webkitBackgroundClip || style.backgroundClip;
8058
- if (colorObj.opacity === 0 && bgClip === 'text') {
8059
- const fallback = getGradientFallbackColor(style.backgroundImage);
8060
- if (fallback) colorObj = parseColor(fallback);
8061
- }
8062
-
8063
- return {
8064
- color: colorObj.hex || '000000',
8065
- fontFace: style.fontFamily.split(',')[0].replace(/['"]/g, ''),
8066
- fontSize: parseFloat(style.fontSize) * 0.75 * scale,
8067
- bold: parseInt(style.fontWeight) >= 600,
8068
- italic: style.fontStyle === 'italic',
8069
- underline: style.textDecoration.includes('underline'),
8070
- };
8071
- }
8072
-
8073
- /**
8074
- * Determines if a given DOM node is primarily a text container.
8075
- */
8076
- function isTextContainer(node) {
8077
- const hasText = node.textContent.trim().length > 0;
8078
- if (!hasText) return false;
8079
-
8080
- const children = Array.from(node.children);
8081
- if (children.length === 0) return true;
8082
-
8083
- // Check if children are purely inline text formatting or visual shapes
8084
- const isSafeInline = (el) => {
8085
- const style = window.getComputedStyle(el);
8086
- const display = style.display;
8087
-
8088
- // If it's a standard inline element
8089
- const isInlineTag = ['SPAN', 'B', 'STRONG', 'EM', 'I', 'A', 'SMALL'].includes(el.tagName);
8090
- const isInlineDisplay = display.includes('inline');
8091
-
8092
- if (!isInlineTag && !isInlineDisplay) return false;
8093
-
8094
- // Check if element is a shape (visual object without text)
8095
- // If an element is empty but has a visible background/border, it's a shape (like a dot).
8096
- // We must return false so the parent isn't treated as a text-only container.
8097
- const hasContent = el.textContent.trim().length > 0;
8098
- const bgColor = parseColor(style.backgroundColor);
8099
- const hasVisibleBg = bgColor.hex && bgColor.opacity > 0;
8100
- const hasBorder = parseFloat(style.borderWidth) > 0 && parseColor(style.borderColor).opacity > 0;
8101
-
8102
- if (!hasContent && (hasVisibleBg || hasBorder)) {
8103
- return false;
8104
- }
8105
-
8106
- return true;
8107
- };
8108
-
8109
- return children.every(isSafeInline);
8110
- }
8111
-
8112
- function getRotation(transformStr) {
8113
- if (!transformStr || transformStr === 'none') return 0;
8114
- const values = transformStr.split('(')[1].split(')')[0].split(',');
8115
- if (values.length < 4) return 0;
8116
- const a = parseFloat(values[0]);
8117
- const b = parseFloat(values[1]);
8118
- return Math.round(Math.atan2(b, a) * (180 / Math.PI));
8119
- }
8120
-
8121
- function svgToPng(node) {
8122
- return new Promise((resolve) => {
8123
- const clone = node.cloneNode(true);
8124
- const rect = node.getBoundingClientRect();
8125
- const width = rect.width || 300;
8126
- const height = rect.height || 150;
8127
-
8128
- function inlineStyles(source, target) {
8129
- const computed = window.getComputedStyle(source);
8130
- const properties = [
8131
- 'fill', 'stroke', 'stroke-width', 'stroke-linecap',
8132
- 'stroke-linejoin', 'opacity', 'font-family', 'font-size', 'font-weight',
8133
- ];
8134
-
8135
- if (computed.fill === 'none') target.setAttribute('fill', 'none');
8136
- else if (computed.fill) target.style.fill = computed.fill;
8137
-
8138
- if (computed.stroke === 'none') target.setAttribute('stroke', 'none');
8139
- else if (computed.stroke) target.style.stroke = computed.stroke;
8140
-
8141
- properties.forEach((prop) => {
8142
- if (prop !== 'fill' && prop !== 'stroke') {
8143
- const val = computed[prop];
8144
- if (val && val !== 'auto') target.style[prop] = val;
8145
- }
8146
- });
8147
-
8148
- for (let i = 0; i < source.children.length; i++) {
8149
- if (target.children[i]) inlineStyles(source.children[i], target.children[i]);
8150
- }
8151
- }
8152
-
8153
- inlineStyles(node, clone);
8154
- clone.setAttribute('width', width);
8155
- clone.setAttribute('height', height);
8156
- clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
8157
-
8158
- const xml = new XMLSerializer().serializeToString(clone);
8159
- const svgUrl = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xml)}`;
8160
- const img = new Image();
8161
- img.crossOrigin = 'Anonymous';
8162
- img.onload = () => {
8163
- const canvas = document.createElement('canvas');
8164
- const scale = 3;
8165
- canvas.width = width * scale;
8166
- canvas.height = height * scale;
8167
- const ctx = canvas.getContext('2d');
8168
- ctx.scale(scale, scale);
8169
- ctx.drawImage(img, 0, 0, width, height);
8170
- resolve(canvas.toDataURL('image/png'));
8171
- };
8172
- img.onerror = () => resolve(null);
8173
- img.src = svgUrl;
8174
- });
8175
- }
8176
-
8177
- function getVisibleShadow(shadowStr, scale) {
8178
- if (!shadowStr || shadowStr === 'none') return null;
8179
- const shadows = shadowStr.split(/,(?![^()]*\))/);
8180
- for (let s of shadows) {
8181
- s = s.trim();
8182
- if (s.startsWith('rgba(0, 0, 0, 0)')) continue;
8183
- const match = s.match(
8184
- /(rgba?\([^)]+\)|#[0-9a-fA-F]+)\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px/
8185
- );
8186
- if (match) {
8187
- const colorStr = match[1];
8188
- const x = parseFloat(match[2]);
8189
- const y = parseFloat(match[3]);
8190
- const blur = parseFloat(match[4]);
8191
- const distance = Math.sqrt(x * x + y * y);
8192
- let angle = Math.atan2(y, x) * (180 / Math.PI);
8193
- if (angle < 0) angle += 360;
8194
- const colorObj = parseColor(colorStr);
8195
- return {
8196
- type: 'outer',
8197
- angle: angle,
8198
- blur: blur * 0.75 * scale,
8199
- offset: distance * 0.75 * scale,
8200
- color: colorObj.hex || '000000',
8201
- opacity: colorObj.opacity,
8202
- };
8203
- }
8204
- }
8205
- return null;
8206
- }
8207
-
8208
- function generateGradientSVG(w, h, bgString, radius, border) {
8209
- try {
8210
- const match = bgString.match(/linear-gradient\((.*)\)/);
8211
- if (!match) return null;
8212
- const content = match[1];
8213
- const parts = content.split(/,(?![^()]*\))/).map((p) => p.trim());
8214
-
8215
- let x1 = '0%', y1 = '0%', x2 = '0%', y2 = '100%';
8216
- let stopsStartIdx = 0;
8217
- if (parts[0].includes('to right')) {
8218
- x1 = '0%'; x2 = '100%'; y2 = '0%'; stopsStartIdx = 1;
8219
- } else if (parts[0].includes('to left')) {
8220
- x1 = '100%'; x2 = '0%'; y2 = '0%'; stopsStartIdx = 1;
8221
- } else if (parts[0].includes('to top')) {
8222
- y1 = '100%'; y2 = '0%'; stopsStartIdx = 1;
8223
- } else if (parts[0].includes('to bottom')) {
8224
- y1 = '0%'; y2 = '100%'; stopsStartIdx = 1;
8225
- }
8226
-
8227
- let stopsXML = '';
8228
- const stopParts = parts.slice(stopsStartIdx);
8229
- stopParts.forEach((part, idx) => {
8230
- let color = part;
8231
- let offset = Math.round((idx / (stopParts.length - 1)) * 100) + '%';
8232
- const posMatch = part.match(/(.*?)\s+(\d+(\.\d+)?%?)$/);
8233
- if (posMatch) {
8234
- color = posMatch[1];
8235
- offset = posMatch[2];
8236
- }
8237
- let opacity = 1;
8238
- if (color.includes('rgba')) {
8239
- const rgba = color.match(/[\d.]+/g);
8240
- if (rgba && rgba.length > 3) {
8241
- opacity = rgba[3];
8242
- color = `rgb(${rgba[0]},${rgba[1]},${rgba[2]})`;
8243
- }
8244
- }
8245
- stopsXML += `<stop offset="${offset}" stop-color="${color}" stop-opacity="${opacity}"/>`;
8246
- });
8247
-
8248
- let strokeAttr = '';
8249
- if (border) {
8250
- strokeAttr = `stroke="#${border.color}" stroke-width="${border.width}"`;
8251
- }
8252
-
8253
- const svg = `
8254
- <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
8255
- <defs><linearGradient id="grad" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">${stopsXML}</linearGradient></defs>
8256
- <rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="url(#grad)" ${strokeAttr} />
8257
- </svg>`;
8258
- return 'data:image/svg+xml;base64,' + btoa(svg);
8259
- } catch {
8260
- return null;
8261
- }
8262
- }
8263
-
8264
- function generateBlurredSVG(w, h, color, radius, blurPx) {
8265
- const padding = blurPx * 3;
8266
- const fullW = w + padding * 2;
8267
- const fullH = h + padding * 2;
8268
- const x = padding;
8269
- const y = padding;
8270
- let shapeTag = '';
8271
- const isCircle = radius >= Math.min(w, h) / 2 - 1 && Math.abs(w - h) < 2;
8272
-
8273
- if (isCircle) {
8274
- const cx = x + w / 2;
8275
- const cy = y + h / 2;
8276
- const rx = w / 2;
8277
- const ry = h / 2;
8278
- shapeTag = `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" fill="#${color}" filter="url(#f1)" />`;
8279
- } else {
8280
- shapeTag = `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="#${color}" filter="url(#f1)" />`;
8281
- }
8282
-
8283
- const svg = `
8284
- <svg xmlns="http://www.w3.org/2000/svg" width="${fullW}" height="${fullH}" viewBox="0 0 ${fullW} ${fullH}">
8285
- <defs>
8286
- <filter id="f1" x="-50%" y="-50%" width="200%" height="200%">
8287
- <feGaussianBlur in="SourceGraphic" stdDeviation="${blurPx}" />
8288
- </filter>
8289
- </defs>
8290
- ${shapeTag}
8291
- </svg>`;
8292
-
8293
- return {
8294
- data: 'data:image/svg+xml;base64,' + btoa(svg),
8295
- padding: padding,
8296
- };
7847
+ // src/utils.js
7848
+
7849
+ /**
7850
+ * Checks if any parent element has overflow: hidden which would clip this element
7851
+ * @param {HTMLElement} node - The DOM node to check
7852
+ * @returns {boolean} - True if a parent has overflow-hidden or overflow-clip
7853
+ */
7854
+ function isClippedByParent(node) {
7855
+ let parent = node.parentElement;
7856
+ while (parent && parent !== document.body) {
7857
+ const style = window.getComputedStyle(parent);
7858
+ const overflow = style.overflow;
7859
+ if (overflow === 'hidden' || overflow === 'clip') {
7860
+ return true;
7861
+ }
7862
+ parent = parent.parentElement;
7863
+ }
7864
+ return false;
8297
7865
  }
8298
7866
 
8299
- // src/image-processor.js
8300
-
8301
- async function getProcessedImage(src, targetW, targetH, radius) {
8302
- return new Promise((resolve) => {
8303
- const img = new Image();
8304
- img.crossOrigin = 'Anonymous'; // Critical for canvas manipulation
8305
-
8306
- img.onload = () => {
8307
- const canvas = document.createElement('canvas');
8308
- // Double resolution for better quality
8309
- const scale = 2;
8310
- canvas.width = targetW * scale;
8311
- canvas.height = targetH * scale;
8312
- const ctx = canvas.getContext('2d');
8313
- ctx.scale(scale, scale);
8314
-
8315
- // Normalize radius input to an object { tl, tr, br, bl }
8316
- let r = { tl: 0, tr: 0, br: 0, bl: 0 };
8317
- if (typeof radius === 'number') {
8318
- r = { tl: radius, tr: radius, br: radius, bl: radius };
8319
- } else if (typeof radius === 'object' && radius !== null) {
8320
- r = { ...r, ...radius }; // Merge with defaults
8321
- }
8322
-
8323
- // 1. Draw the Mask (Custom Shape with specific corners)
8324
- ctx.beginPath();
8325
-
8326
- // Border Radius Clamping Logic (CSS Spec)
8327
- // Prevents corners from overlapping if radii are too large for the container
8328
- const factor = Math.min(
8329
- (targetW / (r.tl + r.tr)) || Infinity,
8330
- (targetH / (r.tr + r.br)) || Infinity,
8331
- (targetW / (r.br + r.bl)) || Infinity,
8332
- (targetH / (r.bl + r.tl)) || Infinity
8333
- );
8334
-
8335
- if (factor < 1) {
8336
- r.tl *= factor; r.tr *= factor; r.br *= factor; r.bl *= factor;
8337
- }
8338
-
8339
- // Draw path: Top-Left -> Top-Right -> Bottom-Right -> Bottom-Left
8340
- ctx.moveTo(r.tl, 0);
8341
- ctx.lineTo(targetW - r.tr, 0);
8342
- ctx.arcTo(targetW, 0, targetW, r.tr, r.tr);
8343
- ctx.lineTo(targetW, targetH - r.br);
8344
- ctx.arcTo(targetW, targetH, targetW - r.br, targetH, r.br);
8345
- ctx.lineTo(r.bl, targetH);
8346
- ctx.arcTo(0, targetH, 0, targetH - r.bl, r.bl);
8347
- ctx.lineTo(0, r.tl);
8348
- ctx.arcTo(0, 0, r.tl, 0, r.tl);
8349
-
8350
- ctx.closePath();
8351
- ctx.fillStyle = '#000';
8352
- ctx.fill();
8353
-
8354
- // 2. Composite Source-In (Crops the next image draw to the mask)
8355
- ctx.globalCompositeOperation = 'source-in';
8356
-
8357
- // 3. Draw Image (Object Cover Logic)
8358
- const wRatio = targetW / img.width;
8359
- const hRatio = targetH / img.height;
8360
- const maxRatio = Math.max(wRatio, hRatio);
8361
- const renderW = img.width * maxRatio;
8362
- const renderH = img.height * maxRatio;
8363
- const renderX = (targetW - renderW) / 2;
8364
- const renderY = (targetH - renderH) / 2;
8365
-
8366
- ctx.drawImage(img, renderX, renderY, renderW, renderH);
8367
-
8368
- resolve(canvas.toDataURL('image/png'));
8369
- };
8370
-
8371
- img.onerror = () => resolve(null);
8372
- img.src = src;
8373
- });
7867
+ // Helper to save gradient text
7868
+ function getGradientFallbackColor(bgImage) {
7869
+ if (!bgImage) return null;
7870
+ const hexMatch = bgImage.match(/#(?:[0-9a-fA-F]{3}){1,2}/);
7871
+ if (hexMatch) return hexMatch[0];
7872
+ const rgbMatch = bgImage.match(/rgba?\(.*?\)/);
7873
+ if (rgbMatch) return rgbMatch[0];
7874
+ return null;
8374
7875
  }
8375
7876
 
8376
- // src/index.js
8377
-
8378
- // Normalize import
8379
- const PptxGenJS = PptxGenJSImport__namespace?.default ?? PptxGenJSImport__namespace;
8380
-
8381
- const PPI = 96;
8382
- const PX_TO_INCH = 1 / PPI;
8383
-
8384
- /**
8385
- * Main export function. Accepts single element or an array.
8386
- * @param {HTMLElement | string | Array<HTMLElement | string>} target - The root element(s) to convert.
8387
- * @param {Object} options - { fileName: string }
8388
- */
8389
- async function exportToPptx(target, options = {}) {
8390
- const resolvePptxConstructor = (pkg) => {
8391
- if (!pkg) return null;
8392
- if (typeof pkg === 'function') return pkg;
8393
- if (pkg && typeof pkg.default === 'function') return pkg.default;
8394
- if (pkg && typeof pkg.PptxGenJS === 'function') return pkg.PptxGenJS;
8395
- if (pkg && pkg.PptxGenJS && typeof pkg.PptxGenJS.default === 'function')
8396
- return pkg.PptxGenJS.default;
8397
- return null;
8398
- };
8399
-
8400
- const PptxConstructor = resolvePptxConstructor(PptxGenJS);
8401
- if (!PptxConstructor) throw new Error('PptxGenJS constructor not found.');
8402
- const pptx = new PptxConstructor();
8403
- pptx.layout = 'LAYOUT_16x9';
8404
-
8405
- const elements = Array.isArray(target) ? target : [target];
8406
-
8407
- for (const el of elements) {
8408
- const root = typeof el === 'string' ? document.querySelector(el) : el;
8409
- if (!root) {
8410
- console.warn('Element not found, skipping slide:', el);
8411
- continue;
8412
- }
8413
- const slide = pptx.addSlide();
8414
- await processSlide(root, slide, pptx);
8415
- }
8416
-
8417
- const fileName = options.fileName || 'export.pptx';
8418
- pptx.writeFile({ fileName });
8419
- }
8420
-
8421
- /**
8422
- * Worker function to process a single DOM element into a single PPTX slide.
8423
- * @param {HTMLElement} root - The root element for this slide.
8424
- * @param {PptxGenJS.Slide} slide - The PPTX slide object to add content to.
8425
- * @param {PptxGenJS} pptx - The main PPTX instance.
8426
- */
8427
- async function processSlide(root, slide, pptx) {
8428
- const rootRect = root.getBoundingClientRect();
8429
- const PPTX_WIDTH_IN = 10;
8430
- const PPTX_HEIGHT_IN = 5.625;
8431
-
8432
- const contentWidthIn = rootRect.width * PX_TO_INCH;
8433
- const contentHeightIn = rootRect.height * PX_TO_INCH;
8434
- const scale = Math.min(PPTX_WIDTH_IN / contentWidthIn, PPTX_HEIGHT_IN / contentHeightIn);
8435
-
8436
- const layoutConfig = {
8437
- rootX: rootRect.x,
8438
- rootY: rootRect.y,
8439
- scale: scale,
8440
- offX: (PPTX_WIDTH_IN - contentWidthIn * scale) / 2,
8441
- offY: (PPTX_HEIGHT_IN - contentHeightIn * scale) / 2,
8442
- };
8443
-
8444
- const renderQueue = [];
8445
- let domOrderCounter = 0;
8446
-
8447
- async function collect(node) {
8448
- const order = domOrderCounter++;
8449
- const result = await createRenderItem(node, { ...layoutConfig, root }, order, pptx);
8450
- if (result) {
8451
- if (result.items) renderQueue.push(...result.items);
8452
- if (result.stopRecursion) return;
8453
- }
8454
- for (const child of node.children) await collect(child);
8455
- }
8456
-
8457
- await collect(root);
8458
-
8459
- renderQueue.sort((a, b) => {
8460
- if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
8461
- return a.domOrder - b.domOrder;
8462
- });
8463
-
8464
- for (const item of renderQueue) {
8465
- if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
8466
- if (item.type === 'image') slide.addImage(item.options);
8467
- if (item.type === 'text') slide.addText(item.textParts, item.options);
8468
- }
8469
- }
8470
-
8471
- async function elementToCanvasImage(node, widthPx, heightPx, root) {
8472
- return new Promise((resolve) => {
8473
- const width = Math.ceil(widthPx);
8474
- const height = Math.ceil(heightPx);
8475
-
8476
- if (width <= 0 || height <= 0) {
8477
- resolve(null);
8478
- return;
8479
- }
8480
-
8481
- const style = window.getComputedStyle(node);
8482
-
8483
- html2canvas(root, {
8484
- width: root.scrollWidth,
8485
- height: root.scrollHeight,
8486
- useCORS: true,
8487
- allowTaint: true,
8488
- backgroundColor: null,
8489
- })
8490
- .then((canvas) => {
8491
- const rootCanvas = canvas;
8492
- const nodeRect = node.getBoundingClientRect();
8493
- const rootRect = root.getBoundingClientRect();
8494
- const sourceX = nodeRect.left - rootRect.left;
8495
- const sourceY = nodeRect.top - rootRect.top;
8496
-
8497
- const destCanvas = document.createElement('canvas');
8498
- destCanvas.width = width;
8499
- destCanvas.height = height;
8500
- const ctx = destCanvas.getContext('2d');
8501
-
8502
- ctx.drawImage(rootCanvas, sourceX, sourceY, width, height, 0, 0, width, height);
8503
-
8504
- // Parse radii
8505
- let tl = parseFloat(style.borderTopLeftRadius) || 0;
8506
- let tr = parseFloat(style.borderTopRightRadius) || 0;
8507
- let br = parseFloat(style.borderBottomRightRadius) || 0;
8508
- let bl = parseFloat(style.borderBottomLeftRadius) || 0;
8509
-
8510
- const f = Math.min(
8511
- width / (tl + tr) || Infinity,
8512
- height / (tr + br) || Infinity,
8513
- width / (br + bl) || Infinity,
8514
- height / (bl + tl) || Infinity
8515
- );
8516
-
8517
- if (f < 1) {
8518
- tl *= f;
8519
- tr *= f;
8520
- br *= f;
8521
- bl *= f;
8522
- }
8523
-
8524
- ctx.globalCompositeOperation = 'destination-in';
8525
- ctx.beginPath();
8526
- ctx.moveTo(tl, 0);
8527
- ctx.lineTo(width - tr, 0);
8528
- ctx.arcTo(width, 0, width, tr, tr);
8529
- ctx.lineTo(width, height - br);
8530
- ctx.arcTo(width, height, width - br, height, br);
8531
- ctx.lineTo(bl, height);
8532
- ctx.arcTo(0, height, 0, height - bl, bl);
8533
- ctx.lineTo(0, tl);
8534
- ctx.arcTo(0, 0, tl, 0, tl);
8535
- ctx.closePath();
8536
- ctx.fill();
8537
-
8538
- resolve(destCanvas.toDataURL('image/png'));
8539
- })
8540
- .catch(() => resolve(null));
8541
- });
8542
- }
8543
-
8544
- async function createRenderItem(node, config, domOrder, pptx) {
8545
- if (node.nodeType !== 1) return null;
8546
- const style = window.getComputedStyle(node);
8547
- if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')
8548
- return null;
8549
-
8550
- const rect = node.getBoundingClientRect();
8551
- if (rect.width < 0.5 || rect.height < 0.5) return null;
8552
-
8553
- const zIndex = style.zIndex !== 'auto' ? parseInt(style.zIndex) : 0;
8554
- const rotation = getRotation(style.transform);
8555
- const elementOpacity = parseFloat(style.opacity);
8556
-
8557
- const widthPx = node.offsetWidth || rect.width;
8558
- const heightPx = node.offsetHeight || rect.height;
8559
- const unrotatedW = widthPx * PX_TO_INCH * config.scale;
8560
- const unrotatedH = heightPx * PX_TO_INCH * config.scale;
8561
- const centerX = rect.left + rect.width / 2;
8562
- const centerY = rect.top + rect.height / 2;
8563
-
8564
- let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
8565
- let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
8566
- let w = unrotatedW;
8567
- let h = unrotatedH;
8568
-
8569
- const items = [];
8570
-
8571
- if (node.nodeName.toUpperCase() === 'SVG') {
8572
- const pngData = await svgToPng(node);
8573
- if (pngData)
8574
- items.push({
8575
- type: 'image',
8576
- zIndex,
8577
- domOrder,
8578
- options: { data: pngData, x, y, w, h, rotate: rotation },
8579
- });
8580
- return { items, stopRecursion: true };
8581
- }
8582
-
8583
- // --- UPDATED IMG BLOCK START ---
8584
- if (node.tagName === 'IMG') {
8585
- // Extract individual corner radii
8586
- let radii = {
8587
- tl: parseFloat(style.borderTopLeftRadius) || 0,
8588
- tr: parseFloat(style.borderTopRightRadius) || 0,
8589
- br: parseFloat(style.borderBottomRightRadius) || 0,
8590
- bl: parseFloat(style.borderBottomLeftRadius) || 0,
8591
- };
8592
-
8593
- const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
8594
-
8595
- // Fallback: Check parent if image has no specific radius but parent clips it
8596
- if (!hasAnyRadius) {
8597
- const parent = node.parentElement;
8598
- const parentStyle = window.getComputedStyle(parent);
8599
- if (parentStyle.overflow !== 'visible') {
8600
- const pRadii = {
8601
- tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
8602
- tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
8603
- br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
8604
- bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
8605
- };
8606
- // Simple heuristic: If image takes up full size of parent, inherit radii.
8607
- // For complex grids (like slide-1), this blindly applies parent radius.
8608
- // In a perfect world, we'd calculate intersection, but for now we apply parent radius
8609
- // if the image is close to the parent's size, effectively masking it.
8610
- const pRect = parent.getBoundingClientRect();
8611
- if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
8612
- radii = pRadii;
8613
- }
8614
- }
8615
- }
8616
-
8617
- const processed = await getProcessedImage(node.src, widthPx, heightPx, radii);
8618
- if (processed)
8619
- items.push({
8620
- type: 'image',
8621
- zIndex,
8622
- domOrder,
8623
- options: { data: processed, x, y, w, h, rotate: rotation },
8624
- });
8625
- return { items, stopRecursion: true };
8626
- }
8627
- // --- UPDATED IMG BLOCK END ---
8628
-
8629
- // Radii processing for Divs/Shapes
8630
- const borderRadiusValue = parseFloat(style.borderRadius) || 0;
8631
- const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
8632
- const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
8633
- const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
8634
- const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
8635
-
8636
- const hasPartialBorderRadius =
8637
- (borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
8638
- (borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
8639
- (borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
8640
- (borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
8641
- (borderRadiusValue === 0 &&
8642
- (borderBottomLeftRadius ||
8643
- borderBottomRightRadius ||
8644
- borderTopLeftRadius ||
8645
- borderTopRightRadius));
8646
-
8647
- // Allow clipped elements to be rendered via canvas
8648
- if (hasPartialBorderRadius && isClippedByParent(node)) {
8649
- const marginLeft = parseFloat(style.marginLeft) || 0;
8650
- const marginTop = parseFloat(style.marginTop) || 0;
8651
- x += marginLeft * PX_TO_INCH * config.scale;
8652
- y += marginTop * PX_TO_INCH * config.scale;
8653
-
8654
- const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx, config.root);
8655
- if (canvasImageData) {
8656
- items.push({
8657
- type: 'image',
8658
- zIndex,
8659
- domOrder,
8660
- options: { data: canvasImageData, x, y, w, h, rotate: rotation },
8661
- });
8662
- return { items, stopRecursion: true };
8663
- }
8664
- }
8665
-
8666
- const bgColorObj = parseColor(style.backgroundColor);
8667
- const bgClip = style.webkitBackgroundClip || style.backgroundClip;
8668
- const isBgClipText = bgClip === 'text';
8669
- const hasGradient =
8670
- !isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
8671
-
8672
- const borderColorObj = parseColor(style.borderColor);
8673
- const borderWidth = parseFloat(style.borderWidth);
8674
- const hasBorder = borderWidth > 0 && borderColorObj.hex;
8675
-
8676
- const borderInfo = getBorderInfo(style, config.scale);
8677
- const hasUniformBorder = borderInfo.type === 'uniform';
8678
- const hasCompositeBorder = borderInfo.type === 'composite';
8679
-
8680
- const shadowStr = style.boxShadow;
8681
- const hasShadow = shadowStr && shadowStr !== 'none';
8682
- const softEdge = getSoftEdges(style.filter, config.scale);
8683
-
8684
- let isImageWrapper = false;
8685
- const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
8686
- if (imgChild) {
8687
- const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
8688
- const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
8689
- if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
8690
- }
8691
-
8692
- let textPayload = null;
8693
- const isText = isTextContainer(node);
8694
-
8695
- if (isText) {
8696
- const textParts = [];
8697
- const isList = style.display === 'list-item';
8698
- if (isList) {
8699
- const fontSizePt = parseFloat(style.fontSize) * 0.75 * config.scale;
8700
- const bulletShift = (parseFloat(style.fontSize) || 16) * PX_TO_INCH * config.scale * 1.5;
8701
- x -= bulletShift;
8702
- w += bulletShift;
8703
- textParts.push({
8704
- text: '• ',
8705
- options: {
8706
- color: parseColor(style.color).hex || '000000',
8707
- fontSize: fontSizePt,
8708
- },
8709
- });
8710
- }
8711
-
8712
- node.childNodes.forEach((child, index) => {
8713
- let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
8714
- let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
8715
- textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
8716
- if (index === 0 && !isList) textVal = textVal.trimStart();
8717
- else if (index === 0) textVal = textVal.trimStart();
8718
- if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
8719
- if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
8720
- if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
8721
-
8722
- if (textVal.length > 0) {
8723
- textParts.push({
8724
- text: textVal,
8725
- options: getTextStyle(nodeStyle, config.scale),
8726
- });
8727
- }
8728
- });
8729
-
8730
- if (textParts.length > 0) {
8731
- let align = style.textAlign || 'left';
8732
- if (align === 'start') align = 'left';
8733
- if (align === 'end') align = 'right';
8734
- let valign = 'top';
8735
- if (style.alignItems === 'center') valign = 'middle';
8736
- if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
8737
-
8738
- const pt = parseFloat(style.paddingTop) || 0;
8739
- const pb = parseFloat(style.paddingBottom) || 0;
8740
- if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
8741
-
8742
- let padding = getPadding(style, config.scale);
8743
- if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
8744
-
8745
- textPayload = { text: textParts, align, valign, inset: padding };
8746
- }
8747
- }
8748
-
8749
- if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
8750
- let bgData = null;
8751
- let padIn = 0;
8752
- if (softEdge) {
8753
- const svgInfo = generateBlurredSVG(
8754
- widthPx,
8755
- heightPx,
8756
- bgColorObj.hex,
8757
- borderRadiusValue,
8758
- softEdge
8759
- );
8760
- bgData = svgInfo.data;
8761
- padIn = svgInfo.padding * PX_TO_INCH * config.scale;
8762
- } else {
8763
- bgData = generateGradientSVG(
8764
- widthPx,
8765
- heightPx,
8766
- style.backgroundImage,
8767
- borderRadiusValue,
8768
- hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
8769
- );
8770
- }
8771
-
8772
- if (bgData) {
8773
- items.push({
8774
- type: 'image',
8775
- zIndex,
8776
- domOrder,
8777
- options: {
8778
- data: bgData,
8779
- x: x - padIn,
8780
- y: y - padIn,
8781
- w: w + padIn * 2,
8782
- h: h + padIn * 2,
8783
- rotate: rotation,
8784
- },
8785
- });
8786
- }
8787
-
8788
- if (textPayload) {
8789
- items.push({
8790
- type: 'text',
8791
- zIndex: zIndex + 1,
8792
- domOrder,
8793
- textParts: textPayload.text,
8794
- options: {
8795
- x,
8796
- y,
8797
- w,
8798
- h,
8799
- align: textPayload.align,
8800
- valign: textPayload.valign,
8801
- inset: textPayload.inset,
8802
- rotate: rotation,
8803
- margin: 0,
8804
- wrap: true,
8805
- autoFit: false,
8806
- },
8807
- });
8808
- }
8809
- if (hasCompositeBorder) {
8810
- // Add border shapes after the main background
8811
- const borderItems = createCompositeBorderItems(
8812
- borderInfo.sides,
8813
- x,
8814
- y,
8815
- w,
8816
- h,
8817
- config.scale,
8818
- zIndex,
8819
- domOrder
8820
- );
8821
- items.push(...borderItems);
8822
- }
8823
- } else if (
8824
- (bgColorObj.hex && !isImageWrapper) ||
8825
- hasUniformBorder ||
8826
- hasCompositeBorder ||
8827
- hasShadow ||
8828
- textPayload
8829
- ) {
8830
- const finalAlpha = elementOpacity * bgColorObj.opacity;
8831
- const transparency = (1 - finalAlpha) * 100;
8832
- const useSolidFill = bgColorObj.hex && !isImageWrapper;
8833
-
8834
- if (hasPartialBorderRadius && useSolidFill && !textPayload) {
8835
- const shapeSvg = generateCustomShapeSVG(
8836
- widthPx,
8837
- heightPx,
8838
- bgColorObj.hex,
8839
- bgColorObj.opacity,
8840
- {
8841
- tl: parseFloat(style.borderTopLeftRadius) || 0,
8842
- tr: parseFloat(style.borderTopRightRadius) || 0,
8843
- br: parseFloat(style.borderBottomRightRadius) || 0,
8844
- bl: parseFloat(style.borderBottomLeftRadius) || 0,
8845
- }
8846
- );
8847
-
8848
- items.push({
8849
- type: 'image',
8850
- zIndex,
8851
- domOrder,
8852
- options: {
8853
- data: shapeSvg,
8854
- x,
8855
- y,
8856
- w,
8857
- h,
8858
- rotate: rotation,
8859
- },
8860
- });
8861
- } else {
8862
- const shapeOpts = {
8863
- x,
8864
- y,
8865
- w,
8866
- h,
8867
- rotate: rotation,
8868
- fill: useSolidFill
8869
- ? { color: bgColorObj.hex, transparency: transparency }
8870
- : { type: 'none' },
8871
- line: hasUniformBorder ? borderInfo.options : null,
8872
- };
8873
-
8874
- if (hasShadow) {
8875
- shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
8876
- }
8877
-
8878
- const borderRadius = parseFloat(style.borderRadius) || 0;
8879
- const aspectRatio = Math.max(widthPx, heightPx) / Math.min(widthPx, heightPx);
8880
- const isCircle = aspectRatio < 1.1 && borderRadius >= Math.min(widthPx, heightPx) / 2 - 1;
8881
-
8882
- let shapeType = pptx.ShapeType.rect;
8883
- if (isCircle) shapeType = pptx.ShapeType.ellipse;
8884
- else if (borderRadius > 0) {
8885
- shapeType = pptx.ShapeType.roundRect;
8886
- shapeOpts.rectRadius = Math.min(0.5, borderRadius / Math.min(widthPx, heightPx));
8887
- }
8888
-
8889
- if (textPayload) {
8890
- const textOptions = {
8891
- shape: shapeType,
8892
- ...shapeOpts,
8893
- align: textPayload.align,
8894
- valign: textPayload.valign,
8895
- inset: textPayload.inset,
8896
- margin: 0,
8897
- wrap: true,
8898
- autoFit: false,
8899
- };
8900
- items.push({
8901
- type: 'text',
8902
- zIndex,
8903
- domOrder,
8904
- textParts: textPayload.text,
8905
- options: textOptions,
8906
- });
8907
- } else if (!hasPartialBorderRadius) {
8908
- items.push({
8909
- type: 'shape',
8910
- zIndex,
8911
- domOrder,
8912
- shapeType,
8913
- options: shapeOpts,
8914
- });
8915
- }
8916
- }
8917
-
8918
- if (hasCompositeBorder) {
8919
- const borderSvgData = generateCompositeBorderSVG(
8920
- widthPx,
8921
- heightPx,
8922
- borderRadiusValue,
8923
- borderInfo.sides
8924
- );
8925
- if (borderSvgData) {
8926
- items.push({
8927
- type: 'image',
8928
- zIndex: zIndex + 1,
8929
- domOrder,
8930
- options: { data: borderSvgData, x, y, w, h, rotate: rotation },
8931
- });
8932
- }
8933
- }
8934
- }
8935
-
8936
- return { items, stopRecursion: !!textPayload };
8937
- }
8938
-
8939
- /**
8940
- * Helper function to create individual border shapes
8941
- */
8942
- function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
8943
- const items = [];
8944
- const pxToInch = 1 / 96;
8945
-
8946
- // TOP BORDER
8947
- if (sides.top.width > 0) {
8948
- items.push({
8949
- type: 'shape',
8950
- zIndex: zIndex + 1,
8951
- domOrder,
8952
- shapeType: 'rect',
8953
- options: {
8954
- x: x,
8955
- y: y,
8956
- w: w,
8957
- h: sides.top.width * pxToInch * scale,
8958
- fill: { color: sides.top.color },
8959
- },
8960
- });
8961
- }
8962
- // RIGHT BORDER
8963
- if (sides.right.width > 0) {
8964
- items.push({
8965
- type: 'shape',
8966
- zIndex: zIndex + 1,
8967
- domOrder,
8968
- shapeType: 'rect',
8969
- options: {
8970
- x: x + w - sides.right.width * pxToInch * scale,
8971
- y: y,
8972
- w: sides.right.width * pxToInch * scale,
8973
- h: h,
8974
- fill: { color: sides.right.color },
8975
- },
8976
- });
8977
- }
8978
- // BOTTOM BORDER
8979
- if (sides.bottom.width > 0) {
8980
- items.push({
8981
- type: 'shape',
8982
- zIndex: zIndex + 1,
8983
- domOrder,
8984
- shapeType: 'rect',
8985
- options: {
8986
- x: x,
8987
- y: y + h - sides.bottom.width * pxToInch * scale,
8988
- w: w,
8989
- h: sides.bottom.width * pxToInch * scale,
8990
- fill: { color: sides.bottom.color },
8991
- },
8992
- });
8993
- }
8994
- // LEFT BORDER
8995
- if (sides.left.width > 0) {
8996
- items.push({
8997
- type: 'shape',
8998
- zIndex: zIndex + 1,
8999
- domOrder,
9000
- shapeType: 'rect',
9001
- options: {
9002
- x: x,
9003
- y: y,
9004
- w: sides.left.width * pxToInch * scale,
9005
- h: h,
9006
- fill: { color: sides.left.color },
9007
- },
9008
- });
9009
- }
9010
-
9011
- return items;
7877
+ function mapDashType(style) {
7878
+ if (style === 'dashed') return 'dash';
7879
+ if (style === 'dotted') return 'dot';
7880
+ return 'solid';
7881
+ }
7882
+
7883
+ /**
7884
+ * Analyzes computed border styles and determines the rendering strategy.
7885
+ */
7886
+ function getBorderInfo(style, scale) {
7887
+ const top = {
7888
+ width: parseFloat(style.borderTopWidth) || 0,
7889
+ style: style.borderTopStyle,
7890
+ color: parseColor(style.borderTopColor).hex,
7891
+ };
7892
+ const right = {
7893
+ width: parseFloat(style.borderRightWidth) || 0,
7894
+ style: style.borderRightStyle,
7895
+ color: parseColor(style.borderRightColor).hex,
7896
+ };
7897
+ const bottom = {
7898
+ width: parseFloat(style.borderBottomWidth) || 0,
7899
+ style: style.borderBottomStyle,
7900
+ color: parseColor(style.borderBottomColor).hex,
7901
+ };
7902
+ const left = {
7903
+ width: parseFloat(style.borderLeftWidth) || 0,
7904
+ style: style.borderLeftStyle,
7905
+ color: parseColor(style.borderLeftColor).hex,
7906
+ };
7907
+
7908
+ const hasAnyBorder = top.width > 0 || right.width > 0 || bottom.width > 0 || left.width > 0;
7909
+ if (!hasAnyBorder) return { type: 'none' };
7910
+
7911
+ // Check if all sides are uniform
7912
+ const isUniform =
7913
+ top.width === right.width &&
7914
+ top.width === bottom.width &&
7915
+ top.width === left.width &&
7916
+ top.style === right.style &&
7917
+ top.style === bottom.style &&
7918
+ top.style === left.style &&
7919
+ top.color === right.color &&
7920
+ top.color === bottom.color &&
7921
+ top.color === left.color;
7922
+
7923
+ if (isUniform) {
7924
+ return {
7925
+ type: 'uniform',
7926
+ options: {
7927
+ width: top.width * 0.75 * scale,
7928
+ color: top.color,
7929
+ transparency: (1 - parseColor(style.borderTopColor).opacity) * 100,
7930
+ dashType: mapDashType(top.style),
7931
+ },
7932
+ };
7933
+ } else {
7934
+ return {
7935
+ type: 'composite',
7936
+ sides: { top, right, bottom, left },
7937
+ };
7938
+ }
7939
+ }
7940
+
7941
+ /**
7942
+ * Generates an SVG image for composite borders that respects border-radius.
7943
+ */
7944
+ function generateCompositeBorderSVG(w, h, radius, sides) {
7945
+ radius = radius / 2; // Adjust for SVG rendering
7946
+ const clipId = 'clip_' + Math.random().toString(36).substr(2, 9);
7947
+ let borderRects = '';
7948
+
7949
+ if (sides.top.width > 0 && sides.top.color) {
7950
+ borderRects += `<rect x="0" y="0" width="${w}" height="${sides.top.width}" fill="#${sides.top.color}" />`;
7951
+ }
7952
+ if (sides.right.width > 0 && sides.right.color) {
7953
+ borderRects += `<rect x="${w - sides.right.width}" y="0" width="${sides.right.width}" height="${h}" fill="#${sides.right.color}" />`;
7954
+ }
7955
+ if (sides.bottom.width > 0 && sides.bottom.color) {
7956
+ borderRects += `<rect x="0" y="${h - sides.bottom.width}" width="${w}" height="${sides.bottom.width}" fill="#${sides.bottom.color}" />`;
7957
+ }
7958
+ if (sides.left.width > 0 && sides.left.color) {
7959
+ borderRects += `<rect x="0" y="0" width="${sides.left.width}" height="${h}" fill="#${sides.left.color}" />`;
7960
+ }
7961
+
7962
+ const svg = `
7963
+ <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
7964
+ <defs>
7965
+ <clipPath id="${clipId}">
7966
+ <rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" />
7967
+ </clipPath>
7968
+ </defs>
7969
+ <g clip-path="url(#${clipId})">
7970
+ ${borderRects}
7971
+ </g>
7972
+ </svg>`;
7973
+
7974
+ return 'data:image/svg+xml;base64,' + btoa(svg);
7975
+ }
7976
+
7977
+ /**
7978
+ * Generates an SVG data URL for a solid shape with non-uniform corner radii.
7979
+ */
7980
+ function generateCustomShapeSVG(w, h, color, opacity, radii) {
7981
+ let { tl, tr, br, bl } = radii;
7982
+
7983
+ // Clamp radii using CSS spec logic (avoid overlap)
7984
+ const factor = Math.min(
7985
+ w / (tl + tr) || Infinity,
7986
+ h / (tr + br) || Infinity,
7987
+ w / (br + bl) || Infinity,
7988
+ h / (bl + tl) || Infinity
7989
+ );
7990
+
7991
+ if (factor < 1) {
7992
+ tl *= factor;
7993
+ tr *= factor;
7994
+ br *= factor;
7995
+ bl *= factor;
7996
+ }
7997
+
7998
+ const path = `
7999
+ M ${tl} 0
8000
+ L ${w - tr} 0
8001
+ A ${tr} ${tr} 0 0 1 ${w} ${tr}
8002
+ L ${w} ${h - br}
8003
+ A ${br} ${br} 0 0 1 ${w - br} ${h}
8004
+ L ${bl} ${h}
8005
+ A ${bl} ${bl} 0 0 1 0 ${h - bl}
8006
+ L 0 ${tl}
8007
+ A ${tl} ${tl} 0 0 1 ${tl} 0
8008
+ Z
8009
+ `;
8010
+
8011
+ const svg = `
8012
+ <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
8013
+ <path d="${path}" fill="#${color}" fill-opacity="${opacity}" />
8014
+ </svg>`;
8015
+
8016
+ return 'data:image/svg+xml;base64,' + btoa(svg);
8017
+ }
8018
+
8019
+ function parseColor(str) {
8020
+ if (!str || str === 'transparent' || str.startsWith('rgba(0, 0, 0, 0)')) {
8021
+ return { hex: null, opacity: 0 };
8022
+ }
8023
+ if (str.startsWith('#')) {
8024
+ let hex = str.slice(1);
8025
+ if (hex.length === 3)
8026
+ hex = hex
8027
+ .split('')
8028
+ .map((c) => c + c)
8029
+ .join('');
8030
+ return { hex: hex.toUpperCase(), opacity: 1 };
8031
+ }
8032
+ const match = str.match(/[\d.]+/g);
8033
+ if (match && match.length >= 3) {
8034
+ const r = parseInt(match[0]);
8035
+ const g = parseInt(match[1]);
8036
+ const b = parseInt(match[2]);
8037
+ const a = match.length > 3 ? parseFloat(match[3]) : 1;
8038
+ const hex = ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1).toUpperCase();
8039
+ return { hex, opacity: a };
8040
+ }
8041
+ return { hex: null, opacity: 0 };
8042
+ }
8043
+
8044
+ function getPadding(style, scale) {
8045
+ const pxToInch = 1 / 96;
8046
+ return [
8047
+ (parseFloat(style.paddingTop) || 0) * pxToInch * scale,
8048
+ (parseFloat(style.paddingRight) || 0) * pxToInch * scale,
8049
+ (parseFloat(style.paddingBottom) || 0) * pxToInch * scale,
8050
+ (parseFloat(style.paddingLeft) || 0) * pxToInch * scale,
8051
+ ];
8052
+ }
8053
+
8054
+ function getSoftEdges(filterStr, scale) {
8055
+ if (!filterStr || filterStr === 'none') return null;
8056
+ const match = filterStr.match(/blur\(([\d.]+)px\)/);
8057
+ if (match) return parseFloat(match[1]) * 0.75 * scale;
8058
+ return null;
8059
+ }
8060
+
8061
+ function getTextStyle(style, scale) {
8062
+ let colorObj = parseColor(style.color);
8063
+
8064
+ const bgClip = style.webkitBackgroundClip || style.backgroundClip;
8065
+ if (colorObj.opacity === 0 && bgClip === 'text') {
8066
+ const fallback = getGradientFallbackColor(style.backgroundImage);
8067
+ if (fallback) colorObj = parseColor(fallback);
8068
+ }
8069
+
8070
+ return {
8071
+ color: colorObj.hex || '000000',
8072
+ fontFace: style.fontFamily.split(',')[0].replace(/['"]/g, ''),
8073
+ fontSize: parseFloat(style.fontSize) * 0.75 * scale,
8074
+ bold: parseInt(style.fontWeight) >= 600,
8075
+ italic: style.fontStyle === 'italic',
8076
+ underline: style.textDecoration.includes('underline'),
8077
+ };
8078
+ }
8079
+
8080
+ /**
8081
+ * Determines if a given DOM node is primarily a text container.
8082
+ */
8083
+ function isTextContainer(node) {
8084
+ const hasText = node.textContent.trim().length > 0;
8085
+ if (!hasText) return false;
8086
+
8087
+ const children = Array.from(node.children);
8088
+ if (children.length === 0) return true;
8089
+
8090
+ // Check if children are purely inline text formatting or visual shapes
8091
+ const isSafeInline = (el) => {
8092
+ const style = window.getComputedStyle(el);
8093
+ const display = style.display;
8094
+
8095
+ // If it's a standard inline element
8096
+ const isInlineTag = ['SPAN', 'B', 'STRONG', 'EM', 'I', 'A', 'SMALL'].includes(el.tagName);
8097
+ const isInlineDisplay = display.includes('inline');
8098
+
8099
+ if (!isInlineTag && !isInlineDisplay) return false;
8100
+
8101
+ // Check if element is a shape (visual object without text)
8102
+ // If an element is empty but has a visible background/border, it's a shape (like a dot).
8103
+ // We must return false so the parent isn't treated as a text-only container.
8104
+ const hasContent = el.textContent.trim().length > 0;
8105
+ const bgColor = parseColor(style.backgroundColor);
8106
+ const hasVisibleBg = bgColor.hex && bgColor.opacity > 0;
8107
+ const hasBorder =
8108
+ parseFloat(style.borderWidth) > 0 && parseColor(style.borderColor).opacity > 0;
8109
+
8110
+ if (!hasContent && (hasVisibleBg || hasBorder)) {
8111
+ return false;
8112
+ }
8113
+
8114
+ return true;
8115
+ };
8116
+
8117
+ return children.every(isSafeInline);
8118
+ }
8119
+
8120
+ function getRotation(transformStr) {
8121
+ if (!transformStr || transformStr === 'none') return 0;
8122
+ const values = transformStr.split('(')[1].split(')')[0].split(',');
8123
+ if (values.length < 4) return 0;
8124
+ const a = parseFloat(values[0]);
8125
+ const b = parseFloat(values[1]);
8126
+ return Math.round(Math.atan2(b, a) * (180 / Math.PI));
8127
+ }
8128
+
8129
+ function svgToPng(node) {
8130
+ return new Promise((resolve) => {
8131
+ const clone = node.cloneNode(true);
8132
+ const rect = node.getBoundingClientRect();
8133
+ const width = rect.width || 300;
8134
+ const height = rect.height || 150;
8135
+
8136
+ function inlineStyles(source, target) {
8137
+ const computed = window.getComputedStyle(source);
8138
+ const properties = [
8139
+ 'fill',
8140
+ 'stroke',
8141
+ 'stroke-width',
8142
+ 'stroke-linecap',
8143
+ 'stroke-linejoin',
8144
+ 'opacity',
8145
+ 'font-family',
8146
+ 'font-size',
8147
+ 'font-weight',
8148
+ ];
8149
+
8150
+ if (computed.fill === 'none') target.setAttribute('fill', 'none');
8151
+ else if (computed.fill) target.style.fill = computed.fill;
8152
+
8153
+ if (computed.stroke === 'none') target.setAttribute('stroke', 'none');
8154
+ else if (computed.stroke) target.style.stroke = computed.stroke;
8155
+
8156
+ properties.forEach((prop) => {
8157
+ if (prop !== 'fill' && prop !== 'stroke') {
8158
+ const val = computed[prop];
8159
+ if (val && val !== 'auto') target.style[prop] = val;
8160
+ }
8161
+ });
8162
+
8163
+ for (let i = 0; i < source.children.length; i++) {
8164
+ if (target.children[i]) inlineStyles(source.children[i], target.children[i]);
8165
+ }
8166
+ }
8167
+
8168
+ inlineStyles(node, clone);
8169
+ clone.setAttribute('width', width);
8170
+ clone.setAttribute('height', height);
8171
+ clone.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
8172
+
8173
+ const xml = new XMLSerializer().serializeToString(clone);
8174
+ const svgUrl = `data:image/svg+xml;charset=utf-8,${encodeURIComponent(xml)}`;
8175
+ const img = new Image();
8176
+ img.crossOrigin = 'Anonymous';
8177
+ img.onload = () => {
8178
+ const canvas = document.createElement('canvas');
8179
+ const scale = 3;
8180
+ canvas.width = width * scale;
8181
+ canvas.height = height * scale;
8182
+ const ctx = canvas.getContext('2d');
8183
+ ctx.scale(scale, scale);
8184
+ ctx.drawImage(img, 0, 0, width, height);
8185
+ resolve(canvas.toDataURL('image/png'));
8186
+ };
8187
+ img.onerror = () => resolve(null);
8188
+ img.src = svgUrl;
8189
+ });
8190
+ }
8191
+
8192
+ function getVisibleShadow(shadowStr, scale) {
8193
+ if (!shadowStr || shadowStr === 'none') return null;
8194
+ const shadows = shadowStr.split(/,(?![^()]*\))/);
8195
+ for (let s of shadows) {
8196
+ s = s.trim();
8197
+ if (s.startsWith('rgba(0, 0, 0, 0)')) continue;
8198
+ const match = s.match(
8199
+ /(rgba?\([^)]+\)|#[0-9a-fA-F]+)\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px/
8200
+ );
8201
+ if (match) {
8202
+ const colorStr = match[1];
8203
+ const x = parseFloat(match[2]);
8204
+ const y = parseFloat(match[3]);
8205
+ const blur = parseFloat(match[4]);
8206
+ const distance = Math.sqrt(x * x + y * y);
8207
+ let angle = Math.atan2(y, x) * (180 / Math.PI);
8208
+ if (angle < 0) angle += 360;
8209
+ const colorObj = parseColor(colorStr);
8210
+ return {
8211
+ type: 'outer',
8212
+ angle: angle,
8213
+ blur: blur * 0.75 * scale,
8214
+ offset: distance * 0.75 * scale,
8215
+ color: colorObj.hex || '000000',
8216
+ opacity: colorObj.opacity,
8217
+ };
8218
+ }
8219
+ }
8220
+ return null;
8221
+ }
8222
+
8223
+ function generateGradientSVG(w, h, bgString, radius, border) {
8224
+ try {
8225
+ const match = bgString.match(/linear-gradient\((.*)\)/);
8226
+ if (!match) return null;
8227
+ const content = match[1];
8228
+ const parts = content.split(/,(?![^()]*\))/).map((p) => p.trim());
8229
+
8230
+ let x1 = '0%',
8231
+ y1 = '0%',
8232
+ x2 = '0%',
8233
+ y2 = '100%';
8234
+ let stopsStartIdx = 0;
8235
+ if (parts[0].includes('to right')) {
8236
+ x1 = '0%';
8237
+ x2 = '100%';
8238
+ y2 = '0%';
8239
+ stopsStartIdx = 1;
8240
+ } else if (parts[0].includes('to left')) {
8241
+ x1 = '100%';
8242
+ x2 = '0%';
8243
+ y2 = '0%';
8244
+ stopsStartIdx = 1;
8245
+ } else if (parts[0].includes('to top')) {
8246
+ y1 = '100%';
8247
+ y2 = '0%';
8248
+ stopsStartIdx = 1;
8249
+ } else if (parts[0].includes('to bottom')) {
8250
+ y1 = '0%';
8251
+ y2 = '100%';
8252
+ stopsStartIdx = 1;
8253
+ }
8254
+
8255
+ let stopsXML = '';
8256
+ const stopParts = parts.slice(stopsStartIdx);
8257
+ stopParts.forEach((part, idx) => {
8258
+ let color = part;
8259
+ let offset = Math.round((idx / (stopParts.length - 1)) * 100) + '%';
8260
+ const posMatch = part.match(/(.*?)\s+(\d+(\.\d+)?%?)$/);
8261
+ if (posMatch) {
8262
+ color = posMatch[1];
8263
+ offset = posMatch[2];
8264
+ }
8265
+ let opacity = 1;
8266
+ if (color.includes('rgba')) {
8267
+ const rgba = color.match(/[\d.]+/g);
8268
+ if (rgba && rgba.length > 3) {
8269
+ opacity = rgba[3];
8270
+ color = `rgb(${rgba[0]},${rgba[1]},${rgba[2]})`;
8271
+ }
8272
+ }
8273
+ stopsXML += `<stop offset="${offset}" stop-color="${color}" stop-opacity="${opacity}"/>`;
8274
+ });
8275
+
8276
+ let strokeAttr = '';
8277
+ if (border) {
8278
+ strokeAttr = `stroke="#${border.color}" stroke-width="${border.width}"`;
8279
+ }
8280
+
8281
+ const svg = `
8282
+ <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
8283
+ <defs><linearGradient id="grad" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">${stopsXML}</linearGradient></defs>
8284
+ <rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="url(#grad)" ${strokeAttr} />
8285
+ </svg>`;
8286
+ return 'data:image/svg+xml;base64,' + btoa(svg);
8287
+ } catch {
8288
+ return null;
8289
+ }
8290
+ }
8291
+
8292
+ function generateBlurredSVG(w, h, color, radius, blurPx) {
8293
+ const padding = blurPx * 3;
8294
+ const fullW = w + padding * 2;
8295
+ const fullH = h + padding * 2;
8296
+ const x = padding;
8297
+ const y = padding;
8298
+ let shapeTag = '';
8299
+ const isCircle = radius >= Math.min(w, h) / 2 - 1 && Math.abs(w - h) < 2;
8300
+
8301
+ if (isCircle) {
8302
+ const cx = x + w / 2;
8303
+ const cy = y + h / 2;
8304
+ const rx = w / 2;
8305
+ const ry = h / 2;
8306
+ shapeTag = `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" fill="#${color}" filter="url(#f1)" />`;
8307
+ } else {
8308
+ shapeTag = `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="#${color}" filter="url(#f1)" />`;
8309
+ }
8310
+
8311
+ const svg = `
8312
+ <svg xmlns="http://www.w3.org/2000/svg" width="${fullW}" height="${fullH}" viewBox="0 0 ${fullW} ${fullH}">
8313
+ <defs>
8314
+ <filter id="f1" x="-50%" y="-50%" width="200%" height="200%">
8315
+ <feGaussianBlur in="SourceGraphic" stdDeviation="${blurPx}" />
8316
+ </filter>
8317
+ </defs>
8318
+ ${shapeTag}
8319
+ </svg>`;
8320
+
8321
+ return {
8322
+ data: 'data:image/svg+xml;base64,' + btoa(svg),
8323
+ padding: padding,
8324
+ };
8325
+ }
8326
+
8327
+ // src/image-processor.js
8328
+
8329
+ async function getProcessedImage(src, targetW, targetH, radius) {
8330
+ return new Promise((resolve) => {
8331
+ const img = new Image();
8332
+ img.crossOrigin = 'Anonymous'; // Critical for canvas manipulation
8333
+
8334
+ img.onload = () => {
8335
+ const canvas = document.createElement('canvas');
8336
+ // Double resolution for better quality
8337
+ const scale = 2;
8338
+ canvas.width = targetW * scale;
8339
+ canvas.height = targetH * scale;
8340
+ const ctx = canvas.getContext('2d');
8341
+ ctx.scale(scale, scale);
8342
+
8343
+ // Normalize radius input to an object { tl, tr, br, bl }
8344
+ let r = { tl: 0, tr: 0, br: 0, bl: 0 };
8345
+ if (typeof radius === 'number') {
8346
+ r = { tl: radius, tr: radius, br: radius, bl: radius };
8347
+ } else if (typeof radius === 'object' && radius !== null) {
8348
+ r = { ...r, ...radius }; // Merge with defaults
8349
+ }
8350
+
8351
+ // 1. Draw the Mask (Custom Shape with specific corners)
8352
+ ctx.beginPath();
8353
+
8354
+ // Border Radius Clamping Logic (CSS Spec)
8355
+ // Prevents corners from overlapping if radii are too large for the container
8356
+ const factor = Math.min(
8357
+ targetW / (r.tl + r.tr) || Infinity,
8358
+ targetH / (r.tr + r.br) || Infinity,
8359
+ targetW / (r.br + r.bl) || Infinity,
8360
+ targetH / (r.bl + r.tl) || Infinity
8361
+ );
8362
+
8363
+ if (factor < 1) {
8364
+ r.tl *= factor;
8365
+ r.tr *= factor;
8366
+ r.br *= factor;
8367
+ r.bl *= factor;
8368
+ }
8369
+
8370
+ // Draw path: Top-Left -> Top-Right -> Bottom-Right -> Bottom-Left
8371
+ ctx.moveTo(r.tl, 0);
8372
+ ctx.lineTo(targetW - r.tr, 0);
8373
+ ctx.arcTo(targetW, 0, targetW, r.tr, r.tr);
8374
+ ctx.lineTo(targetW, targetH - r.br);
8375
+ ctx.arcTo(targetW, targetH, targetW - r.br, targetH, r.br);
8376
+ ctx.lineTo(r.bl, targetH);
8377
+ ctx.arcTo(0, targetH, 0, targetH - r.bl, r.bl);
8378
+ ctx.lineTo(0, r.tl);
8379
+ ctx.arcTo(0, 0, r.tl, 0, r.tl);
8380
+
8381
+ ctx.closePath();
8382
+ ctx.fillStyle = '#000';
8383
+ ctx.fill();
8384
+
8385
+ // 2. Composite Source-In (Crops the next image draw to the mask)
8386
+ ctx.globalCompositeOperation = 'source-in';
8387
+
8388
+ // 3. Draw Image (Object Cover Logic)
8389
+ const wRatio = targetW / img.width;
8390
+ const hRatio = targetH / img.height;
8391
+ const maxRatio = Math.max(wRatio, hRatio);
8392
+ const renderW = img.width * maxRatio;
8393
+ const renderH = img.height * maxRatio;
8394
+ const renderX = (targetW - renderW) / 2;
8395
+ const renderY = (targetH - renderH) / 2;
8396
+
8397
+ ctx.drawImage(img, renderX, renderY, renderW, renderH);
8398
+
8399
+ resolve(canvas.toDataURL('image/png'));
8400
+ };
8401
+
8402
+ img.onerror = () => resolve(null);
8403
+ img.src = src;
8404
+ });
8405
+ }
8406
+
8407
+ // src/index.js
8408
+
8409
+ // Normalize import
8410
+ const PptxGenJS = PptxGenJSImport__namespace?.default ?? PptxGenJSImport__namespace;
8411
+
8412
+ const PPI = 96;
8413
+ const PX_TO_INCH = 1 / PPI;
8414
+
8415
+ /**
8416
+ * Main export function. Accepts single element or an array.
8417
+ * @param {HTMLElement | string | Array<HTMLElement | string>} target - The root element(s) to convert.
8418
+ * @param {Object} options - { fileName: string }
8419
+ */
8420
+ async function exportToPptx(target, options = {}) {
8421
+ const resolvePptxConstructor = (pkg) => {
8422
+ if (!pkg) return null;
8423
+ if (typeof pkg === 'function') return pkg;
8424
+ if (pkg && typeof pkg.default === 'function') return pkg.default;
8425
+ if (pkg && typeof pkg.PptxGenJS === 'function') return pkg.PptxGenJS;
8426
+ if (pkg && pkg.PptxGenJS && typeof pkg.PptxGenJS.default === 'function')
8427
+ return pkg.PptxGenJS.default;
8428
+ return null;
8429
+ };
8430
+
8431
+ const PptxConstructor = resolvePptxConstructor(PptxGenJS);
8432
+ if (!PptxConstructor) throw new Error('PptxGenJS constructor not found.');
8433
+ const pptx = new PptxConstructor();
8434
+ pptx.layout = 'LAYOUT_16x9';
8435
+
8436
+ const elements = Array.isArray(target) ? target : [target];
8437
+
8438
+ for (const el of elements) {
8439
+ const root = typeof el === 'string' ? document.querySelector(el) : el;
8440
+ if (!root) {
8441
+ console.warn('Element not found, skipping slide:', el);
8442
+ continue;
8443
+ }
8444
+ const slide = pptx.addSlide();
8445
+ await processSlide(root, slide, pptx);
8446
+ }
8447
+
8448
+ const fileName = options.fileName || 'export.pptx';
8449
+ pptx.writeFile({ fileName });
8450
+ }
8451
+
8452
+ /**
8453
+ * Worker function to process a single DOM element into a single PPTX slide.
8454
+ * @param {HTMLElement} root - The root element for this slide.
8455
+ * @param {PptxGenJS.Slide} slide - The PPTX slide object to add content to.
8456
+ * @param {PptxGenJS} pptx - The main PPTX instance.
8457
+ */
8458
+ async function processSlide(root, slide, pptx) {
8459
+ const rootRect = root.getBoundingClientRect();
8460
+ const PPTX_WIDTH_IN = 10;
8461
+ const PPTX_HEIGHT_IN = 5.625;
8462
+
8463
+ const contentWidthIn = rootRect.width * PX_TO_INCH;
8464
+ const contentHeightIn = rootRect.height * PX_TO_INCH;
8465
+ const scale = Math.min(PPTX_WIDTH_IN / contentWidthIn, PPTX_HEIGHT_IN / contentHeightIn);
8466
+
8467
+ const layoutConfig = {
8468
+ rootX: rootRect.x,
8469
+ rootY: rootRect.y,
8470
+ scale: scale,
8471
+ offX: (PPTX_WIDTH_IN - contentWidthIn * scale) / 2,
8472
+ offY: (PPTX_HEIGHT_IN - contentHeightIn * scale) / 2,
8473
+ };
8474
+
8475
+ const renderQueue = [];
8476
+ let domOrderCounter = 0;
8477
+
8478
+ async function collect(node) {
8479
+ const order = domOrderCounter++;
8480
+ const result = await createRenderItem(node, { ...layoutConfig, root }, order, pptx);
8481
+ if (result) {
8482
+ if (result.items) renderQueue.push(...result.items);
8483
+ if (result.stopRecursion) return;
8484
+ }
8485
+ for (const child of node.children) await collect(child);
8486
+ }
8487
+
8488
+ await collect(root);
8489
+
8490
+ renderQueue.sort((a, b) => {
8491
+ if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
8492
+ return a.domOrder - b.domOrder;
8493
+ });
8494
+
8495
+ for (const item of renderQueue) {
8496
+ if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
8497
+ if (item.type === 'image') slide.addImage(item.options);
8498
+ if (item.type === 'text') slide.addText(item.textParts, item.options);
8499
+ }
8500
+ }
8501
+
8502
+ async function elementToCanvasImage(node, widthPx, heightPx, root) {
8503
+ return new Promise((resolve) => {
8504
+ const width = Math.ceil(widthPx);
8505
+ const height = Math.ceil(heightPx);
8506
+
8507
+ if (width <= 0 || height <= 0) {
8508
+ resolve(null);
8509
+ return;
8510
+ }
8511
+
8512
+ const style = window.getComputedStyle(node);
8513
+
8514
+ html2canvas(root, {
8515
+ width: root.scrollWidth,
8516
+ height: root.scrollHeight,
8517
+ useCORS: true,
8518
+ allowTaint: true,
8519
+ backgroundColor: null,
8520
+ })
8521
+ .then((canvas) => {
8522
+ const rootCanvas = canvas;
8523
+ const nodeRect = node.getBoundingClientRect();
8524
+ const rootRect = root.getBoundingClientRect();
8525
+ const sourceX = nodeRect.left - rootRect.left;
8526
+ const sourceY = nodeRect.top - rootRect.top;
8527
+
8528
+ const destCanvas = document.createElement('canvas');
8529
+ destCanvas.width = width;
8530
+ destCanvas.height = height;
8531
+ const ctx = destCanvas.getContext('2d');
8532
+
8533
+ ctx.drawImage(rootCanvas, sourceX, sourceY, width, height, 0, 0, width, height);
8534
+
8535
+ // Parse radii
8536
+ let tl = parseFloat(style.borderTopLeftRadius) || 0;
8537
+ let tr = parseFloat(style.borderTopRightRadius) || 0;
8538
+ let br = parseFloat(style.borderBottomRightRadius) || 0;
8539
+ let bl = parseFloat(style.borderBottomLeftRadius) || 0;
8540
+
8541
+ const f = Math.min(
8542
+ width / (tl + tr) || Infinity,
8543
+ height / (tr + br) || Infinity,
8544
+ width / (br + bl) || Infinity,
8545
+ height / (bl + tl) || Infinity
8546
+ );
8547
+
8548
+ if (f < 1) {
8549
+ tl *= f;
8550
+ tr *= f;
8551
+ br *= f;
8552
+ bl *= f;
8553
+ }
8554
+
8555
+ ctx.globalCompositeOperation = 'destination-in';
8556
+ ctx.beginPath();
8557
+ ctx.moveTo(tl, 0);
8558
+ ctx.lineTo(width - tr, 0);
8559
+ ctx.arcTo(width, 0, width, tr, tr);
8560
+ ctx.lineTo(width, height - br);
8561
+ ctx.arcTo(width, height, width - br, height, br);
8562
+ ctx.lineTo(bl, height);
8563
+ ctx.arcTo(0, height, 0, height - bl, bl);
8564
+ ctx.lineTo(0, tl);
8565
+ ctx.arcTo(0, 0, tl, 0, tl);
8566
+ ctx.closePath();
8567
+ ctx.fill();
8568
+
8569
+ resolve(destCanvas.toDataURL('image/png'));
8570
+ })
8571
+ .catch(() => resolve(null));
8572
+ });
8573
+ }
8574
+
8575
+ async function createRenderItem(node, config, domOrder, pptx) {
8576
+ if (node.nodeType !== 1) return null;
8577
+ const style = window.getComputedStyle(node);
8578
+ if (style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0')
8579
+ return null;
8580
+
8581
+ const rect = node.getBoundingClientRect();
8582
+ if (rect.width < 0.5 || rect.height < 0.5) return null;
8583
+
8584
+ const zIndex = style.zIndex !== 'auto' ? parseInt(style.zIndex) : 0;
8585
+ const rotation = getRotation(style.transform);
8586
+ const elementOpacity = parseFloat(style.opacity);
8587
+
8588
+ const widthPx = node.offsetWidth || rect.width;
8589
+ const heightPx = node.offsetHeight || rect.height;
8590
+ const unrotatedW = widthPx * PX_TO_INCH * config.scale;
8591
+ const unrotatedH = heightPx * PX_TO_INCH * config.scale;
8592
+ const centerX = rect.left + rect.width / 2;
8593
+ const centerY = rect.top + rect.height / 2;
8594
+
8595
+ let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
8596
+ let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
8597
+ let w = unrotatedW;
8598
+ let h = unrotatedH;
8599
+
8600
+ const items = [];
8601
+
8602
+ if (node.nodeName.toUpperCase() === 'SVG') {
8603
+ const pngData = await svgToPng(node);
8604
+ if (pngData)
8605
+ items.push({
8606
+ type: 'image',
8607
+ zIndex,
8608
+ domOrder,
8609
+ options: { data: pngData, x, y, w, h, rotate: rotation },
8610
+ });
8611
+ return { items, stopRecursion: true };
8612
+ }
8613
+
8614
+ // --- UPDATED IMG BLOCK START ---
8615
+ if (node.tagName === 'IMG') {
8616
+ // Extract individual corner radii
8617
+ let radii = {
8618
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
8619
+ tr: parseFloat(style.borderTopRightRadius) || 0,
8620
+ br: parseFloat(style.borderBottomRightRadius) || 0,
8621
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
8622
+ };
8623
+
8624
+ const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
8625
+
8626
+ // Fallback: Check parent if image has no specific radius but parent clips it
8627
+ if (!hasAnyRadius) {
8628
+ const parent = node.parentElement;
8629
+ const parentStyle = window.getComputedStyle(parent);
8630
+ if (parentStyle.overflow !== 'visible') {
8631
+ const pRadii = {
8632
+ tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
8633
+ tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
8634
+ br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
8635
+ bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
8636
+ };
8637
+ // Simple heuristic: If image takes up full size of parent, inherit radii.
8638
+ // For complex grids (like slide-1), this blindly applies parent radius.
8639
+ // In a perfect world, we'd calculate intersection, but for now we apply parent radius
8640
+ // if the image is close to the parent's size, effectively masking it.
8641
+ const pRect = parent.getBoundingClientRect();
8642
+ if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
8643
+ radii = pRadii;
8644
+ }
8645
+ }
8646
+ }
8647
+
8648
+ const processed = await getProcessedImage(node.src, widthPx, heightPx, radii);
8649
+ if (processed)
8650
+ items.push({
8651
+ type: 'image',
8652
+ zIndex,
8653
+ domOrder,
8654
+ options: { data: processed, x, y, w, h, rotate: rotation },
8655
+ });
8656
+ return { items, stopRecursion: true };
8657
+ }
8658
+ // --- UPDATED IMG BLOCK END ---
8659
+
8660
+ // Radii processing for Divs/Shapes
8661
+ const borderRadiusValue = parseFloat(style.borderRadius) || 0;
8662
+ const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
8663
+ const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
8664
+ const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
8665
+ const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
8666
+
8667
+ const hasPartialBorderRadius =
8668
+ (borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
8669
+ (borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
8670
+ (borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
8671
+ (borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
8672
+ (borderRadiusValue === 0 &&
8673
+ (borderBottomLeftRadius ||
8674
+ borderBottomRightRadius ||
8675
+ borderTopLeftRadius ||
8676
+ borderTopRightRadius));
8677
+
8678
+ // Allow clipped elements to be rendered via canvas
8679
+ if (hasPartialBorderRadius && isClippedByParent(node)) {
8680
+ const marginLeft = parseFloat(style.marginLeft) || 0;
8681
+ const marginTop = parseFloat(style.marginTop) || 0;
8682
+ x += marginLeft * PX_TO_INCH * config.scale;
8683
+ y += marginTop * PX_TO_INCH * config.scale;
8684
+
8685
+ const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx, config.root);
8686
+ if (canvasImageData) {
8687
+ items.push({
8688
+ type: 'image',
8689
+ zIndex,
8690
+ domOrder,
8691
+ options: { data: canvasImageData, x, y, w, h, rotate: rotation },
8692
+ });
8693
+ return { items, stopRecursion: true };
8694
+ }
8695
+ }
8696
+
8697
+ const bgColorObj = parseColor(style.backgroundColor);
8698
+ const bgClip = style.webkitBackgroundClip || style.backgroundClip;
8699
+ const isBgClipText = bgClip === 'text';
8700
+ const hasGradient =
8701
+ !isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
8702
+
8703
+ const borderColorObj = parseColor(style.borderColor);
8704
+ const borderWidth = parseFloat(style.borderWidth);
8705
+ const hasBorder = borderWidth > 0 && borderColorObj.hex;
8706
+
8707
+ const borderInfo = getBorderInfo(style, config.scale);
8708
+ const hasUniformBorder = borderInfo.type === 'uniform';
8709
+ const hasCompositeBorder = borderInfo.type === 'composite';
8710
+
8711
+ const shadowStr = style.boxShadow;
8712
+ const hasShadow = shadowStr && shadowStr !== 'none';
8713
+ const softEdge = getSoftEdges(style.filter, config.scale);
8714
+
8715
+ let isImageWrapper = false;
8716
+ const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
8717
+ if (imgChild) {
8718
+ const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
8719
+ const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
8720
+ if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
8721
+ }
8722
+
8723
+ let textPayload = null;
8724
+ const isText = isTextContainer(node);
8725
+
8726
+ if (isText) {
8727
+ const textParts = [];
8728
+ const isList = style.display === 'list-item';
8729
+ if (isList) {
8730
+ const fontSizePt = parseFloat(style.fontSize) * 0.75 * config.scale;
8731
+ const bulletShift = (parseFloat(style.fontSize) || 16) * PX_TO_INCH * config.scale * 1.5;
8732
+ x -= bulletShift;
8733
+ w += bulletShift;
8734
+ textParts.push({
8735
+ text: '• ',
8736
+ options: {
8737
+ color: parseColor(style.color).hex || '000000',
8738
+ fontSize: fontSizePt,
8739
+ },
8740
+ });
8741
+ }
8742
+
8743
+ node.childNodes.forEach((child, index) => {
8744
+ let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
8745
+ let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
8746
+ textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
8747
+ if (index === 0 && !isList) textVal = textVal.trimStart();
8748
+ else if (index === 0) textVal = textVal.trimStart();
8749
+ if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
8750
+ if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
8751
+ if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
8752
+
8753
+ if (textVal.length > 0) {
8754
+ textParts.push({
8755
+ text: textVal,
8756
+ options: getTextStyle(nodeStyle, config.scale),
8757
+ });
8758
+ }
8759
+ });
8760
+
8761
+ if (textParts.length > 0) {
8762
+ let align = style.textAlign || 'left';
8763
+ if (align === 'start') align = 'left';
8764
+ if (align === 'end') align = 'right';
8765
+ let valign = 'top';
8766
+ if (style.alignItems === 'center') valign = 'middle';
8767
+ if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
8768
+
8769
+ const pt = parseFloat(style.paddingTop) || 0;
8770
+ const pb = parseFloat(style.paddingBottom) || 0;
8771
+ if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
8772
+
8773
+ let padding = getPadding(style, config.scale);
8774
+ if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
8775
+
8776
+ textPayload = { text: textParts, align, valign, inset: padding };
8777
+ }
8778
+ }
8779
+
8780
+ if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
8781
+ let bgData = null;
8782
+ let padIn = 0;
8783
+ if (softEdge) {
8784
+ const svgInfo = generateBlurredSVG(
8785
+ widthPx,
8786
+ heightPx,
8787
+ bgColorObj.hex,
8788
+ borderRadiusValue,
8789
+ softEdge
8790
+ );
8791
+ bgData = svgInfo.data;
8792
+ padIn = svgInfo.padding * PX_TO_INCH * config.scale;
8793
+ } else {
8794
+ bgData = generateGradientSVG(
8795
+ widthPx,
8796
+ heightPx,
8797
+ style.backgroundImage,
8798
+ borderRadiusValue,
8799
+ hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
8800
+ );
8801
+ }
8802
+
8803
+ if (bgData) {
8804
+ items.push({
8805
+ type: 'image',
8806
+ zIndex,
8807
+ domOrder,
8808
+ options: {
8809
+ data: bgData,
8810
+ x: x - padIn,
8811
+ y: y - padIn,
8812
+ w: w + padIn * 2,
8813
+ h: h + padIn * 2,
8814
+ rotate: rotation,
8815
+ },
8816
+ });
8817
+ }
8818
+
8819
+ if (textPayload) {
8820
+ items.push({
8821
+ type: 'text',
8822
+ zIndex: zIndex + 1,
8823
+ domOrder,
8824
+ textParts: textPayload.text,
8825
+ options: {
8826
+ x,
8827
+ y,
8828
+ w,
8829
+ h,
8830
+ align: textPayload.align,
8831
+ valign: textPayload.valign,
8832
+ inset: textPayload.inset,
8833
+ rotate: rotation,
8834
+ margin: 0,
8835
+ wrap: true,
8836
+ autoFit: false,
8837
+ },
8838
+ });
8839
+ }
8840
+ if (hasCompositeBorder) {
8841
+ // Add border shapes after the main background
8842
+ const borderItems = createCompositeBorderItems(
8843
+ borderInfo.sides,
8844
+ x,
8845
+ y,
8846
+ w,
8847
+ h,
8848
+ config.scale,
8849
+ zIndex,
8850
+ domOrder
8851
+ );
8852
+ items.push(...borderItems);
8853
+ }
8854
+ } else if (
8855
+ (bgColorObj.hex && !isImageWrapper) ||
8856
+ hasUniformBorder ||
8857
+ hasCompositeBorder ||
8858
+ hasShadow ||
8859
+ textPayload
8860
+ ) {
8861
+ const finalAlpha = elementOpacity * bgColorObj.opacity;
8862
+ const transparency = (1 - finalAlpha) * 100;
8863
+ const useSolidFill = bgColorObj.hex && !isImageWrapper;
8864
+
8865
+ if (hasPartialBorderRadius && useSolidFill && !textPayload) {
8866
+ const shapeSvg = generateCustomShapeSVG(
8867
+ widthPx,
8868
+ heightPx,
8869
+ bgColorObj.hex,
8870
+ bgColorObj.opacity,
8871
+ {
8872
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
8873
+ tr: parseFloat(style.borderTopRightRadius) || 0,
8874
+ br: parseFloat(style.borderBottomRightRadius) || 0,
8875
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
8876
+ }
8877
+ );
8878
+
8879
+ items.push({
8880
+ type: 'image',
8881
+ zIndex,
8882
+ domOrder,
8883
+ options: {
8884
+ data: shapeSvg,
8885
+ x,
8886
+ y,
8887
+ w,
8888
+ h,
8889
+ rotate: rotation,
8890
+ },
8891
+ });
8892
+ } else {
8893
+ const shapeOpts = {
8894
+ x,
8895
+ y,
8896
+ w,
8897
+ h,
8898
+ rotate: rotation,
8899
+ fill: useSolidFill
8900
+ ? { color: bgColorObj.hex, transparency: transparency }
8901
+ : { type: 'none' },
8902
+ line: hasUniformBorder ? borderInfo.options : null,
8903
+ };
8904
+
8905
+ if (hasShadow) {
8906
+ shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
8907
+ }
8908
+
8909
+ const borderRadius = parseFloat(style.borderRadius) || 0;
8910
+ const aspectRatio = Math.max(widthPx, heightPx) / Math.min(widthPx, heightPx);
8911
+ const isCircle = aspectRatio < 1.1 && borderRadius >= Math.min(widthPx, heightPx) / 2 - 1;
8912
+
8913
+ let shapeType = pptx.ShapeType.rect;
8914
+ if (isCircle) shapeType = pptx.ShapeType.ellipse;
8915
+ else if (borderRadius > 0) {
8916
+ shapeType = pptx.ShapeType.roundRect;
8917
+ shapeOpts.rectRadius = Math.min(0.5, borderRadius / Math.min(widthPx, heightPx));
8918
+ }
8919
+
8920
+ if (textPayload) {
8921
+ const textOptions = {
8922
+ shape: shapeType,
8923
+ ...shapeOpts,
8924
+ align: textPayload.align,
8925
+ valign: textPayload.valign,
8926
+ inset: textPayload.inset,
8927
+ margin: 0,
8928
+ wrap: true,
8929
+ autoFit: false,
8930
+ };
8931
+ items.push({
8932
+ type: 'text',
8933
+ zIndex,
8934
+ domOrder,
8935
+ textParts: textPayload.text,
8936
+ options: textOptions,
8937
+ });
8938
+ } else if (!hasPartialBorderRadius) {
8939
+ items.push({
8940
+ type: 'shape',
8941
+ zIndex,
8942
+ domOrder,
8943
+ shapeType,
8944
+ options: shapeOpts,
8945
+ });
8946
+ }
8947
+ }
8948
+
8949
+ if (hasCompositeBorder) {
8950
+ const borderSvgData = generateCompositeBorderSVG(
8951
+ widthPx,
8952
+ heightPx,
8953
+ borderRadiusValue,
8954
+ borderInfo.sides
8955
+ );
8956
+ if (borderSvgData) {
8957
+ items.push({
8958
+ type: 'image',
8959
+ zIndex: zIndex + 1,
8960
+ domOrder,
8961
+ options: { data: borderSvgData, x, y, w, h, rotate: rotation },
8962
+ });
8963
+ }
8964
+ }
8965
+ }
8966
+
8967
+ return { items, stopRecursion: !!textPayload };
8968
+ }
8969
+
8970
+ /**
8971
+ * Helper function to create individual border shapes
8972
+ */
8973
+ function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
8974
+ const items = [];
8975
+ const pxToInch = 1 / 96;
8976
+
8977
+ // TOP BORDER
8978
+ if (sides.top.width > 0) {
8979
+ items.push({
8980
+ type: 'shape',
8981
+ zIndex: zIndex + 1,
8982
+ domOrder,
8983
+ shapeType: 'rect',
8984
+ options: {
8985
+ x: x,
8986
+ y: y,
8987
+ w: w,
8988
+ h: sides.top.width * pxToInch * scale,
8989
+ fill: { color: sides.top.color },
8990
+ },
8991
+ });
8992
+ }
8993
+ // RIGHT BORDER
8994
+ if (sides.right.width > 0) {
8995
+ items.push({
8996
+ type: 'shape',
8997
+ zIndex: zIndex + 1,
8998
+ domOrder,
8999
+ shapeType: 'rect',
9000
+ options: {
9001
+ x: x + w - sides.right.width * pxToInch * scale,
9002
+ y: y,
9003
+ w: sides.right.width * pxToInch * scale,
9004
+ h: h,
9005
+ fill: { color: sides.right.color },
9006
+ },
9007
+ });
9008
+ }
9009
+ // BOTTOM BORDER
9010
+ if (sides.bottom.width > 0) {
9011
+ items.push({
9012
+ type: 'shape',
9013
+ zIndex: zIndex + 1,
9014
+ domOrder,
9015
+ shapeType: 'rect',
9016
+ options: {
9017
+ x: x,
9018
+ y: y + h - sides.bottom.width * pxToInch * scale,
9019
+ w: w,
9020
+ h: sides.bottom.width * pxToInch * scale,
9021
+ fill: { color: sides.bottom.color },
9022
+ },
9023
+ });
9024
+ }
9025
+ // LEFT BORDER
9026
+ if (sides.left.width > 0) {
9027
+ items.push({
9028
+ type: 'shape',
9029
+ zIndex: zIndex + 1,
9030
+ domOrder,
9031
+ shapeType: 'rect',
9032
+ options: {
9033
+ x: x,
9034
+ y: y,
9035
+ w: sides.left.width * pxToInch * scale,
9036
+ h: h,
9037
+ fill: { color: sides.left.color },
9038
+ },
9039
+ });
9040
+ }
9041
+
9042
+ return items;
9012
9043
  }
9013
9044
 
9014
9045
  exports.exportToPptx = exportToPptx;