MCPcopy Create free account
hub / github.com/coldtype/coldtype / ColdtypeSerializer

Class ColdtypeSerializer

scripts/robofont_coldtype.py:10–69  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8DEFAULT_PATH = "~/robofont-coldtype.json"
9
10class ColdtypeSerializer(BaseWindowController):
11 def __init__(self):
12 self.w = FloatingWindow((300, 68), "Coldtype Serializer", minSize=(123, 200))
13 self.w.globalToggle = CheckBox((10, 10, -10, 20), 'serialize?', value=True)
14 self.w.pathBox = EditText((10, 34, -10, 22),
15 text=DEFAULT_PATH,
16 callback=self.editTextCallback)
17 self.path = Path(DEFAULT_PATH).expanduser().absolute()
18 addObserver(self, "shouldDraw", "fontWillSave")
19
20 self.setUpBaseWindowBehavior()
21 self.w.open()
22
23 self.writeGlyph()
24
25 def editTextCallback(self, sender):
26 self.path = Path(sender.get()).expanduser().absolute()
27
28 def windowCloseCallback(self, sender):
29 removeObserver(self, 'fontWillSave')
30 super(ColdtypeSerializer, self).windowCloseCallback(sender)
31
32 self.writeGlyph()
33
34 def writeGlyph(self):
35 glyph = CurrentGlyph()
36 image = glyph.image
37
38 print(">>>> writeGlyph", glyph, image)
39
40 data_out = {
41 "font": CurrentFont().path,
42 "name": glyph.name,
43 "image": dict(offset=image.offset, scale=image.scale, fileName=image.__dict__["_wrapped"]["fileName"]),
44 "layers": {}
45 }
46
47 for g in glyph.layers:
48 rp = RecordingPen()
49 g.draw(rp)
50
51 contours = []
52 for c in glyph.contours:
53 contour = []
54 for pt in c.points:
55 contour.append(dict(type=pt.type, x=pt.x, y=pt.y, smooth=pt.smooth, name=pt.name))
56 contours.append(contour)
57
58 data_out["layers"][g.layer.name] = dict(
59 value=rp.value,
60 width=g.width,
61 contours=contours)
62
63 self.path.write_text(json.dumps(data_out))
64
65 def shouldDraw(self, notification):
66 if not self.w.globalToggle.get():
67 return

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected