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(text-measure): accented letters outside the advance table advance like their base letter

hugohe3c050b1fa84ee
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 edfa1f4a..66d2c54f 100644
--- a/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
+++ b/skills/ppt-master/scripts/svg_to_pptx/drawingml/utils.py
@@ -3669,6 +3669,19 @@ _FIXED_SPACE_EMS = {
}
+_UNDECOMPOSED_BASE_LETTERS = str.maketrans('đĐłŁøØħĦ', 'dDlLoOhH')
+
+
+def _base_letter(ch: str) -> str:
+ """Return the unaccented letter a precomposed character advances like."""
+ decomposed = unicodedata.normalize('NFD', ch)
+ if len(decomposed) > 1 and all(
+ unicodedata.category(mark) == 'Mn' for mark in decomposed[1:]
+ ):
+ ch = decomposed[0]
+ return ch.translate(_UNDECOMPOSED_BASE_LETTERS)
+
+
def _estimate_character_width(ch: str, font_size: float) -> float:
if (
0xFF00 <= ord(ch) <= 0xFFEF
@@ -3681,6 +3694,7 @@ def _estimate_character_width(ch: str, font_size: float) -> float:
return font_size * 0.3
if ch in _FIXED_SPACE_EMS:
return font_size * _FIXED_SPACE_EMS[ch]
+ ch = _base_letter(ch)
if ch in 'mMwWOQ%':
return font_size * 0.75
if ch in 'iIlj!|':
@@ -3766,6 +3780,18 @@ def estimate_text_cluster_widths(
):
widths.append(sum(advances[ch] for ch in cluster) * font_size)
continue
+ # A precomposed letter the table lacks advances like its base letter.
+ bases = [
+ _base_letter(ch) for ch in cluster if not _is_grapheme_extend(ch)
+ ]
+ if advances is not None and not cjk and bases and all(
+ ch in advances and not _is_emoji_base(ch) for ch in bases
+ ) and all(
+ unicodedata.category(ch) == 'Mn' and not 0xFE00 <= ord(ch) <= 0xFE0F
+ for ch in cluster if _is_grapheme_extend(ch)
+ ):
+ widths.append(sum(advances[ch] for ch in bases) * font_size)
+ continue
width = _estimate_grapheme_width(cluster, font_size)
widths.append(width * 1.05 if bold and not cjk else width)
return widths
diff --git a/skills/ppt-master/scripts/tests/test_text_measure.py b/skills/ppt-master/scripts/tests/test_text_measure.py
index e7e6b020..6c5aaca9 100644
--- a/skills/ppt-master/scripts/tests/test_text_measure.py
+++ b/skills/ppt-master/scripts/tests/test_text_measure.py
@@ -202,7 +202,7 @@ class TextMeasureTests(unittest.TestCase):
)
def test_extended_clusters_keep_existing_widths_and_tracking(self) -> None:
- text = 'e\u0301👩🏽‍💻🇨🇳1️⃣Aア'
+ text = '👩🏽‍💻🇨🇳1️⃣Aア'
for weight in ('400', 'bold'):
with self.subTest(weight=weight):
crude = estimate_text_cluster_widths(text, 20, weight)
@@ -216,6 +216,13 @@ class TextMeasureTests(unittest.TestCase):
sum(crude) + 2 * (len(crude) - 1),
)
+ def test_accented_letters_outside_the_table_advance_like_their_base(self) -> None:
+ for family in ('Arial', 'Cambria'):
+ with self.subTest(family=family):
+ accented = estimate_text_cluster_widths('ỔỊe\u0301ệ', 20, font_family=family)
+ plain = estimate_text_cluster_widths('OIee', 20, font_family=family)
+ self.assertEqual(accented, plain)
+
def test_arial_black_keeps_wide_family_factor(self) -> None:
crude = sum(estimate_text_cluster_widths('CAPS', 20, 'bold'))
self.assertAlmostEqual(