Beispiel #1
0
    def importButtonCallback(self, sender):

        if not len(self.selectedMasters):
            return

        if self.verbose:
            print('importing glyphs from selected sources...\n')

        # mode 0 : fonts → fonts

        if self.importMode == 0:

            for master in self.selectedMasters:
                ufoPath = self._sources[master['name']]
                srcFont = OpenFont(ufoPath, showInterface=False)
                tmpFont = NewFont(familyName=srcFont.info.familyName, styleName=srcFont.info.styleName, showInterface=False)

                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]

                if self.verbose:
                    print(f'\t{ufoName}:')

                for glyphName in self.glyphNames:
                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\t{glyphName} not in font.')
                        continue

                    srcGlyph = srcFont[glyphName]
                    if srcGlyph.components:
                        for component in srcGlyph.components:
                            if not component.baseGlyph in tmpFont:
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpFont[component.baseGlyph] = srcFont[component.baseGlyph]
                                tmpFont[component.baseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                    if self.verbose:
                        print(f'\t\timporting {glyphName}...')
                    tmpFont[glyphName] = srcGlyph
                    tmpFont[glyphName].lib[self.glyphSetPathKey] = glyphsFolder

                tmpFont.openInterface()

                if self.verbose:
                    print()

        # mode 1 : fonts → glyphs
        
        if self.importMode == 1:
            tmpFont = CurrentFont()
            if tmpFont is None:
                tmpFont = NewFont(familyName='tempEdit')

            for i, master in enumerate(self.selectedMasters):
                ufoPath = self._sources[master['name']]
                if not os.path.exists(ufoPath):
                    if self.verbose:
                        print(f'source file does not exist: {ufoPath}')
                    continue
                
                srcFont = OpenFont(ufoPath, showInterface=False)
                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]
                glyphNameExtension = os.path.splitext(ufoName)[0]

                for glyphName in self.glyphNames:

                    tmpGlyphName = f'{glyphName}.{glyphNameExtension}'

                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\tcreating {glyphName}...')
                            tmpFont.newGlyph(tmpGlyphName)

                    else:
                        srcGlyph = srcFont[glyphName]
                        for component in srcGlyph.components:
                            if component.baseGlyph not in tmpFont:
                                if component.baseGlyph not in srcFont:
                                    continue
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpBaseGlyph = f'{component.baseGlyph}.{glyphNameExtension}'
                                tmpFont[tmpBaseGlyph] = srcFont[component.baseGlyph]
                                tmpFont[tmpBaseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                        if self.verbose:
                            print(f'\t\timporting {glyphName}...')

                        tmpFont.newGlyph(tmpGlyphName)
                        tmpFont[tmpGlyphName].appendGlyph(srcGlyph)
                        tmpFont[tmpGlyphName].width = srcGlyph.width

                    tmpFont[tmpGlyphName].lib[self.glyphSetPathKey] = glyphsFolder

                if 'background' not in tmpFont.layerOrder:
                    tmpFont.newLayer('background')

                if self.verbose:
                    print()

        # mode 2 : fonts → layers

        else:
            
            tmpFont = CurrentFont()
            if tmpFont is None:
                tmpFont = NewFont(familyName='tempEdit')

            for i, master in enumerate(self.selectedMasters):
                ufoPath = self._sources[master['name']]
                if not os.path.exists(ufoPath):
                    if self.verbose:
                        print(f'source file does not exist: {ufoPath}')
                    continue
                
                srcFont = OpenFont(ufoPath, showInterface=False)
                glyphsFolder = os.path.join(ufoPath, 'glyphs')
                ufoName = splitall(glyphsFolder)[-2]
                layerName = os.path.splitext(ufoName)[0]
                tmpLayer = tmpFont.newLayer(layerName)

                if self.verbose:
                    print(f'\t{ufoName}:')

                if i == 0:
                    tmpFont.defaultLayer = tmpLayer
                    if 'foreground' in tmpFont.layerOrder:
                        tmpFont.removeLayer('foreground')

                for glyphName in self.glyphNames:

                    if glyphName not in srcFont:
                        if self.verbose:
                            print(f'\t\tcreating {glyphName}...')
                            tmpLayer.newGlyph(glyphName)

                    else:
                        srcGlyph = srcFont[glyphName]
                        for component in srcGlyph.components:
                            if component.baseGlyph not in tmpLayer:
                                if component.baseGlyph not in srcFont:
                                    continue
                                if self.verbose:
                                    print(f'\t\timporting {component.baseGlyph} ({glyphName})...')
                                tmpLayer[component.baseGlyph] = srcFont[component.baseGlyph]
                                tmpLayer[component.baseGlyph].lib[self.glyphSetPathKey] = glyphsFolder

                        if self.verbose:
                            print(f'\t\timporting {glyphName}...')
                        tmpLayer[glyphName] = srcGlyph
                        tmpLayer[glyphName].width = srcGlyph.width

                    tmpLayer[glyphName].lib[self.glyphSetPathKey] = glyphsFolder

                if self.verbose:
                    print()

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