font_coverage.php 7.9 KB

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