[emoji_builder|png] fix bytes vs str issues in py2.py3

pull/267/head
Cosimo Lupo 2019-10-21 16:17:16 +01:00
parent 03e6d6e39c
commit 60161a370b
No known key found for this signature in database
GPG Key ID: 20D4A261E4A0E642
2 changed files with 27 additions and 42 deletions

View File

@ -26,6 +26,12 @@ from os import path
from nototools import font_data from nototools import font_data
try:
unichr # py2
except NameError:
unichr = chr # py3
def get_glyph_name_from_gsub (string, font, cmap_dict): def get_glyph_name_from_gsub (string, font, cmap_dict):
ligatures = font['GSUB'].table.LookupList.Lookup[0].SubTable[0].ligatures ligatures = font['GSUB'].table.LookupList.Lookup[0].SubTable[0].ligatures
first_glyph = cmap_dict[ord (string[0])] first_glyph = cmap_dict[ord (string[0])]
@ -179,10 +185,7 @@ class CBDT:
self.write (pixel) self.write (pixel)
offset += stride offset += stride
png_allowed_chunks = [ png_allowed_chunks = [b"IHDR", b"PLTE", b"tRNS", b"sRGB", b"IDAT", b"IEND"]
"IHDR", "PLTE", "tRNS", "sRGB", "IDAT", "IEND", # Python2
b"IHDR", b"PLTE", b"tRNS", b"sRGB", b"IDAT", b"IEND", # Python3
]
def write_format17 (self, png): def write_format17 (self, png):
self.write_format17or18(png, False) self.write_format17or18(png, False)
@ -444,10 +447,7 @@ By default they are dropped.
def add_font_table (font, tag, data): def add_font_table (font, tag, data):
tab = ttLib.tables.DefaultTable.DefaultTable (tag) tab = ttLib.tables.DefaultTable.DefaultTable (tag)
if sys.version_info >= (3, 0, 0): tab.data = data
tab.data = data
else:
tab.data = str(data)
font[tag] = tab font[tag] = tab
def drop_outline_tables (font): def drop_outline_tables (font):
@ -498,19 +498,13 @@ By default they are dropped.
if "_" in codes: if "_" in codes:
pieces = codes.split ("_") pieces = codes.split ("_")
cps = [int(code, 16) for code in pieces] cps = [int(code, 16) for code in pieces]
if sys.version_info >= (3, 0, 0): uchars = "".join (unichr(cp) for cp in cps if not is_vs(cp))
uchars = "".join ([chr(cp) for cp in cps if not is_vs(cp)])
else:
uchars = "".join ([unichr(cp) for cp in cps if not is_vs(cp)])
else: else:
cp = int(codes, 16) cp = int(codes, 16)
if is_vs(cp): if is_vs(cp):
print("ignoring unexpected vs input %04x" % cp) print("ignoring unexpected vs input %04x" % cp)
continue continue
if sys.version_info >= (3, 0, 0): uchars = unichr(cp)
uchars = chr(cp)
else:
uchars = unichr(cp)
img_files[uchars] = img_file img_files[uchars] = img_file
if not img_files: if not img_files:
raise Exception ("No image files found in '%s'." % glb) raise Exception ("No image files found in '%s'." % glb)

View File

@ -19,10 +19,13 @@
import struct import struct
import sys import sys
if sys.version_info >= (3,0,0): # Python3 from io import BytesIO
from io import StringIO
else:
from StringIO import StringIO try:
basestring # py2
except NameError:
basestring = str # py3
class PNG: class PNG:
@ -31,7 +34,7 @@ class PNG:
def __init__ (self, f): def __init__ (self, f):
if (isinstance(f, str) or isinstance(f, type(u''))): if isinstance(f, basestring):
f = open (f, 'rb') f = open (f, 'rb')
self.f = f self.f = f
@ -48,10 +51,7 @@ class PNG:
def data (self): def data (self):
self.seek (0) self.seek (0)
if sys.version_info >= (3,0,0): # Python3 return bytearray (self.f.read ())
return bytearray (self.f.read (), 'iso-8859-1')
else:
return bytearray (self.f.read ())
class BadSignature (Exception): pass class BadSignature (Exception): pass
class BadChunk (Exception): pass class BadChunk (Exception): pass
@ -76,7 +76,7 @@ class PNG:
def read_IHDR (self): def read_IHDR (self):
(chunk_type, chunk_data, crc) = self.read_chunk () (chunk_type, chunk_data, crc) = self.read_chunk ()
if chunk_type not in ("IHDR", b"IHDR"): if chunk_type != b"IHDR":
raise PNG.BadChunk raise PNG.BadChunk
# Width: 4 bytes # Width: 4 bytes
# Height: 4 bytes # Height: 4 bytes
@ -102,24 +102,15 @@ class PNG:
def filter_chunks (self, chunks): def filter_chunks (self, chunks):
self.seek (0); self.seek (0);
out = StringIO () out = BytesIO ()
if sys.version_info >= (3,0,0): # Python3 out.write (self.read_signature ())
out.write (self.read_signature ().decode('iso-8859-1'))
else:
out.write (self.read_signature ())
while True: while True:
chunk_type, chunk_data, crc = self.read_chunk () chunk_type, chunk_data, crc = self.read_chunk ()
if chunk_type in chunks: if chunk_type in chunks:
if sys.version_info >= (3,0,0): # Python3 out.write (struct.pack (">I", len (chunk_data)))
out.write (struct.pack (">I", len (chunk_data)).decode('iso-8859-1')) out.write (chunk_type)
out.write (chunk_type.decode('iso-8859-1')) out.write (chunk_data)
out.write (chunk_data.decode('iso-8859-1')) out.write (crc)
out.write (crc.decode('iso-8859-1')) if chunk_type == b"IEND":
else:
out.write (struct.pack (">I", len (chunk_data)))
out.write (chunk_type)
out.write (chunk_data)
out.write (crc)
if chunk_type in ("IEND", b"IEND"):
break break
return PNG (out) return PNG (out)