fix(text): raise a clear error instead of IndexError on glyph/char mismatch#4868
Open
SupernovaIa wants to merge 2 commits into
Open
fix(text): raise a clear error instead of IndexError on glyph/char mismatch#4868SupernovaIa wants to merge 2 commits into
SupernovaIa wants to merge 2 commits into
Conversation
…smatch
Text._gen_chars() assumed disable_ligatures=True guarantees one glyph per
non-space character, but that only holds if the font's ligatures are all
implemented via the 'liga'/'dlig'/'clig'/'hlig' OpenType features that
ManimPango disables. Fonts like Fira Code implement programming ligatures
(e.g. '<=', '->') via 'calt' instead, which isn't covered, so Pango still
merges characters into a single glyph and _gen_chars() walks off the end
of self.submobjects with an opaque IndexError (e.g. via Code(...,
paragraph_config={"font": "Fira Code"}), see issue ManimCommunity#3237).
This adds an upfront check that raises a clear ValueError explaining the
likely cause and a workaround, instead of the bare IndexError. Note this
does not fix the underlying issue -- the actual root cause (ManimPango
not disabling 'calt') lives in a separate repo (ManimPango) and would
need to be addressed there for Code/Text to render correctly with fonts
like Fira Code.
Refs ManimCommunity#3237
The previous pre-loop count compared non-space chars against total submobjects, but spaces render as their own glyph under disable_ligatures=True, so the counts legitimately differ. This caused a false-positive ValueError on ordinary spaced text (e.g. the DisableLigature docs example, breaking the ReadTheDocs build). Move the check inside the loop as a bounds guard so it only fires on the real glyph shortfall.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Text._gen_chars()assumesdisable_ligatures=Trueguarantees exactly one rendered glyph per non-space character. That assumption breaks for fonts that implement some of their ligatures through OpenType features other thanliga/dlig/clig/hlig(the ones ManimPango'sdisable_ligadisables) — for example, Fira Code implements its programming ligatures (<=,->, etc.) exclusively throughcalt(contextual alternates), which isn't covered. I confirmed this by inspecting Fira Code's own GSUB table withfontTools: it definescaltbut none ofliga/dlig/clig/hlig.When that happens, Pango still merges e.g.
<and=into a single glyph, and_gen_chars()walks off the end ofself.submobjects, raising an opaqueIndexError: list index out of rangewith no indication of what went wrong. This is reproducible today via, for instance:This PR does not fix the underlying issue — the actual root cause is in a separate repository,
ManimCommunity/ManimPango(itsdisable_ligafeature string would needcalt=0added). That's outside the scope of this PR (would require setting up the Cython/C build for a separate package).What this PR does do: turn the opaque crash into a clear, actionable error, so that anyone hitting this (there have been a few confused reports in #3237 over time) immediately understands why, instead of debugging a bare
IndexError. This is a pure improvement to the failure path — no behavior changes for the cases that already work.Implementation note
The check is a bounds guard inside the glyph-consuming loop, raised only when there are genuinely fewer glyphs than non-space characters. An earlier version of this PR used a pre-loop count that compared non-space characters against the total number of submobjects; that was wrong, because under
disable_ligatures=Truea space is rendered as its own glyph, so the two counts legitimately differ for any text containing spaces. It produced a false positive on ordinary spaced text — including theDisableLigaturedocs example (Text("fl ligature", disable_ligatures=True)), which broke the docs build. The in-loop bounds check fires only on the real glyph shortfall.Refs #3237
Test plan
test_gen_chars_raises_clear_error_on_glyph_mismatch— simulates the glyph shortfall directly (no dependency on any specific font being installed) and asserts the newValueErroris raisedtest_gen_chars_handles_spaces_with_disable_ligatures— rendersText("fl ligature", disable_ligatures=True)(the case that broke the docs build) and asserts it does not raiseuv run pytest tests/module/mobject/text/test_text_mobject.py tests/test_code_mobject.py— all passDisableLigaturedocs example locally, confirmed it renders without erroruv run pre-commit run --files manim/mobject/text/text_mobject.py tests/module/mobject/text/test_text_mobject.py— ruff, mypy, codespell all pass