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(gradients): preserve stop position precision

hugohe331b5dec4ac24
diff --git a/skills/ppt-master/references/shared-standards.md b/skills/ppt-master/references/shared-standards.md
index 77ca84c1..729fe912 100644
--- a/skills/ppt-master/references/shared-standards.md
+++ b/skills/ppt-master/references/shared-standards.md
@@ -737,6 +737,10 @@ Linear export preserves stops/alpha/direction but reduces coordinates to an
angle. Radial export becomes a centered circular gradient and does not preserve
`cx/cy/r/fx/fy`. Gradient strokes remain editable, but PPTX-to-SVG re-import may
retain only the first stop. Stop alpha and element opacity multiply.
+PPTX import writes stop offsets with five decimal places, and export parses the
+shared unitless/percentage grammar before half-up quantization to 1/100000.
+Thus small positions and values such as `1%` round-trip without truncation or
+percentage reinterpretation.
The quality checker and exporter preflight both validate definition location,
references, gradient structure, and paint context from the same closed contract.
diff --git a/skills/ppt-master/scripts/pptx_to_svg/emu_units.py b/skills/ppt-master/scripts/pptx_to_svg/emu_units.py
index 45478d39..ab481204 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/emu_units.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/emu_units.py
@@ -238,9 +238,14 @@ def _fmt(val: float, ndigits: int = 2) -> str:
fmt_num = _fmt
+def format_ooxml_unit_ratio(value: float) -> str:
+ """Format a normalized ratio without losing 1/100000 OOXML precision."""
+ return _fmt(value, 5)
+
+
def format_ooxml_alpha(alpha: float) -> str:
- """Format a normalized alpha without losing 1/100000 OOXML precision."""
- return _fmt(alpha, 5)
+ """Format one normalized OOXML alpha ratio."""
+ return format_ooxml_unit_ratio(alpha)
def format_canvas_px_from_emu(emu: int) -> str:
diff --git a/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py b/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
index 1234c670..ba8f0a3b 100644
--- a/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
+++ b/skills/ppt-master/scripts/pptx_to_svg/fill_to_svg.py
@@ -17,7 +17,13 @@ from dataclasses import dataclass, field
from xml.etree import ElementTree as ET
from .color_resolver import ColorPalette, find_color_elem, resolve_color
-from .emu_units import NS, fmt_num, format_ooxml_alpha, percent_to_ratio
+from .emu_units import (
+ NS,
+ fmt_num,
+ format_ooxml_alpha,
+ format_ooxml_unit_ratio,
+ percent_to_ratio,
+)
@dataclass
@@ -129,7 +135,8 @@ def _resolve_grad_fill(elem: ET.Element, palette: ColorPalette | None,
else ""
)
stops_xml.append(
- f'<stop offset="{fmt_num(pos_pct, 4)}" stop-color="{hex_}"{opacity_attr}/>'
+ f'<stop offset="{format_ooxml_unit_ratio(pos_pct)}" '
+ f'stop-color="{hex_}"{opacity_attr}/>'
)
if not stops_xml:
return FillResult.inherit()
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
index 67e42e9d..426d0860 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
@@ -16,8 +16,9 @@ from .utils import (
classify_project_marker_shape,
matrix_multiply, parse_svg_color, parse_transform_matrix, resolve_url_id,
parse_project_filter_params, project_filter_drawingml_coordinates,
+ parse_project_gradient_ratio,
parse_project_stroke_dasharray, parse_project_stroke_enum,
- quantize_ooxml_alpha,
+ quantize_ooxml_alpha, quantize_ooxml_unit_ratio,
)
@@ -53,14 +54,11 @@ def build_gradient_fill(
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)
+ offset_str = child.get('offset')
+ if offset_str is None:
+ raise ValueError('Gradient stop requires an explicit offset')
+ offset = parse_project_gradient_ratio(offset_str)
+ pos = quantize_ooxml_unit_ratio(offset)
# Parse color from style attribute or direct attributes
style = child.get('style', '')
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
index 411d472e..58664c0c 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
@@ -1533,13 +1533,20 @@ def parse_opacity(
return parse_project_opacity(raw, allow_percentage=allow_percentage)
+def quantize_ooxml_unit_ratio(value: float) -> int:
+ """Quantize one normalized ratio to DrawingML 1/100000 units."""
+ if not math.isfinite(value):
+ raise ValueError(f'OOXML unit ratio must be finite; got {value!r}')
+ normalized = max(0.0, min(1.0, value))
+ scaled = Decimal(str(normalized)) * Decimal(100000)
+ return int(scaled.to_integral_value(rounding=ROUND_HALF_UP))
+
+
def quantize_ooxml_alpha(opacity: float) -> int:
"""Quantize one normalized alpha to DrawingML 1/100000 units."""
if not math.isfinite(opacity):
raise ValueError(f'Opacity must be finite; got {opacity!r}')
- normalized = max(0.0, min(1.0, opacity))
- scaled = Decimal(str(normalized)) * Decimal(100000)
- return int(scaled.to_integral_value(rounding=ROUND_HALF_UP))
+ return quantize_ooxml_unit_ratio(opacity)
def _functional_color_parts(body: str) -> tuple[list[str], str | None]:
diff --git a/skills/ppt-master/scripts/test_svg_quality_checker.py b/skills/ppt-master/scripts/test_svg_quality_checker.py
index 5f63a0b9..67195aac 100644
--- a/skills/ppt-master/scripts/test_svg_quality_checker.py
+++ b/skills/ppt-master/scripts/test_svg_quality_checker.py
@@ -44,6 +44,7 @@ from pptx_to_svg.effect_to_svg import (
convert_effects,
unsupported_target_effect_metadata,
)
+from pptx_to_svg.fill_to_svg import resolve_fill
from pptx_to_svg.preset_registry_to_svg import render_preset_geometry
from pptx_to_svg.preset_svg_markup import serialize_preset_layers
from pptx_to_svg.ln_to_svg import _build_arrow_marker, resolve_stroke
@@ -64,7 +65,11 @@ from svg_to_pptx.drawingml.converter import (
convert_svg_to_slide_shapes,
)
from svg_to_pptx.drawingml.context import ConvertContext
-from svg_to_pptx.drawingml.styles import build_stroke_xml, get_stroke_opacity
+from svg_to_pptx.drawingml.styles import (
+ build_gradient_fill,
+ build_stroke_xml,
+ get_stroke_opacity,
+)
from svg_to_pptx.pptx_package.builder import create_pptx_with_native_svg
from svg_to_pptx.pptx_package.dimensions import (
CANVAS_FORMATS as PACKAGE_CANVAS_FORMATS,
@@ -77,6 +82,7 @@ from svg_to_pptx.drawingml.utils import (
parse_inline_style,
project_filter_errors,
quantize_ooxml_alpha,
+ quantize_ooxml_unit_ratio,
split_project_text_clusters,
)
from svg_to_pptx.native_objects import (
@@ -2778,6 +2784,35 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
stroke_xml,
)
+ def test_native_gradient_stop_position_round_trips(self):
+ for position in (1, 7, 13, 33333, 99999):
+ with self.subTest(position=position):
+ sp_pr = ET.fromstring(f'''
+<p:spPr xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main"
+ xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
+ <a:gradFill>
+ <a:gsLst>
+ <a:gs pos="{position}"><a:srgbClr val="112233"/></a:gs>
+ </a:gsLst>
+ <a:lin ang="0"/>
+ </a:gradFill>
+</p:spPr>''')
+ fill = resolve_fill(sp_pr, None)
+ gradient = ET.fromstring(fill.defs[0])
+ offset = next(iter(gradient)).get('offset')
+ self.assertEqual(
+ quantize_ooxml_unit_ratio(float(offset)),
+ position,
+ )
+ gradient_xml = build_gradient_fill(gradient)
+ self.assertIn(f'<a:gs pos="{position}">', gradient_xml)
+
+ percentage = ET.fromstring('''
+<linearGradient xmlns="http://www.w3.org/2000/svg">
+ <stop offset="1%" stop-color="#112233"/>
+</linearGradient>''')
+ self.assertIn('<a:gs pos="1000">', build_gradient_fill(percentage))
+
def test_compound_native_line_is_rejected_on_import(self):
for compound in ('dbl', 'thickThin', 'thinThick', 'tri'):
with self.subTest(compound=compound):