Skip to main content

A bridge from Glyphs source files (.glyphs) to UFOs

Project description

Travis Build Status PyPI Version Codecov Join the chat at https://gitter.im/fonttools-dev/glyphsLib

glyphsLib

This library provides a bridge from Glyphs source files (.glyphs) to UFOs via defcon.

The main methods for conversion are found in __init__.py. Intermediate data can be accessed without actually writing UFOs, if needed.

Write and return UFOs

Masters:

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

Interpolated instances (depends on MutatorMath):

master_dir = 'master_ufos'
instance_dir = 'instance_ufos'
ufos = glyphsLib.build_instances('MyFont.glyphs', master_dir, instance_dir)

Load UFO objects without writing

ufos = glyphsLib.load_to_ufos('MyFont.glyphs')

Read and write Glyphs data as Python objects

from glyphsLib import GSFont

font = GSFont(glyphs_file)

font.save(glyphs_file)

The glyphsLib.classes module aims to provide an interface similar to Glyphs.app’s Python Scripting API.

Note that currently not all the classes and methods may be fully implemented. We try to keep up to date, but if you find something that is missing or does not work as expected, please open a issue.

Go back and forth between UFOs and Glyphs

  1. Without a designspace file, using for example the Inria fonts by Black[Foundry]:

import glob
from defcon import Font
from glyphsLib import to_glyphs
ufos = [Font(path) for path in glob.glob('*Italic.ufo')]
# Sort the UFOs because glyphsLib will create masters in the same order
ufos = sorted(ufos, key=lambda ufo: ufo.info.openTypeOS2WeightClass)
font = to_glyphs(ufos)
font.save('InriaSansItalic.glyphs')

Here is the resulting glyphs file

  1. With a designspace, using Spectral from Production Type:

import glob
from fontTools.designspaceLib import DesignSpaceDocument
from glyphsLib import to_glyphs
doc = DesignSpaceDocument()
doc.read('spectral-build-roman.designspace')
font = to_glyphs(doc)
font.save('SpectralRoman.glyphs')

Here is the resulting glyphs file

3. In both cases, if you intend to go back to UFOs after modifying the file with Glyphs, you should use the minimize_ufo_diffs parameter to minimize the amount of diffs that will show up in git after the back and forth. To do so, the glyphsLib will add some bookkeeping values in various userData fields. For example, it will try to remember which GSClass came from groups.plist or from the feature file.

The same option exists for people who want to do Glyphs->UFOs->Glyphs: minimize_glyphs_diffs, which will add some bookkeeping data in UFO lib. For example, it will keep the same UUIDs for Glyphs layers, and so will need to store those layer UUIDs in the UFOs.

import glob
import os
from fontTools.designspaceLib import DesignSpaceDocument
from glyphsLib import to_glyphs, to_designspace, GSFont
doc = DesignSpaceDocument()
doc.read('spectral-build-roman.designspace')
font = to_glyphs(doc, minimize_ufo_diffs=True)
doc2 = to_designspace(font, propagate_anchors=False)
# UFOs are in memory only, attached to the doc via `sources`
# Writing doc2 over the original doc should generate very few git diffs (ideally none)
doc2.write(doc.path)
for source in doc2.sources:
    path = os.path.join(os.path.dirname(doc.path), source.filename)
    # You will want to use ufoNormalizer after
    source.font.save(path)

font = GSFont('SpectralRoman.glyphs')
doc = to_designspace(font, minimize_glyphs_diffs=True, propagate_anchors=False)
font2 = to_glyphs(doc)
# Writing font2 over font should generate very few git diffs (ideally none):
font2.save(font.filepath)

In practice there are always a few diffs on things that don’t really make a difference, like optional things being added/removed or whitespace changes or things getting reordered…

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

glyphsLib-2.3.1.zip (394.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

glyphsLib-2.3.1-py2.py3-none-any.whl (309.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file glyphsLib-2.3.1.zip.

File metadata

  • Download URL: glyphsLib-2.3.1.zip
  • Upload date:
  • Size: 394.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for glyphsLib-2.3.1.zip
Algorithm Hash digest
SHA256 285742d52ca3f473379c6e507db7b7427f53ecf53f3f1986f24ab13bb89d2e9e
MD5 4194acbec65feeac22c8e41a33b5e52b
BLAKE2b-256 5efabc52379b4371af2306df9d611d3012717a2d5feecc39713e2057fd85ce89

See more details on using hashes here.

File details

Details for the file glyphsLib-2.3.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for glyphsLib-2.3.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 45ecde8376b1e0936f6ede8fb4e1679981d48d7a9d23af0777c6b2c4866bcd17
MD5 f5b2e6b5bc165868dcd58176049edd40
BLAKE2b-256 d3d4940e2c871d1d04c21f255af6555c5ae96392e69c38e7b2579f9f18619c2d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page