From eec859384186b0210f3e6e6abcd42f40ed2e9b12 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sat, 26 Apr 2025 23:22:50 +0200 Subject: [PATCH] Improve repr of mathtext internal structures; minor cleanup. - Improve the repr of mathtext boxes (see test_box_repr; previously the entire repr would be in a single line). Boxes are internal objects not exposed by any public API so the change is purely for debugging purposes. - In ship, off_h and off_v are never modified, so there's no need to declare them as nonlocal in the nested functions. --- lib/matplotlib/_mathtext.py | 15 ++++++--- lib/matplotlib/tests/test_mathtext.py | 44 +++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/lib/matplotlib/_mathtext.py b/lib/matplotlib/_mathtext.py index ae230703faed..3739a517978b 100644 --- a/lib/matplotlib/_mathtext.py +++ b/lib/matplotlib/_mathtext.py @@ -14,6 +14,7 @@ import types import unicodedata import string +import textwrap import typing as T from typing import NamedTuple @@ -1165,12 +1166,16 @@ def __init__(self, elements: T.Sequence[Node]): self.glue_sign = 0 # 0: normal, -1: shrinking, 1: stretching self.glue_order = 0 # The order of infinity (0 - 3) for the glue - def __repr__(self) -> str: - return '{}[{}]'.format( + def __repr__(self): + return "{}[{}]".format( super().__repr__(), self.width, self.height, self.depth, self.shift_amount, - ', '.join([repr(x) for x in self.children])) + "\n" + textwrap.indent( + "\n".join(map("{!r},".format, self.children)), + " ") + "\n" + if self.children else "" + ) def _set_glue(self, x: float, sign: int, totals: list[float], error_type: str) -> None: @@ -1604,7 +1609,7 @@ def clamp(value: float) -> float: return -1e9 if value < -1e9 else +1e9 if value > +1e9 else value def hlist_out(box: Hlist) -> None: - nonlocal cur_v, cur_h, off_h, off_v + nonlocal cur_v, cur_h cur_g = 0 cur_glue = 0. @@ -1667,7 +1672,7 @@ def hlist_out(box: Hlist) -> None: cur_h += rule_width def vlist_out(box: Vlist) -> None: - nonlocal cur_v, cur_h, off_h, off_v + nonlocal cur_v, cur_h cur_g = 0 cur_glue = 0. diff --git a/lib/matplotlib/tests/test_mathtext.py b/lib/matplotlib/tests/test_mathtext.py index 9c0f8ee70c1f..198e640ad286 100644 --- a/lib/matplotlib/tests/test_mathtext.py +++ b/lib/matplotlib/tests/test_mathtext.py @@ -4,8 +4,9 @@ from pathlib import Path import platform import re -from xml.etree import ElementTree as ET +import textwrap from typing import Any +from xml.etree import ElementTree as ET import numpy as np from packaging.version import parse as parse_version @@ -16,7 +17,8 @@ import matplotlib as mpl from matplotlib.testing.decorators import check_figures_equal, image_comparison import matplotlib.pyplot as plt -from matplotlib import mathtext, _mathtext +from matplotlib import font_manager as fm, mathtext, _mathtext +from matplotlib.ft2font import LoadFlags pyparsing_version = parse_version(pyparsing.__version__) @@ -558,3 +560,41 @@ def test_mathtext_operators(): def test_boldsymbol(fig_test, fig_ref): fig_test.text(0.1, 0.2, r"$\boldsymbol{\mathrm{abc0123\alpha}}$") fig_ref.text(0.1, 0.2, r"$\mathrm{abc0123\alpha}$") + + +def test_box_repr(): + s = repr(_mathtext.Parser().parse( + r"$\frac{1}{2}$", + _mathtext.DejaVuSansFonts(fm.FontProperties(), LoadFlags.NO_HINTING), + fontsize=12, dpi=100)) + assert s == textwrap.dedent("""\ + Hlist[ + Hlist[], + Hlist[ + Hlist[ + Vlist[ + HCentered[ + Glue, + Hlist[ + `1`, + k2.36, + ], + Glue, + ], + Vbox, + Hrule, + Vbox, + HCentered[ + Glue, + Hlist[ + `2`, + k2.02, + ], + Glue, + ], + ], + Hbox, + ], + ], + Hlist[], + ]""")