From b607a01f15f18ce1fdd1aa0509bc7d76f689a40e Mon Sep 17 00:00:00 2001
From: Antony Lee <anntzer.lee@gmail.com>
Date: Fri, 21 May 2021 15:21:30 +0200
Subject: [PATCH] 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.
---
 lib/matplotlib/dviread.py | 31 ++++++++-----------------------
 1 file changed, 8 insertions(+), 23 deletions(-)

diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py
index 06d86dcbde0a..750bc5d8f49e 100644
--- a/lib/matplotlib/dviread.py
+++ b/lib/matplotlib/dviread.py
@@ -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
@@ -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')