Changes

Jump to navigation Jump to search
Added description
== TrueType fonts in luaTeX ==

The following page will provide a short example of what is possible in ConTeXt mkiv. Most of the actual code has been provided by Arthur Reutenauer, to whom I am immensely grateful for his help. The instructions should help you explore some features of luaTeX/mkiv and should inspire you to find your own solutions.

=== The problem ===

Newer Windows versions come with a nice Palatino font which I use for my ConTeXt typesetting quite a lot. It has oldstyle figures, real small caps, and a couple of nice additional ligatures. With an OpenType font, luaTeX could just "switch on" this additional stuff by calling the right OpenType features. With an old-fashioned TrueType font, things are a bit less handy. Nevertheless, it is still immensely more convenient than they ever were with pdfTeX. So as an exercise, we will do two things: use some of the ligatures, and use old-style numerals.

=== The solution: The <code>fea</code> file ===

<code>fea</code> files tell luaTeX to do something special about a font. In our case, we will create a <code>fea</code> file that tells it to use oldstyle figures and replace some letter combinations by the corresponding ligature. This has to be done in two <code>lua</code> lua tables; the code is easy to understand and write, so even if you have absolutely no experience with <code>lua</code>, you will have no trouble setting this up. So let us begin with the first table. The first wonderful thing about luaTeX is that it can address characters by their unicode value. With pdfTeX, you always had to find out what the name of the character in that particular font was; for luaTeX, it is enough to know that an oldstyle zero is at position F730, oldstyle one at position F731 etc (if you don't know the Adobe glyph list by heart, you can find a reference [[http://partners.adobe.com/public/developer/en/opentype/glyphlist.txt here]]). This means that our first <code>lua</code> table for the "simple" lookups (one character is replaced with one other character) will look like this:
<pre>
lookup PalatinoSimple {
lookupflag 0 ;
sub zero by uniF730 ;
sub one by uniF731 ;
sub two by uniF732 ;
sub three by uniF733 ;
sub four by uniF734 ;
sub five by uniF735 ;
sub six by uniF736 ;
sub seven by uniF737 ;
sub eight by uniF738 ;
sub nine by uniF739 ;
} PalatinoSimple ;
</pre>

This tells luaTeX: please '''sub'''stitute, whenever you encounter the charater <code>zero</code> in the source file, this character witht the character at the unicode position <code>F730</code>, etc.

The next table will contain the "multiple" lookups (two characters are replaced by one character, viz. a ligature). Unfortunately, these ligatures have no fixed Unicode position that would be valid for all fonts, so in this case, it is safer to use the actual character names (which I found out by opening the font in <code>fontforge</code>):

<pre>
lookup PalatinoMultiple {
lookupflag 0 ;
sub f f i by ffi ;
sub f f l by ffl ;
sub f i by fi ;
sub f l by fl ;
sub f f by ff ;
sub f t by ft ;
sub Q u by Qu ;
sub T h by Th ;
} PalatinoMultiple ;
</pre>

This table, then, tells luaTeX: whenever an <code>f</code> is followed by an <code>i</code> in the input stream, replace this with the character <code>fi</code> (which happens to be the fi-ligature).

We need to combine these two lookup tables into something that is called a "font feature." These features have to follow certain conventions; they have to have exactly four letters; so let's call it <code>pala</code> for our Palatino font:

<pre>
feature pala {
script DFLT ;
language dflt ;
lookup PalatinoMultiple ;
lookup PalatinoSimple ;

script latn;
language dflt ;
lookup PalatinoMultiple ;
lookup PalatinoSimple ;
} pala ;
</pre>

We define this feature both for the "default" and for the "latin" script (I don't know if this is strictly necessary, but go for the "belt and suspenders" approach.)

We save the file with these tables under a name that ends in <code>fea</code> (<code>palatino.fea</code> comes to mind) and put it in a directory under <code>$texmf/fonts/fea/context</code>.

=== The solution: The typescript file ===

We now have written the <code>fea</code> file, but we need to instruct ConTeXt to use it. This is done in the typescript. First, we need to make sure that this file and the corresponding typescript will only be used when ConTeXt mkiv is run. So we first write a typescript that is a simple stub. Let's call it <code>type-palatino.tex</code>. It contains just one line:
<texcode>
\loadmarkfile{type-palatino}
</texcode>

Which means: when ConTeXt is run, see if it is run as mkii or as mkiv and load the corresponding typescript.

Next, we write the real typescript, <code>type-palatino.mkiv</code>. In it, we have to "install" the font feature <code>pala</code> which we have defined in the <code>fea</code> file, and we have to connect this feature to the <code>fea</code> file. Moreover, we use the font the mkiv way, i.e. we refer to the font by its disk name so luaTeX will directly read and use the TrueType font file, without needing a <code>tfm</code>, as pdfTeX did. The rest is exactly like any ConTeXt typescript:

<texcode>
\installfontfeature[otf][pala]

\definefontfeature
[myfeature]
[mode=node,script=latn,language=dflt,kern=yes,trep=yes,pala=yes,featurefile=palatino.fea]

\starttypescript [serif] [palatino]
\definefontsynonym [Palatino-Roman] [name:PalatinoLinotype-Roman] [features=myfeature]
\definefontsynonym [Palatino-Bold] [name:PalatinoLinotype-Bold] [features=myfeature]
\definefontsynonym [Palatino-Italic] [name:PalatinoLinotype-Italic] [features=myfeature]
\definefontsynonym [Palatino-Bold-Italic] [name:PalatinoLinotype-BoldItalic] [features=myfeature]
\stoptypescript

\starttypescript [serif] [palatino] [name]
\usetypescript[serif][fallback]
\definefontsynonym [Serif] [Palatino-Roman] [features=myfeature]
\definefontsynonym [SerifItalic] [Palatino-Italic] [features=myfeature]
\definefontsynonym [SerifBold] [Palatino-Bold] [features=myfeature]
\definefontsynonym [SerifBoldItalic] [Palatino-Bold-Italic] [features=myfeature]
\stoptypescript

\starttypescript [Palatino]
\definetypeface [Palatino] [rm] [serif] [palatino] [default]
\stoptypescript

</texcode>

And that's all! Now you can simply use this palatino font with something like this in your files:

<texcode>
\usetypescriptfile[type-palatino]
\usetypescript[Palatino]
\setupbodyfont[Palatino,12pt]
</texcode>

and you'll get the proper ligatures and oldstyle numerals. Defining a small caps Palatino variant is left as an exercise to the reader (hint: small caps characters have names such as <code>Asmall</code> in Palatino, and this is a case for a "simple" lookup table).

===To know more===

Arthur has been kind enought to provide a few links to official documents from Adobe and Microsoft that explain everything in great detail: [[http://www.microsoft.com/typography/otspec/GSUB.htm 1]] and [[http://www.adobe.com/devnet/opentype/afdko/ 2]]. Good luck with your own experiments!

[[User:Thomas|Thomas]] 21:51, 8 December 2007 (CET)
gardener
111

edits

Navigation menu