@dickpy/dsh-oil-slide 0.0.0-stage → 0.2.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.
Files changed (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +89 -2
  3. package/bundled-assets/brand/empty-deck-cobalt.webp +0 -0
  4. package/bundled-assets/brand/oil-slide-mark-512.png +0 -0
  5. package/bundled-assets/skill/LICENSE +21 -0
  6. package/bundled-assets/skill/README.md +61 -0
  7. package/bundled-assets/skill/SKILL.md +185 -0
  8. package/bundled-assets/skill/references/art-directions.md +99 -0
  9. package/bundled-assets/skill/references/brand-and-canvas.md +72 -0
  10. package/bundled-assets/skill/references/design-baseline.txt +1 -0
  11. package/bundled-assets/skill/references/prompt-compiler.md +99 -0
  12. package/bundled-assets/skill/references/restore-and-pptx.md +178 -0
  13. package/bundled-assets/skill/references/staged-pptx.md +71 -0
  14. package/bundled-assets/skill/scripts/_runtime_utils.py +351 -0
  15. package/bundled-assets/skill/scripts/asset_sheet.py +528 -0
  16. package/bundled-assets/skill/scripts/brand_ref.py +776 -0
  17. package/bundled-assets/skill/scripts/crop_asset.py +1709 -0
  18. package/bundled-assets/skill/scripts/deck_shell.py +2322 -0
  19. package/bundled-assets/skill/scripts/export_images.py +280 -0
  20. package/bundled-assets/skill/scripts/extract_pptx.py +360 -0
  21. package/bundled-assets/skill/scripts/extract_slide_dom.js +1640 -0
  22. package/bundled-assets/skill/scripts/html_to_pptx.py +4042 -0
  23. package/bundled-assets/skill/scripts/measure_slide_layout.py +952 -0
  24. package/bundled-assets/skill/scripts/oiloil-connect.mjs +1493 -0
  25. package/bundled-assets/skill/scripts/oiloil-image.mjs +492 -0
  26. package/bundled-assets/skill/scripts/prepare_plate.py +412 -0
  27. package/bundled-assets/skill/scripts/render_gradient.py +258 -0
  28. package/bundled-assets/skill/scripts/slide_brand.py +111 -0
  29. package/bundled-assets/skill/scripts/slide_incremental.py +122 -0
  30. package/bundled-assets/skill/scripts/slide_workflow.py +711 -0
  31. package/bundled-assets/skill/scripts/verify_deck.py +3483 -0
  32. package/bundled-assets/skill/vendor/README.md +4 -0
  33. package/bundled-assets/skill/vendor/codex-ppt/LICENSE +21 -0
  34. package/bundled-assets/skill/vendor/codex-ppt/assemble_ppt.py +435 -0
  35. package/bundled-assets/skill/vendor/codex-ppt/codex_ppt_runtime.py +284 -0
  36. package/bundled-assets/skill/vendor/codex-ppt/image_gen.py +972 -0
  37. package/bundled-assets/skill/vendor/codex-ppt/image_providers/__init__.py +6 -0
  38. package/bundled-assets/skill/vendor/codex-ppt/image_providers/atlascloud.py +222 -0
  39. package/bundled-assets/skill/vendor/codex-ppt/image_providers/base.py +32 -0
  40. package/bundled-assets/skill/vendor/codex-ppt/image_providers/factory.py +21 -0
  41. package/bundled-assets/skill/vendor/codex-ppt/image_providers/openai_compatible.py +238 -0
  42. package/bundled-assets/skill/vendor/codex-ppt/requirements.txt +5 -0
  43. package/bundled-assets/skill/vendor/codex-ppt/styles//345/205/232/346/224/277/347/272/242/351/243/216/346/240/274.md +98 -0
  44. package/bundled-assets/skill/vendor/codex-ppt/styles//345/210/233/346/204/217/346/235/202/345/277/227/351/243/216.md +72 -0
  45. package/bundled-assets/skill/vendor/codex-ppt/styles//345/244/215/345/217/244/346/211/201/345/271/263/346/217/222/347/224/273/351/243/216.md +72 -0
  46. package/bundled-assets/skill/vendor/codex-ppt/styles//346/211/213/347/273/230/346/212/200/346/234/257/350/247/243/351/207/212/351/243/216.md +86 -0
  47. package/bundled-assets/skill/vendor/codex-ppt/styles//346/211/213/347/273/230/347/231/275/346/235/277/351/243/216.md +72 -0
  48. package/bundled-assets/skill/vendor/codex-ppt/styles//346/225/231/345/255/246/350/257/276/344/273/266/351/243/216.md +107 -0
  49. package/bundled-assets/skill/vendor/codex-ppt/styles//346/225/260/346/215/256/344/273/252/350/241/250/347/233/230/351/243/216.md +73 -0
  50. package/bundled-assets/skill/vendor/codex-ppt/styles//346/270/205/347/210/275/344/270/223/344/270/232/351/243/216.md +87 -0
  51. package/bundled-assets/skill/vendor/codex-ppt/styles//346/270/251/346/232/226/346/211/213/345/267/245/351/243/216.md +72 -0
  52. package/bundled-assets/skill/vendor/codex-ppt/styles//347/224/265/345/255/220/345/242/250/346/260/264/346/235/202/345/277/227/351/243/216.md +86 -0
  53. package/bundled-assets/skill/vendor/codex-ppt/styles//347/247/221/347/240/224/347/255/224/350/276/251/351/243/216.md +138 -0
  54. package/bundled-assets/skill/vendor/codex-ppt/styles//351/272/246/350/202/257/351/224/241/351/243/216/346/240/274.md +119 -0
  55. package/bundled-assets/skill/vendor/image-to-editable-ppt/LICENSE +21 -0
  56. package/bundled-assets/skill/vendor/image-to-editable-ppt/SKILL.md +171 -0
  57. package/bundled-assets/skill/vendor/image-to-editable-ppt/agents/openai.yaml +4 -0
  58. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/__init__.py +1 -0
  59. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/cli.py +25 -0
  60. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/__init__.py +1 -0
  61. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/_input_normalization.py +355 -0
  62. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/_page_artifacts.py +132 -0
  63. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/build_pptx_from_manifest.py +1029 -0
  64. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/configure_image_backend.py +52 -0
  65. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/deck_run_state.py +214 -0
  66. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/deck_text_hints.py +179 -0
  67. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/finalize_deck_run.py +96 -0
  68. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/formula_renderer.py +254 -0
  69. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/image_gen.py +206 -0
  70. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/main.py +795 -0
  71. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/make_page_contact_sheet.py +28 -0
  72. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/native_tables.py +140 -0
  73. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/paddle_text_hints.py +212 -0
  74. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/page_job_status.py +54 -0
  75. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/page_text_metrics.py +334 -0
  76. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/path_geometry.py +174 -0
  77. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/prepare_deck_run.py +209 -0
  78. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/process_asset_sheet.py +104 -0
  79. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/record_imagegen_result.py +118 -0
  80. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/record_page_dispatch.py +83 -0
  81. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/record_page_result.py +130 -0
  82. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/remove_chroma_key.py +441 -0
  83. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/reset_page_job.py +74 -0
  84. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/runtime_env.py +283 -0
  85. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/split_alpha_components.py +323 -0
  86. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/text_hints.py +309 -0
  87. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/editppt/runtime/validate_pptx.py +873 -0
  88. package/bundled-assets/skill/vendor/image-to-editable-ppt/cli/pyproject.toml +29 -0
  89. package/bundled-assets/skill/vendor/image-to-editable-ppt/prompts/page-worker.md +47 -0
  90. package/bundled-assets/skill/vendor/image-to-editable-ppt/references/cli-helper.md +282 -0
  91. package/bundled-assets/skill/vendor/image-to-editable-ppt/references/manifest-schema.md +423 -0
  92. package/bundled-assets/skill/vendor/image-to-editable-ppt/references/page-decision-tree.md +301 -0
  93. package/bundled-assets/skill/vendor/image-to-editable-ppt/scripts/build-page-worker-prompt.py +134 -0
  94. package/cordis.patch.yml +3 -0
  95. package/lib/client.js +11282 -0
  96. package/lib/index.js +2638 -0
  97. package/package.json +140 -4
@@ -0,0 +1,1640 @@
1
+ /**
2
+ * Universal Slide DOM & Geometry Extractor for Oil Slide HTML -> PPTX compilation.
3
+ *
4
+ * Single source of truth used by both:
5
+ * 1. Playwright headless measurement in `oil-slide/scripts/html_to_pptx.py`
6
+ * 2. In-browser iframe measurement in the Oil Slide Studio editor
7
+ *
8
+ * Accepts `[slideIdx, canvasW, canvasH, optionalDocument]` or `{ slideIdx, canvasW, canvasH, document }`
9
+ * or a plain `slideIdx` number, and returns a single slide manifest entry with paint-ordered
10
+ * `items` (`box`, `table`, `transformed_visual`, `gradient_box`, `svg`, `img`, `text`) and
11
+ * `captureRequests`.
12
+ */
13
+ (function () {
14
+ function extractSlideDom(arg) {
15
+ const isObjArg = Boolean(arg && typeof arg === 'object' && !Array.isArray(arg));
16
+ const slideIdx = Array.isArray(arg) ? arg[0] : (isObjArg ? arg.slideIdx : arg);
17
+ const customDoc =
18
+ (Array.isArray(arg) && arg[3] && arg[3].nodeType === 9)
19
+ ? arg[3]
20
+ : (isObjArg && arg.document && arg.document.nodeType === 9 ? arg.document : null);
21
+ const document = customDoc || globalThis.document;
22
+ const win = (document && document.defaultView) || globalThis;
23
+ const getComputedStyle = (el, pseudo) => win.getComputedStyle(el, pseudo);
24
+ const NodeFilter = win.NodeFilter || { SHOW_ELEMENT: 1, SHOW_TEXT: 4 };
25
+
26
+ const stage =
27
+ document.querySelector('.deck-stage') ||
28
+ document.querySelector('[data-stage]') ||
29
+ document.querySelector('#deck-stage') ||
30
+ document.querySelector('.stage') ||
31
+ document.body;
32
+
33
+ const slide =
34
+ document.querySelector(`.slide[data-slide="${slideIdx}"]`) ||
35
+ document.querySelector(`[data-slide="${slideIdx}"]`) ||
36
+ document.querySelector('.slide.is-active, .slide.active') ||
37
+ stage;
38
+
39
+ const stageRect = slide.getBoundingClientRect();
40
+ const rawW = Array.isArray(arg) ? arg[1] : (isObjArg ? arg.canvasW : 0);
41
+ const rawH = Array.isArray(arg) ? arg[2] : (isObjArg ? arg.canvasH : 0);
42
+ const canvasW = (rawW > 0) ? Number(rawW) : (slide.offsetWidth || stage.offsetWidth || 1920.0);
43
+ const canvasH = (rawH > 0) ? Number(rawH) : (slide.offsetHeight || stage.offsetHeight || 1080.0);
44
+ const scaleX = stageRect.width > 0 ? (canvasW / stageRect.width) : 1.0;
45
+ const scaleY = stageRect.height > 0 ? (canvasH / stageRect.height) : 1.0;
46
+
47
+ const toStageRect = (r) => ({
48
+ x: (r.left - stageRect.left) * scaleX,
49
+ y: (r.top - stageRect.top) * scaleY,
50
+ w: r.width * scaleX,
51
+ h: r.height * scaleY,
52
+ });
53
+
54
+ const isVisibleRect = (r) => {
55
+ if (r.w <= 0.25 || r.h <= 0.25) return false;
56
+ // Allow elements bleeding off-edge as long as they intersect [0..canvasW, 0..canvasH]
57
+ if (r.x + r.w <= 0.5 || r.x >= canvasW - 0.5 || r.y + r.h <= 0.5 || r.y >= canvasH - 0.5) return false;
58
+ return true;
59
+ };
60
+
61
+ const parseAlpha = (col) => {
62
+ if (!col || col === 'transparent') return 0;
63
+ const m = col.match(/rgba?\(\s*[\d.]+\s*[,\s]\s*[\d.]+\s*[,\s]\s*[\d.]+\s*(?:[,/]\s*([\d.]+%?)\s*)?\)/i);
64
+ if (!m) return 1;
65
+ if (m[1] === undefined) return 1;
66
+ if (m[1].endsWith('%')) return parseFloat(m[1]) / 100.0;
67
+ return parseFloat(m[1]);
68
+ };
69
+
70
+ const parseRadiusPx = (cs, w, h) => {
71
+ const vals = [
72
+ cs.borderTopLeftRadius,
73
+ cs.borderTopRightRadius,
74
+ cs.borderBottomRightRadius,
75
+ cs.borderBottomLeftRadius,
76
+ ].map(v => {
77
+ if (!v) return 0;
78
+ const token = v.split(/\s+/)[0];
79
+ if (token.endsWith('%')) {
80
+ return (parseFloat(token) / 100.0) * Math.min(w, h);
81
+ }
82
+ return parseFloat(token) || 0;
83
+ });
84
+ return Math.max(...vals);
85
+ };
86
+
87
+ const isControlOrIgnored = (el) => {
88
+ if (!el || el.nodeType !== 1) return true;
89
+ const tag = el.tagName.toUpperCase();
90
+ if (tag === 'SCRIPT' || tag === 'STYLE' || tag === 'NOSCRIPT' || tag === 'LINK' || tag === 'META') return true;
91
+ if (el.classList && (el.classList.contains('deck-controls') || el.classList.contains('controls'))) return true;
92
+ if (el.hasAttribute('data-export-ignore')) return true;
93
+ return false;
94
+ };
95
+
96
+ // Pre-index element document order for true CSS stacking-context paint sorting
97
+ const elDocOrder = new WeakMap();
98
+ let docOrderSeq = 0;
99
+ const indexDocOrder = (root) => {
100
+ if (!root) return;
101
+ elDocOrder.set(root, ++docOrderSeq);
102
+ const kids = root.children;
103
+ if (!kids) return;
104
+ for (let i = 0; i < kids.length; i++) {
105
+ indexDocOrder(kids[i]);
106
+ }
107
+ };
108
+ indexDocOrder(slide);
109
+
110
+ const isRepresentableColor = (col) => {
111
+ if (!col) return false;
112
+ const s = col.trim();
113
+ if (s === 'transparent') return true;
114
+ return /^(?:rgba?\(\s*[\d.]+\s*[,\s]\s*[\d.]+\s*[,\s]\s*[\d.]+\s*(?:[,/]\s*[\d.]+%?\s*)?\)|#[0-9a-f]{3,8})$/i.test(s);
115
+ };
116
+
117
+ const parseTransformInfo = (cs) => {
118
+ const t = cs.transform;
119
+ const indRotate = cs.rotate && cs.rotate !== 'none' ? cs.rotate : '';
120
+ const indScale = cs.scale && cs.scale !== 'none' ? cs.scale : '';
121
+ const indTranslate = cs.translate && cs.translate !== 'none' ? cs.translate : '';
122
+ const hasIndividualTransform = Boolean(indRotate || indScale || indTranslate);
123
+ let a = 1, b = 0, c = 0, d = 1;
124
+ if (t && t !== 'none') {
125
+ const m2 = t.match(/^matrix\(([^)]+)\)$/);
126
+ if (m2) {
127
+ const p = m2[1].split(',').map(Number);
128
+ if (p.length === 6 && p.every(Number.isFinite)) {
129
+ [a, b, c, d] = p;
130
+ }
131
+ } else {
132
+ const m3 = t.match(/^matrix3d\(([^)]+)\)$/);
133
+ if (m3) {
134
+ const p = m3[1].split(',').map(Number);
135
+ if (p.length === 16 && p.every(Number.isFinite)) {
136
+ a = p[0]; b = p[1]; c = p[4]; d = p[5];
137
+ }
138
+ }
139
+ }
140
+ }
141
+ const rotationDeg = Math.atan2(b, a) * (180.0 / Math.PI);
142
+ const dot = Math.abs(a * c + b * d);
143
+ const len1 = Math.hypot(a, b);
144
+ const len2 = Math.hypot(c, d);
145
+ const normDot = (len1 > 1e-6 && len2 > 1e-6) ? (dot / (len1 * len2)) : dot;
146
+ const hasSkew = (dot > 0.02) || (normDot > 0.02);
147
+ const hasIndividualRotScale = Boolean(indRotate || indScale);
148
+ const hasTransform = (Math.abs(rotationDeg) > 0.5) || hasSkew || hasIndividualRotScale;
149
+ const hasAnyCssTransform = Boolean((t && t !== 'none') || hasIndividualTransform);
150
+ const maxLen = Math.max(len1, len2);
151
+ const isUniformScale = !indScale && maxLen > 1e-6 && Math.abs(len1 - len2) <= 0.05 * maxLen;
152
+ return {
153
+ rotationDeg,
154
+ hasSkew,
155
+ hasTransform,
156
+ hasAnyCssTransform,
157
+ hasIndividualTransform,
158
+ len1,
159
+ len2,
160
+ isUniformScale,
161
+ };
162
+ };
163
+
164
+ const hasTransformedAncestor = (el) => {
165
+ let cur = el.parentElement;
166
+ while (cur && cur !== slide) {
167
+ if (parseTransformInfo(getComputedStyle(cur)).hasAnyCssTransform) {
168
+ return true;
169
+ }
170
+ cur = cur.parentElement;
171
+ }
172
+ return false;
173
+ };
174
+
175
+ const createsAutoStackingContext = (cs) => {
176
+ if (parseTransformInfo(cs).hasAnyCssTransform) return true;
177
+ if (parseFloat(cs.opacity || '1') < 0.999) return true;
178
+ if (cs.filter && cs.filter !== 'none') return true;
179
+ if (cs.clipPath && cs.clipPath !== 'none') return true;
180
+ if ((cs.maskImage && cs.maskImage !== 'none') || (cs.webkitMaskImage && cs.webkitMaskImage !== 'none')) return true;
181
+ if (cs.isolation === 'isolate') return true;
182
+ if (cs.mixBlendMode && cs.mixBlendMode !== 'normal') return true;
183
+ return false;
184
+ };
185
+
186
+ const isFlexOrGridChild = (node) => {
187
+ const parent = node && node.parentElement;
188
+ if (!parent) return false;
189
+ const d = getComputedStyle(parent).display;
190
+ return d === 'flex' || d === 'inline-flex' || d === 'grid' || d === 'inline-grid';
191
+ };
192
+
193
+ // Compute CSS stacking context chain, positioned-subtree anchor, and integer stackPath for true paint ordering
194
+ const getStackingInfo = (el) => {
195
+ const scChain = [];
196
+ let cur = el;
197
+ let isSelfSc = false;
198
+ let posAnchorOrder = 0;
199
+ let hitSc = false;
200
+ while (cur && cur !== slide && cur !== document.documentElement) {
201
+ const cs = getComputedStyle(cur);
202
+ const pos = cs.position;
203
+ const zi = cs.zIndex;
204
+ let pushed = false;
205
+ if ((pos !== 'static' || isFlexOrGridChild(cur)) && zi !== 'auto') {
206
+ const z = parseInt(zi, 10);
207
+ if (!Number.isNaN(z)) {
208
+ scChain.push({ z, order: elDocOrder.get(cur) || 0 });
209
+ pushed = true;
210
+ }
211
+ }
212
+ if (!pushed && createsAutoStackingContext(cs)) {
213
+ scChain.push({ z: 0, order: elDocOrder.get(cur) || 0 });
214
+ pushed = true;
215
+ }
216
+ if (pushed) {
217
+ if (cur === el) isSelfSc = true;
218
+ hitSc = true;
219
+ } else if (!hitSc && posAnchorOrder === 0 && pos !== 'static') {
220
+ posAnchorOrder = elDocOrder.get(cur) || 0;
221
+ }
222
+ cur = cur.parentElement;
223
+ }
224
+ scChain.reverse();
225
+ return {
226
+ stackPath: scChain.length ? scChain.map((s) => s.z) : [0],
227
+ scChain,
228
+ elOrder: elDocOrder.get(el) || 0,
229
+ isSelfSc,
230
+ posAnchorOrder,
231
+ };
232
+ };
233
+
234
+ const getEffectiveOpacity = (el) => {
235
+ let op = 1.0;
236
+ let cur = el;
237
+ while (cur && cur !== slide.parentElement) {
238
+ const cs = getComputedStyle(cur);
239
+ op *= parseFloat(cs.opacity || '1');
240
+ if (cur === slide) break;
241
+ cur = cur.parentElement;
242
+ }
243
+ return op;
244
+ };
245
+
246
+ const isHiddenTree = (el) => {
247
+ let cur = el;
248
+ while (cur && cur !== slide.parentElement) {
249
+ const cs = getComputedStyle(cur);
250
+ if (cs.display === 'none' || cs.visibility === 'hidden' || parseFloat(cs.opacity || '1') <= 0.01) {
251
+ return true;
252
+ }
253
+ if (cur === slide) break;
254
+ cur = cur.parentElement;
255
+ }
256
+ return false;
257
+ };
258
+
259
+ const slideCs = getComputedStyle(slide);
260
+ const bodyCs = getComputedStyle(document.body);
261
+ const slideBgColor = parseAlpha(slideCs.backgroundColor) > 0.01
262
+ ? slideCs.backgroundColor
263
+ : (parseAlpha(bodyCs.backgroundColor) > 0.01 ? bodyCs.backgroundColor : 'rgb(255, 255, 255)');
264
+ const slideBgImageCss = (slideCs.backgroundImage && slideCs.backgroundImage !== 'none') ? slideCs.backgroundImage : '';
265
+ const slideHasBgImage = Boolean(slideBgImageCss);
266
+
267
+ const motionPreset = ((document.body.getAttribute('data-motion') || 'none').trim().toLowerCase());
268
+ const slideTransition = ((slide.getAttribute('data-transition') || 'auto').trim().toLowerCase());
269
+ const rawSlideTransDur = parseInt(slide.getAttribute('data-transition-dur') || '', 10);
270
+ const slideTransitionDur = Number.isFinite(rawSlideTransDur) ? Math.max(150, Math.min(2000, rawSlideTransDur)) : null;
271
+
272
+ const getMotionMeta = (el) => {
273
+ if (!el || el.nodeType !== 1) return null;
274
+ let morphId = '';
275
+ let morphOwn = false;
276
+ let anim = '';
277
+ let animOrder = null;
278
+ let animDelay = null;
279
+ let animDur = null;
280
+ let animTrigger = 'auto';
281
+ let isHeader = false;
282
+ let cur = el;
283
+ while (cur && cur !== slide) {
284
+ const tag = (cur.tagName || '').toUpperCase();
285
+ if (tag === 'HEADER' || (cur.classList && cur.classList.contains('slide-header'))) {
286
+ isHeader = true;
287
+ }
288
+ if (!morphId && cur.hasAttribute && cur.hasAttribute('data-morph-id')) {
289
+ const mid = (cur.getAttribute('data-morph-id') || '').trim();
290
+ if (mid) {
291
+ morphId = mid;
292
+ morphOwn = (cur === el);
293
+ }
294
+ }
295
+ if (!anim && cur.hasAttribute && cur.hasAttribute('data-anim')) {
296
+ const a = (cur.getAttribute('data-anim') || '').trim().toLowerCase();
297
+ if (a) {
298
+ anim = a;
299
+ const ord = parseInt(cur.getAttribute('data-anim-order') || '', 10);
300
+ if (Number.isNaN(ord) === false) animOrder = Math.max(1, ord);
301
+ const dly = parseInt(cur.getAttribute('data-anim-delay') || '', 10);
302
+ if (Number.isNaN(dly) === false) animDelay = Math.max(0, dly);
303
+ const dur = parseInt(cur.getAttribute('data-anim-dur') || '', 10);
304
+ if (Number.isNaN(dur) === false) animDur = Math.max(120, Math.min(1800, dur));
305
+ const trig = (cur.getAttribute('data-anim-trigger') || 'auto').trim().toLowerCase();
306
+ if (trig === 'click') animTrigger = 'click';
307
+ }
308
+ }
309
+ cur = cur.parentElement;
310
+ }
311
+ if (!morphId && !anim && !isHeader) return null;
312
+ return { morphId, morphOwn, anim, animOrder, animDelay, animDur, animTrigger, isHeader };
313
+ };
314
+
315
+ // Assign deterministic capture IDs for elements that need Playwright screenshot capture
316
+ let captureCounter = 0;
317
+ const tagForCapture = (el) => {
318
+ const id = `h2p-${slideIdx}-${++captureCounter}`;
319
+ el.setAttribute('data-h2p-id', id);
320
+ return id;
321
+ };
322
+
323
+ const items = [];
324
+ const itemStackInfo = new WeakMap();
325
+ const pushItem = (item, stackInfo) => {
326
+ itemStackInfo.set(item, stackInfo);
327
+ items.push(item);
328
+ };
329
+ const droppedImages = [];
330
+ let domOrder = 0;
331
+ let fullBleedImgCount = 0;
332
+
333
+ const isClippedByAncestor = (el, r) => {
334
+ let cur = el.parentElement;
335
+ while (cur && cur !== slide) {
336
+ const cs = getComputedStyle(cur);
337
+ if (cs.overflow === 'hidden' || cs.overflowX === 'hidden' || cs.overflowY === 'hidden' || cs.overflow === 'clip') {
338
+ const pr = toStageRect(cur.getBoundingClientRect());
339
+ if (r.x < pr.x - 1 || r.y < pr.y - 1 || r.x + r.w > pr.x + pr.w + 1 || r.y + r.h > pr.y + pr.h + 1) {
340
+ return true;
341
+ }
342
+ if (parseRadiusPx(cs, pr.w, pr.h) >= 2.0) {
343
+ // Rounded parent with overflow:hidden may clip image corners
344
+ if (Math.abs(r.x - pr.x) < 4 || Math.abs(r.y - pr.y) < 4 || Math.abs((r.x + r.w) - (pr.x + pr.w)) < 4 || Math.abs((r.y + r.h) - (pr.y + pr.h)) < 4) {
345
+ return true;
346
+ }
347
+ }
348
+ }
349
+ cur = cur.parentElement;
350
+ }
351
+ return false;
352
+ };
353
+
354
+ const isOverflowClipVal = (v) => v === 'hidden' || v === 'clip' || v === 'scroll' || v === 'auto';
355
+
356
+ const getAncestorPaddingBoxStage = (anc, acs) => {
357
+ const prStage = toStageRect(anc.getBoundingClientRect());
358
+ if (parseTransformInfo(acs).hasTransform) {
359
+ return {
360
+ left: prStage.x,
361
+ right: prStage.x + prStage.w,
362
+ top: prStage.y,
363
+ bottom: prStage.y + prStage.h,
364
+ w: prStage.w,
365
+ h: prStage.h,
366
+ };
367
+ }
368
+ const bL = acs.borderLeftStyle !== 'none' ? (parseFloat(acs.borderLeftWidth) || 0) : 0;
369
+ const bR = acs.borderRightStyle !== 'none' ? (parseFloat(acs.borderRightWidth) || 0) : 0;
370
+ const bT = acs.borderTopStyle !== 'none' ? (parseFloat(acs.borderTopWidth) || 0) : 0;
371
+ const bB = acs.borderBottomStyle !== 'none' ? (parseFloat(acs.borderBottomWidth) || 0) : 0;
372
+ return {
373
+ left: prStage.x + bL,
374
+ right: prStage.x + prStage.w - bR,
375
+ top: prStage.y + bT,
376
+ bottom: prStage.y + prStage.h - bB,
377
+ w: prStage.w,
378
+ h: prStage.h,
379
+ };
380
+ };
381
+
382
+ const isDiamondClippedByAncestor = (el, rawRect) => {
383
+ const dStage = toStageRect(rawRect);
384
+ let cur = el.parentElement;
385
+ while (cur && cur !== slide) {
386
+ const acs = getComputedStyle(cur);
387
+ if (
388
+ (acs.clipPath && acs.clipPath !== 'none') ||
389
+ (acs.maskImage && acs.maskImage !== 'none') ||
390
+ (acs.webkitMaskImage && acs.webkitMaskImage !== 'none')
391
+ ) {
392
+ return true;
393
+ }
394
+ const clipsX = isOverflowClipVal(acs.overflowX) || isOverflowClipVal(acs.overflow);
395
+ const clipsY = isOverflowClipVal(acs.overflowY) || isOverflowClipVal(acs.overflow);
396
+ if (clipsX || clipsY) {
397
+ const pb = getAncestorPaddingBoxStage(cur, acs);
398
+ if (
399
+ (clipsX && (dStage.x < pb.left - 0.5 || dStage.x + dStage.w > pb.right + 0.5)) ||
400
+ (clipsY && (dStage.y < pb.top - 0.5 || dStage.y + dStage.h > pb.bottom + 0.5))
401
+ ) {
402
+ return true;
403
+ }
404
+ if (parseRadiusPx(acs, pb.w, pb.h) >= 2.0) {
405
+ return true;
406
+ }
407
+ }
408
+ cur = cur.parentElement;
409
+ }
410
+ return false;
411
+ };
412
+
413
+ const establishesFixedCB = (acs) => (
414
+ parseTransformInfo(acs).hasAnyCssTransform ||
415
+ (acs.filter && acs.filter !== 'none') ||
416
+ (acs.backdropFilter && acs.backdropFilter !== 'none') ||
417
+ (acs.webkitBackdropFilter && acs.webkitBackdropFilter !== 'none') ||
418
+ (acs.perspective && acs.perspective !== 'none')
419
+ );
420
+
421
+ const establishesAbsCB = (acs) => acs.position !== 'static' || establishesFixedCB(acs);
422
+
423
+ const computeAtomicCaptureRect = (el, baseRect) => {
424
+ const ecs = getComputedStyle(el);
425
+ const elClipsX = isOverflowClipVal(ecs.overflowX) || isOverflowClipVal(ecs.overflow);
426
+ const elClipsY = isOverflowClipVal(ecs.overflowY) || isOverflowClipVal(ecs.overflow);
427
+ if (elClipsX && elClipsY) {
428
+ return { rect: baseRect, captureClip: false };
429
+ }
430
+ let minX = baseRect.x;
431
+ let minY = baseRect.y;
432
+ let maxX = baseRect.x + baseRect.w;
433
+ let maxY = baseRect.y + baseRect.h;
434
+ for (const desc of Array.from(el.querySelectorAll('*'))) {
435
+ if (isControlOrIgnored(desc)) continue;
436
+ const dcs = getComputedStyle(desc);
437
+ if (dcs.display === 'none' || dcs.visibility === 'hidden') continue;
438
+ if (getEffectiveOpacity(desc) <= 0.01) continue;
439
+ const dr = toStageRect(desc.getBoundingClientRect());
440
+ if (dr.w <= 0.5 || dr.h <= 0.5) continue;
441
+ let x1 = dr.x;
442
+ let y1 = dr.y;
443
+ let x2 = dr.x + dr.w;
444
+ let y2 = dr.y + dr.h;
445
+ let curPos = dcs.position;
446
+ let anc = desc.parentElement;
447
+ while (anc) {
448
+ const acs = getComputedStyle(anc);
449
+ let skipsAnc = false;
450
+ if (curPos === 'fixed') {
451
+ if (!establishesFixedCB(acs)) skipsAnc = true;
452
+ } else if (curPos === 'absolute') {
453
+ if (!establishesAbsCB(acs)) skipsAnc = true;
454
+ }
455
+ if (!skipsAnc) {
456
+ const cX = isOverflowClipVal(acs.overflowX) || isOverflowClipVal(acs.overflow);
457
+ const cY = isOverflowClipVal(acs.overflowY) || isOverflowClipVal(acs.overflow);
458
+ if (cX || cY) {
459
+ const pb = getAncestorPaddingBoxStage(anc, acs);
460
+ if (cX) {
461
+ x1 = Math.max(x1, pb.left);
462
+ x2 = Math.min(x2, pb.right);
463
+ }
464
+ if (cY) {
465
+ y1 = Math.max(y1, pb.top);
466
+ y2 = Math.min(y2, pb.bottom);
467
+ }
468
+ }
469
+ curPos = acs.position;
470
+ }
471
+ if (anc === el) break;
472
+ anc = anc.parentElement;
473
+ }
474
+ if (x2 - x1 > 0.5 && y2 - y1 > 0.5) {
475
+ minX = Math.min(minX, x1);
476
+ minY = Math.min(minY, y1);
477
+ maxX = Math.max(maxX, x2);
478
+ maxY = Math.max(maxY, y2);
479
+ }
480
+ }
481
+ const captureClip = (
482
+ minX < baseRect.x - 0.5 ||
483
+ minY < baseRect.y - 0.5 ||
484
+ maxX > baseRect.x + baseRect.w + 0.5 ||
485
+ maxY > baseRect.y + baseRect.h + 0.5
486
+ );
487
+ return {
488
+ rect: { x: minX, y: minY, w: maxX - minX, h: maxY - minY },
489
+ captureClip,
490
+ };
491
+ };
492
+
493
+ // Walk elements for shapes, images, svgs, gradients, pseudo-elements
494
+ const walkElements = (root) => {
495
+ const children = Array.from(root.children || []);
496
+ for (const el of children) {
497
+ if (isControlOrIgnored(el)) continue;
498
+ const cs = getComputedStyle(el);
499
+ if (cs.display === 'none' || cs.visibility === 'hidden') continue;
500
+ const effOpacity = getEffectiveOpacity(el);
501
+ if (effOpacity <= 0.01) continue;
502
+
503
+ const tag = el.tagName.toUpperCase();
504
+ const rawRect = el.getBoundingClientRect();
505
+ const r = toStageRect(rawRect);
506
+ const stackInfo = getStackingInfo(el);
507
+ const stackPath = stackInfo.stackPath;
508
+ const motion = getMotionMeta(el);
509
+
510
+ if (tag === 'SVG') {
511
+ if (isVisibleRect(r)) {
512
+ const cid = tagForCapture(el);
513
+ pushItem({
514
+ kind: 'svg',
515
+ captureId: cid,
516
+ rect: r,
517
+ stackPath,
518
+ domOrder: ++domOrder,
519
+ motion,
520
+ }, stackInfo);
521
+ }
522
+ // Do not descend into SVG internals
523
+ continue;
524
+ }
525
+
526
+ if (tag === 'TABLE' && isVisibleRect(r)) {
527
+ const hasNestedMedia = Boolean(el.querySelector('table, svg, img, canvas'));
528
+ const rowEls = Array.from(el.rows || []).filter((tr) => {
529
+ const trcs = getComputedStyle(tr);
530
+ const trr = toStageRect(tr.getBoundingClientRect());
531
+ return trcs.display !== 'none' && trcs.visibility !== 'hidden' && trr.h > 1;
532
+ });
533
+ if (!hasNestedMedia && rowEls.length >= 2) {
534
+ const gridRows = [];
535
+ let maxCols = 0;
536
+ for (const tr of rowEls) {
537
+ const trr = toStageRect(tr.getBoundingClientRect());
538
+ const trcs = getComputedStyle(tr);
539
+ const trBgAlpha = parseAlpha(trcs.backgroundColor) * effOpacity;
540
+ const cells = [];
541
+ let colSum = 0;
542
+ for (const td of Array.from(tr.cells || [])) {
543
+ const tdcs = getComputedStyle(td);
544
+ if (tdcs.display === 'none' || tdcs.visibility === 'hidden') continue;
545
+ const tdr = toStageRect(td.getBoundingClientRect());
546
+ const cSpan = Math.max(1, parseInt(td.getAttribute('colspan') || '1', 10) || 1);
547
+ const rSpan = Math.max(1, parseInt(td.getAttribute('rowspan') || '1', 10) || 1);
548
+ colSum += cSpan;
549
+ const tdEffOp = getEffectiveOpacity(td);
550
+ const tdBgAlpha = parseAlpha(tdcs.backgroundColor) * tdEffOp;
551
+ const useBgColor = tdBgAlpha > 0.01 ? tdcs.backgroundColor : (trBgAlpha > 0.01 ? trcs.backgroundColor : '');
552
+ const useBgAlpha = tdBgAlpha > 0.01 ? tdBgAlpha : trBgAlpha;
553
+ let fw = parseInt(tdcs.fontWeight, 10);
554
+ if (Number.isNaN(fw)) fw = tdcs.fontWeight === 'bold' ? 700 : 400;
555
+ let align = (tdcs.textAlign || 'left').toLowerCase();
556
+ if (align === 'start') align = 'left';
557
+ if (align === 'end') align = 'right';
558
+ cells.push({
559
+ text: (td.innerText || '').trim(),
560
+ isHeader: td.tagName.toUpperCase() === 'TH',
561
+ colSpan: cSpan,
562
+ rowSpan: rSpan,
563
+ rect: tdr,
564
+ backgroundColor: useBgColor,
565
+ bgAlpha: useBgAlpha,
566
+ color: tdcs.color,
567
+ opacity: tdEffOp,
568
+ fontFamily: tdcs.fontFamily,
569
+ fontSize: parseFloat(tdcs.fontSize) || 18,
570
+ fontWeight: fw,
571
+ fontStyle: tdcs.fontStyle,
572
+ letterSpacingPx: tdcs.letterSpacing === 'normal' ? 0.0 : (parseFloat(tdcs.letterSpacing) || 0.0),
573
+ textAlign: align,
574
+ verticalAlign: (tdcs.verticalAlign || 'middle').toLowerCase(),
575
+ padTop: parseFloat(tdcs.paddingTop) || 0,
576
+ padRight: parseFloat(tdcs.paddingRight) || 0,
577
+ padBottom: parseFloat(tdcs.paddingBottom) || 0,
578
+ padLeft: parseFloat(tdcs.paddingLeft) || 0,
579
+ borders: {
580
+ top: [tdcs.borderTopStyle !== 'none' ? (parseFloat(tdcs.borderTopWidth) || 0) : 0, tdcs.borderTopColor, tdcs.borderTopStyle],
581
+ right: [tdcs.borderRightStyle !== 'none' ? (parseFloat(tdcs.borderRightWidth) || 0) : 0, tdcs.borderRightColor, tdcs.borderRightStyle],
582
+ bottom: [tdcs.borderBottomStyle !== 'none' ? (parseFloat(tdcs.borderBottomWidth) || 0) : 0, tdcs.borderBottomColor, tdcs.borderBottomStyle],
583
+ left: [tdcs.borderLeftStyle !== 'none' ? (parseFloat(tdcs.borderLeftWidth) || 0) : 0, tdcs.borderLeftColor, tdcs.borderLeftStyle],
584
+ },
585
+ });
586
+ }
587
+ if (cells.length > 0) {
588
+ maxCols = Math.max(maxCols, colSum);
589
+ gridRows.push({ heightPx: trr.h, cells });
590
+ }
591
+ }
592
+ if (gridRows.length >= 2 && maxCols >= 2) {
593
+ el.setAttribute('data-h2p-native-table', '1');
594
+ pushItem({
595
+ kind: 'table',
596
+ rect: r,
597
+ nRows: gridRows.length,
598
+ nCols: maxCols,
599
+ rows: gridRows,
600
+ stackPath,
601
+ domOrder: ++domOrder,
602
+ motion,
603
+ }, stackInfo);
604
+ continue;
605
+ }
606
+ }
607
+ }
608
+
609
+ if (tag === 'IMG') {
610
+ if (isVisibleRect(r)) {
611
+ if (el.complete && el.naturalWidth > 0) {
612
+ const tf = parseTransformInfo(cs);
613
+ const radiusPx = parseRadiusPx(cs, r.w, r.h);
614
+ const bt = cs.borderTopStyle !== 'none' ? (parseFloat(cs.borderTopWidth) || 0) : 0;
615
+ const br = cs.borderRightStyle !== 'none' ? (parseFloat(cs.borderRightWidth) || 0) : 0;
616
+ const bb = cs.borderBottomStyle !== 'none' ? (parseFloat(cs.borderBottomWidth) || 0) : 0;
617
+ const bl = cs.borderLeftStyle !== 'none' ? (parseFloat(cs.borderLeftWidth) || 0) : 0;
618
+ const btc = cs.borderTopColor;
619
+ const brc = cs.borderRightColor;
620
+ const bbc = cs.borderBottomColor;
621
+ const blc = cs.borderLeftColor;
622
+ const hasAnyBorder = (
623
+ (bt > 0.1 && parseAlpha(btc) * effOpacity > 0.01) ||
624
+ (br > 0.1 && parseAlpha(brc) * effOpacity > 0.01) ||
625
+ (bb > 0.1 && parseAlpha(bbc) * effOpacity > 0.01) ||
626
+ (bl > 0.1 && parseAlpha(blc) * effOpacity > 0.01)
627
+ );
628
+ const contentW = Math.max(1, r.w - bl - br);
629
+ const contentH = Math.max(1, r.h - bt - bb);
630
+ const natRatio = el.naturalWidth / Math.max(1, el.naturalHeight);
631
+ const boxRatio = contentW / contentH;
632
+ const ratioMismatch = Math.abs(natRatio - boxRatio) / Math.max(natRatio, boxRatio) > 0.025;
633
+ const hasFilter = cs.filter && cs.filter !== 'none';
634
+ const hasClip = (cs.clipPath && cs.clipPath !== 'none') || isClippedByAncestor(el, r);
635
+ const needsCapture = Boolean(
636
+ tf.hasTransform ||
637
+ hasTransformedAncestor(el) ||
638
+ radiusPx >= 1.5 ||
639
+ ratioMismatch ||
640
+ hasFilter ||
641
+ hasClip ||
642
+ effOpacity < 0.99 ||
643
+ (cs.objectFit === 'cover' && ratioMismatch) ||
644
+ (r.x < -1 || r.y < -1 || r.x + r.w > canvasW + 1 || r.y + r.h > canvasH + 1)
645
+ );
646
+ const isFullBleedImg = (
647
+ r.x <= 2 && r.y <= 2 && r.w >= canvasW - 4 && r.h >= canvasH - 4
648
+ );
649
+ if (isFullBleedImg) fullBleedImgCount++;
650
+ const cid = (needsCapture || isFullBleedImg) ? tagForCapture(el) : null;
651
+ const imgItem = {
652
+ kind: 'img',
653
+ captureId: cid,
654
+ src: el.currentSrc || el.getAttribute('src') || '',
655
+ naturalWidth: el.naturalWidth,
656
+ naturalHeight: el.naturalHeight,
657
+ opacity: effOpacity,
658
+ rect: r,
659
+ needsCapture,
660
+ stackPath,
661
+ domOrder: ++domOrder,
662
+ motion,
663
+ };
664
+ if (hasAnyBorder) {
665
+ imgItem.borders = {
666
+ top: [bt, btc],
667
+ right: [br, brc],
668
+ bottom: [bb, bbc],
669
+ left: [bl, blc],
670
+ };
671
+ imgItem.borderStyles = {
672
+ top: cs.borderTopStyle || 'solid',
673
+ right: cs.borderRightStyle || 'solid',
674
+ bottom: cs.borderBottomStyle || 'solid',
675
+ left: cs.borderLeftStyle || 'solid',
676
+ };
677
+ }
678
+ pushItem(imgItem, stackInfo);
679
+ } else {
680
+ droppedImages.push({
681
+ src: el.currentSrc || el.getAttribute('src') || '',
682
+ complete: Boolean(el.complete),
683
+ naturalWidth: Number(el.naturalWidth || 0),
684
+ naturalHeight: Number(el.naturalHeight || 0),
685
+ rect: r,
686
+ });
687
+ }
688
+ }
689
+ continue;
690
+ }
691
+
692
+ // Check visual box: background-image, background-color, borders, and CSS transforms
693
+ if (isVisibleRect(r)) {
694
+ const tf = parseTransformInfo(cs);
695
+ const bgAlpha = parseAlpha(cs.backgroundColor) * effOpacity;
696
+ const hasBgImage = Boolean(cs.backgroundImage && cs.backgroundImage !== 'none');
697
+ const bt = cs.borderTopStyle !== 'none' ? (parseFloat(cs.borderTopWidth) || 0) : 0;
698
+ const br = cs.borderRightStyle !== 'none' ? (parseFloat(cs.borderRightWidth) || 0) : 0;
699
+ const bb = cs.borderBottomStyle !== 'none' ? (parseFloat(cs.borderBottomWidth) || 0) : 0;
700
+ const bl = cs.borderLeftStyle !== 'none' ? (parseFloat(cs.borderLeftWidth) || 0) : 0;
701
+ const btc = cs.borderTopColor;
702
+ const brc = cs.borderRightColor;
703
+ const bbc = cs.borderBottomColor;
704
+ const blc = cs.borderLeftColor;
705
+ const hasAnyBorder = (
706
+ (bt > 0.1 && parseAlpha(btc) * effOpacity > 0.01) ||
707
+ (br > 0.1 && parseAlpha(brc) * effOpacity > 0.01) ||
708
+ (bb > 0.1 && parseAlpha(bbc) * effOpacity > 0.01) ||
709
+ (bl > 0.1 && parseAlpha(blc) * effOpacity > 0.01)
710
+ );
711
+ const radiusPx = parseRadiusPx(cs, r.w, r.h);
712
+ const hasInnerText = (el.textContent || '').trim().length > 0;
713
+
714
+ // Dense pure-decorative non-text visual clusters (e.g. waveforms, equalizers, dot grids, sparkline strips):
715
+ // collapse >= 6 visual-only leaf children into 1 crisp 2x transparent PNG instead of 40+ tiny shapes/PNGs.
716
+ if (!hasInnerText && el.children.length >= 6 && r.w < canvasW * (1600 / 1920) && r.h < canvasH * (900 / 1080) && !el.querySelector('img')) {
717
+ const leaves = Array.from(el.querySelectorAll('*')).filter((d) => d.children.length === 0);
718
+ let styledLeaves = 0;
719
+ for (const lf of leaves) {
720
+ const lcs = getComputedStyle(lf);
721
+ if (lcs.display === 'none' || lcs.visibility === 'hidden') continue;
722
+ const lBg = parseAlpha(lcs.backgroundColor) > 0.01 || (lcs.backgroundImage && lcs.backgroundImage !== 'none');
723
+ const lBorder = (
724
+ (lcs.borderTopStyle !== 'none' && (parseFloat(lcs.borderTopWidth) || 0) > 0.1) ||
725
+ (lcs.borderLeftStyle !== 'none' && (parseFloat(lcs.borderLeftWidth) || 0) > 0.1)
726
+ );
727
+ if (lBg || lBorder || lf.tagName.toUpperCase() === 'SVG') styledLeaves++;
728
+ }
729
+ if (styledLeaves >= 6) {
730
+ const cid = tagForCapture(el);
731
+ pushItem({
732
+ kind: 'transformed_visual',
733
+ captureId: cid,
734
+ rect: r,
735
+ rotationDeg: tf.rotationDeg,
736
+ hasSkew: tf.hasSkew,
737
+ hideChildren: false,
738
+ stackPath,
739
+ domOrder: ++domOrder,
740
+ motion,
741
+ }, stackInfo);
742
+ continue;
743
+ }
744
+ }
745
+
746
+ // Pure-CSS leaf icons drawn with ::before/::after borders, gradients, transforms, or glyph content
747
+ // (e.g. CSS mic/camera/arrow/crosshair icons): capture whole leaf as 1 crisp 2x transparent PNG.
748
+ if (!hasInnerText && el.children.length === 0 && r.w <= 160 && r.h <= 160) {
749
+ let hasRichPseudo = false;
750
+ for (const pseudo of ['::before', '::after']) {
751
+ const pcs = getComputedStyle(el, pseudo);
752
+ const content = pcs.content;
753
+ if (!content || content === 'none' || content === 'normal') continue;
754
+ const pOp = effOpacity * parseFloat(pcs.opacity || '1');
755
+ if (pOp <= 0.01) continue;
756
+ const pHasTextGlyph = content !== '""' && content !== "''" && content.replace(/^["']|["']$/g, '').trim().length > 0;
757
+ const pHasBgImg = Boolean(pcs.backgroundImage && pcs.backgroundImage !== 'none');
758
+ const pHasBorder = (
759
+ (pcs.borderTopStyle !== 'none' && (parseFloat(pcs.borderTopWidth) || 0) > 0.1 && parseAlpha(pcs.borderTopColor) * pOp > 0.01) ||
760
+ (pcs.borderRightStyle !== 'none' && (parseFloat(pcs.borderRightWidth) || 0) > 0.1 && parseAlpha(pcs.borderRightColor) * pOp > 0.01) ||
761
+ (pcs.borderBottomStyle !== 'none' && (parseFloat(pcs.borderBottomWidth) || 0) > 0.1 && parseAlpha(pcs.borderBottomColor) * pOp > 0.01) ||
762
+ (pcs.borderLeftStyle !== 'none' && (parseFloat(pcs.borderLeftWidth) || 0) > 0.1 && parseAlpha(pcs.borderLeftColor) * pOp > 0.01)
763
+ );
764
+ const pHasTransform = Boolean(pcs.transform && pcs.transform !== 'none');
765
+ if (pHasTextGlyph || pHasBgImg || pHasBorder || pHasTransform) {
766
+ hasRichPseudo = true;
767
+ break;
768
+ }
769
+ }
770
+ if (hasRichPseudo) {
771
+ const cid = tagForCapture(el);
772
+ pushItem({
773
+ kind: 'transformed_visual',
774
+ captureId: cid,
775
+ rect: r,
776
+ rotationDeg: tf.rotationDeg,
777
+ hasSkew: tf.hasSkew,
778
+ hideChildren: false,
779
+ stackPath,
780
+ domOrder: ++domOrder,
781
+ motion,
782
+ }, stackInfo);
783
+ continue;
784
+ }
785
+ }
786
+
787
+ if (tf.hasTransform && (hasBgImage || bgAlpha > 0.01 || hasAnyBorder)) {
788
+ const rotMod90 = ((tf.rotationDeg % 90) + 90) % 90;
789
+ const layoutW = el.offsetWidth || 0;
790
+ const layoutH = el.offsetHeight || 0;
791
+ const minLayout = Math.min(layoutW, layoutH);
792
+ const hasClipOrMask = (
793
+ (cs.clipPath && cs.clipPath !== 'none') ||
794
+ (cs.maskImage && cs.maskImage !== 'none') ||
795
+ (cs.webkitMaskImage && cs.webkitMaskImage !== 'none')
796
+ );
797
+ const hasShadowOrFilter = (
798
+ (cs.boxShadow && cs.boxShadow !== 'none') ||
799
+ (cs.filter && cs.filter !== 'none')
800
+ );
801
+ const bgColorRepresentable = (bgAlpha <= 0.01) || isRepresentableColor(cs.backgroundColor);
802
+ const isSmallDiamond = (
803
+ !hasInnerText &&
804
+ el.children.length === 0 &&
805
+ cs.display !== 'inline' &&
806
+ !hasBgImage &&
807
+ bgColorRepresentable &&
808
+ !tf.hasSkew &&
809
+ !tf.hasIndividualTransform &&
810
+ tf.isUniformScale &&
811
+ !hasClipOrMask &&
812
+ !isDiamondClippedByAncestor(el, rawRect) &&
813
+ !hasShadowOrFilter &&
814
+ !hasTransformedAncestor(el) &&
815
+ radiusPx < 1.5 &&
816
+ layoutW > 0 &&
817
+ layoutH > 0 &&
818
+ Math.abs(layoutW - layoutH) <= Math.max(0.5, 0.05 * minLayout) &&
819
+ Math.abs(r.w - r.h) <= 2.0 &&
820
+ Math.max(r.w, r.h) <= 80 &&
821
+ Math.abs(rotMod90 - 45) <= 2.5
822
+ );
823
+ const borderStyleSupported = (
824
+ cs.borderTopStyle === 'solid' ||
825
+ cs.borderTopStyle === 'dashed' ||
826
+ cs.borderTopStyle === 'dotted'
827
+ );
828
+ const isUnscaledForBorder = (
829
+ Math.abs(tf.len1 - 1.0) <= 0.05 && Math.abs(tf.len2 - 1.0) <= 0.05
830
+ );
831
+ const hasUniformOrNoBorder = (
832
+ !hasAnyBorder ||
833
+ (
834
+ isUnscaledForBorder &&
835
+ borderStyleSupported &&
836
+ isRepresentableColor(btc) &&
837
+ bt > 0.1 && br > 0.1 && bb > 0.1 && bl > 0.1 &&
838
+ Math.abs(bt - br) < 0.2 && Math.abs(bt - bb) < 0.2 && Math.abs(bt - bl) < 0.2 &&
839
+ btc === brc && btc === bbc && btc === blc &&
840
+ cs.borderTopStyle === cs.borderRightStyle &&
841
+ cs.borderTopStyle === cs.borderBottomStyle &&
842
+ cs.borderTopStyle === cs.borderLeftStyle
843
+ )
844
+ );
845
+ if (isSmallDiamond && hasUniformOrNoBorder) {
846
+ pushItem({
847
+ kind: 'box',
848
+ shape: 'diamond',
849
+ rect: r,
850
+ backgroundColor: cs.backgroundColor,
851
+ bgAlpha,
852
+ opacity: effOpacity,
853
+ radiusPx: 0,
854
+ borders: {
855
+ top: [bt, btc],
856
+ right: [br, brc],
857
+ bottom: [bb, bbc],
858
+ left: [bl, blc],
859
+ },
860
+ borderStyles: {
861
+ top: cs.borderTopStyle || 'solid',
862
+ right: cs.borderRightStyle || 'solid',
863
+ bottom: cs.borderBottomStyle || 'solid',
864
+ left: cs.borderLeftStyle || 'solid',
865
+ },
866
+ stackPath,
867
+ domOrder: ++domOrder,
868
+ motion,
869
+ }, stackInfo);
870
+ continue;
871
+ }
872
+ const hasNestedImg = Boolean(el.querySelector('img'));
873
+ const splitForNestedImg = hasNestedImg && effOpacity >= 0.999;
874
+ const isAtomicCapture = !hasInnerText && !splitForNestedImg;
875
+ const atomicBounds = isAtomicCapture ? computeAtomicCaptureRect(el, r) : { rect: r, captureClip: false };
876
+ const cid = tagForCapture(el);
877
+ const tfItem = {
878
+ kind: 'transformed_visual',
879
+ captureId: cid,
880
+ rect: atomicBounds.rect,
881
+ rotationDeg: tf.rotationDeg,
882
+ hasSkew: tf.hasSkew,
883
+ hideChildren: hasInnerText || splitForNestedImg,
884
+ stackPath,
885
+ domOrder: ++domOrder,
886
+ motion,
887
+ };
888
+ if (atomicBounds.captureClip) {
889
+ tfItem.captureClip = true;
890
+ }
891
+ pushItem(tfItem, stackInfo);
892
+ // Visual-only decorative transformed element (or opacity<1 group without inner text) is captured atomically as 2x transparent PNG
893
+ if (isAtomicCapture) {
894
+ continue;
895
+ }
896
+ } else if (hasBgImage) {
897
+ const isThinLine = Math.min(r.w, r.h) <= 4.5;
898
+ if (isThinLine) {
899
+ // Extract first color in gradient for crisp native thin rule
900
+ const cm = cs.backgroundImage.match(/rgba?\([^)]+\)|#[0-9a-fA-F]{3,8}/);
901
+ pushItem({
902
+ kind: 'box',
903
+ rect: r,
904
+ backgroundColor: cm ? cm[0] : cs.backgroundColor,
905
+ bgAlpha: cm ? parseAlpha(cm[0]) * effOpacity : bgAlpha,
906
+ opacity: effOpacity,
907
+ radiusPx,
908
+ borders: { top: [0, ''], right: [0, ''], bottom: [0, ''], left: [0, ''] },
909
+ stackPath,
910
+ domOrder: ++domOrder,
911
+ motion,
912
+ }, stackInfo);
913
+ } else {
914
+ const cid = tagForCapture(el);
915
+ pushItem({
916
+ kind: 'gradient_box',
917
+ captureId: cid,
918
+ rect: r,
919
+ hasChildren: el.childNodes.length > 0,
920
+ stackPath,
921
+ domOrder: ++domOrder,
922
+ motion,
923
+ }, stackInfo);
924
+ }
925
+ } else if (bgAlpha > 0.01 || hasAnyBorder) {
926
+ pushItem({
927
+ kind: 'box',
928
+ rect: r,
929
+ backgroundColor: cs.backgroundColor,
930
+ bgAlpha,
931
+ opacity: effOpacity,
932
+ radiusPx,
933
+ borders: {
934
+ top: [bt, btc],
935
+ right: [br, brc],
936
+ bottom: [bb, bbc],
937
+ left: [bl, blc],
938
+ },
939
+ borderStyles: {
940
+ top: cs.borderTopStyle || 'solid',
941
+ right: cs.borderRightStyle || 'solid',
942
+ bottom: cs.borderBottomStyle || 'solid',
943
+ left: cs.borderLeftStyle || 'solid',
944
+ },
945
+ stackPath,
946
+ domOrder: ++domOrder,
947
+ motion,
948
+ }, stackInfo);
949
+ }
950
+
951
+ // Check ::before and ::after decorative bars/dots
952
+ for (const pseudo of ['::before', '::after']) {
953
+ const pcs = getComputedStyle(el, pseudo);
954
+ const content = pcs.content;
955
+ if (!content || content === 'none' || content === 'normal') continue;
956
+ const pBgAlpha = parseAlpha(pcs.backgroundColor) * effOpacity * parseFloat(pcs.opacity || '1');
957
+ const pw = parseFloat(pcs.width) || 0;
958
+ const ph = parseFloat(pcs.height) || 0;
959
+ if (pBgAlpha > 0.01 && pw > 0.2 && ph > 0.2) {
960
+ // Compute pseudo box position relative to el
961
+ let px = r.x;
962
+ let py = r.y;
963
+ const leftVal = pcs.left;
964
+ const topVal = pcs.top;
965
+ if (leftVal && leftVal !== 'auto') px = r.x + (parseFloat(leftVal) || 0) * scaleX;
966
+ if (topVal && topVal !== 'auto') py = r.y + (parseFloat(topVal) || 0) * scaleY;
967
+ // Handle common translateX(-50%) / translateY(-50%) centering
968
+ if (pcs.transform && pcs.transform !== 'none') {
969
+ const m = pcs.transform.match(/matrix\(([^)]+)\)/);
970
+ if (m) {
971
+ const parts = m[1].split(',').map(Number);
972
+ if (parts.length === 6) {
973
+ px += (parts[4] || 0) * scaleX;
974
+ py += (parts[5] || 0) * scaleY;
975
+ }
976
+ }
977
+ }
978
+ const pr = { x: px, y: py, w: pw * scaleX, h: ph * scaleY };
979
+ if (isVisibleRect(pr)) {
980
+ pushItem({
981
+ kind: 'box',
982
+ rect: pr,
983
+ backgroundColor: pcs.backgroundColor,
984
+ bgAlpha: pBgAlpha,
985
+ opacity: effOpacity * parseFloat(pcs.opacity || '1'),
986
+ radiusPx: parseRadiusPx(pcs, pr.w, pr.h),
987
+ borders: { top: [0, ''], right: [0, ''], bottom: [0, ''], left: [0, ''] },
988
+ stackPath,
989
+ domOrder: ++domOrder,
990
+ }, stackInfo);
991
+ }
992
+ }
993
+ }
994
+ }
995
+
996
+ walkElements(el);
997
+ }
998
+ };
999
+
1000
+ walkElements(slide);
1001
+
1002
+ // Extract native editable text via TreeWalker(SHOW_TEXT) + Range.getClientRects()
1003
+ const isBlockDisplay = (disp) => (
1004
+ disp === 'block' || disp === 'flex' || disp === 'inline-flex' ||
1005
+ disp === 'grid' || disp === 'inline-grid' || disp === 'table-cell' ||
1006
+ disp === 'list-item' || disp === 'flow-root'
1007
+ );
1008
+
1009
+ const getBlockContainer = (el) => {
1010
+ let cur = el;
1011
+ while (cur && cur !== slide) {
1012
+ const cs = getComputedStyle(cur);
1013
+ if (isBlockDisplay(cs.display)) return cur;
1014
+ cur = cur.parentElement;
1015
+ }
1016
+ return el;
1017
+ };
1018
+
1019
+ const applyTextTransform = (txt, transform) => {
1020
+ if (transform === 'uppercase') return txt.toUpperCase();
1021
+ if (transform === 'lowercase') return txt.toLowerCase();
1022
+ return txt;
1023
+ };
1024
+
1025
+ const blockGroups = new Map();
1026
+ let blockSeq = 0;
1027
+ let hasVisibleText = false;
1028
+ const slideCssToStageX = slide.offsetWidth > 0 ? (canvasW / slide.offsetWidth) : 1.0;
1029
+ const slideCssToStageY = slide.offsetHeight > 0 ? (canvasH / slide.offsetHeight) : slideCssToStageX;
1030
+
1031
+ const isPreservingSpaces = (ws) => (
1032
+ ws === 'pre' || ws === 'pre-wrap' || ws === 'break-spaces'
1033
+ );
1034
+ const isPreservingNewlines = (ws) => (
1035
+ ws === 'pre' || ws === 'pre-wrap' || ws === 'pre-line' || ws === 'break-spaces'
1036
+ );
1037
+ // Collapsible HTML ASCII whitespace only (excludes NBSP \u00A0, \u202F, \u2007, \u3000)
1038
+ const isPureCollapsibleAsciiWs = (s) => /^[ \t\r\n\f]+$/.test(s);
1039
+ const hasCollapsibleAsciiWs = (s) => /[ \t\r\n\f]/.test(s);
1040
+
1041
+ const getBlockContentWidth = (blockEl, bcs) => {
1042
+ const padL = parseFloat(bcs.paddingLeft) || 0;
1043
+ const padR = parseFloat(bcs.paddingRight) || 0;
1044
+ const bL = parseFloat(bcs.borderLeftWidth) || 0;
1045
+ const bR = parseFloat(bcs.borderRightWidth) || 0;
1046
+ if (blockEl && typeof blockEl.getBoundingClientRect === 'function') {
1047
+ const br = toStageRect(blockEl.getBoundingClientRect());
1048
+ if (br.w > 0) {
1049
+ const visualScaleX = blockEl.offsetWidth > 0 ? (br.w / blockEl.offsetWidth) : slideCssToStageX;
1050
+ return Math.max(0, br.w - (padL + padR + bL + bR) * visualScaleX);
1051
+ }
1052
+ }
1053
+ if (blockEl && blockEl.clientWidth > 0) {
1054
+ return Math.max(0, (blockEl.clientWidth - padL - padR) * slideCssToStageX);
1055
+ }
1056
+ return 0;
1057
+ };
1058
+
1059
+ const walker = document.createTreeWalker(slide, NodeFilter.SHOW_TEXT);
1060
+ let textNode;
1061
+ while ((textNode = walker.nextNode())) {
1062
+ const parentEl = textNode.parentElement;
1063
+ if (!parentEl || isControlOrIgnored(parentEl)) continue;
1064
+ if (parentEl.closest('svg')) continue;
1065
+ if (parentEl.closest('table[data-h2p-native-table="1"]')) continue;
1066
+ if (isHiddenTree(parentEl)) continue;
1067
+
1068
+ const pcs = getComputedStyle(parentEl);
1069
+ const rawVal = textNode.nodeValue || '';
1070
+ const preservesSpaces = isPreservingSpaces(pcs.whiteSpace);
1071
+ const preservesNewlines = isPreservingNewlines(pcs.whiteSpace);
1072
+ if (rawVal.length === 0) continue;
1073
+
1074
+ const isCollapsibleAsciiOnly = !preservesSpaces && isPureCollapsibleAsciiWs(rawVal);
1075
+ let wsProbeRect = null;
1076
+ if (isCollapsibleAsciiOnly) {
1077
+ if (preservesNewlines && /[\r\n]/.test(rawVal)) {
1078
+ // Pure newline/space node in pre-line: breaks lines via seam scan, no visible glyph
1079
+ continue;
1080
+ }
1081
+ const probeRange = document.createRange();
1082
+ probeRange.setStart(textNode, 0);
1083
+ probeRange.setEnd(textNode, rawVal.length);
1084
+ const probeRects = probeRange.getClientRects();
1085
+ if (!probeRects || probeRects.length === 0 || probeRects[0].width <= 0.2) continue;
1086
+ const pr = toStageRect(probeRects[0]);
1087
+ if (!(pr.w > 0.2 && pr.h > 0.2 && isVisibleRect(pr))) continue;
1088
+ wsProbeRect = pr;
1089
+ }
1090
+
1091
+ const parentRect = toStageRect(parentEl.getBoundingClientRect());
1092
+ if (isVisibleRect(parentRect) && rawVal.trim().length > 0) {
1093
+ hasVisibleText = true;
1094
+ }
1095
+
1096
+ const blockEl = getBlockContainer(parentEl);
1097
+ if (!blockGroups.has(blockEl)) {
1098
+ const bcs = getComputedStyle(blockEl);
1099
+ const direction = (bcs.direction || 'ltr').toLowerCase() === 'rtl' ? 'rtl' : 'ltr';
1100
+ let align = (bcs.textAlign || 'start').toLowerCase();
1101
+ if (align === 'start') align = direction === 'rtl' ? 'right' : 'left';
1102
+ if (align === 'end') align = direction === 'rtl' ? 'left' : 'right';
1103
+ // Check flex centering when textAlign is start/left inside centered flex pill/button
1104
+ if ((bcs.display === 'flex' || bcs.display === 'inline-flex') && bcs.justifyContent === 'center' && blockEl.children.length === 0) {
1105
+ align = 'center';
1106
+ }
1107
+ const blockStackInfo = getStackingInfo(blockEl);
1108
+ blockGroups.set(blockEl, {
1109
+ blockEl,
1110
+ align,
1111
+ direction,
1112
+ blockWidth: getBlockContentWidth(blockEl, bcs),
1113
+ stackPath: blockStackInfo.stackPath,
1114
+ stackInfo: blockStackInfo,
1115
+ domOrder: 100000 + (++blockSeq),
1116
+ motion: getMotionMeta(blockEl),
1117
+ charItems: [],
1118
+ });
1119
+ }
1120
+ const group = blockGroups.get(blockEl);
1121
+
1122
+ const parseScaleToken = (tok) => {
1123
+ if (!tok) return NaN;
1124
+ const v = parseFloat(tok);
1125
+ if (!Number.isFinite(v)) return NaN;
1126
+ return tok.endsWith('%') ? (v / 100.0) : v;
1127
+ };
1128
+ const getVisualTextScale = (el) => {
1129
+ let cur = el;
1130
+ let sx = slideCssToStageX;
1131
+ let sy = slideCssToStageY;
1132
+ while (cur && cur !== slide) {
1133
+ const cs = getComputedStyle(cur);
1134
+ // Per CSS Transforms spec, non-replaced inline / display:contents boxes are not transformable.
1135
+ const isTransformable = cs.display !== 'inline' && cs.display !== 'contents';
1136
+ if (isTransformable) {
1137
+ const sc = (cs.scale || '').trim();
1138
+ if (sc && sc !== 'none') {
1139
+ const parts = sc.split(/\s+/);
1140
+ const fx = Math.abs(parseScaleToken(parts[0]));
1141
+ const fy = parts.length >= 2 ? Math.abs(parseScaleToken(parts[1])) : fx;
1142
+ if (Number.isFinite(fx) && fx > 0) sx *= fx;
1143
+ if (Number.isFinite(fy) && fy > 0) sy *= fy;
1144
+ }
1145
+ const t = cs.transform;
1146
+ if (t && t !== 'none') {
1147
+ const m2 = t.match(/^matrix\(([^)]+)\)$/);
1148
+ if (m2) {
1149
+ const p = m2[1].split(',').map((v) => parseFloat(v.trim()));
1150
+ if (p.length === 6 && p.every(Number.isFinite)) {
1151
+ const fx = Math.hypot(p[0], p[1]);
1152
+ const fy = Math.hypot(p[2], p[3]);
1153
+ if (fx > 0) sx *= fx;
1154
+ if (fy > 0) sy *= fy;
1155
+ }
1156
+ } else {
1157
+ const m3 = t.match(/^matrix3d\(([^)]+)\)$/);
1158
+ if (m3) {
1159
+ const p = m3[1].split(',').map((v) => parseFloat(v.trim()));
1160
+ if (p.length === 16 && p.every(Number.isFinite)) {
1161
+ const fx = Math.hypot(p[0], p[1], p[2]);
1162
+ const fy = Math.hypot(p[4], p[5], p[6]);
1163
+ if (fx > 0) sx *= fx;
1164
+ if (fy > 0) sy *= fy;
1165
+ }
1166
+ }
1167
+ }
1168
+ }
1169
+ }
1170
+ cur = cur.parentElement;
1171
+ }
1172
+ return { sx, sy };
1173
+ };
1174
+ const visualScale = getVisualTextScale(parentEl);
1175
+ const fontSize = (parseFloat(pcs.fontSize) || 24.0) * visualScale.sy;
1176
+ let fontWeight = parseInt(pcs.fontWeight, 10);
1177
+ if (Number.isNaN(fontWeight)) {
1178
+ fontWeight = pcs.fontWeight === 'bold' ? 700 : 400;
1179
+ }
1180
+ const lsPx = pcs.letterSpacing === 'normal' ? 0.0 : ((parseFloat(pcs.letterSpacing) || 0.0) * visualScale.sx);
1181
+ const styleKey = JSON.stringify([
1182
+ pcs.fontFamily,
1183
+ Math.round(fontSize * 10) / 10,
1184
+ fontWeight,
1185
+ pcs.fontStyle,
1186
+ pcs.color,
1187
+ Math.round(getEffectiveOpacity(parentEl) * 100) / 100,
1188
+ Math.round(lsPx * 10) / 10,
1189
+ ]);
1190
+ const styleObj = {
1191
+ fontFamily: pcs.fontFamily,
1192
+ fontSize,
1193
+ fontWeight,
1194
+ fontStyle: pcs.fontStyle,
1195
+ color: pcs.color,
1196
+ opacity: getEffectiveOpacity(parentEl),
1197
+ letterSpacingPx: lsPx,
1198
+ styleKey,
1199
+ };
1200
+
1201
+ if (isCollapsibleAsciiOnly) {
1202
+ group.charItems.push({
1203
+ ch: ' ',
1204
+ rect: wsProbeRect,
1205
+ style: styleObj,
1206
+ node: textNode,
1207
+ offset: 0,
1208
+ endOffset: rawVal.length,
1209
+ preservesSpaces: false,
1210
+ preservesNewlines,
1211
+ });
1212
+ continue;
1213
+ }
1214
+
1215
+ // Measure every character's rendered client rect so we split exact visual lines!
1216
+ const range = document.createRange();
1217
+ const len = rawVal.length;
1218
+ for (let i = 0; i < len; i++) {
1219
+ const ch = rawVal[i];
1220
+ if (!preservesNewlines && (ch === '\n' || ch === '\r' || ch === '\t' || ch === '\f')) {
1221
+ // Collapsible whitespace in HTML becomes single space if it occupies width
1222
+ range.setStart(textNode, i);
1223
+ range.setEnd(textNode, i + 1);
1224
+ const rects = range.getClientRects();
1225
+ if (rects.length > 0 && rects[0].width > 0.2) {
1226
+ const cr = toStageRect(rects[0]);
1227
+ if (isVisibleRect(cr)) {
1228
+ group.charItems.push({
1229
+ ch: ' ',
1230
+ rect: cr,
1231
+ style: styleObj,
1232
+ node: textNode,
1233
+ offset: i,
1234
+ endOffset: i + 1,
1235
+ preservesSpaces: false,
1236
+ preservesNewlines: false,
1237
+ });
1238
+ }
1239
+ }
1240
+ continue;
1241
+ }
1242
+ if (preservesNewlines && (ch === '\n' || ch === '\r')) continue;
1243
+
1244
+ range.setStart(textNode, i);
1245
+ range.setEnd(textNode, i + 1);
1246
+ const rects = range.getClientRects();
1247
+ if (!rects || rects.length === 0) continue;
1248
+ const cr = toStageRect(rects[0]);
1249
+ if (!preservesSpaces && cr.w <= 0.05 && ch === ' ') continue;
1250
+ if (cr.h <= 0.2) continue;
1251
+ const outCh = applyTextTransform(ch, pcs.textTransform);
1252
+ group.charItems.push({
1253
+ ch: outCh,
1254
+ rect: cr,
1255
+ style: styleObj,
1256
+ node: textNode,
1257
+ offset: i,
1258
+ endOffset: i + 1,
1259
+ preservesSpaces,
1260
+ preservesNewlines,
1261
+ });
1262
+ }
1263
+ }
1264
+
1265
+ const computeJoinFromPrev = (prevLast, currFirst, prevTrimmedTrail, currTrimmedLead, blockRoot) => {
1266
+ if (!prevLast || !currFirst || !prevLast.node || !currFirst.node) {
1267
+ return (prevTrimmedTrail || currTrimmedLead) ? 'space' : 'direct';
1268
+ }
1269
+ try {
1270
+ const prevLen = (prevLast.node.nodeValue || '').length;
1271
+ const currLen = (currFirst.node.nodeValue || '').length;
1272
+ const startOff = Math.max(0, Math.min(prevLast.endOffset != null ? prevLast.endOffset : (prevLast.offset + 1), prevLen));
1273
+ const endOff = Math.max(0, Math.min(currFirst.offset, currLen));
1274
+
1275
+ if (prevLast.node === currFirst.node) {
1276
+ if (startOff > endOff) return 'hard';
1277
+ const gapText = (prevLast.node.nodeValue || '').slice(startOff, endOff);
1278
+ const pEl = prevLast.node.parentElement;
1279
+ const pWs = pEl ? getComputedStyle(pEl).whiteSpace : 'normal';
1280
+ if (isPreservingNewlines(pWs) && /[\r\n]/.test(gapText)) {
1281
+ return 'hard';
1282
+ }
1283
+ if (prevTrimmedTrail || currTrimmedLead || hasCollapsibleAsciiWs(gapText)) {
1284
+ return 'space';
1285
+ }
1286
+ return 'direct';
1287
+ }
1288
+
1289
+ if (typeof prevLast.node.compareDocumentPosition === 'function') {
1290
+ const relPos = prevLast.node.compareDocumentPosition(currFirst.node);
1291
+ // 2 = DOCUMENT_POSITION_PRECEDING (visual order reversed relative to DOM)
1292
+ if (relPos & 2) return 'hard';
1293
+ }
1294
+
1295
+ const prevParent = prevLast.node.parentElement;
1296
+ const currParent = currFirst.node.parentElement;
1297
+ if (prevParent && currParent && getBlockContainer(prevParent) !== getBlockContainer(currParent)) {
1298
+ return 'hard';
1299
+ }
1300
+
1301
+ let sawVisibleWhitespace = false;
1302
+
1303
+ // 1. Check tail of prevLast.node
1304
+ const tailText = (prevLast.node.nodeValue || '').slice(startOff);
1305
+ if (tailText && prevParent && !isControlOrIgnored(prevParent) && !isHiddenTree(prevParent)) {
1306
+ const prevWs = getComputedStyle(prevParent).whiteSpace;
1307
+ if (isPreservingNewlines(prevWs) && /[\r\n]/.test(tailText)) {
1308
+ return 'hard';
1309
+ }
1310
+ if (hasCollapsibleAsciiWs(tailText)) {
1311
+ sawVisibleWhitespace = true;
1312
+ }
1313
+ }
1314
+
1315
+ // 2. Walk live DOM nodes strictly between prevLast.node and currFirst.node
1316
+ const walkRoot = (blockRoot && blockRoot.contains(prevLast.node) && blockRoot.contains(currFirst.node))
1317
+ ? blockRoot
1318
+ : slide;
1319
+ const seamWalker = document.createTreeWalker(
1320
+ walkRoot,
1321
+ (NodeFilter.SHOW_ELEMENT || 1) | (NodeFilter.SHOW_TEXT || 4)
1322
+ );
1323
+ seamWalker.currentNode = prevLast.node;
1324
+ let curNode;
1325
+ while ((curNode = seamWalker.nextNode()) && curNode !== currFirst.node) {
1326
+ if (curNode.nodeType === 1) {
1327
+ if (isControlOrIgnored(curNode) || isHiddenTree(curNode)) continue;
1328
+ const tag = (curNode.tagName || '').toUpperCase();
1329
+ if (tag === 'BR' || tag === 'HR') {
1330
+ return 'hard';
1331
+ }
1332
+ const nDisp = getComputedStyle(curNode).display;
1333
+ if (isBlockDisplay(nDisp)) {
1334
+ return 'hard';
1335
+ }
1336
+ } else if (curNode.nodeType === 3) {
1337
+ const np = curNode.parentElement;
1338
+ if (!np || isControlOrIgnored(np) || isHiddenTree(np)) continue;
1339
+ const nVal = curNode.nodeValue || '';
1340
+ if (!nVal) continue;
1341
+ const nWs = getComputedStyle(np).whiteSpace;
1342
+ if (isPreservingNewlines(nWs) && /[\r\n]/.test(nVal)) {
1343
+ return 'hard';
1344
+ }
1345
+ if (hasCollapsibleAsciiWs(nVal)) {
1346
+ sawVisibleWhitespace = true;
1347
+ }
1348
+ }
1349
+ }
1350
+
1351
+ // 3. Check head of currFirst.node
1352
+ const headText = (currFirst.node.nodeValue || '').slice(0, endOff);
1353
+ if (headText && currParent && !isControlOrIgnored(currParent) && !isHiddenTree(currParent)) {
1354
+ const currWs = getComputedStyle(currParent).whiteSpace;
1355
+ if (isPreservingNewlines(currWs) && /[\r\n]/.test(headText)) {
1356
+ return 'hard';
1357
+ }
1358
+ if (hasCollapsibleAsciiWs(headText)) {
1359
+ sawVisibleWhitespace = true;
1360
+ }
1361
+ }
1362
+
1363
+ if (prevTrimmedTrail || currTrimmedLead || sawVisibleWhitespace) {
1364
+ return 'space';
1365
+ }
1366
+ return 'direct';
1367
+ } catch (_) {
1368
+ return (prevTrimmedTrail || currTrimmedLead) ? 'space' : 'direct';
1369
+ }
1370
+ };
1371
+
1372
+ // Group characters in each block into visual lines (by vertical center proximity) & inline runs
1373
+ for (const [, group] of blockGroups.entries()) {
1374
+ const chars = group.charItems;
1375
+ if (!chars.length) continue;
1376
+
1377
+ // Sort only minimally: preserve visual line grouping by Y
1378
+ const visualLines = [];
1379
+ for (const ci of chars) {
1380
+ const cy = ci.rect.y + ci.rect.h * 0.5;
1381
+ let targetLine = null;
1382
+ for (const ln of visualLines) {
1383
+ const tol = Math.max(ln.maxFs * 0.48, ci.style.fontSize * 0.48, 8.0);
1384
+ if (Math.abs(cy - ln.centerY) <= tol) {
1385
+ targetLine = ln;
1386
+ break;
1387
+ }
1388
+ }
1389
+ if (!targetLine) {
1390
+ targetLine = {
1391
+ centerY: cy,
1392
+ maxFs: ci.style.fontSize,
1393
+ chars: [],
1394
+ };
1395
+ visualLines.push(targetLine);
1396
+ }
1397
+ targetLine.chars.push(ci);
1398
+ targetLine.maxFs = Math.max(targetLine.maxFs, ci.style.fontSize);
1399
+ // Update running centerY slightly
1400
+ targetLine.centerY = (targetLine.centerY * (targetLine.chars.length - 1) + cy) / targetLine.chars.length;
1401
+ }
1402
+
1403
+ visualLines.sort((a, b) => a.centerY - b.centerY);
1404
+
1405
+ const outLines = [];
1406
+ let prevLineMeta = null;
1407
+ for (const ln of visualLines) {
1408
+ // Trim leading/trailing collapsible ASCII spaces unless whitespace is preserved
1409
+ const arr = ln.chars.slice();
1410
+ let trimmedLead = false;
1411
+ let trimmedTrail = false;
1412
+ while (arr.length && arr[0].ch === ' ' && !arr[0].preservesSpaces) {
1413
+ arr.shift();
1414
+ trimmedLead = true;
1415
+ }
1416
+ while (arr.length && arr[arr.length - 1].ch === ' ' && !arr[arr.length - 1].preservesSpaces) {
1417
+ arr.pop();
1418
+ trimmedTrail = true;
1419
+ }
1420
+ if (!arr.length) continue;
1421
+
1422
+ const firstChar = arr[0];
1423
+ const lastChar = arr[arr.length - 1];
1424
+
1425
+ // Merge contiguous characters sharing styleKey into runs
1426
+ const runs = [];
1427
+ let curRun = null;
1428
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
1429
+ for (const ci of arr) {
1430
+ minX = Math.min(minX, ci.rect.x);
1431
+ minY = Math.min(minY, ci.rect.y);
1432
+ maxX = Math.max(maxX, ci.rect.x + ci.rect.w);
1433
+ maxY = Math.max(maxY, ci.rect.y + ci.rect.h);
1434
+ if (curRun && curRun._key === ci.style.styleKey) {
1435
+ curRun.text += ci.ch;
1436
+ } else {
1437
+ curRun = {
1438
+ _key: ci.style.styleKey,
1439
+ text: ci.ch,
1440
+ fontFamily: ci.style.fontFamily,
1441
+ fontSize: ci.style.fontSize,
1442
+ fontWeight: ci.style.fontWeight,
1443
+ fontStyle: ci.style.fontStyle,
1444
+ color: ci.style.color,
1445
+ opacity: ci.style.opacity,
1446
+ letterSpacingPx: ci.style.letterSpacingPx,
1447
+ };
1448
+ runs.push(curRun);
1449
+ }
1450
+ }
1451
+ for (const r of runs) delete r._key;
1452
+ const lineRect = { x: minX, y: minY, w: Math.max(1, maxX - minX), h: Math.max(1, maxY - minY) };
1453
+ if (!isVisibleRect(lineRect)) continue;
1454
+ const lineObj = {
1455
+ x: lineRect.x,
1456
+ y: lineRect.y,
1457
+ w: lineRect.w,
1458
+ h: lineRect.h,
1459
+ runs,
1460
+ };
1461
+ if (outLines.length > 0 && prevLineMeta) {
1462
+ lineObj.joinFromPrev = computeJoinFromPrev(
1463
+ prevLineMeta.lastChar,
1464
+ firstChar,
1465
+ prevLineMeta.trimmedTrail,
1466
+ trimmedLead,
1467
+ group.blockEl
1468
+ );
1469
+ }
1470
+ outLines.push(lineObj);
1471
+ prevLineMeta = { lastChar, trimmedTrail };
1472
+ }
1473
+
1474
+ if (outLines.length) {
1475
+ const textItem = {
1476
+ kind: 'text',
1477
+ align: group.align,
1478
+ direction: group.direction,
1479
+ lines: outLines,
1480
+ stackPath: group.stackPath,
1481
+ domOrder: group.domOrder,
1482
+ motion: group.motion,
1483
+ };
1484
+ if (group.blockWidth > 0) {
1485
+ textItem.blockWidth = Math.round(group.blockWidth * 100) / 100;
1486
+ }
1487
+ pushItem(textItem, group.stackInfo);
1488
+ }
1489
+ }
1490
+
1491
+ // Sort all items in exact CSS stacking-context paint order (CSS 2.1 Appendix E phases:
1492
+ // 0: SC own background | 1: negative z-index | 2: non-positioned blocks | 3: non-positioned text |
1493
+ // 4: positioned z-index:auto & z=0 SCs | 5: positive z-index)
1494
+ const buildSortKey = (item) => {
1495
+ const info = itemStackInfo.get(item);
1496
+ const key = [];
1497
+ const chain = (info && info.scChain) || [];
1498
+ for (let i = 0; i < chain.length; i++) {
1499
+ const sc = chain[i];
1500
+ const grp = sc.z < 0 ? 1 : (sc.z === 0 ? 4 : 5);
1501
+ key.push(grp, sc.z, sc.order);
1502
+ }
1503
+ if (info && info.isSelfSc && item.kind !== 'text') {
1504
+ key.push(0, 0, item.domOrder);
1505
+ } else if (info && info.posAnchorOrder > 0) {
1506
+ key.push(4, 0, info.posAnchorOrder, item.kind === 'text' ? 1 : 0, info.elOrder, item.domOrder);
1507
+ } else if (item.kind === 'text') {
1508
+ key.push(3, 0, (info && info.elOrder) || 0, item.domOrder);
1509
+ } else {
1510
+ key.push(2, 0, (info && info.elOrder) || 0, item.domOrder);
1511
+ }
1512
+ return key;
1513
+ };
1514
+ const sortKeyCache = new WeakMap();
1515
+ for (const item of items) {
1516
+ sortKeyCache.set(item, buildSortKey(item));
1517
+ }
1518
+ const cmpStack = (a, b) => {
1519
+ const ka = sortKeyCache.get(a) || [2, 0, 0, a.domOrder];
1520
+ const kb = sortKeyCache.get(b) || [2, 0, 0, b.domOrder];
1521
+ const n = Math.min(ka.length, kb.length);
1522
+ for (let i = 0; i < n; i++) {
1523
+ if (ka[i] !== kb[i]) return ka[i] - kb[i];
1524
+ }
1525
+ if (ka.length !== kb.length) return ka.length - kb.length;
1526
+ return a.domOrder - b.domOrder;
1527
+ };
1528
+ items.sort(cmpStack);
1529
+
1530
+ // Build explicit captureRequests list for elements/backgrounds requiring screenshot rasterization
1531
+ const captureRequests = [];
1532
+ const isSingleBgUrl = Boolean(
1533
+ slideBgImageCss && /^\s*url\(\s*(['"]?)([^'")]+)\1\s*\)\s*$/i.test(slideBgImageCss.trim())
1534
+ );
1535
+ if (slideHasBgImage && !isSingleBgUrl) {
1536
+ captureRequests.push({
1537
+ captureId: `s${slideIdx}-bg`,
1538
+ kind: 'slide_bg',
1539
+ slideIdx,
1540
+ hideChildren: true,
1541
+ });
1542
+ }
1543
+ let fgCount = 0;
1544
+ let hasBgPic = slideHasBgImage;
1545
+ for (const item of items) {
1546
+ if (item.kind === 'transformed_visual') {
1547
+ const capReq = {
1548
+ captureId: item.captureId,
1549
+ kind: 'transformed_visual',
1550
+ slideIdx,
1551
+ rect: item.rect,
1552
+ hideChildren: Boolean(item.hideChildren),
1553
+ };
1554
+ if (item.captureClip) {
1555
+ capReq.captureClip = true;
1556
+ }
1557
+ captureRequests.push(capReq);
1558
+ fgCount++;
1559
+ } else if (item.kind === 'gradient_box') {
1560
+ captureRequests.push({
1561
+ captureId: item.captureId,
1562
+ kind: 'gradient_box',
1563
+ slideIdx,
1564
+ rect: item.rect,
1565
+ hideChildren: Boolean(item.hasChildren),
1566
+ });
1567
+ fgCount++;
1568
+ } else if (item.kind === 'svg') {
1569
+ captureRequests.push({
1570
+ captureId: item.captureId,
1571
+ kind: 'svg',
1572
+ slideIdx,
1573
+ rect: item.rect,
1574
+ hideChildren: false,
1575
+ });
1576
+ fgCount++;
1577
+ } else if (item.kind === 'img') {
1578
+ const r = item.rect || { x: 0, y: 0, w: 0, h: 0 };
1579
+ const isFullBleedBottom = (
1580
+ fgCount === 0 &&
1581
+ r.x <= 2.0 &&
1582
+ r.y <= 2.0 &&
1583
+ r.w >= canvasW - 4.0 &&
1584
+ r.h >= canvasH - 4.0
1585
+ );
1586
+ if (isFullBleedBottom) {
1587
+ const imgOpacity = typeof item.opacity === 'number' ? item.opacity : 1.0;
1588
+ if (item.needsCapture || imgOpacity < 0.99 || hasBgPic) {
1589
+ if (item.captureId) {
1590
+ captureRequests.push({
1591
+ captureId: item.captureId,
1592
+ kind: hasBgPic ? 'slide_bg_with_bottom_img' : 'img',
1593
+ slideIdx,
1594
+ rect: item.rect,
1595
+ hideChildren: false,
1596
+ });
1597
+ }
1598
+ }
1599
+ hasBgPic = true;
1600
+ } else {
1601
+ if (item.needsCapture && item.captureId) {
1602
+ captureRequests.push({
1603
+ captureId: item.captureId,
1604
+ kind: 'img',
1605
+ slideIdx,
1606
+ rect: item.rect,
1607
+ hideChildren: false,
1608
+ });
1609
+ }
1610
+ fgCount++;
1611
+ }
1612
+ } else {
1613
+ fgCount++;
1614
+ }
1615
+ }
1616
+
1617
+ return {
1618
+ slideIdx,
1619
+ backgroundColor: slideBgColor,
1620
+ hasBackgroundImage: slideHasBgImage,
1621
+ backgroundImageCss: slideBgImageCss,
1622
+ motionPreset,
1623
+ transition: slideTransition,
1624
+ transitionDur: slideTransitionDur,
1625
+ hasVisibleText,
1626
+ items,
1627
+ captureRequests,
1628
+ droppedImages,
1629
+ };
1630
+ }
1631
+
1632
+ if (typeof module !== 'undefined' && module.exports) {
1633
+ module.exports = extractSlideDom;
1634
+ module.exports.extractSlideDom = extractSlideDom;
1635
+ }
1636
+ if (typeof globalThis !== 'undefined') {
1637
+ globalThis.extractSlideDom = extractSlideDom;
1638
+ }
1639
+ return extractSlideDom;
1640
+ })()