Using glyphsLib Python Library to Convert Glyphs Source Files

Lake Davenberg Avatar

·

The glyphsLib Python library, developed by Google Fonts, provides a powerful bridge between Glyphs source files (.glyphs) and UFOs and Designspace files. This library is built on top of the defcon and designspaceLib packages, making it a versatile tool for font developers.

Code Implementation 1: Write and return UFOs

The glyphsLib library allows you to easily convert Glyphs source files to UFOs and a Designspace file. The following code snippet demonstrates how to write UFOs and a Designspace file to disk:

python
import glyphsLib

master_dir = "master_ufos"
ufos, designspace_path = glyphsLib.build_masters("MyFont.glyphs", master_dir)

This code imports the glyphsLib module and uses the build_masters function to create UFOs from a given Glyphs file. The resulting UFOs and the path to the Designspace file are returned, allowing you to further work with them.

Code Implementation 2: Load UFO objects without writing

If you prefer to work with UFO objects without actually writing them to disk, the glyphsLib library provides a convenient method for loading Glyphs source files directly into UFOs. Here’s an example:

python
import glyphsLib

ufos = glpyhsLib.load_to_ufos("MyFont.glyphs")

In this code snippet, the load_to_ufos function is used to load the Glyphs source file “MyFont.glyphs” into UFO objects. These UFO objects can then be manipulated or analyzed without the need for writing them to disk.

Code Implementation 3: Read and write Glyphs data as Python objects

Another useful feature of the glyphsLib library is the ability to read and write Glyphs data as Python objects. This allows you to easily access and modify the data within a Glyphs source file. Here’s an example:

python
from glyphsLib import GSFont

font = GSFont(glyphs_file)
font.save(glyphs_file)

In this code snippet, the GSFont class from the glyphsLib module is used to load a Glyphs file into a Python object. This object can then be modified or analyzed, and the changes can be saved back to the original Glyphs file.

Discussion

The glyphsLib Python library provides a seamless workflow for font developers working with Glyphs source files. By leveraging its powerful conversion methods, you can easily generate UFOs and Designspace files for further font processing. The ability to load Glyphs data into UFO objects without writing them to disk allows for efficient analysis and manipulation of font data. Additionally, the library’s support for reading and writing Glyphs data as Python objects provides a convenient way to modify font files programmatically.

Overall, the glyphsLib library simplifies the font development process by providing a Pythonic interface to work with Glyphs source files. Whether you need to convert files, load data for analysis, or modify Glyphs data programmatically, glyphsLib has you covered.

Category: Font Development

Tags: Python, font development, glyphs, UFO, Designspace

Leave a Reply

Your email address will not be published. Required fields are marked *