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(ppt): preserve round2SameRect SVG round-trip

hugohe3010006fd9eec
diff --git a/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
index 758b7e25..b4be3761 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
@@ -179,20 +179,73 @@ def _resolve_blip_fill(_elem, _palette, _prefix, _seq, _placeholder_hex) -> Fill
def _resolve_patt_fill(elem: ET.Element, palette: ColorPalette | None,
- _prefix, _seq, placeholder_hex: str | None) -> FillResult:
- """Pattern fills (<a:pattFill prst="..."/> with fg/bg colors). Approximate
- with the foreground color so the shape isn't transparent. Future work:
- emit a real <pattern> in defs.
- """
+ prefix, seq, placeholder_hex: str | None) -> FillResult:
+ """Pattern fills (<a:pattFill prst="..."/> with fg/bg colors)."""
fg = elem.find("a:fgClr", NS)
- color_elem = find_color_elem(fg)
- hex_, alpha = resolve_color(color_elem, palette, placeholder_hex=placeholder_hex)
- if hex_ is None:
+ bg = elem.find("a:bgClr", NS)
+ fg_hex, fg_alpha = resolve_color(
+ find_color_elem(fg), palette, placeholder_hex=placeholder_hex,
+ )
+ bg_hex, bg_alpha = resolve_color(
+ find_color_elem(bg), palette, placeholder_hex=placeholder_hex,
+ )
+ if fg_hex is None:
return FillResult.inherit()
- attrs: dict[str, str] = {"fill": hex_}
- if alpha < 1.0:
- attrs["fill-opacity"] = fmt_num(alpha, 4)
- return FillResult(attrs=attrs)
+
+ prst = elem.attrib.get("prst", "")
+ if prst in {"ltUpDiag", "ltDnDiag"} and bg_hex is not None:
+ if _hex_distance(fg_hex, bg_hex) < 36:
+ attrs: dict[str, str] = {"fill": bg_hex}
+ if bg_alpha < 1.0:
+ attrs["fill-opacity"] = fmt_num(bg_alpha, 4)
+ return FillResult(attrs=attrs)
+
+ if prst not in {"ltUpDiag", "ltDnDiag"}:
+ attrs: dict[str, str] = {"fill": fg_hex}
+ if fg_alpha < 1.0:
+ attrs["fill-opacity"] = fmt_num(fg_alpha, 4)
+ return FillResult(attrs=attrs)
+
+ if seq is None:
+ seq = [0]
+ seq[0] += 1
+ pattern_id = f"{prefix}patt{seq[0]}"
+ bg_rect = ""
+ if bg_hex is not None:
+ bg_opacity = (
+ f' fill-opacity="{fmt_num(bg_alpha, 4)}"'
+ if bg_alpha < 1.0 else ""
+ )
+ bg_rect = f'<rect width="8" height="8" fill="{bg_hex}"{bg_opacity}/>'
+ fg_opacity = (
+ f' stroke-opacity="{fmt_num(fg_alpha, 4)}"'
+ if fg_alpha < 1.0 else ""
+ )
+ path_d = (
+ "M -2 8 L 8 -2 M 0 10 L 10 0"
+ if prst == "ltUpDiag"
+ else "M -2 0 L 8 10 M 0 -2 L 10 8"
+ )
+ pattern_xml = (
+ f'<pattern id="{pattern_id}" patternUnits="userSpaceOnUse" '
+ f'width="8" height="8">{bg_rect}'
+ f'<path d="{path_d}" stroke="{fg_hex}"{fg_opacity} '
+ f'stroke-width="1" fill="none"/></pattern>'
+ )
+ return FillResult(
+ attrs={"fill": f"url(#{pattern_id})"},
+ defs=[pattern_xml],
+ )
+
+
+def _hex_distance(a: str, b: str) -> float:
+ """Euclidean distance between two #RRGGBB colors."""
+ try:
+ ar, ag, ab = int(a[1:3], 16), int(a[3:5], 16), int(a[5:7], 16)
+ br, bg, bb = int(b[1:3], 16), int(b[3:5], 16), int(b[5:7], 16)
+ except (ValueError, IndexError):
+ return 255.0
+ return math.sqrt((ar - br) ** 2 + (ag - bg) ** 2 + (ab - bb) ** 2)
# ---------------------------------------------------------------------------
diff --git a/skills/ppt-master/scripts/pptx_to_svg/prstgeom_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/prstgeom_to_svg.py
index 8f13ada5..90ce979f 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/prstgeom_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/prstgeom_to_svg.py
@@ -69,6 +69,25 @@ def _adj_value(sp_pr: ET.Element | None, adj_name: str = "adj",
return default_pct
+def _adj_int_value(sp_pr: ET.Element | None, adj_name: str,
+ default: int) -> int:
+ """Read an adjustment value as the original DrawingML integer."""
+ if sp_pr is None:
+ return default
+ av_lst = sp_pr.find(".//a:avLst", NS)
+ if av_lst is None:
+ return default
+ for gd in av_lst.findall("a:gd", NS):
+ if gd.attrib.get("name") == adj_name:
+ fmla = gd.attrib.get("fmla", "")
+ if fmla.startswith("val "):
+ try:
+ return int(float(fmla[4:]))
+ except ValueError:
+ return default
+ return default
+
+
# ---------------------------------------------------------------------------
# Dispatch
# ---------------------------------------------------------------------------
@@ -159,6 +178,62 @@ def _round_2_diag_rect(xfrm: Xfrm, sp_pr) -> GeomResult:
return GeomResult(tag="path", path_d=d)
+def _round_2_same_rect(xfrm: Xfrm, sp_pr) -> GeomResult:
+ """Rectangle with top and bottom same-side corner adjustments.
+
+ Mirrors the OOXML preset definition: adj1 controls the top pair of corners,
+ adj2 controls the bottom pair. A common title-tab setting is adj1=0 and
+ adj2=50000, yielding square top corners and a capsule-like bottom edge.
+ """
+ x, y, w, h = xfrm.x, xfrm.y, xfrm.w, xfrm.h
+ ss = min(w, h)
+ adj1 = _adj_int_value(sp_pr, "adj1", 16667)
+ adj2 = _adj_int_value(sp_pr, "adj2", 0)
+ tx = min(adj1 / 100000.0, 0.5) * ss
+ bx = min(adj2 / 100000.0, 0.5) * ss
+ parts = [
+ f"M {fmt_num(x + tx)} {fmt_num(y)}",
+ f"L {fmt_num(x + w - tx)} {fmt_num(y)}",
+ ]
+ if tx > 0:
+ parts.append(
+ f"A {fmt_num(tx)} {fmt_num(tx)} 0 0 1 "
+ f"{fmt_num(x + w)} {fmt_num(y + tx)}"
+ )
+ parts.append(f"L {fmt_num(x + w)} {fmt_num(y + h - bx)}")
+ if bx > 0:
+ parts.append(
+ f"A {fmt_num(bx)} {fmt_num(bx)} 0 0 1 "
+ f"{fmt_num(x + w - bx)} {fmt_num(y + h)}"
+ )
+ else:
+ parts.append(f"L {fmt_num(x + w)} {fmt_num(y + h)}")
+ parts.append(f"L {fmt_num(x + bx)} {fmt_num(y + h)}")
+ if bx > 0:
+ parts.append(
+ f"A {fmt_num(bx)} {fmt_num(bx)} 0 0 1 "
+ f"{fmt_num(x)} {fmt_num(y + h - bx)}"
+ )
+ else:
+ parts.append(f"L {fmt_num(x)} {fmt_num(y + h)}")
+ parts.append(f"L {fmt_num(x)} {fmt_num(y + tx)}")
+ if tx > 0:
+ parts.append(
+ f"A {fmt_num(tx)} {fmt_num(tx)} 0 0 1 "
+ f"{fmt_num(x + tx)} {fmt_num(y)}"
+ )
+ d = " ".join(parts) + " Z"
+ return GeomResult(
+ tag="path",
+ attrs={
+ "data-pptx-prst": "round2SameRect",
+ "data-pptx-adj1": str(adj1),
+ "data-pptx-adj2": str(adj2),
+ },
+ path_d=d,
+ )
+
+
def _ellipse(xfrm: Xfrm, _sp_pr) -> GeomResult:
cx = xfrm.x + xfrm.w / 2.0
cy = xfrm.y + xfrm.h / 2.0
@@ -462,6 +537,7 @@ _PRESET_HANDLERS = {
"rect": _rect,
"roundRect": _round_rect,
"round2DiagRect": _round_2_diag_rect,
+ "round2SameRect": _round_2_same_rect,
"ellipse": _ellipse,
"line": _line,
diff --git a/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
index d67021ed..da884527 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/slide_to_svg.py
@@ -314,16 +314,19 @@ def _convert_shape(node: ShapeNode, ctx: AssemblyContext, *, top_level: bool) ->
# Text body (a:txBody)
tx_body = node.xml.find("p:txBody", NS)
- is_vertical = is_vertical_txbody(tx_body)
+ is_vertical = is_vertical_txbody(tx_body, node.xfrm)
+ text_default_fill = _resolve_text_style_default(node, ctx)
if tx_body is not None and is_vertical:
text_result = convert_vertical_txbody(
tx_body, node.xfrm, ctx.palette,
theme_fonts=ctx.theme_fonts,
+ default_fill=text_default_fill,
)
else:
text_result = convert_txbody(
tx_body, node.xfrm, ctx.palette,
theme_fonts=ctx.theme_fonts,
+ default_fill=text_default_fill,
) if tx_body is not None else TextResult()
if is_vertical:
@@ -448,6 +451,16 @@ def _resolve_shape_style_defaults(node: ShapeNode, ctx: AssemblyContext) -> dict
return defaults
+def _resolve_text_style_default(node: ShapeNode, ctx: AssemblyContext) -> str:
+ """Resolve p:style fontRef color used by runs without explicit fill."""
+ style = node.xml.find("p:style", NS)
+ if style is None:
+ return "#000000"
+ font_ref = style.find("a:fontRef", NS)
+ font_color = _resolve_ref_color(font_ref, ctx)
+ return font_color or "#000000"
+
+
def _resolve_ref_color(ref_elem: ET.Element | None, ctx: AssemblyContext) -> str | None:
color_elem = find_color_elem(ref_elem)
hex_, _alpha = resolve_color(color_elem, ctx.palette)
diff --git a/skills/ppt-master/scripts/pptx_to_svg/txbody_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/txbody_to_svg.py
index ed80f51a..4df59b8f 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/txbody_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/txbody_to_svg.py
@@ -101,13 +101,16 @@ def convert_txbody(
palette: ColorPalette | None,
*,
theme_fonts: dict[str, str] | None = None,
+ default_fill: str = DEFAULT_FILL_HEX,
) -> TextResult:
"""Convert <p:txBody> under the given shape geometry to SVG <text>(s)."""
if tx_body is None:
return TextResult()
body_pr = tx_body.find("a:bodyPr", NS)
- paragraphs = _parse_paragraphs(tx_body, palette, theme_fonts or {})
+ paragraphs = _parse_paragraphs(
+ tx_body, palette, theme_fonts or {}, default_fill=default_fill,
+ )
if not paragraphs or not _has_visible_text(paragraphs):
return TextResult()
@@ -159,13 +162,15 @@ def convert_txbody(
return TextResult(svg="\n".join(text_blocks))
-def is_vertical_txbody(tx_body: ET.Element | None) -> bool:
+def is_vertical_txbody(tx_body: ET.Element | None, xfrm: Xfrm | None = None) -> bool:
if tx_body is None:
return False
body_pr = tx_body.find("a:bodyPr", NS)
if body_pr is None:
return False
- return body_pr.attrib.get("vert") in VERTICAL_TEXT_MODES
+ if body_pr.attrib.get("vert") in VERTICAL_TEXT_MODES:
+ return True
+ return _looks_like_auto_stacked_cjk(tx_body, body_pr, xfrm)
def convert_vertical_txbody(
@@ -174,6 +179,7 @@ def convert_vertical_txbody(
palette: ColorPalette | None,
*,
theme_fonts: dict[str, str] | None = None,
+ default_fill: str = DEFAULT_FILL_HEX,
) -> TextResult:
"""Render East Asian vertical text as upright stacked glyphs.
@@ -185,7 +191,9 @@ def convert_vertical_txbody(
if tx_body is None:
return TextResult()
- paragraphs = _parse_paragraphs(tx_body, palette, theme_fonts or {})
+ paragraphs = _parse_paragraphs(
+ tx_body, palette, theme_fonts or {}, default_fill=default_fill,
+ )
runs = [
run
for para in paragraphs
@@ -254,6 +262,42 @@ def _rotated_bbox(xfrm: Xfrm) -> tuple[float, float, float, float]:
return xfrm.x, xfrm.y, xfrm.w, xfrm.h
+def _looks_like_auto_stacked_cjk(
+ tx_body: ET.Element,
+ body_pr: ET.Element,
+ xfrm: Xfrm | None,
+) -> bool:
+ """Detect PowerPoint's narrow-box CJK vertical layout without vert=eaVert."""
+ if xfrm is None or xfrm.w <= 0 or xfrm.h <= 0:
+ return False
+ if body_pr.attrib.get("wrap", "square") != "square":
+ return False
+ if xfrm.w > 64 or xfrm.h < xfrm.w * 2.4:
+ return False
+
+ text = _plain_text(tx_body)
+ chars = [ch for ch in text if not ch.isspace()]
+ if len(chars) < 3 or len(chars) > 16:
+ return False
+ cjk_count = sum(1 for ch in chars if _is_cjk(ch))
+ if cjk_count / len(chars) < 0.8:
+ return False
+
+ lins = _read_emu_attr(body_pr, "lIns", DEFAULT_INSETS_EMU["l"])
+ rins = _read_emu_attr(body_pr, "rIns", DEFAULT_INSETS_EMU["r"])
+ inner_w = max(xfrm.w - lins - rins, 1.0)
+ return inner_w <= DEFAULT_FONT_SIZE_PX
+
+
+def _plain_text(tx_body: ET.Element) -> str:
+ """Return concatenated literal text for layout heuristics."""
+ parts: list[str] = []
+ for text_elem in tx_body.findall(".//a:t", NS):
+ if text_elem.text:
+ parts.append(text_elem.text)
+ return "".join(parts)
+
+
# ---------------------------------------------------------------------------
# Parsing helpers
# ---------------------------------------------------------------------------
@@ -275,13 +319,18 @@ def _parse_paragraphs(
tx_body: ET.Element,
palette: ColorPalette | None,
theme_fonts: dict[str, str],
+ *,
+ default_fill: str = DEFAULT_FILL_HEX,
) -> list[TextParagraph]:
"""Walk <a:p> children producing TextParagraph objects."""
paragraphs: list[TextParagraph] = []
autonum_state: dict[int, int] = {}
for p_elem in tx_body.findall("a:p", NS):
- para = _parse_paragraph(p_elem, palette, theme_fonts, autonum_state)
+ para = _parse_paragraph(
+ p_elem, palette, theme_fonts, autonum_state,
+ default_fill=default_fill,
+ )
paragraphs.append(para)
return paragraphs
@@ -292,6 +341,8 @@ def _parse_paragraph(
palette: ColorPalette | None,
theme_fonts: dict[str, str],
autonum_state: dict[int, int],
+ *,
+ default_fill: str = DEFAULT_FILL_HEX,
) -> TextParagraph:
para = TextParagraph()
@@ -345,12 +396,15 @@ def _parse_paragraph(
rpr = child.find("a:rPr", NS)
text_elem = child.find("a:t", NS)
text = text_elem.text or "" if text_elem is not None else ""
- run = _build_run(text, rpr, end_rpr, palette, theme_fonts)
+ run = _build_run(
+ text, rpr, end_rpr, palette, theme_fonts,
+ default_fill=default_fill,
+ )
para.runs.append(run)
elif local == "br":
para.runs.append(TextRun(
text="", font_size_px=DEFAULT_FONT_SIZE_PX,
- font_family="sans-serif", fill=DEFAULT_FILL_HEX,
+ font_family="sans-serif", fill=default_fill,
is_break=True,
))
elif local == "fld":
@@ -359,7 +413,10 @@ def _parse_paragraph(
text_elem = child.find("a:t", NS)
text = text_elem.text or "" if text_elem is not None else ""
if text:
- run = _build_run(text, rpr, end_rpr, palette, theme_fonts)
+ run = _build_run(
+ text, rpr, end_rpr, palette, theme_fonts,
+ default_fill=default_fill,
+ )
para.runs.append(run)
return para
@@ -371,6 +428,8 @@ def _build_run(
end_rpr: ET.Element | None,
palette: ColorPalette | None,
theme_fonts: dict[str, str],
+ *,
+ default_fill: str = DEFAULT_FILL_HEX,
) -> TextRun:
"""Resolve a single <a:r> run from its rPr (with endParaRPr as default)."""
# font-size: rPr@sz; default 1800 (18pt = 24px)
@@ -397,7 +456,7 @@ def _build_run(
pass
# Color
- fill = DEFAULT_FILL_HEX
+ fill = default_fill
fill_opacity = 1.0
color_source = None
for src in (rpr, end_rpr):
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 477fd373..5627e047 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
@@ -67,6 +67,30 @@ def _wrap_shape(
_BEZIER_QUARTER_K = 0.5522847498
+def _build_preset_geom_from_meta(elem: ET.Element) -> str | None:
+ """Build native DrawingML preset geometry from SVG metadata."""
+ prst = elem.get('data-pptx-prst')
+ if prst != 'round2SameRect':
+ return None
+
+ def _adj_attr(name: str, default: int) -> int:
+ try:
+ return int(float(elem.get(name, str(default))))
+ except ValueError:
+ return default
+
+ adj1 = max(0, min(100000, _adj_attr('data-pptx-adj1', 16667)))
+ adj2 = max(0, min(100000, _adj_attr('data-pptx-adj2', 0)))
+ return (
+ '<a:prstGeom prst="round2SameRect">'
+ '<a:avLst>'
+ f'<a:gd name="adj1" fmla="val {adj1}"/>'
+ f'<a:gd name="adj2" fmla="val {adj2}"/>'
+ '</a:avLst>'
+ '</a:prstGeom>'
+ )
+
+
def _build_round_rect_custgeom(w: float, h: float, rx: float, ry: float) -> str:
"""Build a DrawingML ``custGeom`` for a rectangle with elliptical corners.
@@ -617,7 +641,9 @@ def convert_path(elem: ET.Element, ctx: ConvertContext) -> ShapeResult | None:
w_emu = px_to_emu(width)
h_emu = px_to_emu(height)
- geom = f'''<a:custGeom>
+ geom = _build_preset_geom_from_meta(elem)
+ if geom is None:
+ geom = f'''<a:custGeom>
<a:avLst/><a:gdLst/><a:ahLst/><a:cxnLst/>
<a:rect l="l" t="t" r="r" b="b"/>
<a:pathLst><a:path w="{w_emu}" h="{h_emu}">