Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

feat(svg-to-pptx): compile group opacity into native alpha

hugohe3471a1d4f7a4e
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/context.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/context.py
index d855ed59..2b0c0e05 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/context.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/context.py
@@ -50,6 +50,10 @@ class ConvertContext:
rel_id_counter: int = 2 # rId1 reserved for slideLayout
svg_dir: Path | None = None
inherited_styles: dict[str, str] = field(default_factory=dict)
+ # SVG group opacity is post-compositing, not an inherited presentation
+ # property. DrawingML has no equivalent group alpha, so native export
+ # approximates it by multiplying this value into each descendant object.
+ opacity_multiplier: float = 1.0
# Recursion depth — only the depth==0 (root) context records anim targets.
depth: int = 0
# Top-level <g id="..."> groups, recorded as (shape_id, svg_id) in z-order.
@@ -99,6 +103,7 @@ class ConvertContext:
transform_matrix: AffineMatrix | None = None,
filter_id: str | None = None,
style_overrides: dict[str, str] | None = None,
+ opacity_multiplier: float = 1.0,
) -> ConvertContext:
"""Create a child context with accumulated translate / scale / styles.
@@ -111,6 +116,7 @@ class ConvertContext:
converters that can faithfully map it to DrawingML.
filter_id: Override filter ID.
style_overrides: Style attribute overrides from child element.
+ opacity_multiplier: Local group opacity to multiply into descendants.
"""
local_matrix = transform_matrix or IDENTITY_MATRIX
# When first crossing from scalar to matrix mode, fold accumulated
@@ -138,24 +144,10 @@ class ConvertContext:
)
merged = dict(self.inherited_styles)
-
if style_overrides:
- # Opacity is multiplicative, not a simple override
- _OPACITY_KEYS = ('opacity', 'fill-opacity', 'stroke-opacity')
- for op_key in _OPACITY_KEYS:
- if op_key in style_overrides and op_key in merged:
- try:
- merged[op_key] = str(
- float(merged[op_key]) * float(style_overrides[op_key])
- )
- except ValueError:
- merged[op_key] = style_overrides[op_key]
- elif op_key in style_overrides:
- merged[op_key] = style_overrides[op_key]
-
- for k, v in style_overrides.items():
- if k not in _OPACITY_KEYS:
- merged[k] = v
+ merged.update(style_overrides)
+
+ local_opacity = max(0.0, min(1.0, opacity_multiplier))
return ConvertContext(
defs=self.defs,
@@ -177,6 +169,7 @@ class ConvertContext:
rel_id_counter=self.rel_id_counter,
svg_dir=self.svg_dir,
inherited_styles=merged,
+ opacity_multiplier=self.opacity_multiplier * local_opacity,
depth=self.depth + 1,
# anim_targets is intentionally a fresh list on the child;
# only the root-level context's list is read by the builder.
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/converter.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/converter.py
index 4aecdb5b..3108a435 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/converter.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/converter.py
@@ -19,7 +19,8 @@ from .utils import (
parse_svg_length,
)
from .styles import (
- build_effect_xml, build_fill_xml, get_fill_opacity, get_stroke_opacity,
+ build_effect_xml, build_fill_xml,
+ get_element_opacity, get_fill_opacity, get_stroke_opacity,
)
from .elements import (
convert_rect, convert_circle, convert_ellipse,
@@ -154,6 +155,9 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
filter_id = resolve_url_id(elem.get('filter', ''))
style_overrides = _extract_inheritable_styles(elem)
+ local_opacity = get_element_opacity(elem)
+ if local_opacity is None:
+ local_opacity = 1.0
elem_id = elem.get('id')
semantic_role = elem.get('data-pptx-role')
@@ -194,12 +198,14 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
transform_matrix=parse_transform_matrix(transform),
filter_id=filter_id,
style_overrides=style_overrides,
+ opacity_multiplier=local_opacity,
)
elif rotate_pivot is not None:
child_ctx = ctx.child(
0, 0, 1.0, 1.0,
filter_id=filter_id,
style_overrides=style_overrides,
+ opacity_multiplier=local_opacity,
)
else:
child_ctx = ctx.child(
@@ -209,6 +215,13 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
sy,
filter_id=filter_id,
style_overrides=style_overrides,
+ opacity_multiplier=local_opacity,
+ )
+
+ if native_subtree_active and child_ctx.opacity_multiplier < 1.0:
+ raise SvgNativeConversionError(
+ "Group opacity cannot be applied to data-pptx-native table/chart "
+ "objects; export without --native-objects to use the SVG fallback"
)
if child_ctx.native_objects_enabled:
@@ -298,7 +311,10 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
group_effect = ''
if filter_id and filter_id in ctx.defs:
- group_effect = build_effect_xml(ctx.defs[filter_id])
+ group_effect = build_effect_xml(
+ ctx.defs[filter_id],
+ child_ctx.opacity_multiplier,
+ )
rot_emu = 0 if matrix_supported else int(angle_deg * 60000)
rot_attr = f' rot="{rot_emu}"' if rot_emu else ''
@@ -466,7 +482,11 @@ def _extract_background_candidate(
if child.get('transform') or child.get('filter') or child.get('clip-path'):
return '', None
style_overrides = _extract_inheritable_styles(child)
- child_ctx = ctx.child(style_overrides=style_overrides)
+ local_opacity = get_element_opacity(child)
+ child_ctx = ctx.child(
+ style_overrides=style_overrides,
+ opacity_multiplier=1.0 if local_opacity is None else local_opacity,
+ )
visual_children = [
grandchild for grandchild in child
if grandchild.tag.replace(f'{{{SVG_NS}}}', '') not in _NON_VISUAL_TAGS
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
index 0ad4e408..1da17327 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
@@ -363,7 +363,10 @@ def convert_rect(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
effect = ''
filt_id = get_effective_filter_id(elem, ctx)
if filt_id and filt_id in ctx.defs:
- effect = build_effect_xml(ctx.defs[filt_id])
+ effect = build_effect_xml(
+ ctx.defs[filt_id],
+ get_element_opacity(elem, ctx),
+ )
transform = elem.get('transform')
@@ -577,7 +580,10 @@ def convert_circle(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
effect = ''
filt_id = get_effective_filter_id(elem, ctx)
if filt_id and filt_id in ctx.defs:
- effect = build_effect_xml(ctx.defs[filt_id])
+ effect = build_effect_xml(
+ ctx.defs[filt_id],
+ get_element_opacity(elem, ctx),
+ )
shape_id = ctx.next_id()
return ShapeResult(
@@ -609,7 +615,10 @@ def convert_circle(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
effect = ''
filt_id = get_effective_filter_id(elem, ctx)
if filt_id and filt_id in ctx.defs:
- effect = build_effect_xml(ctx.defs[filt_id])
+ effect = build_effect_xml(
+ ctx.defs[filt_id],
+ get_element_opacity(elem, ctx),
+ )
geom = '<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>'
@@ -815,7 +824,10 @@ def convert_path(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
effect = ''
filt_id = get_effective_filter_id(elem, ctx)
if filt_id and filt_id in ctx.defs:
- effect = build_effect_xml(ctx.defs[filt_id])
+ effect = build_effect_xml(
+ ctx.defs[filt_id],
+ get_element_opacity(elem, ctx),
+ )
shape_id = ctx.next_id()
off_x = px_to_emu(min_x)
@@ -1272,6 +1284,16 @@ def _textbox_padding(font_size: float) -> float:
)
+def _text_opacity_ratio(value: str | None) -> float:
+ """Parse a text opacity component and clamp it to the SVG ``0..1`` range."""
+ if value is None:
+ return 1.0
+ try:
+ return max(0.0, min(1.0, float(value)))
+ except ValueError:
+ return 1.0
+
+
def _override_run_attrs(
parent_attrs: dict[str, Any],
tspan: ET.Element,
@@ -1283,6 +1305,27 @@ def _override_run_attrs(
def tspan_attr(name: str) -> str | None:
return inline_style.get(name) or tspan.get(name)
+ object_opacity = float(run_attrs.get('_object_opacity', 1.0))
+ fill_opacity = float(run_attrs.get('_fill_opacity', 1.0))
+ stroke_opacity = float(run_attrs.get('_stroke_opacity', 1.0))
+ if tspan_attr('opacity') is not None:
+ object_opacity *= _text_opacity_ratio(tspan_attr('opacity'))
+ if tspan_attr('fill-opacity') is not None:
+ fill_opacity = _text_opacity_ratio(tspan_attr('fill-opacity'))
+ if tspan_attr('stroke-opacity') is not None:
+ stroke_opacity = _text_opacity_ratio(tspan_attr('stroke-opacity'))
+ run_attrs['_object_opacity'] = object_opacity
+ run_attrs['_fill_opacity'] = fill_opacity
+ run_attrs['_stroke_opacity'] = stroke_opacity
+ effective_fill_opacity = object_opacity * fill_opacity
+ effective_stroke_opacity = object_opacity * stroke_opacity
+ run_attrs['opacity'] = (
+ effective_fill_opacity if effective_fill_opacity < 1.0 else None
+ )
+ run_attrs['stroke_opacity'] = (
+ effective_stroke_opacity if effective_stroke_opacity < 1.0 else None
+ )
+
if tspan_attr('font-weight'):
run_attrs['font_weight'] = tspan_attr('font-weight')
if tspan_attr('fill'):
@@ -1299,11 +1342,6 @@ def _override_run_attrs(
run_attrs.get('stroke_width', 1.0),
font_size=float(run_attrs.get('font_size', 16)),
)
- if tspan_attr('stroke-opacity'):
- try:
- run_attrs['stroke_opacity'] = float(tspan_attr('stroke-opacity') or '1')
- except ValueError:
- pass
if tspan_attr('font-size'):
run_attrs['font_size'] = parse_svg_length(
tspan_attr('font-size'),
@@ -1520,9 +1558,13 @@ def convert_text(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
fill_raw = _get_attr(elem, 'fill', ctx) or '#000000'
fill_color = parse_hex_color(fill_raw) or '000000'
opacity = get_fill_opacity(elem, ctx)
+ object_opacity = get_element_opacity(elem, ctx)
+ object_opacity = 1.0 if object_opacity is None else object_opacity
+ fill_opacity = _text_opacity_ratio(_get_attr(elem, 'fill-opacity', ctx))
stroke_raw = _get_attr(elem, 'stroke', ctx) or ''
stroke_width = svg_length_size(_get_attr(elem, 'stroke-width', ctx), ctx, 1.0)
stroke_opacity = get_stroke_opacity(elem, ctx)
+ stroke_opacity_value = _text_opacity_ratio(_get_attr(elem, 'stroke-opacity', ctx))
font_style = _get_attr(elem, 'font-style', ctx) or ''
text_decoration = _get_attr(elem, 'text-decoration', ctx) or ''
letter_spacing_px = _parse_letter_spacing_px(
@@ -1543,6 +1585,9 @@ def convert_text(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
'text_decoration': text_decoration,
'letter_spacing': letter_spacing_px,
'_scale_x': ctx.scale_x or 1.0,
+ '_object_opacity': object_opacity,
+ '_fill_opacity': fill_opacity,
+ '_stroke_opacity': stroke_opacity_value,
'opacity': opacity,
'stroke_raw': stroke_raw,
'stroke_width': stroke_width,
@@ -1724,9 +1769,15 @@ def convert_text(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
filter_elem = ctx.defs[filt_id]
effect_kind = classify_filter_effect(filter_elem)
if effect_kind == 'glow':
- text_effect_xml = build_effect_xml(filter_elem)
+ text_effect_xml = build_effect_xml(
+ filter_elem,
+ get_element_opacity(elem, ctx),
+ )
elif effect_kind == 'shadow':
- shape_effect_xml = build_effect_xml(filter_elem)
+ shape_effect_xml = build_effect_xml(
+ filter_elem,
+ get_element_opacity(elem, ctx),
+ )
shape_id = ctx.next_id()
rot_attr = f' rot="{text_rot}"' if text_rot else ''
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
index bf2ab7a5..556aa2bc 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
@@ -11,7 +11,7 @@ from .theme_colors import ThemeColorSpec, color_node_xml
from .utils import (
SVG_NS, ANGLE_UNIT, DASH_PRESETS,
px_to_emu, _f, _get_attr, parse_svg_length,
- parse_hex_color, parse_stop_style, resolve_url_id,
+ parse_hex_color, parse_inline_style, parse_stop_style, resolve_url_id,
)
@@ -534,7 +534,10 @@ def _shadow_dir_angle(dx: float, dy: float) -> int:
return int(angle_deg * ANGLE_UNIT)
-def build_shadow_xml(filter_elem: ET.Element) -> str:
+def build_shadow_xml(
+ filter_elem: ET.Element,
+ opacity: float | None = None,
+) -> str:
"""Build <a:effectLst> with <a:outerShdw> from SVG filter element.
SVG-to-DrawingML shadow mapping notes:
@@ -564,7 +567,8 @@ def build_shadow_xml(filter_elem: ET.Element) -> str:
# PowerPoint renders outerShdw alpha slightly heavier than SVG's filter
# composite (different blending path). Scale by 0.75 to match the SVG
# preview after blur has been corrected to 2.0× σ.
- alpha_val = int(p['opacity'] * 75000)
+ opacity_multiplier = 1.0 if opacity is None else opacity
+ alpha_val = int(p['opacity'] * opacity_multiplier * 75000)
algn = _infer_shadow_alignment(dx, dy)
return f'''<a:effectLst>
@@ -574,7 +578,10 @@ def build_shadow_xml(filter_elem: ET.Element) -> str:
</a:effectLst>'''
-def build_glow_xml(filter_elem: ET.Element) -> str:
+def build_glow_xml(
+ filter_elem: ET.Element,
+ opacity: float | None = None,
+) -> str:
"""Build <a:effectLst> with <a:glow> from SVG filter element.
Used for filters that have feGaussianBlur without meaningful feOffset,
@@ -585,7 +592,8 @@ def build_glow_xml(filter_elem: ET.Element) -> str:
p = _parse_filter_params(filter_elem)
rad = px_to_emu(p['std_dev'])
- alpha_val = int(p['opacity'] * 100000)
+ opacity_multiplier = 1.0 if opacity is None else opacity
+ alpha_val = int(p['opacity'] * opacity_multiplier * 100000)
return f'''<a:effectLst>
<a:glow rad="{rad}">
@@ -603,7 +611,10 @@ def classify_filter_effect(filter_elem: ET.Element) -> str | None:
return 'shadow' if p['has_offset'] else 'glow'
-def build_effect_xml(filter_elem: ET.Element) -> str:
+def build_effect_xml(
+ filter_elem: ET.Element,
+ opacity: float | None = None,
+) -> str:
"""Build effect XML by classifying the SVG filter as shadow or glow.
Classification rules:
@@ -615,9 +626,9 @@ def build_effect_xml(filter_elem: ET.Element) -> str:
effect_kind = classify_filter_effect(filter_elem)
if effect_kind == 'shadow':
- return build_shadow_xml(filter_elem)
+ return build_shadow_xml(filter_elem, opacity)
if effect_kind == 'glow':
- return build_glow_xml(filter_elem)
+ return build_glow_xml(filter_elem, opacity)
return ''
@@ -625,15 +636,19 @@ def get_element_opacity(
elem: ET.Element,
ctx: ConvertContext | None = None,
) -> float | None:
- """Get the effective element opacity, clamped to the SVG ``0..1`` range."""
- op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
+ """Get local opacity multiplied by any approximated ancestor group alpha."""
+ base = ctx.opacity_multiplier if ctx is not None else 1.0
+ if ctx is not None:
+ op = _get_attr(elem, 'opacity', ctx)
+ else:
+ op = parse_inline_style(elem.get('style')).get('opacity') or elem.get('opacity')
if op is None:
- return None
+ return base if base < 1.0 else None
try:
- val = max(0.0, min(1.0, float(op)))
+ val = base * max(0.0, min(1.0, float(op)))
return val if val < 1.0 else None
except ValueError:
- return None
+ return base if base < 1.0 else None
def get_fill_opacity(
@@ -645,19 +660,19 @@ def get_fill_opacity(
Returns:
Combined opacity value, or None if fully opaque.
"""
- base = 1.0
+ base = ctx.opacity_multiplier if ctx is not None else 1.0
op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
if op:
try:
- base = float(op)
+ base *= max(0.0, min(1.0, float(op)))
except ValueError:
pass
fill_op = _get_attr(elem, 'fill-opacity', ctx) if ctx else elem.get('fill-opacity')
if fill_op:
try:
- base *= float(fill_op)
+ base *= max(0.0, min(1.0, float(fill_op)))
except ValueError:
pass
@@ -673,19 +688,19 @@ def get_stroke_opacity(
Returns:
Combined opacity value, or None if fully opaque.
"""
- base = 1.0
+ base = ctx.opacity_multiplier if ctx is not None else 1.0
op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
if op:
try:
- base = float(op)
+ base *= max(0.0, min(1.0, float(op)))
except ValueError:
pass
stroke_op = _get_attr(elem, 'stroke-opacity', ctx) if ctx else elem.get('stroke-opacity')
if stroke_op:
try:
- base *= float(stroke_op)
+ base *= max(0.0, min(1.0, float(stroke_op)))
except ValueError:
pass
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
index 9065a2d1..30971a11 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
@@ -22,7 +22,7 @@ ANGLE_UNIT = 60000 # DrawingML angle: 60000ths of a degree
# SVG attributes inheritable from parent <g>
INHERITABLE_ATTRS = [
'fill', 'stroke', 'stroke-width', 'stroke-dasharray', 'stroke-linecap',
- 'stroke-linejoin', 'opacity', 'fill-opacity', 'stroke-opacity',
+ 'stroke-linejoin', 'fill-opacity', 'stroke-opacity',
'font-family', 'font-size', 'font-weight', 'font-style',
'text-anchor', 'letter-spacing', 'text-decoration',
]