# glifLib: Read and write UFO .glif files

Generic module for reading and writing the .glif format.

More info about the .glif format (GLyphInterchangeFormat) can be found here:

> http://unifiedfontobject.org

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.

class fontTools.ufoLib.glifLib.GlyphSet(path: PathOrFS, glyphNameToFileNameFunc: GlyphNameToFileNameFunc = None, ufoFormatVersion: UFOFormatVersionInput = None, validateRead: bool = True, validateWrite: bool = True, expectContentsFile: bool = False)

  Bases: `_UFOBaseIO`

  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 a convenient way to get at the glyph data.

  glyphClass

    alias of `Glyph`

  rebuildContents(validateRead: bool = False) → None

    Rebuild the contents dict by loading contents.plist.

    `validateRead` will validate the data, by default it is set to the class’s `validateRead` value, can be overridden.

  getReverseContents() → dict[str, str]

    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.

  writeContents() → None

    Write the contents.plist file out to disk. Call this method when you’re done writing glyphs.

  readLayerInfo(info: Any, validateRead: bool | None = None) → None

    `validateRead` will validate the data, by default it is set to the class’s `validateRead` value, can be overridden.

  writeLayerInfo(info: Any, validateWrite: bool | None = None) → None

    `validateWrite` will validate the data, by default it is set to the class’s `validateWrite` value, can be overridden.

  getGLIF(glyphName: str) → bytes

    Get the raw GLIF text for a given glyph name. This only works for GLIF files that are already on disk.

    This method is useful in situations when the raw XML needs to be read from a glyph set for a particular glyph before fully parsing it into an object structure via the readGlyph method.

    Raises KeyError if ‘glyphName’ is not in contents.plist, or GlifLibError if the file associated with can’t be found.

  getGLIFModificationTime(glyphName: str) → float | None

    Returns the modification time for the GLIF file with ‘glyphName’, as a floating point number giving the number of seconds since the epoch. Return None if the associated file does not exist or the underlying filesystem does not support getting modified times. Raises KeyError if the glyphName is not in contents.plist.

  readGlyph(glyphName: str, glyphObject: Any | None = None, pointPen: AbstractPointPen | None = None, validate: bool | None = None) → 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 width of the glyph

    height

      the advance height of the glyph

    unicodes

      a list of unicode values for this glyph

    note

      a string

    lib

      a dictionary containing custom data

    image

      a dictionary containing image data

    guidelines

      a list of guideline data dictionaries

    anchors

      a list of anchor data dictionaries

    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.

    `validate` will validate the data, by default it is set to the class’s `validateRead` value, can be overridden.

  writeGlyph(glyphName: str, glyphObject: Any | None = None, drawPointsFunc: Callable[[AbstractPointPen], None] | None = None, formatVersion: GLIFFormatVersionInput = None, validate: bool | None = None) → 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 width of the glyph

    height

      the advance height of the glyph

    unicodes

      a list of unicode values for this glyph

    note

      a string

    lib

      a dictionary containing custom data

    image

      a dictionary containing image data

    guidelines

      a list of guideline data dictionaries

    anchors

      a list of anchor data dictionaries

    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.

    The GLIF format version will be chosen based on the ufoFormatVersion passed during the creation of this object. If a particular format version is desired, it can be passed with the formatVersion argument. The formatVersion argument accepts either a tuple of integers for (major, minor), or a single integer for the major digit only (with minor digit implied as 0).

    An UnsupportedGLIFFormat exception is raised if the requested GLIF formatVersion is not supported.

    `validate` will validate the data, by default it is set to the class’s `validateWrite` value, can be overridden.

  deleteGlyph(glyphName: str) → None

    Permanently delete the glyph from the glyph set on disk. Will raise KeyError if the glyph is not present in the glyph set.

  keys() → list[str]
  has_key(glyphName: str) → bool
  getUnicodes(glyphNames: Iterable[str] | None = None) → dict[str, list[int]]

    Return a dictionary that maps glyph names to lists containing the unicode value[s] for that glyph, if any. This parses the .glif files partially, so it is a lot faster than parsing all files completely. By default this checks all glyphs, but a subset can be passed with glyphNames.

  getComponentReferences(glyphNames: Iterable[str] | None = None) → dict[str, list[str]]

    Return a dictionary that maps glyph names to lists containing the base glyph name of components in the glyph. This parses the .glif files partially, so it is a lot faster than parsing all files completely. By default this checks all glyphs, but a subset can be passed with glyphNames.

  getImageReferences(glyphNames: Iterable[str] | None = None) → dict[str, str | None]

    Return a dictionary that maps glyph names to the file name of the image referenced by the glyph. This parses the .glif files partially, so it is a lot faster than parsing all files completely. By default this checks all glyphs, but a subset can be passed with glyphNames.

  close() → None

exception fontTools.ufoLib.glifLib.GlifLibError

  Bases: `UFOLibError`

  An error raised by glifLib.

  This class is a loose backport of PEP 678, adding a `note` attribute that can hold additional context for errors encountered.

  It will be maintained until only Python 3.11-and-later are supported.

fontTools.ufoLib.glifLib.readGlyphFromString(aString: str | bytes, glyphObject: Any | None = None, pointPen: Any | None = None, formatVersions: FormatVersions = None, validate: bool = True) → 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 width of the glyph

  height

    the advance height of the glyph

  unicodes

    a list of unicode values for this glyph

  note

    a string

  lib

    a dictionary containing custom data

  image

    a dictionary containing image data

  guidelines

    a list of guideline data dictionaries

  anchors

    a list of anchor data dictionaries

  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.

  The formatVersions optional argument define the GLIF format versions that are allowed to be read. The type is Optional[Iterable[tuple[int, int], int]]. It can contain either integers (for the major versions to be allowed, with minor digits defaulting to 0), or tuples of integers to specify both (major, minor) versions. By default when formatVersions is None all the GLIF format versions currently defined are allowed to be read.

  `validate` will validate the read data. It is set to `True` by default.

fontTools.ufoLib.glifLib.writeGlyphToString(glyphName: str, glyphObject: Any | None = None, drawPointsFunc: Callable[[Any], None] | None = None, formatVersion: FormatVersion | None = None, validate: bool = True) → str

  Return .glif data for a glyph as a string. The XML declaration’s encoding is always set to “UTF-8”. 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 width of the glyph

  height

    the advance height of the glyph

  unicodes

    a list of unicode values for this glyph

  note

    a string

  lib

    a dictionary containing custom data

  image

    a dictionary containing image data

  guidelines

    a list of guideline data dictionaries

  anchors

    a list of anchor data dictionaries

  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.

  The GLIF format version can be specified with the formatVersion argument. This accepts either a tuple of integers for (major, minor), or a single integer for the major digit only (with minor digit implied as 0). By default when formatVesion is None the latest GLIF format version will be used; currently it’s 2.0, which is equivalent to formatVersion=(2, 0).

  An UnsupportedGLIFFormat exception is raised if the requested UFO formatVersion is not supported.

  `validate` will validate the written data. It is set to `True` by default.

fontTools.ufoLib.glifLib.glyphNameToFileName(glyphName: str, existingFileNames: set[str] | None) → str

  Wrapper around the userNameToFileName function in filenames.py

  Note that existingFileNames should be a set for large glyphsets or performance will suffer.