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