py23 introduce byteord() and use it
diff --git a/Lib/fontTools/cffLib.py b/Lib/fontTools/cffLib.py
index f976e16..e5f9f73 100644
--- a/Lib/fontTools/cffLib.py
+++ b/Lib/fontTools/cffLib.py
@@ -608,7 +608,7 @@
def readCard8(file):
- return ord(file.read(1))
+ return byteord(file.read(1))
def readCard16(file):
value, = struct.unpack(">H", file.read(2))
diff --git a/Lib/fontTools/fondLib.py b/Lib/fontTools/fondLib.py
index 2daa771..4b7b04d 100644
--- a/Lib/fontTools/fondLib.py
+++ b/Lib/fontTools/fondLib.py
@@ -268,7 +268,7 @@
kerntable = self.kernTables[stylecode] = []
for j in range(numberofpairs):
firstchar, secondchar, kerndistance = struct.unpack(">cch", data[count:count+4])
- kerntable.append((ord(firstchar), ord(secondchar), kerndistance))
+ kerntable.append((byteord(firstchar), byteord(secondchar), kerndistance))
count = count + 4
if DEBUG:
@@ -313,7 +313,7 @@
count = offset + 60
for i in range(stringcount):
- str_len = ord(data[count])
+ str_len = byteord(data[count])
self.styleStrings.append(data[count + 1:count + 1 + str_len])
count = count + 1 + str_len
@@ -357,7 +357,7 @@
psNames[i] = self.styleStrings[0]
else:
style = self.styleStrings[0]
- codes = map(ord, self.styleStrings[index - 1])
+ codes = map(byteord, self.styleStrings[index - 1])
for code in codes:
style = style + self.styleStrings[code - 1]
psNames[i] = style
@@ -410,9 +410,9 @@
numberofentries, = struct.unpack(">h", data[offset:offset+2])
count = offset + 2
for i in range(numberofentries):
- glyphcode = ord(data[count])
+ glyphcode = byteord(data[count])
count = count + 1
- strlen = ord(data[count])
+ strlen = byteord(data[count])
count = count + 1
glyphname = data[count:count+strlen]
glyphEncoding[glyphcode] = glyphname
diff --git a/Lib/fontTools/misc/eexec.py b/Lib/fontTools/misc/eexec.py
index 84f9705..c6c8f7b 100644
--- a/Lib/fontTools/misc/eexec.py
+++ b/Lib/fontTools/misc/eexec.py
@@ -13,13 +13,13 @@
def _decryptChar(cipher, R):
- cipher = ord(cipher)
+ cipher = byteord(cipher)
plain = ( (cipher ^ (R>>8)) ) & 0xFF
R = ( (cipher + R) * 52845 + 22719 ) & 0xFFFF
return bytechr(plain), R
def _encryptChar(plain, R):
- plain = ord(plain)
+ plain = byteord(plain)
cipher = ( (plain ^ (R>>8)) ) & 0xFF
R = ( (cipher + R) * 52845 + 22719 ) & 0xFFFF
return bytechr(cipher), R
diff --git a/Lib/fontTools/misc/homeResFile.py b/Lib/fontTools/misc/homeResFile.py
index ae4a0d2..409f30d 100644
--- a/Lib/fontTools/misc/homeResFile.py
+++ b/Lib/fontTools/misc/homeResFile.py
@@ -79,7 +79,7 @@
def getFileName(self):
self.getInfo()
data = self.__fileName.tostring()
- return data[1:ord(data[0])+1]
+ return data[1:byteord(data[0])+1]
def getFSSpec(self):
self.getInfo()
diff --git a/Lib/fontTools/misc/psCharStrings.py b/Lib/fontTools/misc/psCharStrings.py
index b86308c..41e2d01 100644
--- a/Lib/fontTools/misc/psCharStrings.py
+++ b/Lib/fontTools/misc/psCharStrings.py
@@ -41,11 +41,11 @@
return b0 - 139, index
def read_smallInt1(self, b0, data, index):
- b1 = ord(data[index])
+ b1 = byteord(data[index])
return (b0-247)*256 + b1 + 108, index+1
def read_smallInt2(self, b0, data, index):
- b1 = ord(data[index])
+ b1 = byteord(data[index])
return -(b0-251)*256 - b1 - 108, index+1
def read_shortInt(self, b0, data, index):
@@ -66,7 +66,7 @@
def read_realNumber(self, b0, data, index):
number = ''
while True:
- b = ord(data[index])
+ b = byteord(data[index])
index = index + 1
nibble0 = (b & 0xf0) >> 4
nibble1 = b & 0x0f
@@ -320,11 +320,11 @@
self.program = None
def getToken(self, index,
- len=len, ord=ord, getattr=getattr, type=type, StringType=str):
+ len=len, byteord=byteord, getattr=getattr, type=type, StringType=str):
if self.bytecode is not None:
if index >= len(self.bytecode):
return None, 0, 0
- b0 = ord(self.bytecode[index])
+ b0 = byteord(self.bytecode[index])
index = index + 1
code = self.operandEncoding[b0]
handler = getattr(self, code)
@@ -350,7 +350,7 @@
def do_operator(self, b0, data, index):
if b0 == 12:
- op = (b0, ord(data[index]))
+ op = (b0, byteord(data[index]))
index = index+1
else:
op = b0
@@ -374,7 +374,7 @@
hintMask, isOperator, index = self.getToken(index)
bits = []
for byte in hintMask:
- bits.append(num2binary(ord(byte), 8))
+ bits.append(num2binary(byteord(byte), 8))
hintMask = ''.join(bits)
line = ' '.join(args + [token, hintMask])
else:
@@ -1114,7 +1114,7 @@
lenData = len(data)
push = self.stack.append
while index < lenData:
- b0 = ord(data[index])
+ b0 = byteord(data[index])
index = index + 1
code = self.operandEncoding[b0]
handler = getattr(self, code)
@@ -1134,7 +1134,7 @@
def do_operator(self, b0, data, index):
if b0 == 12:
- op = (b0, ord(data[index]))
+ op = (b0, byteord(data[index]))
index = index+1
else:
op = b0
diff --git a/Lib/fontTools/misc/py23.py b/Lib/fontTools/misc/py23.py
index 2797505..3aee801 100644
--- a/Lib/fontTools/misc/py23.py
+++ b/Lib/fontTools/misc/py23.py
@@ -15,10 +15,13 @@
try:
unichr
bytechr = chr
+ byteord = ord
except:
unichr = chr
def bytechr(n):
return bytes([n])
+ def byteord(c):
+ return ord(c) if isinstance(c, str) else c
try:
from cStringIO import StringIO
diff --git a/Lib/fontTools/misc/textTools.py b/Lib/fontTools/misc/textTools.py
index 6e3418b..1820acd 100644
--- a/Lib/fontTools/misc/textTools.py
+++ b/Lib/fontTools/misc/textTools.py
@@ -31,7 +31,7 @@
h = string.hexdigits
r = ''
for c in data:
- i = ord(c)
+ i = byteord(c)
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
return r
diff --git a/Lib/fontTools/misc/xmlWriter.py b/Lib/fontTools/misc/xmlWriter.py
index 9679c32..4efd3f6 100644
--- a/Lib/fontTools/misc/xmlWriter.py
+++ b/Lib/fontTools/misc/xmlWriter.py
@@ -134,7 +134,7 @@
def escape8bit(data):
def escapechar(c):
- n = ord(c)
+ n = byteord(c)
if c in "<&":
if c == "&":
return "&"
@@ -154,7 +154,7 @@
a.fromstring(data)
if needswap:
a.byteswap()
- def escapenum(n, amp=ord("&"), lt=ord("<")):
+ def escapenum(n, amp=byteord("&"), lt=byteord("<")):
if n == amp:
return "&"
elif n == lt:
@@ -170,7 +170,7 @@
h = string.hexdigits
r = ''
for c in s:
- i = ord(c)
+ i = byteord(c)
r = r + h[(i >> 4) & 0xF] + h[i & 0xF]
return r
diff --git a/Lib/fontTools/nfntLib.py b/Lib/fontTools/nfntLib.py
index d56180a..0e54493 100644
--- a/Lib/fontTools/nfntLib.py
+++ b/Lib/fontTools/nfntLib.py
@@ -59,8 +59,8 @@
self.widthTable = []
self.locTable = []
for i in range(0, tableSize, 2):
- self.offsetTable.append(ord(owTable[i]))
- self.widthTable.append(ord(owTable[i+1]))
+ self.offsetTable.append(byteord(owTable[i]))
+ self.widthTable.append(byteord(owTable[i+1]))
loc, = struct.unpack("h", locTable[i:i+2])
self.locTable.append(loc)
@@ -93,7 +93,7 @@
byte = bits[y * rowBytes + xByte]
for xBit in range(8):
x = 8 * xByte + xBit
- bit = (ord(byte) >> (7 - xBit)) & 0x01
+ bit = (byteord(byte) >> (7 - xBit)) & 0x01
bitImage[x, y] = bit
for i in range(nGlyphs):
@@ -231,14 +231,14 @@
return width
def charwidth(self, ch):
- cindex = ord(ch) - self.firstChar
+ cindex = byteord(ch) - self.firstChar
if cindex > self.lastChar or \
(self.offsetTable[cindex] == 255 and self.widthTable[cindex] == 255):
cindex = -2 # missing char
return self.widthTable[cindex]
def getcharbounds(self, ch):
- cindex = ord(ch) - self.firstChar
+ cindex = byteord(ch) - self.firstChar
if cindex > self.lastChar or \
(self.offsetTable[cindex] == 255 and self.widthTable[cindex] == 255):
return self.getcharboundsindex(-2) # missing char
diff --git a/Lib/fontTools/t1Lib.py b/Lib/fontTools/t1Lib.py
index ea03053..8021795 100644
--- a/Lib/fontTools/t1Lib.py
+++ b/Lib/fontTools/t1Lib.py
@@ -151,8 +151,8 @@
data = []
for i in range(501, 501 + n):
res = Res.Get1Resource('POST', i)
- code = ord(res.data[0])
- if ord(res.data[1]) != 0:
+ code = byteord(res.data[0])
+ if byteord(res.data[1]) != 0:
raise T1Error('corrupt LWFN file')
if code in [1, 2]:
if onlyHeader and code == 2:
@@ -181,7 +181,7 @@
while True:
if f.read(1) != bytechr(128):
raise T1Error('corrupt PFB file')
- code = ord(f.read(1))
+ code = byteord(f.read(1))
if code in [1, 2]:
chunklen = stringToLong(f.read(4))
chunk = f.read(chunklen)
@@ -368,6 +368,6 @@
raise ValueError('string must be 4 bytes long')
long = 0
for i in range(4):
- long = long + (ord(str[i]) << (i * 8))
+ long = long + (byteord(str[i]) << (i * 8))
return long
diff --git a/Lib/fontTools/ttLib/__init__.py b/Lib/fontTools/ttLib/__init__.py
index 2ecb13d..93f6a87 100644
--- a/Lib/fontTools/ttLib/__init__.py
+++ b/Lib/fontTools/ttLib/__init__.py
@@ -815,7 +815,7 @@
elif re.match("[A-Z]", c):
return c + "_"
else:
- return hex(ord(c))[2:]
+ return hex(byteord(c))[2:]
def tagToIdentifier(tag):
diff --git a/Lib/fontTools/ttLib/tables/E_B_D_T_.py b/Lib/fontTools/ttLib/tables/E_B_D_T_.py
index c41370d..365d6a1 100644
--- a/Lib/fontTools/ttLib/tables/E_B_D_T_.py
+++ b/Lib/fontTools/ttLib/tables/E_B_D_T_.py
@@ -203,7 +203,7 @@
def _data2binary(data, numBits):
binaryList = []
for curByte in data:
- value = ord(curByte)
+ value = byteord(curByte)
numBitsCut = min(8, numBits)
for i in range(numBitsCut):
if value & 0x1:
@@ -243,7 +243,7 @@
def _reverseBytes(data):
if len(data) != 1:
return "".join(map(_reverseBytes, data))
- byte = ord(data)
+ byte = byteord(data)
result = 0
for i in range(8):
result = result << 1
@@ -522,12 +522,12 @@
else:
numBitsCut = endBit - curBit
curByte = _reverseBytes(self.imageData[firstByteLoc])
- firstHalf = ord(curByte) >> cutPoint
+ firstHalf = byteord(curByte) >> cutPoint
firstHalf = ((1<<numBitsCut)-1) & firstHalf
newByte = firstHalf
if firstByteLoc < secondByteLoc and secondByteLoc < len(self.imageData):
curByte = _reverseBytes(self.imageData[secondByteLoc])
- secondHalf = ord(curByte) << numBitsCut
+ secondHalf = byteord(curByte) << numBitsCut
newByte = (firstHalf | secondHalf) & ((1<<numBits)-1)
dataList.append(bytechr(newByte))
@@ -559,7 +559,7 @@
numBitsCut = 8 - cutPoint
else:
numBitsCut = endBit - curBit
- curByte = ord(curByte)
+ curByte = byteord(curByte)
firstByte = curByte & ((1<<numBitsCut)-1)
ordDataList[firstByteLoc] |= (firstByte << cutPoint)
if firstByteLoc < secondByteLoc and secondByteLoc < numBytes:
diff --git a/Lib/fontTools/ttLib/tables/M_E_T_A_.py b/Lib/fontTools/ttLib/tables/M_E_T_A_.py
index 2a0da8f..0ec42a9 100644
--- a/Lib/fontTools/ttLib/tables/M_E_T_A_.py
+++ b/Lib/fontTools/ttLib/tables/M_E_T_A_.py
@@ -238,6 +238,7 @@
def __repr__(self):
return "GlyphRecord[ glyphID: " + str(self.glyphID) + ", nMetaEntry: " + str(self.nMetaEntry) + ", offset: " + str(self.offset) + " ]"
+# XXX The following two functions are really broken around UTF-8 vs Unicode
def mapXMLToUTF8(string):
uString = unicode()
@@ -258,7 +259,7 @@
uString = uString + unichr(eval('0x' + valStr))
else:
- uString = uString + unichr(ord(string[i]))
+ uString = uString + unichr(byteord(string[i]))
i = i +1
return uString.encode('utf8')
@@ -268,7 +269,7 @@
uString = string.decode('utf8')
string = bytes()
for uChar in uString:
- i = ord(uChar)
+ i = byteord(uChar)
if (i < 0x80) and (i > 0x1F):
string = string + bytechr(i)
else:
diff --git a/Lib/fontTools/ttLib/tables/S_I_N_G_.py b/Lib/fontTools/ttLib/tables/S_I_N_G_.py
index 498de6b..707bab9 100644
--- a/Lib/fontTools/ttLib/tables/S_I_N_G_.py
+++ b/Lib/fontTools/ttLib/tables/S_I_N_G_.py
@@ -29,20 +29,20 @@
def decompile(self, data, ttFont):
dummy, rest = sstruct.unpack2(SINGFormat, data, self)
self.uniqueName = self.decompileUniqueName(self.uniqueName)
- self.nameLength = ord(self.nameLength)
+ self.nameLength = byteord(self.nameLength)
assert len(rest) == self.nameLength
self.baseGlyphName = rest
rawMETAMD5 = self.METAMD5
- self.METAMD5 = "[" + hex(ord(self.METAMD5[0]))
+ self.METAMD5 = "[" + hex(byteord(self.METAMD5[0]))
for char in rawMETAMD5[1:]:
- self.METAMD5 = self.METAMD5 + ", " + hex(ord(char))
+ self.METAMD5 = self.METAMD5 + ", " + hex(byteord(char))
self.METAMD5 = self.METAMD5 + "]"
def decompileUniqueName(self, data):
name = ""
for char in data:
- val = ord(char)
+ val = byteord(char)
if val == 0:
break
if (val > 31) or (val < 128):
diff --git a/Lib/fontTools/ttLib/tables/_g_l_y_f.py b/Lib/fontTools/ttLib/tables/_g_l_y_f.py
index 4c306ba..4e1d929 100644
--- a/Lib/fontTools/ttLib/tables/_g_l_y_f.py
+++ b/Lib/fontTools/ttLib/tables/_g_l_y_f.py
@@ -426,11 +426,11 @@
yFormat = ">" # big endian
i = j = 0
while True:
- flag = ord(data[i])
+ flag = byteord(data[i])
i = i + 1
repeat = 1
if flag & flagRepeat:
- repeat = ord(data[i]) + 1
+ repeat = byteord(data[i]) + 1
i = i + 1
for k in range(repeat):
if flag & flagXShort:
@@ -971,7 +971,7 @@
def reprflag(flag):
bin = ""
if isinstance(flag, str):
- flag = ord(flag)
+ flag = byteord(flag)
while flag:
if flag & 0x01:
bin = "1" + bin
diff --git a/Lib/fontTools/ttLib/tables/_h_d_m_x.py b/Lib/fontTools/ttLib/tables/_h_d_m_x.py
index 4b0875c..802227e 100644
--- a/Lib/fontTools/ttLib/tables/_h_d_m_x.py
+++ b/Lib/fontTools/ttLib/tables/_h_d_m_x.py
@@ -18,11 +18,11 @@
dummy, data = sstruct.unpack2(hdmxHeaderFormat, data, self)
self.hdmx = {}
for i in range(self.numRecords):
- ppem = ord(data[0])
- maxSize = ord(data[1])
+ ppem = byteord(data[0])
+ maxSize = byteord(data[1])
widths = {}
for glyphID in range(numGlyphs):
- widths[glyphOrder[glyphID]] = ord(data[glyphID+2])
+ widths[glyphOrder[glyphID]] = byteord(data[glyphID+2])
self.hdmx[ppem] = widths
data = data[self.recordSize:]
assert len(data) == 0, "too much hdmx data"
diff --git a/Lib/fontTools/ttLib/tables/_h_e_a_d.py b/Lib/fontTools/ttLib/tables/_h_e_a_d.py
index a95f50f..3ef1717 100644
--- a/Lib/fontTools/ttLib/tables/_h_e_a_d.py
+++ b/Lib/fontTools/ttLib/tables/_h_e_a_d.py
@@ -134,7 +134,7 @@
def bin2long(data):
# thanks </F>!
v = 0
- for i in map(ord, data):
+ for i in map(byteord, data):
v = v<<8 | i
return v
diff --git a/Lib/fontTools/ttLib/tables/_p_o_s_t.py b/Lib/fontTools/ttLib/tables/_p_o_s_t.py
index 8ca10b0..118559b 100644
--- a/Lib/fontTools/ttLib/tables/_p_o_s_t.py
+++ b/Lib/fontTools/ttLib/tables/_p_o_s_t.py
@@ -217,7 +217,7 @@
index = 0
dataLen = len(data)
while index < dataLen:
- length = ord(data[index])
+ length = byteord(data[index])
strings.append(data[index+1:index+1+length])
index = index + 1 + length
return strings
diff --git a/Lib/fontTools/ttLib/tables/otBase.py b/Lib/fontTools/ttLib/tables/otBase.py
index 426a079..f42539e 100644
--- a/Lib/fontTools/ttLib/tables/otBase.py
+++ b/Lib/fontTools/ttLib/tables/otBase.py
@@ -137,7 +137,7 @@
def readUInt24(self):
pos = self.pos
newpos = pos + 3
- value = (ord(self.data[pos]) << 16) | (ord(self.data[pos+1]) << 8) | ord(self.data[pos+2])
+ value = (byteord(self.data[pos]) << 16) | (byteord(self.data[pos+1]) << 8) | byteord(self.data[pos+2])
value, = struct.unpack(">H", self.data[pos:newpos])
self.pos = newpos
return value