def spikeGlyph(aGlyph, segmentLength=20, spikeLength=40, patternFunc=None): """Add narly spikes or dents to the glyph. patternFunc is an optional function which recalculates the offset.""" from math import atan2, sin, cos, pi new = _RGlyph() new.appendGlyph(aGlyph) new.width = aGlyph.width if len(new.contours) == 0: return flattenGlyph(new, segmentLength, segmentLines=True) for contour in new: l = len(contour.points) lastAngle = None for i in range(0, len(contour.points), 2): prev = contour.points[i-1] cur = contour.points[i] next = contour.points[(i+1)%l] angle = atan2(prev.x - next.x, prev.y - next.y) lastAngle = angle if patternFunc is not None: thisSpikeLength = patternFunc(spikeLength) else: thisSpikeLength = spikeLength cur.x -= sin(angle+.5*pi)*thisSpikeLength cur.y -= cos(angle+.5*pi)*thisSpikeLength new.update() aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def spikeGlyph(aGlyph, segmentLength=20, spikeLength=40, patternFunc=None): """Add narly spikes or dents to the glyph. patternFunc is an optional function which recalculates the offset.""" from math import atan2, sin, cos, pi new = _RGlyph() new.appendGlyph(aGlyph) new.width = aGlyph.width if len(new.contours) == 0: return flattenGlyph(new, segmentLength, segmentLines=True) for contour in new: l = len(contour.points) lastAngle = None for i in range(0, len(contour.points), 2): prev = contour.points[i - 1] cur = contour.points[i] next = contour.points[(i + 1) % l] angle = atan2(prev.x - next.x, prev.y - next.y) lastAngle = angle if patternFunc is not None: thisSpikeLength = patternFunc(spikeLength) else: thisSpikeLength = spikeLength cur.x -= sin(angle + .5 * pi) * thisSpikeLength cur.y -= cos(angle + .5 * pi) * thisSpikeLength new.update() aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def makeTestGlyph(): g = _RGlyph() pen = g.getPen() pen.moveTo((100, 100)) pen.lineTo((800, 100)) pen.curveTo((1000, 300), (1000, 600), (800, 800)) pen.lineTo((100, 800)) pen.lineTo((100, 100)) pen.closePath() return g
def thresholdGlyph(aGlyph, threshold=10): from robofab.pens.adapterPens import PointToSegmentPen new = _RGlyph() filterpen = ThresholdPen(new.getPen(), threshold) wrappedPen = PointToSegmentPen(filterpen) aGlyph.drawPoints(wrappedPen) aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def thresholdGlyphPointPen(aGlyph, threshold=10): """ Same a thresholdGlyph, but using the ThresholdPointPen, which should respect anchors.""" from robofab.pens.adapterPens import PointToSegmentPen new = _RGlyph() wrappedPen = new.getPointPen() filterpen = ThresholdPointPen(wrappedPen, threshold) aGlyph.drawPoints(filterpen) aGlyph.clear() new.drawPoints(aGlyph.getPointPen()) aGlyph.update() return aGlyph
def thresholdGlyph(aGlyph, threshold=10): """ Convenience function that handles the filtering. """ from robofab.pens.adapterPens import PointToSegmentPen new = _RGlyph() filterpen = ThresholdPen(new.getPen(), threshold) wrappedPen = PointToSegmentPen(filterpen) aGlyph.drawPoints(wrappedPen) aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def thresholdGlyph(aGlyph, threshold=10): """Convenience function that applies the **ThresholdPen** to a glyph. Returns a new glyph object (from objectsRF.RGlyph).""" from robofab.pens.adapterPens import PointToSegmentPen new = _RGlyph() filterpen = ThresholdPen(new.getPen(), threshold) wrappedPen = PointToSegmentPen(filterpen) aGlyph.drawPoints(wrappedPen) aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def flattenGlyph(aGlyph, threshold=10, segmentLines=True): """Convenience function that applies the **FlattenPen** pen to a glyph. Returns a new glyph object.""" from robofab.pens.adapterPens import PointToSegmentPen if len(aGlyph.contours) == 0: return new = _RGlyph() writerPen = new.getPen() filterpen = FlattenPen(writerPen, threshold, segmentLines) wrappedPen = PointToSegmentPen(filterpen) aGlyph.drawPoints(wrappedPen) aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def flattenGlyph(aGlyph, threshold=10, segmentLines=True): """Replace curves with series of straight l ines.""" from robofab.pens.adapterPens import PointToSegmentPen if len(aGlyph.contours) == 0: return new = _RGlyph() writerPen = new.getPen() filterpen = FlattenPen(writerPen, threshold, segmentLines) wrappedPen = PointToSegmentPen(filterpen) aGlyph.drawPoints(wrappedPen) aGlyph.clear() aGlyph.appendGlyph(new) aGlyph.update() return aGlyph
def copy(self, aParent=None): """Make a copy of this glyph. Note: the copy is not a duplicate fontlab glyph, but a RF RGlyph with the same outlines. The new glyph is not part of the fontlab font in any way. Use font.appendGlyph(glyph) to get it in a FontLab glyph again.""" from robofab.objects.objectsRF import RGlyph as _RGlyph newGlyph = _RGlyph() newGlyph.appendGlyph(self) for attr in GLYPH_COPY_ATTRS: value = getattr(self, attr) setattr(newGlyph, attr, value) parent = self.getParent() if aParent is not None: newGlyph.setParent(aParent) elif self.getParent() is not None: newGlyph.setParent(self.getParent()) return newGlyph
from robofab.world import CurrentFont # We need to import a class with a different # implementation for the glyph object. # It looks a bit odd, but this is how it is done from robofab.objects.objectsRF import RGlyph as _RGlyph f = CurrentFont() # pick two compatible glyphs as masters m1 = f["A"] m2 = f["B"] # make a new glyph object from this other glyph class g = _RGlyph() # interpolation factor which is bound to make floats oddFactor = 0.2382345 # go! g.interpolate(oddFactor, m1, m2) # let's have a look at the raw results for contour in g: for pt in contour.points: print "float", pt.x, pt.y # a glyph can round itself off: g.round()
def _getMathDestination(self): from robofab.objects.objectsRF import RGlyph as _RGlyph return _RGlyph()
def _makeFlat(aGlyph, segmentLength = 10): """Helper function to flatten the glyph with a given approximate segment length.""" new = _RGlyph() filterpen = FlattenPen(aGlyph, new.getPen(), segmentLength) aGlyph.draw(filterpen) return new
def _makeFlat(aGlyph, segmentLength=10): """Helper function to flatten the glyph with a given approximate segment length.""" new = _RGlyph() filterpen = FlattenPen(aGlyph, new.getPen(), segmentLength) aGlyph.draw(filterpen) return new