2to3 sbix code

This seems to work now.

At some point we should go back and clean up and merge datastructures
between the four different color font formats.
This commit is contained in:
Behdad Esfahbod 2013-12-06 19:47:32 -05:00
parent 1b8cde1b6f
commit de85e4599f
3 changed files with 21 additions and 19 deletions

View File

@ -88,7 +88,7 @@ class table__s_b_i_x(DefaultTable.DefaultTable):
#print "Number of Bitmaps:", myBitmapSet.numBitmaps #print "Number of Bitmaps:", myBitmapSet.numBitmaps
if myBitmapSet.size in self.bitmapSets: if myBitmapSet.size in self.bitmapSets:
from fontTools import ttLib from fontTools import ttLib
raise(ttLib.TTLibError, "Pixel 'size' must be unique for each BitmapSet") raise ttLib.TTLibError("Pixel 'size' must be unique for each BitmapSet")
self.bitmapSets[myBitmapSet.size] = myBitmapSet self.bitmapSets[myBitmapSet.size] = myBitmapSet
# after the bitmaps have been extracted, we don't need the offsets anymore # after the bitmaps have been extracted, we don't need the offsets anymore
@ -121,18 +121,19 @@ class table__s_b_i_x(DefaultTable.DefaultTable):
for i in sorted(self.bitmapSets.keys()): for i in sorted(self.bitmapSets.keys()):
self.bitmapSets[i].toXML(xmlWriter, ttFont) self.bitmapSets[i].toXML(xmlWriter, ttFont)
def fromXML(self, (name, attrs, content), ttFont): def fromXML(self, name, attrs, content, ttFont):
if name in ["usVal1", "usVal2"]: if name in ["usVal1", "usVal2"]:
setattr(self, name, int(attrs["value"])) setattr(self, name, int(attrs["value"]))
elif name == "bitmapSet": elif name == "bitmapSet":
myBitmapSet = BitmapSet() myBitmapSet = BitmapSet()
for element in content: for element in content:
if type(element) == TupleType: if isinstance(element, tuple):
myBitmapSet.fromXML(element, ttFont) name, attrs, content = element
myBitmapSet.fromXML(name, attrs, content, ttFont)
self.bitmapSets[myBitmapSet.size] = myBitmapSet self.bitmapSets[myBitmapSet.size] = myBitmapSet
else: else:
from fontTools import ttLib from fontTools import ttLib
raise ttLib.TTLibError, "can't handle '%s' element" % name raise ttLib.TTLibError("can't handle '%s' element" % name)
# Helper classes # Helper classes

View File

@ -30,12 +30,12 @@ class Bitmap(object):
self.glyphName = ttFont.getGlyphName(self.gid) self.glyphName = ttFont.getGlyphName(self.gid)
if self.rawdata is None: if self.rawdata is None:
from fontTools import ttLib from fontTools import ttLib
raise(ttLib.TTLibError, "No table data to decompile.") raise ttLib.TTLibError("No table data to decompile")
if len(self.rawdata) > 0: if len(self.rawdata) > 0:
if len(self.rawdata) < sbixBitmapHeaderFormatSize: if len(self.rawdata) < sbixBitmapHeaderFormatSize:
from fontTools import ttLib from fontTools import ttLib
#print "Bitmap %i header too short: Expected %x, got %x." % (self.gid, sbixBitmapHeaderFormatSize, len(self.rawdata)) #print "Bitmap %i header too short: Expected %x, got %x." % (self.gid, sbixBitmapHeaderFormatSize, len(self.rawdata))
raise(ttLib.TTLibError, "Bitmap header too short.") raise ttLib.TTLibError("Bitmap header too short.")
sstruct.unpack(sbixBitmapHeaderFormat, self.rawdata[:sbixBitmapHeaderFormatSize], self) sstruct.unpack(sbixBitmapHeaderFormat, self.rawdata[:sbixBitmapHeaderFormatSize], self)
@ -53,7 +53,7 @@ class Bitmap(object):
def compile(self, ttFont): def compile(self, ttFont):
if self.glyphName is None: if self.glyphName is None:
from fontTools import ttLib from fontTools import ttLib
raise ttLib.TTLibError, "Can't compile bitmap without glyph name" raise ttLib.TTLibError("Can't compile bitmap without glyph name")
# TODO: if ttFont has no maxp, cmap etc., ignore glyph names and compile by index? # TODO: if ttFont has no maxp, cmap etc., ignore glyph names and compile by index?
# (needed if you just want to compile the sbix table on its own) # (needed if you just want to compile the sbix table on its own)
self.gid = struct.pack(">H", ttFont.getGlyphID(self.glyphName)) self.gid = struct.pack(">H", ttFont.getGlyphID(self.glyphName))
@ -88,7 +88,7 @@ class Bitmap(object):
xmlWriter.endtag("bitmap") xmlWriter.endtag("bitmap")
xmlWriter.newline() xmlWriter.newline()
def fromXML(self, (name, attrs, content), ttFont): def fromXML(self, name, attrs, content, ttFont):
#if name in ["usReserved1", "usReserved2"]: #if name in ["usReserved1", "usReserved2"]:
# setattr(self, name, int(attrs["value"])) # setattr(self, name, int(attrs["value"]))
#elif #elif
@ -101,4 +101,4 @@ class Bitmap(object):
self.imageData = readHex(content) self.imageData = readHex(content)
else: else:
from fontTools import ttLib from fontTools import ttLib
raise ttLib.TTLibError, "can't handle '%s' element" % name raise ttLib.TTLibError("can't handle '%s' element" % name)

View File

@ -30,7 +30,7 @@ class BitmapSet(object):
def decompile(self, ttFont): def decompile(self, ttFont):
if self.data is None: if self.data is None:
from fontTools import ttLib from fontTools import ttLib
raise(ttLib.TTLibError, "No table data to decompile.") raise ttLib.TTLibError
if len(self.data) < sbixBitmapSetHeaderFormatSize: if len(self.data) < sbixBitmapSetHeaderFormatSize:
from fontTools import ttLib from fontTools import ttLib
raise(ttLib.TTLibError, "BitmapSet header too short: Expected %x, got %x.") \ raise(ttLib.TTLibError, "BitmapSet header too short: Expected %x, got %x.") \
@ -42,7 +42,7 @@ class BitmapSet(object):
# calculate number of bitmaps # calculate number of bitmaps
firstBitmapOffset, = struct.unpack(">L", \ firstBitmapOffset, = struct.unpack(">L", \
self.data[sbixBitmapSetHeaderFormatSize : sbixBitmapSetHeaderFormatSize + sbixBitmapOffsetEntryFormatSize]) self.data[sbixBitmapSetHeaderFormatSize : sbixBitmapSetHeaderFormatSize + sbixBitmapOffsetEntryFormatSize])
self.numBitmaps = (firstBitmapOffset - sbixBitmapSetHeaderFormatSize) / sbixBitmapOffsetEntryFormatSize - 1 self.numBitmaps = (firstBitmapOffset - sbixBitmapSetHeaderFormatSize) // sbixBitmapOffsetEntryFormatSize - 1
# ^ -1 because there's one more offset than bitmaps # ^ -1 because there's one more offset than bitmaps
# build offset list for single bitmap offsets # build offset list for single bitmap offsets
@ -113,25 +113,26 @@ class BitmapSet(object):
xmlWriter.endtag("bitmapSet") xmlWriter.endtag("bitmapSet")
xmlWriter.newline() xmlWriter.newline()
def fromXML(self, (name, attrs, content), ttFont): def fromXML(self, name, attrs, content, ttFont):
if name in ["size", "resolution"]: if name in ["size", "resolution"]:
setattr(self, name, int(attrs["value"])) setattr(self, name, int(attrs["value"]))
elif name == "bitmap": elif name == "bitmap":
if attrs.has_key("format"): if "format" in attrs:
myFormat = attrs["format"] myFormat = attrs["format"]
else: else:
myFormat = None myFormat = None
if attrs.has_key("glyphname"): if "glyphname" in attrs:
myGlyphName = attrs["glyphname"] myGlyphName = attrs["glyphname"]
else: else:
from fontTools import ttLib from fontTools import ttLib
raise ttLib.TTLibError, "Bitmap must have a glyph name." raise ttLib.TTLibError("Bitmap must have a glyph name.")
myBitmap = Bitmap(glyphName=myGlyphName, imageFormatTag=myFormat) myBitmap = Bitmap(glyphName=myGlyphName, imageFormatTag=myFormat)
for element in content: for element in content:
if type(element) == TupleType: if isinstance(element, tuple):
myBitmap.fromXML(element, ttFont) name, attrs, content = element
myBitmap.fromXML(name, attrs, content, ttFont)
myBitmap.compile(ttFont) myBitmap.compile(ttFont)
self.bitmaps[myBitmap.glyphName] = myBitmap self.bitmaps[myBitmap.glyphName] = myBitmap
else: else:
from fontTools import ttLib from fontTools import ttLib
raise ttLib.TTLibError, "can't handle '%s' element" % name raise ttLib.TTLibError("can't handle '%s' element" % name)