Esempio n. 1
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
def run_all_glif_validations(ufoobj):
    glyphsdir_path_list = ufoobj.get_glyphsdir_path_list()
    ufoversion = ufoobj.get_ufo_version()
    ss = StdStreamer(ufoobj.ufopath)
    test_error_list = []
    for (
        glyphsdir
    ) in glyphsdir_path_list:  # for each directory that containts .glif files
        print(" ")
        sys.stdout.write(" - " + glyphsdir + "  ")
        sys.stdout.flush()
        res = Result(glyphsdir)
        try:
            gs = GlyphSet(
                glyphsdir, ufoFormatVersion=ufoversion, validateRead=True
            )  # create a ufoLib GlyphSet
            # do not report success for this, previous testing has passed this
        except Exception as e:
            res.test_failed = True
            res.test_long_stdstream_string = (
                " Failed to read glif file paths from "
                + glyphsdir
                + ". Error: "
                + str(e)
            )
            ss.stream_result(res)
            test_error_list.append(res)
            break  # break out loop as it was not possible to read the GlyphSet for this directory, gs not instantiated

        glif_count = 0  # reset glyphs directory .glif file counter
        for (
            glyphname
        ) in (
            gs.contents.keys()
        ):  # for each .glif file (read from glyph name in glyph set contents dict)
            res = Result(gs.contents[glyphname])
            try:
                go = GlifObj()
                gs.readGlyph(
                    glyphname, glyphObject=go
                )  # read the glif file and perform ufoLib validations, requires the glyphObject for validations
                res.test_failed = False
                ss.stream_result(res)
                glif_count += 1
            except Exception as e:
                res.test_failed = True
                filename = os.path.join(glyphsdir, glyphNameToFileName(glyphname, None))
                res.test_long_stdstream_string = '{} (glyph "{}"): Test failed with error: {}'.format(
                    filename, glyphname, e
                )
                ss.stream_result(res)
                test_error_list.append(res)
                glif_count += 1
        print("   " + str(glif_count) + " .glif tests completed")
    return test_error_list
Esempio n. 3
0
# this path must be to the `glyphs` subdirectory of the root UFO directory
GLYPH_DIR = "../ufo_source/GenericSans-GreenHighlight.ufo/glyphs"


# dummy object that is used to set glyph-specific attributes
# by fontTools.ufoLib.glifLib.GlyphObject.readGlyph() method
class GlyphObj(object):
    def __init__(self):
        pass


gs = GlyphSet(GLYPH_DIR)
for glif in gs.contents:
    go = GlyphObj()
    gs.readGlyph(glif, glyphObject=go)

    # define useful glyph data fields through read of the glyph object
    # Includes:
    # - name
    # - unicode(s)
    # - glyph highlight color (defined by designer in font editor)
    #      - defined in RGBA format
    #      - defined in sRGB color space (https://en.wikipedia.org/wiki/SRGB)
    #      - returns comma-separated string of these four integer or float values in set [0.0,1.0]
    #      - defined as None if no highlighting used on glyph
    # - last write time/date
    # - note (glyph-specific free text entered by designer in editor)
    #      - Unicode Python str
    #      - defined as None if no free text note is defined for the glyph
    glyph_name = go.name