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 __call__(self, glyph, font, **arguments): try: filteredGlyph = RGlyph() filteredGlyph.width = glyph.width drawingPen = filteredGlyph.getPen() filterPen = self.filterObject(drawingPen, **arguments) glyph.draw(filterPen) except: try: sourceGlyph = RGlyph() sourceGlyph.appendGlyph(glyph) sourceGlyph.width = glyph.width filteredGlyph = self.filterObject(sourceGlyph, **arguments) except: filteredGlyph = ErrorGlyph() return filteredGlyph
def copyContours(glyph): glyphCopy = RGlyph() glyphCopy.width = glyph.width pen = glyphCopy.getPen() glyph.draw(pen) glyphCopy.unicode = glyph.unicode glyphCopy.name = glyph.name return glyphCopy
def getCleanedGlyph(self, glyph): cleanGlyph = RGlyph() cleanGlyph.width = glyph.width pen = cleanGlyph.getPointPen() cleanPen = FilterPointPen() glyph.drawPoints(cleanPen) cleanPen.extract(pen) return cleanGlyph
def _decompose(glyph): components = glyph.components font = glyph.getParent() decomposedGlyph = RGlyph() if font is not None: for component in components: decomponent = RGlyph() decomponent.appendGlyph(font[component.baseGlyph]) decomponent.scale((component.scale[0], component.scale[1])) decomponent.move((component.offset[0], component.offset[1])) decomposedGlyph.appendGlyph(decomponent) for contour in glyph.contours: decomposedGlyph.appendContour(contour) decomposedGlyph.width = glyph.width return decomposedGlyph
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 source is None: sourceGlyph = canvasGlyph else: try: sourceGlyph = steps[source] 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) steps.append(processedGlyph) if mode in ['union', 'difference', 'intersection', 'xor']: try: b1 = BooleanGlyph(canvasGlyph) b2 = BooleanGlyph(processedGlyph) operation = getattr(b1, mode) processedGlyph = operation(b2) except: error = True if mode != 'add': canvasGlyph.clear() processedGlyph.draw(canvasPen) if error == True: canvasGlyph = ErrorGlyph() elif error == False: 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
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 source is None: sourceGlyph = canvasGlyph else: try: sourceGlyph = steps[source] 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) steps.append(processedGlyph) if mode in ['union', 'difference', 'intersection', 'xor']: try: b1 = BooleanGlyph(canvasGlyph) b2 = BooleanGlyph(processedGlyph) operation = getattr(b1, mode) processedGlyph = operation(b2) except: error = True if mode != 'add': canvasGlyph.clear() processedGlyph.draw(canvasPen) if error == True: canvasGlyph = ErrorGlyph() elif error == False: 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
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 _decompose(glyph): components = glyph.components font = glyph.getParent() decomposedGlyph = RGlyph() if font is not None: for component in components: decomponent = RGlyph() decomponent.appendGlyph(font[component.baseGlyph]) decomponent.scale((component.scale[0], component.scale[1])) decomponent.move((component.offset[0], component.offset[1])) decomposedGlyph.appendGlyph(decomponent) for contour in glyph.contours: decomposedGlyph.appendContour(contour) decomposedGlyph.width = glyph.width return decomposedGlyph class TypeSetter(object): def __init__(self, thisFont): if thisFont is None: return self.thisFont = thisFont self.cmap = thisFont.getCharacterMapping() self.getGroups = thisFont.groups.findGlyph self.kerning = thisFont.kerning self.preset = 'A4-P' self.type = { 'size':96,