1 | #!/usr/bin/php |
---|
2 | <?php |
---|
3 | /** |
---|
4 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
5 | * |
---|
6 | * File: $RCSfile: load_font.php,v $ |
---|
7 | * Created on: 2004-06-23 |
---|
8 | * |
---|
9 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
10 | * |
---|
11 | * This library is free software; you can redistribute it and/or |
---|
12 | * modify it under the terms of the GNU Lesser General Public |
---|
13 | * License as published by the Free Software Foundation; either |
---|
14 | * version 2.1 of the License, or (at your option) any later version. |
---|
15 | * |
---|
16 | * This library is distributed in the hope that it will be useful, |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
19 | * Lesser General Public License for more details. |
---|
20 | * |
---|
21 | * You should have received a copy of the GNU Lesser General Public License |
---|
22 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
23 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
24 | * 02111-1307 USA |
---|
25 | * |
---|
26 | * Alternatively, you may distribute this software under the terms of the |
---|
27 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
28 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
29 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
30 | * |
---|
31 | * The latest version of DOMPDF might be available at: |
---|
32 | * http://www.digitaljunkies.ca/dompdf |
---|
33 | * |
---|
34 | * @link http://www.digitaljunkies.ca/dompdf |
---|
35 | * @copyright 2004 Benj Carson |
---|
36 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
37 | * @package dompdf |
---|
38 | * @version 0.5.1 |
---|
39 | */ |
---|
40 | |
---|
41 | require_once("dompdf_config.inc.php"); |
---|
42 | |
---|
43 | /** |
---|
44 | * @access private |
---|
45 | */ |
---|
46 | define("_TTF2AFM", TTF2AFM . " -a -GAef -OW "); |
---|
47 | |
---|
48 | if ( !file_exists(TTF2AFM) ) { |
---|
49 | die("Unable to locate the ttf2afm / ttf2pt1 executable (checked " . TTF2AFM . ").\n"); |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | /** |
---|
54 | * Display command line usage |
---|
55 | * |
---|
56 | */ |
---|
57 | function usage() { |
---|
58 | |
---|
59 | echo <<<EOD |
---|
60 | |
---|
61 | Usage: {$_SERVER["argv"][0]} font_family n_file [b_file] [i_file] [bi_file] |
---|
62 | |
---|
63 | font_family: the name of the font, e.g. Verdana, 'Times New Roman', |
---|
64 | monospace, sans-serif. |
---|
65 | |
---|
66 | n_file: the .pfb or .ttf file for the normal, non-bold, non-italic |
---|
67 | face of the font. |
---|
68 | |
---|
69 | {b|i|bi}_file: the files for each of the respective (bold, italic, |
---|
70 | bold-italic) faces. |
---|
71 | |
---|
72 | |
---|
73 | If the optional b|i|bi files are not specified, load_font.php will search |
---|
74 | the directory containing normal font file (n_file) for additional files that |
---|
75 | it thinks might be the correct ones (e.g. that end in _Bold or b or B). If |
---|
76 | it finds the files they will also be processed. All files will be |
---|
77 | automatically copied to the DOMPDF font directory, and afm files will be |
---|
78 | generated using ttf2afm. |
---|
79 | |
---|
80 | Examples: |
---|
81 | |
---|
82 | ./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf |
---|
83 | ./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf |
---|
84 | |
---|
85 | |
---|
86 | EOD; |
---|
87 | |
---|
88 | } |
---|
89 | |
---|
90 | if ( $_SERVER["argc"] < 3 ) { |
---|
91 | usage(); |
---|
92 | die(); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Installs a new font family |
---|
97 | * |
---|
98 | * This function maps a font-family name to a font. It tries to locate the |
---|
99 | * bold, italic, and bold italic versions of the font as well. Once the |
---|
100 | * files are located, ttf versions of the font are copied to the fonts |
---|
101 | * directory. Changes to the font lookup table are saved to the cache. |
---|
102 | * |
---|
103 | * @param string $fontname the font-family name |
---|
104 | * @param string $normal the filename of the normal face font subtype |
---|
105 | * @param string $bold the filename of the bold face font subtype |
---|
106 | * @param string $italic the filename of the italic face font subtype |
---|
107 | * @param string $bold_italic the filename of the bold italic face font subtype |
---|
108 | */ |
---|
109 | function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { |
---|
110 | |
---|
111 | // Check if the base filename is readable |
---|
112 | if ( !is_readable($normal) ) |
---|
113 | throw new DOMPDF_Exception("Unable to read '$normal'."); |
---|
114 | |
---|
115 | $dir = dirname($normal); |
---|
116 | list($file, $ext) = explode(".", basename($normal), 2); // subtract extension |
---|
117 | |
---|
118 | // Try $file_Bold.$ext etc. |
---|
119 | $ext = ".$ext"; |
---|
120 | |
---|
121 | if ( !isset($bold) || !is_readable($bold) ) { |
---|
122 | $bold = $dir . "/" . $file . "_Bold" . $ext; |
---|
123 | if ( !is_readable($bold) ) { |
---|
124 | |
---|
125 | // Try $file . "b" |
---|
126 | $bold = $dir . "/" . $file . "b" . $ext; |
---|
127 | if ( !is_readable($bold) ) { |
---|
128 | |
---|
129 | // Try $file . "B" |
---|
130 | $bold = $dir . "/" . $file . "B" . $ext; |
---|
131 | if ( !is_readable($bold) ) |
---|
132 | $bold = null; |
---|
133 | } |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | if ( is_null($bold) ) |
---|
138 | echo ("Unable to find bold face file.\n"); |
---|
139 | |
---|
140 | if ( !isset($italic) || !is_readable($italic) ) { |
---|
141 | $italic = $dir . "/" . $file . "_Italic" . $ext; |
---|
142 | if ( !is_readable($italic) ) { |
---|
143 | |
---|
144 | // Try $file . "i" |
---|
145 | $italic = $dir . "/" . $file . "i" . $ext; |
---|
146 | if ( !is_readable($italic) ) { |
---|
147 | |
---|
148 | // Try $file . "I" |
---|
149 | $italic = $dir . "/" . $file . "I" . $ext; |
---|
150 | if ( !is_readable($italic) ) |
---|
151 | $italic = null; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | if ( is_null($italic) ) |
---|
157 | echo ("Unable to find italic face file.\n"); |
---|
158 | |
---|
159 | if ( !isset($bold_italic) || !is_readable($bold_italic) ) { |
---|
160 | $bold_italic = $dir . "/" . $file . "_Bold_Italic" . $ext; |
---|
161 | |
---|
162 | if ( !is_readable($bold_italic) ) { |
---|
163 | |
---|
164 | // Try $file . "bi" |
---|
165 | $bold_italic = $dir . "/" . $file . "bi" . $ext; |
---|
166 | if ( !is_readable($bold_italic) ) { |
---|
167 | |
---|
168 | // Try $file . "BI" |
---|
169 | $bold_italic = $dir . "/" . $file . "BI" . $ext; |
---|
170 | if ( !is_readable($bold_italic) ) { |
---|
171 | |
---|
172 | // Try $file . "ib" |
---|
173 | $bold_italic = $dir . "/" . $file . "ib" . $ext; |
---|
174 | if ( !is_readable($bold_italic) ) { |
---|
175 | |
---|
176 | // Try $file . "IB" |
---|
177 | $bold_italic = $dir . "/" . $file . "IB" . $ext; |
---|
178 | if ( !is_readable($bold_italic) ) |
---|
179 | $bold_italic = null; |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | if ( is_null($bold_italic) ) |
---|
187 | echo ("Unable to find bold italic face file.\n"); |
---|
188 | |
---|
189 | $fonts = compact("normal", "bold", "italic", "bold_italic"); |
---|
190 | $entry = array(); |
---|
191 | |
---|
192 | if ( mb_strtolower($ext) === ".pfb" || mb_strtolower($ext) === ".ttf" ) { |
---|
193 | |
---|
194 | // Copy the files to the font directory. |
---|
195 | foreach ($fonts as $var => $src) { |
---|
196 | |
---|
197 | if ( is_null($src) ) { |
---|
198 | $entry[$var] = DOMPDF_FONT_DIR . basename($normal); |
---|
199 | continue; |
---|
200 | } |
---|
201 | |
---|
202 | // Verify that the fonts exist and are readable |
---|
203 | if ( !is_readable($src) ) |
---|
204 | throw new User_DOMPDF_Exception("Requested font '$pathname' is not readable"); |
---|
205 | |
---|
206 | $dest = DOMPDF_FONT_DIR . basename($src); |
---|
207 | if ( !is_writeable(dirname($dest)) ) |
---|
208 | throw new User_DOMPDF_Exception("Unable to write to destination '$dest'."); |
---|
209 | |
---|
210 | echo "Copying $src to $dest...\n"; |
---|
211 | |
---|
212 | if ( !copy($src, $dest) ) |
---|
213 | throw new DOMPDF_Exception("Unable to copy '$src' to '" . DOMPDF_FONT_DIR . "$dest'."); |
---|
214 | |
---|
215 | $entry[$var] = $dest; |
---|
216 | } |
---|
217 | |
---|
218 | } else |
---|
219 | throw new DOMPDF_Exception("Unable to process fonts of type '$ext'."); |
---|
220 | |
---|
221 | |
---|
222 | // If the extension is a ttf, try and convert the fonts to afm too |
---|
223 | if ( mb_strtolower($ext) === ".ttf") { |
---|
224 | foreach ($fonts as $var => $font) { |
---|
225 | if ( is_null($font) ) { |
---|
226 | $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); |
---|
227 | continue; |
---|
228 | } |
---|
229 | $dest = DOMPDF_FONT_DIR . mb_substr(basename($font),0, -4); |
---|
230 | echo "Generating .afm for $font...\n"; |
---|
231 | exec( _TTF2AFM . " " . escapeshellarg($font) . " " . $dest . " &> /dev/null", $output, $ret ); |
---|
232 | |
---|
233 | $entry[$var] = $dest; |
---|
234 | } |
---|
235 | |
---|
236 | } |
---|
237 | |
---|
238 | // FIXME: how to generate afms from pfb? |
---|
239 | |
---|
240 | // Store the fonts in the lookup table |
---|
241 | Font_Metrics::set_font_family(mb_strtolower($fontname), $entry); |
---|
242 | |
---|
243 | // Save the changes |
---|
244 | Font_Metrics::save_font_families(); |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | $normal = $_SERVER["argv"][2]; |
---|
249 | $bold = isset($_SERVER["argv"][3]) ? $_SERVER["argv"][3] : null; |
---|
250 | $italic = isset($_SERVER["argv"][4]) ? $_SERVER["argv"][4] : null; |
---|
251 | $bold_italic = isset($_SERVER["argv"][5]) ? $_SERVER["argv"][5] : null; |
---|
252 | |
---|
253 | install_font_family($_SERVER["argv"][1], $normal, $bold, $italic, $bold_italic); |
---|
254 | |
---|
255 | ?> |
---|