Font

See also

Notifications:
The Font object uses notifications to notify observers of changes.
External Changes:
The Font object can observe the files within the UFO for external modifications.

Tasks

Changed State

Notifications

Font

class defcon.Font(path=None, kerningClass=None, infoClass=None, groupsClass=None, featuresClass=None, libClass=None, unicodeDataClass=None, glyphClass=None, glyphContourClass=None, glyphPointClass=None, glyphComponentClass=None, glyphAnchorClass=None)

If loading from an existing UFO, path should be the path to the UFO.

If you subclass one of the sub objects, such as Glyph, the class must be registered when the font is created for defcon to know about it. The *Class arguments allow for individual ovverrides. If None is provided for an argument, the defcon appropriate class will be used.

This object posts the following notifications:

Name Note
Font.Changed Posted when the dirty attribute is set.
Font.ReloadedGlyphs Posted after the reloadGlyphs method has been called.

The Font object has some dict like behavior. For example, to get a glyph:

glyph = font["aGlyphName"]

To iterate over all glyphs:

for glyph in font:

To get the number of glyphs:

glyphCount = len(font)

To find out if a font contains a particular glyph:

exists = "aGlyphName" in font

To remove a glyph:

del font["aGlyphName"]
newGlyph(name)

Create a new glyph with name. If a glyph with that name already exists, the existing glyph will be replaced with the new glyph.

insertGlyph(glyph, name=None)

Insert glyph into the font. Optionally, the glyph can be renamed at the same time by providing name. If a glyph with the glyph name, or the name provided as name, already exists, the existing glyph will be replaced with the new glyph.

keys()

The names of all glyphs in the font.

path

The location of the file on disk. Setting the path should only be done when the user has moved the file in the OS interface. Setting the path is not the same as a save operation.

ufoFormatVersion

The UFO format version that will be used when saving. This is taken from a loaded UFO during __init__. If this font was not loaded from a UFO, this will return None until the font has been saved.

glyphsWithOutlines

A list of glyphs containing outlines.

componentReferences

A dict of describing the component relationshis in the font. The dictionary is of form {base glyph : [references]}.

bounds

The bounds of all glyphs in the font. This can be an expensive operation.

controlPointBounds

The control bounds of all glyphs in the font. This only measures the point positions, it does not measure curves. So, curves without points at the extrema will not be properly measured. This is an expensive operation.

info

The font’s Info object.

kerning

The font’s Kerning object.

groups

The font’s Groups object.

features

The font’s Features object.

lib

The font’s Lib object.

unicodeData

The font’s UnicodeData object.

save(path=None, formatVersion=None)

Save the font to path. If path is None, the path from the last save or when the font was first opened will be used.

The UFO will be saved using the format found at ufoFormatVersion. This value is either the format version from the exising UFO or the format version specified in a previous save. If neither of these is available, the UFO will be written as format version 2. If you wish to specifiy the format version for saving, pass the desired number as the formatVersion argument.

testForExternalChanges()

Test the UFO for changes that occured outside of this font’s tree of objects. This returns a dictionary of values indicating if the objects have changes on disk that are not loaded. For example:

{
    "info" : False,
    "kerning" : True,
    "groups" : True,
    "features" : False,
    "lib" : False,
    "modifiedGlyphs" : ["a", "a.alt"],
    "addedGlyphs" : [],
    "deletedGlyphs" : []
}

It is important to keep in mind that the user could have created conflicting data outside of the font’s tree of objects. For example, say the user has set font.info.unitsPerEm = 1000 inside of the font’s Info object and the user has not saved this change. In the the font’s fontinfo.plist file, the user sets the unitsPerEm value to 2000. Which value is current? Which value is right? defcon leaves this decision up to you.

reloadInfo()

Reload the data in the Info object from the fontinfo.plist file in the UFO.

reloadKerning()

Reload the data in the Kerning object from the kerning.plist file in the UFO.

reloadGroups()

Reload the data in the Groups object from the groups.plist file in the UFO.

reloadFeatures()

Reload the data in the Features object from the features.fea file in the UFO.

reloadLib()

Reload the data in the Lib object from the lib.plist file in the UFO.

reloadGlyphs(glyphNames)

Reload the glyphs listed in glyphNames from the appropriate files within the UFO. When all of the loading is complete, a Font.ReloadedGlyphs notification will be posted.

addObserver(observer, methodName, notification)

Add an observer to this object’s notification dispatcher.

  • observer An object that can be referenced with weakref.
  • methodName A string epresenting the method to be called when the notification is posted.
  • notification The notification that the observer should be notified of.

The method that will be called as a result of the action must accept a single notification argument. This will be a defcon.tools.notifications.Notification object.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.addObserver(observer=observer, methodName=methodName,
    notification=notification, observable=anObject)
dirty

The dirty state of the object. True if the object has been changed. False if not. Setting this to True will cause the base changed notification to be posted. The object will automatically maintain this attribute and update it as you change the object.

disableNotifications(notification=None, observer=None)

Disable this object’s notifications until told to resume them.

  • notification The specific notification to disable. This is optional. If no notification is given, all notifications will be disabled.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.disableNotifications(
    observable=anObject, notification=notification, observer=observer)
dispatcher

The defcon.tools.notifications.NotificationCenter assigned to this object.

enableNotifications(notification=None, observer=None)

Enable this object’s notifications.

  • notification The specific notification to enable. This is optional.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.enableNotifications(
    observable=anObject, notification=notification, observer=observer)
getParent()

Get the parent. Returns None if no parent is set. Note that because the reference to the parent is stored as a weakref, the parent can disappear if it is no longer referenced by any object other than this one.

hasObserver(observer, notification)

Returns a boolean indicating is the observer is registered for notification.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.hasObserver(observer=observer,
    notification=notification, observable=anObject)
holdNotifications(notification=None)

Hold this object’s notifications until told to release them.

  • notification The specific notification to hold. This is optional. If no notification is given, all notifications will be held.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.holdNotifications(
    observable=anObject, notification=notification)
releaseHeldNotifications(notification=None)

Release this object’s held notifications.

  • notification The specific notification to hold. This is optional.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.releaseHeldNotifications(
    observable=anObject, notification=notification)
removeObserver(observer, notification)

Remove an observer from this object’s notification dispatcher.

  • observer A registered object.
  • notification The notification that the observer was registered to be notified of.

This is a convenience method that does the same thing as:

dispatcher = anObject.dispatcher
dispatcher.removeObserver(observer=observer,
    notification=notification, observable=anObject)
setParent(obj)

Set the parent of the object. This will reference the parent using weakref.

undoManager

The undo manager assigned to this object.