def removeOverlap(glyph): glyphCopy = RGlyph() glyphCopy.width = glyph.width pointPen = glyphCopy.getPointPen() glyph.drawPoints(pointPen) glyphCopy.removeOverlap() return glyphCopy
def reverseContours(glyph): glyphCopy = RGlyph() glyphCopy.width = glyph.width pointPen = glyphCopy.getPointPen() reversePen = ReverseContourPointPen(pointPen) glyph.drawPoints(reversePen) return glyphCopy
def removeOverlap(glyph): toRFGlyph = RGlyph() toRFpen = toRFGlyph.getPen() glyph.draw(toRFpen) singleContourGlyph = RGlyph() singleContourGlyph.width = glyph.width singleContourGlyph.name = glyph.name pointPen = singleContourGlyph.getPointPen() if len(toRFGlyph) > 1: 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) else: glyph.drawPoints(pointPen) return singleContourGlyph
def getCleanedGlyph(self, glyph): cleanGlyph = RGlyph() cleanGlyph.width = glyph.width pen = cleanGlyph.getPointPen() cleanPen = FilterPointPen() glyph.drawPoints(cleanPen) cleanPen.extract(pen) return cleanGlyph
def RGlyphFromGlifString(glifString): """\ Return RGlyph object directly from .glif file """ from robofab.world import RGlyph from robofab.glifLib import readGlyphFromString glyph = RGlyph() readGlyphFromString(glifString, glyph, glyph.getPointPen()) return glyph
def normalizeGlyph(self, glyph): componentsCollector = CollectComponentsPen() glyph.draw(componentsCollector) components = componentsCollector.get() anchors = [] for anchor in reversed(glyph.anchors): anchors.append((anchor.name, (anchor.x, anchor.y))) cleanGlyph = RGlyph() cleanGlyph.width = glyph.width pen = cleanGlyph.getPointPen() cleanPen = FilterPointPen() glyph.drawPoints(cleanPen) cleanPen.extract(pen) return cleanGlyph, components, anchors
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 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