font_coverage.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /* This script prints out the Unicode coverage of all TrueType font files
  3. in your font directory.
  4. Point your browser to
  5. http://your.domain/your_path_to _mpdf/utils/font_coverage.php
  6. By default this will examine the folder /ttfonts/ (or the default font
  7. directory defined by _MPDF_TTFONTPATH.
  8. You can optionally define an alternative folder to examine by setting
  9. the variable below (must be a relative path, or filesystem path):
  10. */
  11. $checkdir = '';
  12. //////////////////////////////////
  13. //////////////////////////////////
  14. //////////////////////////////////
  15. set_time_limit(600);
  16. ini_set("memory_limit","256M");
  17. //==============================================================
  18. //==============================================================
  19. include("../mpdf.php");
  20. $mpdf=new mPDF('','A4-L','','',10,10,10,10);
  21. $mpdf->SetDisplayMode('fullpage');
  22. $mpdf->useSubstitutions = true;
  23. $mpdf->debug = true;
  24. $mpdf->simpleTables = true;
  25. if ($checkdir) {
  26. $ttfdir = $checkdir;
  27. }
  28. else { $ttfdir = _MPDF_TTFONTPATH; }
  29. $mqr=ini_get("magic_quotes_runtime");
  30. if ($mqr) { set_magic_quotes_runtime(0); }
  31. if (!class_exists('TTFontFile', false)) { include(_MPDF_PATH .'classes/ttfontsuni.php'); }
  32. //==============================================================
  33. $smp = true;
  34. $maxt = 131071;
  35. //==============================================================
  36. //==============================================================
  37. $unifile = file('UnicodeData.txt');
  38. $unichars = array();
  39. foreach($unifile AS $line) {
  40. if (preg_match('/<control>/',$line,$m)) {
  41. $rangename = '';
  42. continue;
  43. }
  44. else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});<(.*?), Last>/',$line,$m)) {
  45. if ($rangename && $rangename == $m[2]) {
  46. $endrange = hexdec($m[1]);
  47. for ($i=$startrange;$i<=$endrange; $i++) {
  48. $unichars[$i] = $i;
  49. }
  50. }
  51. $rangename = '';
  52. }
  53. else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});<(.*?), First>/',$line,$m)) {
  54. $startrange = hexdec($m[1]);
  55. $rangename = $m[2];
  56. }
  57. else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});/',$line,$m)) {
  58. $unichars[hexdec($m[1])] = hexdec($m[1]);
  59. $rangename = '';
  60. }
  61. }
  62. // loads array $unicode_ranges
  63. include('UnicodeRanges.php');
  64. //==============================================================
  65. $html = '<html><head><style>td { border: 0.1mm solid #555555; }
  66. body { font-weight: normal; font-family: helvetica;font-size:8pt; }
  67. td { font-family: helvetica;font-size:8pt; vertical-align: top;}
  68. </style></head><body>';
  69. //==============================================================
  70. $ff = scandir($ttfdir);
  71. $tempfontdata = array();
  72. foreach($ff AS $f) {
  73. $ttf = new TTFontFile();
  74. $ret = array();
  75. $isTTC = false;
  76. if (strtolower(substr($f,-4,4))=='.ttf' || strtolower(substr($f,-4,4))=='.otf') {
  77. $ret[] = $ttf->extractCoreInfo($ttfdir.$f);
  78. }
  79. for ($i=0; $i<count($ret); $i++) {
  80. if (is_array($ret[$i])) {
  81. $tfname = $ret[$i][0];
  82. $bold = $ret[$i][1];
  83. $italic = $ret[$i][2];
  84. $fname = strtolower($tfname );
  85. $fname = preg_replace('/[ ()]/','',$fname );
  86. //$tempfonttrans[$tfname] = $fname;
  87. $style = '';
  88. if ($bold) { $style .= 'B'; }
  89. if ($italic) { $style .= 'I'; }
  90. if (!$style) {
  91. $tempfontdata[$fname]['file'] = $f;
  92. if ($isTTC) {
  93. $tempfontdata[$fname]['TTCfontID'] = $ret[$i][4];
  94. }
  95. }
  96. }
  97. }
  98. unset($ttf);
  99. }
  100. $fullcovers = array();
  101. $nearlycovers = array();
  102. ksort($tempfontdata);
  103. $ningroup = 14;
  104. $nofgroups = ceil(count($unicode_ranges)/$ningroup);
  105. //==============================================================
  106. for ($urgp = 0; $urgp < $nofgroups; $urgp++) {
  107. $html .= '<table cellpadding="2" cellspacing="0" style="page-break-inside:avoid; text-align:center; border-collapse: collapse; ">';
  108. $html .= '<thead><tr><td></td>';
  109. foreach($unicode_ranges AS $urk => $ur) {
  110. if ($urk >= ($urgp*$ningroup) && $urk < (($urgp+1)*$ningroup)) {
  111. $rangekey = $urk;
  112. $range = $ur['range'];
  113. $rangestart = $ur['starthex'];
  114. $rangeend = $ur['endhex'];
  115. $html .= '<td style="font-family:helvetica;font-size:8pt;font-weight:bold;">'.strtoupper($range).' (U+'.$rangestart .'-U+'.$rangeend.')</td>';
  116. }
  117. }
  118. $html .= '</tr></thead>';
  119. foreach ($tempfontdata AS $fname => $v) {
  120. $cw = '';
  121. if (file_exists((_MPDF_TTFONTDATAPATH.$fname.'.cw.dat'))) { $cw = file_get_contents(_MPDF_TTFONTDATAPATH.$fname.'.cw.dat'); }
  122. else {
  123. $mpdf->fontdata[$fname]['R'] = $tempfontdata[$fname]['file'];
  124. $mpdf->AddFont($fname);
  125. $cw = file_get_contents(_MPDF_TTFONTDATAPATH.$fname.'.cw.dat');
  126. }
  127. if (!$cw) {
  128. continue;
  129. die("Font data not available for $fname");
  130. }
  131. $counter=0;
  132. $max = $maxt;
  133. // create HTML content
  134. $html .= '<tr>';
  135. $html .= '<td>'.$fname.'</td>';
  136. foreach($unicode_ranges AS $urk => $ur) {
  137. if ($urk >= ($urgp*$ningroup) && $urk < (($urgp+1)*$ningroup)) {
  138. if ($ur['pua'] || $ur['reserved'] || $ur['control']) {
  139. $html .= '<td style="background-color: #000000;"></td>';
  140. }
  141. else {
  142. $rangekey = $urk;
  143. $range = $ur['range'];
  144. $rangestart = $ur['starthex'];
  145. $rangeend = $ur['endhex'];
  146. $rangestartdec = $ur['startdec'];
  147. $rangeenddec = $ur['enddec'];
  148. $uniinrange = 0;
  149. $fontinrange = 0;
  150. for ($i=$rangestartdec; $i<=$rangeenddec; $i++) {
  151. //if (isset($cw[$i])) { $fontinrange++; }
  152. if ($mpdf->_charDefined($cw, $i)) { $fontinrange++; }
  153. if (isset($unichars[$i])) { $uniinrange++; }
  154. }
  155. if ($uniinrange) {
  156. if ($fontinrange) {
  157. $pc = ($fontinrange/$uniinrange);
  158. $str = '('.$fontinrange.'/'.$uniinrange.')';
  159. if ($pc==1) {
  160. $fullcovers[$urk][] = $fname;
  161. $html .= '<td style="background-color: #00FF00;"></td>';
  162. }
  163. else if ($pc>1) {
  164. $fullcovers[$urk][] = $fname;
  165. $html .= '<td style="background-color: #00FF00;">'.$str.'</td>';
  166. }
  167. else if ($pc>=0.9) {
  168. $html .= '<td style="background-color: #AAFFAA;">'.$str.'</td>';
  169. $nearlycovers[$urk][] = $fname;
  170. }
  171. else if ($pc>0.75) { $html .= '<td style="background-color: #00FFAA;">'.$str.'</td>'; }
  172. else if ($pc>0.5) { $html .= '<td style="background-color: #AAAAFF;">'.$str.'</td>'; }
  173. else if ($pc>0.25) { $html .= '<td style="background-color: #FFFFAA;">'.$str.'</td>'; }
  174. else { $html .= '<td style="background-color: #FFAAAA;">'.$str.'</td>'; }
  175. }
  176. else { $html .= '<td style="background-color: #555555;">(0/0)</td>'; }
  177. }
  178. else { $html .= '<td style="background-color: #000000;"></td>'; }
  179. }
  180. }
  181. }
  182. $html .= '</tr>';
  183. }
  184. //==============================================================
  185. $html .= '</table><pagebreak />';
  186. }
  187. $html .= '<h4>Fonts with full coverage of Unicode Ranges</h4>';
  188. $html .= '<table>';
  189. //$html .= '<tr><td></td><td></td></tr>';
  190. foreach($unicode_ranges AS $urk => $ur) {
  191. if ($ur['pua'] || $ur['reserved'] || $ur['control']) { continue; }
  192. $rangekey = $urk;
  193. $range = $ur['range'];
  194. $rangestart = $ur['starthex'];
  195. $rangeend = $ur['endhex'];
  196. $ext = $ext2 = '';
  197. if ($ur['combining']) { $ext = 'background-color:#DDDDFF;'; $ext2 = '<br /><span style="color:#AA0000">Special positioning required</span>'; }
  198. if ($ur['vertical']) { $ext = 'background-color:#FFDDDD;'; $ext2 = '<br /><span style="color:#AA0000">Vertical positioning required</span>'; }
  199. if ($ur['special']) { $ext = 'background-color:#FFDDDD;'; $ext2 = '<br /><span style="color:#AA0000">Special processing required</span>'; }
  200. $html .= '<tr><td style="font-family:helvetica;font-size:8pt;font-weight:bold;'.$ext.'">'.strtoupper($range).' (U+'.$rangestart .'-U+'.$rangeend.')'.$ext2.'</td>';
  201. $arr = $fullcovers[$urk];
  202. $narr = $nearlycovers[$urk];
  203. if (is_array($arr)) { $html .= '<td>'. implode(', ',$arr). '</td></tr>'; }
  204. else if (is_array($narr)) { $html .= '<td style="background-color: #AAAAAA;">'. implode(', ',$narr). ' (>90%)</td></tr>'; }
  205. else { $html .= '<td style="background-color: #555555;"> </td></tr>'; }
  206. }
  207. $html.= '</table>';
  208. //==============================================================
  209. echo $html;
  210. exit;
  211. //==============================================================
  212. //==============================================================
  213. //==============================================================
  214. //==============================================================
  215. ?>