def freezeGlyph(glyph): """Return a copy of a glyph, with components decomposed and all overlap removed.""" toRFGlyph = RGlyph() toRFpen = toRFGlyph.getPen() glyph.draw(toRFpen) if len(glyph.components): decomposedComponents = extractComposites(glyph) decomposedComponents.draw(toRFpen) singleContourGlyph = RGlyph() singleContourGlyph.width = glyph.width singleContourGlyph.name = glyph.name pointPen = singleContourGlyph.getPointPen() if len(toRFGlyph.contours) > 1: try: booleanGlyphs = [] for c in toRFGlyph.contours: if len(c) > 1: b = BooleanGlyph() pen = b.getPen() c.draw(pen) booleanGlyphs.append(b) finalBooleanGlyph = reduce(lambda g1, g2: g1 | g2, booleanGlyphs) finalBooleanGlyph.drawPoints(pointPen) except: toRFGlyph.drawPoints(pointPen) else: toRFGlyph.drawPoints(pointPen) return singleContourGlyph
def filterGroup(glyph, font=None, **overrideGlobalArguments): globalArguments = { self.splitSubfilterArgumentName(argumentName): self.arguments[argumentName] for argumentName in self.arguments } for key in overrideGlobalArguments: _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName( key) if (_subfilterName_, _overrideArgumentName_) != (None, None): globalArguments[( _subfilterName_, _overrideArgumentName_, _filterOrder_)] = overrideGlobalArguments[key] subfilters = [(self.getSubfilter(subfilterName), mode, source) for subfilterName, mode, source in self.subfilters] error = False canvasGlyph = RGlyph() canvasPen = canvasGlyph.getPen() canvasGlyph.width = glyph.width glyph.draw(canvasPen) steps = [] for i, (currentFilter, mode, source) in enumerate(subfilters): if error == True: continue if not source: sourceGlyph = canvasGlyph else: try: sourceGlyph = steps[source - 1] except: layerGlyph = glyph.getLayer(source) if len(layerGlyph) > 0: sourceGlyph = RGlyph() pen = sourceGlyph.getPen() layerGlyph.draw(pen) else: sourceGlyph = canvasGlyph sourceGlyph.name = glyph.name arguments = { argumentName: globalArguments[(subfilterName, argumentName, filterOrder)] for subfilterName, argumentName, filterOrder in globalArguments if subfilterName == currentFilter.name and filterOrder == i } processedGlyph = currentFilter.filterGlyph( sourceGlyph, arguments) if mode in ['union', 'difference', 'intersection', 'xor']: try: # collectedComponents = [component for component in processedGlyph.components] b1 = BooleanGlyph(canvasGlyph) b2 = BooleanGlyph(processedGlyph) operation = getattr(b1, mode) b3 = operation(b2) processedGlyph = RGlyph() processedPen = processedGlyph.getPen() b3.draw(processedPen) # for component in collectedComponents: # processedGlyph.appendComponent(component.baseGlyph, component.offset, component.scale) except: error = True steps.append(processedGlyph) if mode == 'ignore' and len(steps) > 1: processedGlyph = steps[-2] elif mode == 'ignore': processedGlyph = sourceGlyph if mode != 'add': canvasGlyph.clear() processedGlyph.draw(canvasPen) if processedGlyph.width: canvasGlyph.width = processedGlyph.width if error == True: canvasGlyph = ErrorGlyph() elif error == False: cleanPen = FilterPointPen(font) canvasGlyph.drawPoints(cleanPen) canvasGlyph.clearContours() canvasPointPen = canvasGlyph.getPointPen() cleanPen.extract(canvasPointPen) canvasGlyph.name = glyph.name canvasGlyph.unicode = glyph.unicode if canvasGlyph.width is None: canvasGlyph.width = glyph.width return canvasGlyph
def makeGlyph(glyphList, font, message, mark, saveBackup): # Initialize the progress bar tickCount = len(glyphList) bar = ProgressBar(message, tickCount) tick = 0 for item in glyphList: glyphName, advanceWidth, components = item # If the font has the glyph, lots of checking is required to see if changes have been made if font.has_key(glyphName): glyph = font[glyphName] #Build new glyph for comparisons newGlyph = RGlyph() count = 0 while count < len(components): component, x, y = components[count] newGlyph.appendComponent(component, offset=(x, y)) count = count + 1 newGlyph.width = advanceWidth # Make digest of the new glyph pointPen = DigestPointPen() newGlyph.drawPoints(pointPen) newDigest = pointPen.getDigest() # Make digest of the old glyph pointPen = DigestPointPen() glyph.drawPoints(pointPen) oldDigest = pointPen.getDigest() # Check the advance width if glyph.width != advanceWidth: glyph.width = advanceWidth if mark == 1: glyph.mark = 200 # If the digests don't match, rebuild components if oldDigest is not newDigest: if saveBackup == 1: backupName = glyph.name + '.bkup' font.insertGlyph(glyph, name=backupName) glyph.clearComponents() count = 0 while count < len(components): component, x, y = components[count] glyph.appendComponent(component, offset=(x, y)) count = count + 1 if mark == 1: glyph.mark = 200 # Clean up things glyph.update() bar.tick(tick) tick = tick + 1 # If the glyph is not in the font, build a new glyph else: font.newGlyph(glyphName, clear=True) glyph = font[glyphName] glyph.width = advanceWidth count = 0 while count < len(components): component, x, y = components[count] glyph.appendComponent(component, offset=(x, y)) count = count + 1 if mark == 1: glyph.mark = 300 glyph.update() bar.tick(tick) tick = tick + 1 bar.close()
def filterGroup(glyph, font=None, **overrideGlobalArguments): globalArguments = {self.splitSubfilterArgumentName(argumentName): self.arguments[argumentName] for argumentName in self.arguments} for key in overrideGlobalArguments: _subfilterName_, _overrideArgumentName_, _filterOrder_ = self.splitSubfilterArgumentName(key) if (_subfilterName_, _overrideArgumentName_) != (None, None): globalArguments[(_subfilterName_, _overrideArgumentName_, _filterOrder_)] = overrideGlobalArguments[key] subfilters = [(self.getSubfilter(subfilterName), mode, source) for subfilterName, mode, source in self.subfilters] error = False canvasGlyph = RGlyph() canvasPen = canvasGlyph.getPen() canvasGlyph.width = glyph.width glyph.draw(canvasPen) steps = [] for i, (currentFilter, mode, source) in enumerate(subfilters): if error == True: continue if not source: sourceGlyph = canvasGlyph else: try: sourceGlyph = steps[source-1] except: layerGlyph = glyph.getLayer(source) if len(layerGlyph) > 0: sourceGlyph = RGlyph() pen = sourceGlyph.getPen() layerGlyph.draw(pen) else: sourceGlyph = canvasGlyph sourceGlyph.name = glyph.name arguments = {argumentName: globalArguments[(subfilterName, argumentName, filterOrder)] for subfilterName, argumentName, filterOrder in globalArguments if subfilterName == currentFilter.name and filterOrder == i} processedGlyph = currentFilter.filterGlyph(sourceGlyph, arguments) if mode in ['union', 'difference', 'intersection', 'xor']: try: # collectedComponents = [component for component in processedGlyph.components] b1 = BooleanGlyph(canvasGlyph) b2 = BooleanGlyph(processedGlyph) operation = getattr(b1, mode) b3 = operation(b2) processedGlyph = RGlyph() processedPen = processedGlyph.getPen() b3.draw(processedPen) # for component in collectedComponents: # processedGlyph.appendComponent(component.baseGlyph, component.offset, component.scale) except: error = True steps.append(processedGlyph) if mode == 'ignore' and len(steps) > 1: processedGlyph = steps[-2] elif mode == 'ignore': processedGlyph = sourceGlyph if mode != 'add': canvasGlyph.clear() processedGlyph.draw(canvasPen) if processedGlyph.width: canvasGlyph.width = processedGlyph.width if error == True: canvasGlyph = ErrorGlyph() elif error == False: cleanPen = FilterPointPen(font) canvasGlyph.drawPoints(cleanPen) canvasGlyph.clearContours() canvasPointPen = canvasGlyph.getPointPen() cleanPen.extract(canvasPointPen) canvasGlyph.name = glyph.name canvasGlyph.unicode = glyph.unicode if canvasGlyph.width is None: canvasGlyph.width = glyph.width return canvasGlyph