[ Index ] |
|
Code source de eGroupWare 1.2.106-2 |
1 <HTML> 2 <HEAD> 3 <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1"> 4 <TITLE>Adding new fonts and encoding support</TITLE> 5 <LINK TYPE="text/css" REL="stylesheet" HREF="../fpdf.css"> 6 </HEAD> 7 <BODY> 8 <H2>Adding new fonts and encoding support</H2> 9 This tutorial explains how to use TrueType or Type1 fonts so that you are not limited to the standard 10 fonts any more. The other interest is that you can choose the font encoding, which allows you to 11 use other languages than the Western ones (the standard fonts having too few available characters). 12 <BR> 13 <BR> 14 There are two ways to use a new font: embedding it in the PDF or not. When a font is not 15 embedded, it is sought in the system. The advantage is that the PDF file is lighter; on the other 16 hand, if it is not available, a substitution font is used. So it is preferable to ensure that the 17 needed font is installed on the client systems. If the file is to be viewed by a large audience, 18 it is better to embed. 19 <BR> 20 <BR> 21 Adding a new font requires three steps for TrueTypes: 22 <UL> 23 <LI>Generation of the metric file (.afm) 24 <LI>Generation of the font definition file (.php) 25 <LI>Declaration of the font in the script 26 </UL> 27 For Type1, the first one is theoretically not necessary because the AFM file is usually shipped 28 with the font. In case you have only a metric file in PFM format, use the convertor available 29 <A HREF="http://www.fpdf.org/fr/dl.php?id=34">here</A>. 30 <H4 CLASS='st'>Generation of the metric file</H4> 31 The first step for a TrueType consists in generating the AFM file. A utility exists to do this 32 task: <A HREF="http://ttf2pt1.sourceforge.net" TARGET="_blank">ttf2pt1</A>. The Windows binary 33 is available <A HREF="http://www.fpdf.org/fr/dl.php?id=22">here</A>. The command line to use is 34 the following: 35 <BR> 36 <BR> 37 <TT>ttf2pt1 -a font.ttf font</TT> 38 <BR> 39 <BR> 40 For example, for Comic Sans MS Regular: 41 <BR> 42 <BR> 43 <TT>ttf2pt1 -a c:\windows\fonts\comic.ttf comic</TT> 44 <BR> 45 <BR> 46 Two files are created; the one we are interested in is comic.afm. 47 <H4 CLASS='st'>Generation of the font definition file</H4> 48 The second step consists in generating a PHP file containing all the information needed by FPDF; 49 in addition, the font file is compressed. To do this, a helper script is provided in the font/makefont/ 50 directory of the package: makefont.php. It contains the following function: 51 <BR> 52 <BR> 53 <TT>MakeFont(<B>string</B> fontfile, <B>string</B> afmfile [, <B>string</B> enc [, <B>array</B> patch [, <B>string</B> type]]])</TT> 54 <BR> 55 <BR> 56 <TT><U>fontfile</U></TT> 57 <BLOCKQUOTE> 58 Path to the .ttf or .pfb file. 59 </BLOCKQUOTE> 60 <TT><U>afmfile</U></TT> 61 <BLOCKQUOTE> 62 Path to the .afm file. 63 </BLOCKQUOTE> 64 <TT><U>enc</U></TT> 65 <BLOCKQUOTE> 66 Name of the encoding to use. Default value: <TT>cp1252</TT>. 67 </BLOCKQUOTE> 68 <TT><U>patch</U></TT> 69 <BLOCKQUOTE> 70 Optional modification of the encoding. Empty by default. 71 </BLOCKQUOTE> 72 <TT><U>type</U></TT> 73 <BLOCKQUOTE> 74 Type of the font (<TT>TrueType</TT> or <TT>Type1</TT>). Default value: <TT>TrueType</TT>. 75 </BLOCKQUOTE> 76 <BR> 77 The first parameter is the name of the font file. The extension must be either .ttf or .pfb and 78 determines the font type. If you own a Type1 font in ASCII format (.pfa), you can convert it to 79 binary format with <A HREF="http://www.lcdf.org/~eddietwo/type/#t1utils" TARGET="_blank">t1utils</A>. 80 <BR> 81 If you don't want to embed the font, pass an empty string. In this case, type is given by the 82 <TT>type</TT> parameter. 83 <BR> 84 Note: in the case of a font with the same name as a standard one, for instance arial.ttf, it is 85 mandatory to embed. If you don't, Acrobat will use its own font. 86 <BR> 87 <BR> 88 The AFM file is the one previously generated. 89 <BR> 90 <BR> 91 The encoding defines the association between a code (from 0 to 255) and a character. The first 92 128 are fixed and correspond to ASCII; the following are variable. The encodings are stored in 93 .map files. Those available are: 94 <UL> 95 <LI>cp1250 (Central Europe) 96 <LI>cp1251 (Cyrillic) 97 <LI>cp1252 (Western Europe) 98 <LI>cp1253 (Greek) 99 <LI>cp1254 (Turkish) 100 <LI>cp1255 (Hebrew) 101 <LI>cp1257 (Baltic) 102 <LI>cp1258 (Vietnamese) 103 <LI>cp874 (Thai) 104 <LI>ISO-8859-1 (Western Europe) 105 <LI>ISO-8859-2 (Central Europe) 106 <LI>ISO-8859-4 (Baltic) 107 <LI>ISO-8859-5 (Cyrillic) 108 <LI>ISO-8859-7 (Greek) 109 <LI>ISO-8859-9 (Turkish) 110 <LI>ISO-8859-11 (Thai) 111 <LI>ISO-8859-15 (Western Europe) 112 <LI>ISO-8859-16 (Central Europe) 113 <LI>KOI8-R (Russian) 114 <LI>KOI8-U (Ukrainian) 115 </UL> 116 Of course, the font must contain the characters corresponding to the chosen encoding. 117 <BR> 118 In the particular case of a symbolic font (that is to say which does not contain letters, such 119 as Symbol or ZapfDingbats), pass an empty string. 120 <BR> 121 The encodings which begin with cp are those used by Windows; Linux systems usually use ISO. 122 <BR> 123 Remark: the standard fonts use cp1252. 124 <BR> 125 <BR> 126 The fourth parameter gives the possibility to alter the encoding. Sometimes you may want to add 127 some characters. For instance, ISO-8859-1 does not contain the euro symbol. To add it at position 128 164, pass <TT>array(164=>'Euro')</TT>. 129 <BR> 130 <BR> 131 The last parameter is used to give the type of the font in case it is not embedded (that is to 132 say the first parameter is empty). 133 <BR> 134 <BR> 135 After you have called the function (create a new file for this and include makefont.php, or 136 simply add the call directly inside), a .php file is created, with the same name as the .afm one. 137 You may rename it if you wish. If the case of embedding, the font file is compressed and gives a 138 second file with .z as extension (except if the compression function is not available, it 139 requires Zlib). You may rename it too, but in this case you have to alter the variable <TT>$file</TT> 140 in the .php file accordingly. 141 <BR> 142 <BR> 143 Example: 144 <BR> 145 <BR> 146 <TT>MakeFont('c:\\windows\\fonts\\comic.ttf','comic.afm','cp1252');</TT> 147 <BR> 148 <BR> 149 which gives the files comic.php and comic.z. 150 <BR> 151 <BR> 152 Then you have to copy the generated file(s) either in the directory of the script which will use 153 the font, or in the directory given by FPDF_FONTPATH if the constant is defined. If the font file 154 could not be compressed, copy the .ttf or .pfb instead of the .z. 155 <H4 CLASS='st'>Declaration of the font in the script</H4> 156 The last step is the most simple. You just need to call the <A HREF='../doc/addfont.htm'>AddFont()</A> method. For instance: 157 <BR> 158 <BR> 159 <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"> 160 <NOBR><code><font color="#000000"> 161 $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> 162 </font> 163 </code></NOBR></TD></TR></TABLE><P></P> 164 or simply: 165 <BR> 166 <BR> 167 <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"> 168 <NOBR><code><font color="#000000"> 169 $pdf<font class="kw">-></font>AddFont<font class="kw">(</font><font class="str">'Comic'</font><font class="kw">);</font><br> 170 </font> 171 </code></NOBR></TD></TR></TABLE><P></P> 172 And the font is now available (in regular and underlined styles), usable like the others. If we 173 had worked with Comic Sans MS Bold (comicbd.ttf), we would have put: 174 <BR> 175 <BR> 176 <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"> 177 <NOBR><code><font color="#000000"> 178 $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> 179 </font> 180 </code></NOBR></TD></TR></TABLE><P></P> 181 <H4 CLASS='st'>Example</H4> 182 Let's now see a small complete example. The font used is Calligrapher, available at 183 <A HREF="http://www.abstractfonts.com/fonts/" TARGET="_blank">www.abstractfonts.com</A> (a site 184 offering numerous free TrueType fonts). The first step is the generation of the AFM file: 185 <BR> 186 <BR> 187 <TT>ttf2pt1 -a calligra.ttf calligra</TT> 188 <BR> 189 <BR> 190 which gives calligra.afm (and calligra.t1a that we can delete). Then we generate the definition 191 file: 192 <BR> 193 <BR> 194 <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"> 195 <NOBR><code><font color="#000000"> 196 <?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>?> 197 </font> 198 </code></NOBR></TD></TR></TABLE><P></P> 199 The function call gives the following report: 200 <BR> 201 <BR> 202 <B>Warning:</B> character Euro is missing<BR> 203 <B>Warning:</B> character Zcaron is missing<BR> 204 <B>Warning:</B> character zcaron is missing<BR> 205 <B>Warning:</B> character eth is missing<BR> 206 Font file compressed (calligra.z)<BR> 207 Font definition file generated (calligra.php)<BR> 208 <BR> 209 The euro character is not present in the font (it is too old). Three other characters are missing 210 too, but we are not interested in them anyway. 211 <BR> 212 We can now copy the two files in the font directory and write the script: 213 <BR> 214 <BR> 215 <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"> 216 <NOBR><code><font color="#000000"> 217 <?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>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>AddPage<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>?> 218 </font> 219 </code></NOBR></TD></TR></TABLE><P></P> 220 <SCRIPT> 221 <!-- 222 if(document.location.href.indexOf('http:')==0) 223 { 224 document.write("<P CLASS='demo'><A HREF='tuto7.php' TARGET='_blank' CLASS='demo'>[Demo]</A></P>"); 225 } 226 //--> 227 </SCRIPT> 228 <H4 CLASS='st'>About the euro symbol</H4> 229 The euro character is not present in all encodings, and is not always placed at the same position: 230 <BR> 231 <BR> 232 <STYLE> 233 TH {text-align:left; background:#E0EBFF} 234 TH, TD {padding-left:10px; padding-right:10px; border-bottom-width:0px; border-left-width:1px; border-right-width:0px; border-top-width:1px} 235 TR.alt0 {background:#FFFFEE} 236 TR.alt1 {background:#FFFFDF} 237 </STYLE> 238 <TABLE STYLE="margin-left:15px; border-style:outset" BORDER="2" CELLSPACING="0" CELLPADDING="2" BGCOLOR2="#FFFFEE"> 239 <TR><TH CLASS="st">Encoding</TH><TH CLASS="st">Position</TH></TR> 240 <TR CLASS="alt0"><TD>cp1250</TD><TD>128<BR></TD></TR> 241 <TR CLASS="alt1"><TD>cp1251</TD><TD>136<BR></TD></TR> 242 <TR CLASS="alt0"><TD>cp1252</TD><TD>128<BR></TD></TR> 243 <TR CLASS="alt1"><TD>cp1253</TD><TD>128<BR></TD></TR> 244 <TR CLASS="alt0"><TD>cp1254</TD><TD>128<BR></TD></TR> 245 <TR CLASS="alt1"><TD>cp1255</TD><TD>128<BR></TD></TR> 246 <TR CLASS="alt0"><TD>cp1257</TD><TD>128<BR></TD></TR> 247 <TR CLASS="alt1"><TD>cp1258</TD><TD>128<BR></TD></TR> 248 <TR CLASS="alt0"><TD>cp874</TD><TD>128<BR></TD></TR> 249 <TR CLASS="alt1"><TD>ISO-8859-1</TD><TD>absent<BR></TD></TR> 250 <TR CLASS="alt0"><TD>ISO-8859-2</TD><TD>absent<BR></TD></TR> 251 <TR CLASS="alt1"><TD>ISO-8859-4</TD><TD>absent<BR></TD></TR> 252 <TR CLASS="alt0"><TD>ISO-8859-5</TD><TD>absent<BR></TD></TR> 253 <TR CLASS="alt1"><TD>ISO-8859-7</TD><TD>absent<BR></TD></TR> 254 <TR CLASS="alt0"><TD>ISO-8859-9</TD><TD>absent<BR></TD></TR> 255 <TR CLASS="alt1"><TD>ISO-8859-11</TD><TD>absent<BR></TD></TR> 256 <TR CLASS="alt0"><TD>ISO-8859-15</TD><TD>164<BR></TD></TR> 257 <TR CLASS="alt1"><TD>ISO-8859-16</TD><TD>164<BR></TD></TR> 258 <TR CLASS="alt0"><TD>KOI8-R</TD><TD>absent<BR></TD></TR> 259 <TR CLASS="alt1"><TD>KOI8-U</TD><TD>absent<BR></TD></TR> 260 </TABLE> 261 <BR> 262 ISO-8859-1 is widespread but does not include the euro sign. If you need it, the simplest thing 263 to do is using cp1252 or ISO-8859-15 instead, which are nearly identical but contain the precious 264 symbol. 265 <BR> 266 As for ISO-8859-2, it is possible to use ISO-8859-16 instead, but it contains many differences. 267 It is therefore simpler to patch the encoding to add the symbol to it, as explained above. The 268 same is true for the other encodings. 269 <H4 CLASS='st'>Font synthesis under Windows</H4> 270 When a TrueType font is not available in a given style, Windows is able to synthesize it from the 271 regular version. For instance, there is no Comic Sans MS Italic, but it can be built from Comic 272 Sans MS Regular. This feature can be used in a PDF file, but unfortunately requires that the 273 regular font be present in the system (you must not embed it). Here is how to do it: 274 <UL> 275 <LI>Generate the definition file for the regular font without embedding (you may rename it to 276 reflect the desired style) 277 <LI>Open it and append to the variable <TT>$name</TT> a comma followed by the desired style 278 (<TT>Italic</TT>, <TT>Bold</TT> or <TT>BoldItalic</TT>) 279 </UL> 280 For instance, for the file comici.php: 281 <BR> 282 <BR> 283 <TT>$name='ComicSansMS,Italic';</TT> 284 <BR> 285 <BR> 286 It can then be used normally: 287 <BR> 288 <BR> 289 <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"> 290 <NOBR><code><font color="#000000"> 291 $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> 292 </font> 293 </code></NOBR></TD></TR></TABLE><P></P> 294 <H4 CLASS='st'>Reducing the size of TrueType fonts</H4> 295 Font files are often quite voluminous (more than 100, even 200KB); this is due to the fact that 296 they contain the characters corresponding to many encodings. Zlib compression reduces them but 297 they remain fairly big. A technique exists to reduce them further. It consists in converting the 298 font to the Type1 format with ttf2pt1 by specifying the encoding you are interested in; all other 299 characters will be discarded. 300 <BR> 301 For instance, the arial.ttf font shipped with Windows 98 is 267KB (it contains 1296 characters). 302 After compression it gives 147. Let's convert it to Type1 by keeping only cp1250 characters: 303 <BR> 304 <BR> 305 <TT>ttf2pt1 -b -L cp1250.map c:\windows\fonts\arial.ttf arial</TT> 306 <BR> 307 <BR> 308 The .map files are located in the font/makefont/ directory of the package. The command produces 309 arial.pfb and arial.afm. The arial.pfb file is only 35KB, and 30KB after compression. 310 <BR> 311 <BR> 312 It is possible to go even further. If you are interested only by a subset of the encoding (you 313 probably don't need all 217 characters), you can open the .map file and remove the lines you are 314 not interested in. This will reduce the file size accordingly. 315 </BODY> 316 </HTML>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 17:20:01 2007 | par Balluche grâce à PHPXref 0.7 |