Formalismos. Departamento -> Carrera.
[CLab.git] / include / dompdf / load_font.php
blobbab357fae76cf253f27d2aa400d3d3c9a06d8212
1 #!/usr/bin/php
2 <?php
3 /**
4 * DOMPDF - PHP5 HTML to PDF renderer
6 * File: $RCSfile: load_font.php,v $
7 * Created on: 2004-06-23
9 * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca>
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.
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.
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
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.
31 * The latest version of DOMPDF might be available at:
32 * http://www.digitaljunkies.ca/dompdf
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
41 require_once("dompdf_config.inc.php");
43 /**
44 * @access private
46 define("_TTF2AFM", TTF2AFM . " -a -GAef -OW ");
48 if ( !file_exists(TTF2AFM) ) {
49 die("Unable to locate the ttf2afm / ttf2pt1 executable (checked " . TTF2AFM . ").\n");
53 /**
54 * Display command line usage
57 function usage() {
59 echo <<<EOD
61 Usage: {$_SERVER["argv"][0]} font_family n_file [b_file] [i_file] [bi_file]
63 font_family: the name of the font, e.g. Verdana, 'Times New Roman',
64 monospace, sans-serif.
66 n_file: the .pfb or .ttf file for the normal, non-bold, non-italic
67 face of the font.
69 {b|i|bi}_file: the files for each of the respective (bold, italic,
70 bold-italic) faces.
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.
80 Examples:
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
86 EOD;
90 if ( $_SERVER["argc"] < 3 ) {
91 usage();
92 die();
95 /**
96 * Installs a new font family
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.
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
109 function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
111 // Check if the base filename is readable
112 if ( !is_readable($normal) )
113 throw new DOMPDF_Exception("Unable to read '$normal'.");
115 $dir = dirname($normal);
116 list($file, $ext) = explode(".", basename($normal), 2); // subtract extension
118 // Try $file_Bold.$ext etc.
119 $ext = ".$ext";
121 if ( !isset($bold) || !is_readable($bold) ) {
122 $bold = $dir . "/" . $file . "_Bold" . $ext;
123 if ( !is_readable($bold) ) {
125 // Try $file . "b"
126 $bold = $dir . "/" . $file . "b" . $ext;
127 if ( !is_readable($bold) ) {
129 // Try $file . "B"
130 $bold = $dir . "/" . $file . "B" . $ext;
131 if ( !is_readable($bold) )
132 $bold = null;
137 if ( is_null($bold) )
138 echo ("Unable to find bold face file.\n");
140 if ( !isset($italic) || !is_readable($italic) ) {
141 $italic = $dir . "/" . $file . "_Italic" . $ext;
142 if ( !is_readable($italic) ) {
144 // Try $file . "i"
145 $italic = $dir . "/" . $file . "i" . $ext;
146 if ( !is_readable($italic) ) {
148 // Try $file . "I"
149 $italic = $dir . "/" . $file . "I" . $ext;
150 if ( !is_readable($italic) )
151 $italic = null;
156 if ( is_null($italic) )
157 echo ("Unable to find italic face file.\n");
159 if ( !isset($bold_italic) || !is_readable($bold_italic) ) {
160 $bold_italic = $dir . "/" . $file . "_Bold_Italic" . $ext;
162 if ( !is_readable($bold_italic) ) {
164 // Try $file . "bi"
165 $bold_italic = $dir . "/" . $file . "bi" . $ext;
166 if ( !is_readable($bold_italic) ) {
168 // Try $file . "BI"
169 $bold_italic = $dir . "/" . $file . "BI" . $ext;
170 if ( !is_readable($bold_italic) ) {
172 // Try $file . "ib"
173 $bold_italic = $dir . "/" . $file . "ib" . $ext;
174 if ( !is_readable($bold_italic) ) {
176 // Try $file . "IB"
177 $bold_italic = $dir . "/" . $file . "IB" . $ext;
178 if ( !is_readable($bold_italic) )
179 $bold_italic = null;
186 if ( is_null($bold_italic) )
187 echo ("Unable to find bold italic face file.\n");
189 $fonts = compact("normal", "bold", "italic", "bold_italic");
190 $entry = array();
192 if ( mb_strtolower($ext) === ".pfb" || mb_strtolower($ext) === ".ttf" ) {
194 // Copy the files to the font directory.
195 foreach ($fonts as $var => $src) {
197 if ( is_null($src) ) {
198 $entry[$var] = DOMPDF_FONT_DIR . basename($normal);
199 continue;
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");
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'.");
210 echo "Copying $src to $dest...\n";
212 if ( !copy($src, $dest) )
213 throw new DOMPDF_Exception("Unable to copy '$src' to '" . DOMPDF_FONT_DIR . "$dest'.");
215 $entry[$var] = $dest;
218 } else
219 throw new DOMPDF_Exception("Unable to process fonts of type '$ext'.");
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;
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 );
233 $entry[$var] = $dest;
238 // FIXME: how to generate afms from pfb?
240 // Store the fonts in the lookup table
241 Font_Metrics::set_font_family(mb_strtolower($fontname), $entry);
243 // Save the changes
244 Font_Metrics::save_font_families();
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;
253 install_font_family($_SERVER["argv"][1], $normal, $bold, $italic, $bold_italic);