load_font.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.dompdf.com/
  33. *
  34. * @link http://www.dompdf.com/
  35. * @copyright 2004 Benj Carson
  36. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  37. * @package dompdf
  38. */
  39. require_once("dompdf_config.inc.php");
  40. /**
  41. * @access private
  42. */
  43. define("_TTF2AFM", escapeshellarg(TTF2AFM) . " -a -GAef -OW ");
  44. if ( !file_exists(TTF2AFM) ) {
  45. die("Unable to locate the ttf2afm / ttf2pt1 executable (checked " . TTF2AFM . ").\n");
  46. }
  47. /**
  48. * Display command line usage
  49. *
  50. */
  51. function usage() {
  52. echo <<<EOD
  53. Usage: {$_SERVER["argv"][0]} font_family n_file [b_file] [i_file] [bi_file]
  54. font_family: the name of the font, e.g. Verdana, 'Times New Roman',
  55. monospace, sans-serif.
  56. n_file: the .pfb or .ttf file for the normal, non-bold, non-italic
  57. face of the font.
  58. {b|i|bi}_file: the files for each of the respective (bold, italic,
  59. bold-italic) faces.
  60. If the optional b|i|bi files are not specified, load_font.php will search
  61. the directory containing normal font file (n_file) for additional files that
  62. it thinks might be the correct ones (e.g. that end in _Bold or b or B). If
  63. it finds the files they will also be processed. All files will be
  64. automatically copied to the DOMPDF font directory, and afm files will be
  65. generated using ttf2afm.
  66. Examples:
  67. ./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
  68. ./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf
  69. EOD;
  70. }
  71. if ( $_SERVER["argc"] < 3 ) {
  72. usage();
  73. die();
  74. }
  75. /**
  76. * Installs a new font family
  77. *
  78. * This function maps a font-family name to a font. It tries to locate the
  79. * bold, italic, and bold italic versions of the font as well. Once the
  80. * files are located, ttf versions of the font are copied to the fonts
  81. * directory. Changes to the font lookup table are saved to the cache.
  82. *
  83. * @param string $fontname the font-family name
  84. * @param string $normal the filename of the normal face font subtype
  85. * @param string $bold the filename of the bold face font subtype
  86. * @param string $italic the filename of the italic face font subtype
  87. * @param string $bold_italic the filename of the bold italic face font subtype
  88. */
  89. function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
  90. // Check if the base filename is readable
  91. if ( !is_readable($normal) )
  92. throw new DOMPDF_Exception("Unable to read '$normal'.");
  93. $dir = dirname($normal);
  94. $basename = basename($normal);
  95. $last_dot = strrpos($basename, '.');
  96. if ($last_dot !== false) {
  97. $file = substr($basename, 0, $last_dot);
  98. $ext = substr($basename, $last_dot);
  99. } else {
  100. $file = $basename;
  101. $ext = '';
  102. }
  103. // Try $file_Bold.$ext etc.
  104. if ( !isset($bold) || !is_readable($bold) ) {
  105. $bold = $dir . "/" . $file . "_Bold" . $ext;
  106. if ( !is_readable($bold) ) {
  107. // Try $file . "b"
  108. $bold = $dir . "/" . $file . "b" . $ext;
  109. if ( !is_readable($bold) ) {
  110. // Try $file . "B"
  111. $bold = $dir . "/" . $file . "B" . $ext;
  112. if ( !is_readable($bold) )
  113. $bold = null;
  114. }
  115. }
  116. }
  117. if ( is_null($bold) )
  118. echo ("Unable to find bold face file.\n");
  119. if ( !isset($italic) || !is_readable($italic) ) {
  120. $italic = $dir . "/" . $file . "_Italic" . $ext;
  121. if ( !is_readable($italic) ) {
  122. // Try $file . "i"
  123. $italic = $dir . "/" . $file . "i" . $ext;
  124. if ( !is_readable($italic) ) {
  125. // Try $file . "I"
  126. $italic = $dir . "/" . $file . "I" . $ext;
  127. if ( !is_readable($italic) )
  128. $italic = null;
  129. }
  130. }
  131. }
  132. if ( is_null($italic) )
  133. echo ("Unable to find italic face file.\n");
  134. if ( !isset($bold_italic) || !is_readable($bold_italic) ) {
  135. $bold_italic = $dir . "/" . $file . "_Bold_Italic" . $ext;
  136. if ( !is_readable($bold_italic) ) {
  137. // Try $file . "bi"
  138. $bold_italic = $dir . "/" . $file . "bi" . $ext;
  139. if ( !is_readable($bold_italic) ) {
  140. // Try $file . "BI"
  141. $bold_italic = $dir . "/" . $file . "BI" . $ext;
  142. if ( !is_readable($bold_italic) ) {
  143. // Try $file . "ib"
  144. $bold_italic = $dir . "/" . $file . "ib" . $ext;
  145. if ( !is_readable($bold_italic) ) {
  146. // Try $file . "IB"
  147. $bold_italic = $dir . "/" . $file . "IB" . $ext;
  148. if ( !is_readable($bold_italic) )
  149. $bold_italic = null;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. if ( is_null($bold_italic) )
  156. echo ("Unable to find bold italic face file.\n");
  157. $fonts = compact("normal", "bold", "italic", "bold_italic");
  158. $entry = array();
  159. if ( strtolower($ext) === ".pfb" || strtolower($ext) === ".ttf" || strtolower($ext) === ".otf" ) {
  160. // Copy the files to the font directory.
  161. foreach ($fonts as $var => $src) {
  162. if ( is_null($src) ) {
  163. $entry[$var] = DOMPDF_FONT_DIR . basename($normal);
  164. continue;
  165. }
  166. // Verify that the fonts exist and are readable
  167. if ( !is_readable($src) )
  168. throw new User_DOMPDF_Exception("Requested font '$pathname' is not readable");
  169. $dest = DOMPDF_FONT_DIR . basename($src);
  170. if ( !is_writeable(dirname($dest)) )
  171. throw new User_DOMPDF_Exception("Unable to write to destination '$dest'.");
  172. echo "Copying $src to $dest...\n";
  173. if ( !copy($src, $dest) )
  174. throw new DOMPDF_Exception("Unable to copy '$src' to '" . DOMPDF_FONT_DIR . "$dest'.");
  175. $entry[$var] = $dest;
  176. }
  177. } else
  178. throw new DOMPDF_Exception("Unable to process fonts of type '$ext'.");
  179. // If the extension is a ttf, try and convert the fonts to afm too
  180. if ( mb_strtolower($ext) === ".ttf" || strtolower($ext) === ".otf" ) {
  181. foreach ($fonts as $var => $font) {
  182. if ( is_null($font) ) {
  183. $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4);
  184. continue;
  185. }
  186. $dest = DOMPDF_FONT_DIR . mb_substr(basename($font),0, -4);
  187. echo "Generating .afm for $font...\n";
  188. echo "Command: " . _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . "\n";
  189. exec( _TTF2AFM . " " . escapeshellarg($font) . " " . escapeshellarg($dest) . " &> /dev/null", $output, $ret );
  190. $entry[$var] = $dest;
  191. }
  192. }
  193. // FIXME: how to generate afms from pfb?
  194. // Store the fonts in the lookup table
  195. Font_Metrics::set_font_family(strtolower($fontname), $entry);
  196. // Save the changes
  197. Font_Metrics::save_font_families();
  198. }
  199. $normal = $_SERVER["argv"][2];
  200. $bold = isset($_SERVER["argv"][3]) ? $_SERVER["argv"][3] : null;
  201. $italic = isset($_SERVER["argv"][4]) ? $_SERVER["argv"][4] : null;
  202. $bold_italic = isset($_SERVER["argv"][5]) ? $_SERVER["argv"][5] : null;
  203. install_font_family($_SERVER["argv"][1], $normal, $bold, $italic, $bold_italic);