RFont()
Usage
#Usage examples # start using the current font f = CurrentFont() # get a clean, empty new font object, # appropriate for the current environment f = robofab.world.RFont() # get an open dialog and start a new font f = OpenFont() # open the font at path f = OpenFont(path)
Description
Perhaps the first object you get to play with. The RFont object is the central part that connects all glyphs with font information (like names, key dimensions etc.). In FontLab the RFont object talks directly to the glyphs and font data in the FontLab font it belongs to. In UFO or NoneLab use, the RFont object contains the data and saves it to UFO. RFont object behave like dictionaries: the glyphname is the key and the returned value is a RGlyph object for that glyph. If the glyph does not exist RFont will raise an IndexError. RFont has a couple of important sub-objects which are worth checking out. The font's kerning is stored in a RKerning object and can be reached as an attribute at RFont.kerning. Fontnames, key dimensions, flags etc are stored in a RInfo object which is available through RFont.info. The RFont.lib is an RLib object which behaves as a dictionary.
Iterating
One of the most important uses of the RFont object is that it makes it really easy to iterate ("step through") the glyphs in the font.
f = CurrentFont() for glyph in f: print glyph.name >>>a >>>b >>>c etc..This makes the code clear and simple.
FontLab / UFO
All basic attributes, methods and behaviour for RFont objects made in FontLab or in NoneLab are identical. However, the RFont objects in FontLab have some additional attributes and methods that make special FontLab functionality available. These extra methods and attributes are listed seperately below.
RFont Attributes
- path: the path to the font. (read only)
- kerning: the RKerning object.
- info: the RInfo object with all the font's names and key dimensions.
- lib: the lib object which behaves like a dictionary for arbitrary data that needs to be stored with the font. In FontLab the lib is stored in the .vfb file. In UFO based fonts the lib is a seperate .plist file. Have a look at how to use the lib
Attribute examples
# Most useful attributes of RFont are # actually stored in RFont.info f = CurrentFont() print f.info.unitsPerEm >>> 2048 # len() gives you the "length" of the font, i.e. the number of glyphs print len(f) >>> 1120 # treat a font object as a dictionary to get to the glyphs print f["A"] >>> < Glyph for MyFont.A >
RFont Methods available in FontLab and UFO
- RFont[glyphName]: asking the font for a glyph by glyphName like a dictionary.
- has_key(glyphName): return True if glyphName is present in the font.
- keys(): return a list of all glyphnames in this font.
- newGlyph(glyphName, clear=True): create a new, empty glyph in the font with glyphName. If clear is True (by default) this will clear the glyph if it already exists under this name.
- removeGlyph(glyphName): remove a glyph from the font. This method will show a slightly different behaviour in FontLab and pure Python. In FontLab, components that reference the glyph that is being removed will be decomposed. In plain Python, the components will continue to point to the glyph.
- insertGlyph(aGlyph) inserts aGlyph in the font. The new glyph is returned. If the font already has a glyph with the same name the exisiting data is deleted.
- compileGlyph(glyphName, baseName, accentNames, adjustWidth=False, preflight=False, printErrors=True): Compile components into a new glyph using components and anchorpoints. glyphName: the name of the glyph where it all needs to go. baseName: the name of the base glyph. accentNames: a list of accentName, anchorName tuples, [('acute', 'top'), etc]
- generateGlyph(glyphName, replace=True, preflight=False, printErrors=True): Generate a glyph and return it. Assembled from GlyphConstruction.txt. replace is True the font will replace the glyph if there is already one with this name. preflight is True: the font will attempt to generate the glyph without adding it to the font. Do this to find out if there are any problems to make this glyph. For instance missing glyphs or components could be a problem. See building accents.
- save(destDir=None, doProgress=False, saveNow=False): Save the font.
- autoUnicodes(): Using fontTools.agl, assign Unicode lists to all glyphs in the font
- interpolate(): see how to interpolate for a detailed description of the interpolate method in RFont.
- round(): round all of the coordinates in all of the glyphs to whole integer numbers. For instance a point at (12.3, -10.99) becomes (12, -11). UFO based fonts can deal with floating point coordinates, but for use in FontLab everything needs to be rounded otherwise bad things happen.
- update(): call to FontLab to refresh the font. You call update() after doing lots of manipulating and editing. In UFO based RFont objects update() doesn't do anything, but it exists.
- copy(): returns a deep copy of the font, i.e. all glyphs and all associated data is duplicated.
- getCharacterMapping(): returns a dict of unicode values to glyph names.
RFont Methods available FontLab only
- naked(): return the wrapped fontlab font object itself. This can be useful if you want to set very specific values in the fontlab font that aren't wrapped or handled by RoboFab objects.
- close(): close the font object and the font window in FontLab.
- appendHGuide(): append a horizontal guide.
- appendVGuide(): append a vertical guide.
- clearHGuides(): clear all horizontal guides
- clearVGuides(): clear all vertical guides
- generate(outputType, path=None): call FontLab to generate fonts with these parameters and location. Have a look at generate fonts for a more detailed description of this method and how to use it.
RFont Attributes available FontLab only
Method examples
f = CurrentFont() # the keys() method returns a list of glyphnames: print f.keys() >>> ['A', 'B', 'space', 'adieresis.alt1'] # find unicodes for each glyph by using the postscript name: f.autoUnicodes()