8000 Improve output of dvi debug parsing. · matplotlib/matplotlib@829dcaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 829dcaa

Browse files
committed
Improve output of dvi debug parsing.
To test, run e.g. ```python from pylab import * set_loglevel("debug") figtext(.5, .5, r"gff\textwon$\frac12$", usetex=True) show() ``` Grab the path of the dvi file that appears last in the log, then run ```bash python -mmatplotlib.dviread /path/to/dvi ``` Prior to this patch, the output was ``` === new page === (w: 1668654, h: 553676, d: 225994) font: 'cmss10' scale: 0.625 x y glyph chr w (glyphs) 983040 1441792 103 g 327681 1310721 1441792 11 . 382295 font: 'tcss1000' scale: 0.625 x y glyph chr w (glyphs) 1693016 1441792 142 . 618800 font: 'cmr7' scale: 0.4375 x y glyph chr w (glyphs) 2390459 1183756 49 1 261235 2390459 1667786 50 2 261235 x y h w (boxes) 2390459 1291058 26213 261235 ``` With this patch, the output is ``` === NEW PAGE === (w: 1668654, h: 553676, d: 225994) --- GLYPHS --- font: cmss10 (scale: 0.625) at /usr/local/texlive/2025/texmf-dist/fonts/type1/public/amsfonts/cm/cmss10.pfb x y glyph chr w 983040 1441792 103 g 327681 1310721 1441792 11 ff 382295 font: tcss1000 (scale: 0.625) at /usr/local/texlive/2025/texmf-dist/fonts/type1/public/cm-super/sfss1000.pfb x y glyph chr w 1693016 1441792 142 ₩ 618800 font: cmr7 (scale: 0.4375) at /usr/local/texlive/2025/texmf-dist/fonts/type1/public/amsfonts/cm/cmr7.pfb x y glyph chr w 2390459 1183756 49 1 261235 2390459 1667786 50 2 261235 --- BOXES --- x y h w 2390459 1291058 26213 261235 ``` Most importantly, glyphs are now resolved to their unicode representation (essentially by resolving the glyph name, similarly to the process described in Text.glyph_name_or_index), whereas many complex glyphs were just output as "." before. Furthermore, full font paths are printed, and the output is slightly better aligned. These improvements will be more significant with the future support for {xe,lua}tex, which can load a much wider variety of glyphs.
1 parent 05663f7 commit 829dcaa

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

lib/matplotlib/dviread.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,26 +1108,44 @@ def _fontfile(cls, suffix, texname):
11081108
from argparse import ArgumentParser
11091109
import itertools
11101110

1111+
import fontTools.agl
1112+
1113+
from matplotlib.ft2font import FT2Font
1114+
from matplotlib.textpath import TextToPath
1115+
11111116
parser = ArgumentParser()
11121117
parser.add_argument("filename")
11131118
parser.add_argument("dpi", nargs="?", type=float, default=None)
11141119
args = parser.parse_args()
1120+
1121+
def _print_fields(*args):
1122+
print(" ".join(map("{:>11}".format, args)))
1123+
11151124
with Dvi(args.filename, args.dpi) as dvi:
11161125
fontmap = PsfontsMap(find_tex_file('pdftex.map'))
11171126
for page in dvi:
1118-
print(f"=== new page === "
1127+
print(f"=== NEW PAGE === "
11191128
f"(w: {page.width}, h: {page.height}, d: {page.descent})")
1129+
print("--- GLYPHS ---")
11201130
for font, group in itertools.groupby(
11211131
page.text, lambda text: text.font):
1122-
print(f"font: {font.texname.decode('latin-1')!r}\t"
1123-
f"scale: {font._scale / 2 ** 20}")
1124-
print("x", "y", "glyph", "chr", "w", "(glyphs)", sep="\t")
1132+
psfont = fontmap[font.texname]
1133+
fontpath = psfont.filename
1134+
print(f"font: {font.texname.decode('latin-1')} "
1135+
f"(scale: {font._scale / 2 ** 20}) at {fontpath}")
1136+
face = FT2Font(fontpath)
1137+
TextToPath._select_native_charmap(face)
1138+
_print_fields("x", "y", "glyph", "chr", "w")
11251139
for text in group:
1126-
print(text.x, text.y, text.glyph,
1127-
chr(text.glyph) if chr(text.glyph).isprintable()
1128-
else ".",
1129-
text.width, sep="\t")
1140+
if psfont.encoding:
1141+
glyph_name = _parse_enc(psfont.encoding)[text.glyph]
1142+
else:
1143+
glyph_name = face.get_glyph_name(
1144+
face.get_char_index(text.glyph))
1145+
glyph_str = fontTools.agl.toUnicode(glyph_name)
1146+
_print_fields(text.x, text.y, text.glyph, glyph_str, text.width)
11301147
if page.boxes:
1131-
print("x", "y", "h", "w", "", "(boxes)", sep="\t")
1148+
print("--- BOXES ---")
1149+
_print_fields("x", "y", "h", "w")
11321150
for box in page.boxes:
1133-
print(box.x, box.y, box.height, box.width, sep="\t")
1151+
_print_fields(box.x, box.y, box.height, box.width)

0 commit comments

Comments
 (0)
0