ezPdf - simplify calling default fonts

Author Message

Trent Reimer

Thursday 30 March 2006 2:10:22 pm

I found that the default fonts could be called from any directory on the server as simply as:

$pdf->selectFont('Helvetica.afm');

...by editing a few lines in "class.pdf.php". Notice that there is no need for a directory path before the font name.

Why? Many developers like to put libraries and components like ezPdf in PHP's "include_path" so they can be called from any directory or project without having to map directories exactly. This is also useful when transferring work from a test server to a live server. This was not an option with ezPdf since methods like "selectFont" require an absolute or relative path from the calling script.

It appears getting around it just requires using specifications like

dirname(__FILE__) . '/fonts/'

instead of

'./fonts/'

the latter of which would only work if the calling script was in the same directory as "class.ezpdf.php". Using the former also ensures the default font is loadable (from any script in any directory) if the user fails to assign one explicity.

Specifically I added the following to the beginning of "openFont()":

$pos=strrpos($font,'/');
  if ($pos===false){
  	if (file_exists('./'.$font)) {
    	$dir = './';
    	$name = $font;
    } else {
    	$dir = dirname(__FILE__).'/fonts/';
    	$name = $font;
    }
  } else {
  	$dir=substr($font,0,$pos+1);
    $name=substr($font,$pos+1);
  }

...and edited the following in "selectFont()":

$name = ($pos=strrpos($fontName,'/')) !== FALSE ? substr($fontName,$pos+1) : $fontName;
if (substr($name,-4)=='.afm'){
    $name=substr($name,0,strlen($name)-4);
}

...and the following in "setCurrentFont()":

if (strlen($this->currentBaseFont)==0){
    // then assume an initial font
    $this->selectFont(dirname(__FILE__).'/fonts/Helvetica.afm');
  }
  $cf = ($pos=strrpos($this->currentBaseFont,'/')) !== FALSE ? substr($this->currentBaseFont, $pos+1) : $this->currentBaseFont;
  if (strlen($this->currentTextState)
    && isset($this->fontFamilies[$cf])
      && isset($this->fontFamilies[$cf][$this->currentTextState])){
    // then we are in some state or another
    // and this font has a family, and the current setting exists within it
    // select the font, then return it
    $nf = ($pos=strrpos($this->currentBaseFont, '/')) !== FALSE ? substr($this->currentBaseFont,0,$pos+1).$this->fontFamilies[$cf][$this->currentTextState] : $this->fontFamilies[$cf][$this->currentTextState];

I hope that is helpful. Let me know if I have missed something there.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.