예제 #1
0
    def insertGlyph(
        self,
        glyph: Glyph,
        name: str | None = None,
        overwrite: bool = True,
        copy: bool = True,
    ) -> None:
        """Inserts Glyph object into this layer.

        Args:
            glyph: The Glyph object.
            name: The name of the glyph.
            overwrite: If True, overwrites (read: deletes) glyph with the same name if
                it exists. If False, raises KeyError.
            copy: If True, copies the Glyph object before insertion. If False, inserts
                as is.
        """
        if copy:
            glyph = glyph.copy()
        if name is not None:
            glyph._name = name
        if glyph.name is None:
            raise ValueError(f"{glyph!r} has no name; can't add it to Layer")
        if not overwrite and glyph.name in self._glyphs:
            raise KeyError(f"glyph named '{glyph.name}' already exists")
        self._glyphs[glyph.name] = glyph
예제 #2
0
 def __setitem__(self, name: str, glyph: Glyph) -> None:
     if not isinstance(glyph, Glyph):
         raise TypeError(f"Expected Glyph, found {type(glyph).__name__}")
     glyph._name = name
     self._glyphs[name] = glyph