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

Skip to content

Simplify tfm parsing. #20299

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
May 26, 2021
Merged
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
31 changes: 8 additions & 23 deletions lib/matplotlib/dviread.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,15 +714,6 @@ def _pre(self, i, x, cs, ds):
# cs = checksum, ds = design size


def _fix2comp(num):
"""Convert from two's complement to negative."""
assert 0 <= num < 2**32
if num & 2**31:
return num - 2**32
else:
return num


def _mul2012(num1, num2):
"""Multiply two numbers in 20.12 fixed point format."""
# Separated into a function because >> has surprising precedence
Expand Down Expand Up @@ -756,29 +747,23 @@ def __init__(self, filename):
_log.debug('opening tfm file %s', filename)
with open(filename, 'rb') as file:
header1 = file.read(24)
lh, bc, ec, nw, nh, nd = \
struct.unpack('!6H', header1[2:14])
lh, bc, ec, nw, nh, nd = struct.unpack('!6H', header1[2:14])
_log.debug('lh=%d, bc=%d, ec=%d, nw=%d, nh=%d, nd=%d',
lh, bc, ec, nw, nh, nd)
header2 = file.read(4*lh)
self.checksum, self.design_size = \
struct.unpack('!2I', header2[:8])
self.checksum, self.design_size = struct.unpack('!2I', header2[:8])
# there is also encoding information etc.
char_info = file.read(4*(ec-bc+1))
widths = file.read(4*nw)
heights = file.read(4*nh)
depths = file.read(4*nd)

widths = struct.unpack(f'!{nw}i', file.read(4*nw))
heights = struct.unpack(f'!{nh}i', file.read(4*nh))
depths = struct.unpack(f'!{nd}i', file.read(4*nd))
self.width, self.height, self.depth = {}, {}, {}
widths, heights, depths = \
[struct.unpack('!%dI' % (len(x)/4), x)
for x in (widths, heights, depths)]
for idx, char in enumerate(range(bc, ec+1)):
byte0 = char_info[4*idx]
byte1 = char_info[4*idx+1]
self.width[char] = _fix2comp(widths[byte0])
self.height[char] = _fix2comp(heights[byte1 >> 4])
self.depth[char] = _fix2comp(depths[byte1 & 0xf])
self.width[char] = widths[byte0]
self.height[char] = heights[byte1 >> 4]
self.depth[char] = depths[byte1 & 0xf]


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