July 7, 2007: to correct problems with finding .ttf fonts and in output of xml file (Tristan Lyons)
July 28, 2007: added a script to use the defoma .hints files instead.
It is easier, not as precise, but perhaps good enough
.
ImageMagick® is a series of magic
programs that allow you to
create, edit, and compose bitmap images. It makes adding images to web
pages programmatically so easy.
However, it is also very useful for making graphics to be put into videos. Think of them as 5 seconds worth of a slide or chart or whatever that you want to explain. All it is, is a single image that is duplicated enough times to fill the alloted time. The goal is to make that single image programmatically, but you will find that adding fonts to ImageMagick on debian is not necessarily intuitive.
Doing it this way makes adding fonts easier, but at some loss in precision.
If you do not. This won't work and you will have to follow the methods below.
Put this this script, hints2IM.awk, in the ${HOME}/.magick directory and run it.
The hints that the script needs are the defoma hint files usually found in /etc/defoma/hints.
Example:
./hints2IM.awk /etc/defoma/hints/gsfonts.hints > type-ghostscript.xml
See Set Up below to find out what to do with the output.
The script works for both postscript and truetype fonts.
The assumptions underlying the scripts and these directions are that
As a quick aside, you might ask
I have TrueType fonts. Why do I need .afm files? Those come with type1 postscript fonts.Good question. If you are using TrueType fonts you don't really need .afm files, but you need the same kind of information about the font contained in the .afm file. So, if you have another way of getting that information, you can use that instead and modify magick.awk.
If you want to work in another directory, just put the two scripts into that directory and point makemagick.sh at the afm files. They can be in the same directory or somewhere else. If they are somewhere else you must change the script.
There are just a few steps that you need to follow:
You should look for this file:
file:///usr/share/doc/imagemagick/www/resources.html
It will explain how ImageMagick finds fonts and you can deduce what you need to do to make it work. I did the following:
$ locate type.xml
/usr/lib/ImageMagick-6.2.4/config/type.xml
$ mkdir ${HOME}/.magick
$ cd {$HOME}/.magick
$ cp /usr/lib/ImageMagick-6.2.4/config/type.xml .
$ cp /usr/lib/ImageMagick-6.2.4/config/type-ghostscript.xml .
$ cp /usr/lib/ImageMagick-6.2.4/config/type-windows.xml .
Then I added type-bitstream.xml (we'll get to this in a minute) and an appropriate entry for it in type.xml. I also added a line for the MS TrueType core fonts.
What information do you really need from the .afm file?
- FontName Incised901BT-Black
- FullName Incised 901 Black BT
- FamilyName Incised901 BT
- Weight Bold
These are the correct formal names associated with the font. You can't make up your own name or what you
thinkis the correct name. And it is most certainly not the file name. It must be the correct name.
There are two options really:
Finding the files. Sometimes it is possible to find font metric files on the web. Metric files are innocuous. You can't print with them. You need the .pfb files for that. So sometimes they are available.
A good place to look is on CTAN, the TeX archive. Try the directory: ctan/fonts/psfonts/
You should find .afm files for some Bitstream fonts, the CorelPak fonts, the URW fonts, the Lucida family, and Courier. They may or may not match your ttf fonts. But they should work for Postscript fonts.
There may be other place on the web also.
Creating the files. The following assumes that you need to create the files from what you have.
If you have the afm files that came with the fonts, good. Move them to the current working directory or point the script at them. Then skip to here
If you don't you will have to create them from the .pfb files. There are two programs to do this: pf2afm from the ghostscript package and getafm from the PSUtils package. Both require that you have ghostscript installed.
Both of these programs assume that the fonts are already installed, and both have their quirks.
getafm doesn't handle hints, and requires the exact fontname.
For one-at-a-time occasions that won't matter, but if you want to do
a lot of files it can be inconvenient because it uses font names
not file names
.
pf2afm puts the afm file in the same directory in which the pfb file resides. In most cases, this is probably not what you want, since that is usually a directory that requires root access. If it is a pfb file in your ${HOME}/.fonts directory it won't be a problem -- just messy. The alternative is to cp the pfb into the directory in which you are working. Another work-around.
or
PFBDIR=/wherever/the/pfbs/are/
for f in ${PFBDIR}/*.pfb; do
echo ${f}
pf2afm ${f}
done
Assume that there are some TrueType fonts on your computer that you wish to used with ImageMagick. They are in, say
These are the free Bitstream fonts that come on linux.
It is possible to use either ttf2pt1 or ttf2afm to get an .afm file from a truetype font. ttf2pt1 is a separate download, or on Debian apt-get. ttf2afm is part of the TeX package.
TTFDIR=/usr/share/fonts/truetype/ttf-dejavu/
find ${TTFDIR} iname "*.ttf" | while read f ; do
echo ${f}
fn=${f##*/}
ttf2pt1 -a -A ${f} ${fn%.*}
done
# We don't need these.
rm -f *.t1a
To use ttf2afm instead of ttf2pt1:
TTFDIR=/usr/share/fonts/truetype/ttf-dejavu/
find ${TTFDIR} iname "*.ttf" | while read f ; do
echo ${f}
fn=${f##*/}
ttf2afm ${f} > ${fn%.*}.afm
done
ImageMagick needs an xml file with information about each font.
<type name="Swiss721BT-Light" fullname="Swiss 721 Light" family="Swiss 721" foundry="Bitstream" weight="200" style="normal" stretch="normal" format="type1" glyphs="/home/valliant/.fonts/truetype/bitstream/tt0001m_.ttf" />
The file name should probably follow the example of the other files. Since I am using Bitstream fonts, I chose to call the file type-bitstream.xml. Notice that I used the name above in the type.xml file.
Here are two scripts shell and awk create the type-bitstream.xml file from the .afm files.
In the makemagick.sh shell script you should change the following two variables. AFMDIR is the directory where the .afm files are.
The original makemagick.sh has been modified to take a single parameter: the type of font being processed. The scripts handle both TrueType and Postscript fonts, so it makes sense to ask which type is being processed. It also helps to correct an error in the original script, pointed out by Tristan Lyons.
The afm files are no longer necessary. They can be deleted.
Sigh. The windows' truetype fonts would not work until I added the path to each one to the type-windows.xml file.
Don't have them? apt-get install msttcorefonts
Original
Fixed
I did it like this. You may have a different way.
Actually I put it all on one line, but it doesn't work on the screen. It wraps.
Now for the acid test. Which name
is the correct one?
name, fullname, or family?
Use the name for ImageMagick. See the following example. grep shows:
type-bitstream.xml: name="HoboBT-Regular"
type-bitstream.xml: fullname="Hobo"
type-bitstream.xml: family="Hobo"
* The gradient in the image was shamelessly taken from the examples found on the ImageMagick examples website They are wonderful!
convert -size 850x80 gradient:white-blue \
-font HoboBT-Regular -pointsize 70 -tile gradient:blue-red \
-annotate +10+65 'Add Fonts to ImageMagick!' gradients.jpg
Robert Valliant, University of Hawaii at Manoa