diff --git a/manim/mobject/svg/svg_mobject.py b/manim/mobject/svg/svg_mobject.py index c296130a27..26834f7e0e 100644 --- a/manim/mobject/svg/svg_mobject.py +++ b/manim/mobject/svg/svg_mobject.py @@ -158,6 +158,15 @@ def __init__( ) self.move_into_position() + # Glyph submobjects are added after super().__init__ set self.z_index, + # so a z_index passed to the constructor never reached them. Propagate + # it so the whole SVG (e.g. Text/MathTex) shares one z_index. Guard with + # getattr since the OpenGL renderer does not track z_index as an attribute. + z_index = getattr(self, "z_index", None) + if z_index is not None: + for submob in self.get_family(): + submob.z_index = z_index + def init_svg_mobject(self, use_svg_cache: bool) -> None: """Checks whether the SVG has already been imported and generates it if not. diff --git a/tests/module/mobject/text/test_text_mobject.py b/tests/module/mobject/text/test_text_mobject.py index 2e656b90ae..a3f446e019 100644 --- a/tests/module/mobject/text/test_text_mobject.py +++ b/tests/module/mobject/text/test_text_mobject.py @@ -17,6 +17,21 @@ def test_font_size(): assert round(markuptext_string.font_size, 5) == 14.4 +def test_z_index_propagates_to_glyphs(): + """A z_index passed to the constructor must reach the glyph submobjects, + otherwise the z-ordering is ignored during rendering (see issue #4667). + """ + text = Text("AB", z_index=3) + assert text.z_index == 3 + assert all(glyph.z_index == 3 for glyph in text.family_members_with_points()) + + markup = MarkupText("AB", z_index=3) + assert all(glyph.z_index == 3 for glyph in markup.family_members_with_points()) + + # The default must remain unchanged. + assert all(glyph.z_index == 0 for glyph in Text("AB").family_members_with_points()) + + def test_font_warnings(): def warning_printed(font: str, **kwargs) -> bool: io = StringIO()