Exemplo n.º 1
0
 def loadGlyph(self, name: str) -> Glyph:
     """Load and return Glyph object."""
     # XXX: Remove and let __getitem__ do it?
     glyph = Glyph(name)
     self._glyphSet.readGlyph(name, glyph, glyph.getPointPen())
     self._glyphs[name] = glyph
     return glyph
Exemplo n.º 2
0
    def read(cls,
             name: str,
             glyphSet: GlyphSet,
             lazy: bool = True,
             default: bool = False) -> Layer:
        """Instantiates a Layer object from a
        :class:`fontTools.ufoLib.glifLib.GlyphSet`.

        Args:
            name: The name of the layer.
            glyphSet: The GlyphSet object to read from.
            lazy: If True, load glyphs as they are accessed. If False, load everything
                up front.
        """
        glyphNames = glyphSet.keys()
        glyphs: dict[str, Glyph]
        if lazy:
            glyphs = {gn: _GLYPH_NOT_LOADED for gn in glyphNames}
        else:
            glyphs = {}
            for glyphName in glyphNames:
                glyph = Glyph(glyphName)
                glyphSet.readGlyph(glyphName, glyph, glyph.getPointPen())
                glyphs[glyphName] = glyph
        self = cls(name, glyphs, default=default)
        if lazy:
            self._glyphSet = glyphSet
        glyphSet.readLayerInfo(self)
        return self
Exemplo n.º 3
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
Exemplo n.º 4
0
 def read(cls, name, glyphSet, lazy=True):
     glyphNames = glyphSet.keys()
     if lazy:
         glyphs = {gn: _NOT_LOADED for gn in glyphNames}
     else:
         glyphs = {}
         for glyphName in glyphNames:
             glyph = Glyph(glyphName)
             glyphSet.readGlyph(glyphName, glyph, glyph.getPointPen())
             glyphs[glyphName] = glyph
     self = cls(name, glyphs)
     if lazy:
         self._glyphSet = glyphSet
     glyphSet.readLayerInfo(self)
     return self
Exemplo n.º 5
0
    def instantiateGlyphObject(self) -> Glyph:
        """Returns a new Glyph instance.

        |defcon_compat|
        """
        return Glyph()
Exemplo n.º 6
0
 def newGlyph(self, name: str) -> Glyph:
     """Creates and returns new Glyph object in this layer with name."""
     if name in self._glyphs:
         raise KeyError(f"glyph named '{name}' already exists")
     self._glyphs[name] = glyph = Glyph(name)
     return glyph
Exemplo n.º 7
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
Exemplo n.º 8
0
from ufoLib2.constants import DEFAULT_LAYER_NAME
from ufoLib2.objects.glyph import Glyph
from ufoLib2.objects.lib import Lib, _convert_Lib, _get_lib, _set_lib
from ufoLib2.objects.misc import (
    BoundingBox,
    _deepcopy_unlazify_attrs,
    _prune_object_libs,
    unionBounds,
)
from ufoLib2.typing import T

if TYPE_CHECKING:
    from cattr import GenConverter

_GLYPH_NOT_LOADED = Glyph(name="___UFOLIB2_LAZY_GLYPH___")


def _convert_glyphs(
        value: dict[str, Glyph] | Sequence[Glyph]) -> dict[str, Glyph]:
    result: dict[str, Glyph] = {}
    if isinstance(value, dict):
        glyph_ids = set()
        for name, glyph in value.items():
            if not isinstance(glyph, Glyph):
                raise TypeError(
                    f"Expected Glyph, found {type(glyph).__name__}")
            if glyph is not _GLYPH_NOT_LOADED:
                glyph_id = id(glyph)
                if glyph_id in glyph_ids:
                    raise KeyError(f"{glyph!r} can't be added twice")
Exemplo n.º 9
0
 def newGlyph(self, name):
     if name in self._glyphs:
         raise KeyError("glyph %r already exists" % name)
     self._glyphs[name] = glyph = Glyph(name)
     return glyph
Exemplo n.º 10
0
 def loadGlyph(self, name):
     glyph = Glyph(name)
     self._glyphSet.readGlyph(name, glyph, glyph.getPointPen())
     self._glyphs[name] = glyph
     return glyph