134 lines
4.8 KiB
Python
Raw Normal View History

from __future__ import print_function, division, absolute_import
2013-12-06 19:41:49 -05:00
from fontTools.misc.py23 import *
from fontTools.misc import sstruct
from fontTools.misc.textTools import readHex
2014-09-19 18:19:28 +02:00
from .sbixGlyph import *
2013-12-06 19:41:49 -05:00
import struct
2013-12-06 19:35:09 -05:00
sbixBitmapSetHeaderFormat = """
2014-09-22 17:20:04 +02:00
>
ppem: H # The PPEM for which this strike was designed (e.g., 9,
# 12, 24)
resolution: H # The screen resolution (in dpi) for which this strike
# was designed (e.g., 72)
2013-12-06 19:35:09 -05:00
"""
sbixBitmapOffsetEntryFormat = """
>
ulOffset: L # 00 00 07 E0 # Offset from start of first offset entry to each bitmap
"""
sbixBitmapSetHeaderFormatSize = sstruct.calcsize(sbixBitmapSetHeaderFormat)
sbixBitmapOffsetEntryFormatSize = sstruct.calcsize(sbixBitmapOffsetEntryFormat)
2014-09-22 17:07:01 +02:00
class Strike(object):
2014-09-22 17:20:04 +02:00
def __init__(self, rawdata=None, ppem=0, resolution=72):
2013-12-06 19:35:09 -05:00
self.data = rawdata
2014-09-22 17:20:04 +02:00
self.ppem = ppem
2013-12-06 19:35:09 -05:00
self.resolution = resolution
self.bitmaps = {}
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
def decompile(self, ttFont):
if self.data is None:
from fontTools import ttLib
raise ttLib.TTLibError
2013-12-06 19:35:09 -05:00
if len(self.data) < sbixBitmapSetHeaderFormatSize:
from fontTools import ttLib
2014-09-22 17:07:01 +02:00
raise(ttLib.TTLibError, "Strike header too short: Expected %x, got %x.") \
2013-12-06 19:35:09 -05:00
% (sbixBitmapSetHeaderFormatSize, len(self.data))
2013-12-06 19:40:00 -05:00
2014-09-22 17:07:01 +02:00
# read Strike header from raw data
2013-12-06 19:35:09 -05:00
sstruct.unpack(sbixBitmapSetHeaderFormat, self.data[:sbixBitmapSetHeaderFormatSize], self)
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# calculate number of bitmaps
firstBitmapOffset, = struct.unpack(">L", \
self.data[sbixBitmapSetHeaderFormatSize : sbixBitmapSetHeaderFormatSize + sbixBitmapOffsetEntryFormatSize])
self.numBitmaps = (firstBitmapOffset - sbixBitmapSetHeaderFormatSize) // sbixBitmapOffsetEntryFormatSize - 1
2013-12-06 19:35:09 -05:00
# ^ -1 because there's one more offset than bitmaps
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# build offset list for single bitmap offsets
self.bitmapOffsets = []
for i in range(self.numBitmaps + 1): # + 1 because there's one more offset than bitmaps
start = i * sbixBitmapOffsetEntryFormatSize + sbixBitmapSetHeaderFormatSize
myOffset, = struct.unpack(">L", self.data[start : start + sbixBitmapOffsetEntryFormatSize])
self.bitmapOffsets.append(myOffset)
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# iterate through offset list and slice raw data into bitmaps
for i in range(self.numBitmaps):
myBitmap = Bitmap(rawdata=self.data[self.bitmapOffsets[i] : self.bitmapOffsets[i+1]], gid=i)
myBitmap.decompile(ttFont)
self.bitmaps[myBitmap.glyphName] = myBitmap
del self.bitmapOffsets
del self.data
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
def compile(self, ttFont):
self.bitmapOffsets = ""
self.bitmapData = ""
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
glyphOrder = ttFont.getGlyphOrder()
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# first bitmap starts right after the header
bitmapOffset = sbixBitmapSetHeaderFormatSize + sbixBitmapOffsetEntryFormatSize * (len(glyphOrder) + 1)
for glyphName in glyphOrder:
if glyphName in self.bitmaps:
# we have a bitmap for this glyph
myBitmap = self.bitmaps[glyphName]
else:
# must add empty bitmap for this glyph
myBitmap = Bitmap(glyphName=glyphName)
myBitmap.compile(ttFont)
myBitmap.ulOffset = bitmapOffset
self.bitmapData += myBitmap.rawdata
bitmapOffset += len(myBitmap.rawdata)
self.bitmapOffsets += sstruct.pack(sbixBitmapOffsetEntryFormat, myBitmap)
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# add last "offset", really the end address of the last bitmap
dummy = Bitmap()
dummy.ulOffset = bitmapOffset
self.bitmapOffsets += sstruct.pack(sbixBitmapOffsetEntryFormat, dummy)
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# pack header
self.data = sstruct.pack(sbixBitmapSetHeaderFormat, self)
2014-09-22 16:02:36 +02:00
# add offsets and image data after header
self.data += self.bitmapOffsets + self.bitmapData
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
def toXML(self, xmlWriter, ttFont):
2014-09-22 17:07:01 +02:00
xmlWriter.begintag("strike")
2013-12-06 19:35:09 -05:00
xmlWriter.newline()
2014-09-22 17:20:04 +02:00
xmlWriter.simpletag("ppem", value=self.ppem)
2013-12-06 19:35:09 -05:00
xmlWriter.newline()
xmlWriter.simpletag("resolution", value=self.resolution)
xmlWriter.newline()
glyphOrder = ttFont.getGlyphOrder()
for i in range(len(glyphOrder)):
if glyphOrder[i] in self.bitmaps:
self.bitmaps[glyphOrder[i]].toXML(xmlWriter, ttFont)
# TODO: what if there are more bitmaps than glyphs?
2014-09-22 17:07:01 +02:00
xmlWriter.endtag("strike")
2013-12-06 19:35:09 -05:00
xmlWriter.newline()
2013-12-06 19:40:00 -05:00
def fromXML(self, name, attrs, content, ttFont):
2014-09-22 17:20:04 +02:00
if name in ["ppem", "resolution"]:
2013-12-06 19:35:09 -05:00
setattr(self, name, int(attrs["value"]))
elif name == "bitmap":
if "format" in attrs:
2013-12-06 19:35:09 -05:00
myFormat = attrs["format"]
else:
myFormat = None
if "glyphname" in attrs:
2013-12-06 19:35:09 -05:00
myGlyphName = attrs["glyphname"]
else:
from fontTools import ttLib
raise ttLib.TTLibError("Bitmap must have a glyph name.")
2013-12-06 19:35:09 -05:00
myBitmap = Bitmap(glyphName=myGlyphName, imageFormatTag=myFormat)
for element in content:
if isinstance(element, tuple):
name, attrs, content = element
myBitmap.fromXML(name, attrs, content, ttFont)
2013-12-06 19:35:09 -05:00
myBitmap.compile(ttFont)
self.bitmaps[myBitmap.glyphName] = myBitmap
else:
from fontTools import ttLib
raise ttLib.TTLibError("can't handle '%s' element" % name)