-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14 Randomize Glyph.py
73 lines (52 loc) · 1.91 KB
/
14 Randomize Glyph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# MenuTitle: 14 Randomize Glyph
from mojo.roboFont import CurrentFont, RGlyph
from fontTools.pens.basePen import BasePen
from random import randint
class MyPen(BasePen):
# from fontTools.pens.basePen.BasePen:
def __init__(self, glyphSet, writer_pen, max_move=0):
self.glyphSet = glyphSet
self.__currentPoint = None
self.writer_pen = writer_pen
self.max_move = max_move
def randomize(self, pt):
dx = randint(-self.max_move, self.max_move)
dy = randint(-self.max_move, self.max_move)
return (pt[0] + dx, pt[1] + dy)
def _moveTo(self, pt):
self.writer_pen.moveTo(self.randomize(pt))
def _lineTo(self, pt):
self.writer_pen.lineTo(self.randomize(pt))
def _curveToOne(self, bcp1, bcp2, pt):
self.writer_pen.curveTo(
self.randomize(bcp1), self.randomize(bcp2), self.randomize(pt)
)
def _closePath(self):
self.writer_pen.closePath()
def _endPath(self):
self.writer_pen.endPath()
def addComponent(self, baseGlyphName, transformation):
pass
def randomize_glyph(glyph):
source = glyph
font = glyph.font
# Save the anchors from the original glyph in a list
anchors = [a for a in source.anchors]
# Remove all anchors from the glyph so they don't interfere with our processing
for a in anchors:
source.removeAnchor(a)
# Temporary glyph to which the pen is writing
target = RGlyph()
target_pen = target.getPen()
source_pen = MyPen(font, target_pen, 10)
source.draw(source_pen)
# Clear the original glyph and add the modfied outline
source.clear(components=False)
source.appendGlyph(target)
# Restore the saved anchors
for a in anchors:
source.appendAnchor(a.name, a.position)
if __name__ == "__main__":
font = CurrentFont()
for glyph_name in font.selection:
randomize_glyph(font[glyph_name])