Add docstrings and ideas for command line options
[pyff3.git] / lib.py
blob337196598283a9139d85479f6dbccb3a1dad505a
1 import os
2 import sys
4 import fontforge
6 class MyFont:
8 def __init__(self, input_font):
9 self.file = input_font
10 self.root = os.path.splitext(self.file)[0]
11 self.ff = fontforge.open(input_font)
13 def to_codepoints(self):
14 for glyph in self.ff.glyphs():
15 print "{0:x}".format(glyph.unicode)
17 class InterpolateFonts:
19 """Produce fonts using the interpolateFonts method of FontForge.
21 The kern of this class is the interpolate method.
23 FontForge defines an `interpolateFonts' method for its font type. The
24 amount of blending is defined with a `fraction'. From the FontForge
25 documentation:
27 "If you are interpolating from a light font to a bold one, then a medium
28 font might be 50% between the two, an extra-bold font might be 200% and a
29 thin one -100%."
31 The three different `buttons' correspond to the three types of
32 interpolation that are alluded to in the FontForge documentation.
34 For example, if the `steps' attribute defines a progression of four fonts,
35 and the `x' button is pressed then the following fonts are made
36 from_font.interpolateFonts(0, to_font)
37 from_font.interpolateFonts(0.25, to_font)
38 from_font.interpolateFonts(0.5, to_font)
39 from_font.interpolateFonts(0.75, to_font)
40 from_font.interpolateFonts(1, to_font)
41 """
43 def __init__(self, from_font, to_font, button, steps):
44 """The from_font is the `initial' font.
45 The to_font is the `final' font.
46 The `steps' determine the number of fonts to be generated.
47 The `button' determines the type of interpolation.
48 """
50 self.from_font = from_font
51 self.to_font = to_font
52 self.button = button
53 self.steps = steps
55 def font_info(self):
56 """Define strings for file and font names based on the input fonts."""
57 self.directory = os.path.split(self.from_font)[1] + os.path.split(self.to_font)[1] + self.button.upper()
58 self.fromfont_noextension = os.path.splitext(os.path.split(self.from_font)[1])[0]
59 self.tofont_noextension = os.path.splitext(os.path.split(self.to_font)[1])[0]
60 self.filename = self.fromfont_noextension + " to " + self.tofont_noextension + " " + self.button.upper()
61 self.familyname = self.fullname = self.fromfont_noextension + " to " + self.tofont_noextension
62 self.fontname = self.fromfont_noextension + "to" + self.tofont_noextension + "-" + self.button.upper()
63 self.copyright = "put together by " + sys.argv[0] + " from " + os.path.split(self.from_font)[1] + " and " + os.path.split(self.to_font)[1]
65 def interpolate(self):
66 """Produce a series of fonts using the interpolateFonts method.
68 The fonts are created in a directory whose name is defined in the
69 font_info method. Likewise for the file names, family names, etc.
71 The try/except checks whether the target directory exists already.
73 The number of iterations in the main loop is dependent on the `steps'
74 attribute.
75 """
77 try:
78 os.mkdir(self.directory)
79 except OSError:
80 pass
82 self.from_font_ff = fontforge.open(self.from_font)
84 myfraction = 1.0 / self.steps
86 os.chdir(self.directory)
88 for mystep in range(self.steps):
90 if self.button == "x":
91 myfont = self.from_font_ff.interpolateFonts(myfraction * mystep, self.to_font)
92 elif self.button == "y":
93 myfont = self.from_font_ff.interpolateFonts(-1 * myfraction * mystep, self.to_font)
94 elif self.button == "z":
95 myfont = self.from_font_ff.interpolateFonts(1 + myfraction * mystep, self.to_font)
97 myfont.familyname = self.familyname
98 myfont.fontname = self.fontname + str(int(myfraction * mystep * self.steps)).zfill(5)
99 myfont.fullname = self.fullname + str(int(myfraction * mystep * self.steps)).zfill(5)
100 myfont.copyright = self.copyright
101 myfont.generate(self.filename + str(int(myfraction * mystep * self.steps)).zfill(5) + ".ttf")
103 if self.button == "x":
104 myfont = self.from_font_ff.interpolateFonts(1, self.to_font)
105 elif self.button == "y":
106 myfont = self.from_font_ff.interpolateFonts(-1, self.to_font)
107 elif self.button == "z":
108 myfont = self.from_font_ff.interpolateFonts(2, self.to_font)
110 myfont.familyname = self.familyname
111 myfont.fontname = self.fontname + str(self.steps).zfill(5)
112 myfont.fullname = self.fullname + str(self.steps).zfill(5)
113 myfont.copyright = self.copyright
114 myfont.generate(self.filename + str(self.steps).zfill(5) + ".ttf")
116 os.chdir("..")