PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/dompdf/load_font.php

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