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): correct <g rotate(angle,cx,cy)> pivot in grpSp export

hugohe318edb1666436
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 5332c79e..f662b9b5 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,
+ SVG_NS, EMU_PER_PX,
_extract_inheritable_styles, parse_transform_matrix, resolve_url_id,
)
from .drawingml_styles import build_effect_xml
@@ -97,6 +97,35 @@ def parse_transform(transform_str: str) -> tuple[float, float, float, float, flo
return e, f, sx, sy, angle_deg
+# ``rotate(angle)`` defaults to pivot (0,0); ``rotate(angle, cx, cy)`` rotates
+# around (cx, cy). DrawingML grpSp ``rot`` always rotates around the group's
+# own bounding-box centre — we need the SVG pivot so ``convert_g`` can
+# compensate for the offset between those two centres.
+_ROTATE_RE = re.compile(
+ r'rotate\(\s*([-\d.eE+]+)(?:[\s,]+([-\d.eE+]+)[\s,]+([-\d.eE+]+))?\s*\)'
+)
+
+
+def _extract_rotate_pivot(transform_str: str) -> tuple[float, float] | None:
+ """Return the (cx, cy) pivot of a sole ``rotate(...)`` in *transform_str*.
+
+ Returns ``None`` when the transform list contains anything other than one
+ rotate (other ops compose with rotate in a way the pivot-compensation
+ fallback can't express). A bare ``rotate(angle)`` returns (0, 0).
+ """
+ if not transform_str:
+ return None
+ ops = [op for op in re.findall(r'(\w+)\s*\(', transform_str) if op]
+ if ops != ['rotate']:
+ return None
+ match = _ROTATE_RE.search(transform_str)
+ if not match:
+ return None
+ cx = float(match.group(2)) if match.group(2) is not None else 0.0
+ cy = float(match.group(3)) if match.group(3) is not None else 0.0
+ return cx, cy
+
+
# ---------------------------------------------------------------------------
# Group handling
# ---------------------------------------------------------------------------
@@ -125,6 +154,15 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
matrix_supported = bool(transform) and visual_children and all(
_supports_matrix_transform(child) for child in visual_children
)
+ # A pure ``rotate(angle [cx cy])`` falls through to the fallback path
+ # below (children are rect/text/path/etc. that don't consume a full
+ # matrix). Decomposing the matrix produces translation components
+ # (e, f) that encode the pivot — handing those to children would
+ # *double-translate* them because grpSp's own ``rot`` already
+ # rotates around the group's bounding-box centre. Skip the child
+ # translation here and apply pivot-centre compensation to ``a:off``
+ # below instead.
+ rotate_pivot = _extract_rotate_pivot(transform) if not matrix_supported else None
if matrix_supported:
child_ctx = ctx.child(
0, 0, 1.0, 1.0,
@@ -132,6 +170,12 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
filter_id=filter_id,
style_overrides=style_overrides,
)
+ elif rotate_pivot is not None:
+ child_ctx = ctx.child(
+ 0, 0, 1.0, 1.0,
+ 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)
@@ -174,6 +218,31 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
group_w = max(int(max_x - min_x), 1)
group_h = max(int(max_y - min_y), 1)
+ # ``rotate(angle, cx, cy)`` rotates around the SVG pivot, but DrawingML
+ # grpSp ``rot`` always rotates around the group's own bbox centre. When
+ # those centres differ, the visual position drifts by exactly the
+ # translation a rotate-around-pivot equals. Compensate by offsetting the
+ # outer <a:off> only; <a:chOff> stays on the unshifted bbox so children
+ # (still at their original SVG positions because rotate_pivot suppressed
+ # the dx/dy translation above) remain aligned inside the group.
+ off_x = group_x
+ off_y = group_y
+ if rotate_pivot is not None and angle_deg:
+ cx_svg, cy_svg = rotate_pivot
+ pivot_ex = (cx_svg + ctx.translate_x) * EMU_PER_PX
+ pivot_ey = (cy_svg + ctx.translate_y) * EMU_PER_PX
+ bbox_cx = group_x + group_w / 2
+ bbox_cy = group_y + group_h / 2
+ theta = math.radians(angle_deg)
+ cos_t = math.cos(theta)
+ sin_t = math.sin(theta)
+ # Where the bbox centre lands after rotating around the pivot, minus
+ # where DrawingML's grpSp rot would leave it (i.e. unchanged).
+ delta_x = (bbox_cx - pivot_ex) * cos_t - (bbox_cy - pivot_ey) * sin_t + pivot_ex - bbox_cx
+ delta_y = (bbox_cx - pivot_ex) * sin_t + (bbox_cy - pivot_ey) * cos_t + pivot_ey - bbox_cy
+ off_x = int(round(group_x + delta_x))
+ off_y = int(round(group_y + delta_y))
+
shapes_xml = '\n'.join(result.xml for result in child_results)
group_id = ctx.next_id()
@@ -200,7 +269,7 @@ def convert_g(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
</p:nvGrpSpPr>
<p:grpSpPr>
<a:xfrm{rot_attr}>
-<a:off x="{group_x}" y="{group_y}"/>
+<a:off x="{off_x}" y="{off_y}"/>
<a:ext cx="{group_w}" cy="{group_h}"/>
<a:chOff x="{group_x}" y="{group_y}"/>
<a:chExt cx="{group_w}" cy="{group_h}"/>