2 /*******************************************************************************
3 * Utility to generate font definition files *
7 * Author: Olivier PLATHEY *
8 *******************************************************************************/
10 function ReadMap($enc)
13 $file=dirname(__FILE__
).'/'.strtolower($enc).'.map';
16 die('<b>Error:</b> encoding not found: '.$enc);
22 $e=preg_split('/[ \\t]+/',rtrim($l));
23 $cc=hexdec(substr($e[0],1));
28 for($i=0;$i<=255;$i++
)
30 if(!isset($cc2gn[$i]))
36 function ReadAFM($file, &$map)
38 //Read a font metric file
41 die('File not found');
44 $fix=array('Edot'=>'Edotaccent','edot'=>'edotaccent','Idot'=>'Idotaccent','Zdot'=>'Zdotaccent','zdot'=>'zdotaccent',
45 'Odblacute'=>'Ohungarumlaut','odblacute'=>'ohungarumlaut','Udblacute'=>'Uhungarumlaut','udblacute'=>'uhungarumlaut',
46 'Gcedilla'=>'Gcommaaccent','gcedilla'=>'gcommaaccent','Kcedilla'=>'Kcommaaccent','kcedilla'=>'kcommaaccent',
47 'Lcedilla'=>'Lcommaaccent','lcedilla'=>'lcommaaccent','Ncedilla'=>'Ncommaaccent','ncedilla'=>'ncommaaccent',
48 'Rcedilla'=>'Rcommaaccent','rcedilla'=>'rcommaaccent','Scedilla'=>'Scommaaccent','scedilla'=>'scommaaccent',
49 'Tcedilla'=>'Tcommaaccent','tcedilla'=>'tcommaaccent','Dslash'=>'Dcroat','dslash'=>'dcroat','Dmacron'=>'Dcroat','dmacron'=>'dcroat',
50 'combininggraveaccent'=>'gravecomb','combininghookabove'=>'hookabovecomb','combiningtildeaccent'=>'tildecomb',
51 'combiningacuteaccent'=>'acutecomb','combiningdotbelow'=>'dotbelowcomb','dongsign'=>'dong');
54 $e=explode(' ',rtrim($l));
65 if(substr($gn,-4)=='20AC')
69 //Fix incorrect glyph name
70 foreach($map as $c=>$n)
78 //Symbolic font: use built-in encoding
85 $fm['CapXHeight']=$e[13];
88 $fm['MissingWidth']=$w;
90 elseif($code=='FontName')
91 $fm['FontName']=$param;
92 elseif($code=='Weight')
94 elseif($code=='ItalicAngle')
95 $fm['ItalicAngle']=(double)$param;
96 elseif($code=='Ascender')
97 $fm['Ascender']=(int)$param;
98 elseif($code=='Descender')
99 $fm['Descender']=(int)$param;
100 elseif($code=='UnderlineThickness')
101 $fm['UnderlineThickness']=(int)$param;
102 elseif($code=='UnderlinePosition')
103 $fm['UnderlinePosition']=(int)$param;
104 elseif($code=='IsFixedPitch')
105 $fm['IsFixedPitch']=($param=='true');
106 elseif($code=='FontBBox')
107 $fm['FontBBox']=array($e[1],$e[2],$e[3],$e[4]);
108 elseif($code=='CapHeight')
109 $fm['CapHeight']=(int)$param;
110 elseif($code=='StdVW')
111 $fm['StdVW']=(int)$param;
113 if(!isset($fm['FontName']))
114 die('FontName not found');
117 if(!isset($widths['.notdef']))
118 $widths['.notdef']=600;
119 if(!isset($widths['Delta']) && isset($widths['increment']))
120 $widths['Delta']=$widths['increment'];
121 //Order widths according to map
122 for($i=0;$i<=255;$i++
)
124 if(!isset($widths[$map[$i]]))
126 echo '<b>Warning:</b> character '.$map[$i].' is missing<br>';
127 $widths[$i]=$widths['.notdef'];
130 $widths[$i]=$widths[$map[$i]];
133 $fm['Widths']=$widths;
137 function MakeFontDescriptor($fm, $symbolic)
140 $asc=(isset($fm['Ascender']) ?
$fm['Ascender'] : 1000);
141 $fd="array('Ascent'=>".$asc;
143 $desc=(isset($fm['Descender']) ?
$fm['Descender'] : -200);
144 $fd.=",'Descent'=>".$desc;
146 if(isset($fm['CapHeight']))
147 $ch=$fm['CapHeight'];
148 elseif(isset($fm['CapXHeight']))
149 $ch=$fm['CapXHeight'];
152 $fd.=",'CapHeight'=>".$ch;
155 if(isset($fm['IsFixedPitch']) && $fm['IsFixedPitch'])
161 if(isset($fm['ItalicAngle']) && $fm['ItalicAngle']!=0)
163 $fd.=",'Flags'=>".$flags;
165 if(isset($fm['FontBBox']))
166 $fbb=$fm['FontBBox'];
168 $fbb=array(0,$desc-100,1000,$asc+
100);
169 $fd.=",'FontBBox'=>'[".$fbb[0].' '.$fbb[1].' '.$fbb[2].' '.$fbb[3]."]'";
171 $ia=(isset($fm['ItalicAngle']) ?
$fm['ItalicAngle'] : 0);
172 $fd.=",'ItalicAngle'=>".$ia;
174 if(isset($fm['StdVW']))
176 elseif(isset($fm['Weight']) && preg_match('/bold|black/i',$fm['Weight']))
180 $fd.=",'StemV'=>".$stemv;
182 if(isset($fm['MissingWidth']))
183 $fd.=",'MissingWidth'=>".$fm['MissingWidth'];
188 function MakeWidthArray($fm)
190 //Make character width array
193 for($i=0;$i<=255;$i++
)
197 elseif(chr($i)=="\\")
199 elseif($i>=32 && $i<=126)
203 $s.='=>'.$fm['Widths'][$i];
213 function MakeFontEncoding($map)
215 //Build differences from reference encoding
216 $ref=ReadMap('cp1252');
219 for($i=32;$i<=255;$i++
)
221 if($map[$i]!=$ref[$i])
226 $s.='/'.$map[$i].' ';
232 function SaveToFile($file, $s, $mode)
234 $f=fopen($file,'w'.$mode);
236 die('Can\'t write to file '.$file);
237 fwrite($f,$s,strlen($s));
241 function ReadShort($f)
243 $a=unpack('n1n',fread($f,2));
247 function ReadLong($f)
249 $a=unpack('N1N',fread($f,4));
253 function CheckTTF($file)
255 //Check if font license allows embedding
256 $f=fopen($file,'rb');
258 die('<b>Error:</b> Can\'t open '.$file);
259 //Extract number of tables
260 fseek($f,4,SEEK_CUR
);
262 fseek($f,6,SEEK_CUR
);
265 for($i=0;$i<$nb;$i++
)
267 if(fread($f,4)=='OS/2')
272 fseek($f,12,SEEK_CUR
);
279 fseek($f,4,SEEK_CUR
);
280 $offset=ReadLong($f);
281 fseek($f,$offset,SEEK_SET
);
282 //Extract fsType flags
283 fseek($f,8,SEEK_CUR
);
284 $fsType=ReadShort($f);
285 $rl=($fsType & 0x02)!=0;
286 $pp=($fsType & 0x04)!=0;
287 $e=($fsType & 0x08)!=0;
289 if($rl && !$pp && !$e)
290 echo '<b>Warning:</b> font license does not allow embedding';
293 /*******************************************************************************
294 * fontfile: path to TTF file (or empty string if not to be embedded) *
295 * afmfile: path to AFM file *
296 * enc: font encoding (or empty string for symbolic fonts) *
297 * patch: optional patch for encoding *
298 * type: font type if fontfile is empty *
299 *******************************************************************************/
300 function MakeFont($fontfile, $afmfile, $enc='cp1252', $patch=array(), $type='TrueType')
302 //Generate a font definition file
303 if(get_magic_quotes_runtime())
304 @set_magic_quotes_runtime
(0);
305 ini_set('auto_detect_line_endings','1');
309 foreach($patch as $cc=>$gn)
314 if(!file_exists($afmfile))
315 die('<b>Error:</b> AFM file not found: '.$afmfile);
316 $fm=ReadAFM($afmfile,$map);
318 $diff=MakeFontEncoding($map);
321 $fd=MakeFontDescriptor($fm,empty($map));
325 $ext=strtolower(substr($fontfile,-3));
331 die('<b>Error:</b> unrecognized font file extension: '.$ext);
335 if($type!='TrueType' && $type!='Type1')
336 die('<b>Error:</b> incorrect font type: '.$type);
340 $s.='$type=\''.$type."';\n";
341 $s.='$name=\''.$fm['FontName']."';\n";
342 $s.='$desc='.$fd.";\n";
343 if(!isset($fm['UnderlinePosition']))
344 $fm['UnderlinePosition']=-100;
345 if(!isset($fm['UnderlineThickness']))
346 $fm['UnderlineThickness']=50;
347 $s.='$up='.$fm['UnderlinePosition'].";\n";
348 $s.='$ut='.$fm['UnderlineThickness'].";\n";
349 $w=MakeWidthArray($fm);
351 $s.='$enc=\''.$enc."';\n";
352 $s.='$diff=\''.$diff."';\n";
353 $basename=substr(basename($afmfile),0,-4);
357 if(!file_exists($fontfile))
358 die('<b>Error:</b> font file not found: '.$fontfile);
359 if($type=='TrueType')
361 $f=fopen($fontfile,'rb');
363 die('<b>Error:</b> Can\'t open '.$fontfile);
364 $file=fread($f,filesize($fontfile));
368 //Find first two sections and discard third one
369 $header=(ord($file[0])==128);
372 //Strip first binary header
373 $file=substr($file,6);
375 $pos=strpos($file,'eexec');
377 die('<b>Error:</b> font file does not seem to be valid Type1');
379 if($header && ord($file[$size1])==128)
381 //Strip second binary header
382 $file=substr($file,0,$size1).substr($file,$size1+
6);
384 $pos=strpos($file,'00000000');
386 die('<b>Error:</b> font file does not seem to be valid Type1');
388 $file=substr($file,0,$size1+
$size2);
390 if(function_exists('gzcompress'))
393 SaveToFile($cmp,gzcompress($file),'b');
394 $s.='$file=\''.$cmp."';\n";
395 echo 'Font file compressed ('.$cmp.')<br>';
399 $s.='$file=\''.basename($fontfile)."';\n";
400 echo '<b>Notice:</b> font file could not be compressed (zlib extension not available)<br>';
404 $s.='$size1='.$size1.";\n";
405 $s.='$size2='.$size2.";\n";
408 $s.='$originalsize='.filesize($fontfile).";\n";
413 $s.='$file='."'';\n";
416 SaveToFile($basename.'.php',$s,'t');
417 echo 'Font definition file generated ('.$basename.'.php'.')<br>';