Ejemplo n.º 1
0
 def __init__(self, family_name: str, unit: Type[Unit]):
     """
     Args:
         family_name: The font name
         unit: A sizing unit, where `unit(1)` is the distance
             between two staff lines.
     """
     self._unit = unit
     try:
         self.metadata = neoscore.registered_music_fonts[family_name]
     except KeyError:
         raise MusicFontMetadataNotFoundError
     self._engraving_defaults = copy.deepcopy(
         self.metadata["engravingDefaults"])
     self._em_size = self.unit(self.__magic_em_scale)
     self._glyph_info_cache = {}
     # engraving_defaults is small, so eagerly converting it to self.unit is ok
     convert_all_to_unit(self._engraving_defaults, self.unit)
     super().__init__(family_name, self._em_size, 1, False)
Ejemplo n.º 2
0
def test_convert_all_to_unit_raises_error_on_bad_input():
    with pytest.raises(TypeError):
        convert_all_to_unit("invalid argument type", GraphicUnit)
Ejemplo n.º 3
0
def test_convert_all_to_unit_handles_strings_correctly():
    iterable = {"a": 5, "b": ["abcd", 2]}
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable["b"][1], GraphicUnit)
    assert iterable["b"][1] == GraphicUnit(2)
    assert iterable["b"][0] == "abcd"
Ejemplo n.º 4
0
def test_convert_all_to_unit_dict_in_dict():
    iterable = {"a": 5, "b": {6: 7}}
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable["b"], dict)
    assert isinstance(iterable["b"][6], GraphicUnit)
    assert iterable["b"][6] == GraphicUnit(7)
Ejemplo n.º 5
0
def test_convert_all_to_unit_simple_dict():
    iterable = {"a": 1, "b": GraphicUnit(2), "c": "h"}
    convert_all_to_unit(iterable, GraphicUnit)
    assert iterable["a"] == GraphicUnit(1)
Ejemplo n.º 6
0
def test_convert_all_to_unit_tuple_in_dict():
    iterable = {"a": 5, "b": (6, 7)}
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable["b"], tuple)
    assert isinstance(iterable["b"][0], GraphicUnit)
    assert iterable["b"][0] == GraphicUnit(6)
Ejemplo n.º 7
0
def test_convert_all_to_unit_list_in_dict():
    iterable = {"a": 5, "b": [6, 7]}
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable["b"], list)
    assert isinstance(iterable["b"][0], GraphicUnit)
    assert iterable["b"][0] == GraphicUnit(6)
Ejemplo n.º 8
0
def test_convert_all_to_unit_dict_in_list():
    iterable = [5, 6, 2, {"b": 3}]
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable[3], dict)
    assert isinstance(iterable[3]["b"], GraphicUnit)
    assert iterable[3]["b"] == GraphicUnit(3)
Ejemplo n.º 9
0
def test_convert_all_to_unit_tuple_in_list():
    iterable = [(5, 6), 2, 3]
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable[0][0], GraphicUnit)
    assert isinstance(iterable[0], tuple)
    assert iterable[0][0] == GraphicUnit(5)
Ejemplo n.º 10
0
def test_convert_all_to_unit_list_in_list():
    iterable = [5, 6, [7, 8]]
    convert_all_to_unit(iterable, GraphicUnit)
    assert isinstance(iterable[2], list)
    assert isinstance(iterable[2][0], GraphicUnit)
    assert iterable[2][0] == GraphicUnit(7)
Ejemplo n.º 11
0
def test_convert_all_to_unit_simple_list():
    iterable = [1, GraphicUnit(2), "b"]
    convert_all_to_unit(iterable, GraphicUnit)
    assert iterable[0] == GraphicUnit(1)
Ejemplo n.º 12
0
    def _glyph_info(self,
                    glyph_name: str,
                    alternate_number: Optional[int] = None) -> Dict:
        """Collect and return all known metadata about a glyph.

        Args:
            glyph_name (str): The canonical name of the glyph
            alternate_number (int or None): A glyph alternate number

        Returns:
            None: If the glyph is not available in the font
            dict: A collection of all known metadata about the glyph

        Raises:
            MusicFontGlyphNotFoundError: If the requested glyph
                could not be found in the font.
        """
        info = {}
        if alternate_number:
            try:
                alternate = self.metadata["glyphsWithAlternates"][glyph_name][
                    "alternates"][alternate_number - 1]
                info["codepoint"] = alternate["codepoint"]
                real_name = alternate["name"]
            except KeyError:
                # Alternate not found in the font
                raise MusicFontGlyphNotFoundError
        else:
            try:
                info["codepoint"] = smufl.glyph_names[glyph_name]["codepoint"]
            except KeyError:
                raise MusicFontGlyphNotFoundError
            real_name = glyph_name

        try:
            info["description"] = smufl.glyph_names[real_name]["description"]
        except KeyError:
            pass
        try:
            info["classes"] = smufl.get_glyph_classes(real_name)
        except KeyError:
            pass
        try:
            info["glyphBBox"] = self.metadata["glyphBBoxes"][real_name]
        except KeyError:
            pass
        try:
            info["alternates"] = self.metadata["glyphsWithAlternates"][
                real_name]["alternates"]
        except KeyError:
            pass
        try:
            info["anchors"] = self.metadata["glyphsWithAnchors"][real_name]
        except KeyError:
            pass
        try:
            info["componentGlyphs"] = self.metadata["ligatures"][real_name][
                "componentGlyphs"]
        except KeyError:
            pass
        for set_key in self.metadata["sets"].keys():
            for glyph in self.metadata["sets"][set_key]["glyphs"]:
                if glyph["alternateFor"] == real_name:
                    info["setAlternatives"] = {}
                    info["setAlternatives"][set_key] = {}
                    info["setAlternatives"][set_key][
                        "description"] = self.metadata["sets"][set_key][
                            "description"]
                    info["setAlternatives"][set_key]["name"] = glyph["name"]
                    info["setAlternatives"][set_key]["codepoint"] = glyph[
                        "codepoint"]
        if not info:
            raise MusicFontGlyphNotFoundError
        info["is_optional"] = real_name in self.metadata["optionalGlyphs"]
        info["canonicalName"] = real_name
        convert_all_to_unit(info, self.unit)
        return info