dom-to-pptx 1.0.6 → 1.0.7

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.
@@ -7844,484 +7844,430 @@
7844
7844
  : defaultBackgroundColor;
7845
7845
  };
7846
7846
 
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;
7865
- }
7866
-
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;
7875
- }
7876
-
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
- };
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;
7865
+ }
7866
+
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;
7875
+ }
7876
+
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
+ // 1. Reject Web Components / Icons / Images
8093
+ if (el.tagName.includes('-')) return false;
8094
+ if (el.tagName === 'IMG' || el.tagName === 'SVG') return false;
8095
+
8096
+ const style = window.getComputedStyle(el);
8097
+ const display = style.display;
8098
+
8099
+ // 2. Initial check: Must be a standard inline tag OR display:inline
8100
+ const isInlineTag = ['SPAN', 'B', 'STRONG', 'EM', 'I', 'A', 'SMALL', 'MARK'].includes(el.tagName);
8101
+ const isInlineDisplay = display.includes('inline');
8102
+
8103
+ if (!isInlineTag && !isInlineDisplay) return false;
8104
+
8105
+ // 3. CRITICAL FIX: Check for Structural Styling
8106
+ // PPTX Text Runs (parts of a text line) CANNOT have backgrounds, borders, or padding.
8107
+ // If a child element has these, the parent is NOT a simple text container;
8108
+ // it is a layout container composed of styled blocks.
8109
+ const bgColor = parseColor(style.backgroundColor);
8110
+ const hasVisibleBg = bgColor.hex && bgColor.opacity > 0;
8111
+ const hasBorder = parseFloat(style.borderWidth) > 0 && parseColor(style.borderColor).opacity > 0;
8112
+
8113
+ if (hasVisibleBg || hasBorder) {
8114
+ return false;
8115
+ }
8116
+
8117
+ // 4. Check for empty shapes (visual objects without text, like dots)
8118
+ const hasContent = el.textContent.trim().length > 0;
8119
+ if (!hasContent && (hasVisibleBg || hasBorder)) {
8120
+ return false;
8121
+ }
8122
+
8123
+ return true;
8124
+ };
8125
+
8126
+ return children.every(isSafeInline);
8127
+ }
8128
+
8129
+ function getRotation(transformStr) {
8130
+ if (!transformStr || transformStr === 'none') return 0;
8131
+ const values = transformStr.split('(')[1].split(')')[0].split(',');
8132
+ if (values.length < 4) return 0;
8133
+ const a = parseFloat(values[0]);
8134
+ const b = parseFloat(values[1]);
8135
+ return Math.round(Math.atan2(b, a) * (180 / Math.PI));
8136
+ }
8137
+
8138
+ function getVisibleShadow(shadowStr, scale) {
8139
+ if (!shadowStr || shadowStr === 'none') return null;
8140
+ const shadows = shadowStr.split(/,(?![^()]*\))/);
8141
+ for (let s of shadows) {
8142
+ s = s.trim();
8143
+ if (s.startsWith('rgba(0, 0, 0, 0)')) continue;
8144
+ const match = s.match(
8145
+ /(rgba?\([^)]+\)|#[0-9a-fA-F]+)\s+(-?[\d.]+)px\s+(-?[\d.]+)px\s+([\d.]+)px/
8146
+ );
8147
+ if (match) {
8148
+ const colorStr = match[1];
8149
+ const x = parseFloat(match[2]);
8150
+ const y = parseFloat(match[3]);
8151
+ const blur = parseFloat(match[4]);
8152
+ const distance = Math.sqrt(x * x + y * y);
8153
+ let angle = Math.atan2(y, x) * (180 / Math.PI);
8154
+ if (angle < 0) angle += 360;
8155
+ const colorObj = parseColor(colorStr);
8156
+ return {
8157
+ type: 'outer',
8158
+ angle: angle,
8159
+ blur: blur * 0.75 * scale,
8160
+ offset: distance * 0.75 * scale,
8161
+ color: colorObj.hex || '000000',
8162
+ opacity: colorObj.opacity,
8163
+ };
8164
+ }
8165
+ }
8166
+ return null;
8167
+ }
8168
+
8169
+ function generateGradientSVG(w, h, bgString, radius, border) {
8170
+ try {
8171
+ const match = bgString.match(/linear-gradient\((.*)\)/);
8172
+ if (!match) return null;
8173
+ const content = match[1];
8174
+ const parts = content.split(/,(?![^()]*\))/).map((p) => p.trim());
8175
+
8176
+ let x1 = '0%',
8177
+ y1 = '0%',
8178
+ x2 = '0%',
8179
+ y2 = '100%';
8180
+ let stopsStartIdx = 0;
8181
+ if (parts[0].includes('to right')) {
8182
+ x1 = '0%';
8183
+ x2 = '100%';
8184
+ y2 = '0%';
8185
+ stopsStartIdx = 1;
8186
+ } else if (parts[0].includes('to left')) {
8187
+ x1 = '100%';
8188
+ x2 = '0%';
8189
+ y2 = '0%';
8190
+ stopsStartIdx = 1;
8191
+ } else if (parts[0].includes('to top')) {
8192
+ y1 = '100%';
8193
+ y2 = '0%';
8194
+ stopsStartIdx = 1;
8195
+ } else if (parts[0].includes('to bottom')) {
8196
+ y1 = '0%';
8197
+ y2 = '100%';
8198
+ stopsStartIdx = 1;
8199
+ }
8200
+
8201
+ let stopsXML = '';
8202
+ const stopParts = parts.slice(stopsStartIdx);
8203
+ stopParts.forEach((part, idx) => {
8204
+ let color = part;
8205
+ let offset = Math.round((idx / (stopParts.length - 1)) * 100) + '%';
8206
+ const posMatch = part.match(/(.*?)\s+(\d+(\.\d+)?%?)$/);
8207
+ if (posMatch) {
8208
+ color = posMatch[1];
8209
+ offset = posMatch[2];
8210
+ }
8211
+ let opacity = 1;
8212
+ if (color.includes('rgba')) {
8213
+ const rgba = color.match(/[\d.]+/g);
8214
+ if (rgba && rgba.length > 3) {
8215
+ opacity = rgba[3];
8216
+ color = `rgb(${rgba[0]},${rgba[1]},${rgba[2]})`;
8217
+ }
8218
+ }
8219
+ stopsXML += `<stop offset="${offset}" stop-color="${color}" stop-opacity="${opacity}"/>`;
8220
+ });
8221
+
8222
+ let strokeAttr = '';
8223
+ if (border) {
8224
+ strokeAttr = `stroke="#${border.color}" stroke-width="${border.width}"`;
8225
+ }
8226
+
8227
+ const svg = `
8228
+ <svg xmlns="http://www.w3.org/2000/svg" width="${w}" height="${h}" viewBox="0 0 ${w} ${h}">
8229
+ <defs><linearGradient id="grad" x1="${x1}" y1="${y1}" x2="${x2}" y2="${y2}">${stopsXML}</linearGradient></defs>
8230
+ <rect x="0" y="0" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="url(#grad)" ${strokeAttr} />
8231
+ </svg>`;
8232
+ return 'data:image/svg+xml;base64,' + btoa(svg);
8233
+ } catch {
8234
+ return null;
8235
+ }
8236
+ }
8237
+
8238
+ function generateBlurredSVG(w, h, color, radius, blurPx) {
8239
+ const padding = blurPx * 3;
8240
+ const fullW = w + padding * 2;
8241
+ const fullH = h + padding * 2;
8242
+ const x = padding;
8243
+ const y = padding;
8244
+ let shapeTag = '';
8245
+ const isCircle = radius >= Math.min(w, h) / 2 - 1 && Math.abs(w - h) < 2;
8246
+
8247
+ if (isCircle) {
8248
+ const cx = x + w / 2;
8249
+ const cy = y + h / 2;
8250
+ const rx = w / 2;
8251
+ const ry = h / 2;
8252
+ shapeTag = `<ellipse cx="${cx}" cy="${cy}" rx="${rx}" ry="${ry}" fill="#${color}" filter="url(#f1)" />`;
8253
+ } else {
8254
+ shapeTag = `<rect x="${x}" y="${y}" width="${w}" height="${h}" rx="${radius}" ry="${radius}" fill="#${color}" filter="url(#f1)" />`;
8255
+ }
8256
+
8257
+ const svg = `
8258
+ <svg xmlns="http://www.w3.org/2000/svg" width="${fullW}" height="${fullH}" viewBox="0 0 ${fullW} ${fullH}">
8259
+ <defs>
8260
+ <filter id="f1" x="-50%" y="-50%" width="200%" height="200%">
8261
+ <feGaussianBlur in="SourceGraphic" stdDeviation="${blurPx}" />
8262
+ </filter>
8263
+ </defs>
8264
+ ${shapeTag}
8265
+ </svg>`;
8266
+
8267
+ return {
8268
+ data: 'data:image/svg+xml;base64,' + btoa(svg),
8269
+ padding: padding,
8270
+ };
8325
8271
  }
8326
8272
 
8327
8273
  // src/image-processor.js
@@ -8404,642 +8350,717 @@
8404
8350
  });
8405
8351
  }
8406
8352
 
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;
8353
+ // src/index.js
8354
+
8355
+ // Normalize import
8356
+ const PptxGenJS = PptxGenJSImport__namespace?.default ?? PptxGenJSImport__namespace;
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
+ const asyncTasks = []; // Queue for heavy operations (Images, Canvas)
8423
+ let domOrderCounter = 0;
8424
+
8425
+ // Sync Traversal Function
8426
+ function collect(node, parentZIndex) {
8427
+ const order = domOrderCounter++;
8428
+
8429
+ let currentZ = parentZIndex;
8430
+ let nodeStyle = null;
8431
+ const nodeType = node.nodeType;
8432
+
8433
+ if (nodeType === 1) {
8434
+ nodeStyle = window.getComputedStyle(node);
8435
+ // Optimization: Skip completely hidden elements immediately
8436
+ if (
8437
+ nodeStyle.display === 'none' ||
8438
+ nodeStyle.visibility === 'hidden' ||
8439
+ nodeStyle.opacity === '0'
8440
+ ) {
8441
+ return;
8442
+ }
8443
+ if (nodeStyle.zIndex !== 'auto') {
8444
+ currentZ = parseInt(nodeStyle.zIndex);
8445
+ }
8446
+ }
8447
+
8448
+ // Prepare the item. If it needs async work, it returns a 'job'
8449
+ const result = prepareRenderItem(
8450
+ node,
8451
+ { ...layoutConfig, root },
8452
+ order,
8453
+ pptx,
8454
+ currentZ,
8455
+ nodeStyle
8456
+ );
8457
+
8458
+ if (result) {
8459
+ if (result.items) {
8460
+ // Push items immediately to queue (data might be missing but filled later)
8461
+ renderQueue.push(...result.items);
8462
+ }
8463
+ if (result.job) {
8464
+ // Push the promise-returning function to the task list
8465
+ asyncTasks.push(result.job);
8466
+ }
8467
+ if (result.stopRecursion) return;
8468
+ }
8469
+
8470
+ // Recurse children synchronously
8471
+ const childNodes = node.childNodes;
8472
+ for (let i = 0; i < childNodes.length; i++) {
8473
+ collect(childNodes[i], currentZ);
8474
+ }
8475
+ }
8476
+
8477
+ // 1. Traverse and build the structure (Fast)
8478
+ collect(root, 0);
8479
+
8480
+ // 2. Execute all heavy tasks in parallel (Fast)
8481
+ if (asyncTasks.length > 0) {
8482
+ await Promise.all(asyncTasks.map((task) => task()));
8483
+ }
8484
+
8485
+ // 3. Cleanup and Sort
8486
+ // Remove items that failed to generate data (marked with skip)
8487
+ const finalQueue = renderQueue.filter(
8488
+ (item) => !item.skip && (item.type !== 'image' || item.options.data)
8489
+ );
8490
+
8491
+ finalQueue.sort((a, b) => {
8492
+ if (a.zIndex !== b.zIndex) return a.zIndex - b.zIndex;
8493
+ return a.domOrder - b.domOrder;
8494
+ });
8495
+
8496
+ // 4. Add to Slide
8497
+ for (const item of finalQueue) {
8498
+ if (item.type === 'shape') slide.addShape(item.shapeType, item.options);
8499
+ if (item.type === 'image') slide.addImage(item.options);
8500
+ if (item.type === 'text') slide.addText(item.textParts, item.options);
8501
+ }
8502
+ }
8503
+
8504
+ /**
8505
+ * Optimized html2canvas wrapper
8506
+ * Now strictly captures the node itself, not the root.
8507
+ */
8508
+ async function elementToCanvasImage(node, widthPx, heightPx) {
8509
+ return new Promise((resolve) => {
8510
+ const width = Math.max(Math.ceil(widthPx), 1);
8511
+ const height = Math.max(Math.ceil(heightPx), 1);
8512
+ const style = window.getComputedStyle(node);
8513
+
8514
+ // Optimized: Capture ONLY the specific node
8515
+ html2canvas(node, {
8516
+ backgroundColor: null,
8517
+ logging: false,
8518
+ scale: 2, // Slight quality boost
8519
+ })
8520
+ .then((canvas) => {
8521
+ const destCanvas = document.createElement('canvas');
8522
+ destCanvas.width = width;
8523
+ destCanvas.height = height;
8524
+ const ctx = destCanvas.getContext('2d');
8525
+
8526
+ // Draw the captured canvas into our sized canvas
8527
+ // html2canvas might return a larger canvas if scale > 1, so we fit it
8528
+ ctx.drawImage(canvas, 0, 0, canvas.width, canvas.height, 0, 0, width, height);
8529
+
8530
+ // Apply border radius clipping
8531
+ let tl = parseFloat(style.borderTopLeftRadius) || 0;
8532
+ let tr = parseFloat(style.borderTopRightRadius) || 0;
8533
+ let br = parseFloat(style.borderBottomRightRadius) || 0;
8534
+ let bl = parseFloat(style.borderBottomLeftRadius) || 0;
8535
+
8536
+ const f = Math.min(
8537
+ width / (tl + tr) || Infinity,
8538
+ height / (tr + br) || Infinity,
8539
+ width / (br + bl) || Infinity,
8540
+ height / (bl + tl) || Infinity
8541
+ );
8542
+
8543
+ if (f < 1) {
8544
+ tl *= f;
8545
+ tr *= f;
8546
+ br *= f;
8547
+ bl *= f;
8548
+ }
8549
+
8550
+ if (tl + tr + br + bl > 0) {
8551
+ ctx.globalCompositeOperation = 'destination-in';
8552
+ ctx.beginPath();
8553
+ ctx.moveTo(tl, 0);
8554
+ ctx.lineTo(width - tr, 0);
8555
+ ctx.arcTo(width, 0, width, tr, tr);
8556
+ ctx.lineTo(width, height - br);
8557
+ ctx.arcTo(width, height, width - br, height, br);
8558
+ ctx.lineTo(bl, height);
8559
+ ctx.arcTo(0, height, 0, height - bl, bl);
8560
+ ctx.lineTo(0, tl);
8561
+ ctx.arcTo(0, 0, tl, 0, tl);
8562
+ ctx.closePath();
8563
+ ctx.fill();
8564
+ }
8565
+
8566
+ resolve(destCanvas.toDataURL('image/png'));
8567
+ })
8568
+ .catch((e) => {
8569
+ console.warn('Canvas capture failed for node', node, e);
8570
+ resolve(null);
8571
+ });
8572
+ });
8573
+ }
8574
+
8575
+ /**
8576
+ * Replaces createRenderItem.
8577
+ * Returns { items: [], job: () => Promise, stopRecursion: boolean }
8578
+ */
8579
+ function prepareRenderItem(node, config, domOrder, pptx, effectiveZIndex, computedStyle) {
8580
+ // 1. Text Node Handling
8581
+ if (node.nodeType === 3) {
8582
+ const textContent = node.nodeValue.trim();
8583
+ if (!textContent) return null;
8584
+
8585
+ const parent = node.parentElement;
8586
+ if (!parent) return null;
8587
+
8588
+ if (isTextContainer(parent)) return null; // Parent handles it
8589
+
8590
+ const range = document.createRange();
8591
+ range.selectNode(node);
8592
+ const rect = range.getBoundingClientRect();
8593
+ range.detach();
8594
+
8595
+ const style = window.getComputedStyle(parent);
8596
+ const widthPx = rect.width;
8597
+ const heightPx = rect.height;
8598
+ const unrotatedW = widthPx * PX_TO_INCH * config.scale;
8599
+ const unrotatedH = heightPx * PX_TO_INCH * config.scale;
8600
+
8601
+ const x = config.offX + (rect.left - config.rootX) * PX_TO_INCH * config.scale;
8602
+ const y = config.offY + (rect.top - config.rootY) * PX_TO_INCH * config.scale;
8603
+
8604
+ return {
8605
+ items: [
8606
+ {
8607
+ type: 'text',
8608
+ zIndex: effectiveZIndex,
8609
+ domOrder,
8610
+ textParts: [
8611
+ {
8612
+ text: textContent,
8613
+ options: getTextStyle(style, config.scale),
8614
+ },
8615
+ ],
8616
+ options: { x, y, w: unrotatedW, h: unrotatedH, margin: 0, autoFit: false },
8617
+ },
8618
+ ],
8619
+ stopRecursion: false,
8620
+ };
8621
+ }
8622
+
8623
+ if (node.nodeType !== 1) return null;
8624
+ const style = computedStyle; // Use pre-computed style
8625
+
8626
+ const rect = node.getBoundingClientRect();
8627
+ if (rect.width < 0.5 || rect.height < 0.5) return null;
8628
+
8629
+ const zIndex = effectiveZIndex;
8630
+ const rotation = getRotation(style.transform);
8631
+ const elementOpacity = parseFloat(style.opacity);
8632
+ const safeOpacity = isNaN(elementOpacity) ? 1 : elementOpacity;
8633
+
8634
+ const widthPx = node.offsetWidth || rect.width;
8635
+ const heightPx = node.offsetHeight || rect.height;
8636
+ const unrotatedW = widthPx * PX_TO_INCH * config.scale;
8637
+ const unrotatedH = heightPx * PX_TO_INCH * config.scale;
8638
+ const centerX = rect.left + rect.width / 2;
8639
+ const centerY = rect.top + rect.height / 2;
8640
+
8641
+ let x = config.offX + (centerX - config.rootX) * PX_TO_INCH * config.scale - unrotatedW / 2;
8642
+ let y = config.offY + (centerY - config.rootY) * PX_TO_INCH * config.scale - unrotatedH / 2;
8643
+ let w = unrotatedW;
8644
+ let h = unrotatedH;
8645
+
8646
+ const items = [];
8647
+
8648
+ // --- ASYNC JOB: SVGs / Icons ---
8649
+ if (
8650
+ node.nodeName.toUpperCase() === 'SVG' ||
8651
+ node.tagName.includes('-') ||
8652
+ node.tagName === 'ION-ICON'
8653
+ ) {
8654
+ const item = {
8655
+ type: 'image',
8656
+ zIndex,
8657
+ domOrder,
8658
+ options: { x, y, w, h, rotate: rotation, data: null }, // Data null initially
8659
+ };
8660
+
8661
+ // Create Job
8662
+ const job = async () => {
8663
+ const pngData = await elementToCanvasImage(node, widthPx, heightPx);
8664
+ if (pngData) item.options.data = pngData;
8665
+ else item.skip = true;
8666
+ };
8667
+
8668
+ return { items: [item], job, stopRecursion: true };
8669
+ }
8670
+
8671
+ // --- ASYNC JOB: IMG Tags ---
8672
+ if (node.tagName === 'IMG') {
8673
+ let radii = {
8674
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
8675
+ tr: parseFloat(style.borderTopRightRadius) || 0,
8676
+ br: parseFloat(style.borderBottomRightRadius) || 0,
8677
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
8678
+ };
8679
+
8680
+ const hasAnyRadius = radii.tl > 0 || radii.tr > 0 || radii.br > 0 || radii.bl > 0;
8681
+ if (!hasAnyRadius) {
8682
+ const parent = node.parentElement;
8683
+ const parentStyle = window.getComputedStyle(parent);
8684
+ if (parentStyle.overflow !== 'visible') {
8685
+ const pRadii = {
8686
+ tl: parseFloat(parentStyle.borderTopLeftRadius) || 0,
8687
+ tr: parseFloat(parentStyle.borderTopRightRadius) || 0,
8688
+ br: parseFloat(parentStyle.borderBottomRightRadius) || 0,
8689
+ bl: parseFloat(parentStyle.borderBottomLeftRadius) || 0,
8690
+ };
8691
+ const pRect = parent.getBoundingClientRect();
8692
+ if (Math.abs(pRect.width - rect.width) < 5 && Math.abs(pRect.height - rect.height) < 5) {
8693
+ radii = pRadii;
8694
+ }
8695
+ }
8696
+ }
8697
+
8698
+ const item = {
8699
+ type: 'image',
8700
+ zIndex,
8701
+ domOrder,
8702
+ options: { x, y, w, h, rotate: rotation, data: null },
8703
+ };
8704
+
8705
+ const job = async () => {
8706
+ const processed = await getProcessedImage(node.src, widthPx, heightPx, radii);
8707
+ if (processed) item.options.data = processed;
8708
+ else item.skip = true;
8709
+ };
8710
+
8711
+ return { items: [item], job, stopRecursion: true };
8712
+ }
8713
+
8714
+ // Radii logic
8715
+ const borderRadiusValue = parseFloat(style.borderRadius) || 0;
8716
+ const borderBottomLeftRadius = parseFloat(style.borderBottomLeftRadius) || 0;
8717
+ const borderBottomRightRadius = parseFloat(style.borderBottomRightRadius) || 0;
8718
+ const borderTopLeftRadius = parseFloat(style.borderTopLeftRadius) || 0;
8719
+ const borderTopRightRadius = parseFloat(style.borderTopRightRadius) || 0;
8720
+
8721
+ const hasPartialBorderRadius =
8722
+ (borderBottomLeftRadius > 0 && borderBottomLeftRadius !== borderRadiusValue) ||
8723
+ (borderBottomRightRadius > 0 && borderBottomRightRadius !== borderRadiusValue) ||
8724
+ (borderTopLeftRadius > 0 && borderTopLeftRadius !== borderRadiusValue) ||
8725
+ (borderTopRightRadius > 0 && borderTopRightRadius !== borderRadiusValue) ||
8726
+ (borderRadiusValue === 0 &&
8727
+ (borderBottomLeftRadius ||
8728
+ borderBottomRightRadius ||
8729
+ borderTopLeftRadius ||
8730
+ borderTopRightRadius));
8731
+
8732
+ // --- ASYNC JOB: Clipped Divs via Canvas ---
8733
+ if (hasPartialBorderRadius && isClippedByParent(node)) {
8734
+ const marginLeft = parseFloat(style.marginLeft) || 0;
8735
+ const marginTop = parseFloat(style.marginTop) || 0;
8736
+ x += marginLeft * PX_TO_INCH * config.scale;
8737
+ y += marginTop * PX_TO_INCH * config.scale;
8738
+
8739
+ const item = {
8740
+ type: 'image',
8741
+ zIndex,
8742
+ domOrder,
8743
+ options: { x, y, w, h, rotate: rotation, data: null },
8744
+ };
8745
+
8746
+ const job = async () => {
8747
+ const canvasImageData = await elementToCanvasImage(node, widthPx, heightPx);
8748
+ if (canvasImageData) item.options.data = canvasImageData;
8749
+ else item.skip = true;
8750
+ };
8751
+
8752
+ return { items: [item], job, stopRecursion: true };
8753
+ }
8754
+
8755
+ // --- SYNC: Standard CSS Extraction ---
8756
+ const bgColorObj = parseColor(style.backgroundColor);
8757
+ const bgClip = style.webkitBackgroundClip || style.backgroundClip;
8758
+ const isBgClipText = bgClip === 'text';
8759
+ const hasGradient =
8760
+ !isBgClipText && style.backgroundImage && style.backgroundImage.includes('linear-gradient');
8761
+
8762
+ const borderColorObj = parseColor(style.borderColor);
8763
+ const borderWidth = parseFloat(style.borderWidth);
8764
+ const hasBorder = borderWidth > 0 && borderColorObj.hex;
8765
+
8766
+ const borderInfo = getBorderInfo(style, config.scale);
8767
+ const hasUniformBorder = borderInfo.type === 'uniform';
8768
+ const hasCompositeBorder = borderInfo.type === 'composite';
8769
+
8770
+ const shadowStr = style.boxShadow;
8771
+ const hasShadow = shadowStr && shadowStr !== 'none';
8772
+ const softEdge = getSoftEdges(style.filter, config.scale);
8773
+
8774
+ let isImageWrapper = false;
8775
+ const imgChild = Array.from(node.children).find((c) => c.tagName === 'IMG');
8776
+ if (imgChild) {
8777
+ const childW = imgChild.offsetWidth || imgChild.getBoundingClientRect().width;
8778
+ const childH = imgChild.offsetHeight || imgChild.getBoundingClientRect().height;
8779
+ if (childW >= widthPx - 2 && childH >= heightPx - 2) isImageWrapper = true;
8780
+ }
8781
+
8782
+ let textPayload = null;
8783
+ const isText = isTextContainer(node);
8784
+
8785
+ if (isText) {
8786
+ const textParts = [];
8787
+ const isList = style.display === 'list-item';
8788
+ if (isList) {
8789
+ const fontSizePt = parseFloat(style.fontSize) * 0.75 * config.scale;
8790
+ const bulletShift = (parseFloat(style.fontSize) || 16) * PX_TO_INCH * config.scale * 1.5;
8791
+ x -= bulletShift;
8792
+ w += bulletShift;
8793
+ textParts.push({
8794
+ text: ' ',
8795
+ options: {
8796
+ color: parseColor(style.color).hex || '000000',
8797
+ fontSize: fontSizePt,
8798
+ },
8799
+ });
8800
+ }
8801
+
8802
+ node.childNodes.forEach((child, index) => {
8803
+ let textVal = child.nodeType === 3 ? child.nodeValue : child.textContent;
8804
+ let nodeStyle = child.nodeType === 1 ? window.getComputedStyle(child) : style;
8805
+ textVal = textVal.replace(/[\n\r\t]+/g, ' ').replace(/\s{2,}/g, ' ');
8806
+ if (index === 0 && !isList) textVal = textVal.trimStart();
8807
+ else if (index === 0) textVal = textVal.trimStart();
8808
+ if (index === node.childNodes.length - 1) textVal = textVal.trimEnd();
8809
+ if (nodeStyle.textTransform === 'uppercase') textVal = textVal.toUpperCase();
8810
+ if (nodeStyle.textTransform === 'lowercase') textVal = textVal.toLowerCase();
8811
+
8812
+ if (textVal.length > 0) {
8813
+ textParts.push({
8814
+ text: textVal,
8815
+ options: getTextStyle(nodeStyle, config.scale),
8816
+ });
8817
+ }
8818
+ });
8819
+
8820
+ if (textParts.length > 0) {
8821
+ let align = style.textAlign || 'left';
8822
+ if (align === 'start') align = 'left';
8823
+ if (align === 'end') align = 'right';
8824
+ let valign = 'top';
8825
+ if (style.alignItems === 'center') valign = 'middle';
8826
+ if (style.justifyContent === 'center' && style.display.includes('flex')) align = 'center';
8827
+
8828
+ const pt = parseFloat(style.paddingTop) || 0;
8829
+ const pb = parseFloat(style.paddingBottom) || 0;
8830
+ if (Math.abs(pt - pb) < 2 && bgColorObj.hex) valign = 'middle';
8831
+
8832
+ let padding = getPadding(style, config.scale);
8833
+ if (align === 'center' && valign === 'middle') padding = [0, 0, 0, 0];
8834
+
8835
+ textPayload = { text: textParts, align, valign, inset: padding };
8836
+ }
8837
+ }
8838
+
8839
+ if (hasGradient || (softEdge && bgColorObj.hex && !isImageWrapper)) {
8840
+ let bgData = null;
8841
+ let padIn = 0;
8842
+ if (softEdge) {
8843
+ const svgInfo = generateBlurredSVG(
8844
+ widthPx,
8845
+ heightPx,
8846
+ bgColorObj.hex,
8847
+ borderRadiusValue,
8848
+ softEdge
8849
+ );
8850
+ bgData = svgInfo.data;
8851
+ padIn = svgInfo.padding * PX_TO_INCH * config.scale;
8852
+ } else {
8853
+ bgData = generateGradientSVG(
8854
+ widthPx,
8855
+ heightPx,
8856
+ style.backgroundImage,
8857
+ borderRadiusValue,
8858
+ hasBorder ? { color: borderColorObj.hex, width: borderWidth } : null
8859
+ );
8860
+ }
8861
+
8862
+ if (bgData) {
8863
+ items.push({
8864
+ type: 'image',
8865
+ zIndex,
8866
+ domOrder,
8867
+ options: {
8868
+ data: bgData,
8869
+ x: x - padIn,
8870
+ y: y - padIn,
8871
+ w: w + padIn * 2,
8872
+ h: h + padIn * 2,
8873
+ rotate: rotation,
8874
+ },
8875
+ });
8876
+ }
8877
+
8878
+ if (textPayload) {
8879
+ items.push({
8880
+ type: 'text',
8881
+ zIndex: zIndex + 1,
8882
+ domOrder,
8883
+ textParts: textPayload.text,
8884
+ options: {
8885
+ x,
8886
+ y,
8887
+ w,
8888
+ h,
8889
+ align: textPayload.align,
8890
+ valign: textPayload.valign,
8891
+ inset: textPayload.inset,
8892
+ rotate: rotation,
8893
+ margin: 0,
8894
+ wrap: true,
8895
+ autoFit: false,
8896
+ },
8897
+ });
8898
+ }
8899
+ if (hasCompositeBorder) {
8900
+ const borderItems = createCompositeBorderItems(
8901
+ borderInfo.sides,
8902
+ x,
8903
+ y,
8904
+ w,
8905
+ h,
8906
+ config.scale,
8907
+ zIndex,
8908
+ domOrder
8909
+ );
8910
+ items.push(...borderItems);
8911
+ }
8912
+ } else if (
8913
+ (bgColorObj.hex && !isImageWrapper) ||
8914
+ hasUniformBorder ||
8915
+ hasCompositeBorder ||
8916
+ hasShadow ||
8917
+ textPayload
8918
+ ) {
8919
+ const finalAlpha = safeOpacity * bgColorObj.opacity;
8920
+ const transparency = (1 - finalAlpha) * 100;
8921
+ const useSolidFill = bgColorObj.hex && !isImageWrapper;
8922
+
8923
+ if (hasPartialBorderRadius && useSolidFill && !textPayload) {
8924
+ const shapeSvg = generateCustomShapeSVG(
8925
+ widthPx,
8926
+ heightPx,
8927
+ bgColorObj.hex,
8928
+ bgColorObj.opacity,
8929
+ {
8930
+ tl: parseFloat(style.borderTopLeftRadius) || 0,
8931
+ tr: parseFloat(style.borderTopRightRadius) || 0,
8932
+ br: parseFloat(style.borderBottomRightRadius) || 0,
8933
+ bl: parseFloat(style.borderBottomLeftRadius) || 0,
8934
+ }
8935
+ );
8936
+
8937
+ items.push({
8938
+ type: 'image',
8939
+ zIndex,
8940
+ domOrder,
8941
+ options: { data: shapeSvg, x, y, w, h, rotate: rotation },
8942
+ });
8943
+ } else {
8944
+ const shapeOpts = {
8945
+ x,
8946
+ y,
8947
+ w,
8948
+ h,
8949
+ rotate: rotation,
8950
+ fill: useSolidFill
8951
+ ? { color: bgColorObj.hex, transparency: transparency }
8952
+ : { type: 'none' },
8953
+ line: hasUniformBorder ? borderInfo.options : null,
8954
+ };
8955
+
8956
+ if (hasShadow) shapeOpts.shadow = getVisibleShadow(shadowStr, config.scale);
8957
+
8958
+ const borderRadius = parseFloat(style.borderRadius) || 0;
8959
+ const aspectRatio = Math.max(widthPx, heightPx) / Math.min(widthPx, heightPx);
8960
+ const isCircle = aspectRatio < 1.1 && borderRadius >= Math.min(widthPx, heightPx) / 2 - 1;
8961
+
8962
+ let shapeType = pptx.ShapeType.rect;
8963
+ if (isCircle) shapeType = pptx.ShapeType.ellipse;
8964
+ else if (borderRadius > 0) {
8965
+ shapeType = pptx.ShapeType.roundRect;
8966
+ shapeOpts.rectRadius = Math.min(0.5, borderRadius / Math.min(widthPx, heightPx));
8967
+ }
8968
+
8969
+ if (textPayload) {
8970
+ const textOptions = {
8971
+ shape: shapeType,
8972
+ ...shapeOpts,
8973
+ align: textPayload.align,
8974
+ valign: textPayload.valign,
8975
+ inset: textPayload.inset,
8976
+ margin: 0,
8977
+ wrap: true,
8978
+ autoFit: false,
8979
+ };
8980
+ items.push({
8981
+ type: 'text',
8982
+ zIndex,
8983
+ domOrder,
8984
+ textParts: textPayload.text,
8985
+ options: textOptions,
8986
+ });
8987
+ } else if (!hasPartialBorderRadius) {
8988
+ items.push({
8989
+ type: 'shape',
8990
+ zIndex,
8991
+ domOrder,
8992
+ shapeType,
8993
+ options: shapeOpts,
8994
+ });
8995
+ }
8996
+ }
8997
+
8998
+ if (hasCompositeBorder) {
8999
+ const borderSvgData = generateCompositeBorderSVG(
9000
+ widthPx,
9001
+ heightPx,
9002
+ borderRadiusValue,
9003
+ borderInfo.sides
9004
+ );
9005
+ if (borderSvgData) {
9006
+ items.push({
9007
+ type: 'image',
9008
+ zIndex: zIndex + 1,
9009
+ domOrder,
9010
+ options: { data: borderSvgData, x, y, w, h, rotate: rotation },
9011
+ });
9012
+ }
9013
+ }
9014
+ }
9015
+
9016
+ return { items, stopRecursion: !!textPayload };
9017
+ }
9018
+
9019
+ function createCompositeBorderItems(sides, x, y, w, h, scale, zIndex, domOrder) {
9020
+ const items = [];
9021
+ const pxToInch = 1 / 96;
9022
+ const common = { zIndex: zIndex + 1, domOrder, shapeType: 'rect' };
9023
+
9024
+ if (sides.top.width > 0)
9025
+ items.push({
9026
+ ...common,
9027
+ options: { x, y, w, h: sides.top.width * pxToInch * scale, fill: { color: sides.top.color } },
9028
+ });
9029
+ if (sides.right.width > 0)
9030
+ items.push({
9031
+ ...common,
9032
+ options: {
9033
+ x: x + w - sides.right.width * pxToInch * scale,
9034
+ y,
9035
+ w: sides.right.width * pxToInch * scale,
9036
+ h,
9037
+ fill: { color: sides.right.color },
9038
+ },
9039
+ });
9040
+ if (sides.bottom.width > 0)
9041
+ items.push({
9042
+ ...common,
9043
+ options: {
9044
+ x,
9045
+ y: y + h - sides.bottom.width * pxToInch * scale,
9046
+ w,
9047
+ h: sides.bottom.width * pxToInch * scale,
9048
+ fill: { color: sides.bottom.color },
9049
+ },
9050
+ });
9051
+ if (sides.left.width > 0)
9052
+ items.push({
9053
+ ...common,
9054
+ options: {
9055
+ x,
9056
+ y,
9057
+ w: sides.left.width * pxToInch * scale,
9058
+ h,
9059
+ fill: { color: sides.left.color },
9060
+ },
9061
+ });
9062
+
9063
+ return items;
9043
9064
  }
9044
9065
 
9045
9066
  exports.exportToPptx = exportToPptx;