source: branches/version-2_11-dev/data/module/fpdf/tutorial/tuto7.htm @ 20993

Revision 20993, 11.2 KB checked in by Seasoft, 13 years ago (diff)

#1374 (依存ライブラリのアップデート)

  • FPDF 1.6 -> 1.7
  • FPDI 1.4 -> 1.4.1 (配置パスをFPDFから分離)
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2<html>
3<head>
4<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
5<title>Adding new fonts and encoding support</title>
6<link type="text/css" rel="stylesheet" href="../fpdf.css">
7<style type="text/css">
8table {border-collapse:collapse; border-style:solid; border-width:2px; border-color:#A0A0A0 #000000 #000000 #A0A0A0}
9table {margin:1.4em 0 1.4em 1em}
10th {background-color:#E0EBFF; color:#900000; text-align:left}
11th, td {border:1px solid #808080; padding:2px 10px}
12tr.alt0 {background-color:#FFFFEE}
13tr.alt1 {background-color:#FFFFE0}
14</style>
15</head>
16<body>
17<h1>Adding new fonts and encoding support</h1>
18This tutorial explains how to use TrueType, OpenType and Type1 fonts so that you are not limited to
19the standard fonts any more. The other benefit is that you can choose the font encoding, which allows
20you to use other languages than the Western ones (the standard fonts having too few available characters).
21<br>
22<br>
23Remark: for OpenType, only the format based on TrueType is supported (not the one based on Type1).
24<br>
25<br>
26There are two ways to use a new font: embedding it in the PDF or not. When a font is not
27embedded, it is searched in the system. The advantage is that the PDF file is lighter; on the other
28hand, if it's not available, a substitution font is used. So it's preferable to ensure that the
29needed font is installed on the client systems. If the file is to be viewed by a large audience,
30it's highly recommended to embed.
31<br>
32<br>
33Adding a new font requires two steps:
34<ul>
35<li>Generation of the font definition file</li>
36<li>Declaration of the font in the script</li>
37</ul>
38For Type1, you need the corresponding AFM file. It's usually provided with the font.
39
40<h2>Generation of the font definition file</h2>
41The first step consists in generating a PHP file containing all the information needed by FPDF;
42in addition, the font file is compressed. To do this, a helper script is provided in the makefont
43directory of the package: makefont.php. It contains the following function:
44<br>
45<br>
46<code>MakeFont(<b>string</b> fontfile, [, <b>string</b> enc [, <b>boolean</b> embed]])</code>
47<dl class="param" style="margin-bottom:2em">
48<dt><code>fontfile</code></dt>
49<dd>
50<p>Path to the .ttf, .otf or .pfb file.</p>
51</dd>
52<dt><code>enc</code></dt>
53<dd>
54<p>Name of the encoding to use. Default value: <code>cp1252</code>.</p>
55</dd>
56<dt><code>embed</code></dt>
57<dd>
58<p>Whether to embed the font or not. Default value: <code>true</code>.</p>
59</dd>
60</dl>
61The first parameter is the name of the font file. The extension must be either .ttf, .otf or .pfb and
62determines the font type. If your Type1 font is in ASCII format (.pfa), you can convert it to binary
63(.pfb) with the help of <a href="http://www.lcdf.org/~eddietwo/type/#t1utils" target="_blank">t1utils</a>.
64<br>
65<br>
66For Type1 fonts, the corresponding .afm file must be present in the same directory.
67<br>
68<br>
69The encoding defines the association between a code (from 0 to 255) and a character. The first 128 are
70always the same and correspond to ASCII; the following are variable. Encodings are stored in .map
71files. The available ones are:
72<ul>
73<li>cp1250 (Central Europe)</li>
74<li>cp1251 (Cyrillic)</li>
75<li>cp1252 (Western Europe)</li>
76<li>cp1253 (Greek)</li>
77<li>cp1254 (Turkish)</li>
78<li>cp1255 (Hebrew)</li>
79<li>cp1257 (Baltic)</li>
80<li>cp1258 (Vietnamese)</li>
81<li>cp874 (Thai)</li>
82<li>ISO-8859-1 (Western Europe)</li>
83<li>ISO-8859-2 (Central Europe)</li>
84<li>ISO-8859-4 (Baltic)</li>
85<li>ISO-8859-5 (Cyrillic)</li>
86<li>ISO-8859-7 (Greek)</li>
87<li>ISO-8859-9 (Turkish)</li>
88<li>ISO-8859-11 (Thai)</li>
89<li>ISO-8859-15 (Western Europe)</li>
90<li>ISO-8859-16 (Central Europe)</li>
91<li>KOI8-R (Russian)</li>
92<li>KOI8-U (Ukrainian)</li>
93</ul>
94Of course, the font must contain the characters corresponding to the chosen encoding.
95<br>
96<br>
97Remark: the standard fonts use cp1252.
98<br>
99<br>
100After you have called the function (create a new file for this and include makefont.php), a .php file
101is created, with the same name as the font file. You may rename it if you wish. If the case of embedding,
102the font file is compressed and gives a second file with .z as extension (except if the compression
103function is not available, it requires Zlib). You may rename it too, but in this case you have to change
104the variable <code>$file</code> in the .php file accordingly.
105<br>
106<br>
107Example:
108<div class="source">
109<pre><code>&lt;?php
110<span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
111
112</span>MakeFont<span class="kw">(</span><span class="str">'c:\\Windows\\Fonts\\comic.ttf'</span><span class="kw">,</span><span class="str">'cp1252'</span><span class="kw">);
113</span>?&gt;</code></pre>
114</div>
115which gives the files comic.php and comic.z.
116<br>
117<br>
118Then copy the generated files to the font directory. If the font file could not be compressed, copy
119it directly instead of the .z version.
120<br>
121<br>
122Another way to call MakeFont() is through the command line:
123<br>
124<br>
125<kbd>php makefont\makefont.php c:\Windows\Fonts\comic.ttf cp1252</kbd>
126<br>
127<br>
128Finally, for TrueType and OpenType fonts, you can also generate the files
129<a href="http://www.fpdf.org/makefont/">online</a> instead of doing it manually.
130
131<h2>Declaration of the font in the script</h2>
132The second step is simple. You just need to call the <a href='../doc/addfont.htm'>AddFont()</a> method:
133<div class="source">
134<pre><code>$pdf<span class="kw">-&gt;</span>AddFont<span class="kw">(</span><span class="str">'Comic'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'comic.php'</span><span class="kw">);
135</span></code></pre>
136</div>
137And the font is now available (in regular and underlined styles), usable like the others. If we
138had worked with Comic Sans MS Bold (comicbd.ttf), we would have written:
139<div class="source">
140<pre><code>$pdf<span class="kw">-&gt;</span>AddFont<span class="kw">(</span><span class="str">'Comic'</span><span class="kw">,</span><span class="str">'B'</span><span class="kw">,</span><span class="str">'comicbd.php'</span><span class="kw">);
141</span></code></pre>
142</div>
143
144<h2>Example</h2>
145Let's now see a complete example. We will use the font <a href="http://www.abstractfonts.com/font/52" target="_blank">Calligrapher</a>.
146The first step is the generation of the font files:
147<div class="source">
148<pre><code>&lt;?php
149<span class="kw">require(</span><span class="str">'makefont/makefont.php'</span><span class="kw">);
150
151</span>MakeFont<span class="kw">(</span><span class="str">'calligra.ttf'</span><span class="kw">,</span><span class="str">'cp1252'</span><span class="kw">);
152</span>?&gt;</code></pre>
153</div>
154The script gives the following report:
155<br>
156<br>
157<b>Warning:</b> character Euro is missing<br>
158<b>Warning:</b> character zcaron is missing<br>
159Font file compressed: calligra.z<br>
160Font definition file generated: calligra.php<br>
161<br>
162The euro character is not present in the font (it's too old). Another character is missing too.
163<br>
164<br>
165Alternatively we could have used the command line:
166<br>
167<br>
168<kbd>php makefont\makefont.php calligra.ttf cp1252</kbd>
169<br>
170<br>
171or used the online generator.
172<br>
173<br>
174We can now copy the two generated files to the font directory and write the script:
175<div class="source">
176<pre><code>&lt;?php
177<span class="kw">require(</span><span class="str">'fpdf.php'</span><span class="kw">);
178
179</span>$pdf <span class="kw">= new </span>FPDF<span class="kw">();
180</span>$pdf<span class="kw">-&gt;</span>AddFont<span class="kw">(</span><span class="str">'Calligrapher'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span><span class="str">'calligra.php'</span><span class="kw">);
181</span>$pdf<span class="kw">-&gt;</span>AddPage<span class="kw">();
182</span>$pdf<span class="kw">-&gt;</span>SetFont<span class="kw">(</span><span class="str">'Calligrapher'</span><span class="kw">,</span><span class="str">''</span><span class="kw">,</span>35<span class="kw">);
183</span>$pdf<span class="kw">-&gt;</span>Write<span class="kw">(</span>10<span class="kw">,</span><span class="str">'Enjoy new fonts with FPDF!'</span><span class="kw">);
184</span>$pdf<span class="kw">-&gt;</span>Output<span class="kw">();
185</span>?&gt;</code></pre>
186</div>
187<p class='demo'><a href='tuto7.php' target='_blank' class='demo'>[Demo]</a></p>
188
189<h2>About the euro symbol</h2>
190The euro character is not present in all encodings, and is not always placed at the same position:
191<table>
192<tr><th>Encoding</th><th>Position</th></tr>
193<tr class="alt0"><td>cp1250</td><td>128</td></tr>
194<tr class="alt1"><td>cp1251</td><td>136</td></tr>
195<tr class="alt0"><td>cp1252</td><td>128</td></tr>
196<tr class="alt1"><td>cp1253</td><td>128</td></tr>
197<tr class="alt0"><td>cp1254</td><td>128</td></tr>
198<tr class="alt1"><td>cp1255</td><td>128</td></tr>
199<tr class="alt0"><td>cp1257</td><td>128</td></tr>
200<tr class="alt1"><td>cp1258</td><td>128</td></tr>
201<tr class="alt0"><td>cp874</td><td>128</td></tr>
202<tr class="alt1"><td>ISO-8859-1</td><td>N/A</td></tr>
203<tr class="alt0"><td>ISO-8859-2</td><td>N/A</td></tr>
204<tr class="alt1"><td>ISO-8859-4</td><td>N/A</td></tr>
205<tr class="alt0"><td>ISO-8859-5</td><td>N/A</td></tr>
206<tr class="alt1"><td>ISO-8859-7</td><td>N/A</td></tr>
207<tr class="alt0"><td>ISO-8859-9</td><td>N/A</td></tr>
208<tr class="alt1"><td>ISO-8859-11</td><td>N/A</td></tr>
209<tr class="alt0"><td>ISO-8859-15</td><td>164</td></tr>
210<tr class="alt1"><td>ISO-8859-16</td><td>164</td></tr>
211<tr class="alt0"><td>KOI8-R</td><td>N/A</td></tr>
212<tr class="alt1"><td>KOI8-U</td><td>N/A</td></tr>
213</table>
214ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing
215to do is to use cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious
216symbol.
217
218<h2>Reducing the size of TrueType fonts</h2>
219Font files are often quite voluminous; this is due to the fact that they contain the characters
220corresponding to many encodings. Zlib compression reduces them but they remain fairly big. A
221technique exists to reduce them further. It consists in converting the font to the Type1 format
222with <a href="http://ttf2pt1.sourceforge.net" target="_blank">ttf2pt1</a> (the Windows binary is
223available <a href="http://www.fpdf.org/fr/dl.php?id=22">here</a>) while specifying the encoding
224you are interested in; all other characters will be discarded.
225<br>
226For example, the arial.ttf font that ships with Windows Vista weights 748 KB (it contains 3381 characters).
227After compression it drops to 411. Let's convert it to Type1 by keeping only cp1250 characters:
228<br>
229<br>
230<kbd>ttf2pt1 -b -L cp1250.map c:\Windows\Fonts\arial.ttf arial</kbd>
231<br>
232<br>
233The .map files are located in the makefont directory of the package. The command produces
234arial.pfb and arial.afm. The arial.pfb file weights only 57 KB, and 53 after compression.
235<br>
236<br>
237It's possible to go even further. If you are interested only by a subset of the encoding (you
238probably don't need all 217 characters), you can open the .map file and remove the lines you are
239not interested in. This will reduce the file size accordingly.
240</body>
241</html>
Note: See TracBrowser for help on using the repository browser.