Skip to content

Commit

Permalink
Rebase onto master, add new custom params
Browse files Browse the repository at this point in the history
  • Loading branch information
belluzj committed Nov 24, 2017
1 parent 52cd7bc commit a43f1d8
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 67 deletions.
3 changes: 1 addition & 2 deletions Lib/glyphsLib/builder/builders.py
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,7 @@ def font(self):
from .blue_values import to_glyphs_blue_values
from .components import (to_glyphs_components,
to_glyphs_smart_component_axes)
from .custom_params import (to_glyphs_family_custom_params,
to_glyphs_master_custom_params)
from .custom_params import to_glyphs_custom_params
from .features import to_glyphs_features
from .font import to_glyphs_font_attributes, to_glyphs_ordered_masters
from .glyph import to_glyphs_glyph
Expand Down
91 changes: 30 additions & 61 deletions Lib/glyphsLib/builder/custom_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
unicode_literals)

from collections import defaultdict
import re

from glyphsLib.util import bin_to_int_list, int_list_to_bin
from .filters import parse_glyphs_filter, write_glyphs_filter
Expand Down Expand Up @@ -136,14 +137,6 @@ def unhandled_lib_items(self):


class AbstractParamHandler(object):
# @abstractmethod
def glyphs_names(self):
return []

# @abstractmethod
def ufo_names(self):
return []

# @abstractmethod
def to_glyphs(self):
pass
Expand Down Expand Up @@ -171,19 +164,6 @@ def __init__(self, glyphs_name, ufo_name=None,
self.value_to_ufo = value_to_ufo
self.value_to_glyphs = value_to_glyphs

def glyphs_names(self):
"""Return the list of names that are handled
from the customParameters.
"""
# Just in case one handler covers several names
if self.glyphs_long_name:
return (self.glyphs_name, self.glyphs_long_name)
return (self.glyphs_name,)

def ufo_names(self):
"""Return the list of names that are handled from the lib.plist."""
return (self.ufo_name,)

# By default, the parameter is read from/written to:
# - the Glyphs object's customParameters
# - the UFO's info object if it has a matching attribute, else the lib
Expand Down Expand Up @@ -247,14 +227,11 @@ def _write_to_ufo(self, glyphs, ufo, value):


KNOWN_PARAM_HANDLERS = []
KNOWN_PARAM_GLYPHS_NAMES = set()
KNOWN_PARAM_UFO_NAMES = set()


def register(handler):
KNOWN_PARAM_HANDLERS.append(handler)
KNOWN_PARAM_GLYPHS_NAMES.update(handler.glyphs_names())
KNOWN_PARAM_UFO_NAMES.update(handler.ufo_names())


GLYPHS_UFO_CUSTOM_PARAMS = (
('hheaAscender', 'openTypeHheaAscender'),
Expand Down Expand Up @@ -358,6 +335,7 @@ def to_glyphs_gasp_table(value):


class MiscParamHandler(ParamHandler):
"""Copy GSFont attributes to ufo lib"""
def _read_from_glyphs(self, glyphs):
return glyphs.get_attribute_value(self.glyphs_name)

Expand All @@ -367,6 +345,7 @@ def _write_to_glyphs(self, glyphs, value):

register(MiscParamHandler(glyphs_name='DisplayStrings'))
register(MiscParamHandler(glyphs_name='disablesAutomaticAlignment'))
register(MiscParamHandler(glyphs_name='iconName'))

# deal with any Glyphs naming quirks here
register(MiscParamHandler(
Expand All @@ -390,12 +369,6 @@ class OS2SelectionParamHandler(AbstractParamHandler):
('Use Typo Metrics', 7),
)

def glyphs_names(self):
return [flag[0] for flag in self.flags]

def ufo_names(self):
return ('openTypeOS2Selection',)

def to_glyphs(self, glyphs, ufo):
ufo_flags = ufo.get_info_value('openTypeOS2Selection')
if ufo_flags is None:
Expand Down Expand Up @@ -451,50 +424,44 @@ def to_ufo(self, glyphs, ufo):
register(FilterParamHandler())


def to_ufo_custom_params(self, ufo, master):
class ReplaceFeatureParamHandler(AbstractParamHandler):
def to_ufo(self, glyphs, ufo):
for value in glyphs.get_custom_values('Replace Feature'):
tag, repl = re.split("\s*;\s*", value, 1)
ufo._owner.features.text = replace_feature(
tag, repl, ufo._owner.features.text or "")

def to_glyphs(self, glyphs, ufo):
# TODO
pass

register(ReplaceFeatureParamHandler())


def to_ufo_custom_params(self, ufo, glyphs_object):
# glyphs_module=None because we shouldn't instanciate any Glyphs classes
font_proxy = GlyphsObjectProxy(self.font, glyphs_module=None)
master_proxy = GlyphsObjectProxy(master, glyphs_module=None)
glyphs_proxy = GlyphsObjectProxy(glyphs_object, glyphs_module=None)
ufo_proxy = UFOProxy(ufo)

for handler in KNOWN_PARAM_HANDLERS:
handler.to_ufo(font_proxy, ufo_proxy)
handler.to_ufo(master_proxy, ufo_proxy)
handler.to_ufo(glyphs_proxy, ufo_proxy)

for param in font_proxy.unhandled_custom_parameters():
for param in glyphs_proxy.unhandled_custom_parameters():
name = _normalize_custom_param_name(param.name)
ufo.lib[CUSTOM_PARAM_PREFIX + font_proxy.sub_key + name] = param.value
for param in master_proxy.unhandled_custom_parameters():
name = _normalize_custom_param_name(param.name)
ufo.lib[CUSTOM_PARAM_PREFIX + master_proxy.sub_key + name] = param.value
ufo.lib[CUSTOM_PARAM_PREFIX + glyphs_proxy.sub_key + name] = param.value

_set_default_params(ufo)


def to_glyphs_family_custom_params(self, ufo):
font_proxy = GlyphsObjectProxy(self.font, glyphs_module=self.glyphs_module)
def to_glyphs_custom_params(self, ufo, glyphs_object):
glyphs_proxy = GlyphsObjectProxy(glyphs_object,
glyphs_module=self.glyphs_module)
ufo_proxy = UFOProxy(ufo)
# Handle known parameters
for handler in KNOWN_PARAM_HANDLERS:
handler.to_glyphs(font_proxy, ufo_proxy)

_to_glyphs_unknown_parameters(font_proxy, ufo_proxy)

_unset_default_params(self.font)


def to_glyphs_master_custom_params(self, ufo, master):
master_proxy = GlyphsObjectProxy(master, glyphs_module=self.glyphs_module)
ufo_proxy = UFOProxy(ufo)
# Handle known parameters
for handler in KNOWN_PARAM_HANDLERS:
handler.to_glyphs(master_proxy, ufo_proxy)
handler.to_glyphs(glyphs_proxy, ufo_proxy)

_to_glyphs_unknown_parameters(master_proxy, ufo_proxy)

_unset_default_params(master)


def _to_glyphs_unknown_parameters(glyphs_proxy, ufo_proxy):
# TODO: (jany) Make sure that all parameters of the UFO info have a handler
# That way, only lib can have extra stuff
prefix = CUSTOM_PARAM_PREFIX + glyphs_proxy.sub_key
Expand All @@ -505,6 +472,8 @@ def _to_glyphs_unknown_parameters(glyphs_proxy, ufo_proxy):
name = name[len(prefix):]
glyphs_proxy.set_custom_value(name, value)

_unset_default_params(glyphs_object)


def _normalize_custom_param_name(name):
"""Replace curved quotes with straight quotes in a custom parameter name.
Expand Down
5 changes: 3 additions & 2 deletions Lib/glyphsLib/builder/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ def to_ufo_font_attributes(self, family_name):
self.to_ufo_family_user_data(ufo)
self.to_ufo_master_user_data(ufo, master)
self.to_ufo_guidelines(ufo, master)
self.to_ufo_custom_params(ufo, font)
self.to_ufo_custom_params(ufo, master)

master_id = master.id
Expand Down Expand Up @@ -175,7 +176,7 @@ def _set_glyphs_font_attributes(self, ufo):

self.to_glyphs_family_names(ufo)
self.to_glyphs_family_user_data(ufo)
self.to_glyphs_family_custom_params(ufo)
self.to_glyphs_custom_params(ufo, font)
self.to_glyphs_features(ufo)


Expand Down Expand Up @@ -227,7 +228,7 @@ def _set_glyphs_master_attributes(self, ufo, master):
self.to_glyphs_master_names(ufo, master)
self.to_glyphs_master_user_data(ufo, master)
self.to_glyphs_guidelines(ufo, master)
self.to_glyphs_master_custom_params(ufo, master)
self.to_glyphs_custom_params(ufo, master)


def to_glyphs_ordered_masters(self):
Expand Down
1 change: 1 addition & 0 deletions Lib/glyphsLib/builder/interpolation.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ def apply_instance_data(instance_data):
set_weight_class(ufo, data)
set_width_class(ufo, data)
self = UFOBuilder(instance_data, defcon)
# to_ufo_custom_params(self, ufo, data.parent) # FIXME: (jany) needed?
to_ufo_custom_params(self, ufo, data)
ufo.save()
instance_ufos.append(ufo)
Expand Down
7 changes: 5 additions & 2 deletions tests/builder_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ def setUp(self):
self.builder = UFOBuilder(self.font)

def set_custom_params(self):
self.builder.to_ufo_custom_params(self.ufo, self.font)
self.builder.to_ufo_custom_params(self.ufo, self.master)

def test_normalizes_curved_quotes_in_names(self):
Expand Down Expand Up @@ -442,7 +443,8 @@ def test_replace_feature(self):

repl = "liga; sub f f by ff;"

set_custom_params(self.ufo, parsed=[("Replace Feature", repl)])
self.master.customParameters["Replace Feature"] = repl
self.set_custom_params()

self.assertEqual(self.ufo.features.text, dedent("""
feature liga {
Expand All @@ -462,7 +464,8 @@ def test_replace_feature(self):
original = self.ufo.features.text
repl = "numr; sub one by one.numr;\nsub two by two.numr;\n"

set_custom_params(self.ufo, parsed=[("Replace Feature", repl)])
self.master.customParameters["Replace Feature"] = repl
self.set_custom_params()

self.assertEqual(self.ufo.features.text, original)

Expand Down

0 comments on commit a43f1d8

Please sign in to comment.