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): preserve nested inline <tspan> formatting through flatten + run build

hugohe3cf4193761229
diff --git a/skills/ppt-master/scripts/svg_finalize/flatten_tspan.py b/skills/ppt-master/scripts/svg_finalize/flatten_tspan.py
index d55d452d..585b5dca 100644
--- a/skills/ppt-master/scripts/svg_finalize/flatten_tspan.py
+++ b/skills/ppt-master/scripts/svg_finalize/flatten_tspan.py
@@ -292,6 +292,31 @@ def flatten_text_with_tspans(tree: ET.ElementTree) -> bool:
return changed
+def _has_tspan_children(elem: ET.Element) -> bool:
+ """Return True if elem contains any nested <tspan> children (inline runs)."""
+ return any(c.tag == f"{{{SVG_NS}}}tspan" for c in list(elem))
+
+
+def _copy_inline_tspan(src: ET.Element, strip_line_attrs: bool) -> ET.Element:
+ """Deep-copy a tspan as an inline run, preserving nested tspan structure, head text, and tail text.
+
+ When strip_line_attrs is True, x/y/dy on the copied tspan are dropped because the
+ enclosing <text> now positions the line. dx is preserved (safe inline kerning).
+ Nested tspans are copied recursively without stripping (they are already inline-only).
+ """
+ new = ET.Element(f"{{{SVG_NS}}}tspan")
+ for k, v in src.attrib.items():
+ if strip_line_attrs and k in ("x", "y", "dy"):
+ continue
+ new.set(k, v)
+ new.text = src.text
+ for child in list(src):
+ if child.tag == f"{{{SVG_NS}}}tspan":
+ new.append(_copy_inline_tspan(child, strip_line_attrs=False))
+ new.tail = src.tail
+ return new
+
+
def _create_text_element_from_line(
text_el: ET.Element,
lead_text: str | None,
@@ -301,71 +326,54 @@ def _create_text_element_from_line(
) -> ET.Element:
"""
Create a text element from a line's content (may contain leading text and multiple tspans).
- If there is only one tspan and no leading text, create a simple text element.
- If there are multiple tspans or leading text, preserve the tspan structure.
+ If there is only one tspan with no nested tspan children and no leading text, the line
+ collapses to a plain <text>...</text>. Otherwise the tspan structure (including any
+ nested inline tspans) is preserved so per-run formatting survives the flatten step.
"""
ne = ET.Element(f"{{{SVG_NS}}}text")
-
+
# Copy attrs from parent <text>
copy_text_attrs(text_el, ne, exclude={"x", "y"})
ne.set("x", format_number(x))
ne.set("y", format_number(y))
-
+
# Transform
p_tf = text_el.get("transform")
if p_tf:
ne.set("transform", p_tf)
-
- # If there is only one tspan and no leading text, create a simple text element
- if not lead_text and len(tspans) == 1:
+
+ # Compact path: a single tspan with no nested inline runs collapses to <text>text</text>
+ if not lead_text and len(tspans) == 1 and not _has_tspan_children(tspans[0]):
tspan = tspans[0]
content = collect_text_content(tspan)
-
+
# Merge style
merged_style = merge_styles(text_el.get("style"), tspan.get("style"))
if merged_style:
ne.set("style", merged_style)
-
+
# Override specific attributes from tspan
for attr in TEXT_STYLE_ATTRS:
cv = tspan.get(attr)
if cv is not None:
ne.set(attr, cv)
-
+
# Combine transform
c_tf = tspan.get("transform")
if p_tf and c_tf:
ne.set("transform", f"{p_tf} {c_tf}")
elif c_tf:
ne.set("transform", c_tf)
-
+
ne.text = content
else:
- # Preserve tspan structure
+ # Preserve tspan structure, including nested inline tspans and tail text
if lead_text:
ne.text = lead_text
-
+
for tspan in tspans:
- # Create a new tspan, but remove position-related attributes
- new_tspan = ET.SubElement(ne, f"{{{SVG_NS}}}tspan")
-
- # Copy style attributes
- for attr in TEXT_STYLE_ATTRS:
- cv = tspan.get(attr)
- if cv is not None:
- new_tspan.set(attr, cv)
-
- # Copy style
- if tspan.get("style"):
- new_tspan.set("style", tspan.get("style"))
-
- # Copy text content
- new_tspan.text = collect_text_content(tspan)
-
- # Copy tail (text following the tspan)
- if tspan.tail:
- new_tspan.tail = tspan.tail
-
+ ne.append(_copy_inline_tspan(tspan, strip_line_attrs=True))
+
return ne
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 93fd9418..dab98c0b 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
@@ -635,6 +635,59 @@ def _normalize_text(text: str) -> str:
return re.sub(r'\s+', ' ', text).strip()
+def _override_run_attrs(
+ parent_attrs: dict[str, Any],
+ tspan: ET.Element,
+) -> dict[str, Any]:
+ """Layer a tspan's styling attributes over the inherited run attrs."""
+ run_attrs = dict(parent_attrs)
+ if tspan.get('font-weight'):
+ run_attrs['font_weight'] = tspan.get('font-weight')
+ if tspan.get('fill'):
+ child_fill = tspan.get('fill')
+ run_attrs['fill_raw'] = child_fill
+ c = parse_hex_color(child_fill)
+ if c:
+ run_attrs['fill'] = c
+ if tspan.get('font-size'):
+ run_attrs['font_size'] = _f(tspan.get('font-size'), run_attrs['font_size'])
+ if tspan.get('font-family'):
+ run_attrs['font_family'] = tspan.get('font-family')
+ if tspan.get('font-style'):
+ run_attrs['font_style'] = tspan.get('font-style')
+ if tspan.get('text-decoration'):
+ run_attrs['text_decoration'] = tspan.get('text-decoration')
+ return run_attrs
+
+
+def _collect_tspan_runs(
+ tspan: ET.Element,
+ inherited_attrs: dict[str, Any],
+) -> list[dict[str, Any]]:
+ """Recursively turn a tspan subtree into runs, propagating styling through nested tspans.
+
+ Order: tspan.text → (each nested child tspan's runs → that child's tail under THIS tspan's attrs).
+ """
+ runs: list[dict[str, Any]] = []
+ own_attrs = _override_run_attrs(inherited_attrs, tspan)
+
+ if tspan.text:
+ t = _normalize_text(tspan.text)
+ if t:
+ runs.append({**own_attrs, 'text': t})
+
+ for child in tspan:
+ child_tag = child.tag.replace(f'{{{SVG_NS}}}', '')
+ if child_tag == 'tspan':
+ runs.extend(_collect_tspan_runs(child, own_attrs))
+ if child.tail:
+ t = _normalize_text(child.tail)
+ if t:
+ runs.append({**own_attrs, 'text': t})
+
+ return runs
+
+
def _build_text_runs(
elem: ET.Element,
parent_attrs: dict[str, Any],
@@ -642,7 +695,8 @@ def _build_text_runs(
"""Build a list of text runs from a <text> element, handling <tspan> children.
Each run is a dict with keys: text, fill, fill_raw, font_weight,
- font_style, font_family, font_size.
+ font_style, font_family, font_size. Nested tspans are walked recursively so
+ inline format changes inside a tspan still produce distinct runs.
"""
runs: list[dict[str, Any]] = []
@@ -654,28 +708,7 @@ def _build_text_runs(
for child in elem:
child_tag = child.tag.replace(f'{{{SVG_NS}}}', '')
if child_tag == 'tspan':
- t = _normalize_text(''.join(child.itertext()))
- if t:
- run_attrs = dict(parent_attrs)
- if child.get('font-weight'):
- run_attrs['font_weight'] = child.get('font-weight')
- if child.get('fill'):
- child_fill = child.get('fill')
- run_attrs['fill_raw'] = child_fill
- c = parse_hex_color(child_fill)
- if c:
- run_attrs['fill'] = c
- if child.get('font-size'):
- run_attrs['font_size'] = _f(child.get('font-size'), run_attrs['font_size'])
- if child.get('font-family'):
- run_attrs['font_family'] = child.get('font-family')
- if child.get('font-style'):
- run_attrs['font_style'] = child.get('font-style')
- if child.get('text-decoration'):
- run_attrs['text_decoration'] = child.get('text-decoration')
- runs.append({**run_attrs, 'text': t})
-
- # Tail text after </tspan> belongs to parent
+ runs.extend(_collect_tspan_runs(child, parent_attrs))
if child.tail:
t = _normalize_text(child.tail)
if t: