2 /*******************************************************************************
3 * Utility to generate font definition files *
6 *******************************************************************************/
11 $file=dirname(__FILE__
).'/'.strtolower($enc).'.map';
14 die('<B>Error:</B> encoding not found: '.$enc);
20 $e=preg_split('/[ \\t]+/',rtrim($l));
21 $cc=hexdec(substr($e[0],1));
26 for($i=0;$i<=255;$i++
)
28 if(!isset($cc2gn[$i]))
34 function ReadAFM($file,&$map)
36 //Read a font metric file
39 die('File not found');
42 $fix=array('Edot'=>'Edotaccent','edot'=>'edotaccent','Idot'=>'Idotaccent','Zdot'=>'Zdotaccent','zdot'=>'zdotaccent',
43 'Odblacute'=>'Ohungarumlaut','odblacute'=>'ohungarumlaut','Udblacute'=>'Uhungarumlaut','udblacute'=>'uhungarumlaut',
44 'Gcedilla'=>'Gcommaaccent','gcedilla'=>'gcommaaccent','Kcedilla'=>'Kcommaaccent','kcedilla'=>'kcommaaccent',
45 'Lcedilla'=>'Lcommaaccent','lcedilla'=>'lcommaaccent','Ncedilla'=>'Ncommaaccent','ncedilla'=>'ncommaaccent',
46 'Rcedilla'=>'Rcommaaccent','rcedilla'=>'rcommaaccent','Scedilla'=>'Scommaaccent','scedilla'=>'scommaaccent',
47 'Tcedilla'=>'Tcommaaccent','tcedilla'=>'tcommaaccent','Dslash'=>'Dcroat','dslash'=>'dcroat','Dmacron'=>'Dcroat','dmacron'=>'dcroat',
48 'combininggraveaccent'=>'gravecomb','combininghookabove'=>'hookabovecomb','combiningtildeaccent'=>'tildecomb',
49 'combiningacuteaccent'=>'acutecomb','combiningdotbelow'=>'dotbelowcomb','dongsign'=>'dong');
52 $e=explode(' ',rtrim($l));
63 if(substr($gn,-4)=='20AC')
67 //Fix incorrect glyph name
68 foreach($map as $c=>$n)
76 //Symbolic font: use built-in encoding
83 $fm['CapXHeight']=$e[13];
86 $fm['MissingWidth']=$w;
88 elseif($code=='FontName')
89 $fm['FontName']=$param;
90 elseif($code=='Weight')
92 elseif($code=='ItalicAngle')
93 $fm['ItalicAngle']=(double)$param;
94 elseif($code=='Ascender')
95 $fm['Ascender']=(int)$param;
96 elseif($code=='Descender')
97 $fm['Descender']=(int)$param;
98 elseif($code=='UnderlineThickness')
99 $fm['UnderlineThickness']=(int)$param;
100 elseif($code=='UnderlinePosition')
101 $fm['UnderlinePosition']=(int)$param;
102 elseif($code=='IsFixedPitch')
103 $fm['IsFixedPitch']=($param=='true');
104 elseif($code=='FontBBox')
105 $fm['FontBBox']=array($e[1],$e[2],$e[3],$e[4]);
106 elseif($code=='CapHeight')
107 $fm['CapHeight']=(int)$param;
108 elseif($code=='StdVW')
109 $fm['StdVW']=(int)$param;
111 if(!isset($fm['FontName']))
112 die('FontName not found');
115 if(!isset($widths['.notdef']))
116 $widths['.notdef']=600;
117 if(!isset($widths['Delta']) and isset($widths['increment']))
118 $widths['Delta']=$widths['increment'];
119 //Order widths according to map
120 for($i=0;$i<=255;$i++
)
122 if(!isset($widths[$map[$i]]))
124 echo '<B>Warning:</B> character '.$map[$i].' is missing<BR>';
125 $widths[$i]=$widths['.notdef'];
128 $widths[$i]=$widths[$map[$i]];
131 $fm['Widths']=$widths;
135 function MakeFontDescriptor($fm,$symbolic)
138 $asc=(isset($fm['Ascender']) ?
$fm['Ascender'] : 1000);
139 $fd="array('Ascent'=>".$asc;
141 $desc=(isset($fm['Descender']) ?
$fm['Descender'] : -200);
142 $fd.=",'Descent'=>".$desc;
144 if(isset($fm['CapHeight']))
145 $ch=$fm['CapHeight'];
146 elseif(isset($fm['CapXHeight']))
147 $ch=$fm['CapXHeight'];
150 $fd.=",'CapHeight'=>".$ch;
153 if(isset($fm['IsFixedPitch']) and $fm['IsFixedPitch'])
159 if(isset($fm['ItalicAngle']) and $fm['ItalicAngle']!=0)
161 $fd.=",'Flags'=>".$flags;
163 if(isset($fm['FontBBox']))
164 $fbb=$fm['FontBBox'];
166 $fbb=array(0,$des-100,1000,$asc+
100);
167 $fd.=",'FontBBox'=>'[".$fbb[0].' '.$fbb[1].' '.$fbb[2].' '.$fbb[3]."]'";
169 $ia=(isset($fm['ItalicAngle']) ?
$fm['ItalicAngle'] : 0);
170 $fd.=",'ItalicAngle'=>".$ia;
172 if(isset($fm['StdVW']))
174 elseif(isset($fm['Weight']) and eregi('(bold|black)',$fm['Weight']))
178 $fd.=",'StemV'=>".$stemv;
180 if(isset($fm['MissingWidth']))
181 $fd.=",'MissingWidth'=>".$fm['MissingWidth'];
186 function MakeWidthArray($fm)
188 //Make character width array
191 for($i=0;$i<=255;$i++
)
195 elseif(chr($i)=="\\")
197 elseif($i>=32 and $i<=126)
201 $s.='=>'.$fm['Widths'][$i];
211 function MakeFontEncoding($map)
213 //Build differences from reference encoding
214 $ref=ReadMap('cp1252');
217 for($i=32;$i<=255;$i++
)
219 if($map[$i]!=$ref[$i])
224 $s.='/'.$map[$i].' ';
230 function SaveToFile($file,$s,$mode='t')
232 $f=fopen($file,'w'.$mode);
234 die('Can\'t write to file '.$file);
235 fwrite($f,$s,strlen($s));
239 function ReadShort($f)
241 $a=unpack('n1n',fread($f,2));
245 function ReadLong($f)
247 $a=unpack('N1N',fread($f,4));
251 function CheckTTF($file)
253 //Check if font license allows embedding
254 $f=fopen($file,'rb');
256 die('<B>Error:</B> Can\'t open '.$file);
257 //Extract number of tables
258 fseek($f,4,SEEK_CUR
);
260 fseek($f,6,SEEK_CUR
);
263 for($i=0;$i<$nb;$i++
)
265 if(fread($f,4)=='OS/2')
270 fseek($f,12,SEEK_CUR
);
277 fseek($f,4,SEEK_CUR
);
278 $offset=ReadLong($f);
279 fseek($f,$offset,SEEK_SET
);
280 //Extract fsType flags
281 fseek($f,8,SEEK_CUR
);
282 $fsType=ReadShort($f);
283 $rl=($fsType & 0x02)!=0;
284 $pp=($fsType & 0x04)!=0;
285 $e=($fsType & 0x08)!=0;
287 if($rl and !$pp and !$e)
288 echo '<B>Warning:</B> font license does not allow embedding';
291 /*******************************************************************************
292 * $fontfile : chemin du fichier TTF (ou chaîne vide si pas d'incorporation) *
293 * $afmfile : chemin du fichier AFM *
294 * $enc : encodage (ou chaîne vide si la police est symbolique) *
295 * $patch : patch optionnel pour l'encodage *
296 * $type : type de la police si $fontfile est vide *
297 *******************************************************************************/
298 function MakeFont($fontfile,$afmfile,$enc='cp1252',$patch=array(),$type='TrueType')
300 //Generate a font definition file
301 set_magic_quotes_runtime(0);
302 ini_set('auto_detect_line_endings','1');
306 foreach($patch as $cc=>$gn)
311 if(!file_exists($afmfile))
312 die('<B>Error:</B> AFM file not found: '.$afmfile);
313 $fm=ReadAFM($afmfile,$map);
315 $diff=MakeFontEncoding($map);
318 $fd=MakeFontDescriptor($fm,empty($map));
322 $ext=strtolower(substr($fontfile,-3));
328 die('<B>Error:</B> unrecognized font file extension: '.$ext);
332 if($type!='TrueType' and $type!='Type1')
333 die('<B>Error:</B> incorrect font type: '.$type);
337 $s.='$type=\''.$type."';\n";
338 $s.='$name=\''.$fm['FontName']."';\n";
339 $s.='$desc='.$fd.";\n";
340 if(!isset($fm['UnderlinePosition']))
341 $fm['UnderlinePosition']=-100;
342 if(!isset($fm['UnderlineThickness']))
343 $fm['UnderlineThickness']=50;
344 $s.='$up='.$fm['UnderlinePosition'].";\n";
345 $s.='$ut='.$fm['UnderlineThickness'].";\n";
346 $w=MakeWidthArray($fm);
348 $s.='$enc=\''.$enc."';\n";
349 $s.='$diff=\''.$diff."';\n";
350 $basename=substr(basename($afmfile),0,-4);
354 if(!file_exists($fontfile))
355 die('<B>Error:</B> font file not found: '.$fontfile);
356 if($type=='TrueType')
358 $f=fopen($fontfile,'rb');
360 die('<B>Error:</B> Can\'t open '.$fontfile);
361 $file=fread($f,filesize($fontfile));
365 //Find first two sections and discard third one
366 $header=(ord($file{0})==128);
369 //Strip first binary header
370 $file=substr($file,6);
372 $pos=strpos($file,'eexec');
374 die('<B>Error:</B> font file does not seem to be valid Type1');
376 if($header and ord($file{$size1})==128)
378 //Strip second binary header
379 $file=substr($file,0,$size1).substr($file,$size1+
6);
381 $pos=strpos($file,'00000000');
383 die('<B>Error:</B> font file does not seem to be valid Type1');
385 $file=substr($file,0,$size1+
$size2);
387 if(function_exists('gzcompress'))
390 SaveToFile($cmp,gzcompress($file),'b');
391 $s.='$file=\''.$cmp."';\n";
392 echo 'Font file compressed ('.$cmp.')<BR>';
396 $s.='$file=\''.basename($fontfile)."';\n";
397 echo '<B>Notice:</B> font file could not be compressed (zlib extension not available)<BR>';
401 $s.='$size1='.$size1.";\n";
402 $s.='$size2='.$size2.";\n";
405 $s.='$originalsize='.filesize($fontfile).";\n";
410 $s.='$file='."'';\n";
413 SaveToFile($basename.'.php',$s);
414 echo 'Font definition file generated ('.$basename.'.php'.')<BR>';