Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Improve repr of mathtext internal structures; minor cleanup. #29972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/matplotlib/_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import types
import unicodedata
import string
import textwrap
import typing as T
from typing import NamedTuple

Expand Down Expand Up @@ -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 '{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]'.format(
def __repr__(self):
return "{}<w={:.02f} h={:.02f} d={:.02f} s={:.02f}>[{}]".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:
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down
44 changes: 42 additions & 2 deletions lib/matplotlib/tests/test_mathtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)

Expand Down Expand Up @@ -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<w=9.49 h=16.08 d=6.64 s=0.00>[
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
Hlist<w=9.49 h=16.08 d=6.64 s=0.00>[
Hlist<w=9.49 h=16.08 d=6.64 s=0.00>[
Vlist<w=7.40 h=22.72 d=0.00 s=6.64>[
HCentered<w=7.40 h=8.67 d=0.00 s=0.00>[
Glue,
Hlist<w=7.40 h=8.67 d=0.00 s=0.00>[
`1`,
k2.36,
],
Glue,
],
Vbox,
Hrule,
Vbox,
HCentered<w=7.40 h=8.84 d=0.00 s=0.00>[
Glue,
Hlist<w=7.40 h=8.84 d=0.00 s=0.00>[
`2`,
k2.02,
],
Glue,
],
],
Hbox,
],
],
Hlist<w=0.00 h=0.00 d=0.00 s=0.00>[],
]""")
Loading