MakeFont

The WooCommerce PDF & Print MakeFont function.

Description

MakeFont( $fontfile, $afmfile, (string) $enc = 'cp1252', (array) $patch = array(), (string) $type = 'TrueType' ); 

Parameters (5)

0. $fontfile
The fontfile.
1. $afmfile
The afmfile.
2. $enc — Optional. (string) => 'cp1252'
The enc.
3. $patch — Optional. (array) => array()
The patch.
4. $type — Optional. (string) => 'TrueType'
The type.

Usage

  1. if ( !function_exists( 'MakeFont' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-pdf-print/pdf/font/makefont/makefont.php'; 
  3.  
  4. // The fontfile. 
  5. $fontfile = null; 
  6.  
  7. // The afmfile. 
  8. $afmfile = null; 
  9.  
  10. // The enc. 
  11. $enc = 'cp1252'; 
  12.  
  13. // The patch. 
  14. $patch = array(); 
  15.  
  16. // The type. 
  17. $type = 'TrueType'; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = MakeFont($fontfile, $afmfile, $enc, $patch, $type); 
  21.  

Defined (1)

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

/pdf/font/makefont/makefont.php  
  1. function MakeFont($fontfile, $afmfile, $enc='cp1252', $patch=array(), $type='TrueType') 
  2. //Generate a font definition file 
  3. if(get_magic_quotes_runtime()) 
  4. @set_magic_quotes_runtime(0); 
  5. ini_set('auto_detect_line_endings', '1'); 
  6. if($enc) 
  7. $map=ReadMap($enc); 
  8. foreach($patch as $cc=>$gn) 
  9. $map[$cc]=$gn; 
  10. else 
  11. $map=array(); 
  12. if(!file_exists($afmfile)) 
  13. die('<b>Error:</b> AFM file not found: '.$afmfile); 
  14. $fm=ReadAFM($afmfile, $map); 
  15. if($enc) 
  16. $diff=MakeFontEncoding($map); 
  17. else 
  18. $diff=''; 
  19. $fd=MakeFontDescriptor($fm, empty($map)); 
  20. //Find font type 
  21. if($fontfile) 
  22. $ext=strtolower(substr($fontfile, -3)); 
  23. if($ext=='ttf') 
  24. $type='TrueType'; 
  25. elseif($ext=='pfb') 
  26. $type='Type1'; 
  27. else 
  28. die('<b>Error:</b> unrecognized font file extension: '.$ext); 
  29. else 
  30. if($type!='TrueType' && $type!='Type1') 
  31. die('<b>Error:</b> incorrect font type: '.$type); 
  32. //Start generation 
  33. $s='<?php'."\n"; 
  34. $s.='$type=\''.$type."';\n"; 
  35. $s.='$name=\''.$fm['FontName']."';\n"; 
  36. $s.='$desc='.$fd.";\n"; 
  37. if(!isset($fm['UnderlinePosition'])) 
  38. $fm['UnderlinePosition']=-100; 
  39. if(!isset($fm['UnderlineThickness'])) 
  40. $fm['UnderlineThickness']=50; 
  41. $s.='$up='.$fm['UnderlinePosition'].";\n"; 
  42. $s.='$ut='.$fm['UnderlineThickness'].";\n"; 
  43. $w=MakeWidthArray($fm); 
  44. $s.='$cw='.$w.";\n"; 
  45. $s.='$enc=\''.$enc."';\n"; 
  46. $s.='$diff=\''.$diff."';\n"; 
  47. $basename=substr(basename($afmfile), 0, -4); 
  48. if($fontfile) 
  49. //Embedded font 
  50. if(!file_exists($fontfile)) 
  51. die('<b>Error:</b> font file not found: '.$fontfile); 
  52. if($type=='TrueType') 
  53. CheckTTF($fontfile); 
  54. $f=fopen($fontfile, 'rb'); 
  55. if(!$f) 
  56. die('<b>Error:</b> Can\'t open '.$fontfile); 
  57. $file=fread($f, filesize($fontfile)); 
  58. fclose($f); 
  59. if($type=='Type1') 
  60. //Find first two sections and discard third one 
  61. $header=(ord($file[0])==128); 
  62. if($header) 
  63. //Strip first binary header 
  64. $file=substr($file, 6); 
  65. $pos=strpos($file, 'eexec'); 
  66. if(!$pos) 
  67. die('<b>Error:</b> font file does not seem to be valid Type1'); 
  68. $size1=$pos+6; 
  69. if($header && ord($file[$size1])==128) 
  70. //Strip second binary header 
  71. $file=substr($file, 0, $size1).substr($file, $size1+6); 
  72. $pos=strpos($file, '00000000'); 
  73. if(!$pos) 
  74. die('<b>Error:</b> font file does not seem to be valid Type1'); 
  75. $size2=$pos-$size1; 
  76. $file=substr($file, 0, $size1+$size2); 
  77. if(function_exists('gzcompress')) 
  78. $cmp=$basename.'.z'; 
  79. SaveToFile($cmp, gzcompress($file), 'b'); 
  80. $s.='$file=\''.$cmp."';\n"; 
  81. echo 'Font file compressed ('.$cmp.')<br>'; 
  82. else 
  83. $s.='$file=\''.basename($fontfile)."';\n"; 
  84. echo '<b>Notice:</b> font file could not be compressed (zlib extension not available)<br>'; 
  85. if($type=='Type1') 
  86. $s.='$size1='.$size1.";\n"; 
  87. $s.='$size2='.$size2.";\n"; 
  88. else 
  89. $s.='$originalsize='.filesize($fontfile).";\n"; 
  90. else 
  91. //Not embedded font 
  92. $s.='$file='."'';\n"; 
  93. $s.="?>\n"; 
  94. SaveToFile($basename.'.php', $s, 't'); 
  95. echo 'Font definition file generated ('.$basename.'.php'.')<br>';