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

Skip to content

Commit b607a01

Browse files
committed
Simplify tfm parsing.
Directly read as signed int32 ("i") instead of reading as signed int32 and converting to unsigned later using fix2comp. Simplify parsing of widths/heights/depths.
1 parent 36c6632 commit b607a01

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

lib/matplotlib/dviread.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -714,15 +714,6 @@ def _pre(self, i, x, cs, ds):
714714
# cs = checksum, ds = design size
715715

716716

717-
def _fix2comp(num):
718-
"""Convert from two's complement to negative."""
719-
assert 0 <= num < 2**32
720-
if num & 2**31:
721-
return num - 2**32
722-
else:
723-
return num
724-
725-
726717
def _mul2012(num1, num2):
727718
"""Multiply two numbers in 20.12 fixed point format."""
728719
# Separated into a function because >> has surprising precedence
@@ -756,29 +747,23 @@ def __init__(self, filename):
756747
_log.debug('opening tfm file %s', filename)
757748
with open(filename, 'rb') as file:
758749
header1 = file.read(24)
759-
lh, bc, ec, nw, nh, nd = \
760-
struct.unpack('!6H', header1[2:14])
750+
lh, bc, ec, nw, nh, nd = struct.unpack('!6H', header1[2:14])
761751
_log.debug('lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%d',
762752
lh, bc, ec, nw, nh, nd)
763753
header2 = file.read(4*lh)
764-
self.checksum, self.design_size = \
765-
struct.unpack('!2I', header2[:8])
754+
self.checksum, self.design_size = struct.unpack('!2I', header2[:8])
766755
# there is also encoding information etc.
767756
char_info = file.read(4*(ec-bc+1))
768-
widths = file.read(4*nw)
769-
heights = file.read(4*nh)
770-
depths = file.read(4*nd)
771-
757+
widths = struct.unpack(f'!{nw}i', file.read(4*nw))
758+
heights = struct.unpack(f'!{nh}i', file.read(4*nh))
759+
depths = struct.unpack(f'!{nd}i', file.read(4*nd))
772760
self.width, self.height, self.depth = {}, {}, {}
773-
widths, heights, depths = \
774-
[struct.unpack('!%dI' % (len(x)/4), x)
775-
for x in (widths, heights, depths)]
776761
for idx, char in enumerate(range(bc, ec+1)):
777762
byte0 = char_info[4*idx]
778763
byte1 = char_info[4*idx+1]
779-
self.width[char] = _fix2comp(widths[byte0])
780-
self.height[char] = _fix2comp(heights[byte1 >> 4])
781-
self.depth[char] = _fix2comp(depths[byte1 & 0xf])
764+
self.width[char] = widths[byte0]
765+
self.height[char] = heights[byte1 >> 4]
766+
self.depth[char] = depths[byte1 & 0xf]
782767

783768

784769
PsFont = namedtuple('PsFont', 'texname psname effects encoding filename')

0 commit comments

Comments
 (0)