tuto7.htm
15.1 KB
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
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">
<TITLE>Adding new fonts and encoding support</TITLE>
<LINK TYPE="text/css" REL="stylesheet" HREF="../fpdf.css">
</HEAD>
<BODY>
<H2>Adding new fonts and encoding support</H2>
This tutorial explains how to use TrueType or Type1 fonts so that you are not limited to the standard
fonts any more. The other interest is that you can choose the font encoding, which allows you to
use other languages than the Western ones (the standard fonts having too few available characters).
<BR>
<BR>
There are two ways to use a new font: embedding it in the PDF or not. When a font is not
embedded, it is sought in the system. The advantage is that the PDF file is lighter; on the other
hand, if it is not available, a substitution font is used. So it is preferable to ensure that the
needed font is installed on the client systems. If the file is to be viewed by a large audience,
it is better to embed.
<BR>
<BR>
Adding a new font requires three steps for TrueTypes:
<UL>
<LI>Generation of the metric file (.afm)
<LI>Generation of the font definition file (.php)
<LI>Declaration of the font in the script
</UL>
For Type1, the first one is theoretically not necessary because the AFM file is usually shipped
with the font. In case you have only a metric file in PFM format, use the convertor available
<A HREF="http://www.fpdf.org/fr/dl.php?id=34">here</A>.
<H4 CLASS='st'>Generation of the metric file</H4>
The first step for a TrueType consists in generating the AFM file. A utility exists to do this
task: <A HREF="http://ttf2pt1.sourceforge.net" TARGET="_BLANK">ttf2pt1</A>. The Windows binary
is available <A HREF="http://www.fpdf.org/fr/dl.php?id=22">here</A>. The command line to use is
the following:
<BR>
<BR>
<TT>ttf2pt1 -a font.ttf font</TT>
<BR>
<BR>
For example, for Comic Sans MS Regular:
<BR>
<BR>
<TT>ttf2pt1 -a c:\windows\fonts\comic.ttf comic</TT>
<BR>
<BR>
Two files are created; the one we are interested in is comic.afm.
<H4 CLASS='st'>Generation of the font definition file</H4>
The second step consists in generating a PHP file containing all the information needed by FPDF;
in addition, the font file is compressed. To do this, a helper script is provided in the font/makefont/
directory of the package: makefont.php. It contains the following function:
<BR>
<BR>
<TT>MakeFont(<B>string</B> fontfile, <B>string</B> afmfile [, <B>string</B> enc [, <B>array</B> patch [, <B>string</B> type]]])</TT>
<BR>
<BR>
<TT><U>fontfile</U></TT>
<BLOCKQUOTE>
Path to the .ttf or .pfb file.
</BLOCKQUOTE>
<TT><U>afmfile</U></TT>
<BLOCKQUOTE>
Path to the .afm file.
</BLOCKQUOTE>
<TT><U>enc</U></TT>
<BLOCKQUOTE>
Name of the encoding to use. Default value: <TT>cp1252</TT>.
</BLOCKQUOTE>
<TT><U>patch</U></TT>
<BLOCKQUOTE>
Optional modification of the encoding. Empty by default.
</BLOCKQUOTE>
<TT><U>type</U></TT>
<BLOCKQUOTE>
Type of the font (<TT>TrueType</TT> or <TT>Type1</TT>). Default value: <TT>TrueType</TT>.
</BLOCKQUOTE>
<BR>
The first parameter is the name of the font file. The extension must be either .ttf or .pfb and
determines the font type. If you own a Type1 font in ASCII format (.pfa), you can convert it to
binary format with <A HREF="http://www.lcdf.org/~eddietwo/type/#t1utils" TARGET="_blank">t1utils</A>.
<BR>
If you don't want to embed the font, pass an empty string. In this case, type is given by the
<TT>type</TT> parameter.
<BR>
Note: in the case of a font with the same name as a standard one, for instance arial.ttf, it is
mandatory to embed. If you don't, Acrobat will use its own font.
<BR>
<BR>
The AFM file is the one previously generated.
<BR>
<BR>
The encoding defines the association between a code (from 0 to 255) and a character. The first
128 are fixed and correspond to ASCII; the following are variable. The encodings are stored in
.map files. Those available are:
<UL>
<LI>cp1250 (Central Europe)
<LI>cp1251 (Cyrillic)
<LI>cp1252 (Western Europe)
<LI>cp1253 (Greek)
<LI>cp1257 (Baltic)
<LI>ISO-8859-1 (Western Europe)
<LI>ISO-8859-2 (Central Europe)
<LI>ISO-8859-4 (Baltic)
<LI>ISO-8859-5 (Cyrillic)
<LI>ISO-8859-7 (Greek)
<LI>ISO-8859-15 (Western Europe)
<LI>ISO-8859-16 (Central Europe)
<LI>KOI8-R (Cyrillic)
</UL>
Of course, the font must contain the characters corresponding to the chosen encoding.
<BR>
In the particular case of a symbolic font (that is to say which does not contain letters, such
as Symbol or ZapfDingbats), pass an empty string.
<BR>
The encodings which begin with cp are those used by Windows; Linux systems usually use ISO.
<BR>
Remark: the standard fonts use cp1252.
<BR>
<BR>
The fourth parameter gives the possibility to alter the encoding. Sometimes you may want to add
some characters. For instance, ISO-8859-1 does not contain the euro symbol. To add it at position
164, pass <TT>array(164=>'Euro')</TT>.
<BR>
<BR>
The last parameter is used to give the type of the font in case it is not embedded (that is to
say the first parameter is empty).
<BR>
<BR>
After you have called the function (create a new file for this and include makefont.php, or
simply add the call directly inside), a .php file is created, with the same name as the .afm one.
You may rename it if you wish. If the case of embedding, the font file is compressed and gives a
second file with .z as extension (except if the compression function is not available, it
requires Zlib). You may rename it too, but in this case you have to alter the variable <TT>$file</TT>
in the .php file accordingly.
<BR>
<BR>
Example:
<BR>
<BR>
<TT>MakeFont('c:\\windows\\fonts\\comic.ttf','comic.afm','cp1252');</TT>
<BR>
<BR>
which gives the files comic.php and comic.z.
<BR>
<BR>
Then you have to copy the generated file(s) either in the directory of the script which will use
the font, or in the directory given by FPDF_FONTPATH if the constant is defined. If the font file
could not be compressed, copy the .ttf or .pfb instead of the .z.
<H4 CLASS='st'>Declaration of the font in the script</H4>
The last step is the most simple. You just need to call the <A HREF='../doc/addfont.htm'>AddFont()</A> method. For instance:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
$pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Comic'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font><font class="str">'comic.php'</font><font class="kw">);</font><br>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
or simply:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
$pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Comic'</font><font class="kw">);</font><br>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
And the font is now available (in regular and underlined styles), usable like the others. If we
had worked with Comic Sans MS Bold (comicbd.ttf), we would have put:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
$pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Comic'</font><font class="kw">,</font><font class="str">'B'</font><font class="kw">,</font><font class="str">'comicbd.php'</font><font class="kw">);</font><br>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
<H4 CLASS='st'>Example</H4>
Let's now see a small complete example. The font used is Calligrapher, available at
<A HREF="http://www.abstractfonts.com/fonts/" TARGET="_blank">www.abstractfonts.com</A> (a site
offering numerous free TrueType fonts). The first step is the generation of the AFM file:
<BR>
<BR>
<TT>ttf2pt1 -a calligra.ttf calligra</TT>
<BR>
<BR>
which gives calligra.afm (and calligra.t1a that we can delete). Then we generate the definition
file:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
<?php<br><font class="kw">require(</font><font class="str">'../font/makefont/makefont.php'</font><font class="kw">);<br><br></font>MakeFont<font class="kw">(</font><font class="str">'calligra.ttf'</font><font class="kw">,</font><font class="str">'calligra.afm'</font><font class="kw">);<br></font>?>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
The function call gives the following report:
<BR>
<BR>
<B>Warning:</B> character Euro is missing<BR>
<B>Warning:</B> character Zcaron is missing<BR>
<B>Warning:</B> character zcaron is missing<BR>
<B>Warning:</B> character eth is missing<BR>
Font file compressed (calligra.z)<BR>
Font definition file generated (calligra.php)<BR>
<BR>
The euro character is not present in the font (it is too old). Three other characters are missing
too, but we are not interested in them anyway.
<BR>
We can now copy the two files in the font directory and write the script:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
<?php<br>define<font class="kw">(</font><font class="str">'FPDF_FONTPATH'</font><font class="kw">,</font><font class="str">'font/'</font><font class="kw">);<br>require(</font><font class="str">'fpdf.php'</font><font class="kw">);<br><br></font>$pdf<font class="kw">=new </font>FPDF<font class="kw">();<br></font>$pdf<font class="kw">-></font>Open<font class="kw">();<br></font>$pdf<font class="kw">-></font>AddPage<font class="kw">();<br></font>$pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Calligrapher'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font><font class="str">'calligra.php'</font><font class="kw">);<br></font>$pdf<font class="kw">-></font>SetFont<font class="kw">(</font><font class="str">'Calligrapher'</font><font class="kw">,</font><font class="str">''</font><font class="kw">,</font>35<font class="kw">);<br></font>$pdf<font class="kw">-></font>Cell<font class="kw">(</font>0<font class="kw">,</font>10<font class="kw">,</font><font class="str">'Enjoy new fonts with FPDF!'</font><font class="kw">);<br></font>$pdf<font class="kw">-></font>Output<font class="kw">();<br></font>?>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
<SCRIPT>
<!--
if(document.location.href.indexOf('http:')==0)
{
document.write("<P CLASS='demo'><A HREF='tuto7.php' TARGET='_blank' CLASS='demo'>[Demo]</A></P>");
}
//-->
</SCRIPT>
<H4 CLASS='st'>About the euro symbol</H4>
The euro character is not present in all encodings, and is not always placed at the same position:
<BR>
<BR>
<STYLE>
TH {text-align:left; background:#E0EBFF}
TH, TD {padding-left:10px; padding-right:10px; border-bottom-width:0px; border-left-width:1px; border-right-width:0px; border-top-width:1px}
TR.alt0 {background:#FFFFEE}
TR.alt1 {background:#FFFFDE}
</STYLE>
<TABLE STYLE="margin-left:15px; border-style:outset" BORDER="2" CELLSPACING="0" CELLPADDING="2" BGCOLOR2="#FFFFEE">
<TR><TH CLASS="st">Encoding</TH><TH CLASS="st">Position</TH></TR>
<TR CLASS="alt0"><TD>cp1250</TD><TD>128<BR></TD></TR>
<TR CLASS="alt1"><TD>cp1251</TD><TD>136<BR></TD></TR>
<TR CLASS="alt0"><TD>cp1252</TD><TD>128<BR></TD></TR>
<TR CLASS="alt1"><TD>cp1253</TD><TD>128<BR></TD></TR>
<TR CLASS="alt0"><TD>cp1257</TD><TD>128<BR></TD></TR>
<TR CLASS="alt1"><TD>ISO-8859-1</TD><TD>absent<BR></TD></TR>
<TR CLASS="alt0"><TD>ISO-8859-2</TD><TD>absent<BR></TD></TR>
<TR CLASS="alt1"><TD>ISO-8859-4</TD><TD>absent<BR></TD></TR>
<TR CLASS="alt0"><TD>ISO-8859-5</TD><TD>absent<BR></TD></TR>
<TR CLASS="alt1"><TD>ISO-8859-7</TD><TD>absent<BR></TD></TR>
<TR CLASS="alt0"><TD>ISO-8859-15</TD><TD>164<BR></TD></TR>
<TR CLASS="alt1"><TD>ISO-8859-16</TD><TD>164<BR></TD></TR>
<TR CLASS="alt0"><TD>KOI8-R</TD><TD>absent<BR></TD></TR>
</TABLE>
<BR>
ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing
to do is using cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious
symbol.
<BR>
As for ISO-8859-2, it is possible to use ISO-8859-16 instead, but it contains many differences.
It is therefore simpler to patch the encoding to add the symbol to it, as explained above. The
same is true for the other encodings.
<H4 CLASS='st'>Font synthesis under Windows</H4>
When a TrueType font is not available in a given style, Windows is able to synthesize it from the
regular version. For instance, there is no Comic Sans MS Italic, but it can be built from Comic
Sans MS Regular. This feature can be used in a PDF file, but unfortunately requires that the
regular font be present in the system (you must not embed it). Here is how to do it:
<UL>
<LI>Generate the definition file for the regular font without embedding (you may rename it to
reflect the desired style)
<LI>Open it and append to the variable <TT>$name</TT> a comma followed by the desired style
(<TT>Italic</TT>, <TT>Bold</TT> or <TT>BoldItalic</TT>)
</UL>
For instance, for the file comici.php:
<BR>
<BR>
<TT>$name='ComicSansMS,Italic';</TT>
<BR>
<BR>
It can then be used normally:
<BR>
<BR>
<TABLE WIDTH="100%" STYLE="color:#4040C0; border-style:ridge" BORDERCOLORLIGHT="#B0B0E0" BORDERCOLORDARK="#000000" BORDER="2" CELLPADDING=6 CELLSPACING=0 BGCOLOR="#F0F5FF"><TR><TD style="border-width:0px">
<NOBR><code><font color="#000000">
$pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Comic'</font><font class="kw">,</font><font class="str">'I'</font><font class="kw">,</font><font class="str">'comici.php'</font><font class="kw">);</font><br>
</font>
</code></NOBR></TD></TR></TABLE><P></P>
<H4 CLASS='st'>Reducing the size of TrueType fonts</H4>
Font files are often quite voluminous (more than 100, even 200KB); this is due to the fact that
they contain the characters corresponding to many encodings. Zlib compression reduces them but
they remain fairly big. A technique exists to reduce them further. It consists in converting the
font to the Type1 format with ttf2pt1 by specifying the encoding you are interested in; all other
characters will be discarded.
<BR>
For instance, the arial.ttf font shipped with Windows 98 is 267KB (it contains 1296 characters).
After compression it gives 147. Let's convert it to Type1 by keeping only cp1250 characters:
<BR>
<BR>
<TT>ttf2pt1 -b -L cp1250.map c:\windows\fonts\arial.ttf arial</TT>
<BR>
<BR>
The .map files are located in the font/makefont/ directory of the package. The command produces
arial.pfb and arial.afm. The arial.pfb file is only 35KB, and 30KB after compression.
<BR>
<BR>
It is possible to go even further. If you are interested only by a subset of the encoding (you
probably don't need all 217 characters), you can open the .map file and remove the lines you are
not interested in. This will reduce the file size accordingly.
</BODY>
</HTML>