chiark / gitweb /
strokefont: More object-oriented approach.
authorBen Harris <bjh21@bjh21.me.uk>
Sat, 12 Aug 2017 08:25:56 +0000 (09:25 +0100)
committerBen Harris <bjh21@bjh21.me.uk>
Sat, 12 Aug 2017 08:25:56 +0000 (09:25 +0100)
Now we have one class for each broad class of stroked font.  I think
this will be easier to manage.

strokefont.py

index 1a81c9e75b00f94afd2cd9174306ee0caea6c626..e6595104c1587a740a60fff406e92d9de54e0de3 100644 (file)
@@ -5,53 +5,52 @@ from math import radians
 from psMat import translate
 from sys import argv
 
-chisel = fontforge.contour()
-chisel.moveTo(-50, 0)
-chisel.lineTo(28, 45)
-chisel.lineTo(50, 0)
-chisel.lineTo(-28, -45)
-chisel.closed = True
+class Stroker(object):
+    def __init__(self, fontname):
+        self.fontname = fontname
+    def modify_font(self, f):
+        f.strokedfont = False
+
+        for g in f.glyphs():
+            g.stroke(*self.nib)
+            g.removeOverlap()
+            g.addExtrema()
+            g.transform(translate(0, self.vshift))
+
+        f.fontname = self.fontname
+        return f
+
+class Plotter(Stroker):
+    def __init__(self, penwidth, weight):
+        self.nib = ['circular', penwidth, 'round', 'round']
+        self.vshift = penwidth / 2.0
+        super(Plotter, self).__init__("BedsteadPlotter-" + weight)
+
+class Chiseltip(Stroker):
+    def __init__(self, fontname):
+        chisel = fontforge.contour()
+        chisel.moveTo(-50, 0)
+        chisel.lineTo(28, 45)
+        chisel.lineTo(50, 0)
+        chisel.lineTo(-28, -45)
+        chisel.closed = True
+        self.nib = ['polygonal', chisel]
+        self.vshift = 45
+        super(Chiseltip, self).__init__(fontname)
 
 modes = {
-    'plotter-thin': {
-        'nib': ['circular', 10, 'round', 'round'],
-        'vshift': 5,
-        'fontname': "BedsteadPlotter-Thin",
-        },
-    'plotter-light': {
-        'nib': ['circular', 50, 'round', 'round'],
-        'vshift': 25,
-        'fontname': "BedsteadPlotter-Light",
-        },
-    'plotter-medium': {
-        'nib': ['circular', 100, 'round', 'round'],
-        'vshift': 50,
-        'fontname': "BedsteadPlotter-Medium",
-        },
-    'plotter-bold': {
-        'nib': ['circular', 150, 'round', 'round'],
-        'vshift': 75,
-        'fontname': "BedsteadPlotter-Bold",
-        },
-    'chiseltip': {
-        'nib': ['polygonal', chisel],
-        'vshift': 45,
-        'fontname': "BedsteadChiseltip",
-        },
+    'plotter-thin': Plotter(10, "Thin"),
+    'plotter-light': Plotter(50, "Light"),
+    'plotter-medium': Plotter(100, "Medium"),
+    'plotter-bold': Plotter(150, "Bold"),
+    'chiseltip': Chiseltip("BedsteadChiseltip"),
 }
 
 mode = modes[argv[1]]
 
 f = fontforge.open(argv[2])
 
-f.strokedfont = False
+mode.modify_font(f)
 
-for g in f.glyphs():
-    g.stroke(*mode['nib'])
-    g.removeOverlap()
-    g.addExtrema()
-    g.transform(translate(0, mode['vshift']))
-
-f.fontname = mode['fontname']
 f.save(argv[3])