load_font.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * @package dompdf
  5. * @link http://dompdf.github.com/
  6. * @author Benj Carson <benjcarson@digitaljunkies.ca>
  7. * @author Fabien Ménager <fabien.menager@gmail.com>
  8. * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  9. */
  10. require_once "dompdf_config.inc.php";
  11. /**
  12. * Display command line usage
  13. */
  14. function usage() {
  15. echo <<<EOD
  16. Usage: {$_SERVER["argv"][0]} font_family [n_file [b_file] [i_file] [bi_file]]
  17. font_family: the name of the font, e.g. Verdana, 'Times New Roman',
  18. monospace, sans-serif. If it equals to "system_fonts",
  19. all the system fonts will be installed.
  20. n_file: the .ttf or .otf file for the normal, non-bold, non-italic
  21. face of the font.
  22. {b|i|bi}_file: the files for each of the respective (bold, italic,
  23. bold-italic) faces.
  24. If the optional b|i|bi files are not specified, load_font.php will search
  25. the directory containing normal font file (n_file) for additional files that
  26. it thinks might be the correct ones (e.g. that end in _Bold or b or B). If
  27. it finds the files they will also be processed. All files will be
  28. automatically copied to the DOMPDF font directory, and afm files will be
  29. generated using php-font-lib (http://code.google.com/p/php-font-lib/).
  30. Examples:
  31. ./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
  32. ./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf
  33. EOD;
  34. exit;
  35. }
  36. if ( $_SERVER["argc"] < 3 && @$_SERVER["argv"][1] != "system_fonts" ) {
  37. usage();
  38. }
  39. /**
  40. * Installs a new font family
  41. * This function maps a font-family name to a font. It tries to locate the
  42. * bold, italic, and bold italic versions of the font as well. Once the
  43. * files are located, ttf versions of the font are copied to the fonts
  44. * directory. Changes to the font lookup table are saved to the cache.
  45. *
  46. * @param string $fontname the font-family name
  47. * @param string $normal the filename of the normal face font subtype
  48. * @param string $bold the filename of the bold face font subtype
  49. * @param string $italic the filename of the italic face font subtype
  50. * @param string $bold_italic the filename of the bold italic face font subtype
  51. *
  52. * @throws DOMPDF_Exception
  53. */
  54. function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
  55. Font_Metrics::init();
  56. // Check if the base filename is readable
  57. if ( !is_readable($normal) )
  58. throw new DOMPDF_Exception("Unable to read '$normal'.");
  59. $dir = dirname($normal);
  60. $basename = basename($normal);
  61. $last_dot = strrpos($basename, '.');
  62. if ($last_dot !== false) {
  63. $file = substr($basename, 0, $last_dot);
  64. $ext = strtolower(substr($basename, $last_dot));
  65. } else {
  66. $file = $basename;
  67. $ext = '';
  68. }
  69. if ( !in_array($ext, array(".ttf", ".otf")) ) {
  70. throw new DOMPDF_Exception("Unable to process fonts of type '$ext'.");
  71. }
  72. // Try $file_Bold.$ext etc.
  73. $path = "$dir/$file";
  74. $patterns = array(
  75. "bold" => array("_Bold", "b", "B", "bd", "BD"),
  76. "italic" => array("_Italic", "i", "I"),
  77. "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB"),
  78. );
  79. foreach ($patterns as $type => $_patterns) {
  80. if ( !isset($$type) || !is_readable($$type) ) {
  81. foreach($_patterns as $_pattern) {
  82. if ( is_readable("$path$_pattern$ext") ) {
  83. $$type = "$path$_pattern$ext";
  84. break;
  85. }
  86. }
  87. if ( is_null($$type) )
  88. echo ("Unable to find $type face file.\n");
  89. }
  90. }
  91. $fonts = compact("normal", "bold", "italic", "bold_italic");
  92. $entry = array();
  93. // Copy the files to the font directory.
  94. foreach ($fonts as $var => $src) {
  95. if ( is_null($src) ) {
  96. $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4);
  97. continue;
  98. }
  99. // Verify that the fonts exist and are readable
  100. if ( !is_readable($src) )
  101. throw new DOMPDF_Exception("Requested font '$src' is not readable");
  102. $dest = DOMPDF_FONT_DIR . basename($src);
  103. if ( !is_writeable(dirname($dest)) )
  104. throw new DOMPDF_Exception("Unable to write to destination '$dest'.");
  105. echo "Copying $src to $dest...\n";
  106. if ( !copy($src, $dest) )
  107. throw new DOMPDF_Exception("Unable to copy '$src' to '$dest'");
  108. $entry_name = mb_substr($dest, 0, -4);
  109. echo "Generating Adobe Font Metrics for $entry_name...\n";
  110. $font_obj = Font::load($dest);
  111. $font_obj->saveAdobeFontMetrics("$entry_name.ufm");
  112. $entry[$var] = $entry_name;
  113. }
  114. // Store the fonts in the lookup table
  115. Font_Metrics::set_font_family($fontname, $entry);
  116. // Save the changes
  117. Font_Metrics::save_font_families();
  118. }
  119. // If installing system fonts (may take a long time)
  120. if ( $_SERVER["argv"][1] === "system_fonts" ) {
  121. $fonts = Font_Metrics::get_system_fonts();
  122. foreach ( $fonts as $family => $files ) {
  123. echo " >> Installing '$family'... \n";
  124. if ( !isset($files["normal"]) ) {
  125. echo "No 'normal' style font file\n";
  126. }
  127. else {
  128. install_font_family( $family, @$files["normal"], @$files["bold"], @$files["italic"], @$files["bold_italic"]);
  129. echo "Done !\n";
  130. }
  131. echo "\n";
  132. }
  133. }
  134. else {
  135. call_user_func_array("install_font_family", array_slice($_SERVER["argv"], 1));
  136. }