install_font_family

Installs a new font family This function maps a font-family name to a font.

Description

install_font_family( (string) $fontname, (string) $normal, (constant) $bold = null, (constant) $italic = null, (constant) $bold_italic = null ); 

It tries to locate the bold, italic, and bold italic versions of the font as well. Once the files are located, ttf versions of the font are copied to the fonts directory. Changes to the font lookup table are saved to the cache.

Parameters (5)

0. $fontname (string)
The font-family name
1. $normal (string)
The filename of the normal face font subtype
2. $bold — Optional. (constant) => null
The filename of the bold face font subtype
3. $italic — Optional. (constant) => null
The filename of the italic face font subtype
4. $bold_italic — Optional. (constant) => null
The filename of the bold italic face font subtype

Usage

  1. if ( !function_exists( 'install_font_family' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-pdf-invoices-packing-slips/lib/dompdf/load_font.php'; 
  3.  
  4. // the font-family name 
  5. $fontname = ''; 
  6.  
  7. // the filename of the normal face font subtype 
  8. $normal = ''; 
  9.  
  10. // the filename of the bold face font subtype 
  11. $bold = null; 
  12.  
  13. // the filename of the italic face font subtype 
  14. $italic = null; 
  15.  
  16. // the filename of the bold italic face font subtype 
  17. $bold_italic = null; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = install_font_family($fontname, $normal, $bold, $italic, $bold_italic); 
  21.  

Defined (1)

The function is defined in the following location(s).

/lib/dompdf/load_font.php  
  1. function install_font_family($fontname, $normal, $bold = null, $italic = null, $bold_italic = null) { 
  2. Font_Metrics::init(); 
  3.  
  4. // Check if the base filename is readable 
  5. if ( !is_readable($normal) ) 
  6. throw new DOMPDF_Exception("Unable to read '$normal'."); 
  7.  
  8. $dir = dirname($normal); 
  9. $basename = basename($normal); 
  10. $last_dot = strrpos($basename, '.'); 
  11. if ($last_dot !== false) { 
  12. $file = substr($basename, 0, $last_dot); 
  13. $ext = strtolower(substr($basename, $last_dot)); 
  14. } else { 
  15. $file = $basename; 
  16. $ext = ''; 
  17.  
  18. if ( !in_array($ext, array(".ttf", ".otf")) ) { 
  19. throw new DOMPDF_Exception("Unable to process fonts of type '$ext'."); 
  20.  
  21. // Try $file_Bold.$ext etc. 
  22. $path = "$dir/$file"; 
  23.  
  24. $patterns = array( 
  25. "bold" => array("_Bold", "b", "B", "bd", "BD"),  
  26. "italic" => array("_Italic", "i", "I"),  
  27. "bold_italic" => array("_Bold_Italic", "bi", "BI", "ib", "IB"),  
  28. ); 
  29.  
  30. foreach ($patterns as $type => $_patterns) { 
  31. if ( !isset($$type) || !is_readable($$type) ) { 
  32. foreach($_patterns as $_pattern) { 
  33. if ( is_readable("$path$_pattern$ext") ) { 
  34. $$type = "$path$_pattern$ext"; 
  35. break; 
  36.  
  37. if ( is_null($$type) ) 
  38. echo ("Unable to find $type face file.\n"); 
  39.  
  40. $fonts = compact("normal", "bold", "italic", "bold_italic"); 
  41. $entry = array(); 
  42.  
  43. // Copy the files to thefontdirectory. 
  44. foreach ($fonts as $var => $src) { 
  45. if ( is_null($src) ) { 
  46. $entry[$var] = DOMPDF_FONT_DIR . mb_substr(basename($normal), 0, -4); 
  47. continue; 
  48.  
  49. // Verify that the fonts exist and are readable 
  50. if ( !is_readable($src) ) 
  51. throw new DOMPDF_Exception("Requestedfont'$src' is not readable"); 
  52.  
  53. $dest = DOMPDF_FONT_DIR . basename($src); 
  54.  
  55. if ( !is_writeable(dirname($dest)) ) 
  56. throw new DOMPDF_Exception("Unable to write to destination '$dest'."); 
  57.  
  58. echo "Copying $src to $dest...\n"; 
  59.  
  60. if ( !copy($src, $dest) ) 
  61. throw new DOMPDF_Exception("Unable to copy '$src' to '$dest'"); 
  62.  
  63. $entry_name = mb_substr($dest, 0, -4); 
  64.  
  65. echo "Generating Adobe Font Metrics for $entry_name...\n"; 
  66.  
  67. $font_obj = Font::load($dest); 
  68. $font_obj->saveAdobeFontMetrics("$entry_name.ufm"); 
  69.  
  70. $entry[$var] = $entry_name; 
  71.  
  72. // Store the fonts in the lookup table 
  73. Font_Metrics::set_font_family($fontname, $entry); 
  74.  
  75. // Save the changes 
  76. Font_Metrics::save_font_families();