added support for instruction disassembly

git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@75 4cde692c-a291-49d1-8350-778aa11640f8
This commit is contained in:
Just 2000-02-01 15:31:18 +00:00
parent 51e75db43e
commit 9eaa7e3e5f
3 changed files with 41 additions and 30 deletions

View File

@ -1,14 +1,26 @@
import DefaultTable import DefaultTable
import array import array
import ttProgram
class table__f_p_g_m(DefaultTable.DefaultTable): class table__f_p_g_m(DefaultTable.DefaultTable):
def decompile(self, data, ttFont): def decompile(self, data, ttFont):
self.fpgm = data program = ttProgram.Program()
program.fromBytecode(data)
self.program = program
def compile(self, ttFont): def compile(self, ttFont):
return self.fpgm return self.program.getBytecode()
def toXML(self, writer, ttFont):
self.program.toXML(writer, ttFont)
writer.newline()
def fromXML(self, (name, attrs, content), ttFont):
program = ttProgram.Program()
program.fromXML((name, attrs, content), ttFont)
self.program = program
def __len__(self): def __len__(self):
return len(self.fpgm) return len(self.program)

View File

@ -18,6 +18,7 @@ import struct, sstruct
import DefaultTable import DefaultTable
from fontTools import ttLib from fontTools import ttLib
from fontTools.misc.textTools import safeEval, readHex from fontTools.misc.textTools import safeEval, readHex
import ttProgram
import array import array
import Numeric import Numeric
import types import types
@ -244,10 +245,9 @@ class Glyph:
if self.isComposite(): if self.isComposite():
for compo in self.components: for compo in self.components:
compo.toXML(writer, ttFont) compo.toXML(writer, ttFont)
if hasattr(self, "instructions"): if hasattr(self, "program"):
writer.begintag("instructions") writer.begintag("instructions")
writer.newline() self.program.toXML(writer, ttFont)
writer.dumphex(self.instructions)
writer.endtag("instructions") writer.endtag("instructions")
writer.newline() writer.newline()
else: else:
@ -266,8 +266,7 @@ class Glyph:
writer.newline() writer.newline()
if self.numberOfContours: if self.numberOfContours:
writer.begintag("instructions") writer.begintag("instructions")
writer.newline() self.program.toXML(writer, ttFont)
writer.dumphex(self.instructions)
writer.endtag("instructions") writer.endtag("instructions")
writer.newline() writer.newline()
@ -306,7 +305,11 @@ class Glyph:
self.components.append(component) self.components.append(component)
component.fromXML((name, attrs, content), ttFont) component.fromXML((name, attrs, content), ttFont)
elif name == "instructions": elif name == "instructions":
self.instructions = readHex(content) self.program = ttProgram.Program()
for element in content:
if type(element) == types.StringType:
continue
self.program.fromXML(element, ttFont)
def getCompositeMaxpValues(self, glyfTable, maxComponentDepth=1): def getCompositeMaxpValues(self, glyfTable, maxComponentDepth=1):
assert self.isComposite() assert self.isComposite()
@ -341,7 +344,8 @@ class Glyph:
if haveInstructions: if haveInstructions:
numInstructions, = struct.unpack(">h", data[:2]) numInstructions, = struct.unpack(">h", data[:2])
data = data[2:] data = data[2:]
self.instructions = data[:numInstructions] self.program = ttProgram.Program()
self.program.fromBytecode(data[:numInstructions])
data = data[numInstructions:] data = data[numInstructions:]
assert len(data) in (0, 1), "bad composite data" assert len(data) in (0, 1), "bad composite data"
@ -356,7 +360,8 @@ class Glyph:
instructionLength, = struct.unpack(">h", data[:2]) instructionLength, = struct.unpack(">h", data[:2])
data = data[2:] data = data[2:]
self.instructions = data[:instructionLength] self.program = ttProgram.Program()
self.program.fromBytecode(data[:instructionLength])
data = data[instructionLength:] data = data[instructionLength:]
nCoordinates = self.endPtsOfContours[-1] + 1 nCoordinates = self.endPtsOfContours[-1] + 1
flags, xCoordinates, yCoordinates = \ flags, xCoordinates, yCoordinates = \
@ -450,12 +455,13 @@ class Glyph:
haveInstructions = 0 haveInstructions = 0
for i in range(len(self.components)): for i in range(len(self.components)):
if i == lastcomponent: if i == lastcomponent:
haveInstructions = hasattr(self, "instructions") haveInstructions = hasattr(self, "program")
more = 0 more = 0
compo = self.components[i] compo = self.components[i]
data = data + compo.compile(more, haveInstructions, glyfTable) data = data + compo.compile(more, haveInstructions, glyfTable)
if haveInstructions: if haveInstructions:
data = data + struct.pack(">h", len(self.instructions)) + self.instructions instructions = self.program.getBytecode()
data = data + struct.pack(">h", len(instructions)) + instructions
return data return data
@ -466,8 +472,8 @@ class Glyph:
if ttLib.endian <> "big": if ttLib.endian <> "big":
endPtsOfContours.byteswap() endPtsOfContours.byteswap()
data = data + endPtsOfContours.tostring() data = data + endPtsOfContours.tostring()
data = data + struct.pack(">h", len(self.instructions)) instructions = self.program.getBytecode()
data = data + self.instructions data = data + struct.pack(">h", len(instructions)) + instructions
nCoordinates = len(self.coordinates) nCoordinates = len(self.coordinates)
# make a copy # make a copy
@ -608,7 +614,7 @@ class Glyph:
return 1 return 1
return ( return (
cmp(self.endPtsOfContours, other.endPtsOfContours) or cmp(self.endPtsOfContours, other.endPtsOfContours) or
cmp(self.instructions, other.instructions) cmp(self.program, other.instructions)
) )

View File

@ -1,14 +1,7 @@
import DefaultTable from fontTools import ttLib
import array
class table__p_r_e_p(DefaultTable.DefaultTable): superclass = ttLib.getTableClass("fpgm")
def decompile(self, data, ttFont): class table__p_r_e_p(superclass):
self.prep = data pass
def compile(self, ttFont):
return self.prep
def __len__(self):
return len(self.prep)