Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Glyphs at end of Code lines no longer deleted when disable_ligatures is False (#4926)

nikolajmunka0afb6d812dc
diff --git a/manim/mobject/text/code_mobject.py b/manim/mobject/text/code_mobject.py
index f2b72cd12..05fdeea68 100644
--- a/manim/mobject/text/code_mobject.py
+++ b/manim/mobject/text/code_mobject.py
@@ -237,6 +237,12 @@ class Code(VMobject, metaclass=ConvertToOpenGL):
# ascender and a descender to the first and last lines. This normalizes
# the vertical bounds of code listings independently of their contents.
alignment_suffix = " pA" + str(line_numbers_from)
+ # Get actual number of glyphs in case it differs from the number of characters
+ alignment_reference = Paragraph(
+ alignment_suffix,
+ **base_paragraph_config,
+ )
+ alignment_length = len(alignment_reference[0])
boundary_line_indices = sorted({0, len(rendered_code_lines) - 1})
aligned_code_lines = rendered_code_lines.copy()
for index in boundary_line_indices:
@@ -302,7 +308,7 @@ class Code(VMobject, metaclass=ConvertToOpenGL):
*(
self.code_lines[index].submobjects.pop()
for index in boundary_line_indices
- for _ in range(len(alignment_suffix))
+ for _ in range(alignment_length)
)
).stretch_to_fit_width(0, about_edge=LEFT)
diff --git a/tests/module/mobject/text/test_code_mobject.py b/tests/module/mobject/text/test_code_mobject.py
index 08f5ee472..65c134294 100644
--- a/tests/module/mobject/text/test_code_mobject.py
+++ b/tests/module/mobject/text/test_code_mobject.py
@@ -189,6 +189,31 @@ def test_code_height_is_independent_of_boundary_glyphs(
np.testing.assert_allclose(background_heights, background_heights[0], atol=1e-6)
[email protected]("disable_ligatures", [True, False])
[email protected]("add_line_numbers", [True, False])
+def test_code_line_mobject_is_independent_of_ordinality(
+ disable_ligatures, add_line_numbers
+):
+ """Test that the (sub)mobject structure and width of a line of code does not depend
+ on the line's order in the block.
+ """
+ code_strings = ("Hello World!",) * 5
+ code_mobject = Code(
+ code_string="\n".join(code_strings),
+ add_line_numbers=add_line_numbers,
+ paragraph_config={"disable_ligatures": disable_ligatures},
+ )
+ lines = code_mobject.code_lines
+ reference_line = lines[len(lines) // 2]
+ for boundary_line in (lines[0], lines[-1]):
+ assert len(boundary_line) == len(reference_line)
+ np.testing.assert_allclose(
+ boundary_line.width,
+ reference_line.width,
+ atol=1e-6,
+ )
+
+
def test_code_syntax_highlighting_colors():
code_string = "pass\n# comment"
rendered_code = Code(