Logo Search packages:      
Sourcecode: acidbase version File versions  Download package

Cpdf::selectFont ( fontName,
encoding = '',
set = 1 
)

if the font is not loaded then load it and make the required object else just make it the current font the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding' note that encoding='none' will need to be used for symbolic fonts and 'differences' => an array of mappings between numbers 0->255 and character names.

Definition at line 1417 of file class.pdf.php.

References $options, addMessage(), o_contents(), o_font(), o_fontDescriptor(), openFont(), and setCurrentFont().

Referenced by addText(), addTextWrap(), getFontDecender(), getFontHeight(), getTextWidth(), and setCurrentFont().

                                                  {
  if (!isset($this->fonts[$fontName])){
    // load the file
    $this->openFont($fontName);
    if (isset($this->fonts[$fontName])){
      $this->numObj++;
      $this->numFonts++;
      $pos=strrpos($fontName,'/');
//      $dir=substr($fontName,0,$pos+1);
      $name=substr($fontName,$pos+1);
      if (substr($name,-4)=='.afm'){
        $name=substr($name,0,strlen($name)-4);
      }
      $options=array('name'=>$name);
      if (is_array($encoding)){
        // then encoding and differences might be set
        if (isset($encoding['encoding'])){
          $options['encoding']=$encoding['encoding'];
        }
        if (isset($encoding['differences'])){
          $options['differences']=$encoding['differences'];
        }
      } else if (strlen($encoding)){
        // then perhaps only the encoding has been set
        $options['encoding']=$encoding;
      }
      $fontObj = $this->numObj;
      $this->o_font($this->numObj,'new',$options);
      $this->fonts[$fontName]['fontNum']=$this->numFonts;
      // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
      // should be for all non-basic fonts), then load it into an object and put the
      // references into the font object
      $basefile = substr($fontName,0,strlen($fontName)-4);
      if (file_exists($basefile.'.pfb')){
        $fbtype = 'pfb';
      } else if (file_exists($basefile.'.ttf')){
        $fbtype = 'ttf';
      } else {
        $fbtype='';
      }
      $fbfile = $basefile.'.'.$fbtype;
      
//      $pfbfile = substr($fontName,0,strlen($fontName)-4).'.pfb';
//      $ttffile = substr($fontName,0,strlen($fontName)-4).'.ttf';
      $this->addMessage('selectFont: checking for - '.$fbfile);
      if (substr($fontName,-4)=='.afm' && strlen($fbtype) ){
        $adobeFontName = $this->fonts[$fontName]['FontName'];
//        $fontObj = $this->numObj;
        $this->addMessage('selectFont: adding font file - '.$fbfile.' - '.$adobeFontName);
        // find the array of fond widths, and put that into an object.
        $firstChar = -1;
        $lastChar = 0;
        $widths = array();
        foreach ($this->fonts[$fontName]['C'] as $num=>$d){
          if (intval($num)>0 || $num=='0'){
            if ($lastChar>0 && $num>$lastChar+1){
              for($i=$lastChar+1;$i<$num;$i++){
                $widths[] = 0;
              }
            }
            $widths[] = $d['WX'];
            if ($firstChar==-1){
              $firstChar = $num;
            }
            $lastChar = $num;
          }
        }
        // also need to adjust the widths for the differences array
        if (isset($options['differences'])){
          foreach($options['differences'] as $charNum=>$charName){
            if ($charNum>$lastChar){
              for($i=$lastChar+1;$i<=$charNum;$i++){
                $widths[]=0;
              }
              $lastChar=$charNum;
            }
            if (isset($this->fonts[$fontName]['C'][$charName])){
              $widths[$charNum-$firstChar]=$this->fonts[$fontName]['C'][$charName]['WX'];
            }
          }
        }
        $this->addMessage('selectFont: FirstChar='.$firstChar);
        $this->addMessage('selectFont: LastChar='.$lastChar);
        $this->numObj++;
        $this->o_contents($this->numObj,'new','raw');
        $this->objects[$this->numObj]['c'].='[';
        foreach($widths as $width){
          $this->objects[$this->numObj]['c'].=' '.$width;
        }
        $this->objects[$this->numObj]['c'].=' ]';
        $widthid = $this->numObj;

        // load the pfb file, and put that into an object too.
        // note that pdf supports only binary format type 1 font files, though there is a 
        // simple utility to convert them from pfa to pfb.
        $fp = fopen($fbfile,'rb');
        $tmp = get_magic_quotes_runtime();
        set_magic_quotes_runtime(0);
        $data = fread($fp,filesize($fbfile));
        set_magic_quotes_runtime($tmp);
        fclose($fp);

        // create the font descriptor
        $this->numObj++;
        $fontDescriptorId = $this->numObj;
        $this->numObj++;
        $pfbid = $this->numObj;
        // determine flags (more than a little flakey, hopefully will not matter much)
        $flags=0;
        if ($this->fonts[$fontName]['ItalicAngle']!=0){ $flags+=pow(2,6); }
        if ($this->fonts[$fontName]['IsFixedPitch']=='true'){ $flags+=1; }
        $flags+=pow(2,5); // assume non-sybolic

        $list = array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
        $fdopt = array(
         'Flags'=>$flags
         ,'FontName'=>$adobeFontName
         ,'StemV'=>100  // don't know what the value for this should be!
        );
        foreach($list as $k=>$v){
          if (isset($this->fonts[$fontName][$v])){
            $fdopt[$k]=$this->fonts[$fontName][$v];
          }
        }

        if ($fbtype=='pfb'){
          $fdopt['FontFile']=$pfbid;
        } else if ($fbtype=='ttf'){
          $fdopt['FontFile2']=$pfbid;
        }
        $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);        

        // embed the font program
        $this->o_contents($this->numObj,'new');
        $this->objects[$pfbid]['c'].=$data;
        // determine the cruicial lengths within this file
        if ($fbtype=='pfb'){
          $l1 = strpos($data,'eexec')+6;
          $l2 = strpos($data,'00000000')-$l1;
          $l3 = strlen($data)-$l2-$l1;
          $this->o_contents($this->numObj,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
        } else if ($fbtype=='ttf'){
          $l1 = strlen($data);
          $this->o_contents($this->numObj,'add',array('Length1'=>$l1));
        }


        // tell the font object about all this new stuff
        $tmp = array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
                                      ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
                                      ,'FontDescriptor'=>$fontDescriptorId);
        if ($fbtype=='ttf'){
          $tmp['SubType']='TrueType';
        }
        $this->addMessage('adding extra info to font.('.$fontObj.')');
        foreach($tmp as $fk=>$fv){
          $this->addMessage($fk." : ".$fv);
        }
        $this->o_font($fontObj,'add',$tmp);

      } else {
        $this->addMessage('selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts');
      }


      // also set the differences here, note that this means that these will take effect only the 
      //first time that a font is selected, else they are ignored
      if (isset($options['differences'])){
        $this->fonts[$fontName]['differences']=$options['differences'];
      }
    }
  }
  if ($set && isset($this->fonts[$fontName])){
    // so if for some reason the font was not set in the last one then it will not be selected
    $this->currentBaseFont=$fontName;
    // the next line means that if a new font is selected, then the current text state will be
    // applied to it as well.
    $this->setCurrentFont();
  }
  return $this->currentFontNum;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index