Debian Codegraph @

doc/sphinx/tutorial/scripting-tutorial.rst

raw
1 FontForge Scripting Tutorial
2 ============================
3
4 .. note::
5
6 FontForge now provides python scripting. If you are familiar with python
7 that is probably a better choice. There is a lot of information available on
8 `python <http://www.python.org/doc/>`__, I shan't repeat it. FontForge's own
9 additions to python are documented :doc:`here </scripting/python>`.
10
11 I try to keep things at a fairly elementary level, but this is *not* an attempt
12 to teach programming.
13
14
15 A simple example
16 ----------------
17
18 Suppose you have a Type1 PostScript font (a pfb/afm combination) and you would
19 like to convert it into a TrueType font. What would a script look like that
20 could do this?
21
22 If you were doing this with the UI you would first
23 :ref:`File->Open <filemenu.Open>` the font and then
24 :ref:`File->Generate <filemenu.Generate>` a truetype font. You do essentially
25 the same thing when writing a script:
26
27
28 Initial Solution
29 ^^^^^^^^^^^^^^^^
30
31 ::
32
33 Open($1)
34 Generate($1:r + ".ttf")
35
36 There is usually a scripting function with the same name as a menu command
37 (well, the same as the English name of the menu command).
38
39 ':ref:`$1 <scripting.variables>`' is magic. It means the
40 :ref:`first argument passed to the script <scripting-tutorial.Invoking>`.
41
42 ':ref:`$1:r + ".ttf" <scripting.Expressions>` ' is even more complicated magic.
43 It means: 'take the first argument ($1) and remove the extension (which is
44 probably ".pfb") and then append the string ".ttf" to the filename.'
45
46 The Generate scripting command decides what type of font to generate depending
47 on the extension of the filename you give it. Here we give it an extension of
48 "ttf" which means truetype.
49
50 Note that I make no attempt to load an afm file. That's because the Open command
51 will do this automagically if it is in the same directory as the pfb.
52
53
54 Real World Considerations
55 ^^^^^^^^^^^^^^^^^^^^^^^^^
56
57 So that's what the script looks like. To be useful it should probably live in a
58 file of its own. So create a file called "convert.pe" and store the above script
59 in it.
60
61 But to be even more useful you should add a comment line to the beginning of the
62 script (a comment line is one that starts with the '#' character:
63
64 ::
65
66 #!/usr/local/bin/fontforge
67 Open($1)
68 Generate($1:r + ".ttf")
69
70 Having done that type:
71
72 ::
73
74 $ chmod +x convert.pe
75
76 This comment is not important to fontforge, but it is meaningful to the unix
77 shell, as we will see in the next section.
78
79
80 .. _scripting-tutorial.Invoking:
81
82 Invoking a script and passing it arguments
83 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
85 Ok, now we've got basic script. How do we use it?
86
87 Well we can pass it to FontForge directly by typing
88
89 ::
90
91 $ fontforge -script convert.pe foo.pfb
92
93 But if you added the comment above you can also type:
94
95 ::
96
97 $ convert.pe foo.pfb
98
99 And the shell knows to call fontforge to process the script.
100
101
102 Using loops
103 ^^^^^^^^^^^
104
105 That's all well and good, but if you have lots of fonts to convert this might
106 get tedious. So let's change our script so it will take lots of filenames which
107 we can then process one at a time.
108
109 ::
110
111 #!/usr/local/bin/fontforge
112 i=1
113 while ( i<$argc )
114 Open($argv[i])
115 Generate($argv[i]:r + ".ttf")
116 i = i+1
117 endloop
118
119 Here we have introduced the variables ``$argc`` and ``$argv``. The first is
120 simple the number of arguments passed to this script, while the second is an
121 array containing all those arguments, and ``$argv[i]`` means the i'th argument
122 passed.
123
124 Then we have:
125
126 ::
127
128 i=1
129
130 This declares that we have a local variable called "i" and assigns it the value
131 1.
132
133 The while loop will execute all statements between the "``while``" keyword and
134 the "``endloop``" keyword as long as the condition ``( i<$argv )`` is true. In
135 other words as long as there are more arguments to convert the loop will keep
136 going.
137
138 And we can invoke this script with
139
140 ::
141
142 $ convert.pe *.pfb
143
144 Or something similar.
145
146
147 Complexities
148 ^^^^^^^^^^^^
149
150 Now suppose that you wanted a script that could convert a truetype font to an
151 opentype font as well as a type1 font to a truetype. Well let's make our script
152 even more complex:
153
154 ::
155
156 #!/usr/local/bin/fontforge
157 i=1
158 format=".ttf"
159 while ( i<$argc )
160 if ( $argv[i]=="-format" || $argv[i]=="--format" )
161 i=i+1
162 format = $argv[i]
163 else
164 Open($argv[i])
165 Generate($argv[i]:r + format)
166 endif
167 i = i+1
168 endloop
169
170 And this could be invoked with something like:
171
172 ::
173
174 $ convert.pe --format ".ttf" *.pfb --format ".otf" *.ttf
175
176 So now we have a new variable, ``format``, which contains the type of output we
177 want to use from now on. We initialize it to truetype, ".ttf", but if the user
178 gives us an argument called "--format" (or "-format") then we change the output
179 to be whatever the user asked for.
180
181 We've introduced the "``if``" statement here. This statement will execute the
182 statements between "``if``" and "``else``" if the condition
183 ``( $argv[i]=="-format" || $argv[i]=="--format" )`` is true, otherwise it will
184 execute the statements between "``else``" and "``endif``". The || operator means
185 "or", so the condition is true if $argv[i] is either "-format" or "--format".
186
187 We really should do some error checking to make sure:
188
189 * There was another argument to store into the ``format`` variable
190 * The argument contained a reasonable value (.ttf, .pfb, .otf, .svg, ...)
191
192 ::
193
194 #!/usr/local/bin/fontforge
195 i=1
196 format=".ttf"
197 while ( i<$argc )
198 if ( $argv[i]=="-format" || $argv[i]=="--format" )
199 i=i+1
200 if ( i<$argc )
201 format = $argv[i]
202 if ( format!=".ttf" && format!=".otf" && \
203 format!=".pfb" && format!=".svg" )
204 Error( "Expected one of '.ttf', '.otf', '.pfb' or '.svg' for format" )
205 endif
206 endif
207 else
208 Open($argv[i])
209 Generate($argv[i]:r + format)
210 endif
211 i = i+1
212 endloop
213
214 Note that when we had a long line we broke it in two by using a backslash.
215 Normally the end of a line marks the end of a statement, so we need the
216 backslash to indicate the statement continues onto the next line.
217
218 Now that will produce a valid postscript font from a truetype one if we want...
219 But we can improve on that conversion:
220
221 ::
222
223 #!/usr/local/bin/fontforge
224 i=1
225 format=".ttf"
226 while ( i<$argc )
227 if ( $argv[i]=="-format" || $argv[i]=="--format" )
228 i=i+1
229 if ( i<$argc )
230 format = $argv[i]
231 if ( format!=".ttf" && format!=".otf" && \
232 format!=".pfb" && format!=".svg" )
233 Error( "Expected one of '.ttf', '.otf', '.pfb' or '.svg' for format" )
234 endif
235 endif
236 else
237 Open($argv[i])
238 if ( $order==2 && (format==".otf" || format==".pfb" ))
239 SetFontOrder(3)
240 SelectAll()
241 Simplify(128+32+8,1.5)
242 ScaleToEm(1000)
243 elseif ( $order==3 && format==".ttf" )
244 ScaleToEm(2048)
245 RoundToInt()
246 endif
247 Generate($argv[i]:r + format)
248 endif
249 i = i+1
250 endloop
251
252 By its nature a truetype font will contain more points than will a postscript
253 font, but we can use the Simplify command to reduce that number when we convert
254 from one format to another. Also PostScript fonts should have 1000 units to the
255 em while TrueType fonts should have a power of 2 units/em (generally 2048 or
256 1024), so enforce these conventions. Finally TrueType fonts only support
257 integral (or in some cases half-integral) coordinates for points.
258
259
260 Other Examples
261 --------------
262
263
264 Adding Accented Characters
265 ^^^^^^^^^^^^^^^^^^^^^^^^^^
266
267 Very few Type1 fonts have the full unicode range of accented characters. With
268 FontForge it is fairly easy to load a Type1 font, add as many possible accented
269 characters as the font permits (If the font does not contain ogonek, then FF
270 won't be able to create Aogonek).
271
272 ::
273
274 #!/usr/local/bin/fontforge
275 Open($1)
276 Reencode("unicode")
277 SelectWorthOutputting()
278 SelectInvert()
279 BuildAccented()
280 Generate($1:r + ".otf")
281
282
283 Merging a type1 and type1 expert font and creating appropriate GSUB tables.
284 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
285
286 Adobe used to ship font packs containing a normal font and an "expert" font
287 which included small caps, lower case numbers, etc. Now-a-days that should all
288 be stuffed into one otf font with appropriate GSUB entries linking the glyphs.
289
290 ::
291
292 #!/usr/local/bin/fontforge
293 Open($1)
294 MergeFonts($2)
295 RenameGlyphs("AGL with PUA")
296 SelectAll()
297 DefaultATT("*")
298
299
300 More examples
301 ^^^^^^^^^^^^^
302
303 See the :ref:`page on scripting <scripting.Example>`.