Skip to content

Commit

Permalink
Rename ufo_module, glyphs_module, masters
Browse files Browse the repository at this point in the history
  • Loading branch information
belluzj committed Oct 23, 2017
1 parent b7f3c9c commit dcebcf3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
11 changes: 6 additions & 5 deletions Lib/glyphsLib/builder/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def to_ufos(font, include_instances=False, family_name=None,
propagate_anchors=True, defcon=defcon):
propagate_anchors=True, ufo_module=defcon):
"""Take .glyphs file data and load it into UFOs.
Takes in data as Glyphs.app-compatible classes, as documented at
Expand All @@ -36,23 +36,24 @@ def to_ufos(font, include_instances=False, family_name=None,
"""
builder = UFOBuilder(
font,
defcon,
ufo_module=ufo_module,
family_name=family_name,
propagate_anchors=propagate_anchors)

result = list(builder.master_ufos)
result = list(builder.masters)

if include_instances:
return result, builder.instance_data
return result


def to_glyphs(ufos, designspace=None, classes=classes):
def to_glyphs(ufos, designspace=None, glyphs_module=classes):
"""
Take a list of UFOs and combine them into a single .glyphs file.
This should be the inverse function of `to_ufos`,
so we should have to_glyphs(to_ufos(font)) == font
"""
builder = GlyphsBuilder(ufos, designspace=designspace, classes=classes)
builder = GlyphsBuilder(
ufos, designspace=designspace, glyphs_module=glyphs_module)
return builder.font
38 changes: 23 additions & 15 deletions Lib/glyphsLib/builder/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,35 @@
from collections import OrderedDict
import logging

import defcon

logger = logging.getLogger(__name__)

from glyphsLib import classes
from .constants import PUBLIC_PREFIX, GLYPHS_PREFIX


class UFOBuilder(object):
"""Builder for Glyphs to UFO + designspace."""

def __init__(self, font, defcon, family_name=None, propagate_anchors=True):
def __init__(self,
font,
ufo_module=defcon,
family_name=None,
propagate_anchors=True):
"""Create a builder that goes from Glyphs to UFO + designspace.
Keyword arguments:
font -- The GSFont object to transform into UFOs
defcon -- The defcon module to use to build UFO objects (you can pass
a custom module that has the same classes as the official
defcon to get instances of your own classes)
ufo_module -- A Python module to use to build UFO objects (you can pass
a custom module that has the same classes as the official
defcon to get instances of your own classes)
family_name -- if provided, the master UFOs will be given this name and
only instances with this name will be returned.
propagate_anchors -- set to False to prevent anchor propagation
"""
self.font = font
self.defcon = defcon
self.ufo_module = ufo_module

# The set of UFOs (= defcon.Font objects) that will be built,
# indexed by master ID, the same order as masters in the source GSFont.
Expand Down Expand Up @@ -77,7 +83,7 @@ def __init__(self, font, defcon, family_name=None, propagate_anchors=True):


@property
def master_ufos(self):
def masters(self):
"""Get an iterator over master UFOs that match the given family_name.
"""
if self._ufos:
Expand Down Expand Up @@ -173,7 +179,7 @@ def instance_data(self):
self._instance_family_name))
instance_data = {'data': instances}

first_ufo = next(iter(self.master_ufos))
first_ufo = next(iter(self.masters))

# the 'Variation Font Origin' is a font-wide custom parameter, thus it is
# shared by all the master ufos; here we just get it from the first one
Expand Down Expand Up @@ -219,20 +225,22 @@ def filter_instances_by_family(instances, family_name=None):
class GlyphsBuilder(object):
"""Builder for UFO + designspace to Glyphs."""

def __init__(self, ufos, designspace, classes):
def __init__(self, ufos, designspace=None, glyphs_module=classes):
"""Create a builder that goes from UFOs + designspace to Glyphs.
Keyword arguments:
ufos -- The list of UFOs to combine into a GSFont
designspace -- A MutatorMath Designspace to use for the GSFont
classes -- The glyphsLib.classes module to use to build glyphsLib
classes (you can pass a custom module with the same classes
as the official glyphsLib.classes to get instances of your
own classes)
glyphs_module -- The glyphsLib.classes module to use to build glyphsLib
classes (you can pass a custom module with the same
classes as the official glyphsLib.classes to get
instances of your own classes, or pass the Glyphs.app
module that holds the official classes to import UFOs
into Glyphs.app)
"""
self.ufos = ufos
self.designspace = designspace
self.classes = classes
self.glyphs_module = glyphs_module

self._font = None
"""The GSFont that will be built."""
Expand All @@ -244,9 +252,9 @@ def font(self):
if self._font is not None:
return self._font

self._font = self.classes.GSFont()
self._font = self.glyphs_module.GSFont()
for index, ufo in enumerate(self.ufos):
master = self.classes.GSFontMaster()
master = self.glyphs_module.GSFontMaster()
self.to_glyphs_font_attributes(ufo, master,
is_initial=(index == 0))
self._font.masters.insert(len(self._font.masters), master)
Expand Down
2 changes: 1 addition & 1 deletion Lib/glyphsLib/builder/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def to_ufo_font_attributes(self, family_name):
manufacturer_url = font.manufacturerURL

for master in font.masters:
ufo = self.defcon.Font()
ufo = self.ufo_module.Font()

if date_created is not None:
ufo.info.openTypeHeadCreated = date_created
Expand Down