# [Fontinst] How to write code in the .fd files?

Lars Hellström Lars.Hellstrom at residenset.net
Mon Jul 21 17:40:14 CEST 2008

Plamen Tanovski skrev:
> Hi,
>
> how can one insert any code to the .fd file[s] whithin
> \insallfonts-\endinstallfonts? I cannot find any command for that.

No, there isn't one.

\begin{reminiscing}
If things had worked out a bit differently, there could have been
one though, as putting extra stuff in FD files was one of my main
interests back in the days where I was most radical with my
fontinst-hacking; check out macros/latex/contrib/relenc on CTAN
if you're interested in that story. One reason I didn't pursue
it further is that I didn't get much support from the LaTeX
maintainers for the idea that one might want to put other code
in FD files than the officially approved \DeclareFontFamily and
\DeclareFontShape, but perhaps the times have changed in that
respect...

(Of course, a more practical problem was that the application
I had in mind also called for some rather extensive collation
of data, which was highly nontrivial to do using TeX alone.
I remember I even examined using BibTeX as an auxiliary
processor, but nothing ever came of that. \emoticon{;-)})
\end{reminiscing}

So how might one put extra material in that file, then? The fontinst
macro that generates FD files is \fd_family, and the bulk of the
material in the file is generated by executing the \<encoding>-<family>
macro, so by adding stuff to that you can force insertion of additional
code into the FD file. There are however a couple of complications.

First, \<encoding>-<family> is executed *three* times: first with
\do_shape let to \fd_shape, second with \do_shape let to
\substitute_shape, and third with \do_shape let to \remove_shape. You
probably only want your things to happen at one of these stages.

Second, the way that items are appended to the \<encoding>-<family>
macro is by way of a full expansion of the old contents. The \do_shape
command is protected from expansion at these times by being let to
\relax, but whatever material you place there is at the mercy of an
unprotected \xdef.

I'd suggest handling the matter as follows:

1. Define a command, \mytask say, which uses \out_line and the like to
write the extra material you want in the FD file.

2. Define a macro \protected_mytask as follows:

% Assuming \fontinstcc
\ifx \do_shape\x_relax
\else \ifx \do_shape\remove_shape
\fi \fi
}

3. Append \protected_mytask to the \<encoding>-<family> macros of the
font families you want it for. It is probably best to define a command
also for this: