277 lines
23 KiB
HTML
277 lines
23 KiB
HTML
|
|
||
|
<!doctype html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
|
||
|
<html><head><title>Python: module robofab.glifLib</title>
|
||
|
</head><body bgcolor="#f0f0f8">
|
||
|
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="heading">
|
||
|
<tr bgcolor="#7799ee">
|
||
|
<td valign=bottom> <br>
|
||
|
<font color="#ffffff" face="helvetica, arial"> <br><big><big><strong><a href="robofab.html"><font color="#ffffff">robofab</font></a>.glifLib</strong></big></big></font></td
|
||
|
><td align=right valign=bottom
|
||
|
><font color="#ffffff" face="helvetica, arial"><a href=".">index</a><br><a href="file:/code/projects/robofab/Lib/robofab/glifLib.py">/code/projects/robofab/Lib/robofab/glifLib.py</a></font></td></tr></table>
|
||
|
<p><tt>glifLib.py -- Generic module for reading and writing the .glif format.<br>
|
||
|
<br>
|
||
|
More info about the .glif format (GLyphInterchangeFormat) can be found here:<br>
|
||
|
<br>
|
||
|
<a href="http://just.letterror.com/ltrwiki/GlyphInterchangeFormat">http://just.letterror.com/ltrwiki/GlyphInterchangeFormat</a><br>
|
||
|
<br>
|
||
|
The main class in this module is <a href="#GlyphSet">GlyphSet</a>. It manages a set of .glif files<br>
|
||
|
in a folder. It offers two ways to read glyph data, and one way to write<br>
|
||
|
glyph data. See the class doc string for details.</tt></p>
|
||
|
<p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#aa55cc">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#fffff" face="helvetica, arial"><big><strong>Modules</strong></big></font></td></tr>
|
||
|
|
||
|
<tr><td bgcolor="#aa55cc"><tt> </tt></td><td> </td>
|
||
|
<td width="100%"><table width="100%" summary="list"><tr><td width="25%" valign=top><a href="os.html">os</a><br>
|
||
|
</td><td width="25%" valign=top></td><td width="25%" valign=top></td><td width="25%" valign=top></td></tr></table></td></tr></table><p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#ee77aa">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#ffffff" face="helvetica, arial"><big><strong>Classes</strong></big></font></td></tr>
|
||
|
|
||
|
<tr><td bgcolor="#ee77aa"><tt> </tt></td><td> </td>
|
||
|
<td width="100%"><dl>
|
||
|
<dt><font face="helvetica, arial"><a href="robofab.pens.pointPen.html#AbstractPointPen">robofab.pens.pointPen.AbstractPointPen</a>
|
||
|
</font></dt><dd>
|
||
|
<dl>
|
||
|
<dt><font face="helvetica, arial"><a href="robofab.glifLib.html#GLIFPointPen">GLIFPointPen</a>
|
||
|
</font></dt></dl>
|
||
|
</dd>
|
||
|
<dt><font face="helvetica, arial"><a href="exceptions.html#Exception">exceptions.Exception</a>
|
||
|
</font></dt><dd>
|
||
|
<dl>
|
||
|
<dt><font face="helvetica, arial"><a href="robofab.glifLib.html#GlifLibError">GlifLibError</a>
|
||
|
</font></dt></dl>
|
||
|
</dd>
|
||
|
<dt><font face="helvetica, arial"><a href="robofab.glifLib.html#Glyph">Glyph</a>
|
||
|
</font></dt><dt><font face="helvetica, arial"><a href="robofab.glifLib.html#GlyphSet">GlyphSet</a>
|
||
|
</font></dt></dl>
|
||
|
<p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#ffc8d8">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#000000" face="helvetica, arial"><a name="GLIFPointPen">class <strong>GLIFPointPen</strong></a>(<a href="robofab.pens.pointPen.html#AbstractPointPen">robofab.pens.pointPen.AbstractPointPen</a>)</font></td></tr>
|
||
|
|
||
|
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
|
||
|
<td colspan=2><tt>Helper class using the PointPen protocol to write the <outline><br>
|
||
|
part of .glif files.<br> </tt></td></tr>
|
||
|
<tr><td> </td>
|
||
|
<td width="100%">Methods defined here:<br>
|
||
|
<dl><dt><a name="GLIFPointPen-__init__"><strong>__init__</strong></a>(self, xmlWriter)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GLIFPointPen-addComponent"><strong>addComponent</strong></a>(self, glyphName, transformation)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GLIFPointPen-addPoint"><strong>addPoint</strong></a>(self, pt, segmentType<font color="#909090">=None</font>, smooth<font color="#909090">=None</font>, name<font color="#909090">=None</font>, **kwargs)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GLIFPointPen-beginPath"><strong>beginPath</strong></a>(self)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GLIFPointPen-endPath"><strong>endPath</strong></a>(self)</dt></dl>
|
||
|
|
||
|
</td></tr></table> <p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#ffc8d8">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#000000" face="helvetica, arial"><a name="GlifLibError">class <strong>GlifLibError</strong></a>(<a href="exceptions.html#Exception">exceptions.Exception</a>)</font></td></tr>
|
||
|
|
||
|
<tr><td bgcolor="#ffc8d8"><tt> </tt></td><td> </td>
|
||
|
<td width="100%">Methods inherited from <a href="exceptions.html#Exception">exceptions.Exception</a>:<br>
|
||
|
<dl><dt><a name="GlifLibError-__getitem__"><strong>__getitem__</strong></a>(...)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlifLibError-__init__"><strong>__init__</strong></a>(...)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlifLibError-__str__"><strong>__str__</strong></a>(...)</dt></dl>
|
||
|
|
||
|
</td></tr></table> <p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#ffc8d8">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#000000" face="helvetica, arial"><a name="Glyph">class <strong>Glyph</strong></a></font></td></tr>
|
||
|
|
||
|
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
|
||
|
<td colspan=2><tt>Minimal glyph object. It has no glyph attributes until either<br>
|
||
|
the <a href="#Glyph-draw">draw</a>() or the drawPoint() method has been called.<br> </tt></td></tr>
|
||
|
<tr><td> </td>
|
||
|
<td width="100%">Methods defined here:<br>
|
||
|
<dl><dt><a name="Glyph-__init__"><strong>__init__</strong></a>(self, glyphName, glyphSet)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="Glyph-draw"><strong>draw</strong></a>(self, pen)</dt><dd><tt>Draw this glyph onto a *FontTools* Pen.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="Glyph-drawPoints"><strong>drawPoints</strong></a>(self, pointPen)</dt><dd><tt>Draw this glyph onto a PointPen.</tt></dd></dl>
|
||
|
|
||
|
</td></tr></table> <p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#ffc8d8">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#000000" face="helvetica, arial"><a name="GlyphSet">class <strong>GlyphSet</strong></a></font></td></tr>
|
||
|
|
||
|
<tr bgcolor="#ffc8d8"><td rowspan=2><tt> </tt></td>
|
||
|
<td colspan=2><tt><a href="#GlyphSet">GlyphSet</a> manages a set of .glif files inside one directory.<br>
|
||
|
<br>
|
||
|
<a href="#GlyphSet">GlyphSet</a>'s constructor takes a path to an existing directory as it's<br>
|
||
|
first argument. Reading glyph data can either be done through the<br>
|
||
|
<a href="#GlyphSet-readGlyph">readGlyph</a>() method, or by using <a href="#GlyphSet">GlyphSet</a>'s dictionary interface, where<br>
|
||
|
the keys are glyph names and the values are (very) simple glyph objects.<br>
|
||
|
<br>
|
||
|
To write a glyph to the glyph set, you use the <a href="#GlyphSet-writeGlyph">writeGlyph</a>() method.<br>
|
||
|
The simple glyph objects returned through the dict interface do not<br>
|
||
|
support writing, they are just means as a convenient way to get at<br>
|
||
|
the glyph data.<br> </tt></td></tr>
|
||
|
<tr><td> </td>
|
||
|
<td width="100%">Methods defined here:<br>
|
||
|
<dl><dt><a name="GlyphSet-__contains__"><strong>__contains__</strong></a> = <a href="#GlyphSet-has_key">has_key</a>(self, glyphName)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-__getitem__"><strong>__getitem__</strong></a>(self, glyphName)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-__init__"><strong>__init__</strong></a>(self, dirName, glyphNameToFileNameFunc<font color="#909090">=None</font>)</dt><dd><tt>'dirName' should be a path to an existing directory.<br>
|
||
|
<br>
|
||
|
The optional 'glyphNameToFileNameFunc' argument must be a callback<br>
|
||
|
function that takes two arguments: a glyph name and the <a href="#GlyphSet">GlyphSet</a><br>
|
||
|
instance. It should return a file name (including the .glif<br>
|
||
|
extension). The glyphNameToFileName function is called whenever<br>
|
||
|
a file name is created for a given glyph name.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-__len__"><strong>__len__</strong></a>(self)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-deleteGlyph"><strong>deleteGlyph</strong></a>(self, glyphName)</dt><dd><tt>Permanently delete the glyph from the glyph set on disk. Will<br>
|
||
|
raise KeyError if the glyph is not present in the glyph set.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-getReverseContents"><strong>getReverseContents</strong></a>(self)</dt><dd><tt>Return a reversed dict of self.<strong>contents</strong>, mapping file names to<br>
|
||
|
glyph names. This is primarily an aid for custom glyph name to file<br>
|
||
|
name schemes that want to make sure they don't generate duplicate<br>
|
||
|
file names. The file names are converted to lowercase so we can<br>
|
||
|
reliably check for duplicates that only differ in case, which is<br>
|
||
|
important for case-insensitive file systems.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-getUnicodes"><strong>getUnicodes</strong></a>(self)</dt><dd><tt>Return a dictionary that maps all glyph names to lists containing<br>
|
||
|
the unicode value[s] for that glyph, if any. This parses the .glif<br>
|
||
|
files partially, so is a lot faster than parsing all files completely.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-has_key"><strong>has_key</strong></a>(self, glyphName)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-keys"><strong>keys</strong></a>(self)</dt></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-readGlyph"><strong>readGlyph</strong></a>(self, glyphName, glyphObject<font color="#909090">=None</font>, pointPen<font color="#909090">=None</font>)</dt><dd><tt>Read a .glif file for 'glyphName' from the glyph set. The<br>
|
||
|
'glyphObject' argument can be any kind of object (even None);<br>
|
||
|
the <a href="#GlyphSet-readGlyph">readGlyph</a>() method will attempt to set the following<br>
|
||
|
attributes on it:<br>
|
||
|
"width" the advance with of the glyph<br>
|
||
|
"unicodes" a list of unicode values for this glyph<br>
|
||
|
"note" a string<br>
|
||
|
"lib" a dictionary containing custom data<br>
|
||
|
<br>
|
||
|
All attributes are optional, in two ways:<br>
|
||
|
1) An attribute *won't* be set if the .glif file doesn't<br>
|
||
|
contain data for it. 'glyphObject' will have to deal<br>
|
||
|
with default values itself.<br>
|
||
|
2) If setting the attribute fails with an AttributeError<br>
|
||
|
(for example if the 'glyphObject' attribute is read-<br>
|
||
|
only), <a href="#GlyphSet-readGlyph">readGlyph</a>() will not propagate that exception,<br>
|
||
|
but ignore that attribute.<br>
|
||
|
<br>
|
||
|
To retrieve outline information, you need to pass an object<br>
|
||
|
conforming to the PointPen protocol as the 'pointPen' argument.<br>
|
||
|
This argument may be None if you don't need the outline data.<br>
|
||
|
<br>
|
||
|
<a href="#GlyphSet-readGlyph">readGlyph</a>() will raise KeyError if the glyph is not present in<br>
|
||
|
the glyph set.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-rebuildContents"><strong>rebuildContents</strong></a>(self)</dt><dd><tt>Rebuild the contents dict by checking what glyphs are available<br>
|
||
|
on disk.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-writeContents"><strong>writeContents</strong></a>(self)</dt><dd><tt>Write the contents.plist file out to disk. Call this method when<br>
|
||
|
you're done writing glyphs.</tt></dd></dl>
|
||
|
|
||
|
<dl><dt><a name="GlyphSet-writeGlyph"><strong>writeGlyph</strong></a>(self, glyphName, glyphObject<font color="#909090">=None</font>, drawPointsFunc<font color="#909090">=None</font>)</dt><dd><tt>Write a .glif file for 'glyphName' to the glyph set. The<br>
|
||
|
'glyphObject' argument can be any kind of object (even None);<br>
|
||
|
the <a href="#GlyphSet-writeGlyph">writeGlyph</a>() method will attempt to get the following<br>
|
||
|
attributes from it:<br>
|
||
|
"width" the advance with of the glyph<br>
|
||
|
"unicodes" a list of unicode values for this glyph<br>
|
||
|
"note" a string<br>
|
||
|
"lib" a dictionary containing custom data<br>
|
||
|
<br>
|
||
|
All attributes are optional: if 'glyphObject' doesn't<br>
|
||
|
have the attribute, it will simply be skipped.<br>
|
||
|
<br>
|
||
|
To write outline data to the .glif file, <a href="#GlyphSet-writeGlyph">writeGlyph</a>() needs<br>
|
||
|
a function (any callable object actually) that will take one<br>
|
||
|
argument: an object that conforms to the PointPen protocol.<br>
|
||
|
The function will be called by <a href="#GlyphSet-writeGlyph">writeGlyph</a>(); it has to call the<br>
|
||
|
proper PointPen methods to transfer the outline to the .glif file.</tt></dd></dl>
|
||
|
|
||
|
<hr>
|
||
|
Data and other attributes defined here:<br>
|
||
|
<dl><dt><strong>glyphClass</strong> = <class robofab.glifLib.Glyph at 0x549120><dd><tt>Minimal glyph object. It has no glyph attributes until either<br>
|
||
|
the draw() or the drawPoint() method has been called.</tt></dl>
|
||
|
|
||
|
</td></tr></table></td></tr></table><p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#eeaa77">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#ffffff" face="helvetica, arial"><big><strong>Functions</strong></big></font></td></tr>
|
||
|
|
||
|
<tr><td bgcolor="#eeaa77"><tt> </tt></td><td> </td>
|
||
|
<td width="100%"><dl><dt><a name="-StringIO"><strong>StringIO</strong></a>(...)</dt><dd><tt><a href="#-StringIO">StringIO</a>([s]) -- Return a StringIO-like stream for reading or writing</tt></dd></dl>
|
||
|
<dl><dt><a name="-buildOutline_Format0"><strong>buildOutline_Format0</strong></a>(pen, xmlNodes)</dt></dl>
|
||
|
<dl><dt><a name="-buildOutline_Format1"><strong>buildOutline_Format1</strong></a>(pen, xmlNodes)</dt></dl>
|
||
|
<dl><dt><a name="-glyphNameToFileName"><strong>glyphNameToFileName</strong></a>(glyphName, glyphSet)</dt><dd><tt>Default algorithm for making a file name out of a glyph name.<br>
|
||
|
This one has limited support for case insensitive file systems:<br>
|
||
|
it assumes glyph names are not case sensitive apart from the first<br>
|
||
|
character:<br>
|
||
|
'a' -> 'a.glif'<br>
|
||
|
'A' -> 'A_.glif'<br>
|
||
|
'A.alt' -> 'A_.alt.glif'<br>
|
||
|
'A.Alt' -> 'A_.Alt.glif' # this one would cause problems</tt></dd></dl>
|
||
|
<dl><dt><a name="-readGlyphFromString"><strong>readGlyphFromString</strong></a>(aString, glyphObject<font color="#909090">=None</font>, pointPen<font color="#909090">=None</font>)</dt><dd><tt>Read .glif data from a string into a glyph object.<br>
|
||
|
<br>
|
||
|
The 'glyphObject' argument can be any kind of object (even None);<br>
|
||
|
the <a href="#-readGlyphFromString">readGlyphFromString</a>() method will attempt to set the following<br>
|
||
|
attributes on it:<br>
|
||
|
"width" the advance with of the glyph<br>
|
||
|
"unicodes" a list of unicode values for this glyph<br>
|
||
|
"note" a string<br>
|
||
|
"lib" a dictionary containing custom data<br>
|
||
|
<br>
|
||
|
All attributes are optional, in two ways:<br>
|
||
|
1) An attribute *won't* be set if the .glif file doesn't<br>
|
||
|
contain data for it. 'glyphObject' will have to deal<br>
|
||
|
with default values itself.<br>
|
||
|
2) If setting the attribute fails with an AttributeError<br>
|
||
|
(for example if the 'glyphObject' attribute is read-<br>
|
||
|
only), <a href="#-readGlyphFromString">readGlyphFromString</a>() will not propagate that<br>
|
||
|
exception, but ignore that attribute.<br>
|
||
|
<br>
|
||
|
To retrieve outline information, you need to pass an object<br>
|
||
|
conforming to the PointPen protocol as the 'pointPen' argument.<br>
|
||
|
This argument may be None if you don't need the outline data.</tt></dd></dl>
|
||
|
<dl><dt><a name="-writeGlyphToString"><strong>writeGlyphToString</strong></a>(glyphName, glyphObject<font color="#909090">=None</font>, drawPointsFunc<font color="#909090">=None</font>, writer<font color="#909090">=None</font>)</dt><dd><tt>Return .glif data for a glyph as a UTF-8 encoded string.<br>
|
||
|
The 'glyphObject' argument can be any kind of object (even None);<br>
|
||
|
the <a href="#-writeGlyphToString">writeGlyphToString</a>() method will attempt to get the following<br>
|
||
|
attributes from it:<br>
|
||
|
"width" the advance with of the glyph<br>
|
||
|
"unicodes" a list of unicode values for this glyph<br>
|
||
|
"note" a string<br>
|
||
|
"lib" a dictionary containing custom data<br>
|
||
|
<br>
|
||
|
All attributes are optional: if 'glyphObject' doesn't<br>
|
||
|
have the attribute, it will simply be skipped.<br>
|
||
|
<br>
|
||
|
To write outline data to the .glif file, <a href="#-writeGlyphToString">writeGlyphToString</a>() needs<br>
|
||
|
a function (any callable object actually) that will take one<br>
|
||
|
argument: an object that conforms to the PointPen protocol.<br>
|
||
|
The function will be called by <a href="#-writeGlyphToString">writeGlyphToString</a>(); it has to call the<br>
|
||
|
proper PointPen methods to transfer the outline to the .glif file.</tt></dd></dl>
|
||
|
</td></tr></table><p>
|
||
|
<table width="100%" cellspacing=0 cellpadding=2 border=0 summary="section">
|
||
|
<tr bgcolor="#55aa55">
|
||
|
<td colspan=3 valign=bottom> <br>
|
||
|
<font color="#ffffff" face="helvetica, arial"><big><strong>Data</strong></big></font></td></tr>
|
||
|
|
||
|
<tr><td bgcolor="#55aa55"><tt> </tt></td><td> </td>
|
||
|
<td width="100%"><strong>READ_MODE</strong> = 'r'<br>
|
||
|
<strong>WRITE_MODE</strong> = 'w'<br>
|
||
|
<strong>__all__</strong> = ['GlyphSet', 'GlifLibError', 'readGlyphFromString', 'writeGlyphToString', 'glyphNameToFileName']</td></tr></table>
|
||
|
</body></html>
|