3
from fontTools.misc.textTools import safeEval, num2binary, binary2num
4
from types import TupleType
7
# panose classification
24
def toXML(self, writer, ttFont):
25
formatstring, names, fixes = sstruct.getformat(panoseFormat)
27
writer.simpletag(name, value=getattr(self, name))
30
def fromXML(self, (name, attrs, content), ttFont):
31
setattr(self, name, safeEval(attrs["value"]))
34
# 'sfnt' OS/2 and Windows Metrics table - 'OS/2'
39
xAvgCharWidth: h # average character width
40
usWeightClass: H # degree of thickness of strokes
41
usWidthClass: H # aspect ratio
42
fsType: h # type flags
43
ySubscriptXSize: h # subscript horizontal font size
44
ySubscriptYSize: h # subscript vertical font size
45
ySubscriptXOffset: h # subscript x offset
46
ySubscriptYOffset: h # subscript y offset
47
ySuperscriptXSize: h # superscript horizontal font size
48
ySuperscriptYSize: h # superscript vertical font size
49
ySuperscriptXOffset: h # superscript x offset
50
ySuperscriptYOffset: h # superscript y offset
51
yStrikeoutSize: h # strikeout size
52
yStrikeoutPosition: h # strikeout position
53
sFamilyClass: h # font family class and subclass
54
panose: 10s # panose classification number
55
ulUnicodeRange1: l # character range
56
ulUnicodeRange2: l # character range
57
ulUnicodeRange3: l # character range
58
ulUnicodeRange4: l # character range
59
achVendID: 4s # font vendor identification
60
fsSelection: H # font selection flags
61
fsFirstCharIndex: H # first unicode character index
62
fsLastCharIndex: H # last unicode character index
63
sTypoAscender: h # typographic ascender
64
sTypoDescender: h # typographic descender
65
sTypoLineGap: h # typographic line gap
66
usWinAscent: H # Windows ascender
67
usWinDescent: H # Windows descender
70
OS2_format_1_addition = """
75
OS2_format_2_addition = OS2_format_1_addition + """
83
bigendian = " > # big endian\n"
85
OS2_format_1 = OS2_format_0 + OS2_format_1_addition
86
OS2_format_2 = OS2_format_0 + OS2_format_2_addition
87
OS2_format_1_addition = bigendian + OS2_format_1_addition
88
OS2_format_2_addition = bigendian + OS2_format_2_addition
91
class table_O_S_2f_2(DefaultTable.DefaultTable):
95
def decompile(self, data, ttFont):
96
dummy, data = sstruct.unpack2(OS2_format_0, data, self)
97
if self.version == 1 and not data:
98
# workaround for buggy Apple fonts
100
if self.version == 1:
101
sstruct.unpack2(OS2_format_1_addition, data, self)
102
elif self.version == 2:
103
sstruct.unpack2(OS2_format_2_addition, data, self)
104
elif self.version <> 0:
105
from fontTools import ttLib
106
raise ttLib.TTLibError, "unknown format for OS/2 table: version %s" % self.version
107
self.panose = sstruct.unpack(panoseFormat, self.panose, Panose())
109
def compile(self, ttFont):
111
self.panose = sstruct.pack(panoseFormat, self.panose)
112
if self.version == 0:
113
data = sstruct.pack(OS2_format_0, self)
114
elif self.version == 1:
115
data = sstruct.pack(OS2_format_1, self)
116
elif self.version == 2:
117
data = sstruct.pack(OS2_format_2, self)
119
from fontTools import ttLib
120
raise ttLib.TTLibError, "unknown format for OS/2 table: version %s" % self.version
124
def toXML(self, writer, ttFont):
125
if self.version == 1:
126
format = OS2_format_1
127
elif self.version == 2:
128
format = OS2_format_2
130
format = OS2_format_0
131
formatstring, names, fixes = sstruct.getformat(format)
133
value = getattr(self, name)
134
if type(value) == type(0L):
137
writer.begintag("panose")
139
value.toXML(writer, ttFont)
140
writer.endtag("panose")
141
elif name in ("ulUnicodeRange1", "ulUnicodeRange2",
142
"ulUnicodeRange3", "ulUnicodeRange4",
143
"ulCodePageRange1", "ulCodePageRange2"):
144
writer.simpletag(name, value=num2binary(value))
145
elif name in ("fsType", "fsSelection"):
146
writer.simpletag(name, value=num2binary(value, 16))
147
elif name == "achVendID":
148
writer.simpletag(name, value=repr(value)[1:-1])
150
writer.simpletag(name, value=value)
153
def fromXML(self, (name, attrs, content), ttFont):
155
self.panose = panose = Panose()
156
for element in content:
157
if type(element) == TupleType:
158
panose.fromXML(element, ttFont)
159
elif name in ("ulUnicodeRange1", "ulUnicodeRange2",
160
"ulUnicodeRange3", "ulUnicodeRange4",
161
"ulCodePageRange1", "ulCodePageRange2",
162
"fsType", "fsSelection"):
163
setattr(self, name, binary2num(attrs["value"]))
164
elif name == "achVendID":
165
setattr(self, name, safeEval("'''" + attrs["value"] + "'''"))
167
setattr(self, name, safeEval(attrs["value"]))