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