예제 #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
예제 #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
예제 #3
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
예제 #4
0
    def instantiateGlyphObject(self) -> Glyph:
        """Returns a new Glyph instance.

        |defcon_compat|
        """
        return Glyph()
예제 #5
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
예제 #6
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")
예제 #7
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
예제 #8
0
 def loadGlyph(self, name):
     glyph = Glyph(name)
     self._glyphSet.readGlyph(name, glyph, glyph.getPointPen())
     self._glyphs[name] = glyph
     return glyph