We write some Python
[pyff3.git] / lib.py
blobf34299251c4c08edd91440a8ea9a238ce06c185f
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 a series of fonts using the interpolateFonts method.
21 FontForge defines an `interpolateFonts' method for its font type. The
22 amount of blending is defined with a `fraction'. From the FontForge
23 documentation:
25 "If you are interpolating from a light font to a bold one, then a medium
26 font might be 50% between the two, an extra-bold font might be 200% and a
27 thin one -100%."
29 The three different `buttons' correspond to the three types of
30 interpolation that are alluded to in the FontForge documentation.
32 For example, if the `steps' attribute defines a progression of four fonts,
33 and the `x' button is pressed then the following fonts are made
34 from_font.interpolateFonts(0, to_font)
35 from_font.interpolateFonts(0.25, to_font)
36 from_font.interpolateFonts(0.5, to_font)
37 from_font.interpolateFonts(1, to_font)
39 """
41 def __init__(self, from_font, to_font, button, steps):
42 """The from_font is the `initial' font.
43 The to_font is the `final' font.
44 The `steps' determine the number of fonts to be generated.
45 The `button' determines the type of interpolation.
46 """
48 self.from_font = from_font
49 self.to_font = to_font
50 self.button = button
51 self.steps = steps
53 def font_info(self):
54 """Define strings for file and font names based on the input fonts."""
56 self.directory = os.path.split(self.from_font)[1] + os.path.split(self.to_font)[1] + self.button.upper()
57 self.fromfont_noextension = os.path.splitext(os.path.split(self.from_font)[1])[0]
58 self.tofont_noextension = os.path.splitext(os.path.split(self.to_font)[1])[0]
59 self.filename = self.fromfont_noextension + " to " + self.tofont_noextension + " " + self.button.upper()
60 self.familyname = self.fullname = self.fromfont_noextension + " to " + self.tofont_noextension
61 self.fontname = self.fromfont_noextension + "to" + self.tofont_noextension + "-" + self.button.upper()
62 self.copyright = "put together by " + sys.argv[0] + " from " + os.path.split(self.from_font)[1] + " and " + os.path.split(self.to_font)[1]
64 def interpolate(self):
65 """Produce a series of fonts using the interpolateFonts method.
67 The fonts are created in a directory whose name is defined in the
68 font_info method. Likewise for the file names, family names, etc.
70 The try/except checks whether the target directory exists already.
72 The number of iterations in the main loop is dependent on the `steps'
73 attribute.
74 """
76 try:
77 os.mkdir(self.directory)
78 except OSError:
79 pass
81 self.from_font_ff = fontforge.open(self.from_font)
83 myfraction = 1.0 / self.steps
85 os.chdir(self.directory)
87 for mystep in range(self.steps):
89 if self.button == "x":
90 myfont = self.from_font_ff.interpolateFonts(myfraction * mystep, self.to_font)
91 elif self.button == "y":
92 myfont = self.from_font_ff.interpolateFonts(-1 * myfraction * mystep, self.to_font)
93 elif self.button == "z":
94 myfont = self.from_font_ff.interpolateFonts(1 + myfraction * mystep, self.to_font)
96 myfont.familyname = self.familyname
97 myfont.fontname = self.fontname + str(int(myfraction * mystep * self.steps)).zfill(5)
98 myfont.fullname = self.fullname + str(int(myfraction * mystep * self.steps)).zfill(5)
99 myfont.copyright = self.copyright
100 myfont.generate(self.filename + str(int(myfraction * mystep * self.steps)).zfill(5) + ".ttf")
102 if self.button == "x":
103 myfont = self.from_font_ff.interpolateFonts(1, self.to_font)
104 elif self.button == "y":
105 myfont = self.from_font_ff.interpolateFonts(-1, self.to_font)
106 elif self.button == "z":
107 myfont = self.from_font_ff.interpolateFonts(2, self.to_font)
109 myfont.familyname = self.familyname
110 myfont.fontname = self.fontname + str(self.steps).zfill(5)
111 myfont.fullname = self.fullname + str(self.steps).zfill(5)
112 myfont.copyright = self.copyright
113 myfont.generate(self.filename + str(self.steps).zfill(5) + ".ttf")
115 os.chdir("..")