fix(text): raise a clear error instead of IndexError on glyph/char mismatch#4868
Open
SupernovaIa wants to merge 1 commit into
Open
fix(text): raise a clear error instead of IndexError on glyph/char mismatch#4868SupernovaIa wants to merge 1 commit into
SupernovaIa wants to merge 1 commit 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
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.Refs #3237
Test plan
test_gen_chars_raises_clear_error_on_glyph_mismatch, which simulates the glyph/char mismatch directly (no dependency on any specific font being installed) and asserts the newValueErroris raiseduv run pytest tests/module/mobject/text/test_text_mobject.py tests/test_code_mobject.py— all passuv run pre-commit run --files manim/mobject/text/text_mobject.py tests/module/mobject/text/test_text_mobject.py— ruff, mypy, codespell all pass