2014-01-14 15:07:50 +08:00
|
|
|
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:
|
|
|
|
|
2014-09-22 16:54:43 +02:00
|
|
|
UInt16 version
|
|
|
|
UInt16 flags
|
2014-09-22 16:58:55 +02:00
|
|
|
UInt32 numStrikes number of strikes (bitmap set for a specific size)
|
|
|
|
offsetEntry offsetEntry[numStrikes] offsetEntries
|
2013-12-06 19:40:00 -05:00
|
|
|
(Variable) storage for bitmap sets
|
|
|
|
|
|
|
|
|
|
|
|
offsetEntry:
|
|
|
|
|
2014-09-22 16:54:43 +02:00
|
|
|
UInt32 offset offset from table start to bitmap set
|
2013-12-06 19:40:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
bitmap set:
|
|
|
|
|
2014-09-22 17:20:04 +02:00
|
|
|
UInt16 ppem
|
|
|
|
UInt16 resolution
|
|
|
|
UInt32 offsetRecord[numGlyphs+1]
|
2013-12-06 19:40:00 -05:00
|
|
|
(Variable) storage for bitmaps
|
|
|
|
|
|
|
|
|
|
|
|
offsetRecord:
|
|
|
|
|
2014-09-22 17:20:04 +02:00
|
|
|
UInt32 bitmapOffset offset from start of bitmap set to individual bitmap
|
2013-12-06 19:40:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
bitmap:
|
|
|
|
|
2014-09-22 16:54:43 +02:00
|
|
|
SInt16 reserved 00 00
|
|
|
|
SInt16 reserved 00 00
|
2013-12-06 19:40:00 -05:00
|
|
|
char[4] format data type, e.g. "png "
|
|
|
|
(Variable) bitmap data
|
|
|
|
"""
|
|
|
|
|
|
|
|
sbixHeaderFormat = """
|
2014-09-22 16:54:43 +02:00
|
|
|
>
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
sbixBitmapSetOffsetFormat = """
|
|
|
|
>
|
|
|
|
offset: L # 00 00 00 10 # offset from table start to each bitmap set
|
|
|
|
"""
|
|
|
|
sbixBitmapSetOffsetFormatSize = sstruct.calcsize(sbixBitmapSetOffsetFormat)
|
|
|
|
|
|
|
|
|
|
|
|
class table__s_b_i_x(DefaultTable.DefaultTable):
|
|
|
|
def __init__(self, tag):
|
|
|
|
self.tableTag = tag
|
2014-09-22 16:54:43 +02:00
|
|
|
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):
|
2013-12-06 19:40:00 -05:00
|
|
|
myOffset = sbixHeaderFormatSize + i * sbixBitmapSetOffsetFormatSize
|
|
|
|
offsetEntry = sbixBitmapSetOffset()
|
|
|
|
sstruct.unpack(sbixBitmapSetOffsetFormat, \
|
|
|
|
data[myOffset : myOffset+sbixBitmapSetOffsetFormatSize], \
|
|
|
|
offsetEntry)
|
|
|
|
self.bitmapSetOffsets.append(offsetEntry.offset)
|
|
|
|
|
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
|
2014-09-22 16:58:55 +02:00
|
|
|
setOffset = sbixHeaderFormatSize + sbixBitmapSetOffsetFormatSize * 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.offset = setOffset
|
|
|
|
sbixHeader += sstruct.pack(sbixBitmapSetOffsetFormat, myBitmapSet)
|
2014-09-22 15:38:02 +02:00
|
|
|
setOffset += len(myBitmapSet.data)
|
2013-12-06 19:40:00 -05:00
|
|
|
sbixData += myBitmapSet.data
|
|
|
|
|
|
|
|
return sbixHeader + sbixData
|
|
|
|
|
|
|
|
def toXML(self, xmlWriter, ttFont):
|
2014-09-22 16:54:43 +02:00
|
|
|
xmlWriter.simpletag("version", value=self.version)
|
2013-12-06 19:40:00 -05:00
|
|
|
xmlWriter.newline()
|
2014-09-22 16:54:43 +02:00
|
|
|
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)
|
|
|
|
|
2013-12-06 19:47:32 -05:00
|
|
|
def fromXML(self, name, attrs, content, ttFont):
|
2014-09-22 16:54:43 +02:00
|
|
|
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:
|
2013-12-06 19:47:32 -05:00
|
|
|
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
|
2013-12-06 19:47:32 -05:00
|
|
|
raise ttLib.TTLibError("can't handle '%s' element" % name)
|
2013-12-06 19:40:00 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Helper classes
|
|
|
|
|
|
|
|
class sbixBitmapSetOffset(object):
|
|
|
|
pass
|