Skip to content

Commit a360989

Browse files
committed
Parse more fields from tfm files.
Run `logging.basicConfig(level="DEBUG"); Tfm("foo.tfm")` to get more info from the tfm header. The values are "as stored" in the file, unlike tftopl which displays them as rounded after scaling by the design size.
1 parent e046a98 commit a360989

1 file changed

Lines changed: 36 additions & 7 deletions

File tree

lib/matplotlib/dviread.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,7 @@ class TexMetrics:
967967

968968
class Tfm:
969969
"""
970-
A TeX Font Metric file.
970+
A TeX Font Metric file (see ``texdoc tftopl`` for the format's description).
971971
972972
This implementation covers only the bare minimum needed by the Dvi class.
973973
@@ -987,17 +987,46 @@ class Tfm:
987987
def __init__(self, filename):
988988
_log.debug('opening tfm file %s', filename)
989989
with open(filename, 'rb') as file:
990-
header1 = file.read(24)
991-
lh, bc, ec, nw, nh, nd = struct.unpack('!6H', header1[2:14])
992-
_log.debug('lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%d',
993-
lh, bc, ec, nw, nh, nd)
994-
header2 = file.read(4*lh)
995-
self.checksum, self.design_size = struct.unpack('!2I', header2[:8])
990+
lf, lh, bc, ec, nw, nh, nd, ni, nl, nk, ne, np = struct.unpack(
991+
'!12H', file.read(24))
992+
_log.debug('lf=%d, lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, '
993+
'nd=%d, ni=%d, nl=%d, nk=%d, ne=%d, np=%d',
994+
lf, lh, bc, ec, nw, nh, nd, ni, nl, nk, ne, np)
995+
(self.checksum, self.design_size,
996+
char_coding_scheme, font_id, bitmask) = struct.unpack(
997+
'!II40s20sI', file.read(4*lh))
998+
char_coding_scheme = char_coding_scheme[1:].rstrip(b"\0").decode("ascii")
999+
font_id = font_id[1:].rstrip(b"\0").decode("ascii")
1000+
_log.debug(
1001+
'checksum=%d, design_size=%d, '
1002+
'char_coding_scheme=%s, font_id=%s, bitmask=%#2X',
1003+
self.checksum, self.design_size,
1004+
char_coding_scheme, font_id,
1005+
bitmask)
9961006
# there is also encoding information etc.
9971007
char_info = file.read(4*(ec-bc+1))
9981008
widths = struct.unpack(f'!{nw}i', file.read(4*nw))
9991009
heights = struct.unpack(f'!{nh}i', file.read(4*nh))
10001010
depths = struct.unpack(f'!{nd}i', file.read(4*nd))
1011+
charic = struct.unpack(f'!{ni}i', file.read(4*ni))
1012+
lig_kern = struct.unpack(f'!{nl}i', file.read(4*nl))
1013+
kern = struct.unpack(f'!{nk}i', file.read(4*nk))
1014+
ext = struct.unpack(f'!{ne}i', file.read(4*ne))
1015+
param = struct.unpack(f'!{np}i', file.read(4*np))
1016+
_log.debug(
1017+
'slant=%d, space=%d, space_stretch=%d, space_shrink=%d, '
1018+
'x_height=%d, quad=%d, extra_space=%d', *param[:7])
1019+
if char_coding_scheme == "TeX math symbols":
1020+
_log.debug(
1021+
'num1=%d, num2=%d, num3=%d, denom1=%d, denom2=%d, '
1022+
'sup1=%d, sup2=%d, sup3=%d, sub1=%d, sub2=%d, '
1023+
'supdrop=%d, subdrop=%d, delim1=%d, delim2=%d, axis_height=%d',
1024+
*param[7:22])
1025+
elif char_coding_scheme == "TeX math extension":
1026+
_log.debug(
1027+
'default_rule_thickness=%d, big_op_spacing1=%d, '
1028+
'big_op_spacing2=%d, big_op_spacing3=%d, big_op_spacing4=%d ,'
1029+
'big_op_spacing5=%d', *param[7:13])
10011030
self._glyph_metrics = {}
10021031
for idx, char in enumerate(range(bc, ec+1)):
10031032
byte0 = char_info[4*idx]

0 commit comments

Comments
 (0)