2929import numpy as np
3030import re
3131import struct
32+ import sys
33+
34+ if sys .version_info [0 ] >= 3 :
35+ def ord (x ):
36+ return x
3237
3338class Type1Font (object ):
3439 """
@@ -64,12 +69,12 @@ def _read(self, file):
6469 Read the font from a file, decoding into usable parts.
6570 """
6671 rawdata = file .read ()
67- if not rawdata .startswith (chr ( 128 ) ):
72+ if not rawdata .startswith (b' \x80 ' ):
6873 return rawdata
6974
70- data = ''
75+ data = b ''
7176 while len (rawdata ) > 0 :
72- if not rawdata .startswith (chr ( 128 ) ):
77+ if not rawdata .startswith (b' \x80 ' ):
7378 raise RuntimeError ('Broken pfb file (expected byte 128, got %d)' % \
7479 ord (rawdata [0 ]))
7580 type = ord (rawdata [1 ])
@@ -81,8 +86,8 @@ def _read(self, file):
8186 if type == 1 : # ASCII text: include verbatim
8287 data += segment
8388 elif type == 2 : # binary data: encode in hexadecimal
84- data += '' .join (['%02x' % ord (char )
85- for char in segment ])
89+ data += b '' .join ([( '%02x' % ord (char )). encode ( 'ascii' )
90+ for char in segment ])
8691 elif type == 3 : # end of file
8792 break
8893 else :
@@ -101,18 +106,19 @@ def _split(self, data):
101106 """
102107
103108 # Cleartext part: just find the eexec and skip whitespace
104- idx = data .index ('eexec' )
105- idx += len ('eexec' )
106- while data [idx ] in ' \t \r \n ' :
109+ idx = data .index (b 'eexec' )
110+ idx += len (b 'eexec' )
111+ while data [idx ] in b ' \t \r \n ' :
107112 idx += 1
108113 len1 = idx
109114
110115 # Encrypted part: find the cleartomark operator and count
111116 # zeros backward
112- idx = data .rindex ('cleartomark' ) - 1
117+ idx = data .rindex (b 'cleartomark' ) - 1
113118 zeros = 512
114- while zeros and data [idx ] in ('0' , '\n ' , '\r ' ):
115- if data [idx ] == '0' :
119+ while zeros and ord (data [idx ]) in (
120+ ord (b'0' [0 ]), ord (b'\n ' [0 ]), ord (b'\r ' [0 ])):
121+ if ord (data [idx ]) == ord (b'0' [0 ]):
116122 zeros -= 1
117123 idx -= 1
118124 if zeros :
@@ -123,15 +129,15 @@ def _split(self, data):
123129 # but if we read a pfa file, this part is already in hex, and
124130 # I am not quite sure if even the pfb format guarantees that
125131 # it will be in binary).
126- binary = '' .join ([chr (int (data [i :i + 2 ], 16 ))
127- for i in range (len1 , idx , 2 )])
132+ binary = b '' .join ([unichr (int (data [i :i + 2 ], 16 )). encode ( 'latin-1' )
133+ for i in range (len1 , idx , 2 )])
128134
129135 return data [:len1 ], binary , data [idx :]
130136
131- _whitespace = re .compile (r '[\0\t\r\014\n ]+' )
132- _token = re .compile (r '/{0,2}[^]\0\t\r\v\n ()<>{}/%[]+' )
133- _comment = re .compile (r '%[^\r\n\v]*' )
134- _instring = re .compile (r '[()\\]' )
137+ _whitespace = re .compile (br '[\0\t\r\014\n ]+' )
138+ _token = re .compile (br '/{0,2}[^]\0\t\r\v\n ()<>{}/%[]+' )
139+ _comment = re .compile (br '%[^\r\n\v]*' )
140+ _instring = re .compile (br '[()\\]' )
135141 @classmethod
136142 def _tokens (cls , text ):
137143 """
@@ -191,22 +197,22 @@ def _parse(self):
191197 tokenizer = self ._tokens (self .parts [0 ])
192198 filtered = itertools .ifilter (lambda x : x [0 ] != 'whitespace' , tokenizer )
193199 for token , value in filtered :
194- if token == 'name' and value .startswith ('/' ):
200+ if token == b 'name' and value .startswith (b '/' ):
195201 key = value [1 :]
196202 token , value = next (filtered )
197- if token == 'name' :
198- if value in ('true' , 'false' ):
199- value = value == 'true'
203+ if token == b 'name' :
204+ if value in (b 'true' , b 'false' ):
205+ value = value == b 'true'
200206 else :
201- value = value .lstrip ('/' )
202- elif token == 'string' :
203- value = value .lstrip ('(' ).rstrip (')' )
204- elif token == 'number' :
205- if '.' in value : value = float (value )
207+ value = value .lstrip (b '/' )
208+ elif token == b 'string' :
209+ value = value .lstrip (b '(' ).rstrip (b ')' )
210+ elif token == b 'number' :
211+ if b '.' in value : value = float (value )
206212 else : value = int (value )
207213 else : # more complicated value such as an array
208214 value = None
209- if key != 'FontInfo' and value is not None :
215+ if key != b 'FontInfo' and value is not None :
210216 prop [key ] = value
211217
212218 # Fill in the various *Name properties
0 commit comments