Skip to content

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

fix(svg-to-pptx): round DrawingML alpha consistently

hugohe3c6a083208f60
diff --git a/skills/ppt-master/references/shared-standards.md b/skills/ppt-master/references/shared-standards.md
index 2bb175ca..77ca84c1 100644
--- a/skills/ppt-master/references/shared-standards.md
+++ b/skills/ppt-master/references/shared-standards.md
@@ -998,7 +998,9 @@ fidelity.
PPTX import formats normalized paint and text alpha with five decimal places,
matching DrawingML's 1/100000 target precision. The smallest positive source
alpha therefore remains non-zero and quantizes back instead of becoming fully
-transparent.
+transparent. Export uses one clamped half-up quantizer for every DrawingML
+`alpha`, `alphaMod`, and `alphaModFix` channel; it never truncates a decimal
+ratio one unit below its intended value.
PPTX import accepts `a:ln@w` only as an integer DrawingML `ST_LineWidth` value
from `0` through `20116800` EMU. Malformed, negative, or oversized source widths
diff --git a/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py b/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
index 94354bb0..1fc1585a 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/elements.py
@@ -57,6 +57,7 @@ from .utils import (
parse_project_image_aspect_ratio,
parse_project_opacity,
parse_project_stroke_dasharray,
+ quantize_ooxml_alpha,
project_definition_index,
matrix_multiply, parse_transform_matrix, parse_transform_operations,
transform_point, _xml_escape,
@@ -2225,7 +2226,7 @@ def _build_bullet_xml(
if color:
opacity = combine_opacity(bullet.get('opacity'), color_alpha)
alpha_xml = (
- f'<a:alphaMod val="{int(opacity * 100000)}"/>'
+ f'<a:alphaMod val="{quantize_ooxml_alpha(opacity)}"/>'
if opacity is not None else ''
)
theme_spec = ctx.theme_color_spec if ctx is not None else None
@@ -2473,7 +2474,7 @@ def _build_text_fill_xml(
opacity = combine_opacity(opacity, color_alpha)
alpha_xml = ''
if opacity is not None:
- alpha_xml = f'<a:alphaMod val="{int(opacity * 100000)}"/>'
+ alpha_xml = f'<a:alphaMod val="{quantize_ooxml_alpha(opacity)}"/>'
theme_spec = ctx.theme_color_spec if ctx is not None else None
return (
'<a:solidFill>'
@@ -2499,7 +2500,9 @@ def _build_text_outline_xml(
stroke_opacity = combine_opacity(run.get('stroke_opacity'), color_alpha)
alpha_xml = ''
if stroke_opacity is not None:
- alpha_xml = f'<a:alphaMod val="{int(stroke_opacity * 100000)}"/>'
+ alpha_xml = (
+ f'<a:alphaMod val="{quantize_ooxml_alpha(stroke_opacity)}"/>'
+ )
theme_spec = ctx.theme_color_spec if ctx is not None else None
return (
@@ -3793,7 +3796,7 @@ def _build_image_blip_xml(r_id: str, opacity: float | None) -> str:
"""Build an image blip with native DrawingML transparency when requested."""
if opacity is None:
return f'<a:blip r:embed="{r_id}"/>'
- alpha = int(round(opacity * 100000))
+ alpha = quantize_ooxml_alpha(opacity)
return (
f'<a:blip r:embed="{r_id}">'
f'<a:alphaModFix amt="{alpha}"/>'
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 16f34984..67e42e9d 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/styles.py
@@ -17,6 +17,7 @@ from .utils import (
matrix_multiply, parse_svg_color, parse_transform_matrix, resolve_url_id,
parse_project_filter_params, project_filter_drawingml_coordinates,
parse_project_stroke_dasharray, parse_project_stroke_enum,
+ quantize_ooxml_alpha,
)
@@ -29,7 +30,7 @@ def build_solid_fill(
"""Build <a:solidFill> XML."""
alpha = ''
if opacity is not None and opacity < 1.0:
- alpha = f'<a:alpha val="{int(opacity * 100000)}"/>'
+ alpha = f'<a:alpha val="{quantize_ooxml_alpha(opacity)}"/>'
return (
'<a:solidFill>'
f'{color_node_xml(color, theme_color_spec, usage, alpha)}'
@@ -81,7 +82,9 @@ def build_gradient_fill(
alpha_xml = ''
effective_opacity = combine_opacity(stop_opacity, opacity)
if effective_opacity is not None:
- alpha_xml = f'<a:alpha val="{int(effective_opacity * 100000)}"/>'
+ alpha_xml = (
+ f'<a:alpha val="{quantize_ooxml_alpha(effective_opacity)}"/>'
+ )
stops_xml.append(
f'<a:gs pos="{pos}">'
@@ -277,11 +280,11 @@ def build_pattern_fill(
fg_opacity = combine_opacity(opacity, fg_alpha, fg_child_opacity)
bg_opacity = combine_opacity(opacity, bg_alpha, bg_child_opacity)
fg_alpha_xml = (
- f'<a:alpha val="{int(fg_opacity * 100000)}"/>'
+ f'<a:alpha val="{quantize_ooxml_alpha(fg_opacity)}"/>'
if fg_opacity is not None else ''
)
bg_alpha_xml = (
- f'<a:alpha val="{int(bg_opacity * 100000)}"/>'
+ f'<a:alpha val="{quantize_ooxml_alpha(bg_opacity)}"/>'
if bg_opacity is not None else ''
)
@@ -527,7 +530,7 @@ def build_stroke_xml(
opacity = combine_opacity(opacity, color_alpha)
alpha_xml = ''
if opacity is not None and opacity < 1.0:
- alpha_xml = f'<a:alpha val="{int(opacity * 100000)}"/>'
+ alpha_xml = f'<a:alpha val="{quantize_ooxml_alpha(opacity)}"/>'
color_xml = color_node_xml(color, ctx.theme_color_spec, "stroke", alpha_xml)
return f'''<a:ln w="{width_emu}"{cap_attr}>
@@ -608,7 +611,9 @@ def build_shadow_xml(
# composite (different blending path). Scale by 0.75 to match the SVG
# preview after blur has been corrected to 2.0× σ.
opacity_multiplier = 1.0 if opacity is None else opacity
- alpha_val = int(round(p['opacity'] * opacity_multiplier * 75000))
+ alpha_val = quantize_ooxml_alpha(
+ p['opacity'] * opacity_multiplier * 0.75
+ )
algn = _infer_shadow_alignment(dx, dy)
return f'''<a:effectLst>
@@ -633,7 +638,7 @@ def build_glow_xml(
p = parse_project_filter_params(filter_elem)
rad = project_filter_drawingml_coordinates(p, 'glow')['rad']
opacity_multiplier = 1.0 if opacity is None else opacity
- alpha_val = int(round(p['opacity'] * opacity_multiplier * 100000))
+ alpha_val = quantize_ooxml_alpha(p['opacity'] * opacity_multiplier)
return f'''<a:effectLst>
<a:glow rad="{rad}">
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 47d673f5..411d472e 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
@@ -12,6 +12,7 @@ import re
import unicodedata
from collections import Counter
from collections.abc import Iterator
+from decimal import Decimal, ROUND_HALF_UP
from xml.etree import ElementTree as ET
from pptx_shapes import (
@@ -1532,6 +1533,15 @@ def parse_opacity(
return parse_project_opacity(raw, allow_percentage=allow_percentage)
+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))
+
+
def _functional_color_parts(body: str) -> tuple[list[str], str | None]:
"""Split legacy comma or modern space/slash functional color syntax."""
before, separator, after = body.partition('/')
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 18f607e8..764b43a6 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
@@ -9,7 +9,13 @@ from xml.etree import ElementTree as ET
from .marker_attributes import native_import_source
from ..drawingml.context import ConvertContext
-from ..drawingml.utils import detect_text_lang, parse_font_family, px_to_emu, _xml_escape
+from ..drawingml.utils import (
+ _xml_escape,
+ detect_text_lang,
+ parse_font_family,
+ px_to_emu,
+ quantize_ooxml_alpha,
+)
from .chart_data import _DEFAULT_CHART_COLORS
from .marker_common import (
_bool_attr,
@@ -365,7 +371,7 @@ def _alpha_xml(value: Any, field_name: str = "fill_opacity") -> str:
raise RuntimeError(f"Native PPTX chart {field_name} must be finite")
if alpha < 0 or alpha > 1:
raise RuntimeError(f"Native PPTX chart {field_name} must be between 0 and 1")
- return f'<a:alpha val="{int(round(alpha * 100000))}"/>'
+ return f'<a:alpha val="{quantize_ooxml_alpha(alpha)}"/>'
def _axis_title_xml(
diff --git a/skills/ppt-master/scripts/test_svg_quality_checker.py b/skills/ppt-master/scripts/test_svg_quality_checker.py
index 673e0d38..5f63a0b9 100644
--- a/skills/ppt-master/scripts/test_svg_quality_checker.py
+++ b/skills/ppt-master/scripts/test_svg_quality_checker.py
@@ -76,6 +76,7 @@ from svg_to_pptx.drawingml.utils import (
estimate_text_width,
parse_inline_style,
project_filter_errors,
+ quantize_ooxml_alpha,
split_project_text_clusters,
)
from svg_to_pptx.native_objects import (
@@ -2740,29 +2741,42 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
def test_minimum_positive_native_alpha_round_trips(self):
self.assertEqual(format_ooxml_alpha(0.00001), '0.00001')
self.assertEqual(format_ooxml_alpha(0.99999), '0.99999')
- sp_pr = ET.fromstring('''
+ self.assertEqual(quantize_ooxml_alpha(0.000005), 1)
+ self.assertEqual(quantize_ooxml_alpha(0.000015), 2)
+ for alpha_value in (1, 7, 13, 33333, 99999):
+ with self.subTest(alpha_value=alpha_value):
+ opacity = format_ooxml_alpha(alpha_value / 100000)
+ self.assertEqual(
+ quantize_ooxml_alpha(float(opacity)),
+ alpha_value,
+ )
+ 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:ln>
<a:solidFill>
- <a:srgbClr val="112233"><a:alpha val="1"/></a:srgbClr>
+ <a:srgbClr val="112233"><a:alpha val="{alpha_value}"/></a:srgbClr>
</a:solidFill>
</a:ln>
</p:spPr>''')
- stroke = resolve_stroke(sp_pr, None)
- opacity = stroke.attrs['stroke-opacity']
- self.assertEqual(opacity, '0.00001')
- line = ET.fromstring(
- '<line xmlns="http://www.w3.org/2000/svg" '
- f'stroke="#112233" stroke-opacity="{opacity}"/>'
- )
- context = ConvertContext()
- stroke_xml = build_stroke_xml(
- line,
- context,
- get_stroke_opacity(line, context),
- )
- self.assertIn('<a:alpha val="1"/>', stroke_xml)
+ stroke = resolve_stroke(sp_pr, None)
+ imported_opacity = stroke.attrs['stroke-opacity']
+ self.assertEqual(imported_opacity, opacity)
+ line = ET.fromstring(
+ '<line xmlns="http://www.w3.org/2000/svg" '
+ f'stroke="#112233" '
+ f'stroke-opacity="{imported_opacity}"/>'
+ )
+ context = ConvertContext()
+ stroke_xml = build_stroke_xml(
+ line,
+ context,
+ get_stroke_opacity(line, context),
+ )
+ self.assertIn(
+ f'<a:alpha val="{alpha_value}"/>',
+ stroke_xml,
+ )
def test_compound_native_line_is_rejected_on_import(self):
for compound in ('dbl', 'thickThin', 'thinThick', 'tri'):