Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

fix(native-charts): preserve authored plot and title geometry

hugohe31704c2921616
diff --git a/skills/ppt-master/scripts/svg_to_pptx/native_objects/__init__.py b/skills/ppt-master/scripts/svg_to_pptx/native_objects/__init__.py
index 1a0b91e0..bae05579 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/native_objects/__init__.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/native_objects/__init__.py
@@ -8,12 +8,13 @@ from xml.etree import ElementTree as ET
from ..drawingml.context import ConvertContext, ShapeResult
from ..drawingml.utils import _xml_escape
-from .chart_data import _chart_data
+from .chart_data import _chart_data, _chart_plot_area_layout
from .chart_style import (
_axis_titles,
_chart_companion_entries,
_chart_companion_text_xml,
_chart_text_sizes,
+ _chart_title_is_bounded,
_classic_chart_style,
_native_chart_chrome_errors,
_native_chart_chrome_warnings,
@@ -159,6 +160,7 @@ def _build_native_chart(elem: ET.Element, ctx: ConvertContext, payload: dict[str
chart_data=chart_data,
inherited_styles=ctx.inherited_styles,
primary_language=ctx.primary_language,
+ chart_bounds=(off_x, off_y, ext_cx, ext_cy),
)
ctx.package_files[chart_rels_part] = _chart_rels_xml(f"../embeddings/{workbook_name}")
if chart_data["kind"] == "xy":
@@ -190,7 +192,10 @@ def _build_native_chart(elem: ET.Element, ctx: ConvertContext, payload: dict[str
chart_style=chart_style,
note_font_size=text_sizes["note"],
title_font_size=text_sizes["title"],
- include_title=chart_data["kind"] == "chartex",
+ include_title=(
+ chart_data["kind"] == "chartex"
+ or _chart_title_is_bounded(payload)
+ ),
include_subtitle_as_caption=chart_data["kind"] == "chartex",
)
xml = chart_frame_xml + companion_xml
@@ -238,10 +243,17 @@ def _validate_native_object_marker_payload(
)
elif kind == "chart":
chart_data = _chart_data(payload)
+ _chart_plot_area_layout(
+ chart_data,
+ (off_x, off_y, ext_cx, ext_cy),
+ )
_validate_chart_companion_boxes(
payload,
chart_bounds=(off_x, off_y, ext_cx, ext_cy),
- include_title=chart_data["kind"] == "chartex",
+ include_title=(
+ chart_data["kind"] == "chartex"
+ or _chart_title_is_bounded(payload)
+ ),
include_subtitle_as_caption=chart_data["kind"] == "chartex",
)
if validate_chrome and native_import_source(elem) != "pptx":
diff --git a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_data.py b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_data.py
index 92c42992..605f40a5 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_data.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_data.py
@@ -12,6 +12,7 @@ from .marker_common import (
_first_present,
_hex_or_none,
_number,
+ _powerpoint_emu,
_powerpoint_line_width_emu,
)
@@ -235,6 +236,8 @@ _AXIS_ROLE_DEFAULTS = {
def _chart_axes(
payload: dict[str, Any],
allowed_roles: set[str],
+ *,
+ bar_orientation: bool = False,
) -> dict[str, dict[str, Any]]:
"""Normalize the narrow classic-chart axis contract."""
raw_axes = payload.get("axes")
@@ -264,6 +267,10 @@ def _chart_axes(
f"Native PPTX chart axes.{role} contains unsupported field(s): {fields}"
)
default_kind, default_position = _AXIS_ROLE_DEFAULTS[role]
+ if bar_orientation and role == "category":
+ default_position = "left"
+ elif bar_orientation and role == "value":
+ default_position = "bottom"
kind = _compact_key(raw_config.get("kind") or default_kind)
if kind not in {"date", "text", "value"}:
raise RuntimeError(
@@ -291,11 +298,16 @@ def _chart_axes(
raise RuntimeError(
f"Native PPTX chart axes.{role}.position must be bottom, left, right, or top"
)
- allowed_positions = (
- {"bottom", "top"}
- if role in {"category", "secondary_category", "x"}
- else {"left", "right"}
- )
+ if bar_orientation and role == "category":
+ allowed_positions = {"left", "right"}
+ elif bar_orientation and role == "value":
+ allowed_positions = {"bottom", "top"}
+ else:
+ allowed_positions = (
+ {"bottom", "top"}
+ if role in {"category", "secondary_category", "x"}
+ else {"left", "right"}
+ )
if position not in allowed_positions:
choices = ", ".join(sorted(allowed_positions))
raise RuntimeError(
@@ -372,6 +384,101 @@ def _category_axis_is_date(axes: dict[str, dict[str, Any]]) -> bool:
return axes.get("category", {}).get("kind") == "date"
+def _chart_plot_area(payload: dict[str, Any]) -> dict[str, float] | None:
+ """Normalize an optional absolute slide-local plot-area box."""
+ raw = payload.get("plot_area")
+ if raw is None:
+ return None
+ if not isinstance(raw, dict):
+ raise RuntimeError("Native PPTX chart plot_area must be an object")
+
+ box_keys = {"x", "y", "width", "height"}
+ unknown_keys = set(raw) - box_keys
+ if unknown_keys:
+ fields = ", ".join(sorted(unknown_keys))
+ raise RuntimeError(
+ f"Native PPTX chart plot_area contains unsupported field(s): {fields}"
+ )
+ missing_keys = box_keys - set(raw)
+ if missing_keys:
+ fields = ", ".join(sorted(missing_keys))
+ raise RuntimeError(
+ "Native PPTX chart plot_area requires x/y/width/height together; "
+ f"missing: {fields}"
+ )
+
+ plot_area = {
+ key: _number(raw[key], f"chart plot_area.{key}")
+ for key in ("x", "y", "width", "height")
+ }
+ if plot_area["width"] <= 0 or plot_area["height"] <= 0:
+ raise RuntimeError("Native PPTX chart plot_area width/height must be positive")
+ return plot_area
+
+
+def _chart_plot_area_layout(
+ chart_data: dict[str, Any],
+ chart_bounds: tuple[int, int, int, int],
+) -> tuple[float, float, float, float] | None:
+ """Resolve an absolute plot-area box to chart-relative manual-layout factors."""
+ plot_area = chart_data.get("plot_area")
+ if plot_area is None:
+ return None
+
+ chart_x, chart_y, chart_width, chart_height = chart_bounds
+ plot_x = _powerpoint_emu(plot_area["x"], "chart plot_area.x")
+ plot_y = _powerpoint_emu(plot_area["y"], "chart plot_area.y")
+ plot_width = _powerpoint_emu(
+ plot_area["width"],
+ "chart plot_area.width",
+ positive=True,
+ )
+ plot_height = _powerpoint_emu(
+ plot_area["height"],
+ "chart plot_area.height",
+ positive=True,
+ )
+ if (
+ plot_x < chart_x
+ or plot_y < chart_y
+ or plot_x + plot_width > chart_x + chart_width
+ or plot_y + plot_height > chart_y + chart_height
+ ):
+ raise RuntimeError(
+ "Native PPTX chart plot_area must be fully contained within the chart frame"
+ )
+ return (
+ (plot_x - chart_x) / chart_width,
+ (plot_y - chart_y) / chart_height,
+ plot_width / chart_width,
+ plot_height / chart_height,
+ )
+
+
+def _doughnut_hole_size(payload: dict[str, Any], chart_type: str) -> int | None:
+ """Normalize the closed doughnut-hole percentage contract."""
+ raw = payload.get("hole_size")
+ if chart_type != "doughnut":
+ if raw is not None:
+ raise RuntimeError(
+ "Native PPTX chart hole_size is supported for doughnut charts only"
+ )
+ return None
+ if raw is None:
+ return 75
+ if not isinstance(raw, (int, float)) or isinstance(raw, bool):
+ raise RuntimeError("Native PPTX doughnut hole_size must be a numeric integer")
+ value = _chart_number(raw)
+ if not float(value).is_integer():
+ raise RuntimeError("Native PPTX doughnut hole_size must be an integer")
+ hole_size = int(value)
+ if not 10 <= hole_size <= 90:
+ raise RuntimeError(
+ "Native PPTX doughnut hole_size must be between 10 and 90"
+ )
+ return hole_size
+
+
def _chart_kind(payload: dict[str, Any]) -> tuple[str, str | None, str | None]:
raw_type = payload.get("type") or payload.get("chart_type") or "column"
key = _compact_key(raw_type)
@@ -655,8 +762,12 @@ def _category_chart_data(
alias_grouping: str | None,
alias_style: str | None,
) -> dict[str, Any]:
- axes = _chart_axes(payload, {"category", "value"})
- if axes and chart_type in {"bar", "doughnut", "of_pie", "pie"}:
+ axes = _chart_axes(
+ payload,
+ {"category", "value"},
+ bar_orientation=chart_type == "bar",
+ )
+ if axes and chart_type in {"doughnut", "of_pie", "pie"}:
raise RuntimeError(
f"Native PPTX {chart_type} chart axes are outside current support"
)
@@ -716,6 +827,7 @@ def _category_chart_data(
"categories": categories,
"grouping": grouping,
"of_pie_type": of_pie_type,
+ "hole_size": _doughnut_hole_size(payload, chart_type),
"line_style": line_style,
"radar_marker_style": radar_marker_style,
"radar_style": radar_style,
@@ -1275,6 +1387,15 @@ def _xy_chart_data(
def _chart_data(payload: dict[str, Any]) -> dict[str, Any]:
chart_type, alias_grouping, alias_style = _chart_kind(payload)
+ if payload.get("hole_size") is not None and chart_type != "doughnut":
+ raise RuntimeError(
+ "Native PPTX chart hole_size is supported for doughnut charts only"
+ )
+ plot_area = _chart_plot_area(payload)
+ if plot_area is not None and chart_type in _CHARTEX_CHART_TYPES:
+ raise RuntimeError(
+ "Native PPTX chart plot_area is supported for classic charts only"
+ )
if (
chart_type not in _CATEGORY_CHART_TYPES | {"combo", "stock"} | _XY_CHART_TYPES
and _data_labels_config(payload) is not None
@@ -1283,14 +1404,27 @@ def _chart_data(payload: dict[str, Any]) -> dict[str, Any]:
f"Native PPTX {chart_type} chart data labels are outside current support"
)
if chart_type == "combo":
- return _combo_chart_data(payload)
+ chart_data = _combo_chart_data(payload)
+ chart_data["plot_area"] = plot_area
+ return chart_data
if chart_type in _CHARTEX_CHART_TYPES:
return _chartex_chart_data(payload, chart_type)
if chart_type == "stock":
- return _stock_chart_data(payload)
+ chart_data = _stock_chart_data(payload)
+ chart_data["plot_area"] = plot_area
+ return chart_data
if chart_type in _XY_CHART_TYPES:
- return _xy_chart_data(payload, chart_type, alias_style)
- return _category_chart_data(payload, chart_type, alias_grouping, alias_style)
+ chart_data = _xy_chart_data(payload, chart_type, alias_style)
+ chart_data["plot_area"] = plot_area
+ return chart_data
+ chart_data = _category_chart_data(
+ payload,
+ chart_type,
+ alias_grouping,
+ alias_style,
+ )
+ chart_data["plot_area"] = plot_area
+ return chart_data
def validate_chart_payload(payload: dict[str, Any]) -> None:
diff --git a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_style.py b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_style.py
index 9b311b72..1bd685fe 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_style.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_style.py
@@ -340,6 +340,12 @@ def _chart_text_entry(value: Any) -> tuple[str, dict[str, Any]] | None:
return str(value).strip(), {}
+def _chart_title_is_bounded(payload: dict[str, Any]) -> bool:
+ """Return whether the classic title requests an explicit companion box."""
+ title = payload.get("title")
+ return isinstance(title, dict) and _chart_companion_box(title) is not None
+
+
def _chart_text_entry_font_size(item: dict[str, Any], fallback: int) -> int:
raw = _first_present(item.get("font_size"), item.get("fontSize"))
if raw is None:
@@ -777,6 +783,16 @@ def _validate_chart_companion_boxes(
include_subtitle_as_caption: bool,
) -> None:
"""Validate companion boxes without allocating shapes or relationships."""
+ title_bounded = _chart_title_is_bounded(payload)
+ if (
+ title_bounded
+ and not include_subtitle_as_caption
+ and _chart_text_entry(payload.get("subtitle")) is not None
+ ):
+ raise RuntimeError(
+ "Native PPTX classic chart bounded title does not support subtitle; "
+ "use a separately bounded caption"
+ )
_, chart_off_y, _, chart_ext_cy = chart_bounds
below_index = 0
for item in _chart_companion_entries(
@@ -808,6 +824,8 @@ def _chart_companion_text_xml(
include_title: bool,
include_subtitle_as_caption: bool,
) -> str:
+ if _chart_title_is_bounded(payload):
+ include_title = True
entries = _chart_companion_entries(
payload,
include_title=include_title,
diff --git a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_xml.py b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_xml.py
index 8ca2a59a..bcc339b0 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_xml.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/native_objects/chart_xml.py
@@ -15,6 +15,7 @@ from .chart_data import (
_DEFAULT_CHART_COLORS,
_category_axis_is_date,
_chart_list,
+ _chart_plot_area_layout,
_data_label_position,
_data_label_point_items,
_data_labels_config,
@@ -30,6 +31,7 @@ from .chart_style import (
_chart_text_entry_font_size,
_chart_text_entry,
_chart_text_sizes,
+ _chart_title_is_bounded,
_chart_tx_pr_xml,
_classic_chart_style,
_font_face_xml,
@@ -495,11 +497,30 @@ def _chart_title_xml(
return (
"<c:title><c:tx><c:rich><a:bodyPr/><a:lstStyle/>"
f"{''.join(paragraphs)}"
- "</c:rich></c:tx><c:layout/></c:title>"
+ "</c:rich></c:tx><c:layout/><c:overlay val=\"0\"/></c:title>"
'<c:autoTitleDeleted val="0"/>'
)
+def _plot_area_layout_xml(
+ chart_data: dict[str, Any],
+ chart_bounds: tuple[int, int, int, int],
+) -> str:
+ layout = _chart_plot_area_layout(chart_data, chart_bounds)
+ if layout is None:
+ return "<c:layout/>"
+ x, y, width, height = layout
+ return (
+ "<c:layout><c:manualLayout>"
+ '<c:layoutTarget val="inner"/>'
+ '<c:xMode val="edge"/><c:yMode val="edge"/>'
+ '<c:wMode val="factor"/><c:hMode val="factor"/>'
+ f'<c:x val="{x:.12g}"/><c:y val="{y:.12g}"/>'
+ f'<c:w val="{width:.12g}"/><c:h val="{height:.12g}"/>'
+ "</c:manualLayout></c:layout>"
+ )
+
+
def _chart_legend_xml(
payload: dict[str, Any],
*,
@@ -1181,7 +1202,8 @@ def _chart_plot_xml(
return (
'<c:doughnutChart><c:varyColors val="1"/>'
f"{ser_xml}"
- '<c:firstSliceAng val="0"/><c:holeSize val="75"/>'
+ '<c:firstSliceAng val="0"/>'
+ f'<c:holeSize val="{chart_data["hole_size"]}"/>'
"</c:doughnutChart>"
)
if chart_type == "of_pie":
@@ -1399,6 +1421,7 @@ def _chart_xml(
*,
chart_rels_id: str,
chart_data: dict[str, Any],
+ chart_bounds: tuple[int, int, int, int],
inherited_styles: dict[str, str] | None = None,
primary_language: str | None = None,
) -> bytes:
@@ -1420,11 +1443,12 @@ def _chart_xml(
axis_titles=axis_titles,
chart_style=chart_style,
)
+ bounded_title = _chart_title_is_bounded(payload)
title_xml = _chart_title_xml(
- payload.get("title"),
+ None if bounded_title else payload.get("title"),
font_size=text_sizes["title"],
color=chart_style.get("text_color"),
- subtitle=payload.get("subtitle"),
+ subtitle=None if bounded_title else payload.get("subtitle"),
subtitle_font_size=text_sizes["subtitle"],
font_face=chart_style.get("font_face"),
primary_language=primary_language,
@@ -1451,7 +1475,7 @@ def _chart_xml(
<c:lang val="{_xml_escape(chart_language)}"/>
<c:chart>
{title_xml}
-<c:plotArea><c:layout/>{plot_xml}{_chart_area_sp_pr_xml(chart_style.get("plot_fill"))}</c:plotArea>
+<c:plotArea>{_plot_area_layout_xml(chart_data, chart_bounds)}{plot_xml}{_chart_area_sp_pr_xml(chart_style.get("plot_fill"))}</c:plotArea>
{legend_xml}
<c:plotVisOnly val="1"/>
<c:dispBlanksAs val="gap"/>