font_names.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /* This script examines your font directory.
  3. Point your browser to
  4. http://your.domain/your_path_to _mpdf/utils/font_names.php
  5. By default this will examine the folder /ttfonts/ (or the default font
  6. directory defined by _MPDF_TTFONTPATH.
  7. You can optionally define an alternative folder to examine by setting
  8. the variable checkdir below (must be a relative path, or filesystem path).
  9. You can optionally output just the font samples as a PDF file by setting $pdf=true.
  10. */
  11. $checkdir = '';
  12. $pdf = false;
  13. //////////////////////////////////
  14. //////////////////////////////////
  15. //////////////////////////////////
  16. ini_set("memory_limit","256M");
  17. define('_MPDF_PATH','../');
  18. include("../mpdf.php");
  19. $mpdf=new mPDF('s');
  20. $mpdf->useSubstitutions = true;
  21. if ($checkdir) {
  22. $ttfdir = $checkdir;
  23. }
  24. else { $ttfdir = _MPDF_TTFONTPATH; }
  25. $mqr=ini_get("magic_quotes_runtime");
  26. if ($mqr) { set_magic_quotes_runtime(0); }
  27. if (!class_exists('TTFontFile', false)) { include(_MPDF_PATH .'classes/ttfontsuni.php'); }
  28. $ttf = new TTFontFile();
  29. $tempfontdata = array();
  30. $tempsansfonts = array();
  31. $tempseriffonts = array();
  32. $tempmonofonts = array();
  33. $tempfonttrans = array();
  34. $ff = scandir($ttfdir);
  35. foreach($ff AS $f) {
  36. $ret = array();
  37. $isTTC = false;
  38. if (strtolower(substr($f,-4,4))=='.ttc' || strtolower(substr($f,-5,5))=='.ttcf') { // Mac ttcf
  39. $isTTC = true;
  40. $ttf->getTTCFonts($ttfdir.$f);
  41. $nf = $ttf->numTTCFonts;
  42. for ($i=1; $i<=$nf; $i++) {
  43. $ret[] = $ttf->extractCoreInfo($ttfdir.$f, $i);
  44. }
  45. }
  46. else if (strtolower(substr($f,-4,4))=='.ttf' || strtolower(substr($f,-4,4))=='.otf' ) {
  47. $ret[] = $ttf->extractCoreInfo($ttfdir.$f);
  48. }
  49. for ($i=0; $i<count($ret); $i++) {
  50. if (!is_array($ret[$i])) {
  51. if (!$pdf) echo $ret[$i].'<br />';
  52. }
  53. else {
  54. $tfname = $ret[$i][0];
  55. $bold = $ret[$i][1];
  56. $italic = $ret[$i][2];
  57. $fname = strtolower($tfname );
  58. $fname = preg_replace('/[ ()]/','',$fname );
  59. $tempfonttrans[$tfname] = $fname;
  60. $style = '';
  61. if ($bold) { $style .= 'B'; }
  62. if ($italic) { $style .= 'I'; }
  63. if (!$style) { $style = 'R'; }
  64. $tempfontdata[$fname][$style] = $f;
  65. if ($isTTC) {
  66. $tempfontdata[$fname]['TTCfontID'][$style] = $ret[$i][4];
  67. }
  68. //if ($ret[$i][5]) { $tempfontdata[$fname]['rtl'] = true; }
  69. //if ($ret[$i][7]) { $tempfontdata[$fname]['cjk'] = true; }
  70. if ($ret[$i][8]) { $tempfontdata[$fname]['sip'] = true; }
  71. if ($ret[$i][9]) { $tempfontdata[$fname]['smp'] = true; }
  72. $ftype = $ret[$i][3]; // mono, sans or serif
  73. if ($ftype=='sans') { $tempsansfonts[] = $fname; }
  74. else if ($ftype=='serif') { $tempseriffonts[] = $fname; }
  75. else if ($ftype=='mono') { $tempmonofonts[] = $fname; }
  76. }
  77. }
  78. }
  79. $tempsansfonts = array_unique($tempsansfonts);
  80. $tempseriffonts = array_unique($tempseriffonts );
  81. $tempmonofonts = array_unique($tempmonofonts );
  82. $tempfonttrans = array_unique($tempfonttrans);
  83. if (!$pdf) {
  84. echo '<h3>Information</h3>';
  85. }
  86. foreach ($tempfontdata AS $fname => $v) {
  87. if (!isset($tempfontdata[$fname]['R']) || !$tempfontdata[$fname]['R']) {
  88. if (!$pdf) echo 'WARNING - Font file for '.$fname.' may be an italic cursive script, or extra-bold etc.<br />';
  89. if (isset($tempfontdata[$fname]['I']) && $tempfontdata[$fname]['I']) {
  90. $tempfontdata[$fname]['R'] = $tempfontdata[$fname]['I'];
  91. }
  92. else if (isset($tempfontdata[$fname]['B']) && $tempfontdata[$fname]['B']) {
  93. $tempfontdata[$fname]['R'] = $tempfontdata[$fname]['B'];
  94. }
  95. else if (isset($tempfontdata[$fname]['BI']) && $tempfontdata[$fname]['BI']) {
  96. $tempfontdata[$fname]['R'] = $tempfontdata[$fname]['BI'];
  97. }
  98. }
  99. if (isset($tempfontdata[$fname]['smp']) && $tempfontdata[$fname]['smp']) {
  100. if (!$pdf) echo 'INFO - Font file '.$fname.' contains characters in Unicode Plane 1 SMP<br />';
  101. $tempfontdata[$fname]['smp'] = false;
  102. }
  103. if (isset($tempfontdata[$fname]['sip']) && $tempfontdata[$fname]['sip']) {
  104. if (!$pdf) echo 'INFO - Font file '.$fname.' contains characters in Unicode Plane 2 SIP<br />';
  105. if (preg_match('/^(.*)-extb/',$fname, $fm)) {
  106. if (isset($tempfontdata[($fm[1])]) && $tempfontdata[($fm[1])]) {
  107. $tempfontdata[($fm[1])]['sip-ext'] = $fname;
  108. if (!$pdf) echo 'INFO - Font file '.$fname.' has been defined as a CJK ext-B for '.($fm[1]).'<br />';
  109. }
  110. else if (isset($tempfontdata[($fm[1].'-exta')]) && $tempfontdata[($fm[1].'-exta')]) {
  111. $tempfontdata[($fm[1].'-exta')]['sip-ext'] = $fname;
  112. if (!$pdf) echo 'INFO - Font file '.$fname.' has been defined as a CJK ext-B for '.($fm[1].'-exta').'<br />';
  113. }
  114. }
  115. // else { unset($tempfontdata[$fname]['sip']); }
  116. }
  117. unset($tempfontdata[$fname]['sip']);
  118. unset($tempfontdata[$fname]['smp']);
  119. }
  120. $mpdf->fontdata = array_merge($tempfontdata ,$mpdf->fontdata);
  121. $mpdf->available_unifonts = array();
  122. foreach ($mpdf->fontdata AS $f => $fs) {
  123. if (isset($fs['R']) && $fs['R']) { $mpdf->available_unifonts[] = $f; }
  124. if (isset($fs['B']) && $fs['B']) { $mpdf->available_unifonts[] = $f.'B'; }
  125. if (isset($fs['I']) && $fs['I']) { $mpdf->available_unifonts[] = $f.'I'; }
  126. if (isset($fs['BI']) && $fs['BI']) { $mpdf->available_unifonts[] = $f.'BI'; }
  127. }
  128. $mpdf->default_available_fonts = $mpdf->available_unifonts;
  129. if (!$pdf) {
  130. echo '<hr />';
  131. echo '<h3>Font names as parsed by mPDF</h3>';
  132. }
  133. ksort($tempfonttrans);
  134. $html = '';
  135. foreach($tempfonttrans AS $on=>$mn) {
  136. if (!file_exists($ttfdir.$mpdf->fontdata[$mn]['R'])) { continue; }
  137. $ond = '"'.$on.'"';
  138. $html .= '<p style="font-family:'.$on.';">'.$ond.' font is available as '.$mn;
  139. if (isset($mpdf->fontdata[$mn]['sip-ext']) && $mpdf->fontdata[$mn]['sip-ext']) {
  140. $html .= '; CJK ExtB: '.$mpdf->fontdata[$mn]['sip-ext'];
  141. }
  142. $html .= '</p>';
  143. }
  144. if ($pdf) {
  145. $mpdf->WriteHTML($html);
  146. $mpdf->Output();
  147. exit;
  148. }
  149. foreach($tempfonttrans AS $on=>$mn) {
  150. $ond = '"'.$on.'"';
  151. echo '<div style="font-family:\''.$on.'\';">'.$ond.' font is available as '.$mn;
  152. if (isset($mpdf->fontdata[$mn]['sip-ext']) && $mpdf->fontdata[$mn]['sip-ext']) {
  153. echo '; CJK ExtB: '.$mpdf->fontdata[$mn]['sip-ext'];
  154. }
  155. echo '</div>';
  156. }
  157. echo '<hr />';
  158. echo '<h3>Sample config_fonts.php file</h3>';
  159. echo '<div>Remember to edit the following arrays to place your preferred default first in order:</div>';
  160. echo '<pre>';
  161. ksort($tempfontdata);
  162. echo '$this->fontdata = '.var_export($tempfontdata,true).";\n";
  163. sort($tempsansfonts);
  164. echo '$this->sans_fonts = array(\''.implode("', '", $tempsansfonts)."');\n";
  165. sort($tempseriffonts);
  166. echo '$this->serif_fonts = array(\''.implode("', '", $tempseriffonts)."');\n";
  167. sort($tempmonofonts);
  168. echo '$this->mono_fonts = array(\''.implode("', '", $tempmonofonts)."');\n";
  169. echo '</pre>';
  170. exit;
  171. ?>