def expand_font(src_font, distance, join=1, cap=1): '''Expand outlines for all glyphs in font.''' # create a new empty font dst_font = NewFont(showUI=False) # expand all glyph for glyph_name in src_font.keys(): # get source glyph src_glyph = src_font[glyph_name] # get dest glyph dst_font.newGlyph(glyph_name) dst_glyph = dst_font[glyph_name] # expand glyph into dest font outline_pen = make_outline(src_glyph, distance, join, cap) outline_pen.drawPoints(dst_glyph.getPointPen()) # copy width from source glyph dst_glyph.width = src_glyph.width # copy components if len(src_glyph.components): for component in src_glyph.components: dst_glyph.appendComponent(component.baseGlyph, component.offset, component.scale) # done return dst_font
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')