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-checker): measure single-line text against the viewBox

hugohe3b5f47d38e213
diff --git a/skills/ppt-master/scripts/svg_quality_checker.py b/skills/ppt-master/scripts/svg_quality_checker.py
index 63004b26..835e3f03 100644
--- a/skills/ppt-master/scripts/svg_quality_checker.py
+++ b/skills/ppt-master/scripts/svg_quality_checker.py
@@ -60,6 +60,7 @@ try:
PROJECT_OPACITY_PROPERTIES as _OPACITY_PROPERTIES,
PROJECT_PAINT_PROPERTIES as _PAINT_PROPERTIES,
PROJECT_PERCENTAGE_OPACITY_PROPERTIES as _PERCENTAGE_OPACITY_PROPERTIES,
+ estimate_text_width as _estimate_text_width,
format_project_geometry_length as _format_project_geometry_length,
format_project_image_aspect_ratio as _format_project_image_aspect_ratio,
format_project_opacity as _format_project_opacity,
@@ -97,6 +98,7 @@ try:
project_stroke_style_errors as _project_stroke_style_errors,
project_transform_errors as _project_transform_errors,
rect_to_dml_xfrm as _rect_to_dml_xfrm,
+ transform_point as _transform_point,
validate_dml_shape_matrix as _validate_dml_shape_matrix,
)
except ImportError:
@@ -109,6 +111,7 @@ except ImportError:
_format_project_geometry_length = None
_format_project_image_aspect_ratio = None
_format_project_opacity = None
+ _estimate_text_width = None
_font_px_to_hpt = None
_is_canonical_project_geometry_length = None
_is_project_opacity_default_form = None
@@ -143,6 +146,7 @@ except ImportError:
_project_stroke_style_errors = None
_project_transform_errors = None
_rect_to_dml_xfrm = None
+ _transform_point = None
_validate_dml_shape_matrix = None
try:
@@ -174,10 +178,18 @@ except ImportError:
try:
from svg_to_pptx.drawingml.text_properties import (
+ parse_project_font_weight as _parse_project_font_weight,
+ parse_project_text_anchor as _parse_project_text_anchor,
project_text_property_diagnostics as _project_text_property_diagnostics,
+ resolve_project_font_sizes as _resolve_project_font_sizes,
+ resolve_project_letter_spacings as _resolve_project_letter_spacings,
)
except ImportError:
+ _parse_project_font_weight = None
+ _parse_project_text_anchor = None
_project_text_property_diagnostics = None
+ _resolve_project_font_sizes = None
+ _resolve_project_letter_spacings = None
try:
from pptx_to_svg.preset_authoring import (
@@ -2430,15 +2442,221 @@ class SVGQualityChecker:
result['info']['text_elements'] = text_count
result['info']['tspan_elements'] = tspan_count
- # Check for overly long single-line text (may need wrapping)
- text_matches = re.findall(r'<text[^>]*>([^<]{100,})</text>', content)
- if text_matches:
+ self._check_single_line_text_bounds(root, result)
+ self._check_unmergeable_leading_text(root, result)
+
+ @classmethod
+ def _single_line_text_runs(
+ cls,
+ text_el: ET.Element,
+ ) -> List[Tuple[ET.Element, str]] | None:
+ """Return normalized inline runs, or ``None`` for positioned text."""
+ runs: List[Tuple[ET.Element, str]] = []
+
+ def preserves_space(elem: ET.Element) -> bool:
+ return (
+ elem.get('{http://www.w3.org/XML/1998/namespace}space')
+ or elem.get('xml:space')
+ ) == 'preserve'
+
+ def append_run(owner: ET.Element, raw: str, preserve: bool) -> None:
+ normalized = raw if preserve else re.sub(r'\s+', ' ', raw)
+ if normalized:
+ runs.append((owner, normalized))
+
+ def collect(container: ET.Element, preserve: bool) -> bool:
+ if container.text:
+ append_run(container, container.text, preserve)
+ for child in list(container):
+ if not cls._is_tspan(child):
+ return False
+ if any(child.get(name) is not None for name in ('x', 'y', 'dx', 'dy')):
+ return False
+ if any(
+ name.startswith('data-paragraph-')
+ for name in child.attrib
+ ):
+ return False
+ child_preserve = preserve or preserves_space(child)
+ if not collect(child, child_preserve):
+ return False
+ if child.tail:
+ append_run(container, child.tail, preserve)
+ return True
+
+ preserve = preserves_space(text_el)
+ if not collect(text_el, preserve):
+ return None
+ if any('\n' in text or '\r' in text for _owner, text in runs):
+ return None
+ if runs and not preserve:
+ owner, text = runs[0]
+ runs[0] = (owner, text.lstrip(' '))
+ owner, text = runs[-1]
+ runs[-1] = (owner, text.rstrip(' '))
+ return [(owner, text) for owner, text in runs if text]
+
+ def _check_single_line_text_bounds(
+ self,
+ root: ET.Element,
+ result: Dict,
+ ) -> None:
+ """Warn only when an estimable single line exceeds the viewBox."""
+ helpers = (
+ _estimate_text_width,
+ _IDENTITY_MATRIX,
+ _matrix_multiply,
+ _parse_project_font_weight,
+ _parse_project_geometry_length,
+ _parse_project_text_anchor,
+ _parse_transform_matrix,
+ _resolve_project_font_sizes,
+ _resolve_project_letter_spacings,
+ _transform_point,
+ )
+ if any(helper is None for helper in helpers):
+ return
+
+ viewbox = _parse_viewbox_values(root.get('viewBox') or '')
+ if viewbox is None:
+ return
+ min_x, _min_y, width, _height = viewbox
+ max_x = min_x + width
+ try:
+ font_sizes = _resolve_project_font_sizes(root)
+ letter_spacings = _resolve_project_letter_spacings(root, font_sizes)
+ except ValueError:
+ return
+
+ parent_by_id = {
+ id(child): parent
+ for parent in root.iter()
+ for child in list(parent)
+ }
+ issues: List[str] = []
+ for text_el in root.iter(f'{{{SVG_NS}}}text'):
+ chain: List[ET.Element] = []
+ current: ET.Element | None = text_el
+ while current is not None:
+ chain.append(current)
+ current = parent_by_id.get(id(current))
+ if any(
+ _local_name(current) in _NON_VISUAL_SVG_TAGS
+ for current in chain
+ ):
+ continue
+ if text_el.get('data-paragraph-line-height') is not None:
+ continue
+
+ runs = self._single_line_text_runs(text_el)
+ if not runs:
+ continue
+ visible_text = ''.join(text for _owner, text in runs)
+ if '{{' in visible_text and '}}' in visible_text:
+ continue
+
+ try:
+ x = _parse_project_geometry_length(text_el.get('x') or '0', 'x')
+ y = _parse_project_geometry_length(text_el.get('y') or '0', 'y')
+ text_advance = 0.0
+ text_left = math.inf
+ text_right = -math.inf
+ for owner, text in runs:
+ raw_weight = (
+ _effective_presentation_value(
+ owner,
+ 'font-weight',
+ parent_by_id,
+ )
+ or 'normal'
+ ).strip().lower()
+ weight = (
+ 'bold'
+ if _parse_project_font_weight(raw_weight).value
+ else 'normal'
+ )
+ font_size = font_sizes[id(owner)]
+ spacing = letter_spacings[id(owner)]
+ for index, character in enumerate(text):
+ character_width = _estimate_text_width(
+ character,
+ font_size,
+ weight,
+ )
+ if not character.isspace():
+ text_left = min(text_left, text_advance)
+ text_right = max(
+ text_right,
+ text_advance + character_width,
+ )
+ text_advance += character_width
+ if index < len(text) - 1:
+ text_advance += spacing
+ except (KeyError, TypeError, ValueError):
+ continue
+ if not all(math.isfinite(value) for value in (
+ text_advance,
+ text_left,
+ text_right,
+ )):
+ continue
+
+ raw_anchor = (
+ _effective_presentation_value(
+ text_el,
+ 'text-anchor',
+ parent_by_id,
+ )
+ or 'start'
+ ).strip().lower()
+ try:
+ anchor = _parse_project_text_anchor(raw_anchor).value
+ except ValueError:
+ continue
+ if anchor == 'middle':
+ anchor_x = x - text_advance / 2
+ elif anchor == 'end':
+ anchor_x = x - text_advance
+ elif anchor == 'start':
+ anchor_x = x
+ else:
+ continue
+ left = anchor_x + text_left
+ right = anchor_x + text_right
+
+ matrix = _IDENTITY_MATRIX
+ try:
+ for current in reversed(chain):
+ raw_transform = current.get('transform')
+ if raw_transform:
+ matrix = _matrix_multiply(
+ matrix,
+ _parse_transform_matrix(raw_transform),
+ )
+ start = _transform_point(matrix, left, y)
+ end = _transform_point(matrix, right, y)
+ except (TypeError, ValueError):
+ continue
+ estimated_left = min(start[0], end[0])
+ estimated_right = max(start[0], end[0])
+ if estimated_left >= min_x - 1 and estimated_right <= max_x + 1:
+ continue
+
+ snippet = re.sub(r'\s+', ' ', visible_text).strip()
+ if len(snippet) > 40:
+ snippet = f'{snippet[:37]}...'
+ issues.append(f'{_element_label(text_el)} {snippet!r}')
+
+ if issues:
+ sample = '; '.join(issues[:3])
+ suffix = '' if len(issues) <= 3 else f'; +{len(issues) - 3} more'
result['warnings'].append(
- f"Detected {len(text_matches)} potentially overly long single-line text(s) (consider using tspan for wrapping)"
+ f'Detected {len(issues)} single-line text element(s) whose '
+ 'estimated horizontal bounds exceed the viewBox '
+ f'({sample}{suffix}); consider repositioning or using '
+ 'positioned <tspan> lines'
)
- self._check_unmergeable_leading_text(root, result)
-
def _check_unmergeable_leading_text(self, root: ET.Element, result: Dict) -> None:
"""Warn when leading text cannot be normalized for paragraph merging."""
risky = []
diff --git a/skills/ppt-master/scripts/test_svg_quality_checker.py b/skills/ppt-master/scripts/test_svg_quality_checker.py
index 0ae01995..55be080a 100644
--- a/skills/ppt-master/scripts/test_svg_quality_checker.py
+++ b/skills/ppt-master/scripts/test_svg_quality_checker.py
@@ -62,6 +62,14 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
svg_path.write_text(content, encoding='utf-8')
return SVGQualityChecker().check_file(str(svg_path))
+ @staticmethod
+ def _text_bound_warnings(result: dict) -> list[str]:
+ return [
+ warning
+ for warning in result['warnings']
+ if 'estimated horizontal bounds exceed the viewBox' in warning
+ ]
+
def _assert_checker_and_exporter_reject(
self,
content: str,
@@ -821,6 +829,18 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
self.assertEqual(noncanonical_sizes, [])
self.assertEqual(undersized_text, [])
+ def test_chart_text_bound_warnings_match_real_overflows(self):
+ chart_root = SCRIPT_DIR.parent / 'templates' / 'charts'
+ warned = set()
+ for svg_path in chart_root.glob('*.svg'):
+ result = SVGQualityChecker().check_file(str(svg_path))
+ if self._text_bound_warnings(result):
+ warned.add(svg_path.name)
+ self.assertEqual(
+ warned,
+ {'bullet_chart.svg', 'pyramid_isometric.svg'},
+ )
+
def test_chart_catalog_passes_checker_and_export_routes(self):
chart_root = SCRIPT_DIR.parent / 'templates' / 'charts'
index = json.loads(
@@ -1372,6 +1392,97 @@ class SVGQualityCheckerCompatibilityTests(unittest.TestCase):
with self.assertRaises(UseExpansionError):
expand_local_use_references(root)
+ def test_single_line_text_warning_uses_rendered_bounds(self):
+ source = (
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 100">'
+ '<text x="10" y="30" font-size="20">\n'
+ f' {" " * 120}Fit\n'
+ '</text>'
+ '<text x="150" y="70" font-size="20">WWWW</text>'
+ '</svg>'
+ )
+ result = self._check(source)
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn("'WWWW'", bounds_warnings[0])
+ self.assertNotIn("'Fit'", bounds_warnings[0])
+
+ def test_single_line_text_warning_allows_one_pixel_tolerance(self):
+ result = self._check(
+ '''<svg xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 200 100" font-size="20">
+ <text x="141" y="30">WWWW</text>
+ <text x="142" y="70">MMMM</text>
+</svg>'''
+ )
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn("'MMMM'", bounds_warnings[0])
+ self.assertNotIn("'WWWW'", bounds_warnings[0])
+
+ def test_single_line_text_warning_respects_text_anchor(self):
+ source = '''<svg xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 200 100" font-size="20">
+ <text x="100" y="20" text-anchor="middle">WWWWWWWW</text>
+ <text x="40" y="20" text-anchor="middle">WWWWWWWW</text>
+ <text x="160" y="20" text-anchor="middle">WWWWWWWW</text>
+ <text x="200" y="50" text-anchor="end">WWWWWWWW</text>
+ <text x="100" y="50" text-anchor="end">WWWWWWWW</text>
+ <text x="210" y="50" text-anchor="end">WWWWWWWW</text>
+</svg>'''
+ result = self._check(source)
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn('Detected 4 single-line text element(s)', bounds_warnings[0])
+
+ def test_single_line_text_warning_resolves_inline_runs_and_transforms(self):
+ source = '''<svg xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 200 100" font-size="20">
+ <defs><g id="hidden"><text x="190" y="20">WWWW</text></g></defs>
+ <g font-weight="semibold">
+ <text x="150" y="20">WW<tspan>WW</tspan></text>
+ </g>
+ <text x="150" y="30" letter-spacing="50">A<tspan
+ fill="#FF0000">B</tspan></text>
+ <text x="50" y="40"><tspan x="10">WWWWWW</tspan>
+ <tspan x="10" dy="20">WWWWWW</tspan></text>
+ <g transform="translate(-200 0)"><text x="250" y="60">WWWW</text></g>
+ <g transform="translate(100 0)"><text x="50" y="80">WWWW</text></g>
+ <g transform="translate(100 0)"><g transform="rotate(180 0 45)">
+ <text x="10" y="45" font-size="10">ii</text>
+ </g></g>
+ <text x="190" y="90">{{LONG_PLACEHOLDER}}</text>
+</svg>'''
+ result = self._check(source)
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn('Detected 2 single-line text element(s)', bounds_warnings[0])
+
+ def test_single_line_text_warning_includes_inherited_tracking(self):
+ result = self._check(
+ '''<svg xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 200 100">
+ <g font-size="20" font-weight="semibold" letter-spacing="10">
+ <text x="100" y="50">iiiiiiii</text>
+ </g>
+</svg>'''
+ )
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn("'iiiiiiii'", bounds_warnings[0])
+
+ def test_single_line_text_warning_measures_negative_tracking_bounds(self):
+ result = self._check(
+ '''<svg xmlns="http://www.w3.org/2000/svg"
+ viewBox="0 0 200 100">
+ <text x="50" y="25" font-size="20" letter-spacing="-100">AB</text>
+ <text x="300" y="50" font-size="20" letter-spacing="-100">AB</text>
+</svg>'''
+ )
+ bounds_warnings = self._text_bound_warnings(result)
+ self.assertEqual(len(bounds_warnings), 1)
+ self.assertIn('Detected 2 single-line text element(s)', bounds_warnings[0])
+
def test_text_property_contract_preserves_registered_drawingml_semantics(self):
source = '''<svg xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 640 360" font-style="italic">