1111 3) Did more than I needed and it was easier to write my own than
1212 figure out how to just get what I needed from theirs
1313
14- It is pretty easy to use, and requires only built-in python libs
14+ It is pretty easy to use, and requires only built-in python libs:
1515
1616 >>> from afm import AFM
1717 >>> fh = file('ptmr8a.afm')
3131
3232
3333AUTHOR:
34- John D. Hunter <[email protected] > 34+ John D. Hunter <[email protected] > 3535"""
3636
3737import sys , os
@@ -56,7 +56,7 @@ def _to_bool(s):
5656def _sanity_check (fh ):
5757 """
5858 Check if the file at least looks like AFM.
59- If not, raise RuntimeError.
59+ If not, raise :exc:` RuntimeError` .
6060 """
6161
6262 # Remember the file position in case the caller wants to
@@ -77,9 +77,12 @@ def _sanity_check(fh):
7777def _parse_header (fh ):
7878 """
7979 Reads the font metrics header (up to the char metrics) and returns
80- a dictionary mapping key to val. val will be converted to the
81- appropriate python type as necessary; eg 'False'->False, '0'->0,
82- '-168 -218 1000 898'-> [-168, -218, 1000, 898]
80+ a dictionary mapping *key* to *val*. *val* will be converted to the
81+ appropriate python type as necessary; eg:
82+
83+ * 'False'->False
84+ * '0'->0
85+ * '-168 -218 1000 898'-> [-168, -218, 1000, 898]
8386
8487 Dictionary keys are
8588
@@ -142,14 +145,12 @@ def _parse_header(fh):
142145def _parse_char_metrics (fh ):
143146 """
144147 Return a character metric dictionary. Keys are the ASCII num of
145- the character, values are a (wx, name, bbox) tuple, where
146-
147- wx is the character width
148- name is the postscript language name
149- bbox (llx, lly, urx, ury)
148+ the character, values are a (*wx*, *name*, *bbox*) tuple, where
149+ *wx* is the character width, *name* is the postscript language
150+ name, and *bbox* is a (*llx*, *lly*, *urx*, *ury*) tuple.
150151
151- This function is incomplete per the standard, but thus far parse
152- all the sample afm files I have
152+ This function is incomplete per the standard, but thus far parses
153+ all the sample afm files tried.
153154 """
154155
155156 ascii_d = {}
@@ -176,12 +177,11 @@ def _parse_char_metrics(fh):
176177
177178def _parse_kern_pairs (fh ):
178179 """
179- Return a kern pairs dictionary; keys are (char1, char2) tuples and
180+ Return a kern pairs dictionary; keys are (* char1*, * char2* ) tuples and
180181 values are the kern pair value. For example, a kern pairs line like
182+ ``KPX A y -50``
181183
182- KPX A y -50
183-
184- will be represented as
184+ will be represented as::
185185
186186 d[ ('A', 'y') ] = -50
187187
@@ -210,13 +210,13 @@ def _parse_kern_pairs(fh):
210210def _parse_composites (fh ):
211211 """
212212 Return a composites dictionary. Keys are the names of the
213- composites. vals are a num parts list of composite information,
214- with each element being a (name, dx, dy ) tuple. Thus if a
213+ composites. Values are a num parts list of composite information,
214+ with each element being a (* name*, *dx*, *dy* ) tuple. Thus a
215215 composites line reading:
216216
217217 CC Aacute 2 ; PCC A 0 0 ; PCC acute 160 170 ;
218218
219- will be represented as
219+ will be represented as::
220220
221221 d['Aacute'] = [ ('A', 0, 0), ('acute', 160, 170) ]
222222
@@ -245,9 +245,10 @@ def _parse_optional(fh):
245245 """
246246 Parse the optional fields for kern pair data and composites
247247
248- return value is a kernDict, compositeDict which are the return
249- values from parse_kern_pairs, and parse_composites if the data
250- exists, or empty dicts otherwise
248+ return value is a (*kernDict*, *compositeDict*) which are the
249+ return values from :func:`_parse_kern_pairs`, and
250+ :func:`_parse_composites` if the data exists, or empty dicts
251+ otherwise
251252 """
252253 optional = {
253254 'StartKernData' : _parse_kern_pairs ,
@@ -269,13 +270,12 @@ def _parse_optional(fh):
269270
270271def parse_afm (fh ):
271272 """
272- Parse the Adobe Font Metics file in file handle fh
273- Return value is a (dhead, dcmetrics, dkernpairs, dcomposite) tuple where
274-
275- dhead : a parse_header dict
276- dcmetrics : a parse_composites dict
277- dkernpairs : a parse_kern_pairs dict, possibly {}
278- dcomposite : a parse_composites dict , possibly {}
273+ Parse the Adobe Font Metics file in file handle *fh*. Return value
274+ is a (*dhead*, *dcmetrics*, *dkernpairs*, *dcomposite*) tuple where
275+ *dhead* is a :func:`_parse_header` dict, *dcmetrics* is a
276+ :func:`_parse_composites` dict, *dkernpairs* is a
277+ :func:`_parse_kern_pairs` dict (possibly {}), and *dcomposite* is a
278+ :func:`_parse_composites` dict (possibly {})
279279 """
280280 _sanity_check (fh )
281281 dhead = _parse_header (fh )
@@ -288,7 +288,7 @@ class AFM:
288288
289289 def __init__ (self , fh ):
290290 """
291- Parse the AFM file in file object fh
291+ Parse the AFM file in file object *fh*
292292 """
293293 (dhead , dcmetrics_ascii , dcmetrics_name , dkernpairs , dcomposite ) = \
294294 parse_afm (fh )
@@ -307,7 +307,7 @@ def get_bbox_char(self, c, isord=False):
307307 def string_width_height (self , s ):
308308 """
309309 Return the string width (including kerning) and string height
310- as a w,h tuple
310+ as a (*w*, *h*) tuple.
311311 """
312312 if not len (s ): return 0 ,0
313313 totalw = 0
@@ -404,39 +404,39 @@ def get_width_from_char_name(self, name):
404404
405405 def get_height_char (self , c , isord = False ):
406406 """
407- Get the height of character c from the bounding box. This is
408- the ink height (space is 0)
407+ Get the height of character *c* from the bounding box. This
408+ is the ink height (space is 0)
409409 """
410410 if not isord : c = ord (c )
411411 wx , name , bbox = self ._metrics [c ]
412412 return bbox [- 1 ]
413413
414414 def get_kern_dist (self , c1 , c2 ):
415415 """
416- Return the kerning pair distance (possibly 0) for chars c1 and
417- c2
416+ Return the kerning pair distance (possibly 0) for chars *c1*
417+ and *c2*
418418 """
419419 name1 , name2 = self .get_name_char (c1 ), self .get_name_char (c2 )
420420 return self .get_kern_dist_from_name (name1 , name2 )
421421
422422 def get_kern_dist_from_name (self , name1 , name2 ):
423423 """
424- Return the kerning pair distance (possibly 0) for chars c1 and
425- c2
424+ Return the kerning pair distance (possibly 0) for chars
425+ *name1* and *name2*
426426 """
427427 try : return self ._kern [ (name1 , name2 ) ]
428428 except : return 0
429429
430430 def get_fontname (self ):
431- "Return the font name, eg, Times-Roman"
431+ "Return the font name, eg, ' Times-Roman' "
432432 return self ._header ['FontName' ]
433433
434434 def get_fullname (self ):
435- "Return the font full name, eg, Times-Roman"
435+ "Return the font full name, eg, ' Times-Roman' "
436436 return self ._header ['FullName' ]
437437
438438 def get_familyname (self ):
439- "Return the font family name, eg, Times"
439+ "Return the font family name, eg, ' Times' "
440440 return self ._header ['FamilyName' ]
441441
442442 def get_weight (self ):
@@ -461,14 +461,14 @@ def get_underline_thickness(self):
461461
462462 def get_horizontal_stem_width (self ):
463463 """
464- Return the standard horizontal stem width as float, or None if
464+ Return the standard horizontal stem width as float, or * None* if
465465 not specified in AFM file.
466466 """
467467 return self ._header .get ('StdHW' , None )
468468
469469 def get_vertical_stem_width (self ):
470470 """
471- Return the standard vertical stem width as float, or None if
471+ Return the standard vertical stem width as float, or * None* if
472472 not specified in AFM file.
473473 """
474474 return self ._header .get ('StdVW' , None )
0 commit comments