Revision: 3727
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3727&view=rev
Author: jouni
Date: 2007-08-21 12:42:28 -0700 (Tue, 21 Aug 2007)
Log Message:
-----------
Run a simple sanity check to avoid attempting to parse non-AFM files as AFM.
Modified Paths:
--------------
trunk/matplotlib/lib/matplotlib/afm.py
Modified: trunk/matplotlib/lib/matplotlib/afm.py
===================================================================
--- trunk/matplotlib/lib/matplotlib/afm.py 2007-08-21 19:10:02 UTC (rev 3726)
+++ trunk/matplotlib/lib/matplotlib/afm.py 2007-08-21 19:42:28 UTC (rev 3727)
@@ -53,7 +53,27 @@
else: return True
+def _sanity_check(fh):
+ """
+ Check if the file at least looks like AFM.
+ If not, raise RuntimeError.
+ """
+ # Remember the file position in case the caller wants to
+ # do something else with the file.
+ pos = fh.tell()
+ try:
+ line = fh.readline()
+ finally:
+ fh.seek(pos, 0)
+
+ # AFM spec, Section 4: The StartFontMetrics keyword [followed by a
+ # version number] must be the first line in the file, and the
+ # EndFontMetrics keyword must be the last non-empty line in the
+ # file. We just check the first line.
+ if not line.startswith('StartFontMetrics'):
+ raise RuntimeError('Not an AFM file')
+
def _parse_header(fh):
"""
Reads the font metrics header (up to the char metrics) and returns
@@ -255,6 +275,7 @@
dkernpairs : a parse_kern_pairs dict, possibly {}
dcomposite : a parse_composites dict , possibly {}
"""
+ _sanity_check(fh)
dhead = _parse_header(fh)
dcmetrics = _parse_char_metrics(fh)
doptional = _parse_optional(fh)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|