blob: 04277d173ff37df656077c7b72cfcf17941f6cd9 [file] [log] [blame]
Just7842e561999-12-16 21:34:53 +00001"""Module for reading and writing AFM files."""
2
3# XXX reads AFM's generated by Fog, not tested with much else.
4# It does not implement the full spec (Adobe Technote 5004, Adobe Font Metrics
5# File Format Specification). Still, it should read most "common" AFM files.
6
7import re
8import string
9import types
10
jvr5910f972003-05-24 12:50:47 +000011__version__ = "$Id: afmLib.py,v 1.6 2003-05-24 12:50:47 jvr Exp $"
Just7842e561999-12-16 21:34:53 +000012
13
14# every single line starts with a "word"
15identifierRE = re.compile("^([A-Za-z]+).*")
16
17# regular expression to parse char lines
18charRE = re.compile(
19 "(-?\d+)" # charnum
20 "\s*;\s*WX\s+" # ; WX
jvr31ad3512002-11-26 14:09:52 +000021 "(-?\d+)" # width
Just7842e561999-12-16 21:34:53 +000022 "\s*;\s*N\s+" # ; N
jvr3c3c32c2002-03-12 14:34:43 +000023 "([.A-Za-z0-9_]+)" # charname
Just7842e561999-12-16 21:34:53 +000024 "\s*;\s*B\s+" # ; B
25 "(-?\d+)" # left
26 "\s+" #
27 "(-?\d+)" # bottom
28 "\s+" #
29 "(-?\d+)" # right
30 "\s+" #
31 "(-?\d+)" # top
32 "\s*;\s*" # ;
33 )
34
35# regular expression to parse kerning lines
36kernRE = re.compile(
37 "([.A-Za-z0-9_]+)" # leftchar
38 "\s+" #
39 "([.A-Za-z0-9_]+)" # rightchar
40 "\s+" #
41 "(-?\d+)" # value
42 "\s*" #
43 )
44
Just32f86842001-04-30 14:40:17 +000045# regular expressions to parse composite info lines of the form:
46# Aacute 2 ; PCC A 0 0 ; PCC acute 182 211 ;
47compositeRE = re.compile(
48 "([.A-Za-z0-9_]+)" # char name
49 "\s+" #
50 "(\d+)" # number of parts
51 "\s*;\s*" #
52 )
53componentRE = re.compile(
54 "PCC\s+" # PPC
55 "([.A-Za-z0-9_]+)" # base char name
56 "\s+" #
57 "(-?\d+)" # x offset
58 "\s+" #
59 "(-?\d+)" # y offset
60 "\s*;\s*" #
61 )
62
63preferredAttributeOrder = [
64 "FontName",
65 "FullName",
66 "FamilyName",
67 "Weight",
68 "ItalicAngle",
69 "IsFixedPitch",
70 "FontBBox",
71 "UnderlinePosition",
72 "UnderlineThickness",
73 "Version",
74 "Notice",
75 "EncodingScheme",
76 "CapHeight",
77 "XHeight",
78 "Ascender",
79 "Descender",
80]
81
82
83class error(Exception): pass
84
Just7842e561999-12-16 21:34:53 +000085
86class AFM:
87
jvr5910f972003-05-24 12:50:47 +000088 _attrs = None
89
Just7842e561999-12-16 21:34:53 +000090 _keywords = ['StartFontMetrics',
91 'EndFontMetrics',
92 'StartCharMetrics',
93 'EndCharMetrics',
94 'StartKernData',
95 'StartKernPairs',
96 'EndKernPairs',
Just32f86842001-04-30 14:40:17 +000097 'EndKernData',
98 'StartComposites',
99 'EndComposites',
100 ]
Just7842e561999-12-16 21:34:53 +0000101
Just32f86842001-04-30 14:40:17 +0000102 def __init__(self, path=None):
Just7842e561999-12-16 21:34:53 +0000103 self._attrs = {}
104 self._chars = {}
105 self._kerning = {}
106 self._index = {}
107 self._comments = []
Just32f86842001-04-30 14:40:17 +0000108 self._composites = {}
Just7842e561999-12-16 21:34:53 +0000109 if path is not None:
110 self.read(path)
111
112 def read(self, path):
113 lines = readlines(path)
114 for line in lines:
115 if not string.strip(line):
116 continue
117 m = identifierRE.match(line)
118 if m is None:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500119 raise error("syntax error in AFM file: " + `line`)
Just7842e561999-12-16 21:34:53 +0000120
121 pos = m.regs[1][1]
122 word = line[:pos]
123 rest = string.strip(line[pos:])
124 if word in self._keywords:
125 continue
Just32f86842001-04-30 14:40:17 +0000126 if word == "C":
Just7842e561999-12-16 21:34:53 +0000127 self.parsechar(rest)
128 elif word == "KPX":
129 self.parsekernpair(rest)
Just32f86842001-04-30 14:40:17 +0000130 elif word == "CC":
131 self.parsecomposite(rest)
Just7842e561999-12-16 21:34:53 +0000132 else:
133 self.parseattr(word, rest)
134
135 def parsechar(self, rest):
136 m = charRE.match(rest)
137 if m is None:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500138 raise error("syntax error in AFM file: " + `rest`)
Just7842e561999-12-16 21:34:53 +0000139 things = []
140 for fr, to in m.regs[1:]:
141 things.append(rest[fr:to])
142 charname = things[2]
143 del things[2]
144 charnum, width, l, b, r, t = map(string.atoi, things)
145 self._chars[charname] = charnum, width, (l, b, r, t)
146
147 def parsekernpair(self, rest):
148 m = kernRE.match(rest)
149 if m is None:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500150 raise error("syntax error in AFM file: " + `rest`)
Just7842e561999-12-16 21:34:53 +0000151 things = []
152 for fr, to in m.regs[1:]:
153 things.append(rest[fr:to])
154 leftchar, rightchar, value = things
155 value = string.atoi(value)
156 self._kerning[(leftchar, rightchar)] = value
157
158 def parseattr(self, word, rest):
159 if word == "FontBBox":
160 l, b, r, t = map(string.atoi, string.split(rest))
161 self._attrs[word] = l, b, r, t
162 elif word == "Comment":
163 self._comments.append(rest)
164 else:
165 try:
166 value = string.atoi(rest)
167 except (ValueError, OverflowError):
168 self._attrs[word] = rest
169 else:
170 self._attrs[word] = value
171
Just32f86842001-04-30 14:40:17 +0000172 def parsecomposite(self, rest):
173 m = compositeRE.match(rest)
174 if m is None:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500175 raise error("syntax error in AFM file: " + `rest`)
Just32f86842001-04-30 14:40:17 +0000176 charname = m.group(1)
177 ncomponents = int(m.group(2))
178 rest = rest[m.regs[0][1]:]
179 components = []
180 while 1:
181 m = componentRE.match(rest)
182 if m is None:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500183 raise error("syntax error in AFM file: " + `rest`)
Just32f86842001-04-30 14:40:17 +0000184 basechar = m.group(1)
185 xoffset = int(m.group(2))
186 yoffset = int(m.group(3))
187 components.append((basechar, xoffset, yoffset))
188 rest = rest[m.regs[0][1]:]
189 if not rest:
190 break
191 assert len(components) == ncomponents
192 self._composites[charname] = components
193
Just6175deb2001-06-24 15:11:31 +0000194 def write(self, path, sep='\r'):
Just7842e561999-12-16 21:34:53 +0000195 import time
196 lines = [ "StartFontMetrics 2.0",
197 "Comment Generated by afmLib, version %s; at %s" %
198 (string.split(__version__)[2],
199 time.strftime("%m/%d/%Y %H:%M:%S",
200 time.localtime(time.time())))]
201
Just32f86842001-04-30 14:40:17 +0000202 # write comments, assuming (possibly wrongly!) they should
203 # all appear at the top
204 for comment in self._comments:
205 lines.append("Comment " + comment)
206
207 # write attributes, first the ones we know about, in
208 # a preferred order
209 attrs = self._attrs
210 for attr in preferredAttributeOrder:
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500211 if attr in attrs:
Just32f86842001-04-30 14:40:17 +0000212 value = attrs[attr]
213 if attr == "FontBBox":
214 value = "%s %s %s %s" % value
215 lines.append(attr + " " + str(value))
216 # then write the attributes we don't know about,
217 # in alphabetical order
218 items = attrs.items()
219 items.sort()
Just7842e561999-12-16 21:34:53 +0000220 for attr, value in items:
Just32f86842001-04-30 14:40:17 +0000221 if attr in preferredAttributeOrder:
222 continue
Just7842e561999-12-16 21:34:53 +0000223 lines.append(attr + " " + str(value))
224
225 # write char metrics
226 lines.append("StartCharMetrics " + `len(self._chars)`)
227 items = map(lambda (charname, (charnum, width, box)):
228 (charnum, (charname, width, box)),
229 self._chars.items())
230
231 def myCmp(a, b):
232 """Custom compare function to make sure unencoded chars (-1)
233 end up at the end of the list after sorting."""
234 if a[0] == -1:
235 a = (0xffff,) + a[1:] # 0xffff is an arbitrary large number
236 if b[0] == -1:
237 b = (0xffff,) + b[1:]
238 return cmp(a, b)
239 items.sort(myCmp)
240
241 for charnum, (charname, width, (l, b, r, t)) in items:
242 lines.append("C %d ; WX %d ; N %s ; B %d %d %d %d ;" %
243 (charnum, width, charname, l, b, r, t))
244 lines.append("EndCharMetrics")
245
246 # write kerning info
247 lines.append("StartKernData")
248 lines.append("StartKernPairs " + `len(self._kerning)`)
249 items = self._kerning.items()
250 items.sort() # XXX is order important?
251 for (leftchar, rightchar), value in items:
252 lines.append("KPX %s %s %d" % (leftchar, rightchar, value))
Just7842e561999-12-16 21:34:53 +0000253 lines.append("EndKernPairs")
254 lines.append("EndKernData")
Just32f86842001-04-30 14:40:17 +0000255
256 if self._composites:
257 composites = self._composites.items()
258 composites.sort()
259 lines.append("StartComposites %s" % len(self._composites))
260 for charname, components in composites:
261 line = "CC %s %s ;" % (charname, len(components))
262 for basechar, xoffset, yoffset in components:
263 line = line + " PCC %s %s %s ;" % (basechar, xoffset, yoffset)
264 lines.append(line)
265 lines.append("EndComposites")
266
Just7842e561999-12-16 21:34:53 +0000267 lines.append("EndFontMetrics")
268
269 writelines(path, lines, sep)
270
271 def has_kernpair(self, pair):
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500272 return pair in self._kerning
Just7842e561999-12-16 21:34:53 +0000273
274 def kernpairs(self):
275 return self._kerning.keys()
276
277 def has_char(self, char):
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500278 return char in self._chars
Just7842e561999-12-16 21:34:53 +0000279
280 def chars(self):
281 return self._chars.keys()
282
283 def comments(self):
284 return self._comments
285
Just6175deb2001-06-24 15:11:31 +0000286 def addComment(self, comment):
287 self._comments.append(comment)
288
289 def addComposite(self, glyphName, components):
290 self._composites[glyphName] = components
291
Just7842e561999-12-16 21:34:53 +0000292 def __getattr__(self, attr):
Behdad Esfahbodbc5e1cb2013-11-27 02:33:03 -0500293 if attr in self._attrs:
Just7842e561999-12-16 21:34:53 +0000294 return self._attrs[attr]
295 else:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500296 raise AttributeError(attr)
Just7842e561999-12-16 21:34:53 +0000297
298 def __setattr__(self, attr, value):
299 # all attrs *not* starting with "_" are consider to be AFM keywords
300 if attr[:1] == "_":
301 self.__dict__[attr] = value
302 else:
303 self._attrs[attr] = value
304
Just6175deb2001-06-24 15:11:31 +0000305 def __delattr__(self, attr):
306 # all attrs *not* starting with "_" are consider to be AFM keywords
307 if attr[:1] == "_":
308 try:
309 del self.__dict__[attr]
310 except KeyError:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500311 raise AttributeError(attr)
Just6175deb2001-06-24 15:11:31 +0000312 else:
313 try:
314 del self._attrs[attr]
315 except KeyError:
Behdad Esfahbodcd5aad92013-11-27 02:42:28 -0500316 raise AttributeError(attr)
Just6175deb2001-06-24 15:11:31 +0000317
Just7842e561999-12-16 21:34:53 +0000318 def __getitem__(self, key):
319 if type(key) == types.TupleType:
320 # key is a tuple, return the kernpair
Just6175deb2001-06-24 15:11:31 +0000321 return self._kerning[key]
Just7842e561999-12-16 21:34:53 +0000322 else:
323 # return the metrics instead
Just6175deb2001-06-24 15:11:31 +0000324 return self._chars[key]
325
326 def __setitem__(self, key, value):
327 if type(key) == types.TupleType:
328 # key is a tuple, set kernpair
329 self._kerning[key] = value
330 else:
331 # set char metrics
332 self._chars[key] = value
333
334 def __delitem__(self, key):
335 if type(key) == types.TupleType:
336 # key is a tuple, del kernpair
337 del self._kerning[key]
338 else:
339 # del char metrics
340 del self._chars[key]
Just7842e561999-12-16 21:34:53 +0000341
342 def __repr__(self):
343 if hasattr(self, "FullName"):
344 return '<AFM object for %s>' % self.FullName
345 else:
346 return '<AFM object at %x>' % id(self)
347
348
349def readlines(path):
350 f = open(path, 'rb')
351 data = f.read()
352 f.close()
353 # read any text file, regardless whether it's formatted for Mac, Unix or Dos
354 sep = ""
355 if '\r' in data:
356 sep = sep + '\r' # mac or dos
357 if '\n' in data:
358 sep = sep + '\n' # unix or dos
359 return string.split(data, sep)
360
Just32f86842001-04-30 14:40:17 +0000361def writelines(path, lines, sep='\r'):
Just7842e561999-12-16 21:34:53 +0000362 f = open(path, 'wb')
363 for line in lines:
364 f.write(line + sep)
365 f.close()
366
367
368
369if __name__ == "__main__":
jvr91bca422012-10-18 12:49:22 +0000370 import EasyDialogs
371 path = EasyDialogs.AskFileForOpen()
372 if path:
Just7842e561999-12-16 21:34:53 +0000373 afm = AFM(path)
374 char = 'A'
375 if afm.has_char(char):
376 print afm[char] # print charnum, width and boundingbox
377 pair = ('A', 'V')
378 if afm.has_kernpair(pair):
379 print afm[pair] # print kerning value for pair
380 print afm.Version # various other afm entries have become attributes
381 print afm.Weight
382 # afm.comments() returns a list of all Comment lines found in the AFM
383 print afm.comments()
384 #print afm.chars()
385 #print afm.kernpairs()
386 print afm
Just32f86842001-04-30 14:40:17 +0000387 afm.write(path + ".muck")
Just7842e561999-12-16 21:34:53 +0000388