load_font.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. // 1. [Required] Point to the composer or dompdf autoloader
  3. require_once "autoload.inc.php";
  4. // 2. [Optional] Set the path to your font directory
  5. // By default dopmdf loads fonts to dompdf/lib/fonts
  6. // If you have modified your font directory set this
  7. // variable appropriately.
  8. //$fontDir = "lib/fonts";
  9. // *** DO NOT MODIFY BELOW THIS POINT ***
  10. use Dompdf\Dompdf;
  11. use Dompdf\CanvasFactory;
  12. use Dompdf\Exception;
  13. use Dompdf\FontMetrics;
  14. use Dompdf\Options;
  15. use FontLib\Font;
  16. /**
  17. * Display command line usage
  18. */
  19. function usage() {
  20. echo <<<EOD
  21. Usage: {$_SERVER["argv"][0]} font_family [n_file [b_file] [i_file] [bi_file]]
  22. font_family: the name of the font, e.g. Verdana, 'Times New Roman',
  23. monospace, sans-serif. If it equals to "system_fonts",
  24. all the system fonts will be installed.
  25. n_file: the .ttf or .otf file for the normal, non-bold, non-italic
  26. face of the font.
  27. {b|i|bi}_file: the files for each of the respective (bold, italic,
  28. bold-italic) faces.
  29. If the optional b|i|bi files are not specified, load_font.php will search
  30. the directory containing normal font file (n_file) for additional files that
  31. it thinks might be the correct ones (e.g. that end in _Bold or b or B). If
  32. it finds the files they will also be processed. All files will be
  33. automatically copied to the DOMPDF font directory, and afm files will be
  34. generated using php-font-lib (https://github.com/PhenX/php-font-lib).
  35. Examples:
  36. ./load_font.php silkscreen /usr/share/fonts/truetype/slkscr.ttf
  37. ./load_font.php 'Times New Roman' /mnt/c_drive/WINDOWS/Fonts/times.ttf
  38. EOD;
  39. exit;
  40. }
  41. if ( $_SERVER["argc"] < 3 && @$_SERVER["argv"][1] != "system_fonts" ) {
  42. usage();
  43. }
  44. $dompdf = new DOMPDF();
  45. if (isset($fontDir) && realpath($fontDir) !== false) {
  46. $dompdf->getOptions()->set('fontDir', $fontDir);
  47. }
  48. /**
  49. * Installs a new font family
  50. * This function maps a font-family name to a font. It tries to locate the
  51. * bold, italic, and bold italic versions of the font as well. Once the
  52. * files are located, ttf versions of the font are copied to the fonts
  53. * directory. Changes to the font lookup table are saved to the cache.
  54. *
  55. * @param string $fontname the font-family name
  56. * @param string $normal the filename of the normal face font subtype
  57. * @param string $bold the filename of the bold face font subtype
  58. * @param string $italic the filename of the italic face font subtype
  59. * @param string $bold_italic the filename of the bold italic face font subtype
  60. *
  61. * @throws Exception
  62. */
  63. function install_font_family($dompdf, $fontname, $normal, $bold = null, $italic = null, $bold_italic = null) {
  64. $fontMetrics = $dompdf->getFontMetrics();
  65. // Check if the base filename is readable
  66. if ( !is_readable($normal) )
  67. throw new Exception("Unable to read '$normal'.");
  68. $dir = dirname($normal);
  69. $basename = basename($normal);
  70. $last_dot = strrpos($basename, '.');
  71. if ($last_dot !== false) {
  72. $file = substr($basename, 0, $last_dot);
  73. $ext = strtolower(substr($basename, $last_dot));
  74. } else {
  75. $file = $basename;
  76. $ext = '';
  77. }
  78. if ( !in_array($ext, array(".ttf", ".otf")) ) {
  79. throw new Exception("Unable to process fonts of type '$ext'.");
  80. }
  81. // Try $file_Bold.$ext etc.
  82. $path = "$dir/$file";
  83. $patterns = array(
  84. "bold" => array("_Bold", "b", "B", "bd", "BD"),
  85. "italic" => array("_Italic", "i", "I"),
  86. "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB"),
  87. );
  88. foreach ($patterns as $type => $_patterns) {
  89. if ( !isset($$type) || !is_readable($$type) ) {
  90. foreach($_patterns as $_pattern) {
  91. if ( is_readable("$path$_pattern$ext") ) {
  92. $$type = "$path$_pattern$ext";
  93. break;
  94. }
  95. }
  96. if ( is_null($$type) )
  97. echo ("Unable to find $type face file.\n");
  98. }
  99. }
  100. $fonts = compact("normal", "bold", "italic", "bold_italic");
  101. $entry = array();
  102. // Copy the files to the font directory.
  103. foreach ($fonts as $var => $src) {
  104. if ( is_null($src) ) {
  105. $entry[$var] = $dompdf->getOptions()->get('fontDir') . '/' . mb_substr(basename($normal), 0, -4);
  106. continue;
  107. }
  108. // Verify that the fonts exist and are readable
  109. if ( !is_readable($src) )
  110. throw new Exception("Requested font '$src' is not readable");
  111. $dest = $dompdf->getOptions()->get('fontDir') . '/' . basename($src);
  112. if ( !is_writeable(dirname($dest)) )
  113. throw new Exception("Unable to write to destination '$dest'.");
  114. echo "Copying $src to $dest...\n";
  115. if ( !copy($src, $dest) )
  116. throw new Exception("Unable to copy '$src' to '$dest'");
  117. $entry_name = mb_substr($dest, 0, -4);
  118. echo "Generating Adobe Font Metrics for $entry_name...\n";
  119. $font_obj = Font::load($dest);
  120. $font_obj->saveAdobeFontMetrics("$entry_name.ufm");
  121. $font_obj->close();
  122. $entry[$var] = $entry_name;
  123. }
  124. // Store the fonts in the lookup table
  125. $fontMetrics->setFontFamily($fontname, $entry);
  126. // Save the changes
  127. $fontMetrics->saveFontFamilies();
  128. }
  129. // If installing system fonts (may take a long time)
  130. if ( $_SERVER["argv"][1] === "system_fonts" ) {
  131. $fontMetrics = $dompdf->getFontMetrics();
  132. $files = glob("/usr/share/fonts/truetype/*.ttf") +
  133. glob("/usr/share/fonts/truetype/*/*.ttf") +
  134. glob("/usr/share/fonts/truetype/*/*/*.ttf") +
  135. glob("C:\\Windows\\fonts\\*.ttf") +
  136. glob("C:\\WinNT\\fonts\\*.ttf") +
  137. glob("/mnt/c_drive/WINDOWS/Fonts/");
  138. $fonts = array();
  139. foreach ($files as $file) {
  140. $font = Font::load($file);
  141. $records = $font->getData("name", "records");
  142. $type = $this->getType($records[2]);
  143. $fonts[mb_strtolower($records[1])][$type] = $file;
  144. $font->close();
  145. }
  146. foreach ( $fonts as $family => $files ) {
  147. echo " >> Installing '$family'... \n";
  148. if ( !isset($files["normal"]) ) {
  149. echo "No 'normal' style font file\n";
  150. }
  151. else {
  152. install_font_family($dompdf, $family, @$files["normal"], @$files["bold"], @$files["italic"], @$files["bold_italic"]);
  153. echo "Done !\n";
  154. }
  155. echo "\n";
  156. }
  157. }
  158. else {
  159. call_user_func_array("install_font_family", array_merge( array($dompdf), array_slice($_SERVER["argv"], 1) ));
  160. }