+++ /dev/null
-<?php\r
-/*******************************************************************************\r
-* Utility to generate font definition files *\r
-* Version: 1.13 *\r
-* Date: 2004-12-31 *\r
-*******************************************************************************/\r
-\r
-function ReadMap($enc)\r
-{\r
- //Read a map file\r
- $file=dirname(__FILE__).'/'.strtolower($enc).'.map';\r
- $a=file($file);\r
- if(empty($a))\r
- die('<B>Error:</B> encoding not found: '.$enc);\r
- $cc2gn=array();\r
- foreach($a as $l)\r
- {\r
- if($l{0}=='!')\r
- {\r
- $e=preg_split('/[ \\t]+/',rtrim($l));\r
- $cc=hexdec(substr($e[0],1));\r
- $gn=$e[2];\r
- $cc2gn[$cc]=$gn;\r
- }\r
- }\r
- for($i=0;$i<=255;$i++)\r
- {\r
- if(!isset($cc2gn[$i]))\r
- $cc2gn[$i]='.notdef';\r
- }\r
- return $cc2gn;\r
-}\r
-\r
-function ReadAFM($file,&$map)\r
-{\r
- //Read a font metric file\r
- $a=file($file);\r
- if(empty($a))\r
- die('File not found');\r
- $widths=array();\r
- $fm=array();\r
- $fix=array('Edot'=>'Edotaccent','edot'=>'edotaccent','Idot'=>'Idotaccent','Zdot'=>'Zdotaccent','zdot'=>'zdotaccent',\r
- 'Odblacute'=>'Ohungarumlaut','odblacute'=>'ohungarumlaut','Udblacute'=>'Uhungarumlaut','udblacute'=>'uhungarumlaut',\r
- 'Gcedilla'=>'Gcommaaccent','gcedilla'=>'gcommaaccent','Kcedilla'=>'Kcommaaccent','kcedilla'=>'kcommaaccent',\r
- 'Lcedilla'=>'Lcommaaccent','lcedilla'=>'lcommaaccent','Ncedilla'=>'Ncommaaccent','ncedilla'=>'ncommaaccent',\r
- 'Rcedilla'=>'Rcommaaccent','rcedilla'=>'rcommaaccent','Scedilla'=>'Scommaaccent','scedilla'=>'scommaaccent',\r
- 'Tcedilla'=>'Tcommaaccent','tcedilla'=>'tcommaaccent','Dslash'=>'Dcroat','dslash'=>'dcroat','Dmacron'=>'Dcroat','dmacron'=>'dcroat',\r
- 'combininggraveaccent'=>'gravecomb','combininghookabove'=>'hookabovecomb','combiningtildeaccent'=>'tildecomb',\r
- 'combiningacuteaccent'=>'acutecomb','combiningdotbelow'=>'dotbelowcomb','dongsign'=>'dong');\r
- foreach($a as $l)\r
- {\r
- $e=explode(' ',rtrim($l));\r
- if(count($e)<2)\r
- continue;\r
- $code=$e[0];\r
- $param=$e[1];\r
- if($code=='C')\r
- {\r
- //Character metrics\r
- $cc=(int)$e[1];\r
- $w=$e[4];\r
- $gn=$e[7];\r
- if(substr($gn,-4)=='20AC')\r
- $gn='Euro';\r
- if(isset($fix[$gn]))\r
- {\r
- //Fix incorrect glyph name\r
- foreach($map as $c=>$n)\r
- {\r
- if($n==$fix[$gn])\r
- $map[$c]=$gn;\r
- }\r
- }\r
- if(empty($map))\r
- {\r
- //Symbolic font: use built-in encoding\r
- $widths[$cc]=$w;\r
- }\r
- else\r
- {\r
- $widths[$gn]=$w;\r
- if($gn=='X')\r
- $fm['CapXHeight']=$e[13];\r
- }\r
- if($gn=='.notdef')\r
- $fm['MissingWidth']=$w;\r
- }\r
- elseif($code=='FontName')\r
- $fm['FontName']=$param;\r
- elseif($code=='Weight')\r
- $fm['Weight']=$param;\r
- elseif($code=='ItalicAngle')\r
- $fm['ItalicAngle']=(double)$param;\r
- elseif($code=='Ascender')\r
- $fm['Ascender']=(int)$param;\r
- elseif($code=='Descender')\r
- $fm['Descender']=(int)$param;\r
- elseif($code=='UnderlineThickness')\r
- $fm['UnderlineThickness']=(int)$param;\r
- elseif($code=='UnderlinePosition')\r
- $fm['UnderlinePosition']=(int)$param;\r
- elseif($code=='IsFixedPitch')\r
- $fm['IsFixedPitch']=($param=='true');\r
- elseif($code=='FontBBox')\r
- $fm['FontBBox']=array($e[1],$e[2],$e[3],$e[4]);\r
- elseif($code=='CapHeight')\r
- $fm['CapHeight']=(int)$param;\r
- elseif($code=='StdVW')\r
- $fm['StdVW']=(int)$param;\r
- }\r
- if(!isset($fm['FontName']))\r
- die('FontName not found');\r
- if(!empty($map))\r
- {\r
- if(!isset($widths['.notdef']))\r
- $widths['.notdef']=600;\r
- if(!isset($widths['Delta']) and isset($widths['increment']))\r
- $widths['Delta']=$widths['increment'];\r
- //Order widths according to map\r
- for($i=0;$i<=255;$i++)\r
- {\r
- if(!isset($widths[$map[$i]]))\r
- {\r
- echo '<B>Warning:</B> character '.$map[$i].' is missing<BR>';\r
- $widths[$i]=$widths['.notdef'];\r
- }\r
- else\r
- $widths[$i]=$widths[$map[$i]];\r
- }\r
- }\r
- $fm['Widths']=$widths;\r
- return $fm;\r
-}\r
-\r
-function MakeFontDescriptor($fm,$symbolic)\r
-{\r
- //Ascent\r
- $asc=(isset($fm['Ascender']) ? $fm['Ascender'] : 1000);\r
- $fd="array('Ascent'=>".$asc;\r
- //Descent\r
- $desc=(isset($fm['Descender']) ? $fm['Descender'] : -200);\r
- $fd.=",'Descent'=>".$desc;\r
- //CapHeight\r
- if(isset($fm['CapHeight']))\r
- $ch=$fm['CapHeight'];\r
- elseif(isset($fm['CapXHeight']))\r
- $ch=$fm['CapXHeight'];\r
- else\r
- $ch=$asc;\r
- $fd.=",'CapHeight'=>".$ch;\r
- //Flags\r
- $flags=0;\r
- if(isset($fm['IsFixedPitch']) and $fm['IsFixedPitch'])\r
- $flags+=1<<0;\r
- if($symbolic)\r
- $flags+=1<<2;\r
- if(!$symbolic)\r
- $flags+=1<<5;\r
- if(isset($fm['ItalicAngle']) and $fm['ItalicAngle']!=0)\r
- $flags+=1<<6;\r
- $fd.=",'Flags'=>".$flags;\r
- //FontBBox\r
- if(isset($fm['FontBBox']))\r
- $fbb=$fm['FontBBox'];\r
- else\r
- $fbb=array(0,$des-100,1000,$asc+100);\r
- $fd.=",'FontBBox'=>'[".$fbb[0].' '.$fbb[1].' '.$fbb[2].' '.$fbb[3]."]'";\r
- //ItalicAngle\r
- $ia=(isset($fm['ItalicAngle']) ? $fm['ItalicAngle'] : 0);\r
- $fd.=",'ItalicAngle'=>".$ia;\r
- //StemV\r
- if(isset($fm['StdVW']))\r
- $stemv=$fm['StdVW'];\r
- elseif(isset($fm['Weight']) and eregi('(bold|black)',$fm['Weight']))\r
- $stemv=120;\r
- else\r
- $stemv=70;\r
- $fd.=",'StemV'=>".$stemv;\r
- //MissingWidth\r
- if(isset($fm['MissingWidth']))\r
- $fd.=",'MissingWidth'=>".$fm['MissingWidth'];\r
- $fd.=')';\r
- return $fd;\r
-}\r
-\r
-function MakeWidthArray($fm)\r
-{\r
- //Make character width array\r
- $s="array(\n\t";\r
- $cw=$fm['Widths'];\r
- for($i=0;$i<=255;$i++)\r
- {\r
- if(chr($i)=="'")\r
- $s.="'\\''";\r
- elseif(chr($i)=="\\")\r
- $s.="'\\\\'";\r
- elseif($i>=32 and $i<=126)\r
- $s.="'".chr($i)."'";\r
- else\r
- $s.="chr($i)";\r
- $s.='=>'.$fm['Widths'][$i];\r
- if($i<255)\r
- $s.=',';\r
- if(($i+1)%22==0)\r
- $s.="\n\t";\r
- }\r
- $s.=')';\r
- return $s;\r
-}\r
-\r
-function MakeFontEncoding($map)\r
-{\r
- //Build differences from reference encoding\r
- $ref=ReadMap('cp1252');\r
- $s='';\r
- $last=0;\r
- for($i=32;$i<=255;$i++)\r
- {\r
- if($map[$i]!=$ref[$i])\r
- {\r
- if($i!=$last+1)\r
- $s.=$i.' ';\r
- $last=$i;\r
- $s.='/'.$map[$i].' ';\r
- }\r
- }\r
- return rtrim($s);\r
-}\r
-\r
-function SaveToFile($file,$s,$mode='t')\r
-{\r
- $f=fopen($file,'w'.$mode);\r
- if(!$f)\r
- die('Can\'t write to file '.$file);\r
- fwrite($f,$s,strlen($s));\r
- fclose($f);\r
-}\r
-\r
-function ReadShort($f)\r
-{\r
- $a=unpack('n1n',fread($f,2));\r
- return $a['n'];\r
-}\r
-\r
-function ReadLong($f)\r
-{\r
- $a=unpack('N1N',fread($f,4));\r
- return $a['N'];\r
-}\r
-\r
-function CheckTTF($file)\r
-{\r
- //Check if font license allows embedding\r
- $f=fopen($file,'rb');\r
- if(!$f)\r
- die('<B>Error:</B> Can\'t open '.$file);\r
- //Extract number of tables\r
- fseek($f,4,SEEK_CUR);\r
- $nb=ReadShort($f);\r
- fseek($f,6,SEEK_CUR);\r
- //Seek OS/2 table\r
- $found=false;\r
- for($i=0;$i<$nb;$i++)\r
- {\r
- if(fread($f,4)=='OS/2')\r
- {\r
- $found=true;\r
- break;\r
- }\r
- fseek($f,12,SEEK_CUR);\r
- }\r
- if(!$found)\r
- {\r
- fclose($f);\r
- return;\r
- }\r
- fseek($f,4,SEEK_CUR);\r
- $offset=ReadLong($f);\r
- fseek($f,$offset,SEEK_SET);\r
- //Extract fsType flags\r
- fseek($f,8,SEEK_CUR);\r
- $fsType=ReadShort($f);\r
- $rl=($fsType & 0x02)!=0;\r
- $pp=($fsType & 0x04)!=0;\r
- $e=($fsType & 0x08)!=0;\r
- fclose($f);\r
- if($rl and !$pp and !$e)\r
- echo '<B>Warning:</B> font license does not allow embedding';\r
-}\r
-\r
-/*******************************************************************************\r
-* $fontfile : chemin du fichier TTF (ou chaîne vide si pas d'incorporation) *\r
-* $afmfile : chemin du fichier AFM *\r
-* $enc : encodage (ou chaîne vide si la police est symbolique) *\r
-* $patch : patch optionnel pour l'encodage *\r
-* $type : type de la police si $fontfile est vide *\r
-*******************************************************************************/\r
-function MakeFont($fontfile,$afmfile,$enc='cp1252',$patch=array(),$type='TrueType')\r
-{\r
- //Generate a font definition file\r
- set_magic_quotes_runtime(0);\r
- ini_set('auto_detect_line_endings','1');\r
- if($enc)\r
- {\r
- $map=ReadMap($enc);\r
- foreach($patch as $cc=>$gn)\r
- $map[$cc]=$gn;\r
- }\r
- else\r
- $map=array();\r
- if(!file_exists($afmfile))\r
- die('<B>Error:</B> AFM file not found: '.$afmfile);\r
- $fm=ReadAFM($afmfile,$map);\r
- if($enc)\r
- $diff=MakeFontEncoding($map);\r
- else\r
- $diff='';\r
- $fd=MakeFontDescriptor($fm,empty($map));\r
- //Find font type\r
- if($fontfile)\r
- {\r
- $ext=strtolower(substr($fontfile,-3));\r
- if($ext=='ttf')\r
- $type='TrueType';\r
- elseif($ext=='pfb')\r
- $type='Type1';\r
- else\r
- die('<B>Error:</B> unrecognized font file extension: '.$ext);\r
- }\r
- else\r
- {\r
- if($type!='TrueType' and $type!='Type1')\r
- die('<B>Error:</B> incorrect font type: '.$type);\r
- }\r
- //Start generation\r
- $s='<?php'."\n";\r
- $s.='$type=\''.$type."';\n";\r
- $s.='$name=\''.$fm['FontName']."';\n";\r
- $s.='$desc='.$fd.";\n";\r
- if(!isset($fm['UnderlinePosition']))\r
- $fm['UnderlinePosition']=-100;\r
- if(!isset($fm['UnderlineThickness']))\r
- $fm['UnderlineThickness']=50;\r
- $s.='$up='.$fm['UnderlinePosition'].";\n";\r
- $s.='$ut='.$fm['UnderlineThickness'].";\n";\r
- $w=MakeWidthArray($fm);\r
- $s.='$cw='.$w.";\n";\r
- $s.='$enc=\''.$enc."';\n";\r
- $s.='$diff=\''.$diff."';\n";\r
- $basename=substr(basename($afmfile),0,-4);\r
- if($fontfile)\r
- {\r
- //Embedded font\r
- if(!file_exists($fontfile))\r
- die('<B>Error:</B> font file not found: '.$fontfile);\r
- if($type=='TrueType')\r
- CheckTTF($fontfile);\r
- $f=fopen($fontfile,'rb');\r
- if(!$f)\r
- die('<B>Error:</B> Can\'t open '.$fontfile);\r
- $file=fread($f,filesize($fontfile));\r
- fclose($f);\r
- if($type=='Type1')\r
- {\r
- //Find first two sections and discard third one\r
- $header=(ord($file{0})==128);\r
- if($header)\r
- {\r
- //Strip first binary header\r
- $file=substr($file,6);\r
- }\r
- $pos=strpos($file,'eexec');\r
- if(!$pos)\r
- die('<B>Error:</B> font file does not seem to be valid Type1');\r
- $size1=$pos+6;\r
- if($header and ord($file{$size1})==128)\r
- {\r
- //Strip second binary header\r
- $file=substr($file,0,$size1).substr($file,$size1+6);\r
- }\r
- $pos=strpos($file,'00000000');\r
- if(!$pos)\r
- die('<B>Error:</B> font file does not seem to be valid Type1');\r
- $size2=$pos-$size1;\r
- $file=substr($file,0,$size1+$size2);\r
- }\r
- if(function_exists('gzcompress'))\r
- {\r
- $cmp=$basename.'.z';\r
- SaveToFile($cmp,gzcompress($file),'b');\r
- $s.='$file=\''.$cmp."';\n";\r
- echo 'Font file compressed ('.$cmp.')<BR>';\r
- }\r
- else\r
- {\r
- $s.='$file=\''.basename($fontfile)."';\n";\r
- echo '<B>Notice:</B> font file could not be compressed (zlib extension not available)<BR>';\r
- }\r
- if($type=='Type1')\r
- {\r
- $s.='$size1='.$size1.";\n";\r
- $s.='$size2='.$size2.";\n";\r
- }\r
- else\r
- $s.='$originalsize='.filesize($fontfile).";\n";\r
- }\r
- else\r
- {\r
- //Not embedded font\r
- $s.='$file='."'';\n";\r
- }\r
- $s.="?>\n";\r
- SaveToFile($basename.'.php',$s);\r
- echo 'Font definition file generated ('.$basename.'.php'.')<BR>';\r
-}\r
-?>\r