Skip to content

Commit dc05767

Browse files
authored
Merge pull request #29828 from anntzer/dvitype
2 parents cf948de + 829dcaa commit dc05767

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

lib/matplotlib/dviread.py

+28-10
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)