# Path: Lib/glyphsLib/builder/masters.py
# UFO_FILENAME_KEY = GLYPHLIB_PREFIX + "ufoFilename"
# 
# Path: Lib/glyphsLib/builder/axes.py
# def get_axis_definitions(font):
#     axesParameter = font.customParameters["Axes"]
#     if axesParameter is None:
#         return DEFAULT_AXES_DEFS
# 
#     factory = AxisDefinitionFactory()
#     return [factory.get(axis.get("Tag"), axis["Name"]) for axis in axesParameter]
# 
# def get_regular_master(font):
#     """Find the "regular" master among the GSFontMasters.
# 
#     Tries to find the master with the passed 'regularName'.
#     If there is no such master or if regularName is None,
#     tries to find a base style shared between all masters
#     (defaulting to "Regular"), and then tries to find a master
#     with that style name. If there is no master with that name,
#     returns the first master in the list.
#     """
#     if not font.masters:
#         return None
#     # The current glyphs source specification supports the custom
#     # parameter name "Variable Font Origin".  This may have been
#     # named "Variation Font Origin" in the past.
#     # We support the current name with a fallback to the previous name
#     # if not found in the GSFont.customParameters dict
#     if "Variable Font Origin" in font.customParameters:
#         regular_id = font.customParameters["Variable Font Origin"]
#         if regular_id:
#             for master in font.masters:
#                 if master.id == regular_id:
#                     return master
#     elif "Variation Font Origin" in font.customParameters:
#         regular_name = font.customParameters["Variation Font Origin"]
#         if regular_name:
#             for master in font.masters:
#                 if master.name == regular_name:
#                     return master
#     base_style = find_base_style(font.masters)
#     if not base_style:
#         base_style = "Regular"
#     for master in font.masters:
#         if master.name == base_style:
#             return master
#     # Second try: maybe the base style has regular in it as well
#     for master in font.masters:
#         name_without_regular = " ".join(
#             n for n in master.name.split(" ") if n != "Regular"
#         )
#         if name_without_regular == base_style:
#             return master
#     return font.masters[0]
# 
# def font_uses_axis_locations(font):
#     # It's possible for fonts to have the 'Axes' parameter but to NOT specify
#     # the master locations using 'Axis Location', in which case we have to
#     # resort to using instances or other old tricks to get the mapping.
#     # https://github.com/googlefonts/glyphsLib/issues/409
#     # https://github.com/googlefonts/glyphsLib/issues/411
#     return font.axes and all(
#         master.customParameters["Axis Location"] for master in font.masters
#     )
# 
# def interp(mapping, x):
#     """Compute the piecewise linear interpolation given by mapping for input x.
# 
#     >>> interp(((1, 1), (2, 4)), 1.5)
#     2.5
#     """
#     mapping = sorted(mapping)
#     if len(mapping) == 1:
#         xa, ya = mapping[0]
#         if xa == x:
#             return ya
#         return x
#     for (xa, ya), (xb, yb) in zip(mapping[:-1], mapping[1:]):
#         if xa <= x <= xb:
#             return ya + float(x - xa) / (xb - xa) * (yb - ya)
#     return x
# 
# Path: Lib/glyphsLib/builder/constants.py
# UFO_FILENAME_CUSTOM_PARAM = "UFO Filename"


import collections
import logging
import os
import fontTools.designspaceLib
from glyphsLib.util import build_ufo_path
from .masters import UFO_FILENAME_KEY
from .axes import (
    get_axis_definitions,
    get_regular_master,
    font_uses_axis_locations,
    interp,
)
from .constants import UFO_FILENAME_CUSTOM_PARAM
                " Make sure that you used consistent 'brace layer' names"
                " in all the glyph layers that share the same location."
            )
        for loc, sources in sorted(duplicates.items()):
            msg.append(
                "\n  %r => %r"
                % ([s.layerName if s.layerName else s.name for s in sources], dict(loc))
            )
        logger.warning("".join(msg))


def _to_designspace_source(self, master, is_regular):
    source = self._sources[master.id]
    ufo = source.font

    if is_regular:
        source.copyLib = True
        source.copyInfo = True
        source.copyGroups = True
        source.copyFeatures = True

    source.familyName = ufo.info.familyName
    source.styleName = ufo.info.styleName
    # TODO: recover original source name from userData
    # UFO_SOURCE_NAME_KEY
    source.name = f"{source.familyName} {source.styleName}"

    if UFO_FILENAME_CUSTOM_PARAM in master.customParameters:
        source.filename = master.customParameters[UFO_FILENAME_CUSTOM_PARAM]
    elif UFO_FILENAME_KEY in master.userData:

# Path: Lib/glyphsLib/builder/masters.py
# UFO_FILENAME_KEY = GLYPHLIB_PREFIX + "ufoFilename"
# 
# Path: Lib/glyphsLib/builder/axes.py
# def get_axis_definitions(font):
#     axesParameter = font.customParameters["Axes"]
#     if axesParameter is None:
#         return DEFAULT_AXES_DEFS
# 
#     factory = AxisDefinitionFactory()
#     return [factory.get(axis.get("Tag"), axis["Name"]) for axis in axesParameter]
# 
# def get_regular_master(font):
#     """Find the "regular" master among the GSFontMasters.
# 
#     Tries to find the master with the passed 'regularName'.
#     If there is no such master or if regularName is None,
#     tries to find a base style shared between all masters
#     (defaulting to "Regular"), and then tries to find a master
#     with that style name. If there is no master with that name,
#     returns the first master in the list.
#     """
#     if not font.masters:
#         return None
#     # The current glyphs source specification supports the custom
#     # parameter name "Variable Font Origin".  This may have been
#     # named "Variation Font Origin" in the past.
#     # We support the current name with a fallback to the previous name
#     # if not found in the GSFont.customParameters dict
#     if "Variable Font Origin" in font.customParameters:
#         regular_id = font.customParameters["Variable Font Origin"]
#         if regular_id:
#             for master in font.masters:
#                 if master.id == regular_id:
#                     return master
#     elif "Variation Font Origin" in font.customParameters:
#         regular_name = font.customParameters["Variation Font Origin"]
#         if regular_name:
#             for master in font.masters:
#                 if master.name == regular_name:
#                     return master
#     base_style = find_base_style(font.masters)
#     if not base_style:
#         base_style = "Regular"
#     for master in font.masters:
#         if master.name == base_style:
#             return master
#     # Second try: maybe the base style has regular in it as well
#     for master in font.masters:
#         name_without_regular = " ".join(
#             n for n in master.name.split(" ") if n != "Regular"
#         )
#         if name_without_regular == base_style:
#             return master
#     return font.masters[0]
# 
# def font_uses_axis_locations(font):
#     # It's possible for fonts to have the 'Axes' parameter but to NOT specify
#     # the master locations using 'Axis Location', in which case we have to
#     # resort to using instances or other old tricks to get the mapping.
#     # https://github.com/googlefonts/glyphsLib/issues/409
#     # https://github.com/googlefonts/glyphsLib/issues/411
#     return font.axes and all(
#         master.customParameters["Axis Location"] for master in font.masters
#     )
# 
# def interp(mapping, x):
#     """Compute the piecewise linear interpolation given by mapping for input x.
# 
#     >>> interp(((1, 1), (2, 4)), 1.5)
#     2.5
#     """
#     mapping = sorted(mapping)
#     if len(mapping) == 1:
#         xa, ya = mapping[0]
#         if xa == x:
#             return ya
#         return x
#     for (xa, ya), (xb, yb) in zip(mapping[:-1], mapping[1:]):
#         if xa <= x <= xb:
#             return ya + float(x - xa) / (xb - xa) * (yb - ya)
#     return x
# 
# Path: Lib/glyphsLib/builder/constants.py
# UFO_FILENAME_CUSTOM_PARAM = "UFO Filename"


# Path: Lib/glyphsLib/builder/sources.py
import collections
import logging
import os
import fontTools.designspaceLib
from glyphsLib.util import build_ufo_path
from .masters import UFO_FILENAME_KEY
from .axes import (
    get_axis_definitions,
    get_regular_master,
    font_uses_axis_locations,
    interp,
)
from .constants import UFO_FILENAME_CUSTOM_PARAM

                " Make sure that you used consistent 'brace layer' names"
                " in all the glyph layers that share the same location."
            )
        for loc, sources in sorted(duplicates.items()):
            msg.append(
                "\n  %r => %r"
                % ([s.layerName if s.layerName else s.name for s in sources], dict(loc))
            )
        logger.warning("".join(msg))


def _to_designspace_source(self, master, is_regular):
    source = self._sources[master.id]
    ufo = source.font

    if is_regular:
        source.copyLib = True
        source.copyInfo = True
        source.copyGroups = True
        source.copyFeatures = True

    source.familyName = ufo.info.familyName
    source.styleName = ufo.info.styleName
    # TODO: recover original source name from userData
    # UFO_SOURCE_NAME_KEY
    source.name = f"{source.familyName} {source.styleName}"

    if UFO_FILENAME_CUSTOM_PARAM in master.customParameters:
        source.filename = master.customParameters[UFO_FILENAME_CUSTOM_PARAM]
    elif UFO_FILENAME_KEY in master.userData:

        source.filename = master.userData[UFO_FILENAME_KEY]