moved some stuff to cffLib

git-svn-id: svn://svn.code.sf.net/p/fonttools/code/trunk@225 4cde692c-a291-49d1-8350-778aa11640f8
This commit is contained in:
jvr 2002-05-13 16:19:51 +00:00
parent 0b63b28615
commit dbc2c173b3
2 changed files with 118 additions and 118 deletions

View File

@ -1,7 +1,7 @@
"""cffLib.py -- read/write tools for Adobe CFF fonts.""" """cffLib.py -- read/write tools for Adobe CFF fonts."""
# #
# $Id: cffLib.py,v 1.7 2002-05-13 11:24:43 jvr Exp $ # $Id: cffLib.py,v 1.8 2002-05-13 16:19:37 jvr Exp $
# #
import struct, sstruct import struct, sstruct
@ -10,6 +10,115 @@ import types
from fontTools.misc import psCharStrings from fontTools.misc import psCharStrings
topDictOperators = [
# opcode name argument type
(0, 'version', 'SID'),
(1, 'Notice', 'SID'),
(2, 'FullName', 'SID'),
(3, 'FamilyName', 'SID'),
(4, 'Weight', 'SID'),
(5, 'FontBBox', 'array'),
(13, 'UniqueID', 'number'),
(14, 'XUID', 'array'),
(15, 'charset', 'number'),
(16, 'Encoding', 'number'),
(17, 'CharStrings', 'number'),
(18, 'Private', ('number', 'number')),
((12, 0), 'Copyright', 'SID'),
((12, 1), 'isFixedPitch', 'number'),
((12, 2), 'ItalicAngle', 'number'),
((12, 3), 'UnderlinePosition', 'number'),
((12, 4), 'UnderlineThickness', 'number'),
((12, 5), 'PaintType', 'number'),
((12, 6), 'CharstringType', 'number'),
((12, 7), 'FontMatrix', 'array'),
((12, 8), 'StrokeWidth', 'number'),
((12, 20), 'SyntheticBase', 'number'),
((12, 21), 'PostScript', 'SID'),
((12, 22), 'BaseFontName', 'SID'),
# CID additions
((12, 30), 'ROS', ('SID', 'SID', 'number')),
((12, 31), 'CIDFontVersion', 'number'),
((12, 32), 'CIDFontRevision', 'number'),
((12, 33), 'CIDFontType', 'number'),
((12, 34), 'CIDCount', 'number'),
((12, 35), 'UIDBase', 'number'),
((12, 36), 'FDArray', 'number'),
((12, 37), 'FDSelect', 'number'),
((12, 38), 'FontName', 'SID'),
# MM, Chameleon. Pft.
]
topDictDefaults = {
'isFixedPitch': 0,
'ItalicAngle': 0,
'UnderlineThickness': 50,
'PaintType': 0,
'CharstringType': 2,
'FontMatrix': [0.001, 0, 0, 0.001, 0, 0],
'FontBBox': [0, 0, 0, 0],
'StrokeWidth': 0,
'charset': 0,
'Encoding': 0,
# CID defaults
'CIDFontVersion': 0,
'CIDFontRevision': 0,
'CIDFontType': 0,
'CIDCount': 8720,
}
class TopDictDecompiler(psCharStrings.DictDecompiler):
operators = psCharStrings.buildOperatorDict(topDictOperators)
dictDefaults = topDictDefaults
privateDictOperators = [
# opcode name argument type
(6, 'BlueValues', 'array'),
(7, 'OtherBlues', 'array'),
(8, 'FamilyBlues', 'array'),
(9, 'FamilyOtherBlues', 'array'),
(10, 'StdHW', 'number'),
(11, 'StdVW', 'number'),
(19, 'Subrs', 'number'),
(20, 'defaultWidthX', 'number'),
(21, 'nominalWidthX', 'number'),
((12, 9), 'BlueScale', 'number'),
((12, 10), 'BlueShift', 'number'),
((12, 11), 'BlueFuzz', 'number'),
((12, 12), 'StemSnapH', 'array'),
((12, 13), 'StemSnapV', 'array'),
((12, 14), 'ForceBold', 'number'),
((12, 15), 'ForceBoldThreshold', 'number'),
((12, 16), 'lenIV', 'number'),
((12, 17), 'LanguageGroup', 'number'),
((12, 18), 'ExpansionFactor', 'number'),
((12, 19), 'initialRandomSeed', 'number'),
]
privateDictDefaults = {
'defaultWidthX': 0,
'nominalWidthX': 0,
'BlueScale': 0.039625,
'BlueShift': 7,
'BlueFuzz': 1,
'ForceBold': 0,
'ForceBoldThreshold': 0,
'lenIV': -1,
'LanguageGroup': 0,
'ExpansionFactor': 0.06,
'initialRandomSeed': 0,
}
class PrivateDictDecompiler(psCharStrings.DictDecompiler):
operators = psCharStrings.buildOperatorDict(privateDictOperators)
dictDefaults = privateDictDefaults
cffHeaderFormat = """ cffHeaderFormat = """
major: B major: B
minor: B minor: B
@ -31,14 +140,12 @@ class CFFFontSet:
topDicts = readINDEX(file) topDicts = readINDEX(file)
strings = IndexedStrings(readINDEX(file)) strings = IndexedStrings(readINDEX(file))
globalSubrs = readINDEX(file) globalSubrs = readINDEX(file)
self.GlobalSubrs = map(psCharStrings.T2CharString, globalSubrs) self.GlobalSubrs = map(psCharStrings.T2CharString, globalSubrs)
file.seek(0, 0)
for i in range(len(topDicts)): for i in range(len(topDicts)):
font = self.fonts[self.fontNames[i]] = CFFFont() font = self.fonts[self.fontNames[i]] = CFFFont()
font.GlobalSubrs = self.GlobalSubrs # Hmm. font.GlobalSubrs = self.GlobalSubrs # Hmm.
file.seek(0, 0)
font.decompile(file, topDicts[i], strings, self) # maybe only 'on demand'? font.decompile(file, topDicts[i], strings, self) # maybe only 'on demand'?
def compile(self): def compile(self):
@ -110,7 +217,7 @@ class IndexedStrings:
class CFFFont: class CFFFont:
defaults = psCharStrings.topDictDefaults defaults = topDictDefaults
def __init__(self): def __init__(self):
pass pass
@ -135,7 +242,7 @@ class CFFFont:
raise NotImplementedError raise NotImplementedError
def decompile(self, file, topDictData, strings, fontSet): def decompile(self, file, topDictData, strings, fontSet):
top = psCharStrings.TopDictDecompiler(strings) top = TopDictDecompiler(strings)
top.decompile(topDictData) top.decompile(topDictData)
self.fromDict(top.getDict()) self.fromDict(top.getDict())
@ -290,13 +397,13 @@ class CFFFont:
class PrivateDict: class PrivateDict:
defaults = psCharStrings.privateDictDefaults defaults = privateDictDefaults
def __init__(self): def __init__(self):
pass pass
def decompile(self, data, privateData, strings): def decompile(self, data, privateData, strings):
p = psCharStrings.PrivateDictDecompiler(strings) p = PrivateDictDecompiler(strings)
p.decompile(privateData) p.decompile(privateData)
self.fromDict(p.getDict()) self.fromDict(p.getDict())

View File

@ -71,7 +71,7 @@ class ByteCodeDecompilerBase:
return string.atof(number), index return string.atof(number), index
def _buildOperatorDict(operatorList): def buildOperatorDict(operatorList):
dict = {} dict = {}
for item in operatorList: for item in operatorList:
if len(item) == 2: if len(item) == 2:
@ -139,7 +139,7 @@ t2Operators = [
class T2CharString(ByteCodeDecompilerBase): class T2CharString(ByteCodeDecompilerBase):
operandEncoding = t2OperandEncoding operandEncoding = t2OperandEncoding
operators = _buildOperatorDict(t2Operators) operators = buildOperatorDict(t2Operators)
def __init__(self, bytecode=None, program=None): def __init__(self, bytecode=None, program=None):
if program is None: if program is None:
@ -259,7 +259,7 @@ t1Operators = [
class T1CharString(T2CharString): class T1CharString(T2CharString):
operandEncoding = t1OperandEncoding operandEncoding = t1OperandEncoding
operators = _buildOperatorDict(t1Operators) operators = buildOperatorDict(t1Operators)
def decompile(self): def decompile(self):
if self.program is not None: if self.program is not None:
@ -860,113 +860,6 @@ class DictDecompiler(ByteCodeDecompilerBase):
return self.popall() return self.popall()
topDictOperators = [
# opcode name argument type
(0, 'version', 'SID'),
(1, 'Notice', 'SID'),
(2, 'FullName', 'SID'),
(3, 'FamilyName', 'SID'),
(4, 'Weight', 'SID'),
(5, 'FontBBox', 'array'),
(13, 'UniqueID', 'number'),
(14, 'XUID', 'array'),
(15, 'charset', 'number'),
(16, 'Encoding', 'number'),
(17, 'CharStrings', 'number'),
(18, 'Private', ('number', 'number')),
((12, 0), 'Copyright', 'SID'),
((12, 1), 'isFixedPitch', 'number'),
((12, 2), 'ItalicAngle', 'number'),
((12, 3), 'UnderlinePosition', 'number'),
((12, 4), 'UnderlineThickness', 'number'),
((12, 5), 'PaintType', 'number'),
((12, 6), 'CharstringType', 'number'),
((12, 7), 'FontMatrix', 'array'),
((12, 8), 'StrokeWidth', 'number'),
((12, 20), 'SyntheticBase', 'number'),
((12, 21), 'PostScript', 'SID'),
((12, 22), 'BaseFontName', 'SID'),
# CID additions
((12, 30), 'ROS', ('SID', 'SID', 'number')),
((12, 31), 'CIDFontVersion', 'number'),
((12, 32), 'CIDFontRevision', 'number'),
((12, 33), 'CIDFontType', 'number'),
((12, 34), 'CIDCount', 'number'),
((12, 35), 'UIDBase', 'number'),
((12, 36), 'FDArray', 'number'),
((12, 37), 'FDSelect', 'number'),
((12, 38), 'FontName', 'SID'),
# MM, Chameleon. Pft.
]
topDictDefaults = {
'isFixedPitch': 0,
'ItalicAngle': 0,
'UnderlineThickness': 50,
'PaintType': 0,
'CharstringType': 2,
'FontMatrix': [0.001, 0, 0, 0.001, 0, 0],
'FontBBox': [0, 0, 0, 0],
'StrokeWidth': 0,
'charset': 0,
'Encoding': 0,
# CID defaults
'CIDFontVersion': 0,
'CIDFontRevision': 0,
'CIDFontType': 0,
'CIDCount': 8720,
}
class TopDictDecompiler(DictDecompiler):
operators = _buildOperatorDict(topDictOperators)
dictDefaults = topDictDefaults
privateDictOperators = [
# opcode name argument type
(6, 'BlueValues', 'array'),
(7, 'OtherBlues', 'array'),
(8, 'FamilyBlues', 'array'),
(9, 'FamilyOtherBlues', 'array'),
(10, 'StdHW', 'number'),
(11, 'StdVW', 'number'),
(19, 'Subrs', 'number'),
(20, 'defaultWidthX', 'number'),
(21, 'nominalWidthX', 'number'),
((12, 9), 'BlueScale', 'number'),
((12, 10), 'BlueShift', 'number'),
((12, 11), 'BlueFuzz', 'number'),
((12, 12), 'StemSnapH', 'array'),
((12, 13), 'StemSnapV', 'array'),
((12, 14), 'ForceBold', 'number'),
((12, 15), 'ForceBoldThreshold', 'number'),
((12, 16), 'lenIV', 'number'),
((12, 17), 'LanguageGroup', 'number'),
((12, 18), 'ExpansionFactor', 'number'),
((12, 19), 'initialRandomSeed', 'number'),
]
privateDictDefaults = {
'defaultWidthX': 0,
'nominalWidthX': 0,
'BlueScale': 0.039625,
'BlueShift': 7,
'BlueFuzz': 1,
'ForceBold': 0,
'ForceBoldThreshold': 0,
'lenIV': -1,
'LanguageGroup': 0,
'ExpansionFactor': 0.06,
'initialRandomSeed': 0,
}
class PrivateDictDecompiler(DictDecompiler):
operators = _buildOperatorDict(privateDictOperators)
dictDefaults = privateDictDefaults
def calcSubrBias(subrs): def calcSubrBias(subrs):
nSubrs = len(subrs) nSubrs = len(subrs)
if nSubrs < 1240: if nSubrs < 1240: