Skip to content

Commit

Permalink
WIP Fix custom parameter tests (pairing with Georg)
Browse files Browse the repository at this point in the history
  • Loading branch information
belluzj committed Dec 6, 2023
1 parent 18c9fe9 commit 3465032
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 55 deletions.
11 changes: 6 additions & 5 deletions Lib/glyphsLib/builder/custom_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
)
from .features import replace_feature, replace_prefixes
from glyphsLib.classes import GSCustomParameter, GSFont, GSFontMaster, GSInstance
from .instances import InstanceDescriptorAsGSInstance

"""Set Glyphs custom parameters in UFO info or lib, where appropriate.
Expand Down Expand Up @@ -161,7 +162,7 @@ def __init__(
self.write_to_ufo = write_to_ufo
self.write_to_glyphs = write_to_glyphs
self.glyphs_owner_class = glyphs_owner_class

def __repr__(self):
return "<{} {}>".format(self.__class__.__name__, self.glyphs_name)

Expand Down Expand Up @@ -317,7 +318,7 @@ def register_property_handler(handler):
register_parameter_handler(ParamHandler(glyphs_name, ufo_name, glyphs_long_name=ufo_name, glyphs_owner_class=GSFontMaster))

for glyphs_name, ufo_name in GLYPHS_INSTANCE_UFO_CUSTOM_PARAMS:
register_parameter_handler(ParamHandler(glyphs_name, ufo_name, glyphs_long_name=ufo_name, glyphs_owner_class=GSInstance))
register_parameter_handler(ParamHandler(glyphs_name, ufo_name, glyphs_long_name=ufo_name, glyphs_owner_class=(GSInstance, InstanceDescriptorAsGSInstance)))

# Reference:
# https://github.com/googlefonts/glyphsLib/pull/881#issuecomment-1474226616
Expand Down Expand Up @@ -476,7 +477,7 @@ def to_ufo(self, builder, glyphs, ufo):
# FIXME: (georg) This is actually not working as the handlers is not applied to instances, yet. There is custom code
for glyphs_name in ("weightClass", "widthClass"):
ufo_name = "openTypeOS2W" + glyphs_name[1:]
register_parameter_handler(ParamHandler(glyphs_name, ufo_name, value_to_ufo=int, glyphs_owner_class=GSInstance))
register_parameter_handler(ParamHandler(glyphs_name, ufo_name, value_to_ufo=int, glyphs_owner_class=(GSInstance, InstanceDescriptorAsGSInstance)))

# convert Glyphs' GASP Table to UFO openTypeGaspRangeRecords
def to_ufo_gasp_table(value):
Expand Down Expand Up @@ -759,7 +760,7 @@ class OS2SelectionParamHandler(AbstractParamHandler):
glyphs_name = None
ufo_name = "openTypeOS2Selection"
flags = {7: "Use Typo Metrics", 8: "Has WWS Names"}
glyphs_owner_class=(GSFont, GSInstance)
glyphs_owner_class=(GSFont, GSInstance, InstanceDescriptorAsGSInstance)
# Note that en empty openTypeOS2Selection list should stay an empty list, as
# opposed to a non-existant list. In the latter case, we round-trip nothing, in the
# former, we at least write an empty list to openTypeOS2SelectionUnsupportedBits
Expand Down Expand Up @@ -846,7 +847,7 @@ class FilterParamHandler(AbstractParamHandler):
+----+-+ |
| |
+-+-----------+ +-+----------+
|GSFontMaster | |GSInstance |
|GSFontMaster | |GSInstance |
+-------------+ +------------+
userData customParameters
com...ufo2ft.filters Filter & PreFilter
Expand Down
62 changes: 41 additions & 21 deletions Lib/glyphsLib/builder/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def to_ufo_font_attributes(self, family_name):

font = self.font
disableAllAutomaticBehaviour = False
disableAllAutomaticBehaviourParameter = font.customParameters["DisableAllAutomaticBehaviour"]
disableAllAutomaticBehaviourParameter = font.customParameters[
"DisableAllAutomaticBehaviour"
]
if disableAllAutomaticBehaviourParameter:
disableAllAutomaticBehaviour = disableAllAutomaticBehaviourParameter
for index, master in enumerate(font.masters):
Expand All @@ -54,23 +56,44 @@ def to_ufo_font_attributes(self, family_name):
self.to_ufo_names(ufo, master, family_name) # .names
self.to_ufo_family_user_data(ufo) # .user_data

if has_any_corner_components(font, master):
ufo.lib.setdefault(UFO2FT_FILTERS_KEY, []).append(
{
"namespace": "glyphsLib.filters",
"name": "cornerComponents",
"pre": True,
}
)
if not disableAllAutomaticBehaviour:
ufo.lib.setdefault(UFO2FT_FILTERS_KEY, []).append(
{"namespace": "glyphsLib.filters", "name": "eraseOpenCorners", "pre": True}
)
self.to_ufo_properties(ufo, font)
self.to_ufo_custom_params(ufo, font, "font") # .custom_params
self.to_ufo_custom_params(ufo, master, "fontMaster") # .custom_params
self.to_ufo_master_attributes(ufo, master) # .masters

# Extract nested lib keys to the top level
nestedUserData = ufo.lib.get("com.schriftgestaltung.fontMaster.userData", {})
if UFO2FT_FILTERS_KEY not in ufo.lib and UFO2FT_FILTERS_KEY in nestedUserData:
ufo.lib[UFO2FT_FILTERS_KEY] = nestedUserData[UFO2FT_FILTERS_KEY]

del nestedUserData[UFO2FT_FILTERS_KEY]
if not nestedUserData:
del ufo.lib["com.schriftgestaltung.fontMaster.userData"]

if not disableAllAutomaticBehaviour:
if UFO2FT_FILTERS_KEY not in ufo.lib:
ufo.lib[UFO2FT_FILTERS_KEY] = [
{
"namespace": "glyphsLib.filters",
"name": "eraseOpenCorners",
"pre": True,
}
]

if has_any_corner_components(font, master):
filters = ufo.lib.setdefault(UFO2FT_FILTERS_KEY, [])
if not any(
hasattr(f, "get") and f.get("name") == "cornerComponents"
for f in filters
):
filters.append(
{
"namespace": "glyphsLib.filters",
"name": "cornerComponents",
"pre": True,
}
)

ufo.lib[MASTER_ORDER_LIB_KEY] = index

# FIXME: (jany) in the future, yield this UFO (for memory, lazy iter)
Expand All @@ -90,14 +113,14 @@ def to_ufo_font_attributes(self, family_name):
"compatibleFullNames": "openTypeNameCompatibleFullName",
"copyrights": "copyright",
"descriptions": "openTypeNameDescription",
"designers" : "openTypeNameDesigner",
"designers": "openTypeNameDesigner",
"designerURL": "openTypeNameDesignerURL",
#"familyNames": "familyName",
# "familyNames": "familyName",
"preferredFamilyNames": "openTypeNamePreferredFamilyName",
"preferredSubfamilyNames": "openTypeNamePreferredSubfamilyName",
"licenses": "openTypeNameLicense",
"licenseURL": "openTypeNameLicenseURL",
"manufacturers": "openTypeNameManufacturer",
"manufacturers": "openTypeNameManufacturer",
"manufacturerURL": "openTypeNameManufacturerURL",
"postscriptFontName": "postscriptFontName",
"postscriptFullNames": "postscriptFullName",
Expand Down Expand Up @@ -136,8 +159,8 @@ def to_ufo_metadata(master, ufo):
# by a glyphOrder custom parameter below in `to_ufo_custom_params`.
ufo.glyphOrder = list(glyph.name for glyph in font.glyphs)


def to_glyphs_metadata(ufo, font):

for glyphs_key, ufo_key in PROPERTIES_FIELDS.items():
value = getattr(ufo.info, ufo_key)
if value:
Expand Down Expand Up @@ -252,10 +275,7 @@ def _original_master_order(source):
def has_any_corner_components(font, master):
for glyph in font.glyphs:
for layerId, layer in glyph._layers.items():
if (
layer.associatedMasterId != master.id
or not layer.hints
):
if layer.associatedMasterId != master.id or not layer.hints:
continue
if layer.hasCorners:
return True
Expand Down
10 changes: 6 additions & 4 deletions Lib/glyphsLib/builder/instances.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
PROPERTIES_KEY,
)
from .names import build_stylemap_names
from .custom_params import to_ufo_custom_params, to_ufo_properties


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -109,7 +108,7 @@ def _to_designspace_instance(self, instance):

ufo_instance.lib["openTypeOS2WidthClass"] = instance.widthClass
ufo_instance.lib["openTypeOS2WeightClass"] = instance.weightClass

uniqueID = instance.customParameters["uniqueID"]
if uniqueID:
ufo_instance.lib["openTypeNameUniqueID"] = uniqueID
Expand Down Expand Up @@ -171,7 +170,7 @@ def _to_properties(instance):
return [
(item.name, item.value)
for item in instance.properties
if item.name not in CUSTOM_PARAMETERS_BLACKLIST
if item.name not in CUSTOM_PARAMETERS_BLACKLIST
]


Expand Down Expand Up @@ -287,7 +286,7 @@ def to_glyphs_instances(self): # noqa: C901

if ufo_instance.filename and self.minimize_ufo_diffs:
instance.customParameters[UFO_FILENAME_CUSTOM_PARAM] = ufo_instance.filename

# some info that needs to be in a instance in Glyphs is stored in the sources. So we try to find a matching source (FIXME: (georg) not nice
for source in self.designspace.sources:
if source.location == ufo_instance.location:
Expand Down Expand Up @@ -384,6 +383,9 @@ def apply_instance_data_to_ufo(ufo, instance, designspace):
Returns:
None.
"""
# Import here to prevent a cyclic import with custom_params
from .custom_params import to_ufo_custom_params, to_ufo_properties

try:
ufo.info.openTypeOS2WidthClass = instance.lib["openTypeOS2WidthClass"]
except:
Expand Down
Loading

0 comments on commit 3465032

Please sign in to comment.