robofab.glifLib
index
/code/projects/robofab/Lib/robofab/glifLib.py

glifLib.py -- Generic module for reading and writing the .glif format.
 
More info about the .glif format (GLyphInterchangeFormat) can be found here:
 
  http://just.letterror.com/ltrwiki/GlyphInterchangeFormat
 
The main class in this module is GlyphSet. It manages a set of .glif files
in a folder. It offers two ways to read glyph data, and one way to write
glyph data. See the class doc string for details.

 
Modules
       
os

 
Classes
       
robofab.pens.pointPen.AbstractPointPen
GLIFPointPen
exceptions.Exception
GlifLibError
Glyph
GlyphSet

 
class GLIFPointPen(robofab.pens.pointPen.AbstractPointPen)
    Helper class using the PointPen protocol to write the <outline>
part of .glif files.
 
  Methods defined here:
__init__(self, xmlWriter)
addComponent(self, glyphName, transformation)
addPoint(self, pt, segmentType=None, smooth=None, name=None, **kwargs)
beginPath(self)
endPath(self)

 
class GlifLibError(exceptions.Exception)
     Methods inherited from exceptions.Exception:
__getitem__(...)
__init__(...)
__str__(...)

 
class Glyph
    Minimal glyph object. It has no glyph attributes until either
the draw() or the drawPoint() method has been called.
 
  Methods defined here:
__init__(self, glyphName, glyphSet)
draw(self, pen)
Draw this glyph onto a *FontTools* Pen.
drawPoints(self, pointPen)
Draw this glyph onto a PointPen.

 
class GlyphSet
    GlyphSet manages a set of .glif files inside one directory.
 
GlyphSet's constructor takes a path to an existing directory as it's
first argument. Reading glyph data can either be done through the
readGlyph() method, or by using GlyphSet's dictionary interface, where
the keys are glyph names and the values are (very) simple glyph objects.
 
To write a glyph to the glyph set, you use the writeGlyph() method.
The simple glyph objects returned through the dict interface do not
support writing, they are just means as a convenient way to get at
the glyph data.
 
  Methods defined here:
__contains__ = has_key(self, glyphName)
__getitem__(self, glyphName)
__init__(self, dirName, glyphNameToFileNameFunc=None)
'dirName' should be a path to an existing directory.
 
The optional 'glyphNameToFileNameFunc' argument must be a callback
function that takes two arguments: a glyph name and the GlyphSet
instance. It should return a file name (including the .glif
extension). The glyphNameToFileName function is called whenever
a file name is created for a given glyph name.
__len__(self)
deleteGlyph(self, glyphName)
Permanently delete the glyph from the glyph set on disk. Will
raise KeyError if the glyph is not present in the glyph set.
getReverseContents(self)
Return a reversed dict of self.contents, mapping file names to
glyph names. This is primarily an aid for custom glyph name to file
name schemes that want to make sure they don't generate duplicate
file names. The file names are converted to lowercase so we can
reliably check for duplicates that only differ in case, which is
important for case-insensitive file systems.
getUnicodes(self)
Return a dictionary that maps all glyph names to lists containing
the unicode value[s] for that glyph, if any. This parses the .glif
files partially, so is a lot faster than parsing all files completely.
has_key(self, glyphName)
keys(self)
readGlyph(self, glyphName, glyphObject=None, pointPen=None)
Read a .glif file for 'glyphName' from the glyph set. The
'glyphObject' argument can be any kind of object (even None);
the readGlyph() method will attempt to set the following
attributes on it:
        "width"     the advance with of the glyph
        "unicodes"  a list of unicode values for this glyph
        "note"      a string
        "lib"       a dictionary containing custom data
 
All attributes are optional, in two ways:
        1) An attribute *won't* be set if the .glif file doesn't
           contain data for it. 'glyphObject' will have to deal
           with default values itself.
        2) If setting the attribute fails with an AttributeError
           (for example if the 'glyphObject' attribute is read-
           only), readGlyph() will not propagate that exception,
           but ignore that attribute.
 
To retrieve outline information, you need to pass an object
conforming to the PointPen protocol as the 'pointPen' argument.
This argument may be None if you don't need the outline data.
 
readGlyph() will raise KeyError if the glyph is not present in
the glyph set.
rebuildContents(self)
Rebuild the contents dict by checking what glyphs are available
on disk.
writeContents(self)
Write the contents.plist file out to disk. Call this method when
you're done writing glyphs.
writeGlyph(self, glyphName, glyphObject=None, drawPointsFunc=None)
Write a .glif file for 'glyphName' to the glyph set. The
'glyphObject' argument can be any kind of object (even None);
the writeGlyph() method will attempt to get the following
attributes from it:
        "width"     the advance with of the glyph
        "unicodes"  a list of unicode values for this glyph
        "note"      a string
        "lib"       a dictionary containing custom data
 
All attributes are optional: if 'glyphObject' doesn't
have the attribute, it will simply be skipped.
 
To write outline data to the .glif file, writeGlyph() needs
a function (any callable object actually) that will take one
argument: an object that conforms to the PointPen protocol.
The function will be called by writeGlyph(); it has to call the
proper PointPen methods to transfer the outline to the .glif file.

Data and other attributes defined here:
glyphClass = <class robofab.glifLib.Glyph at 0x549120>
Minimal glyph object. It has no glyph attributes until either
the draw() or the drawPoint() method has been called.

 
Functions
       
StringIO(...)
StringIO([s]) -- Return a StringIO-like stream for reading or writing
buildOutline_Format0(pen, xmlNodes)
buildOutline_Format1(pen, xmlNodes)
glyphNameToFileName(glyphName, glyphSet)
Default algorithm for making a file name out of a glyph name.
This one has limited support for case insensitive file systems:
it assumes glyph names are not case sensitive apart from the first
character:
        'a'     -> 'a.glif'
        'A'     -> 'A_.glif'
        'A.alt' -> 'A_.alt.glif'
        'A.Alt' -> 'A_.Alt.glif'  # this one would cause problems
readGlyphFromString(aString, glyphObject=None, pointPen=None)
Read .glif data from a string into a glyph object.
 
The 'glyphObject' argument can be any kind of object (even None);
the readGlyphFromString() method will attempt to set the following
attributes on it:
        "width"     the advance with of the glyph
        "unicodes"  a list of unicode values for this glyph
        "note"      a string
        "lib"       a dictionary containing custom data
 
All attributes are optional, in two ways:
        1) An attribute *won't* be set if the .glif file doesn't
           contain data for it. 'glyphObject' will have to deal
           with default values itself.
        2) If setting the attribute fails with an AttributeError
           (for example if the 'glyphObject' attribute is read-
           only), readGlyphFromString() will not propagate that
           exception, but ignore that attribute.
 
To retrieve outline information, you need to pass an object
conforming to the PointPen protocol as the 'pointPen' argument.
This argument may be None if you don't need the outline data.
writeGlyphToString(glyphName, glyphObject=None, drawPointsFunc=None, writer=None)
Return .glif data for a glyph as a UTF-8 encoded string.
The 'glyphObject' argument can be any kind of object (even None);
the writeGlyphToString() method will attempt to get the following
attributes from it:
        "width"     the advance with of the glyph
        "unicodes"  a list of unicode values for this glyph
        "note"      a string
        "lib"       a dictionary containing custom data
 
All attributes are optional: if 'glyphObject' doesn't
have the attribute, it will simply be skipped.
 
To write outline data to the .glif file, writeGlyphToString() needs
a function (any callable object actually) that will take one
argument: an object that conforms to the PointPen protocol.
The function will be called by writeGlyphToString(); it has to call the
proper PointPen methods to transfer the outline to the .glif file.

 
Data
        READ_MODE = 'r'
WRITE_MODE = 'w'
__all__ = ['GlyphSet', 'GlifLibError', 'readGlyphFromString', 'writeGlyphToString', 'glyphNameToFileName']