8000 Improve repr of mathtext internal structures; minor cleanup. · matplotlib/matplotlib@eec8593 · GitHub
[go: up one dir, main page]

Skip to content

Commit eec8593

Browse files
committed
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.
1 parent 492a478 commit eec8593

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

lib/matplotlib/_mathtext.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import types
1515
import unicodedata
1616
import string
17+
import textwrap
1718
import typing as T
1819
from typing import NamedTuple
1920

@@ -1165,12 +1166,16 @@ def __init__(self, elements: T.Sequence[Node]):
11651166
self.glue_sign = 0 # 0: normal, -1: shrinking, 1: stretching
11661167
self.glue_order = 0 # The order of infinity (0 - 3) for the glue
11671168

1168-
def __repr__(self) -> str:
1169-
return '{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]'.format(
1169+
def __repr__(self):
1170+
return "{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]".format(
11701171
super().__repr__(),
11711172
self.width, self.height,
11721173
self.depth, self.shift_amount,
1173-
', '.join([repr(x) for x in self.children]))
1174+
"\n" + textwrap.indent(
1175+
"\n".join(map("{!r},".format, self.children)),
1176+
" ") + "\n"
1177+
if self.children else ""
1178+
)
11741179

11751180
def _set_glue(self, x: float, sign: int, totals: list[float],
11761181
error_type: str) -> None:
@@ -1604,7 +1609,7 @@ def clamp(value: float) -> float:
16041609
return -1e9 if value < -1e9 else +1e9 if value > +1e9 else value
16051610

16061611
def hlist_out(box: Hlist) -> None:
1607-
nonlocal cur_v, cur_h, off_h, off_v
1612+
nonlocal cur_v, cur_h
16081613

16091614
cur_g = 0
16101615
cur_glue = 0.
@@ -1667,7 +1672,7 @@ def hlist_out(box: Hlist) -> None:
16671672
cur_h += rule_width
16681673

16691674
def vlist_out(box: Vlist) -> None:
1670-
nonlocal cur_v, cur_h, off_h, off_v
1675+
nonlocal cur_v, cur_h
16711676

16721677
cur_g = 0
16731678
cur_glue = 0.

lib/matplotlib/tests/test_mathtext.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
from pathlib import Path
55
import platform
66
import re
7-
from xml.etree import ElementTree as ET
7+
import textwrap
88
from typing import Any
9+
from xml.etree import ElementTree as ET
910

1011
import numpy as np
1112
from packaging.version import parse as parse_version
@@ -16,7 +17,8 @@
1617
import matplotlib as mpl
1718
from matplotlib.testing.decorators import check_figures_equal, image_comparison
1819
import matplotlib.pyplot as plt
19-
from matplotlib import mathtext, _mathtext
20+
from matplotlib import font_manager as fm, mathtext, _mathtext
21+
from matplotlib.ft2font import LoadFlags
2022

2123
pyparsing_version = parse_version(pyparsing.__version__)
2224

@@ -558,3 +560,41 @@ def test_mathtext_operators():
558560
def test_boldsymbol(fig_test, fig_ref):
559561
fig_test.text(0.1, 0.2, r"$\boldsymbol{\mathrm{abc0123\alpha}}$")
560562
fig_ref.text(0.1, 0.2, r"$\mathrm{abc0123\alpha}$")
563+
564+
565+
def test_box_repr():
566+
s = repr(_mathtext.Parser().parse(
567+
r"$\frac{1}{2}$",
568+
_mathtext.DejaVuSansFonts(fm.FontProperties(), LoadFlags.NO_HINTING),
569+
fontsize=12, dpi=100))
570+
assert s == textwrap.dedent("""\
571+
Hlist<w=9.49 h=16.08 d=6.64 s=0.00>[
572+
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
573+
Hlist<w=9.49 h=16.08 d=6.64 s=0.00>[
574+
Hlist<w=9.49 h=16.08 d=6.64 s=0.00>[
575+
Vlist<w=7.40 h=22.72 d=0.00 s=6.64>[
576+
HCentered<w=7.40 h=8.67 d=0.00 s=0.00>[
577+
Glue,
578+
Hlist<w=7.40 h=8.67 d=0.00 s=0.00>[
579+
`1`,
580+
k2.36,
581+
],
582+
Glue,
583+
],
584+
Vbox,
585+
Hrule,
586+
Vbox,
587+
HCentered<w=7.40 h=8.84 d=0.00 s=0.00>[
588+
Glue,
589+
Hlist<w=7.40 h=8.84 d=0.00 s=0.00>[
590+
`2`,
591+
k2.02,
592+
],
593+
Glue,
594+
],
595+
],
596+
Hbox,
597+
],
598+
],
599+
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
600+
]""")

0 commit comments

Comments
 (0)
0