GLYPHS_PREFIX = "com.schriftgestaltung." GLYPHLIB_PREFIX = GLYPHS_PREFIX + "Glyphs." UFO_FILENAME_CUSTOM_PARAM = "UFO Filename" def build_stylemap_names(
    family_name, style_name, is_bold=False, is_italic=False, linked_style=None
):
    """Build UFO `styleMapFamilyName` and `styleMapStyleName` based on the
    family and style names, and the entries in the "Style Linking" section
    of the "Instances" tab in the "Font Info".

    The value of `styleMapStyleName` can be either "regular", "bold", "italic"
    or "bold italic", depending on the values of `is_bold` and `is_italic`.

    The `styleMapFamilyName` is a combination of the `family_name` and the
    `linked_style`.

    If `linked_style` is unset or set to 'Regular', the linked style is equal
    to the style_name with the last occurrences of the strings 'Regular',
    'Bold' and 'Italic' stripped from it.
    """

    styleMapStyleName = (
        " ".join(
            s for s in ("bold" if is_bold else "", "italic" if is_italic else "") if s
        )
        or "regular"
    )
    if not linked_style or linked_style == "Regular":
        linked_style = _get_linked_style(style_name, is_bold, is_italic)
    if linked_style:
        styleMapFamilyName = (family_name or "") + " " + linked_style
    else:
        styleMapFamilyName = family_name
    return styleMapFamilyName, styleMapStyleName 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 is_instance_active(instance):
    # Glyphs.app recognizes both "exports=0" and "active=0" as a flag
    # to mark instances as inactive. Inactive instances should get ignored.
    # https://github.com/googlefonts/glyphsLib/issues/129
    return instance.exports and getattr(instance, "active", True) 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 WEIGHT_AXIS_DEF = defaults_factory.get("wght", "Weight") WIDTH_AXIS_DEF = defaults_factory.get("wdth", "Width") class AxisDefinitionFactory:
    """Creates a set of axis definitions, making sure to recognize default axes
    (weight and width) and also keeping track of indices of custom axes.

    From looking at a Glyphs file with only one custom axis, it looks like
    when there is an "Axes" customParameter, the axis design locations are
    stored in `weightValue` for the first axis (regardless of whether it is
    a weight axis, `widthValue` for the second axis, etc.
    """

    def __init__(self):
        self.axis_index = -1

    def get(self, tag=None, name="Custom"):
        self.axis_index += 1
        design_loc_key = self._design_loc_key()
        if tag is None:
            if self.axis_index == 0:
                tag = "XXXX"
            else:
                tag = "XXX%d" % self.axis_index

        if tag == "wght":
            return AxisDefinition(
                tag, name, design_loc_key, 100.0, "weight", "weightClass", 400.0
            )
        if tag == "wdth":
            return AxisDefinition(
                tag, name, design_loc_key, 100.0, "width", "widthClass", 100.0
            )
        return AxisDefinition(tag, name, design_loc_key, 0.0, None, None, 0.0)

    def _design_loc_key(self):
        return self.axis_index def to_ufo_custom_params(self, ufo, glyphs_object):
    # glyphs_module=None because we shouldn't instanciate any Glyphs classes
    glyphs_proxy = GlyphsObjectProxy(glyphs_object, glyphs_module=None)
    ufo_proxy = UFOProxy(ufo)

    glyphs_proxy.mark_handled(UFO_FILENAME_CUSTOM_PARAM)

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

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

    _set_default_params(ufo)
import os
import logging
        import ufoLib2
from glyphsLib.util import build_ufo_path
from glyphsLib.classes import WEIGHT_CODES, GSCustomParameter
from .constants import GLYPHS_PREFIX, GLYPHLIB_PREFIX, UFO_FILENAME_CUSTOM_PARAM
from .names import build_stylemap_names
from .axes import (
    get_axis_definitions,
    is_instance_active,
    interp,
    WEIGHT_AXIS_DEF,
    WIDTH_AXIS_DEF,
    AxisDefinitionFactory,
)
from .custom_params import to_ufo_custom_params
    from fontTools.designspaceLib import DesignSpaceDocument
    from os.path import normcase, normpath
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.




EXPORT_KEY = GLYPHS_PREFIX + "export"
WIDTH_KEY = GLYPHS_PREFIX + "width"
WEIGHT_KEY = GLYPHS_PREFIX + "weight"

FULL_FILENAME_KEY = GLYPHLIB_PREFIX + "fullFilename"