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

refactor: split svg_to_shapes.py and svg_to_pptx.py into svg_to_pptx package

hugohe3eb5ad1f50d0d
diff --git a/skills/ppt-master/scripts/svg_to_pptx.py b/skills/ppt-master/scripts/svg_to_pptx.py
index 82f7c8fc..6968f187 100644
--- a/skills/ppt-master/scripts/svg_to_pptx.py
+++ b/skills/ppt-master/scripts/svg_to_pptx.py
@@ -1,1154 +1,17 @@
#!/usr/bin/env python3
-"""
-PPT Master - SVG to PPTX Conversion Tool
-
-Batch converts SVG files in a project into a PowerPoint presentation.
-Each SVG file corresponds to one slide, with SVG embedded in native vector format.
-
-Usage:
- python3 scripts/svg_to_pptx.py <project_path>
- python3 scripts/svg_to_pptx.py <project_path> -o output.pptx
- python3 scripts/svg_to_pptx.py <project_path> --use-final
+"""PPT Master - SVG to PPTX Tool (thin wrapper).
-Examples:
- python3 scripts/svg_to_pptx.py examples/ppt169_demo
- python3 scripts/svg_to_pptx.py examples/ppt169_demo -o presentation.pptx
- python3 scripts/svg_to_pptx.py examples/ppt169_demo --use-final
-
-Dependencies:
- pip install python-pptx
-
-Notes:
- - SVG is embedded in native vector format, preserving editability
- - Requires PowerPoint 2016+ to display correctly
+Delegates to the svg_to_pptx package. Kept for CLI backward compatibility:
+ python3 scripts/svg_to_pptx.py <project_path> -s final
"""
import sys
-import os
-import argparse
-import re
-import zipfile
-import shutil
-import tempfile
from pathlib import Path
-from typing import Optional, Tuple, List
-from xml.etree import ElementTree as ET
-
-# Check if python-pptx is installed
-try:
- from pptx import Presentation
- from pptx.util import Emu
-except ImportError:
- print("Error: python-pptx library is missing")
- print("Please run: pip install python-pptx")
- sys.exit(1)
-
-# Import project utility modules
-sys.path.insert(0, str(Path(__file__).parent))
-try:
- from project_utils import get_project_info
- from config import CANVAS_FORMATS
-except ImportError:
- CANVAS_FORMATS = {
- 'ppt169': {'name': 'PPT 16:9', 'dimensions': '1280×720', 'viewbox': '0 0 1280 720'},
- }
-
- def get_project_info(path):
- return {'format': 'unknown', 'name': Path(path).name}
-
-# Import animation module
-try:
- from pptx_animations import create_transition_xml, TRANSITIONS
- ANIMATIONS_AVAILABLE = True
-except ImportError:
- ANIMATIONS_AVAILABLE = False
- TRANSITIONS = {}
-
-# Import native shape converter
-try:
- from svg_to_shapes import convert_svg_to_slide_shapes
- NATIVE_SHAPES_AVAILABLE = True
-except ImportError:
- NATIVE_SHAPES_AVAILABLE = False
-
-# SVG to PNG library detection (for Office compatibility mode)
-# Prefer CairoSVG (better rendering quality), fall back to svglib
-PNG_RENDERER = None # 'cairosvg' | 'svglib' | None
-
-try:
- import cairosvg
- PNG_RENDERER = 'cairosvg'
-except (ImportError, OSError):
- try:
- from svglib.svglib import svg2rlg
- from reportlab.graphics import renderPM
- PNG_RENDERER = 'svglib'
- except (ImportError, OSError):
- pass
-
-def get_png_renderer_info() -> tuple:
- """Get PNG renderer information"""
- if PNG_RENDERER == 'cairosvg':
- return ('cairosvg', '(full gradient/filter support)', None)
- elif PNG_RENDERER == 'svglib':
- return ('svglib', '(some gradients may be lost)', 'Install cairosvg for better results: pip install cairosvg')
- else:
- return (None, '(not installed)', 'Install via: pip install cairosvg or pip install svglib reportlab')
-
-
-# EMU conversion constants
-EMU_PER_INCH = 914400
-EMU_PER_PIXEL = EMU_PER_INCH / 96
-
-# XML namespaces
-NAMESPACES = {
- 'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
- 'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
- 'p': 'http://schemas.openxmlformats.org/presentationml/2006/main',
- 'asvg': 'http://schemas.microsoft.com/office/drawing/2016/SVG/main',
-}
-
-# Register namespaces
-for prefix, uri in NAMESPACES.items():
- ET.register_namespace(prefix, uri)
-
-
-def get_slide_dimensions(canvas_format: str, custom_pixels: Optional[Tuple[int, int]] = None) -> Tuple[int, int]:
- """Get slide dimensions (in EMU units)"""
- if custom_pixels:
- width_px, height_px = custom_pixels
- else:
- if canvas_format not in CANVAS_FORMATS:
- canvas_format = 'ppt169'
-
- dimensions = CANVAS_FORMATS[canvas_format]['dimensions']
- match = re.match(r'(\d+)[×x](\d+)', dimensions)
- if match:
- width_px = int(match.group(1))
- height_px = int(match.group(2))
- else:
- width_px, height_px = 1280, 720
-
- return int(width_px * EMU_PER_PIXEL), int(height_px * EMU_PER_PIXEL)
-
-
-def get_pixel_dimensions(canvas_format: str, custom_pixels: Optional[Tuple[int, int]] = None) -> Tuple[int, int]:
- """Get canvas pixel dimensions"""
- if custom_pixels:
- return custom_pixels
-
- if canvas_format not in CANVAS_FORMATS:
- canvas_format = 'ppt169'
-
- dimensions = CANVAS_FORMATS[canvas_format]['dimensions']
- match = re.match(r'(\d+)[×x](\d+)', dimensions)
- if match:
- return int(match.group(1)), int(match.group(2))
- return 1280, 720
-
-
-def get_viewbox_dimensions(svg_path: Path) -> Optional[Tuple[int, int]]:
- """Extract pixel dimensions from SVG viewBox (returns integers)"""
- try:
- with open(svg_path, 'r', encoding='utf-8') as f:
- content = f.read(2000)
-
- match = re.search(r'viewBox="([^"]+)"', content)
- if not match:
- return None
-
- parts = re.split(r'[\s,]+', match.group(1).strip())
- if len(parts) < 4:
- return None
-
- width = float(parts[2])
- height = float(parts[3])
- if width <= 0 or height <= 0:
- return None
-
- return int(round(width)), int(round(height))
- except Exception:
- return None
-
-
-def detect_format_from_svg(svg_path: Path) -> Optional[str]:
- """Detect canvas format from SVG file's viewBox"""
- try:
- with open(svg_path, 'r', encoding='utf-8') as f:
- content = f.read(2000)
-
- match = re.search(r'viewBox="([^"]+)"', content)
- if match:
- viewbox = match.group(1)
- for fmt_key, fmt_info in CANVAS_FORMATS.items():
- if fmt_info['viewbox'] == viewbox:
- return fmt_key
- except Exception:
- pass
- return None
-
-
-def convert_svg_to_png(svg_path: Path, png_path: Path, width: int = None, height: int = None) -> bool:
- """
- Convert SVG to PNG
-
- Args:
- svg_path: SVG file path
- png_path: Output PNG file path
- width: Output width (pixels)
- height: Output height (pixels)
-
- Returns:
- Whether the conversion was successful
- """
- if PNG_RENDERER is None:
- return False
-
- try:
- if PNG_RENDERER == 'cairosvg':
- # Use CairoSVG (better rendering quality)
- cairosvg.svg2png(
- url=str(svg_path),
- write_to=str(png_path),
- output_width=width,
- output_height=height
- )
- return True
-
- elif PNG_RENDERER == 'svglib':
- # Use svglib (lightweight, but limited gradient support)
- drawing = svg2rlg(str(svg_path))
- if drawing is None:
- print(f" Warning: Unable to parse SVG ({svg_path.name})")
- return False
-
- # Render to PNG
- renderPM.drawToFile(
- drawing,
- str(png_path),
- fmt="PNG",
- configPIL={'quality': 95}
- )
- return True
-
- except Exception as e:
- print(f" Warning: SVG to PNG conversion failed ({svg_path.name}): {e}")
- return False
-
- return False
-
-
-def find_svg_files(project_path: Path, source: str = 'output') -> Tuple[List[Path], str]:
- """
- Find SVG files in the project
-
- Args:
- project_path: Project directory path
- source: SVG source directory
- - 'output': svg_output (original version)
- - 'final': svg_final (post-processed, recommended)
- - or any subdirectory name
-
- Returns:
- (list of SVG files, actual directory name used)
- """
- # Predefined directory mapping
- dir_map = {
- 'output': 'svg_output',
- 'final': 'svg_final',
- }
-
- # Get directory name (supports predefined aliases or direct directory names)
- dir_name = dir_map.get(source, source)
- svg_dir = project_path / dir_name
-
- if not svg_dir.exists():
- print(f" Warning: {dir_name} directory does not exist, trying svg_output")
- dir_name = 'svg_output'
- svg_dir = project_path / dir_name
-
- if not svg_dir.exists():
- # Search directly in the specified directory
- if project_path.is_dir():
- svg_dir = project_path
- dir_name = project_path.name
- else:
- return [], ''
-
- return sorted(svg_dir.glob('*.svg')), dir_name
-
-
-def find_notes_files(project_path: Path, svg_files: List[Path] = None) -> dict:
- """
- Find notes files in the project
-
- Supports two matching modes (mixed matching supported):
- 1. Match by filename (priority): notes/01_cover.md corresponds to 01_cover.svg
- 2. Match by index (backward compatible): notes/slide01.md corresponds to the 1st SVG
-
- Args:
- project_path: Project directory path
- svg_files: SVG file list (for filename matching)
-
- Returns:
- Dictionary where key is SVG filename (without extension) and value is notes content
- """
- notes_dir = project_path / 'notes'
- notes = {}
-
- if not notes_dir.exists():
- return notes
-
- svg_stems_mapping = {}
- svg_index_mapping = {}
- if svg_files:
- for i, svg_path in enumerate(svg_files, 1):
- svg_stems_mapping[svg_path.stem] = i
- svg_index_mapping[i] = svg_path.stem
-
- # Collect all notes file information
- for notes_file in notes_dir.glob('*.md'):
- try:
- with open(notes_file, 'r', encoding='utf-8') as f:
- content = f.read().strip()
- if not content:
- continue
-
- stem = notes_file.stem
-
- # Try to extract index (backward compatible with slide01.md format)
- match = re.search(r'slide[_]?(\d+)', stem)
- if match:
- index = int(match.group(1))
- mapped_stem = svg_index_mapping.get(index)
- if mapped_stem:
- notes[mapped_stem] = content
-
- # Match by filename (overrides backward compatible format)
- if stem in svg_stems_mapping:
- notes[stem] = content
- except Exception:
- pass
-
- return notes
-
-
-def markdown_to_plain_text(md_content: str) -> str:
- """
- Convert Markdown notes to plain text (for PPTX notes)
-
- Args:
- md_content: Markdown formatted notes content
-
- Returns:
- Plain text content
- """
- def strip_inline_bold(text: str) -> str:
- # Remove Markdown bold markers while keeping content
- text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
- text = re.sub(r'__(.+?)__', r'\1', text)
- return text
-
- lines = []
- for line in md_content.split('\n'):
- # Skip heading lines (starting with #)
- if line.startswith('#'):
- # Extract heading text
- text = re.sub(r'^#+\s*', '', line).strip()
- text = strip_inline_bold(text)
- if text:
- lines.append(text)
- lines.append('') # Empty line
- # Handle list items (starting with -)
- elif line.strip().startswith('- '):
- item_text = line.strip()[2:]
- item_text = strip_inline_bold(item_text)
- lines.append('• ' + item_text)
- # Regular lines
- elif line.strip():
- text = strip_inline_bold(line.strip())
- lines.append(text)
- else:
- lines.append('')
-
- # Merge consecutive empty lines
- result = []
- prev_empty = False
- for line in lines:
- if line == '':
- if not prev_empty:
- result.append(line)
- prev_empty = True
- else:
- result.append(line)
- prev_empty = False
-
- return '\n'.join(result).strip()
-
-
-def create_notes_slide_xml(slide_num: int, notes_text: str) -> str:
- """
- Create notes slide XML
-
- Args:
- slide_num: Slide number
- notes_text: Notes text (plain text format)
-
- Returns:
- Notes slide XML string
- """
- # Escape XML special characters
- notes_text = notes_text.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
-
- # Convert line breaks to <a:p> paragraphs
- paragraphs = []
- for para in notes_text.split('\n'):
- if para.strip():
- paragraphs.append(f'''<a:p>
- <a:r>
- <a:rPr lang="zh-CN" dirty="0"/>
- <a:t>{para}</a:t>
- </a:r>
- </a:p>''')
- else:
- paragraphs.append('<a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p>')
-
- paragraphs_xml = '\n '.join(paragraphs) if paragraphs else '<a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p>'
-
- return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<p:notes xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
- xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
- xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
- <p:cSld>
- <p:spTree>
- <p:nvGrpSpPr>
- <p:cNvPr id="1" name=""/>
- <p:cNvGrpSpPr/>
- <p:nvPr/>
- </p:nvGrpSpPr>
- <p:grpSpPr>
- <a:xfrm>
- <a:off x="0" y="0"/>
- <a:ext cx="0" cy="0"/>
- <a:chOff x="0" y="0"/>
- <a:chExt cx="0" cy="0"/>
- </a:xfrm>
- </p:grpSpPr>
- <p:sp>
- <p:nvSpPr>
- <p:cNvPr id="2" name="Slide Image Placeholder 1"/>
- <p:cNvSpPr>
- <a:spLocks noGrp="1" noRot="1" noChangeAspect="1"/>
- </p:cNvSpPr>
- <p:nvPr>
- <p:ph type="sldImg"/>
- </p:nvPr>
- </p:nvSpPr>
- <p:spPr/>
- </p:sp>
- <p:sp>
- <p:nvSpPr>
- <p:cNvPr id="3" name="Notes Placeholder 2"/>
- <p:cNvSpPr>
- <a:spLocks noGrp="1"/>
- </p:cNvSpPr>
- <p:nvPr>
- <p:ph type="body" idx="1"/>
- </p:nvPr>
- </p:nvSpPr>
- <p:spPr/>
- <p:txBody>
- <a:bodyPr/>
- <a:lstStyle/>
- {paragraphs_xml}
- </p:txBody>
- </p:sp>
- </p:spTree>
- </p:cSld>
- <p:clrMapOvr>
- <a:masterClrMapping/>
- </p:clrMapOvr>
-</p:notes>'''
-
-
-def create_notes_slide_rels_xml(slide_num: int) -> str:
- """
- Create notes slide relationship file XML
-
- Args:
- slide_num: Slide number
-
- Returns:
- Relationship file XML string
- """
- return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster" Target="../notesMasters/notesMaster1.xml"/>
- <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="../slides/slide{slide_num}.xml"/>
-</Relationships>'''
-
-
-def create_slide_xml_with_svg(
- slide_num: int,
- png_rid: str,
- svg_rid: str,
- width_emu: int,
- height_emu: int,
- transition: Optional[str] = 'fade',
- transition_duration: float = 0.5,
- auto_advance: Optional[float] = None,
- use_compat_mode: bool = True
-) -> str:
- """
- Create slide XML containing an SVG image
-
- Args:
- slide_num: Slide number
- png_rid: PNG fallback image relationship ID
- svg_rid: SVG relationship ID
- width_emu: Width (EMU)
- height_emu: Height (EMU)
- transition: Transition effect name
- transition_duration: Transition duration (seconds)
- auto_advance: Auto-advance interval (seconds)
- use_compat_mode: Whether to use compatibility mode (PNG + SVG dual format)
- """
- # Generate transition effect XML
- transition_xml = ''
- if transition and ANIMATIONS_AVAILABLE:
- transition_xml = '\n' + create_transition_xml(
- effect=transition,
- duration=transition_duration,
- advance_after=auto_advance
- )
-
- # Compatibility mode: PNG main image + SVG extension (Office official recommendation)
- if use_compat_mode:
- blip_xml = f'''<a:blip r:embed="{png_rid}">
- <a:extLst>
- <a:ext uri="{{96DAC541-7B7A-43D3-8B79-37D633B846F1}}">
- <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="{svg_rid}"/>
- </a:ext>
- </a:extLst>
- </a:blip>'''
- else:
- # Pure SVG mode (only supported by newer Office versions)
- blip_xml = f'<a:blip r:embed="{svg_rid}"/>'
-
- return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
- xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
- xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
- <p:cSld>
- <p:spTree>
- <p:nvGrpSpPr>
- <p:cNvPr id="1" name=""/>
- <p:cNvGrpSpPr/>
- <p:nvPr/>
- </p:nvGrpSpPr>
- <p:grpSpPr>
- <a:xfrm>
- <a:off x="0" y="0"/>
- <a:ext cx="0" cy="0"/>
- <a:chOff x="0" y="0"/>
- <a:chExt cx="0" cy="0"/>
- </a:xfrm>
- </p:grpSpPr>
- <p:pic>
- <p:nvPicPr>
- <p:cNvPr id="2" name="SVG Image {slide_num}"/>
- <p:cNvPicPr>
- <a:picLocks noChangeAspect="1"/>
- </p:cNvPicPr>
- <p:nvPr/>
- </p:nvPicPr>
- <p:blipFill>
- {blip_xml}
- <a:stretch>
- <a:fillRect/>
- </a:stretch>
- </p:blipFill>
- <p:spPr>
- <a:xfrm>
- <a:off x="0" y="0"/>
- <a:ext cx="{width_emu}" cy="{height_emu}"/>
- </a:xfrm>
- <a:prstGeom prst="rect">
- <a:avLst/>
- </a:prstGeom>
- </p:spPr>
- </p:pic>
- </p:spTree>
- </p:cSld>
- <p:clrMapOvr>
- <a:masterClrMapping/>
- </p:clrMapOvr>{transition_xml}
-</p:sld>'''
-
-
-def create_slide_rels_xml(png_rid: str, png_filename: str, svg_rid: str, svg_filename: str, use_compat_mode: bool = True) -> str:
- """
- Create slide relationship file XML
-
- Args:
- png_rid: PNG image relationship ID
- png_filename: PNG filename
- svg_rid: SVG relationship ID
- svg_filename: SVG filename
- use_compat_mode: Whether to use compatibility mode
- """
- if use_compat_mode:
- return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>
- <Relationship Id="{png_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{png_filename}"/>
- <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/>
-</Relationships>'''
- else:
- return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>
- <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/>
-</Relationships>'''
-
-
-def create_pptx_with_native_svg(
- svg_files: List[Path],
- output_path: Path,
- canvas_format: Optional[str] = None,
- verbose: bool = True,
- transition: Optional[str] = 'fade',
- transition_duration: float = 0.5,
- auto_advance: Optional[float] = None,
- use_compat_mode: bool = True,
- notes: Optional[dict] = None,
- enable_notes: bool = True,
- use_native_shapes: bool = False
-) -> bool:
- """
- Create a PPTX file with native SVG
-
- Args:
- svg_files: List of SVG files
- output_path: Output path
- canvas_format: Canvas format
- verbose: Whether to output detailed information
- transition: Transition effect (fade/push/wipe/split/strips/cover/random, default: fade)
- transition_duration: Transition duration in seconds (default: 0.4)
- auto_advance: Auto-advance interval (seconds)
- use_compat_mode: Use Office compatibility mode (PNG + SVG dual format, enabled by default)
- notes: Notes dictionary, key is slide number, value is notes content
- enable_notes: Whether to enable notes embedding (enabled by default)
- use_native_shapes: Convert SVG to native DrawingML shapes (directly editable)
- """
- if not svg_files:
- print("Error: No SVG files found")
- return False
-
- # Native shapes mode takes priority over compat mode
- if use_native_shapes:
- if not NATIVE_SHAPES_AVAILABLE:
- print("Error: svg_to_shapes module not available, cannot use native shapes mode")
- print(" Falling back to SVG embedding mode")
- use_native_shapes = False
- else:
- # Native shapes mode doesn't use compat/PNG
- use_compat_mode = False
-
- # Check compatibility mode dependencies
- renderer_name, renderer_status, renderer_hint = get_png_renderer_info()
- if not use_native_shapes and use_compat_mode and PNG_RENDERER is None:
- print("Warning: No PNG rendering library installed, cannot use compatibility mode")
- print(f" {renderer_hint}")
- print(" Will use pure SVG mode (may not display in Office LTSC 2021 and similar versions)")
- use_compat_mode = False
-
- # Auto-detect canvas format or get dimensions from viewBox
- custom_pixels: Optional[Tuple[int, int]] = None
- if canvas_format is None:
- canvas_format = detect_format_from_svg(svg_files[0])
- if canvas_format and verbose:
- format_name = CANVAS_FORMATS.get(canvas_format, {}).get('name', canvas_format)
- print(f" Detected canvas format: {format_name}")
-
- if canvas_format is None:
- custom_pixels = get_viewbox_dimensions(svg_files[0])
- if custom_pixels and verbose:
- print(f" Using SVG viewBox dimensions: {custom_pixels[0]} x {custom_pixels[1]} px")
-
- if canvas_format is None and custom_pixels is None:
- canvas_format = 'ppt169'
- if verbose:
- print(f" Using default format: PPT 16:9")
-
- width_emu, height_emu = get_slide_dimensions(canvas_format or 'ppt169', custom_pixels)
- pixel_width, pixel_height = get_pixel_dimensions(canvas_format or 'ppt169', custom_pixels)
-
- if verbose:
- print(f" Slide dimensions: {pixel_width} x {pixel_height} px")
- print(f" SVG file count: {len(svg_files)}")
- if use_native_shapes:
- print(f" Mode: Native DrawingML shapes (directly editable)")
- elif use_compat_mode:
- print(f" Compatibility mode: Enabled (PNG + SVG dual format)")
- print(f" PNG renderer: {renderer_name} {renderer_status}")
- else:
- print(f" Compatibility mode: Disabled (pure SVG)")
- if transition:
- trans_name = TRANSITIONS.get(transition, {}).get('name', transition) if TRANSITIONS else transition
- print(f" Transition effect: {trans_name}")
- if enable_notes and notes:
- print(f" Speaker notes: {len(notes)} page(s)")
- elif enable_notes:
- print(f" Speaker notes: Enabled (no notes files found)")
- else:
- print(f" Speaker notes: Disabled")
- print()
-
- # Create temporary directory
- temp_dir = Path(tempfile.mkdtemp())
-
- try:
- # First create base PPTX with python-pptx
- prs = Presentation()
- prs.slide_width = width_emu
- prs.slide_height = height_emu
-
- # Add blank slides as placeholders
- blank_layout = prs.slide_layouts[6]
- for _ in svg_files:
- prs.slides.add_slide(blank_layout)
-
- # Save base PPTX
- base_pptx = temp_dir / 'base.pptx'
- prs.save(str(base_pptx))
-
- # Extract PPTX
- extract_dir = temp_dir / 'pptx_content'
- with zipfile.ZipFile(base_pptx, 'r') as zf:
- zf.extractall(extract_dir)
-
- # Create media directory
- media_dir = extract_dir / 'ppt' / 'media'
- media_dir.mkdir(exist_ok=True)
-
- # Process each SVG file
- success_count = 0
- any_png_generated = False
-
- for i, svg_path in enumerate(svg_files, 1):
- slide_num = i
-
- try:
- # ---- Native shapes mode ----
- if use_native_shapes:
- slide_xml, media_files_dict, rel_entries = convert_svg_to_slide_shapes(
- svg_path, slide_num=slide_num, verbose=verbose
- )
-
- # Add transition if specified
- if transition and ANIMATIONS_AVAILABLE:
- transition_xml = '\n' + create_transition_xml(
- effect=transition,
- duration=transition_duration,
- advance_after=auto_advance
- )
- slide_xml = slide_xml.replace(
- '</p:sld>',
- transition_xml + '\n</p:sld>'
- )
-
- # Write slide XML
- slide_xml_path = extract_dir / 'ppt' / 'slides' / f'slide{slide_num}.xml'
- with open(slide_xml_path, 'w', encoding='utf-8') as f:
- f.write(slide_xml)
-
- # Write media files (from embedded images)
- for media_name, media_data in media_files_dict.items():
- with open(media_dir / media_name, 'wb') as f:
- f.write(media_data)
-
- # Build relationships XML
- rels_dir = extract_dir / 'ppt' / 'slides' / '_rels'
- rels_dir.mkdir(exist_ok=True)
- rels_path = rels_dir / f'slide{slide_num}.xml.rels'
-
- extra_rels = ''
- for rel in rel_entries:
- extra_rels += f'\n <Relationship Id="{rel["id"]}" Type="{rel["type"]}" Target="{rel["target"]}"/>'
-
- rels_xml = f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
- <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>{extra_rels}
-</Relationships>'''
- with open(rels_path, 'w', encoding='utf-8') as f:
- f.write(rels_xml)
-
- # Track image formats for Content_Types
- for media_name in media_files_dict:
- ext = media_name.rsplit('.', 1)[-1].lower()
- if ext in ('png', 'jpg', 'jpeg', 'gif', 'webp'):
- any_png_generated = True # reuse flag for any image
-
- # ---- Legacy SVG embedding mode ----
- else:
- svg_filename = f'image{i}.svg'
- png_filename = f'image{i}.png'
- png_rid = 'rId2'
- svg_rid = 'rId3' if use_compat_mode else 'rId2'
-
- # Copy SVG to media directory
- shutil.copy(svg_path, media_dir / svg_filename)
-
- # Compatibility mode: generate PNG fallback image
- slide_has_png = False
- if use_compat_mode:
- png_path = media_dir / png_filename
- png_success = convert_svg_to_png(
- svg_path,
- png_path,
- width=pixel_width,
- height=pixel_height
- )
- if png_success:
- slide_has_png = True
- any_png_generated = True
- else:
- # PNG generation failed, fall back to pure SVG
- if verbose:
- print(f" [{i}/{len(svg_files)}] {svg_path.name} - PNG generation failed, using pure SVG")
- svg_rid = 'rId2'
-
- # Update slide XML
- slide_xml_path = extract_dir / 'ppt' / 'slides' / f'slide{slide_num}.xml'
- slide_xml = create_slide_xml_with_svg(
- slide_num,
- png_rid=png_rid,
- svg_rid=svg_rid,
- width_emu=width_emu,
- height_emu=height_emu,
- transition=transition,
- transition_duration=transition_duration,
- auto_advance=auto_advance,
- use_compat_mode=(use_compat_mode and slide_has_png)
- )
- with open(slide_xml_path, 'w', encoding='utf-8') as f:
- f.write(slide_xml)
-
- # Create/update relationship file
- rels_dir = extract_dir / 'ppt' / 'slides' / '_rels'
- rels_dir.mkdir(exist_ok=True)
- rels_path = rels_dir / f'slide{slide_num}.xml.rels'
- rels_xml = create_slide_rels_xml(
- png_rid=png_rid,
- png_filename=png_filename,
- svg_rid=svg_rid,
- svg_filename=svg_filename,
- use_compat_mode=(use_compat_mode and slide_has_png)
- )
- with open(rels_path, 'w', encoding='utf-8') as f:
- f.write(rels_xml)
-
- # Process notes (shared between native and legacy mode)
- notes_content = ''
- if enable_notes:
- # Match by filename (new logic) or by index (backward compatible)
- svg_stem = svg_path.stem
- notes_content = notes.get(svg_stem, '') if notes else ''
- if notes_content:
- notes_text = markdown_to_plain_text(notes_content)
- else:
- notes_text = '' # Empty notes
-
- # Create notesSlides directory
- notes_slides_dir = extract_dir / 'ppt' / 'notesSlides'
- notes_slides_dir.mkdir(exist_ok=True)
-
- # Create notes slide XML
- notes_xml_path = notes_slides_dir / f'notesSlide{slide_num}.xml'
- notes_xml = create_notes_slide_xml(slide_num, notes_text)
- with open(notes_xml_path, 'w', encoding='utf-8') as f:
- f.write(notes_xml)
-
- # Create notes slide relationship file
- notes_rels_dir = notes_slides_dir / '_rels'
- notes_rels_dir.mkdir(exist_ok=True)
- notes_rels_path = notes_rels_dir / f'notesSlide{slide_num}.xml.rels'
- notes_rels_xml = create_notes_slide_rels_xml(slide_num)
- with open(notes_rels_path, 'w', encoding='utf-8') as f:
- f.write(notes_rels_xml)
-
- # Update slide.xml.rels to add notes association
- with open(rels_path, 'r', encoding='utf-8') as f:
- slide_rels_content = f.read()
- notes_rel = f' <Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" Target="../notesSlides/notesSlide{slide_num}.xml"/>'
- slide_rels_content = slide_rels_content.replace('</Relationships>', notes_rel + '\n</Relationships>')
- with open(rels_path, 'w', encoding='utf-8') as f:
- f.write(slide_rels_content)
-
- if verbose:
- if use_native_shapes:
- mode_str = " (Native)"
- elif use_compat_mode and not use_native_shapes:
- mode_str = " (PNG+SVG)" if any_png_generated else " (SVG)"
- else:
- mode_str = " (SVG)"
- has_notes = enable_notes and bool(notes_content)
- notes_str = " +notes" if has_notes else ""
- print(f" [{i}/{len(svg_files)}] {svg_path.name}{mode_str}{notes_str}")
-
- success_count += 1
-
- except Exception as e:
- if verbose:
- print(f" [{i}/{len(svg_files)}] {svg_path.name} - Error: {e}")
-
- # Update [Content_Types].xml to add media types
- content_types_path = extract_dir / '[Content_Types].xml'
- with open(content_types_path, 'r', encoding='utf-8') as f:
- content_types = f.read()
-
- types_to_add = []
- if not use_native_shapes:
- # Legacy mode: need SVG extension type
- if 'Extension="svg"' not in content_types:
- types_to_add.append(' <Default Extension="svg" ContentType="image/svg+xml"/>')
- if any_png_generated and 'Extension="png"' not in content_types:
- types_to_add.append(' <Default Extension="png" ContentType="image/png"/>')
- # Add jpg support for native mode embedded images
- if use_native_shapes and 'Extension="jpg"' not in content_types:
- types_to_add.append(' <Default Extension="jpg" ContentType="image/jpeg"/>')
- if use_native_shapes and 'Extension="jpeg"' not in content_types:
- types_to_add.append(' <Default Extension="jpeg" ContentType="image/jpeg"/>')
-
- if types_to_add:
- content_types = content_types.replace(
- '</Types>',
- '\n'.join(types_to_add) + '\n</Types>'
- )
- with open(content_types_path, 'w', encoding='utf-8') as f:
- f.write(content_types)
-
- # Add notesSlides content types
- if enable_notes:
- for i in range(1, len(svg_files) + 1):
- override = f' <Override PartName="/ppt/notesSlides/notesSlide{i}.xml" ContentType="application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml"/>'
- if override not in content_types:
- content_types = content_types.replace('</Types>', override + '\n</Types>')
- with open(content_types_path, 'w', encoding='utf-8') as f:
- f.write(content_types)
-
- # Repackage PPTX
- with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zf:
- for file_path in extract_dir.rglob('*'):
- if file_path.is_file():
- arcname = file_path.relative_to(extract_dir)
- zf.write(file_path, arcname)
-
- if verbose:
- print()
- print(f"[Done] Saved: {output_path}")
- print(f" Succeeded: {success_count}, Failed: {len(svg_files) - success_count}")
- if use_compat_mode and any_png_generated:
- print(f" Mode: Office compatibility mode (supports all Office versions)")
- # If using svglib, provide upgrade hint
- if PNG_RENDERER == 'svglib' and renderer_hint:
- print(f" [Tip] {renderer_hint}")
-
- return success_count == len(svg_files)
-
- finally:
- # Clean up temporary directory
- shutil.rmtree(temp_dir, ignore_errors=True)
-
-
-def main():
- # Build transition effect option list
- transition_choices = ['none'] + (list(TRANSITIONS.keys()) if TRANSITIONS else ['fade', 'push', 'wipe', 'split', 'strips', 'cover', 'random'])
-
- parser = argparse.ArgumentParser(
- description='PPT Master - SVG to PPTX Tool (Office Compatibility Mode)',
- formatter_class=argparse.RawDescriptionHelpFormatter,
- epilog=f'''
-Examples:
- %(prog)s examples/ppt169_demo -s final # Default: native + SVG reference (two files)
- %(prog)s examples/ppt169_demo --only native # Only native shapes version
- %(prog)s examples/ppt169_demo --only legacy # Only SVG image version
- %(prog)s examples/ppt169_demo -o out.pptx # Explicit path (SVG ref → out_svg.pptx)
-
- # Disable transition / change transition effect
- %(prog)s examples/ppt169_demo -t none
- %(prog)s examples/ppt169_demo -t push --transition-duration 1.0
-
-SVG source directory (-s):
- output - svg_output (original version)
- final - svg_final (post-processed, recommended)
- <any> - Specify a subdirectory name directly
-
-Transition effects (-t/--transition):
- {', '.join(transition_choices)}
-
-Compatibility mode (enabled by default):
- - Automatically generates PNG fallback images, SVG embedded as extension
- - Compatible with all Office versions (including Office LTSC 2021)
- - Newer Office still displays SVG (editable), older versions display PNG
- - Requires svglib: pip install svglib reportlab
- - Use --no-compat to disable (only Office 2019+ supported)
-
-Speaker notes (enabled by default):
- - Automatically reads Markdown notes files from the notes/ directory
- - Supports two naming conventions:
- 1. Match by filename (recommended): 01_cover.md corresponds to 01_cover.svg
- 2. Match by index: slide01.md corresponds to the 1st SVG (backward compatible)
- - Use --no-notes to disable
-'''
- )
-
- parser.add_argument('project_path', type=str, help='Project directory path')
- parser.add_argument('-o', '--output', type=str, default=None, help='Output file path')
- parser.add_argument('-s', '--source', type=str, default='output',
- help='SVG source: output/final or any subdirectory name (recommended: final)')
- parser.add_argument('-f', '--format', type=str, choices=list(CANVAS_FORMATS.keys()), default=None, help='Specify canvas format')
- parser.add_argument('-q', '--quiet', action='store_true', help='Quiet mode')
-
- # Compatibility mode arguments
- parser.add_argument('--no-compat', action='store_true',
- help='Disable Office compatibility mode (pure SVG only, requires Office 2019+)')
-
- # Output mode arguments
- mode_group = parser.add_mutually_exclusive_group()
- mode_group.add_argument('--only', type=str, choices=['native', 'legacy'], default=None,
- help='Only generate one version: native (editable shapes) or legacy (SVG image)')
- mode_group.add_argument('--native', action='store_true', default=False,
- help='(Deprecated, now default) Convert SVG to native DrawingML shapes')
-
- # Transition effect arguments
- parser.add_argument('-t', '--transition', type=str, choices=transition_choices, default='fade',
- help='Page transition effect (default: fade, use "none" to disable)')
- parser.add_argument('--transition-duration', type=float, default=0.4,
- help='Transition duration in seconds (default: 0.5)')
- parser.add_argument('--auto-advance', type=float, default=None,
- help='Auto-advance interval in seconds (default: manual advance)')
-
- # Notes arguments
- parser.add_argument('--no-notes', action='store_true',
- help='Disable speaker notes embedding (enabled by default)')
-
- args = parser.parse_args()
-
- project_path = Path(args.project_path)
- if not project_path.exists():
- print(f"Error: Path does not exist: {project_path}")
- sys.exit(1)
-
- try:
- project_info = get_project_info(str(project_path))
- project_name = project_info.get('name', project_path.name)
- detected_format = project_info.get('format')
- except Exception:
- project_name = project_path.name
- detected_format = None
-
- canvas_format = args.format
- if canvas_format is None and detected_format and detected_format != 'unknown':
- canvas_format = detected_format
-
- svg_files, source_dir_name = find_svg_files(project_path, args.source)
-
- if not svg_files:
- print("Error: No SVG files found")
- sys.exit(1)
-
- # Determine which versions to generate
- # Default: both native + SVG reference. --only limits to one.
- only_mode = args.only # None | 'native' | 'legacy'
- gen_native = only_mode in (None, 'native')
- gen_legacy = only_mode in (None, 'legacy')
-
- # --native flag (deprecated) maps to --only native
- if args.native and only_mode is None:
- gen_legacy = False
-
- from datetime import datetime
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
-
- if args.output:
- # Explicit output: use as-is for the primary version
- output_base = Path(args.output)
- native_path = output_base
- stem = output_base.stem
- legacy_path = output_base.parent / f"{stem}_svg{output_base.suffix}"
- else:
- native_path = project_path / f"{project_name}_{timestamp}.pptx"
- legacy_path = project_path / f"{project_name}_{timestamp}_svg.pptx"
-
- native_path.parent.mkdir(parents=True, exist_ok=True)
-
- verbose = not args.quiet
-
- # Read notes files
- enable_notes = not args.no_notes
- notes = {}
- if enable_notes:
- notes = find_notes_files(project_path, svg_files)
-
- # Convert 'none' string to None for downstream logic
- transition = args.transition if args.transition != 'none' else None
-
- # Shared args for both runs
- shared_kwargs = dict(
- svg_files=svg_files,
- canvas_format=canvas_format,
- verbose=verbose,
- transition=transition,
- transition_duration=args.transition_duration,
- auto_advance=args.auto_advance,
- use_compat_mode=not args.no_compat,
- notes=notes,
- enable_notes=enable_notes,
- )
-
- success = True
-
- # --- Native shapes version (primary) ---
- if gen_native:
- if verbose:
- print("PPT Master - SVG to PPTX Tool")
- print("=" * 50)
- print(f" Project path: {project_path}")
- print(f" SVG directory: {source_dir_name}")
- print(f" Output file: {native_path}")
- print()
-
- ok = create_pptx_with_native_svg(
- output_path=native_path,
- use_native_shapes=True,
- **shared_kwargs,
- )
- success = success and ok
-
- # --- SVG image reference version ---
- if gen_legacy:
- if verbose:
- if gen_native:
- print()
- print("-" * 50)
- print("PPT Master - SVG to PPTX Tool (SVG Reference)")
- print("=" * 50)
- print(f" Project path: {project_path}")
- print(f" SVG directory: {source_dir_name}")
- print(f" Output file: {legacy_path}")
- print()
-
- ok = create_pptx_with_native_svg(
- output_path=legacy_path,
- use_native_shapes=False,
- **shared_kwargs,
- )
- success = success and ok
- sys.exit(0 if success else 1)
+# Ensure the scripts directory is on sys.path so the package can be found
+sys.path.insert(0, str(Path(__file__).resolve().parent))
+from svg_to_pptx import main
if __name__ == '__main__':
main()
diff --git a/skills/ppt-master/scripts/svg_to_pptx/__init__.py b/skills/ppt-master/scripts/svg_to_pptx/__init__.py
new file mode 100644
index 00000000..4cc803bf
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/__init__.py
@@ -0,0 +1,17 @@
+"""svg_to_pptx — SVG to PPTX conversion package.
+
+Public API:
+ - main(): CLI entry point
+ - convert_svg_to_slide_shapes(): SVG -> DrawingML slide XML
+ - create_pptx_with_native_svg(): Build PPTX from SVG files
+"""
+
+from .pptx_cli import main
+from .drawingml_converter import convert_svg_to_slide_shapes
+from .pptx_builder import create_pptx_with_native_svg
+
+__all__ = [
+ 'main',
+ 'convert_svg_to_slide_shapes',
+ 'create_pptx_with_native_svg',
+]
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_context.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_context.py
new file mode 100644
index 00000000..909d3fa7
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_context.py
@@ -0,0 +1,102 @@
+"""ConvertContext — shared state passed through the SVG → DrawingML pipeline."""
+
+from __future__ import annotations
+
+from pathlib import Path
+from xml.etree import ElementTree as ET
+from dataclasses import dataclass, field
+
+
+@dataclass
+class ConvertContext:
+ """Shared context passed through the SVG → DrawingML conversion pipeline.
+
+ Derived via child() during recursive SVG tree traversal to accumulate
+ translate / scale / inherited style information.
+ """
+
+ defs: dict[str, ET.Element] = field(default_factory=dict)
+ id_counter: int = 2 # 1 is reserved for spTree root
+ slide_num: int = 1
+ translate_x: float = 0.0
+ translate_y: float = 0.0
+ scale_x: float = 1.0
+ scale_y: float = 1.0
+ filter_id: str | None = None
+ media_files: dict[str, bytes] = field(default_factory=dict)
+ rel_entries: list[dict[str, str]] = field(default_factory=list)
+ rel_id_counter: int = 2 # rId1 reserved for slideLayout
+ svg_dir: Path | None = None
+ inherited_styles: dict[str, str] = field(default_factory=dict)
+
+ def next_id(self) -> int:
+ """Allocate the next shape ID."""
+ cid = self.id_counter
+ self.id_counter += 1
+ return cid
+
+ def next_rel_id(self) -> str:
+ """Allocate the next relationship ID (rIdN)."""
+ rid = f'rId{self.rel_id_counter}'
+ self.rel_id_counter += 1
+ return rid
+
+ def child(
+ self,
+ dx: float = 0,
+ dy: float = 0,
+ sx: float = 1.0,
+ sy: float = 1.0,
+ filter_id: str | None = None,
+ style_overrides: dict[str, str] | None = None,
+ ) -> ConvertContext:
+ """Create a child context with accumulated translate / scale / styles.
+
+ Args:
+ dx: X translation delta.
+ dy: Y translation delta.
+ sx: X scale factor.
+ sy: Y scale factor.
+ filter_id: Override filter ID.
+ style_overrides: Style attribute overrides from child element.
+ """
+ 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
+
+ return ConvertContext(
+ defs=self.defs,
+ id_counter=self.id_counter,
+ slide_num=self.slide_num,
+ translate_x=self.translate_x + dx,
+ translate_y=self.translate_y + dy,
+ scale_x=self.scale_x * sx,
+ scale_y=self.scale_y * sy,
+ filter_id=filter_id or self.filter_id,
+ media_files=self.media_files,
+ rel_entries=self.rel_entries,
+ rel_id_counter=self.rel_id_counter,
+ svg_dir=self.svg_dir,
+ inherited_styles=merged,
+ )
+
+ def sync_from_child(self, child_ctx: ConvertContext) -> None:
+ """Sync counters back from a child context."""
+ self.id_counter = child_ctx.id_counter
+ self.rel_id_counter = child_ctx.rel_id_counter
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_converter.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_converter.py
new file mode 100644
index 00000000..eef8b27f
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_converter.py
@@ -0,0 +1,269 @@
+"""Core SVG -> DrawingML dispatcher, group handling, and main entry point."""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+from xml.etree import ElementTree as ET
+
+from .drawingml_context import ConvertContext
+from .drawingml_utils import (
+ SVG_NS,
+ _extract_inheritable_styles, resolve_url_id,
+)
+from .drawingml_styles import build_shadow_xml
+from .drawingml_elements import (
+ convert_rect, convert_circle, convert_ellipse,
+ convert_line, convert_path,
+ convert_polygon, convert_polyline,
+ convert_text, convert_image,
+)
+
+
+# ---------------------------------------------------------------------------
+# Transform & layout helpers
+# ---------------------------------------------------------------------------
+
+def parse_transform(transform_str: str) -> tuple[float, float, float, float]:
+ """Parse SVG transform string, extract translate and scale.
+
+ Returns:
+ (dx, dy, sx, sy) tuple.
+ """
+ if not transform_str:
+ return 0.0, 0.0, 1.0, 1.0
+
+ dx, dy = 0.0, 0.0
+ sx, sy = 1.0, 1.0
+
+ m = re.search(r'translate\(\s*([-\d.]+)[\s,]+([-\d.]+)\s*\)', transform_str)
+ if m:
+ dx = float(m.group(1))
+ dy = float(m.group(2))
+
+ m = re.search(r'scale\(\s*([-\d.]+)(?:[\s,]+([-\d.]+))?\s*\)', transform_str)
+ if m:
+ sx = float(m.group(1))
+ sy = float(m.group(2)) if m.group(2) else sx
+
+ return dx, dy, sx, sy
+
+
+def _extract_shape_bounds_emu(shape_xml: str) -> tuple[int, int, int, int] | None:
+ """Extract bounds (x, y, x+cx, y+cy) in EMU from a shape XML string.
+
+ Works for <p:sp>, <p:pic>, and <p:grpSp>.
+ """
+ off_match = re.search(r'<a:off x="(-?\d+)" y="(-?\d+)"', shape_xml)
+ ext_match = re.search(r'<a:ext cx="(\d+)" cy="(\d+)"', shape_xml)
+ if off_match and ext_match:
+ x = int(off_match.group(1))
+ y = int(off_match.group(2))
+ cx = int(ext_match.group(1))
+ cy = int(ext_match.group(2))
+ return (x, y, x + cx, y + cy)
+ return None
+
+
+# ---------------------------------------------------------------------------
+# Group handling
+# ---------------------------------------------------------------------------
+
+def convert_g(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <g> to DrawingML group shape <p:grpSp>.
+
+ Preserves group structure so elements can be selected and moved together
+ in PowerPoint. Single-child groups are flattened to avoid unnecessary nesting.
+
+ Uses identity coordinate mapping (chOff/chExt == off/ext) so child shapes
+ keep their absolute slide coordinates unchanged.
+ """
+ transform = elem.get('transform', '')
+ dx, dy, sx, sy = parse_transform(transform)
+
+ 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)
+
+ child_shapes: list[str] = []
+ for child in elem:
+ shape_xml = convert_element(child, child_ctx)
+ if shape_xml:
+ child_shapes.append(shape_xml)
+
+ ctx.sync_from_child(child_ctx)
+
+ if not child_shapes:
+ return ''
+
+ # Single child: flatten
+ if len(child_shapes) == 1:
+ return child_shapes[0]
+
+ # Multiple children: wrap in <p:grpSp>
+ min_x = min_y = float('inf')
+ max_x = max_y = float('-inf')
+
+ for shape_xml in child_shapes:
+ bounds = _extract_shape_bounds_emu(shape_xml)
+ if bounds:
+ min_x = min(min_x, bounds[0])
+ min_y = min(min_y, bounds[1])
+ max_x = max(max_x, bounds[2])
+ max_y = max(max_y, bounds[3])
+
+ if min_x == float('inf'):
+ return '\n'.join(child_shapes)
+
+ group_x = int(min_x)
+ group_y = int(min_y)
+ group_w = max(int(max_x - min_x), 1)
+ group_h = max(int(max_y - min_y), 1)
+
+ shapes_xml = '\n'.join(child_shapes)
+ group_id = ctx.next_id()
+
+ group_effect = ''
+ if filter_id and filter_id in ctx.defs:
+ group_effect = build_shadow_xml(ctx.defs[filter_id])
+
+ return f'''<p:grpSp>
+<p:nvGrpSpPr>
+<p:cNvPr id="{group_id}" name="Group {group_id}"/>
+<p:cNvGrpSpPr/>
+<p:nvPr/>
+</p:nvGrpSpPr>
+<p:grpSpPr>
+<a:xfrm>
+<a:off x="{group_x}" y="{group_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}"/>
+</a:xfrm>
+{group_effect}
+</p:grpSpPr>
+{shapes_xml}
+</p:grpSp>'''
+
+
+# ---------------------------------------------------------------------------
+# Defs collection & element dispatch
+# ---------------------------------------------------------------------------
+
+_NON_VISUAL_TAGS = frozenset(('defs', 'title', 'desc', 'metadata', 'style'))
+
+_CONVERTERS = {
+ 'rect': convert_rect,
+ 'circle': convert_circle,
+ 'ellipse': convert_ellipse,
+ 'line': convert_line,
+ 'path': convert_path,
+ 'polygon': convert_polygon,
+ 'polyline': convert_polyline,
+ 'text': convert_text,
+ 'image': convert_image,
+ 'g': convert_g,
+}
+
+
+def collect_defs(root: ET.Element) -> dict[str, ET.Element]:
+ """Collect all <defs> children into an {id: element} dictionary."""
+ defs: dict[str, ET.Element] = {}
+ for defs_elem in root.iter(f'{{{SVG_NS}}}defs'):
+ for child in defs_elem:
+ elem_id = child.get('id')
+ if elem_id:
+ defs[elem_id] = child
+ # Also check for defs without namespace
+ for defs_elem in root.iter('defs'):
+ for child in defs_elem:
+ elem_id = child.get('id')
+ if elem_id:
+ defs[elem_id] = child
+ return defs
+
+
+def convert_element(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Dispatch an SVG element to the appropriate converter."""
+ tag = elem.tag.replace(f'{{{SVG_NS}}}', '')
+
+ converter = _CONVERTERS.get(tag)
+ if converter:
+ try:
+ return converter(elem, ctx)
+ except Exception as e:
+ print(f' Warning: Failed to convert <{tag}>: {e}')
+ return ''
+
+ if tag in _NON_VISUAL_TAGS:
+ return ''
+
+ return ''
+
+
+def convert_svg_to_slide_shapes(
+ svg_path: Path,
+ slide_num: int = 1,
+ verbose: bool = False,
+) -> tuple[str, dict[str, bytes], list[dict[str, str]]]:
+ """Convert an SVG file to a complete DrawingML slide XML.
+
+ Args:
+ svg_path: Path to the SVG file.
+ slide_num: Slide number (for naming).
+ verbose: Print progress info.
+
+ Returns:
+ (slide_xml, media_files, rel_entries) where:
+ - slide_xml: Complete slide XML string.
+ - media_files: Dict of {filename: bytes} for media to write.
+ - rel_entries: List of relationship entries to add.
+ """
+ tree = ET.parse(str(svg_path))
+ root = tree.getroot()
+
+ defs = collect_defs(root)
+ ctx = ConvertContext(defs=defs, slide_num=slide_num, svg_dir=Path(svg_path).parent)
+
+ shapes: list[str] = []
+ converted = 0
+ skipped = 0
+
+ for child in root:
+ tag = child.tag.replace(f'{{{SVG_NS}}}', '')
+ if tag == 'defs':
+ continue
+ result = convert_element(child, ctx)
+ if result:
+ shapes.append(result)
+ converted += 1
+ else:
+ if tag not in _NON_VISUAL_TAGS:
+ skipped += 1
+
+ if verbose:
+ print(f' Converted {converted} elements, skipped {skipped}')
+
+ shapes_xml = '\n'.join(shapes)
+
+ slide_xml = f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
+ xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
+<p:cSld>
+<p:spTree>
+<p:nvGrpSpPr>
+<p:cNvPr id="1" name=""/>
+<p:cNvGrpSpPr/><p:nvPr/>
+</p:nvGrpSpPr>
+<p:grpSpPr>
+<a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/>
+<a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm>
+</p:grpSpPr>
+{shapes_xml}
+</p:spTree>
+</p:cSld>
+<p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr>
+</p:sld>'''
+
+ return slide_xml, ctx.media_files, ctx.rel_entries
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
new file mode 100644
index 00000000..f65fdc28
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_elements.py
@@ -0,0 +1,797 @@
+"""SVG element converters: rect, circle, line, path, polygon, polyline, text, image, ellipse."""
+
+from __future__ import annotations
+
+import math
+import re
+import base64
+from typing import Any
+from xml.etree import ElementTree as ET
+
+from .drawingml_context import ConvertContext
+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,
+ parse_hex_color, resolve_url_id, get_effective_filter_id,
+ parse_font_family, is_cjk_char, estimate_text_width,
+ _xml_escape,
+)
+from .drawingml_styles import (
+ build_solid_fill, build_gradient_fill,
+ build_fill_xml, build_stroke_xml, build_shadow_xml,
+ get_fill_opacity, get_stroke_opacity,
+)
+from .drawingml_paths import (
+ PathCommand, parse_svg_path, svg_path_to_absolute,
+ normalize_path_commands, path_commands_to_drawingml,
+)
+
+
+def _wrap_shape(
+ shape_id: int, name: str,
+ off_x: int, off_y: int,
+ ext_cx: int, ext_cy: int,
+ geom_xml: str, fill_xml: str, stroke_xml: str,
+ effect_xml: str = '', extra_xml: str = '',
+ rot: int = 0,
+) -> str:
+ """Wrap DrawingML content into a <p:sp> shape element."""
+ rot_attr = f' rot="{rot}"' if rot else ''
+ return f'''<p:sp>
+<p:nvSpPr>
+<p:cNvPr id="{shape_id}" name="{_xml_escape(name)}"/>
+<p:cNvSpPr/><p:nvPr/>
+</p:nvSpPr>
+<p:spPr>
+<a:xfrm{rot_attr}><a:off x="{off_x}" y="{off_y}"/><a:ext cx="{ext_cx}" cy="{ext_cy}"/></a:xfrm>
+{geom_xml}
+{fill_xml}
+{stroke_xml}
+{effect_xml}
+</p:spPr>
+{extra_xml}
+</p:sp>'''
+
+
+# ---------------------------------------------------------------------------
+# rect
+# ---------------------------------------------------------------------------
+
+def convert_rect(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <rect> to DrawingML shape."""
+ x = ctx_x(_f(elem.get('x')), ctx)
+ y = ctx_y(_f(elem.get('y')), ctx)
+ w = ctx_w(_f(elem.get('width')), ctx)
+ h = ctx_h(_f(elem.get('height')), ctx)
+
+ if w <= 0 or h <= 0:
+ return ''
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ effect = ''
+ filt_id = get_effective_filter_id(elem, ctx)
+ if filt_id and filt_id in ctx.defs:
+ effect = build_shadow_xml(ctx.defs[filt_id])
+
+ geom = '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>'
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Rectangle {shape_id}',
+ px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
+ geom, fill, stroke, effect,
+ )
+
+
+# ---------------------------------------------------------------------------
+# circle (including donut-chart arc segments)
+# ---------------------------------------------------------------------------
+
+def _build_arc_ring_path(
+ cx: float, cy: float, r: float,
+ stroke_width: float,
+ dash_len: float, dash_offset: float,
+ rotate_deg: float,
+ sx: float, sy: float,
+) -> tuple[str, int, int, int, int]:
+ """Build a filled annular-sector (donut segment) as DrawingML custGeom.
+
+ SVG donut charts use stroke-dasharray on a circle to draw arc segments.
+ DrawingML cannot reproduce this, so we convert each arc segment into a
+ filled ring shape (outer arc -> line -> inner arc -> close).
+
+ Returns:
+ (geom_xml, min_x_emu, min_y_emu, w_emu, h_emu).
+ """
+ circumference = 2 * math.pi * r
+ if circumference <= 0:
+ return '', 0, 0, 0, 0
+
+ start_frac = -dash_offset / circumference
+ end_frac = start_frac + dash_len / circumference
+
+ start_angle = start_frac * 2 * math.pi + math.radians(rotate_deg)
+ end_angle = end_frac * 2 * math.pi + math.radians(rotate_deg)
+
+ half_sw = stroke_width / 2
+ r_outer = r + half_sw
+ r_inner = r - half_sw
+
+ num_segments = max(16, int(abs(end_angle - start_angle) / (math.pi / 32)))
+ angles = [
+ start_angle + (end_angle - start_angle) * i / num_segments
+ for i in range(num_segments + 1)
+ ]
+
+ outer_pts = [(cx + r_outer * math.sin(a), cy - r_outer * math.cos(a)) for a in angles]
+ inner_pts = [(cx + r_inner * math.sin(a), cy - r_inner * math.cos(a)) for a in reversed(angles)]
+
+ all_pts = [(px * sx, py * sy) for px, py in outer_pts + inner_pts]
+
+ xs = [p[0] for p in all_pts]
+ ys = [p[1] for p in all_pts]
+ min_x, max_x = min(xs), max(xs)
+ min_y, max_y = min(ys), max(ys)
+ width = max_x - min_x
+ height = max_y - min_y
+
+ if width < 0.5 or height < 0.5:
+ return '', 0, 0, 0, 0
+
+ w_emu = px_to_emu(width)
+ h_emu = px_to_emu(height)
+
+ lines: list[str] = []
+ for i, (px, py) in enumerate(all_pts):
+ lx = px_to_emu(px - min_x)
+ ly = px_to_emu(py - min_y)
+ if i == 0:
+ lines.append(f'<a:moveTo><a:pt x="{lx}" y="{ly}"/></a:moveTo>')
+ else:
+ lines.append(f'<a:lnTo><a:pt x="{lx}" y="{ly}"/></a:lnTo>')
+ lines.append('<a:close/>')
+
+ path_xml = '\n'.join(lines)
+ 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}">
+{path_xml}
+</a:path></a:pathLst>
+</a:custGeom>'''
+
+ return geom, px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu
+
+
+def _is_donut_circle(elem: ET.Element, ctx: ConvertContext) -> bool:
+ """Detect if a circle uses stroke-dasharray to simulate an arc segment."""
+ dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
+ if not dasharray or dasharray == 'none':
+ return False
+ stroke = _get_attr(elem, 'stroke', ctx)
+ if not stroke or stroke == 'none':
+ return False
+
+ sw = _f(_get_attr(elem, 'stroke-width', ctx), 0)
+ r = _f(elem.get('r'), 0)
+ if sw <= 0 or r <= 0:
+ return False
+
+ # Standard dash presets are not donut segments
+ if dasharray.strip() in DASH_PRESETS:
+ return False
+
+ return True
+
+
+def convert_circle(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <circle> to DrawingML ellipse or donut-arc shape."""
+ cx_ = _f(elem.get('cx'))
+ cy_ = _f(elem.get('cy'))
+ r = _f(elem.get('r'))
+
+ if r <= 0:
+ return ''
+
+ # --- Donut-chart arc segment detection ---
+ if _is_donut_circle(elem, ctx):
+ dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
+ dash_vals = re.split(r'[\s,]+', dasharray.strip())
+ dash_len = float(dash_vals[0]) if dash_vals else 0
+ dash_offset = _f(elem.get('stroke-dashoffset'), 0)
+ stroke_width = _f(_get_attr(elem, 'stroke-width', ctx), 1)
+
+ rotate_deg = 0.0
+ transform = elem.get('transform', '')
+ r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
+ if r_match:
+ rotate_deg = float(r_match.group(1))
+
+ geom, min_x, min_y, w_emu, h_emu = _build_arc_ring_path(
+ ctx_x(cx_, ctx) / ctx.scale_x,
+ ctx_y(cy_, ctx) / ctx.scale_y,
+ r, stroke_width, dash_len, dash_offset, rotate_deg,
+ ctx.scale_x, ctx.scale_y,
+ )
+ if not geom:
+ return ''
+
+ # Use the stroke color/gradient as fill for the arc shape
+ stroke_val = _get_attr(elem, 'stroke', ctx)
+ op = get_fill_opacity(elem, ctx)
+ grad_id = resolve_url_id(stroke_val) if stroke_val else None
+ if grad_id and grad_id in ctx.defs:
+ fill = build_gradient_fill(ctx.defs[grad_id], op)
+ elif stroke_val:
+ color = parse_hex_color(stroke_val)
+ fill = build_solid_fill(color, op) if color else '<a:noFill/>'
+ else:
+ fill = '<a:noFill/>'
+
+ stroke_xml = '<a:ln><a:noFill/></a:ln>'
+
+ effect = ''
+ filt_id = get_effective_filter_id(elem, ctx)
+ if filt_id and filt_id in ctx.defs:
+ effect = build_shadow_xml(ctx.defs[filt_id])
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Arc {shape_id}',
+ min_x, min_y, w_emu, h_emu,
+ geom, fill, stroke_xml, effect,
+ )
+
+ # --- Normal circle ---
+ cx_s = ctx_x(cx_, ctx)
+ cy_s = ctx_y(cy_, ctx)
+ r_x = r * ctx.scale_x
+ r_y = r * ctx.scale_y
+
+ x = cx_s - r_x
+ y = cy_s - r_y
+ w = r_x * 2
+ h = r_y * 2
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ effect = ''
+ filt_id = get_effective_filter_id(elem, ctx)
+ if filt_id and filt_id in ctx.defs:
+ effect = build_shadow_xml(ctx.defs[filt_id])
+
+ geom = '<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>'
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Ellipse {shape_id}',
+ px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
+ geom, fill, stroke, effect,
+ )
+
+
+# ---------------------------------------------------------------------------
+# line
+# ---------------------------------------------------------------------------
+
+def convert_line(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <line> to DrawingML custom geometry shape."""
+ x1 = ctx_x(_f(elem.get('x1')), ctx)
+ y1 = ctx_y(_f(elem.get('y1')), ctx)
+ x2 = ctx_x(_f(elem.get('x2')), ctx)
+ y2 = ctx_y(_f(elem.get('y2')), ctx)
+
+ min_x = min(x1, x2)
+ min_y = min(y1, y2)
+ w = max(abs(x2 - x1), 1)
+ h = max(abs(y2 - y1), 1)
+
+ w_emu = px_to_emu(w)
+ h_emu = px_to_emu(h)
+
+ lx1 = px_to_emu(x1 - min_x)
+ ly1 = px_to_emu(y1 - min_y)
+ lx2 = px_to_emu(x2 - min_x)
+ ly2 = px_to_emu(y2 - min_y)
+
+ 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}">
+<a:moveTo><a:pt x="{lx1}" y="{ly1}"/></a:moveTo>
+<a:lnTo><a:pt x="{lx2}" y="{ly2}"/></a:lnTo>
+</a:path></a:pathLst>
+</a:custGeom>'''
+
+ stroke_op = get_stroke_opacity(elem, ctx)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Line {shape_id}',
+ px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
+ geom, '<a:noFill/>', stroke,
+ )
+
+
+# ---------------------------------------------------------------------------
+# path
+# ---------------------------------------------------------------------------
+
+def convert_path(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <path> to DrawingML custom geometry shape."""
+ d = elem.get('d', '')
+ if not d:
+ return ''
+
+ commands = parse_svg_path(d)
+ commands = svg_path_to_absolute(commands)
+ commands = normalize_path_commands(commands)
+
+ tx, ty = 0.0, 0.0
+ rot = 0
+ transform = elem.get('transform')
+ if transform:
+ t_match = re.search(r'translate\(\s*([-\d.]+)[\s,]+([-\d.]+)\s*\)', transform)
+ if t_match:
+ tx = float(t_match.group(1))
+ ty = float(t_match.group(2))
+ r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
+ if r_match:
+ rot = int(float(r_match.group(1)) * ANGLE_UNIT)
+
+ path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
+ commands, ctx.translate_x + tx, ctx.translate_y + ty,
+ ctx.scale_x, ctx.scale_y,
+ )
+
+ if not path_xml:
+ return ''
+
+ w_emu = px_to_emu(width)
+ h_emu = px_to_emu(height)
+
+ 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}">
+{path_xml}
+</a:path></a:pathLst>
+</a:custGeom>'''
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ effect = ''
+ filt_id = get_effective_filter_id(elem, ctx)
+ if filt_id and filt_id in ctx.defs:
+ effect = build_shadow_xml(ctx.defs[filt_id])
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Freeform {shape_id}',
+ px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
+ geom, fill, stroke, effect, rot=rot,
+ )
+
+
+# ---------------------------------------------------------------------------
+# polygon / polyline
+# ---------------------------------------------------------------------------
+
+def _parse_points(points_str: str) -> list[tuple[float, float]]:
+ """Parse SVG points attribute into a list of (x, y) tuples."""
+ nums = re.findall(r'[-+]?(?:\d+\.?\d*|\.\d+)', points_str)
+ if len(nums) < 4:
+ return []
+ return [(float(nums[i]), float(nums[i + 1])) for i in range(0, len(nums) - 1, 2)]
+
+
+def convert_polygon(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <polygon> to DrawingML custom geometry shape."""
+ points = _parse_points(elem.get('points', ''))
+ if not points:
+ return ''
+
+ commands = [PathCommand('M', [points[0][0], points[0][1]])]
+ for px_, py_ in points[1:]:
+ commands.append(PathCommand('L', [px_, py_]))
+ commands.append(PathCommand('Z', []))
+
+ path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
+ commands, ctx.translate_x, ctx.translate_y,
+ ctx.scale_x, ctx.scale_y,
+ )
+
+ if not path_xml:
+ return ''
+
+ w_emu = px_to_emu(width)
+ h_emu = px_to_emu(height)
+
+ 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}">
+{path_xml}
+</a:path></a:pathLst>
+</a:custGeom>'''
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Polygon {shape_id}',
+ px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
+ geom, fill, stroke,
+ )
+
+
+def convert_polyline(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <polyline> to DrawingML custom geometry shape."""
+ points = _parse_points(elem.get('points', ''))
+ if not points:
+ return ''
+
+ commands = [PathCommand('M', [points[0][0], points[0][1]])]
+ for px_, py_ in points[1:]:
+ commands.append(PathCommand('L', [px_, py_]))
+
+ path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
+ commands, ctx.translate_x, ctx.translate_y,
+ ctx.scale_x, ctx.scale_y,
+ )
+
+ if not path_xml:
+ return ''
+
+ w_emu = px_to_emu(width)
+ h_emu = px_to_emu(height)
+
+ 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}">
+{path_xml}
+</a:path></a:pathLst>
+</a:custGeom>'''
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Polyline {shape_id}',
+ px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
+ geom, '<a:noFill/>', stroke,
+ )
+
+
+# ---------------------------------------------------------------------------
+# text
+# ---------------------------------------------------------------------------
+
+def _normalize_text(text: str) -> str:
+ """Collapse internal whitespace/newlines into a single space, strip ends."""
+ if not text:
+ return ''
+ return re.sub(r'\s+', ' ', text).strip()
+
+
+def _build_text_runs(
+ elem: ET.Element,
+ parent_attrs: dict[str, Any],
+) -> list[dict[str, Any]]:
+ """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.
+ """
+ runs: list[dict[str, Any]] = []
+
+ if elem.text:
+ t = _normalize_text(elem.text)
+ if t:
+ runs.append({**parent_attrs, 'text': t})
+
+ 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')
+ runs.append({**run_attrs, 'text': t})
+
+ # Tail text after </tspan> belongs to parent
+ if child.tail:
+ t = _normalize_text(child.tail)
+ if t:
+ runs.append({**parent_attrs, 'text': t})
+
+ return runs
+
+
+def _build_run_xml(
+ run: dict[str, Any],
+ default_fonts: dict[str, str],
+ ctx: ConvertContext | None = None,
+) -> str:
+ """Build a single <a:r> XML from a run dict. Supports gradient fills on text."""
+ text = run['text']
+ fill = run.get('fill', '000000')
+ fill_raw = run.get('fill_raw', '')
+ fw = run.get('font_weight', '400')
+ fs_px = run.get('font_size', 16)
+ fstyle = run.get('font_style', '')
+ ff = run.get('font_family', '')
+ opacity = run.get('opacity')
+
+ sz = round(fs_px * FONT_PX_TO_HUNDREDTHS_PT)
+ b_attr = ' b="1"' if fw in ('bold', '600', '700', '800', '900') else ''
+ i_attr = ' i="1"' if fstyle == 'italic' else ''
+
+ fonts = parse_font_family(ff) if ff else default_fonts
+
+ # Build fill XML - gradient or solid
+ grad_id = resolve_url_id(fill_raw)
+ if grad_id and ctx and grad_id in ctx.defs:
+ fill_xml = build_gradient_fill(ctx.defs[grad_id], opacity)
+ else:
+ alpha_xml = ''
+ if opacity is not None and opacity < 1.0:
+ alpha_xml = f'<a:alpha val="{int(opacity * 100000)}"/>'
+ fill_xml = f'<a:solidFill><a:srgbClr val="{fill}">{alpha_xml}</a:srgbClr></a:solidFill>'
+
+ return f'''<a:r>
+<a:rPr lang="zh-CN" sz="{sz}"{b_attr}{i_attr} dirty="0">
+{fill_xml}
+<a:latin typeface="{_xml_escape(fonts['latin'])}"/>
+<a:ea typeface="{_xml_escape(fonts['ea'])}"/>
+<a:cs typeface="{_xml_escape(fonts['latin'])}"/>
+</a:rPr>
+<a:t>{_xml_escape(text)}</a:t>
+</a:r>'''
+
+
+def convert_text(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <text> to DrawingML text shape with multi-run support."""
+ x = ctx_x(_f(elem.get('x')), ctx)
+ y = ctx_y(_f(elem.get('y')), ctx)
+ font_size = _f(_get_attr(elem, 'font-size', ctx), 16) * ctx.scale_y
+ font_weight = _get_attr(elem, 'font-weight', ctx) or '400'
+ font_family_str = _get_attr(elem, 'font-family', ctx) or ''
+ text_anchor = _get_attr(elem, 'text-anchor', ctx) or 'start'
+ fill_raw = _get_attr(elem, 'fill', ctx) or '#000000'
+ fill_color = parse_hex_color(fill_raw) or '000000'
+ opacity = get_fill_opacity(elem, ctx)
+ font_style = _get_attr(elem, 'font-style', ctx) or ''
+
+ fonts = parse_font_family(font_family_str)
+
+ parent_attrs: dict[str, Any] = {
+ 'fill': fill_color,
+ 'fill_raw': fill_raw,
+ 'font_weight': font_weight,
+ 'font_size': font_size,
+ 'font_family': font_family_str,
+ 'font_style': font_style,
+ 'opacity': opacity,
+ }
+ runs = _build_text_runs(elem, parent_attrs)
+
+ if not runs:
+ return ''
+
+ full_text = ''.join(r['text'] for r in runs)
+ if not full_text.strip():
+ return ''
+
+ # Estimate text dimensions
+ text_width = estimate_text_width(full_text, font_size, font_weight) * 1.15
+ text_height = font_size * 1.5
+ padding = font_size * 0.1
+
+ # Adjust position based on text-anchor
+ if text_anchor == 'middle':
+ box_x = x - text_width / 2 - padding
+ elif text_anchor == 'end':
+ box_x = x - text_width - padding
+ else:
+ box_x = x - padding
+
+ box_y = y - font_size * 0.85
+ box_w = text_width + padding * 2
+ box_h = text_height + padding
+
+ # Letter spacing
+ spc_attr = ''
+ letter_spacing = _get_attr(elem, 'letter-spacing', ctx)
+ if letter_spacing:
+ try:
+ spc_val = float(letter_spacing) * 100
+ spc_attr = f' spc="{int(spc_val)}"'
+ except ValueError:
+ pass
+
+ # Text rotation
+ text_rot = 0
+ text_transform = elem.get('transform', '')
+ if text_transform:
+ rot_match = re.search(r'rotate\(\s*([-\d.]+)', text_transform)
+ if rot_match:
+ text_rot = int(float(rot_match.group(1)) * ANGLE_UNIT)
+
+ # Alignment
+ algn_map = {'start': 'l', 'middle': 'ctr', 'end': 'r'}
+ algn = algn_map.get(text_anchor, 'l')
+
+ # Shadow effect
+ effect_xml = ''
+ filt_id = get_effective_filter_id(elem, ctx)
+ if filt_id and filt_id in ctx.defs:
+ effect_xml = build_shadow_xml(ctx.defs[filt_id])
+
+ shape_id = ctx.next_id()
+ rot_attr = f' rot="{text_rot}"' if text_rot else ''
+
+ runs_xml = '\n'.join(_build_run_xml(r, fonts, ctx) for r in runs)
+
+ return f'''<p:sp>
+<p:nvSpPr>
+<p:cNvPr id="{shape_id}" name="TextBox {shape_id}"/>
+<p:cNvSpPr txBox="1"/><p:nvPr/>
+</p:nvSpPr>
+<p:spPr>
+<a:xfrm{rot_attr}><a:off x="{px_to_emu(box_x)}" y="{px_to_emu(box_y)}"/>
+<a:ext cx="{px_to_emu(box_w)}" cy="{px_to_emu(box_h)}"/></a:xfrm>
+<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
+<a:noFill/>
+<a:ln><a:noFill/></a:ln>
+{effect_xml}
+</p:spPr>
+<p:txBody>
+<a:bodyPr wrap="none" lIns="0" tIns="0" rIns="0" bIns="0" anchor="t" anchorCtr="0">
+<a:spAutoFit/>
+</a:bodyPr>
+<a:lstStyle/>
+<a:p>
+<a:pPr algn="{algn}"/>
+{runs_xml}
+</a:p>
+</p:txBody>
+</p:sp>'''
+
+
+# ---------------------------------------------------------------------------
+# image
+# ---------------------------------------------------------------------------
+
+def convert_image(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <image> to DrawingML picture element."""
+ href = elem.get('href') or elem.get(f'{{{XLINK_NS}}}href')
+ if not href:
+ return ''
+
+ x = ctx_x(_f(elem.get('x')), ctx)
+ y = ctx_y(_f(elem.get('y')), ctx)
+ w = ctx_w(_f(elem.get('width')), ctx)
+ h = ctx_h(_f(elem.get('height')), ctx)
+
+ if w <= 0 or h <= 0:
+ return ''
+
+ # Extract image data
+ if href.startswith('data:'):
+ match = re.match(r'data:image/(\w+);base64,(.+)', href, re.DOTALL)
+ if not match:
+ return ''
+ img_format = match.group(1).lower()
+ if img_format == 'jpeg':
+ img_format = 'jpg'
+ img_data = base64.b64decode(match.group(2))
+ else:
+ if ctx.svg_dir is None:
+ return ''
+ img_path = ctx.svg_dir / href
+ if not img_path.exists():
+ img_path = ctx.svg_dir.parent / href
+ if not img_path.exists():
+ print(f' Warning: External image not found: {href}')
+ return ''
+ img_format = img_path.suffix.lstrip('.').lower()
+ if img_format == 'jpeg':
+ img_format = 'jpg'
+ img_data = img_path.read_bytes()
+
+ img_idx = len(ctx.media_files) + 1
+ img_filename = f's{ctx.slide_num}_img{img_idx}.{img_format}'
+ ctx.media_files[img_filename] = img_data
+
+ r_id = ctx.next_rel_id()
+ ctx.rel_entries.append({
+ 'id': r_id,
+ 'type': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
+ 'target': f'../media/{img_filename}',
+ })
+
+ shape_id = ctx.next_id()
+
+ return f'''<p:pic>
+<p:nvPicPr>
+<p:cNvPr id="{shape_id}" name="Image {shape_id}"/>
+<p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr>
+<p:nvPr/>
+</p:nvPicPr>
+<p:blipFill>
+<a:blip r:embed="{r_id}"/>
+<a:stretch><a:fillRect/></a:stretch>
+</p:blipFill>
+<p:spPr>
+<a:xfrm><a:off x="{px_to_emu(x)}" y="{px_to_emu(y)}"/>
+<a:ext cx="{px_to_emu(w)}" cy="{px_to_emu(h)}"/></a:xfrm>
+<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
+</p:spPr>
+</p:pic>'''
+
+
+# ---------------------------------------------------------------------------
+# ellipse
+# ---------------------------------------------------------------------------
+
+def convert_ellipse(elem: ET.Element, ctx: ConvertContext) -> str:
+ """Convert SVG <ellipse> to DrawingML ellipse shape."""
+ cx_ = ctx_x(_f(elem.get('cx')), ctx)
+ cy_ = ctx_y(_f(elem.get('cy')), ctx)
+ rx = _f(elem.get('rx')) * ctx.scale_x
+ ry = _f(elem.get('ry')) * ctx.scale_y
+
+ if rx <= 0 or ry <= 0:
+ return ''
+
+ x = cx_ - rx
+ y = cy_ - ry
+ w = rx * 2
+ h = ry * 2
+
+ fill_op = get_fill_opacity(elem, ctx)
+ stroke_op = get_stroke_opacity(elem, ctx)
+ fill = build_fill_xml(elem, ctx, fill_op)
+ stroke = build_stroke_xml(elem, ctx, stroke_op)
+
+ geom = '<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>'
+
+ shape_id = ctx.next_id()
+ return _wrap_shape(
+ shape_id, f'Ellipse {shape_id}',
+ px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
+ geom, fill, stroke,
+ )
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_paths.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_paths.py
new file mode 100644
index 00000000..ec2d1036
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_paths.py
@@ -0,0 +1,429 @@
+"""SVG path parsing, normalization, and DrawingML path command generation."""
+
+from __future__ import annotations
+
+import math
+import re
+from dataclasses import dataclass, field
+
+from .drawingml_utils import px_to_emu
+
+
+@dataclass
+class PathCommand:
+ """A single SVG path command with its arguments."""
+ cmd: str # M, L, C, Z, etc. (uppercase = absolute)
+ args: list[float] = field(default_factory=list)
+
+
+# Argument counts per SVG path command
+_ARG_COUNTS = {
+ 'M': 2, 'm': 2, 'L': 2, 'l': 2,
+ 'H': 1, 'h': 1, 'V': 1, 'v': 1,
+ 'C': 6, 'c': 6, 'S': 4, 's': 4,
+ 'Q': 4, 'q': 4, 'T': 2, 't': 2,
+ 'A': 7, 'a': 7, 'Z': 0, 'z': 0,
+}
+
+
+def parse_svg_path(d: str) -> list[PathCommand]:
+ """Parse SVG path d attribute into a list of PathCommands."""
+ if not d:
+ return []
+
+ commands: list[PathCommand] = []
+ tokens = re.findall(
+ r'[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?', d
+ )
+
+ current_cmd: str | None = None
+ current_args: list[float] = []
+
+ def flush() -> None:
+ nonlocal current_cmd, current_args
+ if current_cmd is None:
+ return
+
+ n = _ARG_COUNTS.get(current_cmd, 0)
+ if n == 0:
+ commands.append(PathCommand(current_cmd, []))
+ elif n > 0 and len(current_args) >= n:
+ i = 0
+ while i + n <= len(current_args):
+ commands.append(PathCommand(current_cmd, current_args[i:i + n]))
+ # After first M, implicit commands become L
+ if current_cmd == 'M':
+ current_cmd = 'L'
+ elif current_cmd == 'm':
+ current_cmd = 'l'
+ i += n
+ current_args = []
+
+ for token in tokens:
+ if token in 'MmLlHhVvCcSsQqTtAaZz':
+ flush()
+ current_cmd = token
+ current_args = []
+ else:
+ try:
+ current_args.append(float(token))
+ except ValueError:
+ pass
+
+ flush()
+ return commands
+
+
+def svg_path_to_absolute(commands: list[PathCommand]) -> list[PathCommand]:
+ """Convert all relative path commands to absolute."""
+ result: list[PathCommand] = []
+ cx, cy = 0.0, 0.0 # current point
+ sx, sy = 0.0, 0.0 # subpath start
+
+ for cmd in commands:
+ a = cmd.args
+ if cmd.cmd == 'M':
+ cx, cy = a[0], a[1]
+ sx, sy = cx, cy
+ result.append(PathCommand('M', [cx, cy]))
+ elif cmd.cmd == 'm':
+ cx += a[0]; cy += a[1]
+ sx, sy = cx, cy
+ result.append(PathCommand('M', [cx, cy]))
+ elif cmd.cmd == 'L':
+ cx, cy = a[0], a[1]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'l':
+ cx += a[0]; cy += a[1]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'H':
+ cx = a[0]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'h':
+ cx += a[0]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'V':
+ cy = a[0]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'v':
+ cy += a[0]
+ result.append(PathCommand('L', [cx, cy]))
+ elif cmd.cmd == 'C':
+ result.append(PathCommand('C', list(a)))
+ cx, cy = a[4], a[5]
+ elif cmd.cmd == 'c':
+ abs_args = [
+ cx + a[0], cy + a[1],
+ cx + a[2], cy + a[3],
+ cx + a[4], cy + a[5],
+ ]
+ result.append(PathCommand('C', abs_args))
+ cx, cy = abs_args[4], abs_args[5]
+ elif cmd.cmd == 'S':
+ result.append(PathCommand('S', list(a)))
+ cx, cy = a[2], a[3]
+ elif cmd.cmd == 's':
+ abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
+ result.append(PathCommand('S', abs_args))
+ cx, cy = abs_args[2], abs_args[3]
+ elif cmd.cmd == 'Q':
+ result.append(PathCommand('Q', list(a)))
+ cx, cy = a[2], a[3]
+ elif cmd.cmd == 'q':
+ abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
+ result.append(PathCommand('Q', abs_args))
+ cx, cy = abs_args[2], abs_args[3]
+ elif cmd.cmd == 'T':
+ result.append(PathCommand('T', list(a)))
+ cx, cy = a[0], a[1]
+ elif cmd.cmd == 't':
+ abs_args = [cx + a[0], cy + a[1]]
+ result.append(PathCommand('T', abs_args))
+ cx, cy = abs_args[0], abs_args[1]
+ elif cmd.cmd == 'A':
+ result.append(PathCommand('A', list(a)))
+ cx, cy = a[5], a[6]
+ elif cmd.cmd == 'a':
+ abs_args = [a[0], a[1], a[2], a[3], a[4], cx + a[5], cy + a[6]]
+ result.append(PathCommand('A', abs_args))
+ cx, cy = abs_args[5], abs_args[6]
+ elif cmd.cmd in ('Z', 'z'):
+ result.append(PathCommand('Z', []))
+ cx, cy = sx, sy
+
+ return result
+
+
+def _reflect_control_point(
+ cp_x: float, cp_y: float,
+ cx: float, cy: float,
+) -> tuple[float, float]:
+ """Reflect a control point through the current point."""
+ return 2 * cx - cp_x, 2 * cy - cp_y
+
+
+def _quad_to_cubic(
+ qp_x: float, qp_y: float,
+ p0_x: float, p0_y: float,
+ p3_x: float, p3_y: float,
+) -> list[float]:
+ """Convert quadratic bezier control point to cubic bezier control points."""
+ cp1_x = p0_x + 2.0 / 3.0 * (qp_x - p0_x)
+ cp1_y = p0_y + 2.0 / 3.0 * (qp_y - p0_y)
+ cp2_x = p3_x + 2.0 / 3.0 * (qp_x - p3_x)
+ cp2_y = p3_y + 2.0 / 3.0 * (qp_y - p3_y)
+ return [cp1_x, cp1_y, cp2_x, cp2_y, p3_x, p3_y]
+
+
+def _arc_to_cubic_beziers(
+ cx_: float, cy_: float,
+ rx: float, ry: float,
+ phi: float,
+ large_arc: int, sweep: int,
+ x2: float, y2: float,
+) -> list[PathCommand]:
+ """Convert SVG arc (endpoint parameterization) to cubic bezier curves.
+
+ Uses the algorithm from the SVG spec (F.6.5) to convert endpoint to center
+ parameterization, then approximates each arc segment with cubic beziers.
+ """
+ x1, y1 = cx_, cy_
+
+ if abs(x1 - x2) < 1e-10 and abs(y1 - y2) < 1e-10:
+ return []
+
+ rx = abs(rx)
+ ry = abs(ry)
+ if rx < 1e-10 or ry < 1e-10:
+ return [PathCommand('L', [x2, y2])]
+
+ phi_rad = math.radians(phi)
+ cos_phi = math.cos(phi_rad)
+ sin_phi = math.sin(phi_rad)
+
+ # Step 1: Compute (x1', y1')
+ dx = (x1 - x2) / 2.0
+ dy = (y1 - y2) / 2.0
+ x1p = cos_phi * dx + sin_phi * dy
+ y1p = -sin_phi * dx + cos_phi * dy
+
+ # Step 2: Compute (cx', cy')
+ x1p2 = x1p * x1p
+ y1p2 = y1p * y1p
+ rx2 = rx * rx
+ ry2 = ry * ry
+
+ # Ensure radii are large enough
+ lam = x1p2 / rx2 + y1p2 / ry2
+ if lam > 1:
+ lam_sqrt = math.sqrt(lam)
+ rx *= lam_sqrt
+ ry *= lam_sqrt
+ rx2 = rx * rx
+ ry2 = ry * ry
+
+ num = max(rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2, 0)
+ den = rx2 * y1p2 + ry2 * x1p2
+ sq = math.sqrt(num / den) if den > 1e-10 else 0.0
+
+ if large_arc == sweep:
+ sq = -sq
+
+ cxp = sq * rx * y1p / ry
+ cyp = -sq * ry * x1p / rx
+
+ # Step 3: Compute (cx, cy)
+ arc_cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.0
+ arc_cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.0
+
+ # Step 4: Compute theta1 and dtheta
+ def angle_between(ux: float, uy: float, vx: float, vy: float) -> float:
+ n = math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy))
+ if n < 1e-10:
+ return 0
+ c = max(-1, min(1, (ux * vx + uy * vy) / n))
+ a = math.acos(c)
+ if ux * vy - uy * vx < 0:
+ a = -a
+ return a
+
+ theta1 = angle_between(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry)
+ dtheta = angle_between(
+ (x1p - cxp) / rx, (y1p - cyp) / ry,
+ (-x1p - cxp) / rx, (-y1p - cyp) / ry,
+ )
+
+ if sweep == 0 and dtheta > 0:
+ dtheta -= 2 * math.pi
+ elif sweep == 1 and dtheta < 0:
+ dtheta += 2 * math.pi
+
+ # Split arc into segments of at most 90 degrees
+ n_segs = max(1, int(math.ceil(abs(dtheta) / (math.pi / 2))))
+ d_per_seg = dtheta / n_segs
+
+ result: list[PathCommand] = []
+ alpha = 4.0 / 3.0 * math.tan(d_per_seg / 4.0)
+
+ for i in range(n_segs):
+ t1 = theta1 + i * d_per_seg
+ t2 = theta1 + (i + 1) * d_per_seg
+
+ cos_t1 = math.cos(t1)
+ sin_t1 = math.sin(t1)
+ cos_t2 = math.cos(t2)
+ sin_t2 = math.sin(t2)
+
+ ep1_x = cos_t1 - alpha * sin_t1
+ ep1_y = sin_t1 + alpha * cos_t1
+ ep2_x = cos_t2 + alpha * sin_t2
+ ep2_y = sin_t2 - alpha * cos_t2
+ ep_x = cos_t2
+ ep_y = sin_t2
+
+ def transform_pt(px: float, py: float) -> tuple[float, float]:
+ x = rx * px
+ y = ry * py
+ xr = cos_phi * x - sin_phi * y + arc_cx
+ yr = sin_phi * x + cos_phi * y + arc_cy
+ return xr, yr
+
+ cp1 = transform_pt(ep1_x, ep1_y)
+ cp2 = transform_pt(ep2_x, ep2_y)
+ ep = transform_pt(ep_x, ep_y)
+
+ result.append(PathCommand('C', [cp1[0], cp1[1], cp2[0], cp2[1], ep[0], ep[1]]))
+
+ return result
+
+
+def normalize_path_commands(commands: list[PathCommand]) -> list[PathCommand]:
+ """Normalize path commands to M/L/C/Z only.
+
+ Converts S -> C, Q -> C, T -> C, A -> C sequences.
+ """
+ result: list[PathCommand] = []
+ cx, cy = 0.0, 0.0
+ last_cp_x, last_cp_y = 0.0, 0.0
+ last_cmd = ''
+
+ for cmd in commands:
+ a = cmd.args
+
+ if cmd.cmd == 'M':
+ cx, cy = a[0], a[1]
+ last_cp_x, last_cp_y = cx, cy
+ result.append(cmd)
+ elif cmd.cmd == 'L':
+ cx, cy = a[0], a[1]
+ last_cp_x, last_cp_y = cx, cy
+ result.append(cmd)
+ elif cmd.cmd == 'C':
+ last_cp_x, last_cp_y = a[2], a[3]
+ cx, cy = a[4], a[5]
+ result.append(cmd)
+ elif cmd.cmd == 'S':
+ if last_cmd in ('C', 'S'):
+ rcp_x, rcp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
+ else:
+ rcp_x, rcp_y = cx, cy
+ last_cp_x, last_cp_y = a[0], a[1]
+ new_cx, new_cy = a[2], a[3]
+ result.append(PathCommand('C', [rcp_x, rcp_y, a[0], a[1], new_cx, new_cy]))
+ cx, cy = new_cx, new_cy
+ elif cmd.cmd == 'Q':
+ cubic = _quad_to_cubic(a[0], a[1], cx, cy, a[2], a[3])
+ last_cp_x, last_cp_y = a[0], a[1]
+ result.append(PathCommand('C', cubic))
+ cx, cy = a[2], a[3]
+ elif cmd.cmd == 'T':
+ if last_cmd in ('Q', 'T'):
+ qp_x, qp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
+ else:
+ qp_x, qp_y = cx, cy
+ last_cp_x, last_cp_y = qp_x, qp_y
+ cubic = _quad_to_cubic(qp_x, qp_y, cx, cy, a[0], a[1])
+ result.append(PathCommand('C', cubic))
+ cx, cy = a[0], a[1]
+ elif cmd.cmd == 'A':
+ arc_beziers = _arc_to_cubic_beziers(
+ cx, cy, a[0], a[1], a[2], int(a[3]), int(a[4]), a[5], a[6],
+ )
+ for bc in arc_beziers:
+ result.append(bc)
+ cx, cy = a[5], a[6]
+ last_cp_x, last_cp_y = cx, cy
+ elif cmd.cmd == 'Z':
+ result.append(cmd)
+ else:
+ result.append(cmd)
+
+ last_cmd = cmd.cmd
+
+ return result
+
+
+def path_commands_to_drawingml(
+ commands: list[PathCommand],
+ offset_x: float = 0,
+ offset_y: float = 0,
+ scale_x: float = 1.0,
+ scale_y: float = 1.0,
+) -> tuple[str, float, float, float, float]:
+ """Convert normalized path commands to DrawingML <a:path> inner XML.
+
+ Returns:
+ (path_xml, min_x, min_y, width, height) in scaled+offset coordinates.
+ """
+ if not commands:
+ return '', 0, 0, 0, 0
+
+ # First pass: calculate bounding box
+ points: list[tuple[float, float]] = []
+ for cmd in commands:
+ if cmd.cmd in ('M', 'L'):
+ points.append((
+ cmd.args[0] * scale_x + offset_x,
+ cmd.args[1] * scale_y + offset_y,
+ ))
+ elif cmd.cmd == 'C':
+ for i in range(0, 6, 2):
+ points.append((
+ cmd.args[i] * scale_x + offset_x,
+ cmd.args[i + 1] * scale_y + offset_y,
+ ))
+
+ if not points:
+ return '', 0, 0, 0, 0
+
+ min_x = min(p[0] for p in points)
+ min_y = min(p[1] for p in points)
+ max_x = max(p[0] for p in points)
+ max_y = max(p[1] for p in points)
+
+ width = max(max_x - min_x, 1)
+ height = max(max_y - min_y, 1)
+
+ # Second pass: generate DrawingML path commands (EMU, relative to shape)
+ parts: list[str] = []
+ for cmd in commands:
+ if cmd.cmd == 'M':
+ x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
+ y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
+ parts.append(f'<a:moveTo><a:pt x="{x_emu}" y="{y_emu}"/></a:moveTo>')
+ elif cmd.cmd == 'L':
+ x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
+ y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
+ parts.append(f'<a:lnTo><a:pt x="{x_emu}" y="{y_emu}"/></a:lnTo>')
+ elif cmd.cmd == 'C':
+ pts = []
+ for i in range(0, 6, 2):
+ x_emu = px_to_emu(cmd.args[i] * scale_x + offset_x - min_x)
+ y_emu = px_to_emu(cmd.args[i + 1] * scale_y + offset_y - min_y)
+ pts.append(f'<a:pt x="{x_emu}" y="{y_emu}"/>')
+ parts.append(f'<a:cubicBezTo>{"".join(pts)}</a:cubicBezTo>')
+ elif cmd.cmd == 'Z':
+ parts.append('<a:close/>')
+
+ path_inner = '\n'.join(parts)
+ return path_inner, min_x, min_y, width, height
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py
new file mode 100644
index 00000000..4148efa4
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_styles.py
@@ -0,0 +1,288 @@
+"""Fill, stroke, and shadow XML builders for DrawingML conversion."""
+
+from __future__ import annotations
+
+import math
+from xml.etree import ElementTree as ET
+
+from .drawingml_context import ConvertContext
+from .drawingml_utils import (
+ SVG_NS, ANGLE_UNIT, DASH_PRESETS,
+ px_to_emu, _f, _get_attr,
+ parse_hex_color, parse_stop_style, resolve_url_id,
+)
+
+
+def build_solid_fill(color: str, opacity: float | None = None) -> str:
+ """Build <a:solidFill> XML."""
+ alpha = ''
+ if opacity is not None and opacity < 1.0:
+ alpha = f'<a:alpha val="{int(opacity * 100000)}"/>'
+ return f'<a:solidFill><a:srgbClr val="{color}">{alpha}</a:srgbClr></a:solidFill>'
+
+
+def build_gradient_fill(
+ grad_elem: ET.Element,
+ opacity: float | None = None,
+) -> str:
+ """Build <a:gradFill> from SVG linearGradient or radialGradient element."""
+ tag = grad_elem.tag.replace(f'{{{SVG_NS}}}', '')
+
+ stops_xml = []
+ for child in grad_elem:
+ child_tag = child.tag.replace(f'{{{SVG_NS}}}', '')
+ if child_tag != 'stop':
+ continue
+
+ offset_str = child.get('offset', '0').strip().rstrip('%')
+ try:
+ offset = float(offset_str)
+ if offset > 1.0:
+ offset = offset / 100.0
+ except ValueError:
+ offset = 0.0
+ pos = int(offset * 100000)
+
+ # Parse color from style attribute or direct attributes
+ style = child.get('style', '')
+ color, stop_opacity = parse_stop_style(style)
+ if not color:
+ color = parse_hex_color(child.get('stop-color', '#000000'))
+ if color is None:
+ color = '000000'
+
+ direct_stop_op = child.get('stop-opacity')
+ if direct_stop_op is not None:
+ try:
+ stop_opacity = float(direct_stop_op)
+ except ValueError:
+ pass
+
+ alpha_xml = ''
+ effective_opacity = stop_opacity
+ if opacity is not None:
+ effective_opacity *= opacity
+ if effective_opacity < 1.0:
+ alpha_xml = f'<a:alpha val="{int(effective_opacity * 100000)}"/>'
+
+ stops_xml.append(
+ f'<a:gs pos="{pos}"><a:srgbClr val="{color}">{alpha_xml}</a:srgbClr></a:gs>'
+ )
+
+ if not stops_xml:
+ return ''
+
+ gs_list = '\n'.join(stops_xml)
+
+ if tag == 'linearGradient':
+ def parse_grad_coord(val_str: str, default: float = 0.0) -> float:
+ val_str = val_str.strip()
+ if val_str.endswith('%'):
+ return float(val_str.rstrip('%')) / 100.0
+ v = float(val_str)
+ return v / 100.0 if v > 1.0 else v
+
+ x1 = parse_grad_coord(grad_elem.get('x1', '0'))
+ y1 = parse_grad_coord(grad_elem.get('y1', '0'))
+ x2 = parse_grad_coord(grad_elem.get('x2', '1'))
+ y2 = parse_grad_coord(grad_elem.get('y2', '1'))
+
+ angle_rad = math.atan2(y2 - y1, x2 - x1)
+ angle_deg = math.degrees(angle_rad)
+ dml_angle = int((angle_deg % 360) * ANGLE_UNIT)
+
+ return f'''<a:gradFill>
+<a:gsLst>{gs_list}</a:gsLst>
+<a:lin ang="{dml_angle}" scaled="1"/>
+</a:gradFill>'''
+
+ elif tag == 'radialGradient':
+ return f'''<a:gradFill>
+<a:gsLst>{gs_list}</a:gsLst>
+<a:path path="circle">
+<a:fillToRect l="50000" t="50000" r="50000" b="50000"/>
+</a:path>
+</a:gradFill>'''
+
+ return ''
+
+
+def build_fill_xml(
+ elem: ET.Element,
+ ctx: ConvertContext,
+ opacity: float | None = None,
+) -> str:
+ """Build fill XML for a shape element, with inherited style support."""
+ fill = _get_attr(elem, 'fill', ctx)
+ if fill is None:
+ fill = '#000000' # SVG default fill is black
+
+ if fill == 'none':
+ return '<a:noFill/>'
+
+ grad_id = resolve_url_id(fill)
+ if grad_id and grad_id in ctx.defs:
+ return build_gradient_fill(ctx.defs[grad_id], opacity)
+
+ color = parse_hex_color(fill)
+ if color:
+ return build_solid_fill(color, opacity)
+
+ return '<a:noFill/>'
+
+
+def build_stroke_xml(
+ elem: ET.Element,
+ ctx: ConvertContext,
+ opacity: float | None = None,
+) -> str:
+ """Build <a:ln> XML for stroke, with inherited style support."""
+ stroke = _get_attr(elem, 'stroke', ctx)
+ if not stroke or stroke == 'none':
+ return '<a:ln><a:noFill/></a:ln>'
+
+ width = _f(_get_attr(elem, 'stroke-width', ctx), 1.0)
+ width_emu = px_to_emu(width)
+
+ # Dash pattern
+ dash_xml = ''
+ dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
+ if dasharray and dasharray != 'none':
+ preset = DASH_PRESETS.get(dasharray.strip())
+ if preset:
+ dash_xml = f'<a:prstDash val="{preset}"/>'
+ else:
+ dash_xml = '<a:prstDash val="dash"/>'
+
+ # Line cap
+ cap_map = {'round': 'rnd', 'square': 'sq', 'butt': 'flat'}
+ cap_attr = ''
+ linecap = _get_attr(elem, 'stroke-linecap', ctx)
+ if linecap and linecap in cap_map:
+ cap_attr = f' cap="{cap_map[linecap]}"'
+
+ # Gradient stroke
+ grad_id = resolve_url_id(stroke)
+ if grad_id and grad_id in ctx.defs:
+ grad_fill = build_gradient_fill(ctx.defs[grad_id], opacity)
+ return f'<a:ln w="{width_emu}"{cap_attr}>{grad_fill}{dash_xml}</a:ln>'
+
+ # Solid color stroke
+ color = parse_hex_color(stroke)
+ if not color:
+ return '<a:ln><a:noFill/></a:ln>'
+
+ alpha_xml = ''
+ if opacity is not None and opacity < 1.0:
+ alpha_xml = f'<a:alpha val="{int(opacity * 100000)}"/>'
+
+ return f'''<a:ln w="{width_emu}"{cap_attr}>
+<a:solidFill><a:srgbClr val="{color}">{alpha_xml}</a:srgbClr></a:solidFill>{dash_xml}
+</a:ln>'''
+
+
+def build_shadow_xml(filter_elem: ET.Element) -> str:
+ """Build <a:effectLst> with <a:outerShdw> from SVG filter element."""
+ if filter_elem is None:
+ return ''
+
+ std_dev = 4.0
+ dx = 0.0
+ dy = 4.0
+ shadow_opacity = 0.3
+ shadow_color = '000000'
+
+ for child in filter_elem.iter():
+ tag = child.tag.replace(f'{{{SVG_NS}}}', '')
+ if tag == 'feGaussianBlur':
+ std_dev = _f(child.get('stdDeviation'), 4.0)
+ elif tag == 'feOffset':
+ dx = _f(child.get('dx'), 0.0)
+ dy = _f(child.get('dy'), 4.0)
+ elif tag == 'feFlood':
+ shadow_opacity = _f(child.get('flood-opacity'), 0.3)
+ raw_color = child.get('flood-color', '').strip().lstrip('#')
+ if len(raw_color) == 6 and all(c in '0123456789abcdefABCDEF' for c in raw_color):
+ shadow_color = raw_color.upper()
+ elif tag == 'feFuncA':
+ if child.get('type') == 'linear':
+ shadow_opacity = _f(child.get('slope'), 0.3)
+
+ blur_rad = px_to_emu(std_dev * 2)
+ dist = px_to_emu(math.sqrt(dx * dx + dy * dy))
+ dir_angle = int(((90 + math.degrees(math.atan2(dy, max(dx, 0.001)))) % 360) * ANGLE_UNIT)
+ alpha_val = int(shadow_opacity * 100000)
+
+ return f'''<a:effectLst>
+<a:outerShdw blurRad="{blur_rad}" dist="{dist}" dir="{dir_angle}" algn="tl" rotWithShape="0">
+<a:srgbClr val="{shadow_color}"><a:alpha val="{alpha_val}"/></a:srgbClr>
+</a:outerShdw>
+</a:effectLst>'''
+
+
+def get_element_opacity(elem: ET.Element) -> float | None:
+ """Get opacity value from element. Returns None if 1.0 or not set."""
+ op = elem.get('opacity')
+ if op is None:
+ return None
+ try:
+ val = float(op)
+ return val if val < 1.0 else None
+ except ValueError:
+ return None
+
+
+def get_fill_opacity(
+ elem: ET.Element,
+ ctx: ConvertContext | None = None,
+) -> float | None:
+ """Get effective fill opacity combining 'opacity' and 'fill-opacity'.
+
+ Returns:
+ Combined opacity value, or None if fully opaque.
+ """
+ base = 1.0
+
+ op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
+ if op:
+ try:
+ base = 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)
+ except ValueError:
+ pass
+
+ return base if base < 1.0 else None
+
+
+def get_stroke_opacity(
+ elem: ET.Element,
+ ctx: ConvertContext | None = None,
+) -> float | None:
+ """Get effective stroke opacity combining 'opacity' and 'stroke-opacity'.
+
+ Returns:
+ Combined opacity value, or None if fully opaque.
+ """
+ base = 1.0
+
+ op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
+ if op:
+ try:
+ base = 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)
+ except ValueError:
+ pass
+
+ return base if base < 1.0 else None
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
new file mode 100644
index 00000000..2e7a8c9a
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml_utils.py
@@ -0,0 +1,310 @@
+"""Coordinate helpers, color parsing, and font utilities for DrawingML conversion."""
+
+from __future__ import annotations
+
+import re
+from xml.etree import ElementTree as ET
+
+from .drawingml_context import ConvertContext
+
+# ---------------------------------------------------------------------------
+# Constants
+# ---------------------------------------------------------------------------
+
+SVG_NS = 'http://www.w3.org/2000/svg'
+XLINK_NS = 'http://www.w3.org/1999/xlink'
+
+EMU_PER_PX = 9525 # 1 SVG px = 9525 EMU (96 DPI)
+FONT_PX_TO_HUNDREDTHS_PT = 75 # 1px = 0.75pt -> 75 hundredths-of-a-point
+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',
+ 'opacity', 'fill-opacity', 'stroke-opacity',
+ 'font-family', 'font-size', 'font-weight', 'font-style',
+ 'text-anchor', 'letter-spacing',
+]
+
+# Known East Asian fonts
+EA_FONTS = {
+ 'PingFang SC', 'PingFang TC', 'PingFang HK',
+ 'Microsoft YaHei', 'Microsoft JhengHei',
+ 'SimSun', 'SimHei', 'FangSong', 'KaiTi', 'STKaiti',
+ 'STHeiti', 'STSong', 'STFangsong', 'STXihei', 'STZhongsong',
+ 'Hiragino Sans', 'Hiragino Sans GB', 'Hiragino Mincho ProN',
+ 'Noto Sans SC', 'Noto Sans TC', 'Noto Serif SC', 'Noto Serif TC',
+ 'Source Han Sans SC', 'Source Han Sans TC',
+ 'Source Han Serif SC', 'Source Han Serif TC',
+ 'WenQuanYi Micro Hei', 'WenQuanYi Zen Hei',
+ 'YouYuan', 'LiSu', 'HuaWenKaiTi',
+ 'Songti SC', 'Songti TC',
+}
+SYSTEM_FONTS = {'system-ui', '-apple-system', 'BlinkMacSystemFont'}
+
+# macOS/Linux-only fonts -> Windows equivalents
+FONT_FALLBACK_WIN = {
+ 'PingFang SC': 'Microsoft YaHei',
+ 'PingFang TC': 'Microsoft JhengHei',
+ 'PingFang HK': 'Microsoft JhengHei',
+ 'Hiragino Sans': 'Microsoft YaHei',
+ 'Hiragino Sans GB': 'Microsoft YaHei',
+ 'Hiragino Mincho ProN': 'SimSun',
+ 'STHeiti': 'SimHei',
+ 'STSong': 'SimSun',
+ 'STKaiti': 'KaiTi',
+ 'STFangsong': 'FangSong',
+ 'STXihei': 'Microsoft YaHei',
+ 'STZhongsong': 'SimSun',
+ 'Songti SC': 'SimSun',
+ 'Songti TC': 'SimSun',
+ 'Noto Sans SC': 'Microsoft YaHei',
+ 'Noto Sans TC': 'Microsoft JhengHei',
+ 'Noto Serif SC': 'SimSun',
+ 'Noto Serif TC': 'SimSun',
+ 'Source Han Sans SC': 'Microsoft YaHei',
+ 'Source Han Sans TC': 'Microsoft JhengHei',
+ 'Source Han Serif SC': 'SimSun',
+ 'Source Han Serif TC': 'SimSun',
+ 'WenQuanYi Micro Hei': 'Microsoft YaHei',
+ 'WenQuanYi Zen Hei': 'Microsoft YaHei',
+ # Latin fonts (macOS / Linux / Web -> Windows)
+ 'SF Pro': 'Segoe UI',
+ 'SF Pro Display': 'Segoe UI',
+ 'SF Pro Text': 'Segoe UI',
+ 'SF Mono': 'Consolas',
+ 'Menlo': 'Consolas',
+ 'Monaco': 'Consolas',
+ 'Helvetica Neue': 'Arial',
+ 'Helvetica': 'Arial',
+ 'Roboto': 'Segoe UI',
+ 'Ubuntu': 'Segoe UI',
+ 'Liberation Sans': 'Arial',
+ 'Liberation Serif': 'Times New Roman',
+ 'Liberation Mono': 'Consolas',
+ 'DejaVu Sans': 'Segoe UI',
+ 'DejaVu Serif': 'Times New Roman',
+ 'DejaVu Sans Mono': 'Consolas',
+}
+
+GENERIC_FONT_MAP = {
+ 'monospace': 'Consolas',
+ 'sans-serif': 'Segoe UI',
+ 'serif': 'Times New Roman',
+}
+
+# When the latin font is serif and no EA font is specified,
+# prefer SimSun (serif CJK) over Microsoft YaHei (sans-serif CJK).
+_SERIF_LATIN = {
+ 'Times New Roman', 'Georgia', 'Garamond', 'Palatino', 'Palatino Linotype',
+ 'Book Antiqua', 'Cambria', 'SimSun', 'Liberation Serif', 'DejaVu Serif',
+}
+
+# SVG stroke-dasharray -> DrawingML prstDash
+DASH_PRESETS = {
+ '4,4': 'dash', '4 4': 'dash',
+ '6,3': 'dash', '6 3': 'dash',
+ '2,2': 'sysDot', '2 2': 'sysDot',
+ '8,4': 'lgDash', '8 4': 'lgDash',
+ '8,4,2,4': 'lgDashDot', '8 4 2 4': 'lgDashDot',
+}
+
+
+# ---------------------------------------------------------------------------
+# Coordinate helpers
+# ---------------------------------------------------------------------------
+
+def px_to_emu(px: float) -> int:
+ """Convert SVG pixels to EMU."""
+ return round(px * EMU_PER_PX)
+
+
+def _f(val: str | None, default: float = 0.0) -> float:
+ """Parse a float attribute value, returning default if missing."""
+ if val is None:
+ return default
+ try:
+ return float(val)
+ except (ValueError, TypeError):
+ return default
+
+
+def _extract_inheritable_styles(elem: ET.Element) -> dict[str, str]:
+ """Extract all SVG-inheritable presentation attributes from an element."""
+ styles: dict[str, str] = {}
+ for attr in INHERITABLE_ATTRS:
+ val = elem.get(attr)
+ if val is not None:
+ styles[attr] = val
+ return styles
+
+
+def _get_attr(elem: ET.Element, attr: str, ctx: ConvertContext) -> str | None:
+ """Get effective attribute: element's own value first, then inherited."""
+ val = elem.get(attr)
+ if val is not None:
+ return val
+ return ctx.inherited_styles.get(attr)
+
+
+def ctx_x(val: float, ctx: ConvertContext) -> float:
+ """Apply context scale + translate to an X coordinate."""
+ return val * ctx.scale_x + ctx.translate_x
+
+
+def ctx_y(val: float, ctx: ConvertContext) -> float:
+ """Apply context scale + translate to a Y coordinate."""
+ return val * ctx.scale_y + ctx.translate_y
+
+
+def ctx_w(val: float, ctx: ConvertContext) -> float:
+ """Apply context scale to a width value."""
+ return val * ctx.scale_x
+
+
+def ctx_h(val: float, ctx: ConvertContext) -> float:
+ """Apply context scale to a height value."""
+ return val * ctx.scale_y
+
+
+# ---------------------------------------------------------------------------
+# Color / style parsing
+# ---------------------------------------------------------------------------
+
+def parse_hex_color(color_str: str) -> str | None:
+ """Parse '#RRGGBB' or '#RGB' to 'RRGGBB'. Returns None on failure."""
+ if not color_str:
+ return None
+ color_str = color_str.strip()
+ if color_str.startswith('#'):
+ color_str = color_str[1:]
+ if len(color_str) == 3:
+ color_str = ''.join(c * 2 for c in color_str)
+ if len(color_str) == 6 and all(c in '0123456789abcdefABCDEF' for c in color_str):
+ return color_str.upper()
+ return None
+
+
+def parse_stop_style(style_str: str) -> tuple[str | None, float]:
+ """Parse a gradient stop's style attribute.
+
+ Args:
+ style_str: Style string like 'stop-color:#XXX;stop-opacity:N'.
+
+ Returns:
+ (color, opacity) tuple.
+ """
+ color = None
+ opacity = 1.0
+ if not style_str:
+ return color, opacity
+
+ for part in style_str.split(';'):
+ part = part.strip()
+ if part.startswith('stop-color:'):
+ color = parse_hex_color(part.split(':', 1)[1].strip())
+ elif part.startswith('stop-opacity:'):
+ try:
+ opacity = float(part.split(':', 1)[1].strip())
+ except ValueError:
+ pass
+
+ return color, opacity
+
+
+def resolve_url_id(url_str: str) -> str | None:
+ """Extract ID from 'url(#someId)' reference."""
+ if not url_str:
+ return None
+ m = re.match(r'url\(#([^)]+)\)', url_str.strip())
+ return m.group(1) if m else None
+
+
+def get_effective_filter_id(elem: ET.Element, ctx: ConvertContext) -> str | None:
+ """Get the effective filter ID for an element, including inherited context."""
+ filt = elem.get('filter')
+ if filt:
+ return resolve_url_id(filt)
+ return ctx.filter_id
+
+
+# ---------------------------------------------------------------------------
+# Font parsing
+# ---------------------------------------------------------------------------
+
+def parse_font_family(font_family_str: str) -> dict[str, str]:
+ """Parse CSS font-family into latin/ea typeface names.
+
+ Prioritizes Windows-available fonts since PPTX is primarily opened on
+ Windows. macOS/Linux-only fonts are mapped via FONT_FALLBACK_WIN.
+ """
+ if not font_family_str:
+ return {'latin': 'Segoe UI', 'ea': 'Microsoft YaHei'}
+
+ fonts = [f.strip().strip("'\"") for f in font_family_str.split(',')]
+ latin_font = None
+ ea_font = None
+
+ for font in fonts:
+ if font in SYSTEM_FONTS:
+ continue
+ if font in GENERIC_FONT_MAP:
+ resolved = GENERIC_FONT_MAP[font]
+ latin_font = latin_font or resolved
+ continue
+
+ win_font = FONT_FALLBACK_WIN.get(font, font)
+ if font in EA_FONTS:
+ ea_font = ea_font or win_font
+ else:
+ latin_font = latin_font or win_font
+
+ # PPT renders CJK text via latin typeface when ea doesn't match
+ if not latin_font and ea_font:
+ latin_font = ea_font
+
+ final_latin = latin_font or 'Segoe UI'
+
+ # EA must always be a CJK-capable font
+ if not ea_font:
+ ea_font = 'SimSun' if final_latin in _SERIF_LATIN else 'Microsoft YaHei'
+
+ return {'latin': final_latin, 'ea': ea_font}
+
+
+def is_cjk_char(ch: str) -> bool:
+ """Check if a character is CJK (Chinese/Japanese/Korean)."""
+ cp = ord(ch)
+ return (0x4E00 <= cp <= 0x9FFF or 0x3400 <= cp <= 0x4DBF or
+ 0x2E80 <= cp <= 0x2EFF or 0x3000 <= cp <= 0x303F or
+ 0xFF00 <= cp <= 0xFFEF or 0xF900 <= cp <= 0xFAFF or
+ 0x20000 <= cp <= 0x2A6DF)
+
+
+def estimate_text_width(text: str, font_size: float, font_weight: str = '400') -> float:
+ """Estimate text width in SVG pixels."""
+ width = 0.0
+ for ch in text:
+ if is_cjk_char(ch):
+ width += font_size
+ elif ch == ' ':
+ width += font_size * 0.3
+ elif ch in 'mMwWOQ':
+ width += font_size * 0.75
+ elif ch in 'iIlj1!|':
+ width += font_size * 0.3
+ else:
+ width += font_size * 0.55
+
+ if font_weight in ('bold', '600', '700', '800', '900'):
+ width *= 1.05
+
+ return width
+
+
+def _xml_escape(text: str) -> str:
+ """Escape XML special characters."""
+ return (text.replace('&', '&amp;')
+ .replace('<', '&lt;')
+ .replace('>', '&gt;')
+ .replace('"', '&quot;'))
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_builder.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_builder.py
new file mode 100644
index 00000000..5ade3d7c
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_builder.py
@@ -0,0 +1,366 @@
+"""Core PPTX assembly: create_pptx_with_native_svg."""
+
+from __future__ import annotations
+
+import shutil
+import tempfile
+import zipfile
+from pathlib import Path
+
+from pptx import Presentation
+from pptx.util import Emu
+
+from .drawingml_converter import convert_svg_to_slide_shapes
+from .pptx_dimensions import (
+ CANVAS_FORMATS,
+ get_slide_dimensions, get_pixel_dimensions,
+ get_viewbox_dimensions, detect_format_from_svg,
+)
+from .pptx_media import (
+ PNG_RENDERER,
+ get_png_renderer_info, convert_svg_to_png,
+)
+from .pptx_notes import (
+ markdown_to_plain_text,
+ create_notes_slide_xml, create_notes_slide_rels_xml,
+)
+from .pptx_slide_xml import (
+ ANIMATIONS_AVAILABLE, TRANSITIONS,
+ create_slide_xml_with_svg, create_slide_rels_xml,
+)
+
+# Re-import create_transition_xml only if available
+try:
+ from pptx_animations import create_transition_xml
+except ImportError:
+ create_transition_xml = None
+
+
+def create_pptx_with_native_svg(
+ svg_files: list[Path],
+ output_path: Path,
+ canvas_format: str | None = None,
+ verbose: bool = True,
+ transition: str | None = 'fade',
+ transition_duration: float = 0.5,
+ auto_advance: float | None = None,
+ use_compat_mode: bool = True,
+ notes: dict[str, str] | None = None,
+ enable_notes: bool = True,
+ use_native_shapes: bool = False,
+) -> bool:
+ """Create a PPTX file with native SVG.
+
+ Args:
+ svg_files: List of SVG files.
+ output_path: Output PPTX path.
+ canvas_format: Canvas format key.
+ verbose: Whether to output detailed information.
+ transition: Transition effect name.
+ transition_duration: Transition duration in seconds.
+ auto_advance: Auto-advance interval in seconds.
+ use_compat_mode: Use Office compatibility mode (PNG + SVG dual format).
+ notes: Notes dict, key is SVG stem, value is notes content.
+ enable_notes: Whether to enable notes embedding.
+ use_native_shapes: Convert SVG to native DrawingML shapes.
+
+ Returns:
+ Whether all slides were successfully created.
+ """
+ if not svg_files:
+ print("Error: No SVG files found")
+ return False
+
+ # Native shapes mode takes priority over compat mode
+ if use_native_shapes:
+ use_compat_mode = False
+
+ # Check compatibility mode dependencies
+ renderer_name, renderer_status, renderer_hint = get_png_renderer_info()
+ if not use_native_shapes and use_compat_mode and PNG_RENDERER is None:
+ print("Warning: No PNG rendering library installed, cannot use compatibility mode")
+ print(f" {renderer_hint}")
+ print(" Will use pure SVG mode (may not display in Office LTSC 2021 and similar versions)")
+ use_compat_mode = False
+
+ # Auto-detect canvas format or get dimensions from viewBox
+ custom_pixels: tuple[int, int] | None = None
+ if canvas_format is None:
+ canvas_format = detect_format_from_svg(svg_files[0])
+ if canvas_format and verbose:
+ format_name = CANVAS_FORMATS.get(canvas_format, {}).get('name', canvas_format)
+ print(f" Detected canvas format: {format_name}")
+
+ if canvas_format is None:
+ custom_pixels = get_viewbox_dimensions(svg_files[0])
+ if custom_pixels and verbose:
+ print(f" Using SVG viewBox dimensions: {custom_pixels[0]} x {custom_pixels[1]} px")
+
+ if canvas_format is None and custom_pixels is None:
+ canvas_format = 'ppt169'
+ if verbose:
+ print(f" Using default format: PPT 16:9")
+
+ width_emu, height_emu = get_slide_dimensions(canvas_format or 'ppt169', custom_pixels)
+ pixel_width, pixel_height = get_pixel_dimensions(canvas_format or 'ppt169', custom_pixels)
+
+ if verbose:
+ print(f" Slide dimensions: {pixel_width} x {pixel_height} px")
+ print(f" SVG file count: {len(svg_files)}")
+ if use_native_shapes:
+ print(f" Mode: Native DrawingML shapes (directly editable)")
+ elif use_compat_mode:
+ print(f" Compatibility mode: Enabled (PNG + SVG dual format)")
+ print(f" PNG renderer: {renderer_name} {renderer_status}")
+ else:
+ print(f" Compatibility mode: Disabled (pure SVG)")
+ if transition:
+ trans_name = TRANSITIONS.get(transition, {}).get('name', transition) if TRANSITIONS else transition
+ print(f" Transition effect: {trans_name}")
+ if enable_notes and notes:
+ print(f" Speaker notes: {len(notes)} page(s)")
+ elif enable_notes:
+ print(f" Speaker notes: Enabled (no notes files found)")
+ else:
+ print(f" Speaker notes: Disabled")
+ print()
+
+ temp_dir = Path(tempfile.mkdtemp())
+
+ try:
+ # Create base PPTX with python-pptx
+ prs = Presentation()
+ prs.slide_width = width_emu
+ prs.slide_height = height_emu
+
+ blank_layout = prs.slide_layouts[6]
+ for _ in svg_files:
+ prs.slides.add_slide(blank_layout)
+
+ base_pptx = temp_dir / 'base.pptx'
+ prs.save(str(base_pptx))
+
+ # Extract PPTX
+ extract_dir = temp_dir / 'pptx_content'
+ with zipfile.ZipFile(base_pptx, 'r') as zf:
+ zf.extractall(extract_dir)
+
+ media_dir = extract_dir / 'ppt' / 'media'
+ media_dir.mkdir(exist_ok=True)
+
+ success_count = 0
+ has_any_image = False
+
+ for i, svg_path in enumerate(svg_files, 1):
+ slide_num = i
+
+ try:
+ # ---- Native shapes mode ----
+ if use_native_shapes:
+ slide_xml, media_files_dict, rel_entries = convert_svg_to_slide_shapes(
+ svg_path, slide_num=slide_num, verbose=verbose,
+ )
+
+ # Add transition if specified
+ if transition and ANIMATIONS_AVAILABLE and create_transition_xml:
+ transition_xml = '\n' + create_transition_xml(
+ effect=transition,
+ duration=transition_duration,
+ advance_after=auto_advance,
+ )
+ slide_xml = slide_xml.replace(
+ '</p:sld>',
+ transition_xml + '\n</p:sld>',
+ )
+
+ # Write slide XML
+ slide_xml_path = extract_dir / 'ppt' / 'slides' / f'slide{slide_num}.xml'
+ with open(slide_xml_path, 'w', encoding='utf-8') as f:
+ f.write(slide_xml)
+
+ # Write media files
+ for media_name, media_data in media_files_dict.items():
+ with open(media_dir / media_name, 'wb') as f:
+ f.write(media_data)
+
+ # Build relationships XML
+ rels_dir = extract_dir / 'ppt' / 'slides' / '_rels'
+ rels_dir.mkdir(exist_ok=True)
+ rels_path = rels_dir / f'slide{slide_num}.xml.rels'
+
+ extra_rels = ''
+ for rel in rel_entries:
+ extra_rels += (
+ f'\n <Relationship Id="{rel["id"]}" '
+ f'Type="{rel["type"]}" Target="{rel["target"]}"/>'
+ )
+
+ rels_xml = f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>{extra_rels}
+</Relationships>'''
+ with open(rels_path, 'w', encoding='utf-8') as f:
+ f.write(rels_xml)
+
+ # Track image formats for Content_Types
+ for media_name in media_files_dict:
+ ext = media_name.rsplit('.', 1)[-1].lower()
+ if ext in ('png', 'jpg', 'jpeg', 'gif', 'webp'):
+ has_any_image = True
+
+ # ---- Legacy SVG embedding mode ----
+ else:
+ svg_filename = f'image{i}.svg'
+ png_filename = f'image{i}.png'
+ png_rid = 'rId2'
+ svg_rid = 'rId3' if use_compat_mode else 'rId2'
+
+ shutil.copy(svg_path, media_dir / svg_filename)
+
+ slide_has_png = False
+ if use_compat_mode:
+ png_path = media_dir / png_filename
+ png_success = convert_svg_to_png(
+ svg_path, png_path,
+ width=pixel_width, height=pixel_height,
+ )
+ if png_success:
+ slide_has_png = True
+ has_any_image = True
+ else:
+ if verbose:
+ print(f" [{i}/{len(svg_files)}] {svg_path.name} - PNG generation failed, using pure SVG")
+ svg_rid = 'rId2'
+
+ slide_xml_path = extract_dir / 'ppt' / 'slides' / f'slide{slide_num}.xml'
+ slide_xml = create_slide_xml_with_svg(
+ slide_num,
+ png_rid=png_rid, svg_rid=svg_rid,
+ width_emu=width_emu, height_emu=height_emu,
+ transition=transition,
+ transition_duration=transition_duration,
+ auto_advance=auto_advance,
+ use_compat_mode=(use_compat_mode and slide_has_png),
+ )
+ with open(slide_xml_path, 'w', encoding='utf-8') as f:
+ f.write(slide_xml)
+
+ rels_dir = extract_dir / 'ppt' / 'slides' / '_rels'
+ rels_dir.mkdir(exist_ok=True)
+ rels_path = rels_dir / f'slide{slide_num}.xml.rels'
+ rels_xml = create_slide_rels_xml(
+ png_rid=png_rid, png_filename=png_filename,
+ svg_rid=svg_rid, svg_filename=svg_filename,
+ use_compat_mode=(use_compat_mode and slide_has_png),
+ )
+ with open(rels_path, 'w', encoding='utf-8') as f:
+ f.write(rels_xml)
+
+ # --- Process notes (shared between native and legacy mode) ---
+ notes_content = ''
+ if enable_notes:
+ svg_stem = svg_path.stem
+ notes_content = notes.get(svg_stem, '') if notes else ''
+ notes_text = markdown_to_plain_text(notes_content) if notes_content else ''
+
+ notes_slides_dir = extract_dir / 'ppt' / 'notesSlides'
+ notes_slides_dir.mkdir(exist_ok=True)
+
+ notes_xml_path = notes_slides_dir / f'notesSlide{slide_num}.xml'
+ notes_xml = create_notes_slide_xml(slide_num, notes_text)
+ with open(notes_xml_path, 'w', encoding='utf-8') as f:
+ f.write(notes_xml)
+
+ notes_rels_dir = notes_slides_dir / '_rels'
+ notes_rels_dir.mkdir(exist_ok=True)
+ notes_rels_path = notes_rels_dir / f'notesSlide{slide_num}.xml.rels'
+ notes_rels_xml = create_notes_slide_rels_xml(slide_num)
+ with open(notes_rels_path, 'w', encoding='utf-8') as f:
+ f.write(notes_rels_xml)
+
+ # Update slide.xml.rels to add notes association
+ with open(rels_path, 'r', encoding='utf-8') as f:
+ slide_rels_content = f.read()
+ notes_rel = (
+ f' <Relationship Id="rId10" '
+ f'Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide" '
+ f'Target="../notesSlides/notesSlide{slide_num}.xml"/>'
+ )
+ slide_rels_content = slide_rels_content.replace(
+ '</Relationships>', notes_rel + '\n</Relationships>',
+ )
+ with open(rels_path, 'w', encoding='utf-8') as f:
+ f.write(slide_rels_content)
+
+ if verbose:
+ if use_native_shapes:
+ mode_str = " (Native)"
+ elif use_compat_mode and not use_native_shapes:
+ mode_str = " (PNG+SVG)" if has_any_image else " (SVG)"
+ else:
+ mode_str = " (SVG)"
+ has_notes = enable_notes and bool(notes_content)
+ notes_str = " +notes" if has_notes else ""
+ print(f" [{i}/{len(svg_files)}] {svg_path.name}{mode_str}{notes_str}")
+
+ success_count += 1
+
+ except Exception as e:
+ if verbose:
+ print(f" [{i}/{len(svg_files)}] {svg_path.name} - Error: {e}")
+
+ # Update [Content_Types].xml
+ content_types_path = extract_dir / '[Content_Types].xml'
+ with open(content_types_path, 'r', encoding='utf-8') as f:
+ content_types = f.read()
+
+ types_to_add: list[str] = []
+ if not use_native_shapes:
+ if 'Extension="svg"' not in content_types:
+ types_to_add.append(' <Default Extension="svg" ContentType="image/svg+xml"/>')
+ if has_any_image and 'Extension="png"' not in content_types:
+ types_to_add.append(' <Default Extension="png" ContentType="image/png"/>')
+ if use_native_shapes and 'Extension="jpg"' not in content_types:
+ types_to_add.append(' <Default Extension="jpg" ContentType="image/jpeg"/>')
+ if use_native_shapes and 'Extension="jpeg"' not in content_types:
+ types_to_add.append(' <Default Extension="jpeg" ContentType="image/jpeg"/>')
+
+ if types_to_add:
+ content_types = content_types.replace(
+ '</Types>', '\n'.join(types_to_add) + '\n</Types>',
+ )
+ with open(content_types_path, 'w', encoding='utf-8') as f:
+ f.write(content_types)
+
+ # Add notesSlides content types
+ if enable_notes:
+ for i in range(1, len(svg_files) + 1):
+ override = (
+ f' <Override PartName="/ppt/notesSlides/notesSlide{i}.xml" '
+ f'ContentType="application/vnd.openxmlformats-officedocument.presentationml.notesSlide+xml"/>'
+ )
+ if override not in content_types:
+ content_types = content_types.replace('</Types>', override + '\n</Types>')
+ with open(content_types_path, 'w', encoding='utf-8') as f:
+ f.write(content_types)
+
+ # Repackage PPTX
+ with zipfile.ZipFile(output_path, 'w', zipfile.ZIP_DEFLATED) as zf:
+ for file_path in extract_dir.rglob('*'):
+ if file_path.is_file():
+ arcname = file_path.relative_to(extract_dir)
+ zf.write(file_path, arcname)
+
+ if verbose:
+ print()
+ print(f"[Done] Saved: {output_path}")
+ print(f" Succeeded: {success_count}, Failed: {len(svg_files) - success_count}")
+ if use_compat_mode and has_any_image:
+ print(f" Mode: Office compatibility mode (supports all Office versions)")
+ if PNG_RENDERER == 'svglib' and renderer_hint:
+ print(f" [Tip] {renderer_hint}")
+
+ return success_count == len(svg_files)
+
+ finally:
+ shutil.rmtree(temp_dir, ignore_errors=True)
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_cli.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_cli.py
new file mode 100644
index 00000000..41bb9aa0
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_cli.py
@@ -0,0 +1,196 @@
+"""CLI entry point for svg_to_pptx."""
+
+from __future__ import annotations
+
+import sys
+import argparse
+from datetime import datetime
+from pathlib import Path
+
+from .pptx_dimensions import CANVAS_FORMATS, get_project_info
+from .pptx_discovery import find_svg_files, find_notes_files
+from .pptx_builder import create_pptx_with_native_svg
+from .pptx_slide_xml import TRANSITIONS
+
+
+def main() -> None:
+ """CLI entry point for the SVG to PPTX conversion tool."""
+ transition_choices = (
+ ['none'] + (list(TRANSITIONS.keys()) if TRANSITIONS
+ else ['fade', 'push', 'wipe', 'split', 'strips', 'cover', 'random'])
+ )
+
+ parser = argparse.ArgumentParser(
+ description='PPT Master - SVG to PPTX Tool (Office Compatibility Mode)',
+ formatter_class=argparse.RawDescriptionHelpFormatter,
+ epilog=f'''
+Examples:
+ %(prog)s examples/ppt169_demo -s final # Default: native + SVG reference (two files)
+ %(prog)s examples/ppt169_demo --only native # Only native shapes version
+ %(prog)s examples/ppt169_demo --only legacy # Only SVG image version
+ %(prog)s examples/ppt169_demo -o out.pptx # Explicit path (SVG ref -> out_svg.pptx)
+
+ # Disable transition / change transition effect
+ %(prog)s examples/ppt169_demo -t none
+ %(prog)s examples/ppt169_demo -t push --transition-duration 1.0
+
+SVG source directory (-s):
+ output - svg_output (original version)
+ final - svg_final (post-processed, recommended)
+ <any> - Specify a subdirectory name directly
+
+Transition effects (-t/--transition):
+ {', '.join(transition_choices)}
+
+Compatibility mode (enabled by default):
+ - Automatically generates PNG fallback images, SVG embedded as extension
+ - Compatible with all Office versions (including Office LTSC 2021)
+ - Newer Office still displays SVG (editable), older versions display PNG
+ - Requires svglib: pip install svglib reportlab
+ - Use --no-compat to disable (only Office 2019+ supported)
+
+Speaker notes (enabled by default):
+ - Automatically reads Markdown notes files from the notes/ directory
+ - Supports two naming conventions:
+ 1. Match by filename (recommended): 01_cover.md corresponds to 01_cover.svg
+ 2. Match by index: slide01.md corresponds to the 1st SVG (backward compatible)
+ - Use --no-notes to disable
+''',
+ )
+
+ parser.add_argument('project_path', type=str, help='Project directory path')
+ parser.add_argument('-o', '--output', type=str, default=None, help='Output file path')
+ parser.add_argument('-s', '--source', type=str, default='output',
+ help='SVG source: output/final or any subdirectory name (recommended: final)')
+ parser.add_argument('-f', '--format', type=str,
+ choices=list(CANVAS_FORMATS.keys()), default=None,
+ help='Specify canvas format')
+ parser.add_argument('-q', '--quiet', action='store_true', help='Quiet mode')
+
+ parser.add_argument('--no-compat', action='store_true',
+ help='Disable Office compatibility mode (pure SVG only, requires Office 2019+)')
+
+ mode_group = parser.add_mutually_exclusive_group()
+ mode_group.add_argument('--only', type=str, choices=['native', 'legacy'], default=None,
+ help='Only generate one version: native (editable shapes) or legacy (SVG image)')
+ mode_group.add_argument('--native', action='store_true', default=False,
+ help='(Deprecated, now default) Convert SVG to native DrawingML shapes')
+
+ parser.add_argument('-t', '--transition', type=str, choices=transition_choices, default='fade',
+ help='Page transition effect (default: fade, use "none" to disable)')
+ parser.add_argument('--transition-duration', type=float, default=0.4,
+ help='Transition duration in seconds (default: 0.5)')
+ parser.add_argument('--auto-advance', type=float, default=None,
+ help='Auto-advance interval in seconds (default: manual advance)')
+
+ parser.add_argument('--no-notes', action='store_true',
+ help='Disable speaker notes embedding (enabled by default)')
+
+ args = parser.parse_args()
+
+ project_path = Path(args.project_path)
+ if not project_path.exists():
+ print(f"Error: Path does not exist: {project_path}")
+ sys.exit(1)
+
+ try:
+ project_info = get_project_info(str(project_path))
+ project_name = project_info.get('name', project_path.name)
+ detected_format = project_info.get('format')
+ except Exception:
+ project_name = project_path.name
+ detected_format = None
+
+ canvas_format = args.format
+ if canvas_format is None and detected_format and detected_format != 'unknown':
+ canvas_format = detected_format
+
+ svg_files, source_dir_name = find_svg_files(project_path, args.source)
+
+ if not svg_files:
+ print("Error: No SVG files found")
+ sys.exit(1)
+
+ # Determine which versions to generate
+ only_mode = args.only
+ gen_native = only_mode in (None, 'native')
+ gen_legacy = only_mode in (None, 'legacy')
+
+ # --native flag (deprecated) maps to --only native
+ if args.native and only_mode is None:
+ gen_legacy = False
+
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
+
+ if args.output:
+ output_base = Path(args.output)
+ native_path = output_base
+ stem = output_base.stem
+ legacy_path = output_base.parent / f"{stem}_svg{output_base.suffix}"
+ else:
+ native_path = project_path / f"{project_name}_{timestamp}.pptx"
+ legacy_path = project_path / f"{project_name}_{timestamp}_svg.pptx"
+
+ native_path.parent.mkdir(parents=True, exist_ok=True)
+
+ verbose = not args.quiet
+
+ enable_notes = not args.no_notes
+ notes: dict[str, str] = {}
+ if enable_notes:
+ notes = find_notes_files(project_path, svg_files)
+
+ transition = args.transition if args.transition != 'none' else None
+
+ shared_kwargs = dict(
+ svg_files=svg_files,
+ canvas_format=canvas_format,
+ verbose=verbose,
+ transition=transition,
+ transition_duration=args.transition_duration,
+ auto_advance=args.auto_advance,
+ use_compat_mode=not args.no_compat,
+ notes=notes,
+ enable_notes=enable_notes,
+ )
+
+ success = True
+
+ # --- Native shapes version (primary) ---
+ if gen_native:
+ if verbose:
+ print("PPT Master - SVG to PPTX Tool")
+ print("=" * 50)
+ print(f" Project path: {project_path}")
+ print(f" SVG directory: {source_dir_name}")
+ print(f" Output file: {native_path}")
+ print()
+
+ ok = create_pptx_with_native_svg(
+ output_path=native_path,
+ use_native_shapes=True,
+ **shared_kwargs,
+ )
+ success = success and ok
+
+ # --- SVG image reference version ---
+ if gen_legacy:
+ if verbose:
+ if gen_native:
+ print()
+ print("-" * 50)
+ print("PPT Master - SVG to PPTX Tool (SVG Reference)")
+ print("=" * 50)
+ print(f" Project path: {project_path}")
+ print(f" SVG directory: {source_dir_name}")
+ print(f" Output file: {legacy_path}")
+ print()
+
+ ok = create_pptx_with_native_svg(
+ output_path=legacy_path,
+ use_native_shapes=False,
+ **shared_kwargs,
+ )
+ success = success and ok
+
+ sys.exit(0 if success else 1)
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_dimensions.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_dimensions.py
new file mode 100644
index 00000000..75f9ac18
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_dimensions.py
@@ -0,0 +1,148 @@
+"""Slide dimensions, format detection, EMU conversion, and constants."""
+
+from __future__ import annotations
+
+import re
+import sys
+from pathlib import Path
+from xml.etree import ElementTree as ET
+
+# Import project utility modules
+sys.path.insert(0, str(Path(__file__).resolve().parent.parent))
+try:
+ from project_utils import get_project_info
+ from config import CANVAS_FORMATS
+except ImportError:
+ CANVAS_FORMATS = {
+ 'ppt169': {'name': 'PPT 16:9', 'dimensions': '1280×720', 'viewbox': '0 0 1280 720'},
+ }
+
+ def get_project_info(path: str) -> dict:
+ return {'format': 'unknown', 'name': Path(path).name}
+
+# EMU conversion constants
+EMU_PER_INCH = 914400
+EMU_PER_PIXEL = EMU_PER_INCH / 96
+
+# XML namespaces
+NAMESPACES = {
+ 'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
+ 'r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships',
+ 'p': 'http://schemas.openxmlformats.org/presentationml/2006/main',
+ 'asvg': 'http://schemas.microsoft.com/office/drawing/2016/SVG/main',
+}
+
+# Register namespaces for ElementTree output
+for prefix, uri in NAMESPACES.items():
+ ET.register_namespace(prefix, uri)
+
+
+def get_slide_dimensions(
+ canvas_format: str,
+ custom_pixels: tuple[int, int] | None = None,
+) -> tuple[int, int]:
+ """Get slide dimensions in EMU units.
+
+ Args:
+ canvas_format: Canvas format key (e.g. 'ppt169').
+ custom_pixels: Optional custom pixel dimensions override.
+
+ Returns:
+ (width_emu, height_emu) tuple.
+ """
+ if custom_pixels:
+ width_px, height_px = custom_pixels
+ else:
+ if canvas_format not in CANVAS_FORMATS:
+ canvas_format = 'ppt169'
+
+ dimensions = CANVAS_FORMATS[canvas_format]['dimensions']
+ match = re.match(r'(\d+)[×x](\d+)', dimensions)
+ if match:
+ width_px = int(match.group(1))
+ height_px = int(match.group(2))
+ else:
+ width_px, height_px = 1280, 720
+
+ return int(width_px * EMU_PER_PIXEL), int(height_px * EMU_PER_PIXEL)
+
+
+def get_pixel_dimensions(
+ canvas_format: str,
+ custom_pixels: tuple[int, int] | None = None,
+) -> tuple[int, int]:
+ """Get canvas pixel dimensions.
+
+ Args:
+ canvas_format: Canvas format key.
+ custom_pixels: Optional custom pixel dimensions override.
+
+ Returns:
+ (width_px, height_px) tuple.
+ """
+ if custom_pixels:
+ return custom_pixels
+
+ if canvas_format not in CANVAS_FORMATS:
+ canvas_format = 'ppt169'
+
+ dimensions = CANVAS_FORMATS[canvas_format]['dimensions']
+ match = re.match(r'(\d+)[×x](\d+)', dimensions)
+ if match:
+ return int(match.group(1)), int(match.group(2))
+ return 1280, 720
+
+
+def get_viewbox_dimensions(svg_path: Path) -> tuple[int, int] | None:
+ """Extract pixel dimensions from SVG viewBox.
+
+ Args:
+ svg_path: Path to the SVG file.
+
+ Returns:
+ (width, height) as integers, or None if not found.
+ """
+ try:
+ with open(svg_path, 'r', encoding='utf-8') as f:
+ content = f.read(2000)
+
+ match = re.search(r'viewBox="([^"]+)"', content)
+ if not match:
+ return None
+
+ parts = re.split(r'[\s,]+', match.group(1).strip())
+ if len(parts) < 4:
+ return None
+
+ width = float(parts[2])
+ height = float(parts[3])
+ if width <= 0 or height <= 0:
+ return None
+
+ return int(round(width)), int(round(height))
+ except Exception:
+ return None
+
+
+def detect_format_from_svg(svg_path: Path) -> str | None:
+ """Detect canvas format from an SVG file's viewBox.
+
+ Args:
+ svg_path: Path to the SVG file.
+
+ Returns:
+ Canvas format key (e.g. 'ppt169'), or None if not detected.
+ """
+ try:
+ with open(svg_path, 'r', encoding='utf-8') as f:
+ content = f.read(2000)
+
+ match = re.search(r'viewBox="([^"]+)"', content)
+ if match:
+ viewbox = match.group(1)
+ for fmt_key, fmt_info in CANVAS_FORMATS.items():
+ if fmt_info['viewbox'] == viewbox:
+ return fmt_key
+ except Exception:
+ pass
+ return None
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_discovery.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_discovery.py
new file mode 100644
index 00000000..abd4f5c0
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_discovery.py
@@ -0,0 +1,101 @@
+"""Find SVG and notes files in a project directory."""
+
+from __future__ import annotations
+
+import re
+from pathlib import Path
+
+
+def find_svg_files(
+ project_path: Path,
+ source: str = 'output',
+) -> tuple[list[Path], str]:
+ """Find SVG files in the project.
+
+ Args:
+ project_path: Project directory path.
+ source: SVG source directory alias or name.
+ - 'output': svg_output (original version)
+ - 'final': svg_final (post-processed, recommended)
+ - or any subdirectory name
+
+ Returns:
+ (list_of_svg_files, actual_directory_name) tuple.
+ """
+ dir_map = {
+ 'output': 'svg_output',
+ 'final': 'svg_final',
+ }
+
+ dir_name = dir_map.get(source, source)
+ svg_dir = project_path / dir_name
+
+ if not svg_dir.exists():
+ print(f" Warning: {dir_name} directory does not exist, trying svg_output")
+ dir_name = 'svg_output'
+ svg_dir = project_path / dir_name
+
+ if not svg_dir.exists():
+ if project_path.is_dir():
+ svg_dir = project_path
+ dir_name = project_path.name
+ else:
+ return [], ''
+
+ return sorted(svg_dir.glob('*.svg')), dir_name
+
+
+def find_notes_files(
+ project_path: Path,
+ svg_files: list[Path] | None = None,
+) -> dict[str, str]:
+ """Find notes files and map them to SVG files.
+
+ Supports two matching modes (mixed matching supported):
+ 1. Match by filename (priority): notes/01_cover.md -> 01_cover.svg
+ 2. Match by index (backward compatible): notes/slide01.md -> 1st SVG
+
+ Args:
+ project_path: Project directory path.
+ svg_files: SVG file list (for filename matching).
+
+ Returns:
+ Dict mapping SVG filename stem to notes content.
+ """
+ notes_dir = project_path / 'notes'
+ notes: dict[str, str] = {}
+
+ if not notes_dir.exists():
+ return notes
+
+ svg_stems_mapping: dict[str, int] = {}
+ svg_index_mapping: dict[int, str] = {}
+ if svg_files:
+ for i, svg_path in enumerate(svg_files, 1):
+ svg_stems_mapping[svg_path.stem] = i
+ svg_index_mapping[i] = svg_path.stem
+
+ for notes_file in notes_dir.glob('*.md'):
+ try:
+ with open(notes_file, 'r', encoding='utf-8') as f:
+ content = f.read().strip()
+ if not content:
+ continue
+
+ stem = notes_file.stem
+
+ # Try index-based matching (backward compat with slide01.md format)
+ match = re.search(r'slide[_]?(\d+)', stem)
+ if match:
+ index = int(match.group(1))
+ mapped_stem = svg_index_mapping.get(index)
+ if mapped_stem:
+ notes[mapped_stem] = content
+
+ # Filename-based matching (overrides index-based)
+ if stem in svg_stems_mapping:
+ notes[stem] = content
+ except Exception:
+ pass
+
+ return notes
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_media.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_media.py
new file mode 100644
index 00000000..e016763c
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_media.py
@@ -0,0 +1,86 @@
+"""SVG to PNG conversion for Office compatibility mode."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+# SVG to PNG library detection
+# Prefer CairoSVG (better quality), fall back to svglib
+PNG_RENDERER: str | None = None
+
+try:
+ import cairosvg
+ PNG_RENDERER = 'cairosvg'
+except (ImportError, OSError):
+ try:
+ from svglib.svglib import svg2rlg
+ from reportlab.graphics import renderPM
+ PNG_RENDERER = 'svglib'
+ except (ImportError, OSError):
+ pass
+
+
+def get_png_renderer_info() -> tuple[str | None, str, str | None]:
+ """Get PNG renderer status information.
+
+ Returns:
+ (renderer_name, status_text, install_hint) tuple.
+ """
+ if PNG_RENDERER == 'cairosvg':
+ return ('cairosvg', '(full gradient/filter support)', None)
+ elif PNG_RENDERER == 'svglib':
+ return ('svglib', '(some gradients may be lost)',
+ 'Install cairosvg for better results: pip install cairosvg')
+ else:
+ return (None, '(not installed)',
+ 'Install via: pip install cairosvg or pip install svglib reportlab')
+
+
+def convert_svg_to_png(
+ svg_path: Path,
+ png_path: Path,
+ width: int | None = None,
+ height: int | None = None,
+) -> bool:
+ """Convert SVG to PNG using the available renderer.
+
+ Args:
+ svg_path: SVG file path.
+ png_path: Output PNG file path.
+ width: Output width in pixels.
+ height: Output height in pixels.
+
+ Returns:
+ Whether the conversion was successful.
+ """
+ if PNG_RENDERER is None:
+ return False
+
+ try:
+ if PNG_RENDERER == 'cairosvg':
+ cairosvg.svg2png(
+ url=str(svg_path),
+ write_to=str(png_path),
+ output_width=width,
+ output_height=height,
+ )
+ return True
+
+ elif PNG_RENDERER == 'svglib':
+ drawing = svg2rlg(str(svg_path))
+ if drawing is None:
+ print(f" Warning: Unable to parse SVG ({svg_path.name})")
+ return False
+ renderPM.drawToFile(
+ drawing,
+ str(png_path),
+ fmt="PNG",
+ configPIL={'quality': 95},
+ )
+ return True
+
+ except Exception as e:
+ print(f" Warning: SVG to PNG conversion failed ({svg_path.name}): {e}")
+ return False
+
+ return False
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_notes.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_notes.py
new file mode 100644
index 00000000..563ae84b
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_notes.py
@@ -0,0 +1,157 @@
+"""Markdown to plain text conversion and notes slide XML generation."""
+
+from __future__ import annotations
+
+import re
+
+
+def markdown_to_plain_text(md_content: str) -> str:
+ """Convert Markdown notes to plain text for PPTX notes.
+
+ Args:
+ md_content: Markdown formatted notes content.
+
+ Returns:
+ Plain text content.
+ """
+ def strip_inline_bold(text: str) -> str:
+ text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
+ text = re.sub(r'__(.+?)__', r'\1', text)
+ return text
+
+ lines: list[str] = []
+ for line in md_content.split('\n'):
+ if line.startswith('#'):
+ text = re.sub(r'^#+\s*', '', line).strip()
+ text = strip_inline_bold(text)
+ if text:
+ lines.append(text)
+ lines.append('')
+ elif line.strip().startswith('- '):
+ item_text = line.strip()[2:]
+ item_text = strip_inline_bold(item_text)
+ lines.append('• ' + item_text)
+ elif line.strip():
+ text = strip_inline_bold(line.strip())
+ lines.append(text)
+ else:
+ lines.append('')
+
+ # Merge consecutive empty lines
+ result: list[str] = []
+ is_prev_empty = False
+ for line in lines:
+ if line == '':
+ if not is_prev_empty:
+ result.append(line)
+ is_prev_empty = True
+ else:
+ result.append(line)
+ is_prev_empty = False
+
+ return '\n'.join(result).strip()
+
+
+def create_notes_slide_xml(slide_num: int, notes_text: str) -> str:
+ """Create notes slide XML.
+
+ Args:
+ slide_num: Slide number.
+ notes_text: Notes text in plain text format.
+
+ Returns:
+ Notes slide XML string.
+ """
+ notes_text = (notes_text
+ .replace('&', '&amp;')
+ .replace('<', '&lt;')
+ .replace('>', '&gt;'))
+
+ paragraphs: list[str] = []
+ for para in notes_text.split('\n'):
+ if para.strip():
+ paragraphs.append(f'''<a:p>
+ <a:r>
+ <a:rPr lang="zh-CN" dirty="0"/>
+ <a:t>{para}</a:t>
+ </a:r>
+ </a:p>''')
+ else:
+ paragraphs.append('<a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p>')
+
+ paragraphs_xml = (
+ '\n '.join(paragraphs)
+ if paragraphs
+ else '<a:p><a:endParaRPr lang="zh-CN" dirty="0"/></a:p>'
+ )
+
+ return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<p:notes xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
+ xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
+ <p:cSld>
+ <p:spTree>
+ <p:nvGrpSpPr>
+ <p:cNvPr id="1" name=""/>
+ <p:cNvGrpSpPr/>
+ <p:nvPr/>
+ </p:nvGrpSpPr>
+ <p:grpSpPr>
+ <a:xfrm>
+ <a:off x="0" y="0"/>
+ <a:ext cx="0" cy="0"/>
+ <a:chOff x="0" y="0"/>
+ <a:chExt cx="0" cy="0"/>
+ </a:xfrm>
+ </p:grpSpPr>
+ <p:sp>
+ <p:nvSpPr>
+ <p:cNvPr id="2" name="Slide Image Placeholder 1"/>
+ <p:cNvSpPr>
+ <a:spLocks noGrp="1" noRot="1" noChangeAspect="1"/>
+ </p:cNvSpPr>
+ <p:nvPr>
+ <p:ph type="sldImg"/>
+ </p:nvPr>
+ </p:nvSpPr>
+ <p:spPr/>
+ </p:sp>
+ <p:sp>
+ <p:nvSpPr>
+ <p:cNvPr id="3" name="Notes Placeholder 2"/>
+ <p:cNvSpPr>
+ <a:spLocks noGrp="1"/>
+ </p:cNvSpPr>
+ <p:nvPr>
+ <p:ph type="body" idx="1"/>
+ </p:nvPr>
+ </p:nvSpPr>
+ <p:spPr/>
+ <p:txBody>
+ <a:bodyPr/>
+ <a:lstStyle/>
+ {paragraphs_xml}
+ </p:txBody>
+ </p:sp>
+ </p:spTree>
+ </p:cSld>
+ <p:clrMapOvr>
+ <a:masterClrMapping/>
+ </p:clrMapOvr>
+</p:notes>'''
+
+
+def create_notes_slide_rels_xml(slide_num: int) -> str:
+ """Create notes slide relationship file XML.
+
+ Args:
+ slide_num: Slide number.
+
+ Returns:
+ Relationship file XML string.
+ """
+ return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesMaster" Target="../notesMasters/notesMaster1.xml"/>
+ <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="../slides/slide{slide_num}.xml"/>
+</Relationships>'''
diff --git a/skills/ppt-master/scripts/svg_to_pptx/pptx_slide_xml.py b/skills/ppt-master/scripts/svg_to_pptx/pptx_slide_xml.py
new file mode 100644
index 00000000..da326225
--- /dev/null
+++ b/skills/ppt-master/scripts/svg_to_pptx/pptx_slide_xml.py
@@ -0,0 +1,136 @@
+"""Slide XML and slide relationship XML generation."""
+
+from __future__ import annotations
+
+# Import animation module (optional)
+try:
+ from pptx_animations import create_transition_xml, TRANSITIONS
+ ANIMATIONS_AVAILABLE = True
+except ImportError:
+ ANIMATIONS_AVAILABLE = False
+ TRANSITIONS = {}
+
+
+def create_slide_xml_with_svg(
+ slide_num: int,
+ png_rid: str,
+ svg_rid: str,
+ width_emu: int,
+ height_emu: int,
+ transition: str | None = 'fade',
+ transition_duration: float = 0.5,
+ auto_advance: float | None = None,
+ use_compat_mode: bool = True,
+) -> str:
+ """Create slide XML containing an SVG image.
+
+ Args:
+ slide_num: Slide number.
+ png_rid: PNG fallback image relationship ID.
+ svg_rid: SVG relationship ID.
+ width_emu: Width in EMU.
+ height_emu: Height in EMU.
+ transition: Transition effect name.
+ transition_duration: Transition duration in seconds.
+ auto_advance: Auto-advance interval in seconds.
+ use_compat_mode: Whether to use compatibility mode (PNG + SVG dual format).
+ """
+ transition_xml = ''
+ if transition and ANIMATIONS_AVAILABLE:
+ transition_xml = '\n' + create_transition_xml(
+ effect=transition,
+ duration=transition_duration,
+ advance_after=auto_advance,
+ )
+
+ if use_compat_mode:
+ blip_xml = f'''<a:blip r:embed="{png_rid}">
+ <a:extLst>
+ <a:ext uri="{{96DAC541-7B7A-43D3-8B79-37D633B846F1}}">
+ <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="{svg_rid}"/>
+ </a:ext>
+ </a:extLst>
+ </a:blip>'''
+ else:
+ blip_xml = f'<a:blip r:embed="{svg_rid}"/>'
+
+ return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
+ xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
+ xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
+ <p:cSld>
+ <p:spTree>
+ <p:nvGrpSpPr>
+ <p:cNvPr id="1" name=""/>
+ <p:cNvGrpSpPr/>
+ <p:nvPr/>
+ </p:nvGrpSpPr>
+ <p:grpSpPr>
+ <a:xfrm>
+ <a:off x="0" y="0"/>
+ <a:ext cx="0" cy="0"/>
+ <a:chOff x="0" y="0"/>
+ <a:chExt cx="0" cy="0"/>
+ </a:xfrm>
+ </p:grpSpPr>
+ <p:pic>
+ <p:nvPicPr>
+ <p:cNvPr id="2" name="SVG Image {slide_num}"/>
+ <p:cNvPicPr>
+ <a:picLocks noChangeAspect="1"/>
+ </p:cNvPicPr>
+ <p:nvPr/>
+ </p:nvPicPr>
+ <p:blipFill>
+ {blip_xml}
+ <a:stretch>
+ <a:fillRect/>
+ </a:stretch>
+ </p:blipFill>
+ <p:spPr>
+ <a:xfrm>
+ <a:off x="0" y="0"/>
+ <a:ext cx="{width_emu}" cy="{height_emu}"/>
+ </a:xfrm>
+ <a:prstGeom prst="rect">
+ <a:avLst/>
+ </a:prstGeom>
+ </p:spPr>
+ </p:pic>
+ </p:spTree>
+ </p:cSld>
+ <p:clrMapOvr>
+ <a:masterClrMapping/>
+ </p:clrMapOvr>{transition_xml}
+</p:sld>'''
+
+
+def create_slide_rels_xml(
+ png_rid: str,
+ png_filename: str,
+ svg_rid: str,
+ svg_filename: str,
+ use_compat_mode: bool = True,
+) -> str:
+ """Create slide relationship file XML.
+
+ Args:
+ png_rid: PNG image relationship ID.
+ png_filename: PNG filename.
+ svg_rid: SVG relationship ID.
+ svg_filename: SVG filename.
+ use_compat_mode: Whether to use compatibility mode.
+ """
+ if use_compat_mode:
+ return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>
+ <Relationship Id="{png_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{png_filename}"/>
+ <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/>
+</Relationships>'''
+ else:
+ return f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout" Target="../slideLayouts/slideLayout1.xml"/>
+ <Relationship Id="{svg_rid}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="../media/{svg_filename}"/>
+</Relationships>'''
diff --git a/skills/ppt-master/scripts/svg_to_shapes.py b/skills/ppt-master/scripts/svg_to_shapes.py
deleted file mode 100644
index ba63b3e2..00000000
--- a/skills/ppt-master/scripts/svg_to_shapes.py
+++ /dev/null
@@ -1,2117 +0,0 @@
-#!/usr/bin/env python3
-"""
-PPT Master - SVG to DrawingML Native Shapes Converter
-
-Converts SVG elements into native PowerPoint DrawingML shapes,
-so the resulting PPTX is directly editable without manual "Convert to Shape".
-
-This module handles the SVG subset used by PPT Master (after finalize_svg.py processing):
-- rect, circle, line, path, polygon, polyline, text, g, image
-- linearGradient, radialGradient (in defs)
-- filter (shadow effects via feGaussianBlur + feOffset)
-- transform (translate, scale, rotate)
-
-Usage:
- from svg_to_shapes import convert_svg_to_slide_shapes
- slide_xml, media_files, rel_entries = convert_svg_to_slide_shapes(svg_path, slide_num=1)
-"""
-
-import math
-import re
-import base64
-from pathlib import Path
-from typing import Optional, Tuple, List, Dict, Any
-from xml.etree import ElementTree as ET
-from dataclasses import dataclass, field
-
-# ---------------------------------------------------------------------------
-# Constants
-# ---------------------------------------------------------------------------
-
-SVG_NS = 'http://www.w3.org/2000/svg'
-XLINK_NS = 'http://www.w3.org/1999/xlink'
-
-# 1 SVG pixel = 9525 EMU (at 96 DPI)
-EMU_PER_PX = 9525
-
-# DrawingML font size unit: 1/100 of a point. 1px = 0.75pt at 96 DPI.
-FONT_PX_TO_HUNDREDTHS_PT = 75
-
-# DrawingML angle unit: 60000ths of a degree
-ANGLE_UNIT = 60000
-
-# SVG attributes that are inheritable from parent <g> to children
-INHERITABLE_ATTRS = [
- 'fill', 'stroke', 'stroke-width', 'stroke-dasharray', 'stroke-linecap',
- 'opacity', 'fill-opacity', 'stroke-opacity',
- 'font-family', 'font-size', 'font-weight', 'font-style',
- 'text-anchor', 'letter-spacing',
-]
-
-# Known East Asian fonts
-EA_FONTS = {
- 'PingFang SC', 'PingFang TC', 'PingFang HK',
- 'Microsoft YaHei', 'Microsoft JhengHei',
- 'SimSun', 'SimHei', 'FangSong', 'KaiTi', 'STKaiti',
- 'STHeiti', 'STSong', 'STFangsong', 'STXihei', 'STZhongsong',
- 'Hiragino Sans', 'Hiragino Sans GB', 'Hiragino Mincho ProN',
- 'Noto Sans SC', 'Noto Sans TC', 'Noto Serif SC', 'Noto Serif TC',
- 'Source Han Sans SC', 'Source Han Sans TC',
- 'Source Han Serif SC', 'Source Han Serif TC',
- 'WenQuanYi Micro Hei', 'WenQuanYi Zen Hei',
- 'YouYuan', 'LiSu', 'HuaWenKaiTi',
- 'Songti SC', 'Songti TC',
-}
-SYSTEM_FONTS = {'system-ui', '-apple-system', 'BlinkMacSystemFont'}
-
-# macOS/Linux-only fonts → Windows equivalents (PPTX targets Windows primarily)
-FONT_FALLBACK_WIN = {
- 'PingFang SC': 'Microsoft YaHei',
- 'PingFang TC': 'Microsoft JhengHei',
- 'PingFang HK': 'Microsoft JhengHei',
- 'Hiragino Sans': 'Microsoft YaHei',
- 'Hiragino Sans GB': 'Microsoft YaHei',
- 'Hiragino Mincho ProN': 'SimSun',
- 'STHeiti': 'SimHei',
- 'STSong': 'SimSun',
- 'STKaiti': 'KaiTi',
- 'STFangsong': 'FangSong',
- 'STXihei': 'Microsoft YaHei',
- 'STZhongsong': 'SimSun',
- 'Songti SC': 'SimSun',
- 'Songti TC': 'SimSun',
- 'Noto Sans SC': 'Microsoft YaHei',
- 'Noto Sans TC': 'Microsoft JhengHei',
- 'Noto Serif SC': 'SimSun',
- 'Noto Serif TC': 'SimSun',
- 'Source Han Sans SC': 'Microsoft YaHei',
- 'Source Han Sans TC': 'Microsoft JhengHei',
- 'Source Han Serif SC': 'SimSun',
- 'Source Han Serif TC': 'SimSun',
- 'WenQuanYi Micro Hei': 'Microsoft YaHei',
- 'WenQuanYi Zen Hei': 'Microsoft YaHei',
- # Latin fonts (macOS / Linux / Web → Windows)
- 'SF Pro': 'Segoe UI',
- 'SF Pro Display': 'Segoe UI',
- 'SF Pro Text': 'Segoe UI',
- 'SF Mono': 'Consolas',
- 'Menlo': 'Consolas',
- 'Monaco': 'Consolas',
- 'Helvetica Neue': 'Arial',
- 'Helvetica': 'Arial',
- 'Roboto': 'Segoe UI',
- 'Ubuntu': 'Segoe UI',
- 'Liberation Sans': 'Arial',
- 'Liberation Serif': 'Times New Roman',
- 'Liberation Mono': 'Consolas',
- 'DejaVu Sans': 'Segoe UI',
- 'DejaVu Serif': 'Times New Roman',
- 'DejaVu Sans Mono': 'Consolas',
-}
-# Generic CSS font families → Windows defaults
-GENERIC_FONT_MAP = {
- 'monospace': 'Consolas',
- 'sans-serif': 'Segoe UI',
- 'serif': 'Times New Roman',
-}
-# Serif latin fonts — when these are the latin choice and no EA font is
-# specified, prefer SimSun (serif CJK) over Microsoft YaHei (sans-serif CJK).
-_SERIF_LATIN = {
- 'Times New Roman', 'Georgia', 'Garamond', 'Palatino', 'Palatino Linotype',
- 'Book Antiqua', 'Cambria', 'SimSun', 'Liberation Serif', 'DejaVu Serif',
-}
-
-# Preset dash patterns: SVG stroke-dasharray -> DrawingML prstDash
-DASH_PRESETS = {
- '4,4': 'dash',
- '4 4': 'dash',
- '6,3': 'dash',
- '6 3': 'dash',
- '2,2': 'sysDot',
- '2 2': 'sysDot',
- '8,4': 'lgDash',
- '8 4': 'lgDash',
- '8,4,2,4': 'lgDashDot',
- '8 4 2 4': 'lgDashDot',
-}
-
-
-# ---------------------------------------------------------------------------
-# Data classes
-# ---------------------------------------------------------------------------
-
-@dataclass
-class ConvertContext:
- """Shared context passed through the conversion pipeline."""
- defs: Dict[str, ET.Element] = field(default_factory=dict)
- id_counter: int = 2 # start at 2 (1 is reserved for spTree root)
- slide_num: int = 1 # slide number for unique media filenames
- translate_x: float = 0.0
- translate_y: float = 0.0
- scale_x: float = 1.0
- scale_y: float = 1.0
- filter_id: Optional[str] = None # inherited filter from parent <g>
- media_files: Dict[str, bytes] = field(default_factory=dict) # filename -> data
- rel_entries: List[Dict[str, str]] = field(default_factory=list)
- rel_id_counter: int = 2 # rId1 reserved for slideLayout
- svg_dir: Optional[Path] = None # directory of the source SVG file
- inherited_styles: Dict[str, str] = field(default_factory=dict)
-
- def next_id(self) -> int:
- cid = self.id_counter
- self.id_counter += 1
- return cid
-
- def next_rel_id(self) -> str:
- rid = f'rId{self.rel_id_counter}'
- self.rel_id_counter += 1
- return rid
-
- def child(self, dx: float = 0, dy: float = 0,
- sx: float = 1.0, sy: float = 1.0,
- filter_id: Optional[str] = None,
- style_overrides: Optional[Dict[str, str]] = None) -> 'ConvertContext':
- """Create child context with accumulated translation, scale, and styles."""
- merged = dict(self.inherited_styles)
- if style_overrides:
- # Opacity is multiplicative, not override
- for op_key in ('opacity', 'fill-opacity', 'stroke-opacity'):
- 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]
- # Other attrs: child overrides parent
- for k, v in style_overrides.items():
- if k not in ('opacity', 'fill-opacity', 'stroke-opacity'):
- merged[k] = v
- return ConvertContext(
- defs=self.defs,
- id_counter=self.id_counter,
- slide_num=self.slide_num,
- translate_x=self.translate_x + dx,
- translate_y=self.translate_y + dy,
- scale_x=self.scale_x * sx,
- scale_y=self.scale_y * sy,
- filter_id=filter_id or self.filter_id,
- media_files=self.media_files,
- rel_entries=self.rel_entries,
- rel_id_counter=self.rel_id_counter,
- svg_dir=self.svg_dir,
- inherited_styles=merged,
- )
-
- def sync_from_child(self, child_ctx: 'ConvertContext'):
- """Sync counters back from child context."""
- self.id_counter = child_ctx.id_counter
- self.rel_id_counter = child_ctx.rel_id_counter
-
-
-# ---------------------------------------------------------------------------
-# Coordinate helpers
-# ---------------------------------------------------------------------------
-
-def px_to_emu(px: float) -> int:
- """Convert SVG pixels to EMU."""
- return round(px * EMU_PER_PX)
-
-
-def _f(val: Optional[str], default: float = 0.0) -> float:
- """Parse a float attribute, returning default if missing."""
- if val is None:
- return default
- try:
- return float(val)
- except (ValueError, TypeError):
- return default
-
-
-def _extract_inheritable_styles(elem: ET.Element) -> Dict[str, str]:
- """Extract all SVG-inheritable presentation attributes from an element."""
- styles = {}
- for attr in INHERITABLE_ATTRS:
- val = elem.get(attr)
- if val is not None:
- styles[attr] = val
- return styles
-
-
-def _get_attr(elem: ET.Element, attr: str, ctx: 'ConvertContext') -> Optional[str]:
- """Get effective attribute: element's own value first, then inherited from context."""
- val = elem.get(attr)
- if val is not None:
- return val
- return ctx.inherited_styles.get(attr)
-
-
-def ctx_x(val: float, ctx: 'ConvertContext') -> float:
- """Apply context scale + translate to an x coordinate."""
- return val * ctx.scale_x + ctx.translate_x
-
-
-def ctx_y(val: float, ctx: 'ConvertContext') -> float:
- """Apply context scale + translate to a y coordinate."""
- return val * ctx.scale_y + ctx.translate_y
-
-
-def ctx_w(val: float, ctx: 'ConvertContext') -> float:
- """Apply context scale to a width value."""
- return val * ctx.scale_x
-
-
-def ctx_h(val: float, ctx: 'ConvertContext') -> float:
- """Apply context scale to a height value."""
- return val * ctx.scale_y
-
-
-# ---------------------------------------------------------------------------
-# Color / style parsing
-# ---------------------------------------------------------------------------
-
-def parse_hex_color(color_str: str) -> Optional[str]:
- """Parse '#RRGGBB' or '#RGB' to 'RRGGBB'. Returns None on failure."""
- if not color_str:
- return None
- color_str = color_str.strip()
- if color_str.startswith('#'):
- color_str = color_str[1:]
- if len(color_str) == 3:
- color_str = ''.join(c * 2 for c in color_str)
- if len(color_str) == 6 and all(c in '0123456789abcdefABCDEF' for c in color_str):
- return color_str.upper()
- return None
-
-
-def parse_stop_style(style_str: str) -> Tuple[Optional[str], float]:
- """Parse stop element's style attribute: 'stop-color:#XXX;stop-opacity:N'"""
- color = None
- opacity = 1.0
- if not style_str:
- return color, opacity
- for part in style_str.split(';'):
- part = part.strip()
- if part.startswith('stop-color:'):
- color = parse_hex_color(part.split(':', 1)[1].strip())
- elif part.startswith('stop-opacity:'):
- try:
- opacity = float(part.split(':', 1)[1].strip())
- except ValueError:
- pass
- return color, opacity
-
-
-def resolve_url_id(url_str: str) -> Optional[str]:
- """Extract ID from 'url(#someId)' reference."""
- if not url_str:
- return None
- m = re.match(r'url\(#([^)]+)\)', url_str.strip())
- return m.group(1) if m else None
-
-
-def get_effective_filter_id(elem: ET.Element, ctx: ConvertContext) -> Optional[str]:
- """Get the filter ID for an element, considering inherited context."""
- filt = elem.get('filter')
- if filt:
- return resolve_url_id(filt)
- return ctx.filter_id
-
-
-# ---------------------------------------------------------------------------
-# Font parsing
-# ---------------------------------------------------------------------------
-
-def parse_font_family(font_family_str: str) -> Dict[str, str]:
- """Parse CSS font-family to latin/ea typefaces.
-
- Prioritises Windows-available fonts since PPTX is primarily opened on
- Windows. macOS/Linux-only fonts are mapped to their Windows equivalents
- via FONT_FALLBACK_WIN.
- """
- if not font_family_str:
- return {'latin': 'Segoe UI', 'ea': 'Microsoft YaHei'}
-
- fonts = [f.strip().strip("'\"") for f in font_family_str.split(',')]
- latin_font = None
- ea_font = None
-
- for font in fonts:
- if font in SYSTEM_FONTS:
- continue
- # Resolve generic families
- if font in GENERIC_FONT_MAP:
- resolved = GENERIC_FONT_MAP[font]
- latin_font = latin_font or resolved
- continue
- # Map to Windows equivalent if needed
- win_font = FONT_FALLBACK_WIN.get(font, font)
- if font in EA_FONTS:
- ea_font = ea_font or win_font
- else:
- latin_font = latin_font or win_font
-
- # If no latin font found but we have EA, use EA as latin too
- # (PPT renders CJK text via latin typeface when ea doesn't match)
- if not latin_font and ea_font:
- latin_font = ea_font
-
- final_latin = latin_font or 'Segoe UI'
-
- # EA must always be a CJK-capable font — never fall back to a latin-only
- # font like Arial or Georgia, which would break Chinese/Japanese/Korean text.
- if not ea_font:
- ea_font = 'SimSun' if final_latin in _SERIF_LATIN else 'Microsoft YaHei'
-
- return {
- 'latin': final_latin,
- 'ea': ea_font,
- }
-
-
-def is_cjk_char(ch: str) -> bool:
- """Check if a character is CJK."""
- cp = ord(ch)
- return (0x4E00 <= cp <= 0x9FFF or 0x3400 <= cp <= 0x4DBF or
- 0x2E80 <= cp <= 0x2EFF or 0x3000 <= cp <= 0x303F or
- 0xFF00 <= cp <= 0xFFEF or 0xF900 <= cp <= 0xFAFF or
- 0x20000 <= cp <= 0x2A6DF)
-
-
-def estimate_text_width(text: str, font_size: float, font_weight: str = '400') -> float:
- """Estimate text width in SVG pixels."""
- width = 0.0
- for ch in text:
- if is_cjk_char(ch):
- width += font_size
- elif ch == ' ':
- width += font_size * 0.3
- elif ch in 'mMwWOQ':
- width += font_size * 0.75
- elif ch in 'iIlj1!|':
- width += font_size * 0.3
- else:
- width += font_size * 0.55
- # Bold text is slightly wider
- if font_weight in ('bold', '600', '700', '800', '900'):
- width *= 1.05
- return width
-
-
-# ---------------------------------------------------------------------------
-# DrawingML XML builders
-# ---------------------------------------------------------------------------
-
-def _xml_escape(text: str) -> str:
- """Escape XML special characters."""
- return (text.replace('&', '&amp;')
- .replace('<', '&lt;')
- .replace('>', '&gt;')
- .replace('"', '&quot;'))
-
-
-def build_solid_fill(color: str, opacity: Optional[float] = None) -> str:
- """Build <a:solidFill> XML."""
- alpha = ''
- if opacity is not None and opacity < 1.0:
- alpha = f'<a:alpha val="{int(opacity * 100000)}"/>'
- return f'<a:solidFill><a:srgbClr val="{color}">{alpha}</a:srgbClr></a:solidFill>'
-
-
-def build_gradient_fill(grad_elem: ET.Element,
- opacity: Optional[float] = None) -> str:
- """Build <a:gradFill> from SVG linearGradient or radialGradient element."""
- tag = grad_elem.tag.replace(f'{{{SVG_NS}}}', '')
-
- # Parse stops
- stops_xml = []
- for child in grad_elem:
- child_tag = child.tag.replace(f'{{{SVG_NS}}}', '')
- if child_tag != 'stop':
- continue
- offset_str = child.get('offset', '0').strip().rstrip('%')
- try:
- offset = float(offset_str)
- # If percentage (most common), offset is 0-100
- if offset > 1.0:
- offset = offset / 100.0
- except ValueError:
- offset = 0.0
- pos = int(offset * 100000)
-
- # Parse color from style attribute or direct attributes
- style = child.get('style', '')
- color, stop_opacity = parse_stop_style(style)
- if not color:
- color = parse_hex_color(child.get('stop-color', '#000000'))
- if color is None:
- color = '000000'
- # Also check direct stop-opacity attribute (overrides style)
- direct_stop_op = child.get('stop-opacity')
- if direct_stop_op is not None:
- try:
- stop_opacity = float(direct_stop_op)
- except ValueError:
- pass
-
- alpha_xml = ''
- effective_opacity = stop_opacity
- if opacity is not None:
- effective_opacity *= opacity
- if effective_opacity < 1.0:
- alpha_xml = f'<a:alpha val="{int(effective_opacity * 100000)}"/>'
-
- stops_xml.append(
- f'<a:gs pos="{pos}"><a:srgbClr val="{color}">{alpha_xml}</a:srgbClr></a:gs>'
- )
-
- if not stops_xml:
- return ''
-
- gs_list = '\n'.join(stops_xml)
-
- if tag == 'linearGradient':
- # Calculate angle from x1,y1 -> x2,y2
- # Values can be fractions (0-1) or percentages (0%-100%)
- def parse_grad_coord(val_str: str, default: float = 0.0) -> float:
- val_str = val_str.strip()
- if val_str.endswith('%'):
- return float(val_str.rstrip('%')) / 100.0
- v = float(val_str)
- # Heuristic: if > 1, treat as percentage
- return v / 100.0 if v > 1.0 else v
-
- x1 = parse_grad_coord(grad_elem.get('x1', '0'))
- y1 = parse_grad_coord(grad_elem.get('y1', '0'))
- x2 = parse_grad_coord(grad_elem.get('x2', '1'))
- y2 = parse_grad_coord(grad_elem.get('y2', '1'))
-
- angle_rad = math.atan2(y2 - y1, x2 - x1)
- angle_deg = math.degrees(angle_rad)
- # DrawingML lin ang: 0°=left-to-right, 90°=top-to-bottom (clockwise)
- # SVG atan2 with y-down: 0°=left-to-right, 90°=top-to-bottom — same system
- dml_angle = int((angle_deg % 360) * ANGLE_UNIT)
-
- return f'''<a:gradFill>
-<a:gsLst>{gs_list}</a:gsLst>
-<a:lin ang="{dml_angle}" scaled="1"/>
-</a:gradFill>'''
-
- elif tag == 'radialGradient':
- return f'''<a:gradFill>
-<a:gsLst>{gs_list}</a:gsLst>
-<a:path path="circle">
-<a:fillToRect l="50000" t="50000" r="50000" b="50000"/>
-</a:path>
-</a:gradFill>'''
-
- return ''
-
-
-def build_fill_xml(elem: ET.Element, ctx: ConvertContext,
- opacity: Optional[float] = None) -> str:
- """Build fill XML for a shape element, with inherited style support."""
- fill = _get_attr(elem, 'fill', ctx)
- if fill is None:
- # SVG default fill is black
- fill = '#000000'
-
- if fill == 'none':
- return '<a:noFill/>'
-
- # Check for gradient reference
- grad_id = resolve_url_id(fill)
- if grad_id and grad_id in ctx.defs:
- return build_gradient_fill(ctx.defs[grad_id], opacity)
-
- # Solid color
- color = parse_hex_color(fill)
- if color:
- return build_solid_fill(color, opacity)
-
- return '<a:noFill/>'
-
-
-def build_stroke_xml(elem: ET.Element, ctx: ConvertContext,
- opacity: Optional[float] = None) -> str:
- """Build <a:ln> XML for stroke, with inherited style support."""
- stroke = _get_attr(elem, 'stroke', ctx)
- if not stroke or stroke == 'none':
- return '<a:ln><a:noFill/></a:ln>'
-
- width = _f(_get_attr(elem, 'stroke-width', ctx), 1.0)
- width_emu = px_to_emu(width)
-
- # Dash pattern
- dash_xml = ''
- dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
- if dasharray and dasharray != 'none':
- preset = DASH_PRESETS.get(dasharray.strip())
- if preset:
- dash_xml = f'<a:prstDash val="{preset}"/>'
- else:
- dash_xml = '<a:prstDash val="dash"/>'
-
- # Line cap
- cap_map = {'round': 'rnd', 'square': 'sq', 'butt': 'flat'}
- cap_attr = ''
- linecap = _get_attr(elem, 'stroke-linecap', ctx)
- if linecap and linecap in cap_map:
- cap_attr = f' cap="{cap_map[linecap]}"'
-
- # Check for gradient stroke
- grad_id = resolve_url_id(stroke)
- if grad_id and grad_id in ctx.defs:
- grad_fill = build_gradient_fill(ctx.defs[grad_id], opacity)
- return f'<a:ln w="{width_emu}"{cap_attr}>{grad_fill}{dash_xml}</a:ln>'
-
- # Solid color stroke
- color = parse_hex_color(stroke)
- if not color:
- return '<a:ln><a:noFill/></a:ln>'
-
- alpha_xml = ''
- if opacity is not None and opacity < 1.0:
- alpha_xml = f'<a:alpha val="{int(opacity * 100000)}"/>'
-
- return f'''<a:ln w="{width_emu}"{cap_attr}>
-<a:solidFill><a:srgbClr val="{color}">{alpha_xml}</a:srgbClr></a:solidFill>{dash_xml}
-</a:ln>'''
-
-
-def build_shadow_xml(filter_elem: ET.Element) -> str:
- """Build <a:effectLst> with <a:outerShdw> from SVG filter element."""
- if filter_elem is None:
- return ''
-
- std_dev = 4.0
- dx = 0.0
- dy = 4.0
- shadow_opacity = 0.3
- shadow_color = '000000'
-
- for child in filter_elem.iter():
- tag = child.tag.replace(f'{{{SVG_NS}}}', '')
- if tag == 'feGaussianBlur':
- std_dev = _f(child.get('stdDeviation'), 4.0)
- elif tag == 'feOffset':
- dx = _f(child.get('dx'), 0.0)
- dy = _f(child.get('dy'), 4.0)
- elif tag == 'feFlood':
- shadow_opacity = _f(child.get('flood-opacity'), 0.3)
- raw_color = child.get('flood-color', '').strip().lstrip('#')
- if len(raw_color) == 6 and all(c in '0123456789abcdefABCDEF' for c in raw_color):
- shadow_color = raw_color.upper()
- elif tag == 'feFuncA':
- # feComponentTransfer > feFuncA type="linear" slope="0.3"
- if child.get('type') == 'linear':
- shadow_opacity = _f(child.get('slope'), 0.3)
-
- blur_rad = px_to_emu(std_dev * 2)
- dist = px_to_emu(math.sqrt(dx * dx + dy * dy))
- # Direction angle: atan2(dy, dx), converted to DrawingML (from top, CW)
- dir_angle = int(((90 + math.degrees(math.atan2(dy, max(dx, 0.001)))) % 360) * ANGLE_UNIT)
- alpha_val = int(shadow_opacity * 100000)
-
- return f'''<a:effectLst>
-<a:outerShdw blurRad="{blur_rad}" dist="{dist}" dir="{dir_angle}" algn="tl" rotWithShape="0">
-<a:srgbClr val="{shadow_color}"><a:alpha val="{alpha_val}"/></a:srgbClr>
-</a:outerShdw>
-</a:effectLst>'''
-
-
-def get_element_opacity(elem: ET.Element) -> Optional[float]:
- """Get opacity value from element, returns None if 1.0 or not set."""
- op = elem.get('opacity')
- if op is None:
- return None
- try:
- val = float(op)
- return val if val < 1.0 else None
- except ValueError:
- return None
-
-
-def get_fill_opacity(elem: ET.Element, ctx: Optional[ConvertContext] = None) -> Optional[float]:
- """
- Get effective fill opacity combining 'opacity' and 'fill-opacity',
- including inherited values from context.
- Returns None if fully opaque.
- """
- base = 1.0
- op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
- if op:
- try:
- base = 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)
- except ValueError:
- pass
-
- return base if base < 1.0 else None
-
-
-def get_stroke_opacity(elem: ET.Element, ctx: Optional[ConvertContext] = None) -> Optional[float]:
- """
- Get effective stroke opacity combining 'opacity' and 'stroke-opacity',
- including inherited values from context.
- Returns None if fully opaque.
- """
- base = 1.0
- op = _get_attr(elem, 'opacity', ctx) if ctx else elem.get('opacity')
- if op:
- try:
- base = 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)
- except ValueError:
- pass
-
- return base if base < 1.0 else None
-
-
-# ---------------------------------------------------------------------------
-# SVG Path Parser
-# ---------------------------------------------------------------------------
-
-@dataclass
-class PathCommand:
- cmd: str # M, L, C, Z, etc. (uppercase = absolute)
- args: List[float] = field(default_factory=list)
-
-
-def parse_svg_path(d: str) -> List[PathCommand]:
- """Parse SVG path d attribute into a list of PathCommands."""
- if not d:
- return []
-
- commands = []
- # Tokenize: split into commands and numbers
- # Handle negative numbers and decimals correctly
- tokens = re.findall(r'[MmLlHhVvCcSsQqTtAaZz]|[-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?', d)
-
- current_cmd = None
- current_args = []
-
- def flush():
- nonlocal current_cmd, current_args
- if current_cmd is not None:
- # Some commands can have implicit repeats
- arg_counts = {
- 'M': 2, 'm': 2, 'L': 2, 'l': 2,
- 'H': 1, 'h': 1, 'V': 1, 'v': 1,
- 'C': 6, 'c': 6, 'S': 4, 's': 4,
- 'Q': 4, 'q': 4, 'T': 2, 't': 2,
- 'A': 7, 'a': 7, 'Z': 0, 'z': 0,
- }
- n = arg_counts.get(current_cmd, 0)
- if n == 0:
- commands.append(PathCommand(current_cmd, []))
- elif n > 0 and len(current_args) >= n:
- # Split into multiple commands if there are extra args
- i = 0
- while i + n <= len(current_args):
- commands.append(PathCommand(current_cmd, current_args[i:i + n]))
- # After first M, implicit commands become L
- if current_cmd == 'M':
- current_cmd = 'L'
- elif current_cmd == 'm':
- current_cmd = 'l'
- i += n
- current_args = []
-
- for token in tokens:
- if token in 'MmLlHhVvCcSsQqTtAaZz':
- flush()
- current_cmd = token
- current_args = []
- else:
- try:
- current_args.append(float(token))
- except ValueError:
- pass
-
- flush()
- return commands
-
-
-def svg_path_to_absolute(commands: List[PathCommand]) -> List[PathCommand]:
- """Convert all relative path commands to absolute."""
- result = []
- cx, cy = 0.0, 0.0 # Current point
- sx, sy = 0.0, 0.0 # Subpath start
-
- for cmd in commands:
- a = cmd.args
- if cmd.cmd == 'M':
- cx, cy = a[0], a[1]
- sx, sy = cx, cy
- result.append(PathCommand('M', [cx, cy]))
- elif cmd.cmd == 'm':
- cx += a[0]
- cy += a[1]
- sx, sy = cx, cy
- result.append(PathCommand('M', [cx, cy]))
- elif cmd.cmd == 'L':
- cx, cy = a[0], a[1]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'l':
- cx += a[0]
- cy += a[1]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'H':
- cx = a[0]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'h':
- cx += a[0]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'V':
- cy = a[0]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'v':
- cy += a[0]
- result.append(PathCommand('L', [cx, cy]))
- elif cmd.cmd == 'C':
- result.append(PathCommand('C', list(a)))
- cx, cy = a[4], a[5]
- elif cmd.cmd == 'c':
- abs_args = [
- cx + a[0], cy + a[1],
- cx + a[2], cy + a[3],
- cx + a[4], cy + a[5],
- ]
- result.append(PathCommand('C', abs_args))
- cx, cy = abs_args[4], abs_args[5]
- elif cmd.cmd == 'S':
- result.append(PathCommand('S', list(a)))
- cx, cy = a[2], a[3]
- elif cmd.cmd == 's':
- abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
- result.append(PathCommand('S', abs_args))
- cx, cy = abs_args[2], abs_args[3]
- elif cmd.cmd == 'Q':
- result.append(PathCommand('Q', list(a)))
- cx, cy = a[2], a[3]
- elif cmd.cmd == 'q':
- abs_args = [cx + a[0], cy + a[1], cx + a[2], cy + a[3]]
- result.append(PathCommand('Q', abs_args))
- cx, cy = abs_args[2], abs_args[3]
- elif cmd.cmd == 'T':
- result.append(PathCommand('T', list(a)))
- cx, cy = a[0], a[1]
- elif cmd.cmd == 't':
- abs_args = [cx + a[0], cy + a[1]]
- result.append(PathCommand('T', abs_args))
- cx, cy = abs_args[0], abs_args[1]
- elif cmd.cmd == 'A':
- result.append(PathCommand('A', list(a)))
- cx, cy = a[5], a[6]
- elif cmd.cmd == 'a':
- abs_args = [a[0], a[1], a[2], a[3], a[4], cx + a[5], cy + a[6]]
- result.append(PathCommand('A', abs_args))
- cx, cy = abs_args[5], abs_args[6]
- elif cmd.cmd in ('Z', 'z'):
- result.append(PathCommand('Z', []))
- cx, cy = sx, sy
-
- return result
-
-
-def _reflect_control_point(cp_x: float, cp_y: float,
- cx: float, cy: float) -> Tuple[float, float]:
- """Reflect a control point through the current point."""
- return 2 * cx - cp_x, 2 * cy - cp_y
-
-
-def _quad_to_cubic(qp_x: float, qp_y: float,
- p0_x: float, p0_y: float,
- p3_x: float, p3_y: float) -> List[float]:
- """Convert quadratic bezier control point to cubic bezier control points."""
- cp1_x = p0_x + 2.0 / 3.0 * (qp_x - p0_x)
- cp1_y = p0_y + 2.0 / 3.0 * (qp_y - p0_y)
- cp2_x = p3_x + 2.0 / 3.0 * (qp_x - p3_x)
- cp2_y = p3_y + 2.0 / 3.0 * (qp_y - p3_y)
- return [cp1_x, cp1_y, cp2_x, cp2_y, p3_x, p3_y]
-
-
-def _arc_to_cubic_beziers(cx_: float, cy_: float,
- rx: float, ry: float,
- phi: float,
- large_arc: int, sweep: int,
- x2: float, y2: float) -> List[PathCommand]:
- """
- Convert SVG arc (endpoint parameterization) to cubic bezier curves.
-
- Uses the algorithm from the SVG spec (F.6.5) to convert endpoint to center
- parameterization, then approximates each arc segment with cubic beziers.
- """
- x1, y1 = cx_, cy_
-
- # If endpoints are the same, skip
- if abs(x1 - x2) < 1e-10 and abs(y1 - y2) < 1e-10:
- return []
-
- # Ensure radii are positive
- rx = abs(rx)
- ry = abs(ry)
- if rx < 1e-10 or ry < 1e-10:
- return [PathCommand('L', [x2, y2])]
-
- phi_rad = math.radians(phi)
- cos_phi = math.cos(phi_rad)
- sin_phi = math.sin(phi_rad)
-
- # Step 1: Compute (x1', y1')
- dx = (x1 - x2) / 2.0
- dy = (y1 - y2) / 2.0
- x1p = cos_phi * dx + sin_phi * dy
- y1p = -sin_phi * dx + cos_phi * dy
-
- # Step 2: Compute (cx', cy')
- x1p2 = x1p * x1p
- y1p2 = y1p * y1p
- rx2 = rx * rx
- ry2 = ry * ry
-
- # Ensure radii are large enough
- lam = x1p2 / rx2 + y1p2 / ry2
- if lam > 1:
- lam_sqrt = math.sqrt(lam)
- rx *= lam_sqrt
- ry *= lam_sqrt
- rx2 = rx * rx
- ry2 = ry * ry
-
- num = max(rx2 * ry2 - rx2 * y1p2 - ry2 * x1p2, 0)
- den = rx2 * y1p2 + ry2 * x1p2
- sq = math.sqrt(num / den) if den > 1e-10 else 0.0
-
- if large_arc == sweep:
- sq = -sq
-
- cxp = sq * rx * y1p / ry
- cyp = -sq * ry * x1p / rx
-
- # Step 3: Compute (cx, cy)
- arc_cx = cos_phi * cxp - sin_phi * cyp + (x1 + x2) / 2.0
- arc_cy = sin_phi * cxp + cos_phi * cyp + (y1 + y2) / 2.0
-
- # Step 4: Compute theta1 and dtheta
- def angle_between(ux, uy, vx, vy):
- n = math.sqrt((ux * ux + uy * uy) * (vx * vx + vy * vy))
- if n < 1e-10:
- return 0
- c = (ux * vx + uy * vy) / n
- c = max(-1, min(1, c))
- a = math.acos(c)
- if ux * vy - uy * vx < 0:
- a = -a
- return a
-
- theta1 = angle_between(1, 0, (x1p - cxp) / rx, (y1p - cyp) / ry)
- dtheta = angle_between(
- (x1p - cxp) / rx, (y1p - cyp) / ry,
- (-x1p - cxp) / rx, (-y1p - cyp) / ry
- )
-
- if sweep == 0 and dtheta > 0:
- dtheta -= 2 * math.pi
- elif sweep == 1 and dtheta < 0:
- dtheta += 2 * math.pi
-
- # Split arc into segments of at most 90 degrees
- n_segs = max(1, int(math.ceil(abs(dtheta) / (math.pi / 2))))
- d_per_seg = dtheta / n_segs
-
- result = []
- alpha = 4.0 / 3.0 * math.tan(d_per_seg / 4.0)
-
- for i in range(n_segs):
- t1 = theta1 + i * d_per_seg
- t2 = theta1 + (i + 1) * d_per_seg
-
- cos_t1 = math.cos(t1)
- sin_t1 = math.sin(t1)
- cos_t2 = math.cos(t2)
- sin_t2 = math.sin(t2)
-
- # Control points in unit circle
- ep1_x = cos_t1 - alpha * sin_t1
- ep1_y = sin_t1 + alpha * cos_t1
- ep2_x = cos_t2 + alpha * sin_t2
- ep2_y = sin_t2 - alpha * cos_t2
- ep_x = cos_t2
- ep_y = sin_t2
-
- # Scale by radii, rotate by phi, translate to center
- def transform_pt(px, py):
- x = rx * px
- y = ry * py
- xr = cos_phi * x - sin_phi * y + arc_cx
- yr = sin_phi * x + cos_phi * y + arc_cy
- return xr, yr
-
- cp1 = transform_pt(ep1_x, ep1_y)
- cp2 = transform_pt(ep2_x, ep2_y)
- ep = transform_pt(ep_x, ep_y)
-
- result.append(PathCommand('C', [cp1[0], cp1[1], cp2[0], cp2[1], ep[0], ep[1]]))
-
- return result
-
-
-def normalize_path_commands(commands: List[PathCommand]) -> List[PathCommand]:
- """
- Normalize path commands:
- - Convert S/s to C (smooth cubic → explicit cubic)
- - Convert Q/q to C (quadratic → cubic)
- - Convert T/t to C (smooth quadratic → explicit cubic)
- - Convert A/a to C sequences (arc → cubic bezier approximation)
- """
- result = []
- cx, cy = 0.0, 0.0
- last_cp_x, last_cp_y = 0.0, 0.0 # Last control point for S/T
- last_cmd = ''
-
- for cmd in commands:
- a = cmd.args
-
- if cmd.cmd == 'M':
- cx, cy = a[0], a[1]
- last_cp_x, last_cp_y = cx, cy
- result.append(cmd)
- elif cmd.cmd == 'L':
- cx, cy = a[0], a[1]
- last_cp_x, last_cp_y = cx, cy
- result.append(cmd)
- elif cmd.cmd == 'C':
- last_cp_x, last_cp_y = a[2], a[3] # Second control point
- cx, cy = a[4], a[5]
- result.append(cmd)
- elif cmd.cmd == 'S':
- # Reflect last cubic control point
- if last_cmd in ('C', 'S'):
- rcp_x, rcp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
- else:
- rcp_x, rcp_y = cx, cy
- last_cp_x, last_cp_y = a[0], a[1]
- new_cx, new_cy = a[2], a[3]
- result.append(PathCommand('C', [rcp_x, rcp_y, a[0], a[1], new_cx, new_cy]))
- cx, cy = new_cx, new_cy
- elif cmd.cmd == 'Q':
- cubic = _quad_to_cubic(a[0], a[1], cx, cy, a[2], a[3])
- last_cp_x, last_cp_y = a[0], a[1]
- result.append(PathCommand('C', cubic))
- cx, cy = a[2], a[3]
- elif cmd.cmd == 'T':
- # Reflect last quadratic control point
- if last_cmd in ('Q', 'T'):
- qp_x, qp_y = _reflect_control_point(last_cp_x, last_cp_y, cx, cy)
- else:
- qp_x, qp_y = cx, cy
- last_cp_x, last_cp_y = qp_x, qp_y
- cubic = _quad_to_cubic(qp_x, qp_y, cx, cy, a[0], a[1])
- result.append(PathCommand('C', cubic))
- cx, cy = a[0], a[1]
- elif cmd.cmd == 'A':
- arc_beziers = _arc_to_cubic_beziers(
- cx, cy, a[0], a[1], a[2], int(a[3]), int(a[4]), a[5], a[6]
- )
- for bc in arc_beziers:
- result.append(bc)
- cx, cy = a[5], a[6]
- last_cp_x, last_cp_y = cx, cy
- elif cmd.cmd == 'Z':
- result.append(cmd)
- else:
- result.append(cmd)
-
- last_cmd = cmd.cmd
-
- return result
-
-
-def path_commands_to_drawingml(commands: List[PathCommand],
- offset_x: float = 0, offset_y: float = 0,
- scale_x: float = 1.0, scale_y: float = 1.0) -> Tuple[str, float, float, float, float]:
- """
- Convert normalized path commands to DrawingML <a:path> inner XML.
-
- Returns: (path_xml, min_x, min_y, width, height) in scaled+offset coordinates.
- """
- if not commands:
- return '', 0, 0, 0, 0
-
- # First pass: calculate bounding box (applying scale + offset)
- points = []
- for cmd in commands:
- if cmd.cmd == 'M' or cmd.cmd == 'L':
- points.append((cmd.args[0] * scale_x + offset_x,
- cmd.args[1] * scale_y + offset_y))
- elif cmd.cmd == 'C':
- for i in range(0, 6, 2):
- points.append((cmd.args[i] * scale_x + offset_x,
- cmd.args[i + 1] * scale_y + offset_y))
-
- if not points:
- return '', 0, 0, 0, 0
-
- min_x = min(p[0] for p in points)
- min_y = min(p[1] for p in points)
- max_x = max(p[0] for p in points)
- max_y = max(p[1] for p in points)
-
- width = max(max_x - min_x, 1)
- height = max(max_y - min_y, 1)
-
- # Second pass: generate DrawingML path commands
- # Coordinates are in EMU, relative to shape's position
- parts = []
- for cmd in commands:
- if cmd.cmd == 'M':
- x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
- y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
- parts.append(f'<a:moveTo><a:pt x="{x_emu}" y="{y_emu}"/></a:moveTo>')
- elif cmd.cmd == 'L':
- x_emu = px_to_emu(cmd.args[0] * scale_x + offset_x - min_x)
- y_emu = px_to_emu(cmd.args[1] * scale_y + offset_y - min_y)
- parts.append(f'<a:lnTo><a:pt x="{x_emu}" y="{y_emu}"/></a:lnTo>')
- elif cmd.cmd == 'C':
- pts = []
- for i in range(0, 6, 2):
- x_emu = px_to_emu(cmd.args[i] * scale_x + offset_x - min_x)
- y_emu = px_to_emu(cmd.args[i + 1] * scale_y + offset_y - min_y)
- pts.append(f'<a:pt x="{x_emu}" y="{y_emu}"/>')
- parts.append(f'<a:cubicBezTo>{"".join(pts)}</a:cubicBezTo>')
- elif cmd.cmd == 'Z':
- parts.append('<a:close/>')
-
- path_inner = '\n'.join(parts)
- return path_inner, min_x, min_y, width, height
-
-
-# ---------------------------------------------------------------------------
-# Element converters
-# ---------------------------------------------------------------------------
-
-def _wrap_shape(shape_id: int, name: str, off_x: int, off_y: int,
- ext_cx: int, ext_cy: int,
- geom_xml: str, fill_xml: str, stroke_xml: str,
- effect_xml: str = '', extra_xml: str = '',
- rot: int = 0) -> str:
- """Wrap DrawingML content into a <p:sp> shape element."""
- rot_attr = f' rot="{rot}"' if rot else ''
- return f'''<p:sp>
-<p:nvSpPr>
-<p:cNvPr id="{shape_id}" name="{_xml_escape(name)}"/>
-<p:cNvSpPr/><p:nvPr/>
-</p:nvSpPr>
-<p:spPr>
-<a:xfrm{rot_attr}><a:off x="{off_x}" y="{off_y}"/><a:ext cx="{ext_cx}" cy="{ext_cy}"/></a:xfrm>
-{geom_xml}
-{fill_xml}
-{stroke_xml}
-{effect_xml}
-</p:spPr>
-{extra_xml}
-</p:sp>'''
-
-
-def convert_rect(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <rect> to DrawingML shape."""
- x = ctx_x(_f(elem.get('x')), ctx)
- y = ctx_y(_f(elem.get('y')), ctx)
- w = ctx_w(_f(elem.get('width')), ctx)
- h = ctx_h(_f(elem.get('height')), ctx)
-
- if w <= 0 or h <= 0:
- return ''
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- # Shadow
- effect = ''
- filt_id = get_effective_filter_id(elem, ctx)
- if filt_id and filt_id in ctx.defs:
- effect = build_shadow_xml(ctx.defs[filt_id])
-
- geom = '<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>'
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Rectangle {shape_id}',
- px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
- geom, fill, stroke, effect
- )
-
-
-def _build_arc_ring_path(cx: float, cy: float, r: float,
- stroke_width: float,
- dash_len: float, dash_offset: float,
- rotate_deg: float,
- sx: float, sy: float) -> tuple:
- """Build a filled annular-sector (donut segment) as DrawingML custGeom.
-
- SVG donut charts use stroke-dasharray on a circle to draw arc segments.
- DrawingML cannot reproduce this, so we convert each arc segment into a
- filled ring shape (outer arc → line → inner arc → close).
-
- Returns (geom_xml, min_x_emu, min_y_emu, w_emu, h_emu).
- """
- circumference = 2 * math.pi * r
- if circumference <= 0:
- return '', 0, 0, 0, 0
-
- # Arc start/end as fraction of circumference
- start_frac = -dash_offset / circumference
- end_frac = start_frac + dash_len / circumference
-
- # Convert to angles (radians), starting from top (SVG rotate(-90) is common)
- start_angle = start_frac * 2 * math.pi + math.radians(rotate_deg)
- end_angle = end_frac * 2 * math.pi + math.radians(rotate_deg)
-
- half_sw = stroke_width / 2
- r_outer = (r + half_sw)
- r_inner = (r - half_sw)
-
- # Generate points for the arc (use enough segments for smoothness)
- num_segments = max(16, int(abs(end_angle - start_angle) / (math.pi / 32)))
- angles = [start_angle + (end_angle - start_angle) * i / num_segments
- for i in range(num_segments + 1)]
-
- outer_pts = [(cx + r_outer * math.sin(a), cy - r_outer * math.cos(a)) for a in angles]
- inner_pts = [(cx + r_inner * math.sin(a), cy - r_inner * math.cos(a)) for a in reversed(angles)]
-
- all_pts = outer_pts + inner_pts
-
- # Apply scale
- all_pts = [(px * sx, py * sy) for px, py in all_pts]
-
- # Bounding box
- xs = [p[0] for p in all_pts]
- ys = [p[1] for p in all_pts]
- min_x = min(xs)
- min_y = min(ys)
- max_x = max(xs)
- max_y = max(ys)
- width = max_x - min_x
- height = max_y - min_y
-
- if width < 0.5 or height < 0.5:
- return '', 0, 0, 0, 0
-
- w_emu = px_to_emu(width)
- h_emu = px_to_emu(height)
-
- # Build path commands (translate to local coordinates)
- lines = []
- for i, (px, py) in enumerate(all_pts):
- lx = px_to_emu(px - min_x)
- ly = px_to_emu(py - min_y)
- if i == 0:
- lines.append(f'<a:moveTo><a:pt x="{lx}" y="{ly}"/></a:moveTo>')
- else:
- lines.append(f'<a:lnTo><a:pt x="{lx}" y="{ly}"/></a:lnTo>')
- lines.append('<a:close/>')
-
- path_xml = '\n'.join(lines)
- 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}">
-{path_xml}
-</a:path></a:pathLst>
-</a:custGeom>'''
-
- return geom, px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu
-
-
-def _is_donut_circle(elem: ET.Element, ctx: ConvertContext) -> bool:
- """Detect if a circle uses stroke-dasharray to simulate an arc segment."""
- dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
- if not dasharray or dasharray == 'none':
- return False
- stroke = _get_attr(elem, 'stroke', ctx)
- if not stroke or stroke == 'none':
- return False
- # Donut segments have large stroke-width relative to radius
- sw = _f(_get_attr(elem, 'stroke-width', ctx), 0)
- r = _f(elem.get('r'), 0)
- if sw <= 0 or r <= 0:
- return False
- # If dasharray doesn't match a standard preset, treat as arc
- if dasharray.strip() in DASH_PRESETS:
- return False
- return True
-
-
-def convert_circle(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <circle> to DrawingML ellipse shape.
-
- Detects SVG donut-chart circles (stroke-dasharray arc segments) and
- converts them to filled annular-sector shapes instead.
- """
- cx_ = _f(elem.get('cx'))
- cy_ = _f(elem.get('cy'))
- r = _f(elem.get('r'))
-
- if r <= 0:
- return ''
-
- # --- Donut-chart arc segment detection ---
- if _is_donut_circle(elem, ctx):
- dasharray = _get_attr(elem, 'stroke-dasharray', ctx)
- dash_vals = re.split(r'[\s,]+', dasharray.strip())
- dash_len = float(dash_vals[0]) if dash_vals else 0
- dash_offset = _f(elem.get('stroke-dashoffset'), 0)
- stroke_width = _f(_get_attr(elem, 'stroke-width', ctx), 1)
-
- # Parse rotate transform on the element
- rotate_deg = 0.0
- transform = elem.get('transform', '')
- r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
- if r_match:
- rotate_deg = float(r_match.group(1))
-
- geom, min_x, min_y, w_emu, h_emu = _build_arc_ring_path(
- ctx_x(cx_, ctx) / ctx.scale_x, # pass unscaled center
- ctx_y(cy_, ctx) / ctx.scale_y,
- r, stroke_width, dash_len, dash_offset, rotate_deg,
- ctx.scale_x, ctx.scale_y
- )
- if not geom:
- return ''
-
- # Use the stroke color/gradient as fill for the arc shape
- stroke_val = _get_attr(elem, 'stroke', ctx)
- op = get_fill_opacity(elem, ctx)
- grad_id = resolve_url_id(stroke_val) if stroke_val else None
- if grad_id and grad_id in ctx.defs:
- fill = build_gradient_fill(ctx.defs[grad_id], op)
- elif stroke_val:
- color = parse_hex_color(stroke_val)
- fill = build_solid_fill(color, op) if color else '<a:noFill/>'
- else:
- fill = '<a:noFill/>'
-
- stroke_xml = '<a:ln><a:noFill/></a:ln>'
-
- effect = ''
- filt_id = get_effective_filter_id(elem, ctx)
- if filt_id and filt_id in ctx.defs:
- effect = build_shadow_xml(ctx.defs[filt_id])
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Arc {shape_id}',
- min_x, min_y, w_emu, h_emu,
- geom, fill, stroke_xml, effect
- )
-
- # --- Normal circle ---
- cx_s = ctx_x(cx_, ctx)
- cy_s = ctx_y(cy_, ctx)
- r_x = r * ctx.scale_x
- r_y = r * ctx.scale_y
-
- x = cx_s - r_x
- y = cy_s - r_y
- w = r_x * 2
- h = r_y * 2
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- effect = ''
- filt_id = get_effective_filter_id(elem, ctx)
- if filt_id and filt_id in ctx.defs:
- effect = build_shadow_xml(ctx.defs[filt_id])
-
- geom = '<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>'
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Ellipse {shape_id}',
- px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
- geom, fill, stroke, effect
- )
-
-
-def convert_line(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <line> to DrawingML custom geometry shape."""
- x1 = ctx_x(_f(elem.get('x1')), ctx)
- y1 = ctx_y(_f(elem.get('y1')), ctx)
- x2 = ctx_x(_f(elem.get('x2')), ctx)
- y2 = ctx_y(_f(elem.get('y2')), ctx)
-
- min_x = min(x1, x2)
- min_y = min(y1, y2)
- w = max(abs(x2 - x1), 1)
- h = max(abs(y2 - y1), 1)
-
- w_emu = px_to_emu(w)
- h_emu = px_to_emu(h)
-
- lx1 = px_to_emu(x1 - min_x)
- ly1 = px_to_emu(y1 - min_y)
- lx2 = px_to_emu(x2 - min_x)
- ly2 = px_to_emu(y2 - min_y)
-
- 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}">
-<a:moveTo><a:pt x="{lx1}" y="{ly1}"/></a:moveTo>
-<a:lnTo><a:pt x="{lx2}" y="{ly2}"/></a:lnTo>
-</a:path></a:pathLst>
-</a:custGeom>'''
-
- stroke_op = get_stroke_opacity(elem, ctx)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Line {shape_id}',
- px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
- geom, '<a:noFill/>', stroke
- )
-
-
-def convert_path(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <path> to DrawingML custom geometry shape."""
- d = elem.get('d', '')
- if not d:
- return ''
-
- # Parse, absolutize, normalize
- commands = parse_svg_path(d)
- commands = svg_path_to_absolute(commands)
- commands = normalize_path_commands(commands)
-
- # Handle transform on the path element itself
- tx, ty = 0.0, 0.0
- rot = 0
- transform = elem.get('transform')
- if transform:
- t_match = re.search(r'translate\(\s*([-\d.]+)[\s,]+([-\d.]+)\s*\)', transform)
- if t_match:
- tx = float(t_match.group(1))
- ty = float(t_match.group(2))
- r_match = re.search(r'rotate\(\s*([-\d.]+)', transform)
- if r_match:
- rot = int(float(r_match.group(1)) * ANGLE_UNIT)
-
- path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
- commands, ctx.translate_x + tx, ctx.translate_y + ty,
- ctx.scale_x, ctx.scale_y
- )
-
- if not path_xml:
- return ''
-
- w_emu = px_to_emu(width)
- h_emu = px_to_emu(height)
-
- 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}">
-{path_xml}
-</a:path></a:pathLst>
-</a:custGeom>'''
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- effect = ''
- filt_id = get_effective_filter_id(elem, ctx)
- if filt_id and filt_id in ctx.defs:
- effect = build_shadow_xml(ctx.defs[filt_id])
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Freeform {shape_id}',
- px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
- geom, fill, stroke, effect, rot=rot
- )
-
-
-def convert_polygon(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <polygon> to DrawingML custom geometry shape."""
- points_str = elem.get('points', '')
- if not points_str:
- return ''
-
- # Parse points
- nums = re.findall(r'[-+]?(?:\d+\.?\d*|\.\d+)', points_str)
- if len(nums) < 4:
- return ''
-
- points = []
- for i in range(0, len(nums) - 1, 2):
- points.append((float(nums[i]), float(nums[i + 1])))
-
- # Build path commands
- commands = [PathCommand('M', [points[0][0], points[0][1]])]
- for px_, py_ in points[1:]:
- commands.append(PathCommand('L', [px_, py_]))
- commands.append(PathCommand('Z', []))
-
- path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
- commands, ctx.translate_x, ctx.translate_y,
- ctx.scale_x, ctx.scale_y
- )
-
- if not path_xml:
- return ''
-
- w_emu = px_to_emu(width)
- h_emu = px_to_emu(height)
-
- 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}">
-{path_xml}
-</a:path></a:pathLst>
-</a:custGeom>'''
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Polygon {shape_id}',
- px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
- geom, fill, stroke
- )
-
-
-def convert_polyline(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <polyline> to DrawingML custom geometry shape."""
- points_str = elem.get('points', '')
- if not points_str:
- return ''
-
- nums = re.findall(r'[-+]?(?:\d+\.?\d*|\.\d+)', points_str)
- if len(nums) < 4:
- return ''
-
- points = []
- for i in range(0, len(nums) - 1, 2):
- points.append((float(nums[i]), float(nums[i + 1])))
-
- commands = [PathCommand('M', [points[0][0], points[0][1]])]
- for px_, py_ in points[1:]:
- commands.append(PathCommand('L', [px_, py_]))
- # No close for polyline
-
- path_xml, min_x, min_y, width, height = path_commands_to_drawingml(
- commands, ctx.translate_x, ctx.translate_y,
- ctx.scale_x, ctx.scale_y
- )
-
- if not path_xml:
- return ''
-
- w_emu = px_to_emu(width)
- h_emu = px_to_emu(height)
-
- 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}">
-{path_xml}
-</a:path></a:pathLst>
-</a:custGeom>'''
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Polyline {shape_id}',
- px_to_emu(min_x), px_to_emu(min_y), w_emu, h_emu,
- geom, '<a:noFill/>', stroke
- )
-
-
-def _normalize_text(text: str) -> str:
- """Collapse internal whitespace/newlines into a single space, strip ends."""
- if not text:
- return ''
- return re.sub(r'\s+', ' ', text).strip()
-
-
-def _build_text_runs(elem: ET.Element, parent_attrs: Dict[str, Any]) -> List[Dict[str, Any]]:
- """
- 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. Child <tspan> attributes override parent defaults.
- """
- runs = []
-
- # Direct text of <text> element (before first child)
- if elem.text:
- t = _normalize_text(elem.text)
- if t:
- runs.append({**parent_attrs, 'text': t})
-
- 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)
- # Override with tspan-specific attributes
- 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')
- runs.append({**run_attrs, 'text': t})
-
- # Tail text after </tspan> (still belongs to parent)
- if child.tail:
- t = _normalize_text(child.tail)
- if t:
- runs.append({**parent_attrs, 'text': t})
-
- return runs
-
-
-def _build_run_xml(run: Dict[str, Any], default_fonts: Dict[str, str],
- ctx: Optional[ConvertContext] = None) -> str:
- """Build a single <a:r> XML from a run dict. Supports gradient fills on text."""
- text = run['text']
- fill = run.get('fill', '000000')
- fill_raw = run.get('fill_raw', '')
- fw = run.get('font_weight', '400')
- fs_px = run.get('font_size', 16)
- fstyle = run.get('font_style', '')
- ff = run.get('font_family', '')
- opacity = run.get('opacity')
-
- sz = round(fs_px * FONT_PX_TO_HUNDREDTHS_PT)
- b_attr = ' b="1"' if fw in ('bold', '600', '700', '800', '900') else ''
- i_attr = ' i="1"' if fstyle == 'italic' else ''
-
- fonts = parse_font_family(ff) if ff else default_fonts
-
- # Build fill XML - gradient or solid
- grad_id = resolve_url_id(fill_raw)
- if grad_id and ctx and grad_id in ctx.defs:
- fill_xml = build_gradient_fill(ctx.defs[grad_id], opacity)
- else:
- alpha_xml = ''
- if opacity is not None and opacity < 1.0:
- alpha_xml = f'<a:alpha val="{int(opacity * 100000)}"/>'
- fill_xml = f'<a:solidFill><a:srgbClr val="{fill}">{alpha_xml}</a:srgbClr></a:solidFill>'
-
- return f'''<a:r>
-<a:rPr lang="zh-CN" sz="{sz}"{b_attr}{i_attr} dirty="0">
-{fill_xml}
-<a:latin typeface="{_xml_escape(fonts['latin'])}"/>
-<a:ea typeface="{_xml_escape(fonts['ea'])}"/>
-<a:cs typeface="{_xml_escape(fonts['latin'])}"/>
-</a:rPr>
-<a:t>{_xml_escape(text)}</a:t>
-</a:r>'''
-
-
-def convert_text(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <text> to DrawingML text shape with multi-run support."""
- x = ctx_x(_f(elem.get('x')), ctx)
- y = ctx_y(_f(elem.get('y')), ctx)
- font_size = _f(_get_attr(elem, 'font-size', ctx), 16) * ctx.scale_y
- font_weight = _get_attr(elem, 'font-weight', ctx) or '400'
- font_family_str = _get_attr(elem, 'font-family', ctx) or ''
- text_anchor = _get_attr(elem, 'text-anchor', ctx) or 'start'
- fill_raw = _get_attr(elem, 'fill', ctx) or '#000000'
- fill_color = parse_hex_color(fill_raw) or '000000'
- opacity = get_fill_opacity(elem, ctx)
- font_style = _get_attr(elem, 'font-style', ctx) or ''
-
- fonts = parse_font_family(font_family_str)
-
- # Build runs from <text> + <tspan> children
- parent_attrs = {
- 'fill': fill_color,
- 'fill_raw': fill_raw,
- 'font_weight': font_weight,
- 'font_size': font_size,
- 'font_family': font_family_str,
- 'font_style': font_style,
- 'opacity': opacity,
- }
- runs = _build_text_runs(elem, parent_attrs)
-
- if not runs:
- return ''
-
- # Combined text for width estimation
- full_text = ''.join(r['text'] for r in runs)
- if not full_text.strip():
- return ''
-
- # Estimate text dimensions
- text_width = estimate_text_width(full_text, font_size, font_weight) * 1.15
- text_height = font_size * 1.5
- padding = font_size * 0.1
-
- # Adjust position based on text-anchor
- if text_anchor == 'middle':
- box_x = x - text_width / 2 - padding
- elif text_anchor == 'end':
- box_x = x - text_width - padding
- else:
- box_x = x - padding
-
- box_y = y - font_size * 0.85
- box_w = text_width + padding * 2
- box_h = text_height + padding
-
- # Letter spacing
- spc_attr = ''
- letter_spacing = _get_attr(elem, 'letter-spacing', ctx)
- if letter_spacing:
- try:
- spc_val = float(letter_spacing) * 100
- spc_attr = f' spc="{int(spc_val)}"'
- except ValueError:
- pass
-
- # Text rotation
- text_rot = 0
- text_transform = elem.get('transform', '')
- if text_transform:
- rot_match = re.search(r'rotate\(\s*([-\d.]+)', text_transform)
- if rot_match:
- text_rot = int(float(rot_match.group(1)) * ANGLE_UNIT)
-
- # Alignment
- algn_map = {'start': 'l', 'middle': 'ctr', 'end': 'r'}
- algn = algn_map.get(text_anchor, 'l')
-
- # Shadow effect from filter
- effect_xml = ''
- filt_id = get_effective_filter_id(elem, ctx)
- if filt_id and filt_id in ctx.defs:
- effect_xml = build_shadow_xml(ctx.defs[filt_id])
-
- shape_id = ctx.next_id()
- rot_attr = f' rot="{text_rot}"' if text_rot else ''
-
- # Build runs XML (pass ctx for gradient support)
- runs_xml = '\n'.join(_build_run_xml(r, fonts, ctx) for r in runs)
-
- return f'''<p:sp>
-<p:nvSpPr>
-<p:cNvPr id="{shape_id}" name="TextBox {shape_id}"/>
-<p:cNvSpPr txBox="1"/><p:nvPr/>
-</p:nvSpPr>
-<p:spPr>
-<a:xfrm{rot_attr}><a:off x="{px_to_emu(box_x)}" y="{px_to_emu(box_y)}"/>
-<a:ext cx="{px_to_emu(box_w)}" cy="{px_to_emu(box_h)}"/></a:xfrm>
-<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
-<a:noFill/>
-<a:ln><a:noFill/></a:ln>
-{effect_xml}
-</p:spPr>
-<p:txBody>
-<a:bodyPr wrap="none" lIns="0" tIns="0" rIns="0" bIns="0" anchor="t" anchorCtr="0">
-<a:spAutoFit/>
-</a:bodyPr>
-<a:lstStyle/>
-<a:p>
-<a:pPr algn="{algn}"/>
-{runs_xml}
-</a:p>
-</p:txBody>
-</p:sp>'''
-
-
-def convert_image(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <image> to DrawingML picture element."""
- href = elem.get('href') or elem.get(f'{{{XLINK_NS}}}href')
- if not href:
- return ''
-
- x = ctx_x(_f(elem.get('x')), ctx)
- y = ctx_y(_f(elem.get('y')), ctx)
- w = ctx_w(_f(elem.get('width')), ctx)
- h = ctx_h(_f(elem.get('height')), ctx)
-
- if w <= 0 or h <= 0:
- return ''
-
- # Extract image data
- if href.startswith('data:'):
- # data:image/png;base64,iVBOR...
- match = re.match(r'data:image/(\w+);base64,(.+)', href, re.DOTALL)
- if not match:
- return ''
- img_format = match.group(1).lower()
- if img_format == 'jpeg':
- img_format = 'jpg'
- img_data = base64.b64decode(match.group(2))
- else:
- # External file reference - resolve relative to SVG directory
- if ctx.svg_dir is None:
- return ''
- img_path = ctx.svg_dir / href
- if not img_path.exists():
- # Also try relative to project root (parent of svg_final/)
- img_path = ctx.svg_dir.parent / href
- if not img_path.exists():
- print(f' Warning: External image not found: {href}')
- return ''
- img_format = img_path.suffix.lstrip('.').lower()
- if img_format == 'jpeg':
- img_format = 'jpg'
- img_data = img_path.read_bytes()
-
- # Generate filename and relationship
- img_idx = len(ctx.media_files) + 1
- img_filename = f's{ctx.slide_num}_img{img_idx}.{img_format}'
- ctx.media_files[img_filename] = img_data
-
- r_id = ctx.next_rel_id()
- ctx.rel_entries.append({
- 'id': r_id,
- 'type': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image',
- 'target': f'../media/{img_filename}',
- })
-
- shape_id = ctx.next_id()
-
- return f'''<p:pic>
-<p:nvPicPr>
-<p:cNvPr id="{shape_id}" name="Image {shape_id}"/>
-<p:cNvPicPr><a:picLocks noChangeAspect="1"/></p:cNvPicPr>
-<p:nvPr/>
-</p:nvPicPr>
-<p:blipFill>
-<a:blip r:embed="{r_id}"/>
-<a:stretch><a:fillRect/></a:stretch>
-</p:blipFill>
-<p:spPr>
-<a:xfrm><a:off x="{px_to_emu(x)}" y="{px_to_emu(y)}"/>
-<a:ext cx="{px_to_emu(w)}" cy="{px_to_emu(h)}"/></a:xfrm>
-<a:prstGeom prst="rect"><a:avLst/></a:prstGeom>
-</p:spPr>
-</p:pic>'''
-
-
-def convert_ellipse(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <ellipse> to DrawingML ellipse shape."""
- cx_ = ctx_x(_f(elem.get('cx')), ctx)
- cy_ = ctx_y(_f(elem.get('cy')), ctx)
- rx = _f(elem.get('rx')) * ctx.scale_x
- ry = _f(elem.get('ry')) * ctx.scale_y
-
- if rx <= 0 or ry <= 0:
- return ''
-
- x = cx_ - rx
- y = cy_ - ry
- w = rx * 2
- h = ry * 2
-
- fill_op = get_fill_opacity(elem, ctx)
- stroke_op = get_stroke_opacity(elem, ctx)
- fill = build_fill_xml(elem, ctx, fill_op)
- stroke = build_stroke_xml(elem, ctx, stroke_op)
-
- geom = '<a:prstGeom prst="ellipse"><a:avLst/></a:prstGeom>'
-
- shape_id = ctx.next_id()
- return _wrap_shape(
- shape_id, f'Ellipse {shape_id}',
- px_to_emu(x), px_to_emu(y), px_to_emu(w), px_to_emu(h),
- geom, fill, stroke
- )
-
-
-# ---------------------------------------------------------------------------
-# Group handling
-# ---------------------------------------------------------------------------
-
-def parse_transform(transform_str: str) -> Tuple[float, float, float, float]:
- """Parse transform string, extract translate and scale. Returns (dx, dy, sx, sy)."""
- if not transform_str:
- return 0.0, 0.0, 1.0, 1.0
-
- dx, dy = 0.0, 0.0
- sx, sy = 1.0, 1.0
- m = re.search(r'translate\(\s*([-\d.]+)[\s,]+([-\d.]+)\s*\)', transform_str)
- if m:
- dx = float(m.group(1))
- dy = float(m.group(2))
- m = re.search(r'scale\(\s*([-\d.]+)(?:[\s,]+([-\d.]+))?\s*\)', transform_str)
- if m:
- sx = float(m.group(1))
- sy = float(m.group(2)) if m.group(2) else sx
- return dx, dy, sx, sy
-
-
-def _extract_shape_bounds_emu(shape_xml: str) -> Optional[Tuple[int, int, int, int]]:
- """Extract bounds (x, y, x+cx, y+cy) in EMU from a shape XML string.
-
- Works for <p:sp>, <p:pic>, and <p:grpSp> — the first <a:off> and <a:ext>
- in each is the element's own position/size on the slide.
- """
- off_match = re.search(r'<a:off x="(-?\d+)" y="(-?\d+)"', shape_xml)
- ext_match = re.search(r'<a:ext cx="(\d+)" cy="(\d+)"', shape_xml)
- if off_match and ext_match:
- x = int(off_match.group(1))
- y = int(off_match.group(2))
- cx = int(ext_match.group(1))
- cy = int(ext_match.group(2))
- return (x, y, x + cx, y + cy)
- return None
-
-
-def convert_g(elem: ET.Element, ctx: ConvertContext) -> str:
- """Convert SVG <g> to DrawingML group shape <p:grpSp>.
-
- Preserves group structure so elements can be selected and moved together
- in PowerPoint. Single-child groups are flattened to avoid unnecessary nesting.
-
- Uses identity coordinate mapping (chOff/chExt == off/ext) so child shapes
- keep their absolute slide coordinates unchanged.
- """
- transform = elem.get('transform', '')
- dx, dy, sx, sy = parse_transform(transform)
-
- # Check for filter on the group
- filter_id = resolve_url_id(elem.get('filter', ''))
-
- # Extract all inheritable styles from this <g>
- style_overrides = _extract_inheritable_styles(elem)
-
- child_ctx = ctx.child(dx, dy, sx, sy, filter_id, style_overrides)
-
- # Convert all children
- child_shapes = []
- for child in elem:
- shape_xml = convert_element(child, child_ctx)
- if shape_xml:
- child_shapes.append(shape_xml)
-
- ctx.sync_from_child(child_ctx)
-
- if not child_shapes:
- return ''
-
- # Single child: flatten (no need for group wrapper)
- if len(child_shapes) == 1:
- return child_shapes[0]
-
- # Multiple children: wrap in <p:grpSp>
- # Calculate group bounds from child shapes (in EMU)
- min_x = min_y = float('inf')
- max_x = max_y = float('-inf')
-
- for shape_xml in child_shapes:
- bounds = _extract_shape_bounds_emu(shape_xml)
- if bounds:
- min_x = min(min_x, bounds[0])
- min_y = min(min_y, bounds[1])
- max_x = max(max_x, bounds[2])
- max_y = max(max_y, bounds[3])
-
- if min_x == float('inf'):
- # No shapes with extractable bounds — fall back to flat join
- return '\n'.join(child_shapes)
-
- group_x = int(min_x)
- group_y = int(min_y)
- group_w = max(int(max_x - min_x), 1)
- group_h = max(int(max_y - min_y), 1)
-
- shapes_xml = '\n'.join(child_shapes)
- group_id = ctx.next_id()
-
- # Shadow effect from filter on the group
- group_effect = ''
- if filter_id and filter_id in ctx.defs:
- group_effect = build_shadow_xml(ctx.defs[filter_id])
-
- # Identity coordinate mapping: chOff/chExt mirrors off/ext
- # so children keep their absolute slide coordinates as-is.
- return f'''<p:grpSp>
-<p:nvGrpSpPr>
-<p:cNvPr id="{group_id}" name="Group {group_id}"/>
-<p:cNvGrpSpPr/>
-<p:nvPr/>
-</p:nvGrpSpPr>
-<p:grpSpPr>
-<a:xfrm>
-<a:off x="{group_x}" y="{group_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}"/>
-</a:xfrm>
-{group_effect}
-</p:grpSpPr>
-{shapes_xml}
-</p:grpSp>'''
-
-
-# ---------------------------------------------------------------------------
-# SVG parsing and main dispatch
-# ---------------------------------------------------------------------------
-
-def collect_defs(root: ET.Element) -> Dict[str, ET.Element]:
- """Collect all <defs> children into an {id: element} dictionary."""
- defs = {}
- for defs_elem in root.iter(f'{{{SVG_NS}}}defs'):
- for child in defs_elem:
- elem_id = child.get('id')
- if elem_id:
- defs[elem_id] = child
- # Also check for defs without namespace (some SVGs)
- for defs_elem in root.iter('defs'):
- for child in defs_elem:
- elem_id = child.get('id')
- if elem_id:
- defs[elem_id] = child
- return defs
-
-
-def convert_element(elem: ET.Element, ctx: ConvertContext) -> str:
- """Dispatch SVG element to appropriate converter."""
- tag = elem.tag.replace(f'{{{SVG_NS}}}', '')
-
- converters = {
- 'rect': convert_rect,
- 'circle': convert_circle,
- 'ellipse': convert_ellipse,
- 'line': convert_line,
- 'path': convert_path,
- 'polygon': convert_polygon,
- 'polyline': convert_polyline,
- 'text': convert_text,
- 'image': convert_image,
- 'g': convert_g,
- }
-
- converter = converters.get(tag)
- if converter:
- try:
- return converter(elem, ctx)
- except Exception as e:
- print(f' Warning: Failed to convert <{tag}>: {e}')
- return ''
-
- # Skip known non-visual elements silently
- if tag in ('defs', 'title', 'desc', 'metadata', 'style'):
- return ''
-
- return ''
-
-
-def convert_svg_to_slide_shapes(
- svg_path: Path,
- slide_num: int = 1,
- verbose: bool = False,
-) -> Tuple[str, Dict[str, bytes], List[Dict[str, str]]]:
- """
- Convert an SVG file to a complete DrawingML slide XML.
-
- Args:
- svg_path: Path to the SVG file
- slide_num: Slide number (for naming)
- verbose: Print progress info
-
- Returns:
- (slide_xml, media_files, rel_entries)
- - slide_xml: Complete slide XML string
- - media_files: Dict of {filename: bytes} for media to write
- - rel_entries: List of relationship entries to add
- """
- tree = ET.parse(str(svg_path))
- root = tree.getroot()
-
- # Collect defs
- defs = collect_defs(root)
-
- # Create context
- ctx = ConvertContext(defs=defs, slide_num=slide_num, svg_dir=Path(svg_path).parent)
-
- # Convert all top-level elements
- shapes = []
- converted = 0
- skipped = 0
-
- for child in root:
- tag = child.tag.replace(f'{{{SVG_NS}}}', '')
- if tag == 'defs':
- continue
- result = convert_element(child, ctx)
- if result:
- shapes.append(result)
- converted += 1
- else:
- if tag not in ('title', 'desc', 'metadata', 'style', 'defs'):
- skipped += 1
-
- if verbose:
- print(f' Converted {converted} elements, skipped {skipped}')
-
- shapes_xml = '\n'.join(shapes)
-
- # Build complete slide XML
- slide_xml = f'''<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
-<p:sld xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"
- xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"
- xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main">
-<p:cSld>
-<p:spTree>
-<p:nvGrpSpPr>
-<p:cNvPr id="1" name=""/>
-<p:cNvGrpSpPr/><p:nvPr/>
-</p:nvGrpSpPr>
-<p:grpSpPr>
-<a:xfrm><a:off x="0" y="0"/><a:ext cx="0" cy="0"/>
-<a:chOff x="0" y="0"/><a:chExt cx="0" cy="0"/></a:xfrm>
-</p:grpSpPr>
-{shapes_xml}
-</p:spTree>
-</p:cSld>
-<p:clrMapOvr><a:masterClrMapping/></p:clrMapOvr>
-</p:sld>'''
-
- return slide_xml, ctx.media_files, ctx.rel_entries