149 lines
5.2 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
sbixStrikeHeaderFormat = """
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
"""
sbixGlyphDataOffsetFormat = """
>
glyphDataOffset: L # Offset from the beginning of the strike data record
# to data for the individual glyph
2013-12-06 19:35:09 -05:00
"""
sbixStrikeHeaderFormatSize = sstruct.calcsize(sbixStrikeHeaderFormat)
sbixGlyphDataOffsetFormatSize = sstruct.calcsize(sbixGlyphDataOffsetFormat)
2013-12-06 19:35:09 -05:00
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
2014-09-23 18:29:42 +02:00
self.glyphs = {}
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
if len(self.data) < sbixStrikeHeaderFormatSize:
2013-12-06 19:35:09 -05:00
from fontTools import ttLib
2014-09-22 17:07:01 +02:00
raise(ttLib.TTLibError, "Strike header too short: Expected %x, got %x.") \
% (sbixStrikeHeaderFormatSize, 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
sstruct.unpack(sbixStrikeHeaderFormat, self.data[:sbixStrikeHeaderFormatSize], self)
2013-12-06 19:40:00 -05:00
2014-09-23 18:29:42 +02:00
# calculate number of glyphs
firstGlyphDataOffset, = struct.unpack(">L", \
self.data[sbixStrikeHeaderFormatSize : sbixStrikeHeaderFormatSize + sbixGlyphDataOffsetFormatSize])
2014-09-23 18:29:42 +02:00
self.numGlyphs = (firstGlyphDataOffset - sbixStrikeHeaderFormatSize) // sbixGlyphDataOffsetFormatSize - 1
# ^ -1 because there's one more offset than glyphs
2013-12-06 19:40:00 -05:00
2014-09-23 18:29:42 +02:00
# build offset list for single glyph data offsets
self.glyphDataOffsets = []
2014-09-23 18:29:42 +02:00
for i in range(self.numGlyphs + 1): # + 1 because there's one more offset than glyphs
start = i * sbixGlyphDataOffsetFormatSize + sbixStrikeHeaderFormatSize
myOffset, = struct.unpack(">L", self.data[start : start + sbixGlyphDataOffsetFormatSize])
self.glyphDataOffsets.append(myOffset)
2013-12-06 19:40:00 -05:00
2014-09-23 18:29:42 +02:00
# iterate through offset list and slice raw data into glyph data records
for i in range(self.numGlyphs):
myBitmap = Bitmap(rawdata=self.data[self.glyphDataOffsets[i] : self.glyphDataOffsets[i+1]], gid=i)
2013-12-06 19:35:09 -05:00
myBitmap.decompile(ttFont)
2014-09-23 18:29:42 +02:00
self.glyphs[myBitmap.glyphName] = myBitmap
del self.glyphDataOffsets
2014-09-23 18:29:42 +02:00
del self.numGlyphs
2013-12-06 19:35:09 -05:00
del self.data
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
def compile(self, ttFont):
self.glyphDataOffsets = ""
2013-12-06 19:35:09 -05:00
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
2014-09-23 18:29:42 +02:00
# first glyph starts right after the header
currentGlyphDataOffset = sbixStrikeHeaderFormatSize + sbixGlyphDataOffsetFormatSize * (len(glyphOrder) + 1)
2013-12-06 19:35:09 -05:00
for glyphName in glyphOrder:
2014-09-23 18:29:42 +02:00
if glyphName in self.glyphs:
# we have glyph data for this glyph
myBitmap = self.glyphs[glyphName]
2013-12-06 19:35:09 -05:00
else:
2014-09-23 18:29:42 +02:00
# must add empty glyph data record for this glyph
2013-12-06 19:35:09 -05:00
myBitmap = Bitmap(glyphName=glyphName)
myBitmap.compile(ttFont)
myBitmap.glyphDataOffset = currentGlyphDataOffset
2013-12-06 19:35:09 -05:00
self.bitmapData += myBitmap.rawdata
currentGlyphDataOffset += len(myBitmap.rawdata)
self.glyphDataOffsets += sstruct.pack(sbixGlyphDataOffsetFormat, myBitmap)
2013-12-06 19:40:00 -05:00
2014-09-23 18:29:42 +02:00
# add last "offset", really the end address of the last glyph data record
2013-12-06 19:35:09 -05:00
dummy = Bitmap()
dummy.glyphDataOffset = currentGlyphDataOffset
self.glyphDataOffsets += sstruct.pack(sbixGlyphDataOffsetFormat, dummy)
2013-12-06 19:40:00 -05:00
2013-12-06 19:35:09 -05:00
# pack header
self.data = sstruct.pack(sbixStrikeHeaderFormat, self)
2014-09-22 16:02:36 +02:00
# add offsets and image data after header
self.data += self.glyphDataOffsets + 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)):
2014-09-23 18:29:42 +02:00
if glyphOrder[i] in self.glyphs:
self.glyphs[glyphOrder[i]].toXML(xmlWriter, ttFont)
# TODO: what if there are more glyph data records than (glyf table) 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"]))
2014-09-23 18:29:42 +02:00
elif name == "glyph":
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.")
if "originOffsetX" in attrs:
myOffsetX = int(attrs["originOffsetX"])
else:
myOffsetX = 0
if "originOffsetY" in attrs:
myOffsetY = int(attrs["originOffsetY"])
else:
myOffsetY = 0
myBitmap = Bitmap(
glyphName=myGlyphName,
imageFormatTag=myFormat,
originOffsetX=myOffsetX,
originOffsetY=myOffsetY,
)
2013-12-06 19:35:09 -05:00
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)
2014-09-23 18:29:42 +02:00
self.glyphs[myBitmap.glyphName] = myBitmap
2013-12-06 19:35:09 -05:00
else:
from fontTools import ttLib
raise ttLib.TTLibError("can't handle '%s' element" % name)