151 lines
5.3 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 = """
>
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", \
2015-04-26 00:15:26 -04:00
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
2015-04-26 00:15:26 -04:00
current_offset, = struct.unpack(">L", self.data[start:start + sbixGlyphDataOffsetFormatSize])
2014-09-24 12:07:35 +02:00
self.glyphDataOffsets.append(current_offset)
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):
2015-04-26 00:15:26 -04:00
current_glyph = Glyph(rawdata=self.data[self.glyphDataOffsets[i]:self.glyphDataOffsets[i+1]], gid=i)
2014-09-24 12:07:35 +02:00
current_glyph.decompile(ttFont)
self.glyphs[current_glyph.glyphName] = current_glyph
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 = b""
self.bitmapData = b""
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
2014-09-24 12:07:35 +02:00
current_glyph = 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
2014-09-24 12:07:35 +02:00
current_glyph = Glyph(glyphName=glyphName)
current_glyph.compile(ttFont)
current_glyph.glyphDataOffset = currentGlyphDataOffset
self.bitmapData += current_glyph.rawdata
currentGlyphDataOffset += len(current_glyph.rawdata)
self.glyphDataOffsets += sstruct.pack(sbixGlyphDataOffsetFormat, current_glyph)
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
2014-09-24 12:07:35 +02:00
dummy = Glyph()
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"]:
2014-09-24 12:43:43 +02:00
setattr(self, name, safeEval(attrs["value"]))
2014-09-23 18:29:42 +02:00
elif name == "glyph":
2014-09-24 11:54:32 +02:00
if "graphicType" in attrs:
2014-09-24 12:43:43 +02:00
myFormat = safeEval("'''" + attrs["graphicType"] + "'''")
2013-12-06 19:35:09 -05:00
else:
myFormat = None
if "glyphname" in attrs:
2014-09-24 12:43:43 +02:00
myGlyphName = safeEval("'''" + attrs["glyphname"] + "'''")
2014-09-23 18:47:13 +02:00
elif "name" in attrs:
2014-09-24 12:43:43 +02:00
myGlyphName = safeEval("'''" + attrs["name"] + "'''")
2013-12-06 19:35:09 -05:00
else:
from fontTools import ttLib
2014-09-23 18:47:13 +02:00
raise ttLib.TTLibError("Glyph must have a glyph name.")
if "originOffsetX" in attrs:
2014-09-24 12:43:43 +02:00
myOffsetX = safeEval(attrs["originOffsetX"])
else:
myOffsetX = 0
if "originOffsetY" in attrs:
2014-09-24 12:43:43 +02:00
myOffsetY = safeEval(attrs["originOffsetY"])
else:
myOffsetY = 0
2014-09-24 12:07:35 +02:00
current_glyph = Glyph(
glyphName=myGlyphName,
2014-09-24 11:54:32 +02:00
graphicType=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
2014-09-24 12:07:35 +02:00
current_glyph.fromXML(name, attrs, content, ttFont)
current_glyph.compile(ttFont)
self.glyphs[current_glyph.glyphName] = current_glyph
2013-12-06 19:35:09 -05:00
else:
from fontTools import ttLib
raise ttLib.TTLibError("can't handle '%s' element" % name)