Sophie

Sophie

distrib > Mageia > 5 > i586 > media > core-release > by-pkgid > 6b465285c9860724a695461ac61807f4 > files > 656

fontforge-1.0-1.20120731.9.mga5.i586.rpm

<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-&gt;Open</A> the font and then
  <A HREF="filemenu.html#Generate">File-&gt;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&lt;$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&lt;$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&lt;$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&lt;$argc )
  if ( $argv[i]=="-format" || $argv[i]=="--format" )
    i=i+1
    if ( i&lt;$argc )
      format = $argv[i]
      if ( format!=".ttf" &amp;&amp; format!=".otf" &amp;&amp; \
	  format!=".pfb" &amp;&amp; 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&lt;$argc )
  if ( $argv[i]=="-format" || $argv[i]=="--format" )
    i=i+1
    if ( i&lt;$argc )
      format = $argv[i]
      if ( format!=".ttf" &amp;&amp; format!=".otf" &amp;&amp; \
	  format!=".pfb" &amp;&amp; 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 &amp;&amp; (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 &amp;&amp; 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>