font_coverage.php 7.97 KB
Newer Older
Nahla Shiri's avatar
Nahla Shiri committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
<?php

/* This script prints out the Unicode coverage of all TrueType font files
   in your font directory. 
   Point your browser to 
   http://your.domain/your_path_to _mpdf/utils/font_coverage.php
   By default this will examine the folder /ttfonts/ (or the default font
   directory defined by _MPDF_TTFONTPATH.
   You can optionally define an alternative folder to examine by setting 
   the variable below (must be a relative path, or filesystem path):
*/


$checkdir = '';

//////////////////////////////////


set_time_limit(600);
ini_set("memory_limit","256M");

//==============================================================
//==============================================================
include("../mpdf.php");

$mpdf=new mPDF('','A4-L','','',10,10,10,10); 
$mpdf->SetDisplayMode('fullpage');
$mpdf->useSubstitutions = true;
$mpdf->debug = true;
$mpdf->simpleTables = true;
if ($checkdir) { 
	$ttfdir = $checkdir;
}
else { $ttfdir = _MPDF_TTFONTPATH; }



$mqr=ini_get("magic_quotes_runtime");
if ($mqr) { set_magic_quotes_runtime(0); }
if (!class_exists('TTFontFile_Analysis', false)) { include(_MPDF_PATH .'classes/ttfontsuni_analysis.php'); }

//==============================================================
	$smp = true;
	$maxt = 131071;
//==============================================================
//==============================================================
$unifile = file('UnicodeData.txt');
$unichars = array();
foreach($unifile AS $line) {
	if (preg_match('/<control>/',$line,$m)) { 
	  $rangename = '';
	  continue;
	}
	else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});<(.*?), Last>/',$line,$m)) { 
	  if ($rangename && $rangename == $m[2]) {
		$endrange = hexdec($m[1]);
		for ($i=$startrange;$i<=$endrange; $i++) {
			$unichars[$i] = $i;
		}
	  }
	  $rangename = '';
	}
	else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});<(.*?), First>/',$line,$m)) { 
	  $startrange = hexdec($m[1]);
	  $rangename = $m[2];
	}
	else if (preg_match('/^([12]{0,1}[0-9A-Za-z]{4});/',$line,$m)) { 
	  $unichars[hexdec($m[1])] = hexdec($m[1]);
	  $rangename = '';
	}
}

// loads array $unicode_ranges
include('UnicodeRanges.php'); 
//==============================================================
$html = '<html><head><style>td { border: 0.1mm solid #555555; } 
body { font-weight: normal; font-family: helvetica;font-size:8pt; }
td { font-family: helvetica;font-size:8pt; vertical-align: top;}
</style></head><body>';

//==============================================================
$ff = scandir($ttfdir);
$tempfontdata = array();
foreach($ff AS $f) {
	$ttf = new TTFontFile_Analysis();
	$ret = array();
	$isTTC = false;
	if (strtolower(substr($f,-4,4))=='.ttf' || strtolower(substr($f,-4,4))=='.otf') {
		$ret[] = $ttf->extractCoreInfo($ttfdir.$f);
	}
	for ($i=0; $i<count($ret); $i++) {
	   if (is_array($ret[$i])) { 
		$tfname = $ret[$i][0];
		$bold = $ret[$i][1];
		$italic = $ret[$i][2];
		$fname = strtolower($tfname );
		$fname = preg_replace('/[ ()]/','',$fname );
		//$tempfonttrans[$tfname] = $fname;
		$style = '';
		if ($bold) { $style .= 'B'; }
		if ($italic) { $style .= 'I'; }
		if (!$style) { 
			$tempfontdata[$fname]['file'] = $f;
			if ($isTTC) { 
				$tempfontdata[$fname]['TTCfontID'] = $ret[$i][4];
			}
		}
	   }
	}
	unset($ttf);

}

$fullcovers = array();
$nearlycovers = array();
ksort($tempfontdata);
$ningroup = 14;
$nofgroups = ceil(count($unicode_ranges)/$ningroup);

//==============================================================


for ($urgp = 0; $urgp < $nofgroups; $urgp++) {


   $html .= '<table cellpadding="2" cellspacing="0" style="page-break-inside:avoid; text-align:center; border-collapse: collapse; ">';
   $html .= '<thead><tr><td></td>';

   foreach($unicode_ranges AS $urk => $ur) {
	   if ($urk >= ($urgp*$ningroup) && $urk < (($urgp+1)*$ningroup)) {
				$rangekey = $urk;
				$range = $ur['range'];
				$rangestart = $ur['starthex'];
				$rangeend = $ur['endhex'];
				$html .= '<td style="font-family:helvetica;font-size:8pt;font-weight:bold;">'.strtoupper($range).' (U+'.$rangestart .'-U+'.$rangeend.')</td>';
	   }
   }
   $html .= '</tr></thead>';


  foreach ($tempfontdata AS $fname => $v) {
	$cw = '';
	if (file_exists((_MPDF_TTFONTDATAPATH.$fname.'.cw.dat'))) { $cw = file_get_contents(_MPDF_TTFONTDATAPATH.$fname.'.cw.dat'); }
	else {
		$mpdf->fontdata[$fname]['R'] = $tempfontdata[$fname]['file']; 
		$mpdf->AddFont($fname);
		$cw = file_get_contents(_MPDF_TTFONTDATAPATH.$fname.'.cw.dat'); 
	}
	if (!$cw) {
		continue;
		die("Font data not available for $fname");
	}

	$counter=0;
	$max = $maxt;

	// create HTML content
	$html .= '<tr>';
	$html .= '<td>'.$fname.'</td>';

	foreach($unicode_ranges AS $urk => $ur) {
	   if ($urk >= ($urgp*$ningroup) && $urk < (($urgp+1)*$ningroup)) {
	    if ($ur['pua'] || $ur['reserved'] || $ur['control']) {
		$html .= '<td style="background-color: #000000;"></td>'; 
	    }
	    else {
		$rangekey = $urk;
		$range = $ur['range'];
		$rangestart = $ur['starthex'];
		$rangeend = $ur['endhex'];
		$rangestartdec = $ur['startdec'];
		$rangeenddec = $ur['enddec'];
		$uniinrange = 0;
		$fontinrange = 0;
		for ($i=$rangestartdec; $i<=$rangeenddec; $i++) {
			//if (isset($cw[$i])) { $fontinrange++; }
			if ($mpdf->_charDefined($cw, $i)) { $fontinrange++; }
			if (isset($unichars[$i])) { $uniinrange++; }
		}
		if ($uniinrange) {
			if ($fontinrange) {
				$pc = ($fontinrange/$uniinrange);
				$str = '('.$fontinrange.'/'.$uniinrange.')';
				if ($pc==1) { 
					$fullcovers[$urk][] = $fname;
					$html .= '<td style="background-color: #00FF00;"></td>'; 
				}
				else if ($pc>1) { 
					$fullcovers[$urk][] = $fname;
					$html .= '<td style="background-color: #00FF00;">'.$str.'</td>'; 
				}
				else if ($pc>=0.9) { 
					$html .= '<td style="background-color: #AAFFAA;">'.$str.'</td>'; 
					$nearlycovers[$urk][] = $fname;
				}
				else if ($pc>0.75) { $html .= '<td style="background-color: #00FFAA;">'.$str.'</td>'; }
				else if ($pc>0.5) { $html .= '<td style="background-color: #AAAAFF;">'.$str.'</td>'; }
				else if ($pc>0.25) { $html .= '<td style="background-color: #FFFFAA;">'.$str.'</td>'; }
				else { $html .= '<td style="background-color: #FFAAAA;">'.$str.'</td>'; }
			}
			else { $html .= '<td style="background-color: #555555;">(0/0)</td>'; }
		}
		else { $html .= '<td style="background-color: #000000;"></td>'; }
	    }
	   }
	}


	$html .= '</tr>';

   }
//==============================================================
  $html .= '</table><pagebreak />';
}

$html .= '<h4>Fonts with full coverage of Unicode Ranges</h4>';
$html .= '<table>';
//$html .= '<tr><td></td><td></td></tr>';
foreach($unicode_ranges AS $urk => $ur) {
	if ($ur['pua'] || $ur['reserved'] || $ur['control']) { continue; }
	$rangekey = $urk;
	$range = $ur['range'];
	$rangestart = $ur['starthex'];
	$rangeend = $ur['endhex'];
	$ext = $ext2 = '';
	if ($ur['combining']) { $ext = 'background-color:#DDDDFF;'; $ext2 = '<br /><span style="color:#AA0000">Special positioning required</span>'; }
	if ($ur['vertical']) { $ext = 'background-color:#FFDDDD;'; $ext2 = '<br /><span style="color:#AA0000">Vertical positioning required</span>'; }
	if ($ur['special']) { $ext = 'background-color:#FFDDDD;'; $ext2 = '<br /><span style="color:#AA0000">Special processing required</span>'; }


	$html .= '<tr><td style="font-family:helvetica;font-size:8pt;font-weight:bold;'.$ext.'">'.strtoupper($range).' (U+'.$rangestart .'-U+'.$rangeend.')'.$ext2.'</td>';
	$arr = $fullcovers[$urk];
	$narr = $nearlycovers[$urk];
	if (is_array($arr)) { $html .= '<td>'. implode(', ',$arr). '</td></tr>'; }
	else if (is_array($narr)) { $html .= '<td style="background-color: #AAAAAA;">'. implode(', ',$narr). ' (>90%)</td></tr>'; }
	else { $html .= '<td style="background-color: #555555;"> </td></tr>'; }
}
$html.= '</table>';


//==============================================================
echo $html;

exit;

//==============================================================
//==============================================================
//==============================================================
//==============================================================


?>