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

fix(svg_to_pptx): preserve affine transforms for native pictures

hugohe33b5a1c520391
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 f0d1a89c..df0e2e74 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_context.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_context.py
@@ -6,6 +6,9 @@ from pathlib import Path
from xml.etree import ElementTree as ET
from dataclasses import dataclass, field
+AffineMatrix = tuple[float, float, float, float, float, float]
+IDENTITY_MATRIX: AffineMatrix = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)
+
@dataclass
class ShapeResult:
@@ -30,6 +33,8 @@ class ConvertContext:
translate_y: float = 0.0
scale_x: float = 1.0
scale_y: float = 1.0
+ transform_matrix: AffineMatrix = IDENTITY_MATRIX
+ use_transform_matrix: bool = False
filter_id: str | None = None
media_files: dict[str, bytes] = field(default_factory=dict)
rel_entries: list[dict[str, str]] = field(default_factory=list)
@@ -60,6 +65,7 @@ class ConvertContext:
dy: float = 0,
sx: float = 1.0,
sy: float = 1.0,
+ transform_matrix: AffineMatrix | None = None,
filter_id: str | None = None,
style_overrides: dict[str, str] | None = None,
) -> ConvertContext:
@@ -70,9 +76,23 @@ class ConvertContext:
dy: Y translation delta.
sx: X scale factor.
sy: Y scale factor.
+ transform_matrix: Full affine transform to accumulate for
+ converters that can faithfully map it to DrawingML.
filter_id: Override filter ID.
style_overrides: Style attribute overrides from child element.
"""
+ local_matrix = transform_matrix or IDENTITY_MATRIX
+ a1, b1, c1, d1, e1, f1 = self.transform_matrix
+ a2, b2, c2, d2, e2, f2 = local_matrix
+ combined_matrix: AffineMatrix = (
+ a1 * a2 + c1 * b2,
+ b1 * a2 + d1 * b2,
+ a1 * c2 + c1 * d2,
+ b1 * c2 + d1 * d2,
+ a1 * e2 + c1 * f2 + e1,
+ b1 * e2 + d1 * f2 + f1,
+ )
+
merged = dict(self.inherited_styles)
if style_overrides:
@@ -101,6 +121,8 @@ class ConvertContext:
translate_y=self.translate_y + dy,
scale_x=self.scale_x * sx,
scale_y=self.scale_y * sy,
+ transform_matrix=combined_matrix,
+ use_transform_matrix=self.use_transform_matrix or transform_matrix is not None,
filter_id=filter_id or self.filter_id,
media_files=self.media_files,
rel_entries=self.rel_entries,
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 0881fae0..d4a33cb8 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_converter.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_converter.py
@@ -9,7 +9,7 @@ from xml.etree import ElementTree as ET
from .drawingml_context import ConvertContext, ShapeResult
from .drawingml_utils import (
SVG_NS,
- _extract_inheritable_styles, resolve_url_id,
+ _extract_inheritable_styles, parse_transform_matrix, resolve_url_id,
)
from .drawingml_styles import build_effect_xml
from .drawingml_elements import (
@@ -100,7 +100,25 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
filter_id = resolve_url_id(elem.get('filter', ''))
style_overrides = _extract_inheritable_styles(elem)
- child_ctx = ctx.child(dx, dy, sx, sy, filter_id, style_overrides)
+
+ elem_id = elem.get('id')
+ should_animate_group = ctx.depth == 0 and elem_id and not _is_chrome_id(elem_id)
+ visual_children = [
+ child for child in elem
+ if child.tag.replace(f'{{{SVG_NS}}}', '') not in _NON_VISUAL_TAGS
+ ]
+ matrix_supported = bool(transform) and visual_children and all(
+ _supports_matrix_transform(child) for child in visual_children
+ )
+ if matrix_supported:
+ child_ctx = ctx.child(
+ 0, 0, 1.0, 1.0,
+ transform_matrix=parse_transform_matrix(transform),
+ filter_id=filter_id,
+ style_overrides=style_overrides,
+ )
+ else:
+ child_ctx = ctx.child(dx, dy, sx, sy, filter_id=filter_id, style_overrides=style_overrides)
child_results: list[ShapeResult] = []
for child in elem:
@@ -113,9 +131,6 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
if not child_results:
return None
- elem_id = elem.get('id')
- should_animate_group = ctx.depth == 0 and elem_id and not _is_chrome_id(elem_id)
-
# Single-child non-semantic groups are flattened to reduce nesting. Top-level
# semantic groups are preserved so animations target the group, not its
# individual child shapes.
@@ -159,7 +174,7 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
if filter_id and filter_id in ctx.defs:
group_effect = build_effect_xml(ctx.defs[filter_id])
- rot_emu = int(angle_deg * 60000)
+ rot_emu = 0 if matrix_supported else int(angle_deg * 60000)
rot_attr = f' rot="{rot_emu}"' if rot_emu else ''
return ShapeResult(xml=f'''<p:grpSp>
@@ -187,6 +202,30 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
_NON_VISUAL_TAGS = frozenset(('defs', 'title', 'desc', 'metadata', 'style'))
+
+def _supports_matrix_transform(elem: ET.Element) -> bool:
+ """Return whether this subtree can consume a full affine matrix directly."""
+ tag = elem.tag.replace(f'{{{SVG_NS}}}', '')
+ if tag == 'image':
+ return True
+ if tag == 'svg':
+ visual_children = [
+ child for child in elem
+ if child.tag.replace(f'{{{SVG_NS}}}', '') not in _NON_VISUAL_TAGS
+ ]
+ return len(visual_children) == 1 and (
+ visual_children[0].tag.replace(f'{{{SVG_NS}}}', '') == 'image'
+ )
+ if tag == 'g':
+ visual_children = [
+ child for child in elem
+ if child.tag.replace(f'{{{SVG_NS}}}', '') not in _NON_VISUAL_TAGS
+ ]
+ return bool(visual_children) and all(
+ _supports_matrix_transform(child) for child in visual_children
+ )
+ return False
+
_CONVERTERS = {
'rect': convert_rect,
'circle': convert_circle,
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 bbb27941..d435e5b6 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
@@ -14,6 +14,7 @@ from .drawingml_utils import (
SVG_NS, XLINK_NS, ANGLE_UNIT, FONT_PX_TO_HUNDREDTHS_PT, DASH_PRESETS,
px_to_emu, _f, _get_attr,
ctx_x, ctx_y, ctx_w, ctx_h,
+ rect_to_dml_xfrm,
parse_hex_color, resolve_url_id, get_effective_filter_id,
parse_font_family, is_cjk_char, estimate_text_width,
_xml_escape,
@@ -1250,6 +1251,28 @@ def _resolve_clip_geometry(
# image
# ---------------------------------------------------------------------------
+def _picture_xfrm_from_rect(
+ ctx: ConvertContext,
+ x: float,
+ y: float,
+ w: float,
+ h: float,
+) -> tuple[str, int, int, int, int, tuple[int, int, int, int]]:
+ """Build DrawingML xfrm data for a picture rectangle."""
+ if ctx.use_transform_matrix:
+ return rect_to_dml_xfrm(x, y, w, h, ctx.transform_matrix)
+
+ x_t = ctx_x(x, ctx)
+ y_t = ctx_y(y, ctx)
+ w_t = ctx_w(w, ctx)
+ h_t = ctx_h(h, ctx)
+ off_x = px_to_emu(x_t)
+ off_y = px_to_emu(y_t)
+ ext_cx = px_to_emu(w_t)
+ ext_cy = px_to_emu(h_t)
+ return '', off_x, off_y, ext_cx, ext_cy, (off_x, off_y, off_x + ext_cx, off_y + ext_cy)
+
+
def _read_image_size(data: bytes) -> tuple[int | None, int | None]:
"""Read intrinsic image dimensions (width, height) from raw bytes.
@@ -1420,10 +1443,16 @@ def convert_image(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
raw_w = _f(elem.get('width'))
raw_h = _f(elem.get('height'))
- x = ctx_x(raw_x, ctx)
- y = ctx_y(raw_y, ctx)
- w = ctx_w(raw_w, ctx)
- h = ctx_h(raw_h, ctx)
+ if ctx.use_transform_matrix:
+ x = raw_x
+ y = raw_y
+ w = raw_w
+ h = raw_h
+ else:
+ x = ctx_x(raw_x, ctx)
+ y = ctx_y(raw_y, ctx)
+ w = ctx_w(raw_w, ctx)
+ h = ctx_h(raw_h, ctx)
if w <= 0 or h <= 0:
return None
@@ -1464,7 +1493,7 @@ def convert_image(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
rot = 0
transform = elem.get('transform')
- if transform:
+ if transform and not ctx.use_transform_matrix:
r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
if r_match:
rot = int(float(r_match.group(1)) * ANGLE_UNIT)
@@ -1489,15 +1518,15 @@ def convert_image(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
shape_id = ctx.next_id()
if meet_fit is not None:
dx, dy, fit_w, fit_h = meet_fit
- off_x = px_to_emu(x + dx)
- off_y = px_to_emu(y + dy)
- ext_cx = px_to_emu(fit_w)
- ext_cy = px_to_emu(fit_h)
+ xfrm_attr, off_x, off_y, ext_cx, ext_cy, bounds_emu = _picture_xfrm_from_rect(
+ ctx, x + dx, y + dy, fit_w, fit_h,
+ )
else:
- off_x = px_to_emu(x)
- off_y = px_to_emu(y)
- ext_cx = px_to_emu(w)
- ext_cy = px_to_emu(h)
+ xfrm_attr, off_x, off_y, ext_cx, ext_cy, bounds_emu = _picture_xfrm_from_rect(
+ ctx, x, y, w, h,
+ )
+ if rot_attr:
+ xfrm_attr += rot_attr
return ShapeResult(xml=f'''<p:pic>
<p:nvPicPr>
@@ -1510,11 +1539,11 @@ def convert_image(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
{src_rect_xml}<a:stretch><a:fillRect/></a:stretch>
</p:blipFill>
<p:spPr>
-<a:xfrm{rot_attr}><a:off x="{off_x}" y="{off_y}"/>
+<a:xfrm{xfrm_attr}><a:off x="{off_x}" y="{off_y}"/>
<a:ext cx="{ext_cx}" cy="{ext_cy}"/></a:xfrm>
{clip_geom}
</p:spPr>
-</p:pic>''', bounds_emu=(off_x, off_y, off_x + ext_cx, off_y + ext_cy))
+</p:pic>''', bounds_emu=bounds_emu)
# ---------------------------------------------------------------------------
@@ -1603,10 +1632,16 @@ def convert_nested_svg(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | N
if svg_w <= 0 or svg_h <= 0:
return None
- x = ctx_x(svg_x, ctx)
- y = ctx_y(svg_y, ctx)
- w = ctx_w(svg_w, ctx)
- h = ctx_h(svg_h, ctx)
+ if ctx.use_transform_matrix:
+ x = svg_x
+ y = svg_y
+ w = svg_w
+ h = svg_h
+ else:
+ x = ctx_x(svg_x, ctx)
+ y = ctx_y(svg_y, ctx)
+ w = ctx_w(svg_w, ctx)
+ h = ctx_h(svg_h, ctx)
src_rect_xml = ''
view_box = elem.get('viewBox', '')
@@ -1656,17 +1691,18 @@ def convert_nested_svg(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | N
rot = 0
transform = elem.get('transform')
- if transform:
+ if transform and not ctx.use_transform_matrix:
r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
if r_match:
rot = int(float(r_match.group(1)) * ANGLE_UNIT)
rot_attr = f' rot="{rot}"' if rot else ''
shape_id = ctx.next_id()
- off_x = px_to_emu(x)
- off_y = px_to_emu(y)
- ext_cx = px_to_emu(w)
- ext_cy = px_to_emu(h)
+ xfrm_attr, off_x, off_y, ext_cx, ext_cy, bounds_emu = _picture_xfrm_from_rect(
+ ctx, x, y, w, h,
+ )
+ if rot_attr:
+ xfrm_attr += rot_attr
return ShapeResult(xml=f'''<p:pic>
<p:nvPicPr>
@@ -1679,8 +1715,8 @@ def convert_nested_svg(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | N
{src_rect_xml}<a:stretch><a:fillRect/></a:stretch>
</p:blipFill>
<p:spPr>
-<a:xfrm{rot_attr}><a:off x="{off_x}" y="{off_y}"/>
+<a:xfrm{xfrm_attr}><a:off x="{off_x}" y="{off_y}"/>
<a:ext cx="{ext_cx}" cy="{ext_cy}"/></a:xfrm>
<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
</p:spPr>
-</p:pic>''', bounds_emu=(off_x, off_y, off_x + ext_cx, off_y + ext_cy))
+</p:pic>''', bounds_emu=bounds_emu)
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 a4d0c84f..ed6fbf91 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
@@ -3,9 +3,10 @@
from __future__ import annotations
import re
+import math
from xml.etree import ElementTree as ET
-from .drawingml_context import ConvertContext
+from .drawingml_context import AffineMatrix, ConvertContext, IDENTITY_MATRIX
# ---------------------------------------------------------------------------
# Constants
@@ -129,6 +130,140 @@ def _f(val: str | None, default: float = 0.0) -> float:
return default
+# ---------------------------------------------------------------------------
+# SVG transform matrix helpers
+# ---------------------------------------------------------------------------
+
+_TRANSFORM_RE = re.compile(r'([a-zA-Z]+)\(([^)]*)\)')
+_NUMBER_RE = re.compile(r'[-+]?(?:\d*\.\d+|\d+\.?)(?:[eE][-+]?\d+)?')
+
+
+def matrix_multiply(left: AffineMatrix, right: AffineMatrix) -> AffineMatrix:
+ """Compose two SVG affine matrices, applying ``right`` before ``left``."""
+ a1, b1, c1, d1, e1, f1 = left
+ a2, b2, c2, d2, e2, f2 = right
+ return (
+ a1 * a2 + c1 * b2,
+ b1 * a2 + d1 * b2,
+ a1 * c2 + c1 * d2,
+ b1 * c2 + d1 * d2,
+ a1 * e2 + c1 * f2 + e1,
+ b1 * e2 + d1 * f2 + f1,
+ )
+
+
+def _translate_matrix(tx: float, ty: float = 0.0) -> AffineMatrix:
+ return (1.0, 0.0, 0.0, 1.0, tx, ty)
+
+
+def _scale_matrix(sx: float, sy: float | None = None) -> AffineMatrix:
+ return (sx, 0.0, 0.0, sx if sy is None else sy, 0.0, 0.0)
+
+
+def _rotate_matrix(angle_deg: float, cx: float | None = None, cy: float | None = None) -> AffineMatrix:
+ rad = math.radians(angle_deg)
+ cos_a = math.cos(rad)
+ sin_a = math.sin(rad)
+ rot = (cos_a, sin_a, -sin_a, cos_a, 0.0, 0.0)
+ if cx is None or cy is None:
+ return rot
+ return matrix_multiply(
+ matrix_multiply(_translate_matrix(cx, cy), rot),
+ _translate_matrix(-cx, -cy),
+ )
+
+
+def parse_transform_matrix(transform_str: str) -> AffineMatrix:
+ """Parse an SVG transform list into one affine matrix."""
+ if not transform_str:
+ return IDENTITY_MATRIX
+
+ matrix = IDENTITY_MATRIX
+ for name, raw_args in _TRANSFORM_RE.findall(transform_str):
+ args = [float(n) for n in _NUMBER_RE.findall(raw_args)]
+ name = name.lower()
+ local = IDENTITY_MATRIX
+
+ if name == 'matrix' and len(args) >= 6:
+ local = (args[0], args[1], args[2], args[3], args[4], args[5])
+ elif name == 'translate' and args:
+ local = _translate_matrix(args[0], args[1] if len(args) > 1 else 0.0)
+ elif name == 'scale' and args:
+ local = _scale_matrix(args[0], args[1] if len(args) > 1 else None)
+ elif name == 'rotate' and args:
+ local = _rotate_matrix(
+ args[0],
+ args[1] if len(args) > 2 else None,
+ args[2] if len(args) > 2 else None,
+ )
+
+ matrix = matrix_multiply(matrix, local)
+
+ return matrix
+
+
+def transform_point(matrix: AffineMatrix, x: float, y: float) -> tuple[float, float]:
+ """Apply an SVG affine matrix to a point."""
+ a, b, c, d, e, f = matrix
+ return a * x + c * y + e, b * x + d * y + f
+
+
+def rect_to_dml_xfrm(
+ x: float,
+ y: float,
+ w: float,
+ h: float,
+ matrix: AffineMatrix,
+) -> tuple[str, int, int, int, int, tuple[int, int, int, int]]:
+ """Map a transformed SVG rectangle to DrawingML xfrm attributes.
+
+ DrawingML can represent rotated/flipped rectangles, but not arbitrary
+ shear. Template-import picture wrappers only use translate/rotate/scale,
+ so decomposing the transformed local X/Y axes is sufficient here.
+ """
+ p0 = transform_point(matrix, x, y)
+ p1 = transform_point(matrix, x + w, y)
+ p2 = transform_point(matrix, x + w, y + h)
+ p3 = transform_point(matrix, x, y + h)
+
+ ux = p1[0] - p0[0]
+ uy = p1[1] - p0[1]
+ vx = p3[0] - p0[0]
+ vy = p3[1] - p0[1]
+
+ rect_w = max(math.hypot(ux, uy), 0.001)
+ rect_h = max(math.hypot(vx, vy), 0.001)
+ cross = ux * vy - uy * vx
+
+ if cross < 0:
+ angle_deg = math.degrees(math.atan2(-uy, -ux))
+ flip_attr = ' flipH="1"'
+ else:
+ angle_deg = math.degrees(math.atan2(uy, ux))
+ flip_attr = ''
+
+ rot = round(angle_deg * ANGLE_UNIT)
+ rot_attr = f' rot="{rot}"' if rot else ''
+
+ center_x = (p0[0] + p2[0]) / 2
+ center_y = (p0[1] + p2[1]) / 2
+ off_x = px_to_emu(center_x - rect_w / 2)
+ off_y = px_to_emu(center_y - rect_h / 2)
+ ext_cx = px_to_emu(rect_w)
+ ext_cy = px_to_emu(rect_h)
+
+ xs = [p0[0], p1[0], p2[0], p3[0]]
+ ys = [p0[1], p1[1], p2[1], p3[1]]
+ bounds = (
+ px_to_emu(min(xs)),
+ px_to_emu(min(ys)),
+ px_to_emu(max(xs)),
+ px_to_emu(max(ys)),
+ )
+
+ return f'{flip_attr}{rot_attr}', off_x, off_y, ext_cx, ext_cy, bounds
+
+
def _extract_inheritable_styles(elem: ET.Element) -> dict[str, str]:
"""Extract all SVG-inheritable presentation attributes from an element."""
styles: dict[str, str] = {}