Example #1
0
    def write(self, glyphSet: GlyphSet, saveAs: bool = True) -> None:
        """Write Layer to a :class:`fontTools.ufoLib.glifLib.GlyphSet`.

        Args:
            glyphSet: The GlyphSet object to write to.
            saveAs: If True, tells the writer to save out-of-place. If False, tells the
                writer to save in-place. This affects how resources are cleaned before
                writing.
        """
        glyphs = self._glyphs
        if not saveAs:
            for name in set(glyphSet.contents).difference(glyphs):
                glyphSet.deleteGlyph(name)
        for name, glyph in glyphs.items():
            if glyph is _GLYPH_NOT_LOADED:
                if saveAs:
                    glyph = self.loadGlyph(name)
                else:
                    continue
            _prune_object_libs(glyph.lib, _fetch_glyph_identifiers(glyph))
            glyphSet.writeGlyph(name,
                                glyphObject=glyph,
                                drawPointsFunc=glyph.drawPoints)
        glyphSet.writeContents()
        glyphSet.writeLayerInfo(self)
        if saveAs:
            # all glyphs are loaded by now, no need to keep ref to glyphSet
            self._glyphSet = None
Example #2
0
    def exportButtonCallback(self, sender):

        f = CurrentFont()

        if f is None:
            return

        if self.verbose:
            print('exporting selected glyphs back to their sources...\n')

        for glyphName in f.selectedGlyphNames:

            if self.importMode == 1:

                glyph = f[glyphName].getLayer('foreground')

                if self.glyphSetPathKey not in glyph.lib:
                    continue

                glyphsFolder = glyph.lib[self.glyphSetPathKey]
                ufoName = splitall(glyphsFolder)[-2]
                glyphNameExtension = os.path.splitext(ufoName)[0]
                glyphNameParts = glyphName.split('.')

                if not (len(glyphNameParts) > 1 and glyphNameParts[-1] == os.path.splitext(ufoName)[0]):
                    print(f'{glyphName} does not have the expected glyph name extension, skipping...')
                    continue

                if self.verbose:
                    print(f'\twriting {glyphName} to {ufoName}...')

                outputGlyphName = '.'.join(glyphNameParts[:-1])
                glyphSet = GlyphSet(glyphsFolder, validateWrite=True)
                glyphSet.writeGlyph(outputGlyphName, glyph.naked(), glyph.drawPoints)
                glyphSet.writeContents()

            else:

                for layerName in f.layerOrder:
                    glyph = f[glyphName].getLayer(layerName)

                    if self.glyphSetPathKey not in glyph.lib:
                        continue

                    glyphsFolder = glyph.lib[self.glyphSetPathKey]

                    # mode 0
                    if not '.ufoz' in glyphsFolder:
                        glyphSet = GlyphSet(glyphsFolder, validateWrite=True)
                        ufoName = splitall(glyphsFolder)[-2]
                        if self.verbose:
                            print(f'\twriting {glyphName} to {ufoName}...')
                        glyphSet.writeGlyph(glyphName, glyph.naked(), glyph.drawPoints)
                        glyphSet.writeContents()

                    # mode 2
                    else:
                        ufoPath = os.path.dirname(glyphsFolder)
                        ufoName = splitall(ufoPath)[-1]
                        dstFont = OpenFont(ufoPath, showInterface=False)
                        if self.verbose:
                            print(f'\twriting {glyphName} to {ufoName}...')
                        dstFont.insertGlyph(glyph, name=glyph.name)
                        dstFont.save()
                        dstFont.close()

        if self.verbose:
            print()
            print('...done.\n')