151 lines
4.7 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
from . import DefaultTable
2014-09-19 18:19:28 +02:00
from .sbixGlyph import *
2014-09-19 16:21:23 +02:00
from .sbixStrike import *
2013-12-06 19:41:49 -05:00
import struct
2013-12-06 19:40:00 -05:00
"""
sbix Table organization:
UInt16 version
UInt16 flags
2014-09-22 16:58:55 +02:00
UInt32 numStrikes number of strikes (bitmap set for a specific size)
UInt32 strikeOffset[numStrikes] offsetEntries
2013-12-06 19:40:00 -05:00
(Variable) storage for bitmap sets
offsetEntry:
UInt32 strikeOffset Offset from begining of table to data for the individual strike
2013-12-06 19:40:00 -05:00
bitmap set:
2014-09-22 17:20:04 +02:00
UInt16 ppem
UInt16 resolution
UInt32 glyphDataOffset[numGlyphs+1]
2013-12-06 19:40:00 -05:00
(Variable) storage for bitmaps
offsetRecord:
UInt32 glyphDataOffset offset from start of bitmap set to individual bitmap
2013-12-06 19:40:00 -05:00
bitmap:
SInt16 originOffsetX 00 00
SInt16 originOffsetY 00 00
2013-12-06 19:40:00 -05:00
char[4] format data type, e.g. "png "
(Variable) bitmap data
"""
sbixHeaderFormat = """
>
version: H # Version number (set to 1)
flags: H # The only two bits used in the flags field are bits 0
# and 1. For historical reasons, bit 0 must always be 1.
# Bit 1 is a sbixDrawOutlines flag and is interpreted as
# follows:
# 0: Draw only 'sbix' bitmaps
# 1: Draw both 'sbix' bitmaps and outlines, in that
# order
2014-09-22 16:58:55 +02:00
numStrikes: L # Number of bitmap strikes to follow
2013-12-06 19:40:00 -05:00
"""
sbixHeaderFormatSize = sstruct.calcsize(sbixHeaderFormat)
sbixStrikeOffsetFormat = """
>
strikeOffset: L # Offset from begining of table to data for the
# individual strike
2013-12-06 19:40:00 -05:00
"""
sbixStrikeOffsetFormatSize = sstruct.calcsize(sbixStrikeOffsetFormat)
2013-12-06 19:40:00 -05:00
class table__s_b_i_x(DefaultTable.DefaultTable):
def __init__(self, tag):
self.tableTag = tag
self.version = 1
self.flags = 1
2014-09-22 16:58:55 +02:00
self.numStrikes = 0
2013-12-06 19:40:00 -05:00
self.bitmapSets = {}
self.bitmapSetOffsets = []
def decompile(self, data, ttFont):
# read table header
sstruct.unpack(sbixHeaderFormat, data[ : sbixHeaderFormatSize], self)
# collect offsets to individual bitmap sets in self.bitmapSetOffsets
2014-09-22 16:58:55 +02:00
for i in range(self.numStrikes):
myOffset = sbixHeaderFormatSize + i * sbixStrikeOffsetFormatSize
2013-12-06 19:40:00 -05:00
offsetEntry = sbixBitmapSetOffset()
sstruct.unpack(sbixStrikeOffsetFormat, \
data[myOffset : myOffset+sbixStrikeOffsetFormatSize], \
2013-12-06 19:40:00 -05:00
offsetEntry)
self.bitmapSetOffsets.append(offsetEntry.strikeOffset)
2013-12-06 19:40:00 -05:00
2014-09-22 17:07:01 +02:00
# decompile Strikes
2014-09-22 16:58:55 +02:00
for i in range(self.numStrikes-1, -1, -1):
2014-09-22 17:07:01 +02:00
myBitmapSet = Strike(rawdata=data[self.bitmapSetOffsets[i]:])
2013-12-06 19:40:00 -05:00
data = data[:self.bitmapSetOffsets[i]]
myBitmapSet.decompile(ttFont)
2014-09-22 17:07:01 +02:00
#print " Strike length: %xh" % len(bitmapSetData)
2013-12-06 19:40:00 -05:00
#print "Number of Bitmaps:", myBitmapSet.numBitmaps
2014-09-22 17:20:04 +02:00
if myBitmapSet.ppem in self.bitmapSets:
2013-12-06 19:40:00 -05:00
from fontTools import ttLib
2014-09-22 17:20:04 +02:00
raise ttLib.TTLibError("Pixel 'ppem' must be unique for each Strike")
self.bitmapSets[myBitmapSet.ppem] = myBitmapSet
2013-12-06 19:40:00 -05:00
# after the bitmaps have been extracted, we don't need the offsets anymore
del self.bitmapSetOffsets
def compile(self, ttFont):
sbixData = ""
2014-09-22 16:58:55 +02:00
self.numStrikes = len(self.bitmapSets)
2013-12-06 19:40:00 -05:00
sbixHeader = sstruct.pack(sbixHeaderFormat, self)
# calculate offset to start of first bitmap set
setOffset = sbixHeaderFormatSize + sbixStrikeOffsetFormatSize * self.numStrikes
2013-12-06 19:40:00 -05:00
for si in sorted(self.bitmapSets.keys()):
myBitmapSet = self.bitmapSets[si]
myBitmapSet.compile(ttFont)
# append offset to this bitmap set to table header
myBitmapSet.strikeOffset = setOffset
sbixHeader += sstruct.pack(sbixStrikeOffsetFormat, myBitmapSet)
setOffset += len(myBitmapSet.data)
2013-12-06 19:40:00 -05:00
sbixData += myBitmapSet.data
return sbixHeader + sbixData
def toXML(self, xmlWriter, ttFont):
xmlWriter.simpletag("version", value=self.version)
2013-12-06 19:40:00 -05:00
xmlWriter.newline()
xmlWriter.simpletag("flags", value=self.flags)
2013-12-06 19:40:00 -05:00
xmlWriter.newline()
for i in sorted(self.bitmapSets.keys()):
self.bitmapSets[i].toXML(xmlWriter, ttFont)
def fromXML(self, name, attrs, content, ttFont):
if name in ["version", "flags"]:
2013-12-06 19:40:00 -05:00
setattr(self, name, int(attrs["value"]))
2014-09-22 17:07:01 +02:00
elif name == "strike":
myBitmapSet = Strike()
2013-12-06 19:40:00 -05:00
for element in content:
if isinstance(element, tuple):
name, attrs, content = element
myBitmapSet.fromXML(name, attrs, content, ttFont)
2014-09-22 17:20:04 +02:00
self.bitmapSets[myBitmapSet.ppem] = myBitmapSet
2013-12-06 19:40:00 -05:00
else:
from fontTools import ttLib
raise ttLib.TTLibError("can't handle '%s' element" % name)
2013-12-06 19:40:00 -05:00
# Helper classes
class sbixBitmapSetOffset(object):
pass