eZ Publish  [4.0]
class.pdf.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <26-Aug-2003 15:15:32 kk>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.0.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 /*! \file class.pdf.php
00030 */
00031 
00032 /*!
00033   \class Cpdf class.pdf.php
00034   \ingroup eZPDF
00035   \brief Cpdf provides
00036 */
00037 
00038 //include_once( 'lib/ezutils/classes/ezmath.php' );
00039 
00040 class Cpdf
00041 {
00042 
00043     /**
00044      * the current number of pdf objects in the document
00045      */
00046     public $numObj = 0;
00047 
00048     /**
00049      * this array contains all of the pdf objects, ready for final assembly
00050      */
00051     public $objects = array();
00052 
00053     /**
00054      * the objectId (number within the objects array) of the document catalog
00055      */
00056     public $catalogId;
00057 
00058     /**
00059      * array carrying information about the fonts that the system currently knows about
00060      * used to ensure that a font is not loaded twice, among other things
00061      */
00062     public $fonts = array();
00063 
00064     /**
00065      * a record of the current font
00066      */
00067     public $currentFont = '';
00068 
00069     /**
00070      * the current base font
00071      */
00072     public $currentBaseFont = '';
00073 
00074     /**
00075      * the number of the current font within the font array
00076      */
00077     public $currentFontNum = 0;
00078 
00079     /**
00080      *
00081      */
00082     public $currentNode;
00083 
00084     /**
00085      * object number of the current page
00086      */
00087     public $currentPage;
00088 
00089     /**
00090      * object number of the currently active contents block
00091      */
00092     public $currentContents;
00093 
00094     /**
00095      * number of fonts within the system
00096      */
00097     public $numFonts = 0;
00098 
00099     /**
00100      * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
00101      */
00102     public $currentColour;
00103 
00104     /**
00105      * current colour for stroke operations (lines etc.)
00106      */
00107     public $currentStrokeColour;
00108 
00109     /**
00110      * current style that lines are drawn in
00111      */
00112     public $currentLineStyle = '';
00113 
00114     /**
00115      * an array which is used to save the state of the document, mainly the colours and styles
00116      * it is used to temporarily change to another state, the change back to what it was before
00117      */
00118     public $stateStack = array();
00119 
00120     /**
00121      * number of elements within the state stack
00122      */
00123     public $nStateStack = 0;
00124 
00125     /**
00126      * number of page objects within the document
00127      */
00128     public $numPages = 0;
00129 
00130     /**
00131      * object Id storage stack
00132      */
00133     public $stack = array();
00134 
00135     /**
00136      * number of elements within the object Id storage stack
00137      */
00138     public $nStack = 0;
00139 
00140     /**
00141      * an array which contains information about the objects which are not firmly attached to pages
00142      * these have been added with the addObject function
00143      */
00144     public $looseObjects = array();
00145 
00146     /**
00147      * array contains infomation about how the loose objects are to be added to the document
00148      */
00149     public $addLooseObjects = array();
00150 
00151     /**
00152      * the objectId of the information object for the document
00153      * this contains authorship, title etc.
00154      */
00155     public $infoObject = 0;
00156 
00157     /**
00158      * number of images being tracked within the document
00159      */
00160     public $numImages = 0;
00161 
00162     /**
00163      * an array containing options about the document
00164      * it defaults to turning on the compression of the objects
00165      */
00166     public $options = array( 'compression' => 1 );
00167 
00168     /**
00169      * the objectId of the first page of the document
00170      */
00171     public $firstPageId;
00172 
00173     /**
00174      * used to track the last used value of the inter-word spacing, this is so that it is known
00175      * when the spacing is changed.
00176      */
00177     public $wordSpaceAdjust = 0;
00178 
00179     /**
00180      * the object Id of the procset object
00181      */
00182     public $procsetObjectId;
00183 
00184     /**
00185      * store the information about the relationship between font families
00186      * this used so that the code knows which font is the bold version of another font, etc.
00187      * the value of this array is initialised in the constuctor function.
00188      */
00189     public $fontFamilies = array();
00190 
00191     /**
00192      * track if the current font is bolded or italicised
00193      */
00194     public $currentTextState = '';
00195 
00196     /**
00197      * messages are stored here during processing, these can be selected afterwards to give some useful debug information
00198      */
00199     public $messages = '';
00200 
00201     /**
00202      * the ancryption array for the document encryption is stored here
00203      */
00204     public $arc4 = '';
00205 
00206     /**
00207      * the object Id of the encryption information
00208      */
00209     public $arc4_objnum = 0;
00210 
00211     /**
00212      * the file identifier, used to uniquely identify a pdf document
00213      */
00214     public $fileIdentifier = '';
00215 
00216     /**
00217      * a flag to say if a document is to be encrypted or not
00218      */
00219     public $encrypted = 0;
00220 
00221     /**
00222      * the ancryption key for the encryption of all the document content (structure is not encrypted)
00223      */
00224     public $encryptionKey = '';
00225 
00226     /**
00227      * array which forms a stack to keep track of nested callback functions
00228      */
00229     public $callback = array();
00230 
00231     /**
00232      * the number of callback functions in the callback array
00233      */
00234     public $nCallback = 0;
00235 
00236     /**
00237      * store label->id pairs for named destinations, these will be used to replace internal links
00238      * done this way so that destinations can be defined after the location that links to them
00239      */
00240     public $destinations = array();
00241 
00242     /**
00243      * store the stack for the transaction commands, each item in here is a record of the values of all the
00244      * variables within the class, so that the user can rollback at will (from each 'start' command)
00245      * note that this includes the objects array, so these can be large.
00246      */
00247     public $checkpoint = '';
00248 
00249     /**
00250      * class constructor
00251      * this will start a new document
00252      * @var array array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
00253      */
00254     function Cpdf ( $pageSize = array( 0, 0, 612, 792 ) )
00255     {
00256         $this->currentColour = eZMath::rgbToCMYK( array( 'r' => -1,
00257                                                          'g' => -1,
00258                                                          'b' => -1 ) );
00259         $this->currentStrokeColour = eZMath::rgbToCMYK( array( 'r' => -1,
00260                                                                'g' => -1,
00261                                                                'b' => -1 ) );
00262 
00263         $this->newDocument( $pageSize );
00264 
00265         // also initialize the font families that are known about already
00266         $this->setFontFamily( 'init' );
00267         //  $this->fileIdentifier = md5('xxxxxxxx'.time());
00268 
00269     }
00270 
00271     /**
00272      * Document object methods (internal use only)
00273      *
00274      * There is about one object method for each type of object in the pdf document
00275      * Each function has the same call list ($id,$action,$options).
00276      * $id = the object ID of the object, or what it is to be if it is being created
00277      * $action = a string specifying the action to be performed, though ALL must support:
00278      *           'new' - create the object with the id $id
00279      *           'out' - produce the output for the pdf object
00280      * $options = optional, a string or array containing the various parameters for the object
00281      *
00282      * These, in conjunction with the output function are the ONLY way for output to be produced
00283      * within the pdf 'file'.
00284      */
00285 
00286     /**
00287      *destination object, used to specify the location for the user to jump to, presently on opening
00288      */
00289     function o_destination( $id, $action, $options = '' )
00290     {
00291         if ( $action != 'new' )
00292         {
00293             $o =& $this->objects[$id];
00294         }
00295         switch ( $action )
00296         {
00297             case 'new':
00298                 $this->objects[$id] = array( 't' => 'destination', 'info' => array() );
00299                 $tmp = '';
00300                 switch ( $options['type'] )
00301                 {
00302                 case 'XYZ':
00303                 case 'FitR':
00304                     $tmp =  ' ' . sprintf( '%.3F', $options['p3'] ) . $tmp;
00305                 case 'FitV':
00306                 case 'FitBH':
00307                 case 'FitBV':
00308                     $tmp =  ' ' . sprintf( '%.3F', $options['p2'] ) . $tmp;
00309                 case 'Fit':
00310                 case 'FitH':
00311                     $tmp =  ' ' . sprintf( '%.3F', $options['p1'] ) . $tmp;
00312                 case 'FitB':
00313                     $tmp = $options['type'] . $tmp;
00314                     $this->objects[$id]['info']['string'] = $tmp;
00315                     $this->objects[$id]['info']['page'] = $options['page'];
00316                 }break;
00317 
00318             case 'out':
00319                 $tmp = $o['info'];
00320                 $res = "\n" . $id . " 0 obj\n" . '[' . $tmp['page'] . ' 0 R /' . $tmp['string'] . "]\nendobj\n";
00321                 return $res;
00322                 break;
00323         }
00324     }
00325 
00326     /**
00327      * set the viewer preferences
00328      */
00329     function o_viewerPreferences( $id, $action, $options = '' )
00330     {
00331         if ( $action != 'new' )
00332         {
00333             $o =& $this->objects[$id];
00334         }
00335         switch ( $action )
00336         {
00337         case 'new':
00338             $this->objects[$id] = array( 't' => 'viewerPreferences', 'info' => array() );
00339             break;
00340         case 'add':
00341             foreach ( $options as $k => $v )
00342             {
00343                 switch ( $k )
00344                 {
00345                 case 'HideToolbar':
00346                 case 'HideMenubar':
00347                 case 'HideWindowUI':
00348                 case 'FitWindow':
00349                 case 'CenterWindow':
00350                 case 'NonFullScreenPageMode':
00351                 case 'Direction':
00352                     $o['info'][$k] = $v;
00353                     break;
00354                 }
00355             }break;
00356         case 'out':
00357             $res = "\n" . $id . " 0 obj\n" . '<< ';
00358             foreach($o['info'] as $k=>$v)
00359             {
00360                 $res .= "\n/" . $k . ' ' . $v;
00361             }
00362             $res .= "\n>>\n";
00363             return $res;
00364             break;
00365         }
00366     }
00367 
00368     /**
00369      * define the document catalog, the overall controller for the document
00370      */
00371     function o_catalog( $id, $action, $options = '' )
00372     {
00373         if ( $action != 'new' )
00374         {
00375             $o =& $this->objects[$id];
00376         }
00377         switch ( $action )
00378         {
00379         case 'new':
00380             $this->objects[$id] = array( 't' => 'catalog', 'info' => array() );
00381             $this->catalogId = $id;
00382             break;
00383         case 'outlines':
00384         case 'pages':
00385         case 'openHere':
00386             $o['info'][$action] = $options;
00387             break;
00388         case 'viewerPreferences':
00389             if ( !isset( $o['info']['viewerPreferences'] ) )
00390             {
00391                 $this->numObj++;
00392                 $this->o_viewerPreferences( $this->numObj, 'new' );
00393                 $o['info']['viewerPreferences'] = $this->numObj;
00394             }
00395             $vp = $o['info']['viewerPreferences'];
00396             $this->o_viewerPreferences( $vp, 'add', $options );
00397             break;
00398         case 'out':
00399             $res= "\n" . $id . " 0 obj\n" . '<< /Type /Catalog';
00400             foreach ( $o['info'] as $k => $v )
00401             {
00402                 switch ( $k )
00403                 {
00404                 case 'outlines':
00405                     $res .= "\n" . '/Outlines ' . $v . ' 0 R';
00406                     break;
00407                 case 'pages':
00408                     $res .= "\n" . '/Pages ' . $v . ' 0 R';
00409                     break;
00410                 case 'viewerPreferences':
00411                     $res .= "\n" . '/ViewerPreferences ' . $o['info']['viewerPreferences'] . ' 0 R';
00412                     break;
00413                 case 'openHere':
00414                     $res .= "\n" . '/OpenAction ' . $o['info']['openHere'] . ' 0 R';
00415                     break;
00416                 }
00417             }
00418             $res .= " >>\nendobj";
00419             return $res;
00420             break;
00421         }
00422     }
00423 
00424     /**
00425      * object which is a parent to the pages in the document
00426      */
00427     function o_pages( $id, $action, $options = '' )
00428     {
00429         if ($action!='new')
00430         {
00431             $o =& $this->objects[$id];
00432         }
00433         switch ( $action )
00434         {
00435         case 'new':
00436             $this->objects[$id] = array( 't' => 'pages', 'info' => array() );
00437             $this->o_catalog( $this->catalogId, 'pages', $id );
00438             break;
00439         case 'page':
00440             if ( !is_array( $options ) )
00441             {
00442                 // then it will just be the id of the new page
00443                 $o['info']['pages'][] = $options;
00444             }
00445             else
00446             {
00447                 // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
00448                 // and pos is either 'before' or 'after', saying where this page will fit.
00449                 if ( isset( $options['id'] ) && isset( $options['rid'] ) && isset( $options['pos'] ) )
00450                 {
00451                     $i = array_search( $options['rid'], $o['info']['pages'] );
00452                     if ( isset( $o['info']['pages'][$i] ) && $o['info']['pages'][$i] == $options['rid'])
00453                     {
00454                         // then there is a match
00455                         // make a space
00456                         switch ( $options['pos'] )
00457                         {
00458                         case 'before':
00459                             $k = $i;
00460                             break;
00461                         case 'after':
00462                             $k = $i + 1;
00463                             break;
00464                         default:
00465                             $k = -1;
00466                             break;
00467                         }
00468                         if ( $k >= 0 )
00469                         {
00470                             for ( $j = count( $o['info']['pages'] ) - 1; $j >= $k; $j-- )
00471                             {
00472                                 $o['info']['pages'][$j+1] = $o['info']['pages'][$j];
00473                             }
00474                             $o['info']['pages'][$k] = $options['id'];
00475                         }
00476                     }
00477                 }
00478             }break;
00479 
00480         case 'procset':
00481             $o['info']['procset'] = $options;
00482             break;
00483         case 'mediaBox':
00484             $o['info']['mediaBox'] = $options; // which should be an array of 4 numbers
00485             break;
00486         case 'font':
00487             $o['info']['fonts'][] = array( 'objNum' => $options['objNum'], 'fontNum' => $options['fontNum'] );
00488             break;
00489         case 'shading':
00490             $o['info']['shading'][] = array( 'objNum' => $options['objNum'], 'label' => $options['label'] );
00491             break;
00492         case 'xObject':
00493             $o['info']['xObjects'][] = array( 'objNum' => $options['objNum'], 'label' => $options['label'] );
00494             break;
00495         case 'out':
00496             if ( count( $o['info']['pages'] ) )
00497             {
00498                 $res = "\n" . $id . " 0 obj\n<< /Type /Pages\n/Kids [";
00499                 foreach ( $o['info']['pages'] as $k => $v )
00500                 {
00501                     $res .= $v . " 0 R\n";
00502                 }
00503                 $res .= "]\n/Count " . count( $this->objects[$id]['info']['pages'] );
00504                 if ( ( isset( $o['info']['fonts'] ) && count( $o['info']['fonts'] ) ) || isset( $o['info']['procset'] ) )
00505                 {
00506                     $res .= "\n/Resources <<";
00507                     if ( isset( $o['info']['procset'] ) )
00508                     {
00509                         $res .= "\n/ProcSet " . $o['info']['procset'] . " 0 R";
00510                     }
00511                     if ( isset( $o['info']['fonts'] ) && count( $o['info']['fonts'] ) )
00512                     {
00513                         $res .= "\n/Font << ";
00514                         foreach ( $o['info']['fonts'] as $finfo )
00515                         {
00516                             $res .= "\n/F" . $finfo['fontNum'] . " " . $finfo['objNum'] . " 0 R";
00517                         }
00518                         $res .= " >>";
00519                     }
00520 
00521                     if ( isset( $o['info']['shading'] ) && count( $o['info']['shading'] ) )
00522                     {
00523                         $res .= "\n/Shading <<";
00524                         foreach ( $o['info']['shading'] as $finfo )
00525                         {
00526                             $res .= "\n/" . $finfo['label'] . " " . $finfo['objNum'] . " 0 R";
00527                         }
00528                         $res .= " >>";
00529                     }
00530 
00531                     if ( isset( $o['info']['xObjects'] ) && count( $o['info']['xObjects'] ) )
00532                     {
00533                         $res .= "\n/XObject << ";
00534                         foreach ( $o['info']['xObjects'] as $finfo )
00535                         {
00536                             $res .= "\n/" . $finfo['label'] . " " . $finfo['objNum'] . " 0 R";
00537                         }
00538                         $res .= " >>";
00539                     }
00540                     $res .= "\n>>";
00541                     if ( isset( $o['info']['mediaBox'] ) )
00542                     {
00543                         $tmp = $o['info']['mediaBox'];
00544                         $res .= "\n/MediaBox [" . sprintf( '%.3F', $tmp[0] ) . ' ' . sprintf( '%.3F', $tmp[1] ) . ' ' . sprintf( '%.3F', $tmp[2] ) . ' ' . sprintf( '%.3F', $tmp[3] ) . ']';
00545                     }
00546                 }
00547                 $res.="\n >>\nendobj";
00548             }
00549             else
00550             {
00551                 $res = "\n" . $id . " 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
00552             }
00553             return $res;
00554             break;
00555         }
00556     }
00557 
00558     /**
00559      * define the outlines in the doc, empty for now
00560      */
00561     function o_outlines( $id, $action, $options = '' )
00562     {
00563         if ( $action != 'new' )
00564         {
00565             $o =& $this->objects[$id];
00566         }
00567         switch ( $action )
00568         {
00569         case 'new':
00570             $this->objects[$id] = array( 't' => 'outlines', 'info' => array( 'outlines' => array() ) );
00571             $this->o_catalog( $this->catalogId, 'outlines', $id );
00572             break;
00573         case 'outline':
00574             $o['info']['outlines'][] = $options;
00575             break;
00576         case 'out':
00577             if ( count( $o['info']['outlines'] ) )
00578             {
00579                 $res = "\n" . $id . " 0 obj\n<< /Type /Outlines /Kids [";
00580                 foreach ( $o['info']['outlines'] as $k => $v )
00581                 {
00582                     $res .= $v . " 0 R ";
00583                 }
00584                 $res .= "] /Count " . count( $o['info']['outlines'] ) . " >>\nendobj";
00585             }
00586             else
00587             {
00588                 $res = "\n" . $id . " 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
00589             }
00590             return $res;
00591             break;
00592         }
00593     }
00594 
00595     /**
00596       Add object to hold Function properties
00597       Only partial support for type 0, 3 and 4
00598 
00599      \param object id.
00600      \param action
00601      \param options
00602      */
00603     function o_function( $id, $action, $options = '' )
00604     {
00605         if ( $action != 'new' )
00606         {
00607             $o =& $this->objects[$id];
00608         }
00609 
00610         switch ( $action )
00611         {
00612             case 'new':
00613             {
00614                 $this->objects[$id] = array ( 't' => 'function',
00615                                               'info' => array() );
00616 
00617                 if ( isset( $options['type'] ) ) // Set function type
00618                 {
00619                     $this->objects[$id]['info']['type'] = $options['type'];
00620                 }
00621                 else
00622                 {
00623                     $this->objects[$id]['info']['type'] = '2';
00624                 }
00625 
00626                 if ( isset( $options['Domain'] ) ) // Set function Domain
00627                 {
00628                     $this->objects[$id]['info']['Domain'] = $options['Domain'];
00629                 }
00630                 else
00631                 {
00632                     $this->objects[$id]['info']['Domain'] = '[0.0 1.0]';
00633                 }
00634 
00635                 if ( $this->objects[$id]['info']['type'] == '2' ) // set start and en values
00636                 {
00637                     if ( isset( $options['C0'] ) ) // Set values0
00638                     {
00639                         $this->objects[$id]['info']['C0'] = $options['C0'];
00640                     }
00641 
00642                     if ( isset( $options['C1'] ) ) // Set values1
00643                     {
00644                         $this->objects[$id]['info']['C1'] = $options['C1'];
00645                     }
00646 
00647                     if ( isset( $options['N'] ) ) // set exponent
00648                     {
00649                         $this->objects[$id]['info']['N'] = $options['N'];
00650                     }
00651                     else
00652                     {
00653                         $this->objects[$id]['info']['N'] = '1.0';
00654                     }
00655                 }
00656 
00657             } break;
00658 
00659             case 'out':
00660             {
00661                 $res = "\n" . $id . " 0 obj\n<< ";
00662                 $res.= "/FunctionType " . $o['info']['type'] . "\n";
00663                 $res.= "/Domain " . $o['info']['Domain'] . "\n";
00664                 if ( isset ( $o['info']['C0'] ) )
00665                     $res.= "/C0 " . $o['info']['C0'] . "\n";
00666                 if ( isset ( $o['info']['C1'] ) )
00667                     $res.= "/C1 " . $o['info']['C1'] . "\n";
00668                 $res.= "/N " . $o['info']['N'] . "\n";
00669                 $res.= ">>\nendobject";
00670                 return $res;
00671             } break;
00672         }
00673     }
00674 
00675     /**
00676       Add object to hold pattern properties
00677 
00678      \param object id.
00679      \param action
00680      \param options
00681 
00682      \return action 'new' - dictionary name
00683             'out' - pdf output
00684      */
00685     function o_pattern( $id, $action, $options = '' )
00686     {
00687         if ( $action != 'new' )
00688         {
00689             $o =& $this->objects[$id];
00690         }
00691 
00692         switch ( $action )
00693         {
00694             case 'new':
00695             {
00696                 $this->objects[$id] = array( 't' => 'shading',
00697                                              'info' => array() );
00698 
00699                 if ( isset( $options['type'] ) ) //Set pattern type
00700                 {
00701                     $this->objects[$id]['info']['PatternType'] = $options['type'];
00702                 }
00703                 else
00704                 {
00705                     $this->objects[$id]['info']['PatternType'] = '2';
00706                 }
00707 
00708                 if ( isset( $options['matrix'] ) )
00709                 {
00710                     $this->objects[$id]['info']['Matrix'] = '[ ';
00711                     foreach ( $options['matrix'] as $coord )
00712                     {
00713                         $this->objects[$id]['info']['Matrix'] .= sprintf( ' %.3F ', $coord );
00714                     }
00715                     $this->objects[$id]['info']['Matrix'] .= ' ]';
00716                 }
00717 
00718                 if ( $this->objects[$id]['info']['PatternType'] == 1 ) // Pattern type 1, tiling pattern
00719                 {
00720                     $this->objects[$id]['info']['PaintType'] = $options['paintType'];
00721                     $this->objects[$id]['info']['TilingType'] = $options['tilingType'];
00722 
00723                     $this->objects[$id]['info']['BBox'] = '[ ';
00724                     foreach ( $options['bbox'] as $coord )
00725                     {
00726                         $this->objects[$id]['info']['BBox'] .= sprintf( ' %.3F ', $coord );
00727                     }
00728                     $this->objects[$id]['info']['BBox'] .= ']';
00729 
00730                     $this->objects[$id]['info']['XStep'] = $options['xstep'];
00731                     $this->objects[$id]['info']['YStep'] = $options['ystep'];
00732                 }
00733                 else // Non tiling, smooth pattern.
00734                 {
00735                     if ( isset( $options['shading'] ) )
00736                     {
00737                         $this->objects[$id]['info']['Shading'] = $options['shading'] . ' R';
00738                     }
00739                 }
00740             } break;
00741 
00742             case 'out':
00743             {
00744                 $res = "\n" . $id . " 0 obj\n<< ";
00745                 foreach ( $this->objects[$id]['info'] as $key => $info )
00746                 {
00747                     $res .= '/' . $key . ' ' . $info[$key] . "\n";
00748                 }
00749                 $res .= ">>\nendobject";
00750                 return $res;
00751             } break;
00752         }
00753     } //TODO: Add pattern too
00754 
00755 
00756     /**
00757       Add object to hold shading properties
00758 
00759      \param object id.
00760      \param action
00761      \param options
00762 
00763      \return action 'new' - dictionary name
00764             'out' - pdf output
00765      */
00766     function o_shading( $id, $action, $options = '' )
00767     {
00768         if ( $action != 'new' )
00769         {
00770             $o =& $this->objects[$id];
00771         }
00772 
00773         switch ( $action )
00774         {
00775             case 'new':
00776             {
00777                 $this->objects[$id] = array( 't' => 'shading',
00778                                              'info' => array( 'type' => '2',
00779                                                               'ColorSpace' => '/DeviceCMYK' ) );
00780 
00781             if ( !isset( $options['orientation'] ) )
00782             {
00783                 $options['orientation'] = 'vertical';
00784             }
00785 
00786             if ( $options['orientation'] == 'horizontal' )
00787             {
00788                 $angle = 0;
00789             }
00790             else if ( $options['orientation'] == 'vertical' )
00791             {
00792                 $angle = deg2rad( 90 );
00793             }
00794             else // orientation is an angle
00795             {
00796                 $angle = $options['orientation'];
00797             }
00798 
00799             $this->objects[$id]['info']['Coords'] = '[ '.  // compute angle of gradient chading
00800                  sprintf( '%.3F', $options['size']['x1'] ) . ' ' .
00801                  sprintf( '%.3F', $options['size']['y1'] + $options['size']['height'] ) . ' ' .
00802                  sprintf( '%.3F', $options['size']['x1'] + $options['size']['width'] * cos( $angle ) ) . ' ' .
00803                  sprintf( '%.3F', $options['size']['y1'] + $options['size']['height'] * ( 1 - sin( $angle ) ) ) . ']';
00804 
00805             //$this->objects[$id]['info']['Coords'] = '[0.0 0.0 50.0 50.0]';
00806 
00807             if ( is_array( $options['color0'] ) ) //Set chading colors
00808             {
00809                 if ( count( $options['color0'] ) == 3 )
00810                 {
00811                     $options['color0'] = eZMath::rgbToCMYK( $options['color0'] );
00812                 }
00813                 $color0 = array ( 'c' => sprintf( '%.3F', $options['color0']['c'] ),
00814                                   'm' => sprintf( '%.3F', $options['color0']['m'] ),
00815                                   'y' => sprintf( '%.3F', $options['color0']['y'] ),
00816                                   'k' => sprintf( '%.3F', $options['color0']['k'] ) );
00817             }
00818             else
00819             {
00820                 $color0 = eZMath::rgbToCMYK2( 0, 0, 0 );
00821             }
00822             if ( is_array( $options['color1'] ) )
00823             {
00824                 if ( count( $options['color1'] ) == 3 )
00825                 {
00826                     $options['color1'] = eZMath::rgbToCMYK( $options['color1'] );
00827                 }
00828                 $color1 = array ( 'c' => sprintf( '%.3F', $options['color1']['c'] ),
00829                                   'm' => sprintf( '%.3F', $options['color1']['m'] ),
00830                                   'y' => sprintf( '%.3F', $options['color1']['y'] ),
00831                                   'k' => sprintf( '%.3F', $options['color1']['k'] ) );
00832             }
00833             else
00834             {
00835                 $color1 = eZMath::rgbToCMYK2( 1, 1, 1 );
00836             }
00837 
00838             $this->numObj++;
00839             $this->objects[$id]['info']['Function'] = $this->numObj;
00840             $this->o_function( $this->numObj, 'new', array ( 'type' => '2',
00841                                                              'Domain' => '[0.0 1.0]',
00842                                                              'C0' => '['.$color0['c'].' '.$color0['m'].' '.$color0['y'].' '.$color0['k'].']',
00843                                                              'C1' => '['.$color1['c'].' '.$color1['m'].' '.$color1['y'].' '.$color1['k'].']',
00844                                                              'N' => '1.0' ) );
00845             $this->objects[$id]['info']['Extend'] = '[true true]';
00846             // also tell the pages node about the new shading
00847             $this->o_pages( $this->currentNode, 'shading', array( 'label' => 'Sh' . $id,
00848                                                                   'objNum'=> $id ) );
00849             return 'Sh' . $id;
00850             } break;
00851 
00852             case 'out':
00853             {
00854                 $res = "\n" . $id . " 0 obj\n<< ";
00855                 $res.= "/ShadingType " . $o['info']['type'] . "\n";
00856                 $res.= "/ColorSpace " . $o['info']['ColorSpace'] . "\n";
00857                 $res.= "/Coords " . $o['info']['Coords'] . "\n";
00858                 $res.= "/Function " . $o['info']['Function'] . " 0 R\n";
00859                 $res.= "/Extend " . $o['info']['Extend'] . "\n";
00860                 $res.= ">>\nendobject";
00861                 return $res;
00862             } break;
00863         }
00864     }
00865 
00866     /**
00867      * an object to hold the font description
00868      */
00869     function o_font( $id, $action, $options = '' )
00870     {
00871         if ( $action != 'new' )
00872         {
00873             $o =& $this->objects[$id];
00874         }
00875         switch ( $action )
00876         {
00877         case 'new':
00878             $this->objects[$id] = array( 't' => 'font',
00879                                          'info' => array( 'name' => $options['name'],
00880                                                           'SubType' => 'Type1' ) );
00881             $fontNum = $this->numFonts;
00882             $this->objects[$id]['info']['fontNum'] = $fontNum;
00883             // deal with the encoding and the differences
00884             if ( isset( $options['differences'] ) )
00885             {
00886                 // then we'll need an encoding dictionary
00887                 $this->numObj++;
00888                 $this->o_fontEncoding( $this->numObj, 'new', $options );
00889                 $this->objects[$id]['info']['encodingDictionary'] = $this->numObj;
00890             }
00891             else if ( isset( $options['encoding'] ) )
00892             {
00893                 // we can specify encoding here
00894                 switch ( $options['encoding'] )
00895                 {
00896                 case 'WinAnsiEncoding':
00897                 case 'MacRomanEncoding':
00898                 case 'MacExpertEncoding':
00899                     $this->objects[$id]['info']['encoding'] = $options['encoding'];
00900                     break;
00901                 case 'none':
00902                     break;
00903                 default:
00904                     $this->objects[$id]['info']['encoding'] = 'WinAnsiEncoding';
00905                     break;
00906                 }
00907             }
00908             else
00909             {
00910                 $this->objects[$id]['info']['encoding'] = 'WinAnsiEncoding';
00911             }
00912             // also tell the pages node about the new font
00913             $this->o_pages( $this->currentNode, 'font', array( 'fontNum' => $fontNum, 'objNum' => $id ) );
00914             break;
00915 
00916         case 'add':
00917             foreach ( $options as $k => $v )
00918             {
00919                 switch ( $k )
00920                 {
00921                 case 'BaseFont':
00922                     $o['info']['name'] = $v;
00923                     break;
00924                 case 'FirstChar':
00925                 case 'LastChar':
00926                 case 'Widths':
00927                 case 'FontDescriptor':
00928                 case 'SubType':
00929                     $this->addMessage( 'o_font ' . $k . " : " . $v );
00930                     $o['info'][$k] = $v;
00931                     break;
00932                 }
00933             }
00934             break;
00935 
00936         case 'out':
00937             $res = "\n" . $id . " 0 obj\n<< /Type /Font\n/Subtype /" . $o['info']['SubType'] . "\n";
00938             $res.= "/Name /F" . $o['info']['fontNum'] . "\n";
00939             $res.= "/BaseFont /" . $o['info']['name'] . "\n";
00940             if ( isset( $o['info']['encodingDictionary'] ) )
00941             {
00942                 // then place a reference to the dictionary
00943                 $res.= "/Encoding " . $o['info']['encodingDictionary'] . " 0 R\n";
00944             }
00945             else if ( isset( $o['info']['encoding'] ) )
00946             {
00947                 // use the specified encoding
00948                 $res.= "/Encoding /" . $o['info']['encoding'] . "\n";
00949             }
00950             if ( isset($o['info']['FirstChar'] ) )
00951             {
00952                 $res.= "/FirstChar " . $o['info']['FirstChar'] . "\n";
00953             }
00954             if ( isset( $o['info']['LastChar'] ) )
00955             {
00956                 $res.= "/LastChar " . $o['info']['LastChar'] . "\n";
00957             }
00958             if ( isset( $o['info']['Widths'] ) )
00959             {
00960                 $res.= "/Widths " . $o['info']['Widths'] . " 0 R\n";
00961             }
00962             if ( isset( $o['info']['FontDescriptor'] ) )
00963             {
00964                 $res.= "/FontDescriptor " . $o['info']['FontDescriptor'] . " 0 R\n";
00965             }
00966             $res.= ">>\nendobj";
00967             return $res;
00968             break;
00969         }
00970     }
00971 
00972 /**
00973  * a font descriptor, needed for including additional fonts
00974  */
00975     function o_fontDescriptor( $id, $action, $options = '')
00976     {
00977         if ( $action != 'new' )
00978         {
00979             $o =& $this->objects[$id];
00980         }
00981         switch ( $action )
00982         {
00983         case 'new':
00984             $this->objects[$id] = array( 't' => 'fontDescriptor', 'info' => $options );
00985             break;
00986         case 'out':
00987             $res = "\n" . $id . " 0 obj\n<< /Type /FontDescriptor\n";
00988             foreach ( $o['info'] as $label => $value )
00989             {
00990                 switch ( $label )
00991                 {
00992                 case 'Ascent':
00993                 case 'CapHeight':
00994                 case 'Descent':
00995                 case 'Flags':
00996                 case 'ItalicAngle':
00997                 case 'StemV':
00998                 case 'AvgWidth':
00999                 case 'Leading':
01000                 case 'MaxWidth':
01001                 case 'MissingWidth':
01002                 case 'StemH':
01003                 case 'XHeight':
01004                 case 'CharSet':
01005                     if ( strlen( $value ) )
01006                     {
01007                         $res.= '/'.$label.' '.$value."\n";
01008                     }
01009                     break;
01010                 case 'FontFile':
01011                 case 'FontFile2':
01012                 case 'FontFile3':
01013                     $res.= '/'.$label.' '.$value." 0 R\n";
01014                     break;
01015                 case 'FontBBox':
01016                     $res.= '/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."]\n";
01017                     break;
01018                 case 'FontName':
01019                     $res.= '/'.$label.' /'.$value."\n";
01020                     break;
01021                 }
01022             }
01023             $res .= ">>\nendobj";
01024             return $res;
01025             break;
01026         }
01027     }
01028 
01029     /**
01030      * the font encoding
01031      */
01032     function o_fontEncoding( $id, $action, $options = '' )
01033     {
01034         if ( $action != 'new' )
01035         {
01036             $o =& $this->objects[$id];
01037         }
01038         switch ( $action )
01039         {
01040         case 'new':
01041             // the options array should contain 'differences' and maybe 'encoding'
01042             $this->objects[$id] = array( 't' => 'fontEncoding',
01043                                          'info' => $options );
01044             break;
01045         case 'out':
01046             $res = "\n" . $id . " 0 obj\n<< /Type /Encoding\n";
01047             if ( !isset( $o['info']['encoding'] ) )
01048             {
01049                 $o['info']['encoding'] = 'WinAnsiEncoding';
01050             }
01051             if ( $o['info']['encoding'] != 'none' )
01052             {
01053                 $res.= "/BaseEncoding /" . $o['info']['encoding'] . "\n";
01054             }
01055             $res.= "/Differences \n[";
01056             $onum = -100;
01057             foreach ( $o['info']['differences'] as $num => $label )
01058             {
01059                 if ($num!=$onum+1)
01060                 {
01061                     // we cannot make use of consecutive numbering
01062                     $res.= "\n".$num." /".$label;
01063                 }
01064                 else
01065                 {
01066                     $res.= " /".$label;
01067                 }
01068                 $onum = $num;
01069             }
01070             $res .= "\n]\n>>\nendobj";
01071             return $res;
01072             break;
01073         }
01074     }
01075 
01076 /**
01077  * the document procset, solves some problems with printing to old PS printers
01078  */
01079     function o_procset( $id, $action, $options = '' )
01080     {
01081         if ( $action != 'new' )
01082         {
01083             $o =& $this->objects[$id];
01084         }
01085         switch ( $action )
01086         {
01087         case 'new':
01088             $this->objects[$id] = array( 't' => 'procset',
01089                                          'info' => array( 'PDF' => 1, 'Text' => 1 ) );
01090             $this->o_pages( $this->currentNode, 'procset', $id );
01091             $this->procsetObjectId = $id;
01092             break;
01093         case 'add':
01094             // this is to add new items to the procset list, despite the fact that this is considered
01095             // obselete, the items are required for printing to some postscript printers
01096             switch ( $options )
01097             {
01098             case 'ImageB':
01099             case 'ImageC':
01100             case 'ImageI':
01101                 $o['info'][$options] = 1;
01102                 break;
01103             }
01104             break;
01105         case 'out':
01106             $res = "\n" . $id . " 0 obj\n[";
01107             foreach ( $o['info'] as $label => $val )
01108             {
01109                 $res .= '/'.$label.' ';
01110             }
01111             $res .= "]\nendobj";
01112             return $res;
01113             break;
01114         }
01115     }
01116 
01117     /**
01118      * define the document information
01119      */
01120     function o_info( $id, $action, $options = '' )
01121     {
01122         if ( $action != 'new' )
01123         {
01124             $o =& $this->objects[$id];
01125         }
01126         switch ($action)
01127         {
01128         case 'new':
01129             $this->infoObject = $id;
01130             $date = 'D:' . date( 'Ymd' );
01131             $this->objects[$id] = array( 't' => 'info',
01132                                          'info' => array( 'Creator' => 'eZ Publish CMS, http://ez.no',
01133                                                           'CreationDate' => $date ) );
01134             break;
01135         case 'Title':
01136         case 'Author':
01137         case 'Subject':
01138         case 'Keywords':
01139         case 'Creator':
01140         case 'Producer':
01141         case 'CreationDate':
01142         case 'ModDate':
01143         case 'Trapped':
01144             $o['info'][$action]=$options;
01145             break;
01146         case 'out':
01147             if ( $this->encrypted )
01148             {
01149                 $this->encryptInit( $id );
01150             }
01151             $res = "\n" . $id . " 0 obj\n<<\n";
01152             foreach ( $o['info']  as $k => $v )
01153             {
01154                 $res .= '/' . $k . ' (';
01155                 if ( $this->encrypted )
01156                 {
01157                     $res .= $this->filterText( $this->ARC4( $v ) );
01158                 }
01159                 else
01160                 {
01161                     $res .= $this->filterText( $v );
01162                 }
01163                 $res .= ")\n";
01164             }
01165             $res .= ">>\nendobj";
01166             return $res;
01167             break;
01168         }
01169     }
01170 
01171 /**
01172  * an action object, used to link to URLS initially
01173  */
01174     function o_action( $id, $action, $options = '' )
01175     {
01176         if ( $action != 'new' )
01177         {
01178             $o =& $this->objects[$id];
01179         }
01180         switch ( $action )
01181         {
01182         case 'new':
01183             if ( is_array( $options ) )
01184             {
01185                 $this->objects[$id] = array( 't' => 'action',
01186                                              'info' => $options,
01187                                              'type' => $options['type'] );
01188             }
01189             else
01190             {
01191                 // then assume a URI action
01192                 $this->objects[$id] = array( 't' => 'action',
01193                                              'info' => $options,
01194                                              'type' => 'URI' );
01195             }
01196             break;
01197         case 'out':
01198             if ( $this->encrypted )
01199             {
01200                 $this->encryptInit( $id );
01201             }
01202             $res = "\n" . $id . " 0 obj\n<< /Type /Action";
01203             switch( $o['type'] )
01204             {
01205             case 'ilink':
01206                 // there will be an 'label' setting, this is the name of the destination
01207                 //$res.="\n/S /GoTo\n/D [".$this->destinations[(string)$o['info']['label']]." 0 R /Fit]";
01208                 $res .= "\n/S /GoTo\n/D [" . $this->objects[ $this->destinations[ (string)$o['info']['label'] ] ]['info']['page'] . " 0 R /" .
01209                         $this->objects[ $this->destinations[ (string)$o['info']['label'] ] ]['info']['string'] . ']';
01210                 break;
01211             case 'URI':
01212                 $res .= "\n/S /URI\n/URI (";
01213                 if ( $this->encrypted )
01214                 {
01215                     $res .= $this->filterText( $this->ARC4( $o['info'] ) );
01216                 }
01217                 else
01218                 {
01219                     $res .= $this->filterText( $o['info'] );
01220                 }
01221                 $res .= ")";
01222                 break;
01223             }
01224             $res .= "\n>>\nendobj";
01225             return $res;
01226             break;
01227         }
01228     }
01229 
01230 /**
01231  * an annotation object, this will add an annotation to the current page.
01232  * initially will support just link annotations
01233  */
01234     function o_annotation( $id, $action, $options = '' )
01235     {
01236         if ( $action != 'new' )
01237         {
01238             $o =& $this->objects[$id];
01239         }
01240         switch ( $action )
01241         {
01242         case 'new':
01243             // add the annotation to the current page
01244             $pageId = $this->currentPage;
01245             $this->o_page( $pageId, 'annot', $id);
01246             // and add the action object which is going to be required
01247             switch ( $options['type'] )
01248             {
01249             case 'link':
01250                 $this->objects[$id] = array( 't' => 'annotation', 'info' => $options );
01251                 $this->numObj++;
01252                 $this->o_action( $this->numObj, 'new', $options['url'] );
01253                 $this->objects[$id]['info']['actionId'] = $this->numObj;
01254                 break;
01255             case 'ilink':
01256                 // this is to a named internal link
01257                 $label = $options['label'];
01258                 $this->objects[$id] = array( 't' => 'annotation', 'info' => $options );
01259                 $this->numObj++;
01260                 $this->o_action( $this->numObj, 'new', array( 'type' => 'ilink', 'label' => $label ) );
01261                 $this->objects[$id]['info']['actionId'] = $this->numObj;
01262                 break;
01263             }
01264             break;
01265 
01266         case 'out':
01267             $res = "\n" . $id . " 0 obj\n<< /Type /Annot";
01268             switch ( $o['info']['type'] )
01269             {
01270             case 'link':
01271             case 'ilink':
01272                 $res.= "\n/Subtype /Link";
01273                 break;
01274             }
01275             $res.= "\n/A ".$o['info']['actionId']." 0 R";
01276             $res.= "\n/Border [0 0 0]";
01277             $res.= "\n/H /I";
01278             $res.= "\n/Rect [ ";
01279             foreach ( $o['info']['rect'] as $v )
01280             {
01281                 $res.= sprintf( "%.4F ", $v );
01282             }
01283             $res.= "]";
01284             $res.= "\n>>\nendobj";
01285             return $res;
01286             break;
01287         }
01288     }
01289 
01290 /**
01291  * a page object, it also creates a contents object to hold its contents
01292  */
01293     function o_page($id,$action,$options=''){
01294     if ($action!='new'){
01295         $o =& $this->objects[$id];
01296     }
01297     switch ($action){
01298         case 'new':
01299         $this->numPages++;
01300         $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
01301         if (is_array($options)){
01302         // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
01303         $options['id']=$id;
01304         $this->o_pages($this->currentNode,'page',$options);
01305         } else {
01306         $this->o_pages($this->currentNode,'page',$id);
01307         }
01308         $this->currentPage=$id;
01309         //make a contents object to go with this page
01310         $this->numObj++;
01311         $this->o_contents($this->numObj,'new',$id);
01312         $this->currentContents=$this->numObj;
01313         $this->objects[$id]['info']['contents']=array();
01314         $this->objects[$id]['info']['contents'][]=$this->numObj;
01315         $match = ($this->numPages%2 ? 'odd' : 'even');
01316         foreach($this->addLooseObjects as $oId=>$target){
01317         if ($target=='all' || $match==$target){
01318             $this->objects[$id]['info']['contents'][]=$oId;
01319         }
01320         }
01321         break;
01322         case 'content':
01323         $o['info']['contents'][]=$options;
01324         break;
01325         case 'annot':
01326         // add an annotation to this page
01327         if (!isset($o['info']['annot'])){
01328             $o['info']['annot']=array();
01329         }
01330         // $options should contain the id of the annotation dictionary
01331         $o['info']['annot'][]=$options;
01332         break;
01333         case 'out':
01334         $res="\n".$id." 0 obj\n<< /Type /Page";
01335         $res.="\n/Parent ".$o['info']['parent']." 0 R";
01336         if (isset($o['info']['annot'])){
01337         $res.="\n/Annots [";
01338         foreach($o['info']['annot'] as $aId){
01339             $res.=" ".$aId." 0 R";
01340         }
01341         $res.=" ]";
01342         }
01343         $count = count($o['info']['contents']);
01344         if ($count==1){
01345         $res.="\n/Contents ".$o['info']['contents'][0]." 0 R";
01346         } else if ($count>1){
01347         $res.="\n/Contents [\n";
01348         foreach ($o['info']['contents'] as $cId){
01349             $res.=$cId." 0 R\n";
01350         }
01351         $res.="]";
01352         }
01353         $res.="\n>>\nendobj";
01354         return $res;
01355         break;
01356     }
01357     }
01358 
01359 /**
01360  * the contents objects hold all of the content which appears on pages
01361  */
01362     function o_contents($id,$action,$options=''){
01363     if ($action!='new'){
01364         $o =& $this->objects[$id];
01365     }
01366     switch ($action){
01367         case 'new':
01368         $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
01369         if (strlen($options) && intval($options)){
01370         // then this contents is the primary for a page
01371         $this->objects[$id]['onPage']=$options;
01372         } else if ($options=='raw'){
01373         // then this page contains some other type of system object
01374         $this->objects[$id]['raw']=1;
01375         }
01376         break;
01377         case 'add':
01378         // add more options to the decleration
01379         foreach ($options as $k=>$v){
01380             $o['info'][$k]=$v;
01381         }
01382         case 'out':
01383         $tmp=$o['c'];
01384         $res= "\n".$id." 0 obj\n";
01385         if (isset($this->objects[$id]['raw'])){
01386         $res.=$tmp;
01387         } else {
01388         $res.= "<<";
01389         if (function_exists('gzcompress') && $this->options['compression']){
01390             // then implement ZLIB based compression on this content stream
01391             $res.=" /Filter /FlateDecode";
01392             $tmp = gzcompress($tmp);
01393         }
01394         if ($this->encrypted){
01395             $this->encryptInit($id);
01396             $tmp = $this->ARC4($tmp);
01397         }
01398         foreach($o['info'] as $k=>$v){
01399             $res .= "\n/".$k.' '.$v;
01400         }
01401         $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream";
01402         }
01403         $res.="\nendobj\n";
01404         return $res;
01405         break;
01406     }
01407     }
01408 
01409 /**
01410  * an image object, will be an XObject in the document, includes description and data
01411  */
01412     function o_image($id,$action,$options=''){
01413     if ($action!='new'){
01414         $o =& $this->objects[$id];
01415     }
01416     switch($action){
01417         case 'new':
01418         // make the new object
01419         $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
01420         $this->objects[$id]['info']['Type']='/XObject';
01421         $this->objects[$id]['info']['Subtype']='/Image';
01422         $this->objects[$id]['info']['Width']=$options['iw'];
01423         $this->objects[$id]['info']['Height']=$options['ih'];
01424         if (!isset($options['type']) || $options['type']=='jpg'){
01425         if (!isset($options['channels'])){
01426             $options['channels']=3;
01427         }
01428         switch($options['channels']){
01429             case 1:
01430             $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
01431             break;
01432             default:
01433             $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
01434             break;
01435         }
01436         $this->objects[$id]['info']['Filter']='/DCTDecode';
01437         $this->objects[$id]['info']['BitsPerComponent']=8;
01438         } else if ($options['type']=='png'){
01439         $this->objects[$id]['info']['Filter']='/FlateDecode';
01440         $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
01441         if (strlen($options['pdata'])){
01442             $tmp = ' [ /Indexed /DeviceCMYK '.(strlen($options['pdata'])/3-1).' ';
01443             $this->numObj++;
01444             $this->o_contents($this->numObj,'new');
01445             $this->objects[$this->numObj]['c']=$options['pdata'];
01446             $tmp.=$this->numObj.' 0 R';
01447             $tmp .=' ]';
01448             $this->objects[$id]['info']['ColorSpace'] = $tmp;
01449             if (isset($options['transparency'])){
01450             switch($options['transparency']['type']){
01451                 case 'indexed':
01452                 $tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
01453                 $this->objects[$id]['info']['Mask'] = $tmp;
01454                 break;
01455             }
01456             }
01457         } else {
01458             $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
01459         }
01460         $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
01461         }
01462         // assign it a place in the named resource dictionary as an external object, according to
01463         // the label passed in with it.
01464         $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
01465         // also make sure that we have the right procset object for it.
01466         $this->o_procset($this->procsetObjectId,'add','ImageC');
01467         break;
01468         case 'out':
01469         $tmp=$o['data'];
01470         $res= "\n".$id." 0 obj\n<<";
01471         foreach($o['info'] as $k=>$v){
01472         $res.="\n/".$k.' '.$v;
01473         }
01474         if ($this->encrypted){
01475         $this->encryptInit($id);
01476         $tmp = $this->ARC4($tmp);
01477         }
01478         $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream\nendobj\n";
01479         return $res;
01480         break;
01481     }
01482     }
01483 
01484 /**
01485  * encryption object.
01486  */
01487     function o_encryption($id,$action,$options=''){
01488     if ($action!='new'){
01489         $o =& $this->objects[$id];
01490     }
01491     switch($action){
01492         case 'new':
01493         // make the new object
01494         $this->objects[$id]=array('t'=>'encryption','info'=>$options);
01495         $this->arc4_objnum=$id;
01496         // figure out the additional paramaters required
01497         $pad = chr(0x28).chr(0xBF).chr(0x4E).chr(0x5E).chr(0x4E).chr(0x75).chr(0x8A).chr(0x41).chr(0x64).chr(0x00).chr(0x4E).chr(0x56).chr(0xFF).chr(0xFA).chr(0x01).chr(0x08).chr(0x2E).chr(0x2E).chr(0x00).chr(0xB6).chr(0xD0).chr(0x68).chr(0x3E).chr(0x80).chr(0x2F).chr(0x0C).chr(0xA9).chr(0xFE).chr(0x64).chr(0x53).chr(0x69).chr(0x7A);
01498         $len = strlen($options['owner']);
01499         if ($len>32){
01500         $owner = substr($options['owner'],0,32);
01501         } else if ($len<32){
01502         $owner = $options['owner'].substr($pad,0,32-$len);
01503         } else {
01504         $owner = $options['owner'];
01505         }
01506         $len = strlen($options['user']);
01507         if ($len>32){
01508         $user = substr($options['user'],0,32);
01509         } else if ($len<32){
01510         $user = $options['user'].substr($pad,0,32-$len);
01511         } else {
01512         $user = $options['user'];
01513         }
01514         $tmp = $this->md5_16($owner);
01515         $okey = substr($tmp,0,5);
01516         $this->ARC4_init($okey);
01517         $ovalue=$this->ARC4($user);
01518         $this->objects[$id]['info']['O']=$ovalue;
01519         // now make the u value, phew.
01520         $tmp = $this->md5_16($user.$ovalue.chr($options['p']).chr(255).chr(255).chr(255).$this->fileIdentifier);
01521         $ukey = substr($tmp,0,5);
01522 
01523         $this->ARC4_init($ukey);
01524         $this->encryptionKey = $ukey;
01525         $this->encrypted=1;
01526         $uvalue=$this->ARC4($pad);
01527 
01528         $this->objects[$id]['info']['U']=$uvalue;
01529         $this->encryptionKey=$ukey;
01530 
01531         // initialize the arc4 array
01532         break;
01533         case 'out':
01534         $res= "\n".$id." 0 obj\n<<";
01535         $res.="\n/Filter /Standard";
01536         $res.="\n/V 1";
01537         $res.="\n/R 2";
01538         $res.="\n/O (".$this->filterText($o['info']['O']).')';
01539         $res.="\n/U (".$this->filterText($o['info']['U']).')';
01540         // and the p-value needs to be converted to account for the twos-complement approach
01541         $o['info']['p'] = (($o['info']['p']^255)+1)*-1;
01542         $res.="\n/P ".($o['info']['p']);
01543         $res.="\n>>\nendobj\n";
01544 
01545         return $res;
01546         break;
01547     }
01548     }
01549 
01550 /**
01551  * ARC4 functions
01552  * A series of function to implement ARC4 encoding in PHP
01553  */
01554 
01555 /**
01556  * calculate the 16 byte version of the 128 bit md5 digest of the string
01557  */
01558     function md5_16($string){
01559     $tmp = md5($string);
01560     $out='';
01561     for ($i=0;$i<=30;$i=$i+2){
01562         $out.=chr(hexdec(substr($tmp,$i,2)));
01563     }
01564     return $out;
01565     }
01566 
01567 /**
01568  * initialize the encryption for processing a particular object
01569  */
01570     function encryptInit($id){
01571     $tmp = $this->encryptionKey;
01572     $hex = dechex($id);
01573     if (strlen($hex)<6){
01574         $hex = substr('000000',0,6-strlen($hex)).$hex;
01575     }
01576     $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
01577     $key = $this->md5_16($tmp);
01578     $this->ARC4_init(substr($key,0,10));
01579     }
01580 
01581 /**
01582  * initialize the ARC4 encryption
01583  */
01584     function ARC4_init($key=''){
01585     $this->arc4 = '';
01586     // setup the control array
01587     if (strlen($key)==0){
01588         return;
01589     }
01590     $k = '';
01591     while(strlen($k)<256){
01592         $k.=$key;
01593     }
01594     $k=substr($k,0,256);
01595     for ($i=0;$i<256;$i++){
01596         $this->arc4 .= chr($i);
01597     }
01598     $j=0;
01599     for ($i=0;$i<256;$i++){
01600         $t = $this->arc4[$i];
01601         $j = ($j + ord($t) + ord($k[$i]))%256;
01602         $this->arc4[$i]=$this->arc4[$j];
01603         $this->arc4[$j]=$t;
01604     }
01605     }
01606 
01607 /**
01608  * ARC4 encrypt a text string
01609  */
01610     function ARC4($text){
01611     $len=strlen($text);
01612     $a=0;
01613     $b=0;
01614     $c = $this->arc4;
01615     $out='';
01616     for ($i=0;$i<$len;$i++){
01617         $a = ($a+1)%256;
01618         $t= $c[$a];
01619         $b = ($b+ord($t))%256;
01620         $c[$a]=$c[$b];
01621         $c[$b]=$t;
01622         $k = ord($c[(ord($c[$a])+ord($c[$b]))%256]);
01623         $out.=chr(ord($text[$i]) ^ $k);
01624     }
01625 
01626     return $out;
01627     }
01628 
01629 /**
01630  * functions which can be called to adjust or add to the document
01631  */
01632 
01633 /**
01634  * add a link in the document to an external URL
01635  */
01636     function addLink($url,$x0,$y0,$x1,$y1){
01637     $this->numObj++;
01638     $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
01639     $this->o_annotation($this->numObj,'new',$info);
01640     }
01641 
01642 /**
01643  * add a link in the document to an internal destination (ie. within the document)
01644  */
01645     function addInternalLink($label,$x0,$y0,$x1,$y1){
01646     $this->numObj++;
01647     $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
01648     $this->o_annotation($this->numObj,'new',$info);
01649     }
01650 
01651 /**
01652  * set the encryption of the document
01653  * can be used to turn it on and/or set the passwords which it will have.
01654  * also the functions that the user will have are set here, such as print, modify, add
01655  */
01656     function setEncryption($userPass='',$ownerPass='',$pc=array()){
01657     $p=bindec(11000000);
01658 
01659     $options = array(
01660         'print'=>4
01661         ,'modify'=>8
01662         ,'copy'=>16
01663         ,'add'=>32
01664         );
01665     foreach($pc as $k=>$v){
01666         if ($v && isset($options[$k])){
01667         $p+=$options[$k];
01668         } else if (isset($options[$v])){
01669         $p+=$options[$v];
01670         }
01671     }
01672     // implement encryption on the document
01673     if ($this->arc4_objnum == 0){
01674         // then the block does not exist already, add it.
01675         $this->numObj++;
01676         if (strlen($ownerPass)==0){
01677         $ownerPass=$userPass;
01678         }
01679         $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
01680     }
01681     }
01682 
01683 /**
01684  * should be used for internal checks, not implemented as yet
01685  */
01686     function checkAllHere(){
01687     }
01688 
01689 /**
01690  * return the pdf stream as a string returned from the function
01691  */
01692     function output($debug=0){
01693 
01694     if ($debug){
01695         // turn compression off
01696         $this->options['compression']=0;
01697     }
01698 
01699     if ($this->arc4_objnum){
01700         $this->ARC4_init($this->encryptionKey);
01701     }
01702 
01703     $this->checkAllHere();
01704 
01705     $xref=array();
01706     $content="%PDF-1.3\n%âãÏÓ\n";
01707 //  $content="%PDF-1.3\n";
01708     $pos=strlen($content);
01709     foreach($this->objects as $k=>$v){
01710         $tmp='o_'.$v['t'];
01711         $cont=$this->$tmp($k,'out');
01712         $content.=$cont;
01713         $xref[]=$pos;
01714         $pos+=strlen($cont);
01715     }
01716     $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
01717     foreach($xref as $p){
01718         ++$p;
01719         $content.=substr('0000000000',0,10-strlen($p)).$p." 00000 n \n";
01720     }
01721     $content.="\ntrailer\n  << /Size ".(count($xref)+1)."\n     /Root 1 0 R\n     /Info ".$this->infoObject." 0 R\n";
01722     // if encryption has been applied to this document then add the marker for this dictionary
01723     if ($this->arc4_objnum > 0){
01724         $content .= "/Encrypt ".$this->arc4_objnum." 0 R\n";
01725     }
01726     if (strlen($this->fileIdentifier)){
01727         $content .= "/ID[<".$this->fileIdentifier."><".$this->fileIdentifier.">]\n";
01728     }
01729     $content .= "  >>\nstartxref\n".($pos+1)."\n%%EOF\n";
01730 
01731     return $content;
01732     }
01733 
01734 /**
01735  * intialize a new document
01736  * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
01737  * this function is called automatically by the constructor function
01738  *
01739  * @access private
01740  */
01741     function newDocument($pageSize=array(0,0,612,792)){
01742     $this->numObj=0;
01743     $this->objects = array();
01744 
01745     $this->numObj++;
01746     $this->o_catalog($this->numObj,'new');
01747 
01748     $this->numObj++;
01749     $this->o_outlines($this->numObj,'new');
01750 
01751     $this->numObj++;
01752     $this->o_pages($this->numObj,'new');
01753 
01754     $this->o_pages($this->numObj,'mediaBox',$pageSize);
01755     $this->currentNode = 3;
01756 
01757     $this->numObj++;
01758     $this->o_procset($this->numObj,'new');
01759 
01760     $this->numObj++;
01761     $this->o_info($this->numObj,'new');
01762 
01763     $this->numObj++;
01764     $this->o_page($this->numObj,'new');
01765 
01766     // need to store the first page id as there is no way to get it to the user during
01767     // startup
01768     $this->firstPageId = $this->currentContents;
01769     }
01770 
01771     /**
01772      * open the font file and return a php structure containing it.
01773      * first check if this one has been done before and saved in a form more suited to php
01774      * note that if a php serialized version does not exist it will try and make one, but will
01775      * require write access to the directory to do it... it is MUCH faster to have these serialized
01776      * files.
01777      *
01778      * @access private
01779      */
01780     function openFont( $font )
01781     {
01782         // assume that $font contains both the path and perhaps the extension to the file, split them
01783         $pos = strrpos( $font, '/' );
01784         if ( $pos === false )
01785         {
01786             $dir = './';
01787             $name = $font;
01788         }
01789         else
01790         {
01791             $dir = substr( $font, 0, $pos + 1 );
01792             $name = substr( $font, $pos + 1 );
01793         }
01794 
01795         if ( substr( $name, -5 ) == '.font' )
01796         {
01797             $name = substr( $name, 0, strlen( $name ) - 5 );
01798         }
01799 
01800         $this->addMessage( 'openFont: ' . $font . ' - ' . $name );
01801         if ( file_exists( $dir.'php_'.$name.'.font' ) )
01802         {
01803             $this->addMessage( 'openFont: php file exists ' . $dir . 'php_' . $name . '.font' );
01804             $tmp = file( $dir . 'php_' . $name . '.font' );
01805             $this->fonts[$font] = unserialize( $tmp[0] );
01806             if ( !isset( $this->fonts[$font]['_version_'] ) || $this->fonts[$font]['_version_'] < 1 )
01807             {
01808                 // if the font file is old, then clear it out and prepare for re-creation
01809                 $this->addMessage( 'openFont: clear out, make way for new version.' );
01810                 unset( $this->fonts[$font] );
01811             }
01812         }
01813         if ( !isset( $this->fonts[$font] ) && file_exists( $dir.$name.'.afm' ) )
01814         {
01815             // then rebuild the php_<font>.afm file from the <font>.afm file
01816             $this->addMessage( 'openFont: build php file from ' . $dir.$name.'.afm' );
01817             $data = array();
01818             $file = file( $dir.$name.'.afm' );
01819             foreach ( $file as $rowA )
01820             {
01821                 $row = trim( $rowA );
01822                 $pos = strpos( $row, ' ' );
01823                 if ( $pos )
01824                 {
01825                     // then there must be some keyword
01826                     $key = substr( $row, 0, $pos );
01827                     switch ( $key )
01828                     {
01829                         case 'FontName':
01830                         case 'FullName':
01831                         case 'FamilyName':
01832                         case 'Weight':
01833                         case 'ItalicAngle':
01834                         case 'IsFixedPitch':
01835                         case 'CharacterSet':
01836                         case 'UnderlinePosition':
01837                         case 'UnderlineThickness':
01838                         case 'Version':
01839                         case 'EncodingScheme':
01840                         case 'CapHeight':
01841                         case 'XHeight':
01842                         case 'Ascender':
01843                         case 'Descender':
01844                         case 'StdHW':
01845                         case 'StdVW':
01846                         case 'StartCharMetrics':
01847                             $data[$key] = trim( substr( $row, $pos ) );
01848                         break;
01849                         case 'FontBBox':
01850                             $data[$key] = explode(' ', trim( substr( $row, $pos ) ) );
01851                         break;
01852                         case 'C':
01853                             //C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
01854                             $bits = explode( ';', trim( $row ) );
01855                             $dtmp = array();
01856                             foreach ( $bits as $bit )
01857                             {
01858                                 $bits2 = explode( ' ', trim( $bit ) );
01859                                 if ( strlen( $bits2[0] ) )
01860                                 {
01861                                     if ( count( $bits2 ) > 2 )
01862                                     {
01863                                         $dtmp[$bits2[0]] = array();
01864                                         for ( $i = 1; $i < count( $bits2 ); $i++ )
01865                                         {
01866                                             $dtmp[$bits2[0]][] = $bits2[$i];
01867                                         }
01868                                     }
01869                                     else if ( count( $bits2 ) == 2 )
01870                                     {
01871                                         $dtmp[$bits2[0]] = $bits2[1];
01872                                     }
01873                                 }
01874                             }
01875                             if ( $dtmp['C'] >= 0 )
01876                             {
01877                                 $data['C'][$dtmp['C']] = $dtmp;
01878                                 $data['C'][$dtmp['N']] = $dtmp;
01879                             }
01880                             else
01881                             {
01882                                 $data['C'][$dtmp['N']] = $dtmp;
01883                             }
01884                             break;
01885 
01886                         case 'KPX':
01887                             //KPX Adieresis yacute -40
01888                             $bits = explode( ' ', trim( $row ) );
01889                             $data['KPX'][$bits[1]][$bits[2]] = $bits[3];
01890                             break;
01891                     }
01892                 }
01893             }
01894             $data['_version_'] = 1;
01895             $this->fonts[$font] = $data;
01896             $fp = fopen( $dir.'php_'.$name.'.font', 'w' );
01897             fwrite( $fp, serialize( $data ) );
01898             fclose( $fp );
01899         }
01900         else if ( !isset( $this->fonts[$font] ) )
01901         {
01902             $this->addMessage( 'openFont: no font file found' );
01903             //echo 'Font not Found '.$font;
01904         }
01905     }
01906 
01907     /**
01908      * if the font is not loaded then load it and make the required object
01909      * else just make it the current font
01910      * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
01911      * note that encoding='none' will need to be used for symbolic fonts
01912      * and 'differences' => an array of mappings between numbers 0->255 and character names.
01913      *
01914      */
01915     function selectFont( $fontName, $encoding = '', $set = 1 )
01916     {
01917         if ( !isset( $this->fonts[$fontName] ) )
01918         {
01919             // load the file
01920             $this->openFont( $fontName );
01921             if ( isset( $this->fonts[$fontName] ) )
01922             {
01923                 $this->numObj++;
01924                 $this->numFonts++;
01925                 $pos = strrpos( $fontName, '/' );
01926                 $name = substr( $fontName, $pos + 1 );
01927                 if ( substr( $name, -5 ) == '.font' )
01928                 {
01929                     $name = substr( $name, 0, strlen( $name ) - 5 );
01930                 }
01931                 if ( !isset( $this->fontFamilies[$name] ) )
01932                 {
01933                     $this->setFontFamily( $name, array( 'b' => $name . '-Bold',
01934                                                         'i' => $name . '-Italic',
01935                                                         'bi' =>  $name . '-BoldItalic',
01936                                                         'ib' => $name . '-BoldItalic' ) );
01937                 }
01938                 $options = array( 'name' => $name );
01939                 if ( is_array( $encoding ) )
01940                 {
01941                     // then encoding and differences might be set
01942                     if ( isset( $encoding['encoding'] ) )
01943                     {
01944                         $options['encoding'] = $encoding['encoding'];
01945                     }
01946                     if ( isset( $encoding['differences'] ) )
01947                     {
01948                         $options['differences'] = $encoding['differences'];
01949                     }
01950                 }
01951                 else if ( strlen( $encoding ) )
01952                 {
01953                     // then perhaps only the encoding has been set
01954                     $options['encoding'] = $encoding;
01955                 }
01956                 $fontObj = $this->numObj;
01957                 $this->o_font( $this->numObj, 'new', $options );
01958                 $this->fonts[$fontName]['fontNum'] = $this->numFonts;
01959                 // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
01960                 // should be for all non-basic fonts), then load it into an object and put the
01961                 // references into the font object
01962                 $basefile = substr( $fontName, 0, strlen( $fontName ) );
01963                 if ( file_exists( $basefile.'.pfb' ) )
01964                 {
01965                     $fbtype = 'pfb';
01966                 }
01967                 else if ( file_exists( $basefile.'.ttf' ) )
01968                 {
01969                     $fbtype = 'ttf';
01970                 }
01971                 else
01972                 {
01973                     $fbtype = '';
01974                 }
01975                 $fbfile = $basefile.'.'.$fbtype;
01976 
01977                 $this->addMessage( 'selectFont: checking for - ' . $fbfile );
01978                 if ( strlen( $fbtype ) )
01979                 {
01980                     $adobeFontName = $this->fonts[$fontName]['FontName'];
01981                     $this->addMessage( 'selectFont: adding font file - ' . $fbfile . ' - ' . $adobeFontName );
01982                     // find the array of fond widths, and put that into an object.
01983                     $firstChar = -1;
01984                     $lastChar = 0;
01985                     $widths = array();
01986                     foreach ( $this->fonts[$fontName]['C'] as $num => $d )
01987                     {
01988                         if ( intval($num) > 0 || $num == '0' )
01989                         {
01990                             if ( $lastChar > 0 && $num > $lastChar + 1 )
01991                             {
01992                                 for ( $i = $lastChar + 1; $i < $num; $i++ )
01993                                 {
01994                                     $widths[] = 0;
01995                                 }
01996                             }
01997                             $widths[] = $d['WX'];
01998                             if ( $firstChar == -1 )
01999                             {
02000                                 $firstChar = $num;
02001                             }
02002                             $lastChar = $num;
02003                         }
02004                     }
02005                     // also need to adjust the widths for the differences array
02006                     if ( isset( $options['differences'] ) )
02007                     {
02008                         foreach( $options['differences'] as $charNum => $charName )
02009                         {
02010                             if ( $charNum > $lastChar )
02011                             {
02012                                 for( $i = $lastChar + 1; $i <= $charNum; $i++ )
02013                                 {
02014                                     $widths[] = 0;
02015                                 }
02016                                 $lastChar = $charNum;
02017                             }
02018                             if ( isset( $this->fonts[$fontName]['C'][$charName] ) )
02019                             {
02020                                 $widths[$charNum-$firstChar] = $this->fonts[$fontName]['C'][$charName]['WX'];
02021                             }
02022                         }
02023                     }
02024                     $this->addMessage( 'selectFont: FirstChar=' . $firstChar );
02025                     $this->addMessage( 'selectFont: LastChar=' . $lastChar );
02026                     $this->numObj++;
02027                     $this->o_contents( $this->numObj, 'new', 'raw' );
02028                     $this->objects[$this->numObj]['c'] .= '[';
02029                     foreach ( $widths as $width )
02030                     {
02031                         $this->objects[$this->numObj]['c'] .= ' ' . $width;
02032                     }
02033                     $this->objects[$this->numObj]['c'] .= ' ]';
02034                     $widthid = $this->numObj;
02035 
02036                     // load the pfb file, and put that into an object too.
02037                     // note that pdf supports only binary format type 1 font files, though there is a
02038                     // simple utility to convert them from pfa to pfb.
02039                     $fp = fopen( $fbfile, 'rb' );
02040                     $tmp = get_magic_quotes_runtime();
02041                     set_magic_quotes_runtime( 0 );
02042                     $data = fread( $fp, filesize( $fbfile ) );
02043                     set_magic_quotes_runtime( $tmp );
02044                     fclose( $fp );
02045 
02046                     // create the font descriptor
02047                     $this->numObj++;
02048                     $fontDescriptorId = $this->numObj;
02049                     $this->numObj++;
02050                     $pfbid = $this->numObj;
02051                     // determine flags (more than a little flakey, hopefully will not matter much)
02052                     $flags=0;
02053                     if ( $this->fonts[$fontName]['ItalicAngle'] != 0 )
02054                     {
02055                         $flags += pow( 2, 6 );
02056                     }
02057                     if ( $this->fonts[$fontName]['IsFixedPitch'] == 'true' )
02058                     {
02059                         $flags += 1;
02060                     }
02061                     $flags += pow( 2, 5 ); // assume non-sybolic
02062 
02063                     $list = array( 'Ascent' => 'Ascender',
02064                                    'CapHeight' => 'CapHeight',
02065                                    'Descent' => 'Descender',
02066                                    'FontBBox' => 'FontBBox',
02067                                    'ItalicAngle' => 'ItalicAngle' );
02068                     $fdopt = array( 'Flags' => $flags,
02069                                     'FontName' => $adobeFontName,
02070                                     'StemV' => 100  // don't know what the value for this should be!
02071                                     );
02072                     foreach ( $list as $k => $v )
02073                     {
02074                         if ( isset( $this->fonts[$fontName][$v] ) )
02075                         {
02076                             $fdopt[$k] = $this->fonts[$fontName][$v];
02077                         }
02078                     }
02079 
02080                     if ( $fbtype == 'pfb' )
02081                     {
02082                         $fdopt['FontFile'] = $pfbid;
02083                     }
02084                     else if ( $fbtype == 'ttf' )
02085                     {
02086                         $fdopt['FontFile2'] = $pfbid;
02087                     }
02088                     $this->o_fontDescriptor( $fontDescriptorId, 'new', $fdopt );
02089 
02090                     // embed the font program
02091                     $this->o_contents( $this->numObj, 'new' );
02092                     $this->objects[$pfbid]['c'] .= $data;
02093                     // determine the cruicial lengths within this file
02094                     if ($fbtype=='pfb')
02095                     {
02096                         $l1 = strpos($data,'eexec') + 6;
02097                         $l2 = strpos($data,'00000000') - $l1;
02098                         $l3 = strlen($data) - $l2 - $l1;
02099                         $this->o_contents( $this->numObj, 'add', array( 'Length1' => $l1,
02100                                                                         'Length2' => $l2,
02101                                                                         'Length3' => $l3 ) );
02102                     }
02103                     else if ( $fbtype == 'ttf' )
02104                     {
02105                         $l1 = strlen($data);
02106                         $this->o_contents( $this->numObj, 'add', array( 'Length1' => $l1 ) );
02107                     }
02108 
02109                     // tell the font object about all this new stuff
02110                     $tmp = array( 'BaseFont' => $adobeFontName,
02111                                   'Widths' => $widthid,
02112                                   'FirstChar' => $firstChar,
02113                                   'LastChar' => $lastChar,
02114                                   'FontDescriptor' => $fontDescriptorId );
02115                     if ( $fbtype == 'ttf' )
02116                     {
02117                         $tmp['SubType'] = 'TrueType';
02118                     }
02119                     $this->addMessage( 'adding extra info to font.('.$fontObj.')' );
02120                     foreach ( $tmp as $fk => $fv )
02121                     {
02122                         $this->addMessage( $fk." : ".$fv );
02123                     }
02124                     $this->o_font( $fontObj, 'add', $tmp );
02125 
02126                 }
02127                 else
02128                 {
02129                     $this->addMessage( 'selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts' );
02130                 }
02131 
02132 
02133                 // also set the differences here, note that this means that these will take effect only the
02134                 //first time that a font is selected, else they are ignored
02135                 if ( isset( $options['differences'] ) )
02136                 {
02137                     $this->fonts[$fontName]['differences'] = $options['differences'];
02138                 }
02139             }
02140         }
02141         if ( $set && isset( $this->fonts[$fontName] ) )
02142         {
02143             // so if for some reason the font was not set in the last one then it will not be selected
02144             $this->currentBaseFont = $fontName;
02145             // the next line means that if a new font is selected, then the current text state will be
02146             // applied to it as well.
02147             $this->setCurrentFont();
02148         }
02149         return $this->currentFontNum;
02150     }
02151 
02152     /**
02153      Get current font
02154 
02155      \return current font name
02156     */
02157     function currentFont()
02158     {
02159         return $this->currentBaseFont;
02160     }
02161 
02162     /**
02163      * sets up the current font, based on the font families, and the current text state
02164      * note that this system is quite flexible, a <b><i> font can be completely different to a
02165      * <i><b> font, and even <b><b> will have to be defined within the family to have meaning
02166      * This function is to be called whenever the currentTextState is changed, it will update
02167      * the currentFont setting to whatever the appropriatte family one is.
02168      * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
02169      * This function will change the currentFont to whatever it should be, but will not change the
02170      * currentBaseFont.
02171      *
02172      * @access private
02173      */
02174     function setCurrentFont()
02175     {
02176         if ( strlen( $this->currentBaseFont ) == 0 )
02177         {
02178             // then assume an initial font
02179             $this->selectFont( 'lib/ezpdf/classes/fonts/Helvetica' );
02180         }
02181         $cf = substr( $this->currentBaseFont, strrpos( $this->currentBaseFont, '/' ) + 1 );
02182         if ( strlen( $this->currentTextState )
02183              && isset( $this->fontFamilies[$cf] )
02184              && isset( $this->fontFamilies[$cf][$this->currentTextState] ) )
02185         {
02186             // then we are in some state or another
02187             // and this font has a family, and the current setting exists within it
02188             // select the font, then return it
02189             $nf = substr( $this->currentBaseFont, 0, strrpos( $this->currentBaseFont, '/' ) + 1 ) .
02190                   $this->fontFamilies[$cf][$this->currentTextState];
02191             $this->selectFont( $nf, '', 0 );
02192             $this->currentFont = $nf;
02193             $this->currentFontNum = $this->fonts[$nf]['fontNum'];
02194         }
02195         else
02196         {
02197             // the this font must not have the right family member for the current state
02198             // simply assume the base font
02199             $this->currentFont = $this->currentBaseFont;
02200             $this->currentFontNum = $this->fonts[$this->currentFont]['fontNum'];
02201         }
02202     }
02203 
02204     /**
02205      * function for the user to find out what the ID is of the first page that was created during
02206      * startup - useful if they wish to add something to it later.
02207      */
02208     function getFirstPageId()
02209     {
02210         return $this->firstPageId;
02211     }
02212 
02213     /**
02214      * add content to the currently active object
02215      *
02216      * @access private
02217      */
02218     function addContent( $content )
02219     {
02220         $this->objects[$this->currentContents]['c'] .= $content;
02221     }
02222 
02223     /**
02224      * sets the colour for fill operations
02225      */
02226     function setColorRGB( $r, $g, $b, $force = 0 )
02227     {
02228         $this->setColor( eZMath::rgbToCMYK2( $r, $g, $b ), $force );
02229     }
02230 
02231     /*!
02232      sets the colour for fill operations
02233 
02234      \param CMYK array
02235     */
02236     function setColor( $cmykArray, $force = 0 )
02237     {
02238         if ( $force || !$this->compareCMYK( $cmykArray, $this->currentColour ) )
02239         {
02240             $this->objects[$this->currentContents]['c'] .= "\n" .
02241                  sprintf( '%.3F', $cmykArray['c'] ) . ' ' . sprintf( '%.3F', $cmykArray['m'] ) . ' ' .
02242                  sprintf( '%.3F', $cmykArray['y'] ) . ' ' . sprintf( '%.3F', $cmykArray['k'] ) . ' k';
02243             $this->currentColour = $cmykArray;
02244         }
02245     }
02246 
02247     /**
02248      * sets the colour for stroke operations
02249      */
02250     function setStrokeColorRGB( $r, $g, $b, $force = 0 )
02251     {
02252         $this->setStrokeColor( eZMath::rgbToCMYK2( $r, $g, $b ), $force );
02253     }
02254 
02255     /*!
02256       sets the colour for stroke operations
02257 
02258       \param cmyk array
02259       \param force color change
02260      */
02261     function setStrokeColor( $cmykArray, $force = 0 )
02262     {
02263         if ( $force || !$this->compareCMYK( $cmykArray, $this->currentStrokeColour ) )
02264         {
02265             $this->objects[$this->currentContents]['c'] .= "\n" .
02266                  sprintf( '%.3F', $cmykArray['c'] ) . ' ' . sprintf( '%.3F', $cmykArray['m'] ) . ' ' .
02267                  sprintf( '%.3F', $cmykArray['y'] ) . ' ' . sprintf( '%.3F', $cmykArray['k'] ) . ' K';
02268             $this->currentStrokeColour = $cmykArray;
02269         }
02270     }
02271 
02272     /**
02273      * draw a line from one set of coordinates to another
02274      */
02275     function line( $x1, $y1, $x2, $y2 )
02276     {
02277         $this->objects[$this->currentContents]['c'] .= "\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' m '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' l S';
02278     }
02279 
02280     /**
02281      * draw a bezier curve based on 4 control points
02282      */
02283     function curve( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 )
02284     {
02285         // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
02286         // as the control points for the curve.
02287         $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' m '.sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1);
02288         $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',$x2).' '.sprintf('%.3F',$y2).' '.sprintf('%.3F',$x3).' '.sprintf('%.3F',$y3).' c S';
02289     }
02290 
02291     /**
02292      * draw a part of an ellipse
02293      */
02294     function partEllipse( $x0, $y0, $astart, $afinish, $r1, $r2 = 0, $angle = 0, $nSeg = 8 )
02295     {
02296         $this->ellipse( $x0, $y0, $r1, $r2, $angle, $nSeg, $astart, $afinish, 0 );
02297     }
02298 
02299     /**
02300      * draw a filled ellipse
02301      */
02302     function filledEllipse( $x0, $y0, $r1, $r2 = 0, $angle = 0, $nSeg = 8, $astart = 0, $afinish = 360 )
02303     {
02304         return $this->ellipse( $x0, $y0, $r1, $r2 = 0, $angle, $nSeg, $astart, $afinish, 1, 1 );
02305     }
02306 
02307     /**
02308      * draw an ellipse
02309      * note that the part and filled ellipse are just special cases of this function
02310      *
02311      * draws an ellipse in the current line style
02312      * centered at $x0,$y0, radii $r1,$r2
02313      * if $r2 is not set, then a circle is drawn
02314      * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
02315      * pretty crappy shape at 2, as we are approximating with bezier curves.
02316      */
02317     function ellipse( $x0, $y0, $r1, $r2 = 0, $angle = 0, $nSeg = 8, $astart = 0, $afinish = 360, $close = 1, $fill = 0 )
02318     {
02319         if ( $r1 == 0 )
02320         {
02321             return;
02322         }
02323         if ( $r2 == 0 )
02324         {
02325             $r2 = $r1;
02326         }
02327         if ( $nSeg < 2 )
02328         {
02329             $nSeg = 2;
02330         }
02331 
02332         $astart = deg2rad( (float)$astart );
02333         $afinish = deg2rad( (float)$afinish );
02334         $totalAngle = $afinish - $astart;
02335 
02336         $dt = $totalAngle / $nSeg;
02337         $dtm = $dt / 3;
02338 
02339         if ( $angle != 0 )
02340         {
02341             $a = -1*deg2rad((float)$angle);
02342             $tmp = "\n q ";
02343             $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
02344             $tmp .= sprintf('%.3F',$x0).' '.sprintf('%.3F',$y0).' cm';
02345             $this->objects[$this->currentContents]['c'].= $tmp;
02346             $x0=0;
02347             $y0=0;
02348         }
02349 
02350         $t1 = $astart;
02351         $a0 = $x0 + $r1*cos($t1);
02352         $b0 = $y0 + $r2*sin($t1);
02353         $c0 = -$r1 * sin($t1);
02354         $d0 = $r2 * cos($t1);
02355 
02356         $this->objects[$this->currentContents]['c'] .= "\n".sprintf('%.3F',$a0).' '.sprintf('%.3F',$b0).' m ';
02357         for ($i=1;$i<=$nSeg;$i++)
02358         {
02359             // draw this bit of the total curve
02360             $t1 = $i*$dt + $astart;
02361             $a1 = $x0 + $r1*cos($t1);
02362             $b1 = $y0 + $r2*sin($t1);
02363             $c1 = -$r1 * sin($t1);
02364             $d1 = $r2 * cos($t1);
02365             $this->objects[$this->currentContents]['c'].= "\n".sprintf('%.3F',($a0+$c0*$dtm)).' '.sprintf('%.3F',($b0+$d0*$dtm));
02366             $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3F',($a1-$c1*$dtm)).' '.sprintf('%.3F',($b1-$d1*$dtm)).' '.sprintf('%.3F',$a1).' '.sprintf('%.3F',$b1).' c';
02367             $a0 = $a1;
02368             $b0 = $b1;
02369             $c0 = $c1;
02370             $d0 = $d1;
02371         }
02372         if ( $fill )
02373         {
02374             $this->objects[$this->currentContents]['c'].= ' f';
02375         }
02376         else
02377         {
02378             if ($close)
02379             {
02380                 $this->objects[$this->currentContents]['c'].= ' s'; // small 's' signifies closing the path as well
02381             }
02382             else
02383             {
02384                 $this->objects[$this->currentContents]['c'].= ' S';
02385             }
02386         }
02387         if ( $angle != 0 )
02388         {
02389             $this->objects[$this->currentContents]['c'].= ' Q';
02390         }
02391     }
02392 
02393     /**
02394      * this sets the line drawing style.
02395      * width, is the thickness of the line in user units
02396      * cap is the type of cap to put on the line, values can be 'butt','round','square'
02397      *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
02398      *    end of the line.
02399      * join can be 'miter', 'round', 'bevel'
02400      * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
02401      *   on and off dashes.
02402      *   (2) represents 2 on, 2 off, 2 on , 2 off ...
02403      *   (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
02404      * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
02405      */
02406     function setLineStyle( $width = 1, $cap = '', $join = '', $dash = '', $phase = 0 )
02407     {
02408 
02409         // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
02410         $string = '';
02411         if ( $width > 0 )
02412         {
02413             $string.= $width.' w';
02414         }
02415         $ca = array( 'butt' => 0, 'round' => 1, 'square' => 2 );
02416         if ( isset( $ca[$cap] ) )
02417         {
02418             $string.= ' '.$ca[$cap].' J';
02419         }
02420         $ja = array('miter'=>0,'round'=>1,'bevel'=>2);
02421         if ( isset( $ja[$join] ) )
02422         {
02423             $string.= ' '.$ja[$join].' j';
02424         }
02425         if ( is_array( $dash ) )
02426         {
02427             $string.= ' [';
02428             foreach ( $dash as $len )
02429             {
02430                 $string.=' '.$len;
02431             }
02432             $string.= ' ] '.$phase.' d';
02433         }
02434         $this->currentLineStyle = $string;
02435         $this->objects[$this->currentContents]['c'].= "\n".$string;
02436     }
02437 
02438     /**
02439      * draw a polygon, the syntax for this is similar to the GD polygon command
02440      */
02441     function polygon( $p, $np, $f = 0 )
02442     {
02443         $this->objects[$this->currentContents]['c'].= "\n";
02444         $this->objects[$this->currentContents]['c'].= sprintf('%.3F',$p[0]).' '.sprintf('%.3F',$p[1]).' m ';
02445         for ( $i = 2; $i < $np*2; $i = $i + 2 )
02446         {
02447             $this->objects[$this->currentContents]['c'].= sprintf('%.3F',$p[$i]).' '.sprintf('%.3F',$p[$i+1]).' l ';
02448         }
02449         if ( $f == 1 )
02450         {
02451             $this->objects[$this->currentContents]['c'].= ' f';
02452         }
02453         else
02454         {
02455             $this->objects[$this->currentContents]['c'].= ' S';
02456         }
02457     }
02458 
02459     /**
02460      Create shaded rectangle area
02461 
02462      \param x1
02463      \param x2
02464      \param width
02465      \param height
02466      \param options
02467             array ( 'orientation' => <vertical|horizontal ( optianal )>,
02468                     'color0' => <CMYK color array>,
02469                     'color1' => <CMYK color array> )
02470     */
02471     function shadedRectangle( $x1, $y1, $width, $height, $options )
02472     {
02473         $this->numObj++;
02474         $shadingLabel = $this->o_shading( $this->numObj, 'new', $options );
02475 
02476         $this->saveState();
02477 
02478         $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re';
02479 
02480         $this->objects[$this->currentContents]['c'].="\nW n";
02481 
02482         $this->objects[$this->currentContents]['c'].="\n/".$shadingLabel.' sh';
02483 
02484         $this->restoreState();
02485     }
02486 
02487     /**
02488      * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
02489      * the coordinates of the upper-right corner
02490      */
02491     function filledRectangle( $x1, $y1, $width, $height )
02492     {
02493         $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re f';
02494     }
02495 
02496     /**
02497      * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
02498      * the coordinates of the upper-right corner
02499      */
02500     function rectangle( $x1, $y1, $width, $height )
02501     {
02502         $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$x1).' '.sprintf('%.3F',$y1).' '.sprintf('%.3F',$width).' '.sprintf('%.3F',$height).' re S';
02503     }
02504 
02505     /**
02506      * add a new page to the document
02507      * this also makes the new page the current active object
02508      */
02509     function newPage( $insert = 0, $id = 0, $pos = 'after' )
02510     {
02511         // if there is a state saved, then go up the stack closing them
02512         // then on the new page, re-open them with the right setings
02513 
02514         if ( $this->nStateStack )
02515         {
02516             for ( $i = $this->nStateStack; $i >= 1; $i-- )
02517             {
02518                 $this->restoreState( $i );
02519             }
02520         }
02521 
02522         $this->numObj++;
02523         if ( $insert )
02524         {
02525             // the id from the ezPdf class is the od of the contents of the page, not the page object itself
02526             // query that object to find the parent
02527             $rid = $this->objects[$id]['onPage'];
02528             $opt = array( 'rid' => $rid, 'pos' => $pos );
02529             $this->o_page( $this->numObj, 'new', $opt);
02530         }
02531         else
02532         {
02533             $this->o_page( $this->numObj, 'new' );
02534         }
02535         // if there is a stack saved, then put that onto the page
02536         if ( $this->nStateStack )
02537         {
02538             for ( $i = 1; $i <= $this->nStateStack; $i++)
02539             {
02540                 $this->saveState($i);
02541             }
02542         }
02543         // and if there has been a stroke or fill colour set, then transfer them
02544         if ( $this->currentColour['c'] >= 0 )
02545         {
02546             $this->setColor( $this->currentColour, 1 );
02547         }
02548         if ( $this->currentStrokeColour['c'] >= 0 )
02549         {
02550             $this->setStrokeColor( $this->currentStrokeColour, 1 );
02551         }
02552 
02553         // if there is a line style set, then put this in too
02554         if ( strlen( $this->currentLineStyle ) )
02555         {
02556             $this->objects[$this->currentContents]['c'].= "\n".$this->currentLineStyle;
02557         }
02558 
02559         // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
02560         return $this->currentContents;
02561     }
02562 
02563     /**
02564      * output the pdf code, streaming it to the browser
02565      * the relevant headers are set so that hopefully the browser will recognise it
02566      */
02567     function stream( $options = '' )
02568     {
02569         // setting the options allows the adjustment of the headers
02570         // values at the moment are:
02571         // 'Content-Disposition'=>'filename'  - sets the filename, though not too sure how well this will
02572         //        work as in my trial the browser seems to use the filename of the php file with .pdf on the end
02573         // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
02574         //    this header seems to have caused some problems despite tha fact that it is supposed to solve
02575         //    them, so I am leaving it off by default.
02576         // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
02577         if ( !is_array( $options ) )
02578         {
02579             $options = array();
02580         }
02581         if ( isset( $options['compress'] ) && $options['compress'] == 0 )
02582         {
02583             $tmp = $this->output( 1 );
02584         }
02585         else
02586         {
02587             $tmp = $this->output();
02588         }
02589         $tmp = ltrim( $tmp );
02590 
02591         ob_clean();
02592 
02593         header( 'Pragma: ' );
02594         header( 'Cache-Control: ' );
02595         /* Set cache time out to 10 seconds, this should be good enough to work around an IE bug */
02596         header( "Expires: ". gmdate( 'D, d M Y H:i:s', time() + 10 ) . ' GMT' );
02597         header( 'X-Powered-By: eZ Publish' );
02598 
02599         header( 'Content-Length: '.strlen( $tmp ) );
02600         header( 'Content-Type: application/pdf' );
02601         header( 'Content-Transfer-Encoding: binary' );
02602         header( 'Accept-Ranges: bytes' );
02603 
02604         ob_end_clean();
02605 
02606         echo $tmp;
02607 
02608         require_once( 'lib/ezutils/classes/ezexecution.php' );
02609         eZExecution::cleanExit();
02610     }
02611 
02612     /**
02613      * return the height in units of the current font in the given size
02614      */
02615     function getFontHeight( $size = false )
02616     {
02617         if ( !$this->numFonts )
02618         {
02619             $this->selectFont( './fonts/Helvetica' );
02620         }
02621         // for the current font, and the given size, what is the height of the font in user units
02622         $h = $this->fonts[$this->currentFont]['FontBBox'][3]-$this->fonts[$this->currentFont]['FontBBox'][1];
02623         if ( $size === false )
02624         {
02625             $size = $this->fontSize();
02626         }
02627         return $this->ez['lineSpace'] * $size * $h / 1000;
02628     }
02629 
02630     /**
02631      * return the font decender, this will normally return a negative number
02632      * if you add this number to the baseline, you get the level of the bottom of the font
02633      * it is in the pdf user units
02634      */
02635     function getFontDecender( $size = false )
02636     {
02637         // note that this will most likely return a negative value
02638         if ( !$this->numFonts )
02639         {
02640             $this->selectFont( './fonts/Helvetica' );
02641         }
02642         $h = $this->fonts[$this->currentFont]['FontBBox'][1];
02643         if ( $size === false )
02644         {
02645             $size = $this->fontSize();
02646         }
02647         return $size*$h/1000;
02648     }
02649 
02650     /**
02651      * filter the text, this is applied to all text just before being inserted into the pdf document
02652      * it escapes the various things that need to be escaped, and so on
02653      *
02654      * @access private
02655      */
02656     function filterText( $text )
02657     {
02658         $text = str_replace('\\','\\\\',$text);
02659         $text = str_replace('(','\(',$text);
02660         $text = str_replace(')','\)',$text);
02661         $text = str_replace('&lt;','<',$text);
02662         $text = str_replace('&gt;','>',$text);
02663         $text = str_replace('&#039;','\'',$text);
02664         $text = str_replace('&quot;','"',$text);
02665         $text = str_replace('&amp;','&',$text);
02666 
02667         return $text;
02668     }
02669 
02670     /**
02671      * given a start position and information about how text is to be laid out, calculate where
02672      * on the page the text will end
02673      *
02674      * @access private
02675      */
02676     function PRVTgetTextPosition( $x, $y, $angle, $size, $wa, $text )
02677     {
02678         $tmp = false;
02679         // given this information return an array containing x and y for the end position as elements 0 and 1
02680 
02681         $w = $this->getTextWidth($size,$text);
02682         // need to adjust for the number of spaces in this text
02683         $words = explode(' ',$text);
02684         $nspaces=count($words)-1;
02685         $w += $wa*$nspaces;
02686         $a = deg2rad((float)$angle);
02687         if ( $tmp )
02688         {
02689             return array($this->xOffset(),-sin($a)*$w+$y);
02690         }
02691         return array(cos($a)*$w+$x,-sin($a)*$w+$y);
02692     }
02693 
02694     /**
02695      * wrapper function for PRVTcheckTextDirective1
02696      *
02697      * @access private
02698      */
02699     function PRVTcheckTextDirective( &$text, $i, &$f, $final = 0 )
02700     {
02701         $x = 0;
02702         $y = 0;
02703         return $this->PRVTcheckTextDirective1( $text, $i, $f, $final, $x, $y );
02704     }
02705 
02706     /**
02707      * checks if the text stream contains a control directive
02708      * if so then makes some changes and returns the number of characters involved in the directive
02709      * this has been re-worked to include everything neccesary to fins the current writing point, so that
02710      * the location can be sent to the callback function if required
02711      * if the directive does not require a font change, then $f should be set to 0
02712      *
02713      * @access private
02714      */
02715     function PRVTcheckTextDirective1( &$text, $i, &$f, $final, &$x, &$y, $size = 0, $angle = 0, $wordSpaceAdjust = 0 )
02716     {
02717         $newTextState = $this->currentTextState;
02718         $noClose = 0;
02719         $directive = 0;
02720         $j = $i;
02721         if ( $text[$j] == '<' )
02722         {
02723             $j++;
02724             switch ( $text[$j] )
02725             {
02726             case '/':
02727                 $j++;
02728                 if (strlen($text) <= $j)
02729                 {
02730                     return $directive;
02731                 }
02732                 switch ( $text[$j] )
02733                 {
02734                     case 'b':
02735                     case 'i':
02736                         ++$j;
02737                     if ( $text[$j] == '>' )
02738                     {
02739                         if ( $final === true )
02740                             $this->currentTextState = substr( $this->currentTextState, 0, $p).substr( $this->currentTextState, $p+1);
02741                         $directive = $j-$i+1;
02742                     }
02743                     break;
02744 
02745                     case 'c':
02746                     {
02747                         // this this might be a callback function
02748                         $j++;
02749                         $k = strpos( $text, '>', $j );
02750                         if ( $k !== false && $text[$j] == ':' )
02751                         {
02752                             // then this will be treated as a callback directive
02753                             $directive = $k-$i+1;
02754                             $f = 0;
02755                             // split the remainder on colons to get the function name and the paramater
02756                             $tmp = substr( $text, $j+1, $k-$j-1 );
02757                             $b1 = strpos( $tmp, ':' );
02758                             if ( $b1 !== false )
02759                             {
02760                                 $func = substr( $tmp, 0, $b1 );
02761                                 $parm = substr( $tmp, $b1+1 );
02762                             }
02763                             else
02764                             {
02765                                 $func = $tmp;
02766                                 $parm = '';
02767                             }
02768                             if ( !isset( $func ) || !strlen( trim( $func ) ) )
02769                             {
02770                                 $directive = 0;
02771                             }
02772                             else
02773                             {
02774                                 // only call the function if this is the final call
02775                                 if ( $final === 1 )
02776                                 {
02777                                     // need to assess the text position, calculate the text width to this point
02778                                     // can use getTextWidth to find the text width I think
02779                                     if ( $x < $this->leftMargin() )
02780                                     {
02781                                         $x = $this->leftMargin();
02782                                     }
02783                                     $startInfo = $this->callback[$this->nCallback];
02784 
02785                                     if ( $x < $this->xOffset() )
02786                                     {
02787                                         $x = $this->xOffset();
02788                                     }
02789                                     $tmp = $this->PRVTgetTextPosition( $x, $y, $angle, $size, $wordSpaceAdjust, substr( $text, $startInfo['i'], $i-$startInfo['i'] ) );
02790                                     $info = array( 'x' => $tmp[0],'y' => $tmp[1], 'angle' => $angle, 'status' => 'end', 'p' => $parm, 'nCallback' => $this->nCallback );
02791 
02792                                     $x = $tmp[0];
02793                                     $y = $tmp[1];
02794                                     $ret = $this->$func( $info );
02795                                     if ( is_array( $ret ) )
02796                                     {
02797                                         // then the return from the callback function could set the position, to start with, later will do font colour, and font
02798                                         foreach ( $ret as $rk => $rv )
02799                                         {
02800                                             switch ( $rk )
02801                                             {
02802                                                 case 'x':
02803                                                 case 'y':
02804                                                     $$rk = $rv;
02805                                                 break;
02806                                             }
02807                                         }
02808                                     }
02809                                     // also remove from to the stack
02810                                     // for simplicity, just take from the end, fix this another day
02811                                     $this->nCallback--;
02812                                     if ( $this->nCallback < 0 )
02813                                     {
02814                                         $this->nCallBack = 0;
02815                                     }
02816                                 }
02817                             }
02818                         }
02819                     } break;
02820                 }
02821                 break;
02822 
02823             case 'b':
02824             case 'i':
02825                 ++$j;
02826 
02827                 if ( $text[$j] == '>' )
02828                 {
02829                     if (  $final === true )
02830                         $this->currentTextState .= $text[$j-1];
02831                     $directive = $j-$i+1;
02832                 }
02833                 break;
02834 
02835             case 'C':
02836                 $noClose = 1;
02837             case 'c':
02838                 // this this might be a callback function
02839                 $j++;
02840                 $k = strpos( $text, '>', $j );
02841                 if ( $k !== false && $text[$j] == ':' )
02842                 {
02843                     // then this will be treated as a callback directive
02844                     $directive = $k-$i+1;
02845                     $f=0;
02846                     // split the remainder on colons to get the function name and the paramater
02847                     $tmp = substr( $text, $j+1, $k-$j-1 );
02848                     $b1 = strpos( $tmp, ':' );
02849                     if ( $b1 !== false )
02850                     {
02851                         $func = substr( $tmp, 0, $b1 );
02852                         $parm = substr( $tmp, $b1+1 );
02853                     }
02854                     else
02855                     {
02856                         $func = $tmp;
02857                         $parm = '';
02858                     }
02859                     if ( !isset( $func ) || !strlen( trim( $func ) ) )
02860                     {
02861                         $directive=0;
02862                     }
02863                     else
02864                     {
02865                         // only call the function if this is the final call, ie, the one actually doing printing, not measurement
02866                         if ($final === 1)
02867                         {
02868                             // need to assess the text position, calculate the text width to this point
02869                             // can use getTextWidth to find the text width I think
02870                             // also add the text height and decender
02871                             $tmp = $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
02872                             if ( $tmp[0] < $this->leftMargin() )
02873                             {
02874                                 $tmp[0] = $this->leftMargin();
02875                             }
02876                             if ( $tmp[0] < $this->xOffset() )
02877                             {
02878                                 $tmp[0] = $this->xOffset();
02879                             }
02880                             $info = array( 'x' => $tmp[0],
02881                                            'y' => $tmp[1],
02882                                            'angle' => $angle,
02883                                            'status' => 'start',
02884                                            'p' => $parm,
02885                                            'f' => $func,
02886                                            'height' => $this->getFontHeight( $size ),
02887                                            'decender' => $this->getFontDecender( $size ),
02888                                            'i' => $i );
02889 
02890                             $x = $tmp[0];
02891                             $y = $tmp[1];
02892                             if (!isset($noClose) || !$noClose)
02893                             {
02894                                 // only add to the stack if this is a small 'c', therefore is a start-stop pair
02895                                 $this->nCallback++;
02896                                 $info['nCallback']=$this->nCallback;
02897                                 $this->callback[$this->nCallback]=$info;
02898                             }
02899                             $ret = $this->$func( $info );
02900                             if ( is_array( $ret ) )
02901                             {
02902                                 // then the return from the callback function could set the position, to start with, later will do font colour, and font
02903                                 foreach ( $ret as $rk => $rv )
02904                                 {
02905                                     switch ( $rk )
02906                                     {
02907                                     case 'x':
02908                                     case 'y':
02909                                         $$rk=$rv;
02910                                         break;
02911                                     }
02912                                 }
02913                             }
02914                         }
02915                     }
02916                 }
02917                 break;
02918             }
02919         }
02920         return array( 'directive' => $directive,
02921                       'y' => (float)$y,
02922                       'noClose' => $noClose,
02923                       'run_final' => ( $f == 0 ) );
02924     }
02925 
02926     /**
02927      * add text to the document, at a specified location, size and angle on the page
02928      *
02929      * \return array( 'height' => <used height if more than normal text, -1 if not> )
02930      */
02931     function addText( $x, $y, $size, $text, $angle = 0, $wordSpaceAdjust = 0 )
02932     {
02933         $returnArray = array( 'height' => -1 );
02934 
02935         if (!$this->numFonts)
02936         {
02937             $this->selectFont('./fonts/Helvetica');
02938         }
02939 
02940         // if there are any open callbacks, then they should be called, to show the start of the line
02941         if ($this->nCallback>0)
02942         {
02943             for ( $i = $this->nCallback; $i > 0; $i-- )
02944             {
02945                 // call each function
02946                 $info = array( 'x' => $x, 'y' => $y,
02947                                'angle' => $angle,
02948                                'status' => 'sol',
02949                                'p' => $this->callback[$i]['p'],
02950                                'nCallback' => $this->callback[$i]['nCallback'],
02951                                'height' => $this->callback[$i]['height'],
02952                                'decender' => $this->callback[$i]['decender'] );
02953                 $func = $this->callback[$i]['f'];
02954                 $this->$func( $info );
02955             }
02956         }
02957         if ( $angle == 0 )
02958         {
02959             $this->objects[$this->currentContents]['c'] .= "\n".'BT '.sprintf('%.3F',$x).' '.sprintf('%.3F',$y).' Td';
02960         }
02961         else
02962         {
02963             $a = deg2rad((float)$angle);
02964             $tmp = "\n".'BT ';
02965             $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
02966             $tmp .= sprintf('%.3F',$x).' '.sprintf('%.3F',$y).' Tm';
02967             $this->objects[$this->currentContents]['c'] .= $tmp;
02968         }
02969         if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust)
02970         {
02971             $this->wordSpaceAdjust=$wordSpaceAdjust;
02972             $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3F',$wordSpaceAdjust).' Tw';
02973         }
02974         $len = strlen($text);
02975         $start = 0;
02976         for ( $i = 0; $i < $len; $i++ )
02977         {
02978             $f = 1;
02979             $directiveArray = $this->PRVTcheckTextDirective( $text, $i, $f, true );
02980             $directive = $directiveArray['directive'];
02981             if ($directive)
02982             {
02983                 // then we should write what we need to
02984                 if ( $i > $start )
02985                 {
02986                     $part = substr($text,$start,$i-$start);
02987                     $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1F',$size).' Tf ';
02988                     $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
02989                 }
02990                 if ( !$directiveArray['run_final'] )
02991                 {
02992                     // then there was nothing drastic done here, restore the contents
02993                     $this->setCurrentFont();
02994                 }
02995                 else
02996                 {
02997                     $this->objects[$this->currentContents]['c'] .= ' ET';
02998                     $f = 1;
02999                     $xp = $x;
03000                     $yp = $y;
03001                     $directiveArray = $this->PRVTcheckTextDirective1( $text, $i, $f, 1, $xp, $yp, $size, $angle, $wordSpaceAdjust );
03002                     if ( $directiveArray['y'] != 0 )
03003                     {
03004                         $returnArray['height'] = $y - $directiveArray['y'];
03005                     }
03006 
03007                     $directive = $directiveArray['directive'];
03008 
03009                     // restart the text object
03010                     if ( $angle == 0 )
03011                     {
03012                         $this->objects[$this->currentContents]['c'] .= "\n".'BT '.sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Td';
03013                     }
03014                     else
03015                     {
03016                         $a = deg2rad( (float)$angle );
03017                         $tmp = "\n".'BT ';
03018                         $tmp .= sprintf('%.3F',cos($a)).' '.sprintf('%.3F',(-1.0*sin($a))).' '.sprintf('%.3F',sin($a)).' '.sprintf('%.3F',cos($a)).' ';
03019                         $tmp .= sprintf('%.3F',$xp).' '.sprintf('%.3F',$yp).' Tm';
03020                         $this->objects[$this->currentContents]['c'] .= $tmp;
03021                     }
03022                     if ($wordSpaceAdjust!=0 || $wordSpaceAdjust != $this->wordSpaceAdjust)
03023                     {
03024                         $this->wordSpaceAdjust=$wordSpaceAdjust;
03025                         $this->objects[$this->currentContents]['c'] .= ' '.sprintf('%.3F',$wordSpaceAdjust).' Tw';
03026                     }
03027                 }
03028                 // and move the writing point to the next piece of text
03029                 $i = $i + $directive - 1;
03030                 $start = $i + 1;
03031             }
03032 
03033         }
03034         if ( $start < $len )
03035         {
03036             $part = substr($text,$start);
03037             $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1F',$size).' Tf ';
03038             $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
03039         }
03040         $this->objects[$this->currentContents]['c'].=' ET';
03041 
03042         // if there are any open callbacks, then they should be called, to show the end of the line
03043         if ( $this->nCallback > 0 )
03044         {
03045             for ( $i = $this->nCallback; $i > 0; $i-- )
03046             {
03047                 // call each function
03048                 $tmp = $this->PRVTgetTextPosition( $x, $y, $angle, $size, $wordSpaceAdjust, $text );
03049                 $info = array('x' => $tmp[0], 'y' => $tmp[1],
03050                               'angle' => $angle,
03051                               'status' => 'eol',
03052                               'p' => $this->callback[$i]['p'],
03053                               'nCallback' => $this->callback[$i]['nCallback'],
03054                               'height' => $this->callback[$i]['height'],
03055                               'decender' => $this->callback[$i]['decender'] );
03056                 $func = $this->callback[$i]['f'];
03057                 $this->$func( $info );
03058             }
03059         }
03060 
03061         return $returnArray;
03062     }
03063 
03064     /**
03065      * calculate how wide a given text string will be on a page, at a given size.
03066      * this can be called externally, but is alse used by the other class functions
03067      */
03068     function getTextWidth( $size, $text )
03069     {
03070         // this function should not change any of the settings, though it will need to
03071         // track any directives which change during calculation, so copy them at the start
03072         // and put them back at the end.
03073         $this->pushTextState( $this->currentTextState );
03074 
03075         if (!$this->numFonts)
03076         {
03077             $this->selectFont('./fonts/Helvetica');
03078         }
03079 
03080         // converts a number or a float to a string so it can get the width
03081         $text = "$text";
03082         // hmm, this is where it all starts to get tricky - use the font information to
03083         // calculate the width of each character, add them up and convert to user units
03084         $w = 0;
03085         $len = strlen( $text );
03086         $cf = $this->currentFont;
03087         for ( $i = 0; $i < $len; $i++ )
03088         {
03089             $f = 1;
03090             $directiveArray = $this->PRVTcheckTextDirective( $text, $i, $f );
03091             $directive = $directiveArray['directive'];
03092             if ( $directive )
03093             {
03094                 if ($f)
03095                 {
03096                     $this->setCurrentFont();
03097                     $cf = $this->currentFont;
03098                 }
03099                 $i = $i + $directive-1;
03100             }
03101             else
03102             {
03103                 $char = ord( $text[$i] );
03104                 if ( isset($this->fonts[$cf]['differences'][$char] ) )
03105                 {
03106                     // then this character is being replaced by another
03107                     $name = $this->fonts[$cf]['differences'][$char];
03108                     if ( isset($this->fonts[$cf]['C'][$name]['WX'] ) )
03109                     {
03110                         $w += $this->fonts[$cf]['C'][$name]['WX'];
03111                     }
03112                 }
03113                 else if ( isset($this->fonts[$cf]['C'][$char]['WX'] ) )
03114                 {
03115                     $w += $this->fonts[$cf]['C'][$char]['WX'];
03116                 }
03117                 else
03118                 {
03119                     $w += 700;
03120                 }
03121             }
03122         }
03123 
03124         $this->popTextState();
03125         $this->setCurrentFont();
03126 
03127         return $w*$size/1000;
03128     }
03129 
03130     /**
03131      * do a part of the calculation for sorting out the justification of the text
03132      *
03133      * @access private
03134      */
03135     function PRVTadjustWrapText( $text, $actual, $width, &$x, &$adjust, $justification )
03136     {
03137         switch ( $justification )
03138         {
03139         case 'left':
03140             return;
03141             break;
03142         case 'right':
03143             $x += $width - $actual;
03144             break;
03145         case 'center':
03146         case 'centre':
03147             $x += ($width - $actual) / 2;
03148             break;
03149         case 'full':
03150             // count the number of words
03151             $words = explode( ' ', $text );
03152             $nspaces = count($words) - 1;
03153             if ( $nspaces > 0 )
03154             {
03155                 $adjust = ($width-$actual) / $nspaces;
03156             }
03157             else
03158             {
03159                 $adjust = 0;
03160             }
03161         break;
03162     }
03163     }
03164 
03165     /**
03166      * add text to the page, but ensure that it fits within a certain width
03167      * if it does not fit then put in as much as possible, splitting at word boundaries
03168      * and return the remainder.
03169      * justification and angle can also be specified for the text
03170      *
03171      * return array ('text' => <text for new line>,
03172                      'width' => <width of added text, 0 i more text>,
03173                      'height' => <height of added text, -1 if none/default> )
03174      */
03175     function addTextWrap( $x, $y, $width, $size, $text, $justification = 'left', $angle = 0, $test = 0 )
03176     {
03177         // this will display the text, and if it goes beyond the width $width, will backtrack to the
03178         // previous space or hyphen, and return the remainder of the text.
03179 
03180         // $justification can be set to 'left','right','center','centre','full'
03181 
03182         // need to store the initial text state, as this will change during the width calculation
03183         // but will need to be re-set before printing, so that the chars work out right
03184         $store_currentTextState = $this->currentTextState;
03185         $returnArray = array ( 'text' => '',
03186                                'width' => 0,
03187                                'height' => 0 );
03188         if ( $text != '' )
03189         {
03190             $returnArray['height'] = $this->getFontHeight( $size );
03191         }
03192 
03193         if ( !$this->numFonts )
03194         {
03195             $this->selectFont('./fonts/Helvetica');
03196         }
03197         if ( $width <= 0 )
03198         {
03199             // error, pretend it printed ok, otherwise risking a loop
03200             return $returnArray;
03201         }
03202         $w = 0;
03203         $break = 0;
03204         $breakWidth = 0;
03205         $len = strlen( $text );
03206         $cf = $this->currentFont;
03207         $tw = $width/$size*1000;
03208         $directive = 0;
03209         for ( $i = 0; $i < $len; $i++ )
03210         {
03211             $f = 1;
03212             $directiveArray = $this->PRVTcheckTextDirective( $text, $i, $f );
03213             $deltaDirective = $directiveArray['directive'];
03214             $directive += $deltaDirective;
03215             if ( $directiveArray['y'] != 0 )
03216             {
03217                 $y -= $directiveArray['y'];
03218                 $returnArray['height'] = $directiveArray['y'];
03219             }
03220             if ($deltaDirective)
03221             {
03222                 if ($f)
03223                 {
03224                     $this->setCurrentFont();
03225                     $cf = $this->currentFont;
03226                 }
03227                 $i=$i+$deltaDirective-1;
03228             }
03229             else
03230             {
03231                 $cOrd = ord($text[$i]);
03232                 if ( isset( $this->fonts[$cf]['differences'][$cOrd] ) )
03233                 {
03234                     // then this character is being replaced by another
03235                     $cOrd2 = $this->fonts[$cf]['differences'][$cOrd];
03236                 }
03237                 else
03238                 {
03239                     $cOrd2 = $cOrd;
03240                 }
03241 
03242                 if ( isset( $this->fonts[$cf]['C'][$cOrd2]['WX'] ) )
03243                 {
03244                     $w += $this->fonts[$cf]['C'][$cOrd2]['WX'];
03245                 }
03246                 else
03247                 {
03248                     $w += 700;
03249                 }
03250 
03251                 if ( $w > $tw )
03252                 {
03253                     // then we need to truncate this line
03254                     if ( $break > 0 )
03255                     {
03256                         // then we have somewhere that we can split :)
03257                         if ($text[$break] == ' ')
03258                         {
03259                             $tmp = substr( $text, 0, $break );
03260                         }
03261                         else
03262                         {
03263                             $tmp = substr( $text, 0, $break + 1 );
03264                         }
03265                         $adjust = 0;
03266                         $this->PRVTadjustWrapText( $tmp, $breakWidth, $width, $x, $adjust, $justification );
03267 
03268                         // reset the text state
03269                         $this->currentTextState = $store_currentTextState;
03270                         $this->setCurrentFont();
03271                         if ( !$test )
03272                         {
03273                             $addTextArray = $this->addText( $x, $y, $size, $tmp, $angle, $adjust );
03274                             if ( $addTextArray['height'] != -1 &&
03275                                  $returnArray['height'] < $addTextArray['height'] )
03276                             {
03277                                 $returnArray['height'] = $addTextArray['height'];
03278                             }
03279                         }
03280                         $returnArray['text'] = substr( $text, $break + 1 );
03281                         return $returnArray;
03282                     }
03283                     else
03284                     {
03285                         // just split before the current character
03286                         $tmp = substr( $text, 0, $i );
03287                         $adjust = 0;
03288                         $ctmp = ord( $text[$i] );
03289                         if ( isset($this->fonts[$cf]['differences'][$ctmp] ) )
03290                         {
03291                             $ctmp = $this->fonts[$cf]['differences'][$ctmp];
03292                         }
03293                         $tmpw = ( $w - $this->fonts[$cf]['C'][$ctmp]['WX'] ) * $size/1000;
03294                         $this->PRVTadjustWrapText( $tmp, $tmpw, $width, $x, $adjust, $justification );
03295                         // reset the text state
03296                         $this->currentTextState = $store_currentTextState;
03297                         $this->setCurrentFont();
03298                         if ( !$test )
03299                         {
03300                             $addTextArray = $this->addText( $x, $y, $size, $tmp, $angle, $adjust );
03301                             if ( $addTextArray['height'] != -1 &&
03302                                  $returnArray['height'] < $addTextArray['height'] )
03303                             {
03304                                 $returnArray['height'] = $addTextArray['height'];
03305                             }
03306                         }
03307                         $returnArray['text'] = substr( $text, $i );
03308                         return $returnArray;
03309                     }
03310                 }
03311 
03312                 if ( $text[$i] == '-' )
03313                 {
03314                     $break = $i;
03315                     $breakWidth = $w*$size/1000;
03316                 }
03317                 if ( $text[$i] == ' ' )
03318                 {
03319                     $break = $i;
03320                     $ctmp = ord( $text[$i] );
03321                     if ( isset($this->fonts[$cf]['differences'][$ctmp] ) )
03322                     {
03323                         $ctmp = $this->fonts[$cf]['differences'][$ctmp];
03324                     }
03325                     $breakWidth = ( $w - $this->fonts[$cf]['C'][$ctmp]['WX'] ) * $size/1000;
03326                 }
03327             }
03328         }
03329         // then there was no need to break this line
03330         if ( $justification == 'full' )
03331         {
03332             $justification = 'left';
03333         }
03334         $adjust = 0;
03335         $tmpw = $w * $size/1000; //tmpw, text width
03336         $this->PRVTadjustWrapText( $text, $tmpw, $width, $x, $adjust, $justification );
03337         // reset the text state
03338         $this->currentTextState = $store_currentTextState;
03339         $this->setCurrentFont();
03340         if ( !$test )
03341         {
03342             $addTextArray = $this->addText( $x, $y, $size, $text, $angle, $adjust, $angle );
03343             if ( isset( $directive ) && $directive == $len )
03344             {
03345                 return array( 'only_directive' => true );
03346             }
03347 
03348             if ( $addTextArray['height'] != -1 &&
03349                  $returnArray['height'] < $addTextArray['height'] )
03350             {
03351                 $returnArray['height'] = $addTextArray['height'];
03352             }
03353         }
03354         $returnArray['width'] = $tmpw;
03355         return $returnArray;
03356     }
03357 
03358     /**
03359      * this will be called at a new page to return the state to what it was on the
03360      * end of the previous page, before the stack was closed down
03361      * This is to get around not being able to have open 'q' across pages
03362      *
03363      */
03364     function saveState( $pageEnd = 0 )
03365     {
03366         if ( $pageEnd )
03367         {
03368             // this will be called at a new page to return the state to what it was on the
03369             // end of the previous page, before the stack was closed down
03370             // This is to get around not being able to have open 'q' across pages
03371             $opt = $this->stateStack[$pageEnd]; // ok to use this as stack starts numbering at 1
03372             $this->setColor($opt['col'],1);
03373             $this->setStrokeColor($opt['str'],1);
03374             $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
03375             //$this->currentLineStyle = $opt['lin'];
03376         }
03377         else
03378         {
03379             $this->nStateStack++;
03380             $this->stateStack[$this->nStateStack]=array( 'col' => $this->currentColour,
03381                                                          'str' => $this->currentStrokeColour,
03382                                                          'lin' => $this->currentLineStyle );
03383         }
03384         $this->objects[$this->currentContents]['c'] .= "\nq";
03385     }
03386 
03387     /**
03388      * restore a previously saved state
03389      */
03390     function restoreState( $pageEnd = 0 )
03391     {
03392         if ( !$pageEnd )
03393         {
03394             $n = $this->nStateStack;
03395             $this->currentColour = $this->stateStack[$n]['col'];
03396             $this->currentStrokeColour = $this->stateStack[$n]['str'];
03397             $this->objects[$this->currentContents]['c'] .= "\n".$this->stateStack[$n]['lin'];
03398             $this->currentLineStyle = $this->stateStack[$n]['lin'];
03399             unset( $this->stateStack[$n] );
03400             $this->nStateStack--;
03401         }
03402         $this->objects[$this->currentContents]['c'] .= "\nQ";
03403     }
03404 
03405     /**
03406      * make a loose object, the output will go into this object, until it is closed, then will revert to
03407      * the current one.
03408      * this object will not appear until it is included within a page.
03409      * the function will return the object number
03410      */
03411     function openObject()
03412     {
03413         $this->nStack++;
03414         $this->stack[$this->nStack] = array( 'c' => $this->currentContents,
03415                                              'p' => $this->currentPage );
03416         // add a new object of the content type, to hold the data flow
03417         $this->numObj++;
03418         $this->o_contents( $this->numObj, 'new' );
03419         $this->currentContents = $this->numObj;
03420         $this->looseObjects[$this->numObj] = 1;
03421 
03422         return $this->numObj;
03423     }
03424 
03425     /**
03426      * open an existing object for editing
03427      */
03428     function reopenObject( $id )
03429     {
03430         $this->nStack++;
03431         $this->stack[$this->nStack] = array( 'c' => $this->currentContents,
03432                                              'p' => $this->currentPage );
03433         $this->currentContents=$id;
03434         // also if this object is the primary contents for a page, then set the current page to its parent
03435         if ( isset( $this->objects[$id]['onPage'] ) )
03436         {
03437             $this->currentPage = $this->objects[$id]['onPage'];
03438         }
03439     }
03440 
03441     /**
03442      * close an object
03443      */
03444     function closeObject()
03445     {
03446         // close the object, as long as there was one open in the first place, which will be indicated by
03447         // an objectId on the stack.
03448         if ( $this->nStack > 0 )
03449         {
03450             $this->currentContents=$this->stack[$this->nStack]['c'];
03451             $this->currentPage=$this->stack[$this->nStack]['p'];
03452             $this->nStack--;
03453             // easier to probably not worry about removing the old entries, they will be overwritten
03454             // if there are new ones.
03455         }
03456     }
03457 
03458     /**
03459      * stop an object from appearing on pages from this point on
03460      */
03461     function stopObject( $id )
03462     {
03463         // if an object has been appearing on pages up to now, then stop it, this page will
03464         // be the last one that could contian it.
03465         if ( isset( $this->addLooseObjects[$id] ) )
03466         {
03467             $this->addLooseObjects[$id] = '';
03468         }
03469     }
03470 
03471     /**
03472      * after an object has been created, it wil only show if it has been added, using this function.
03473      */
03474     function addObject( $id, $options = 'add' )
03475     {
03476         // add the specified object to the page
03477         if ( isset( $this->looseObjects[$id] ) && $this->currentContents != $id )
03478         {
03479             // then it is a valid object, and it is not being added to itself
03480             switch ( $options )
03481             {
03482             case 'all':
03483                 // then this object is to be added to this page (done in the next block) and
03484                 // all future new pages.
03485                 $this->addLooseObjects[$id] = 'all';
03486             case 'add':
03487                 if ( isset( $this->objects[$this->currentContents]['onPage'] ) )
03488                 {
03489                     // then the destination contents is the primary for the page
03490                     // (though this object is actually added to that page)
03491                     $this->o_page( $this->objects[$this->currentContents]['onPage'], 'content', $id );
03492                 }
03493                 break;
03494             case 'even':
03495                 $this->addLooseObjects[$id] = 'even';
03496                 $pageObjectId = $this->objects[$this->currentContents]['onPage'];
03497                 if ( $this->objects[$pageObjectId]['info']['pageNum'] % 2 == 0 )
03498                 {
03499                     $this->addObject( $id ); // hacky huh :)
03500                 }
03501                 break;
03502             case 'odd':
03503                 $this->addLooseObjects[$id] = 'odd';
03504                 $pageObjectId = $this->objects[$this->currentContents]['onPage'];
03505                 if ( $this->objects[$pageObjectId]['info']['pageNum'] % 2 == 1 )
03506                 {
03507                     $this->addObject( $id ); // hacky huh :)
03508                 }
03509                 break;
03510             case 'next':
03511                 $this->addLooseObjects[$id] = 'all';
03512                 break;
03513             case 'nexteven':
03514                 $this->addLooseObjects[$id] = 'even';
03515                 break;
03516             case 'nextodd':
03517                 $this->addLooseObjects[$id] = 'odd';
03518                 break;
03519             }
03520         }
03521     }
03522 
03523     /**
03524      * add content to the documents info object
03525      */
03526     function addInfo( $label, $value = 0 )
03527     {
03528         // this will only work if the label is one of the valid ones.
03529         // modify this so that arrays can be passed as well.
03530         // if $label is an array then assume that it is key=>value pairs
03531         // else assume that they are both scalar, anything else will probably error
03532         if ( is_array( $label ) )
03533         {
03534             foreach ( $label as $l => $v )
03535             {
03536                 $this->o_info( $this->infoObject, $l, $v );
03537             }
03538         }
03539         else
03540         {
03541             $this->o_info( $this->infoObject, $label, $value );
03542         }
03543     }
03544 
03545     /**
03546      * set the viewer preferences of the document, it is up to the browser to obey these.
03547      */
03548     function setPreferences( $label, $value = 0 )
03549     {
03550         // this will only work if the label is one of the valid ones.
03551         if ( is_array( $label ) )
03552         {
03553             foreach ( $label as $l => $v )
03554             {
03555                 $this->o_catalog( $this->catalogId, 'viewerPreferences', array( $l => $v ) );
03556             }
03557         }
03558         else
03559         {
03560             $this->o_catalog( $this->catalogId, 'viewerPreferences', array( $label => $value ) );
03561         }
03562     }
03563 
03564     /**
03565      * extract an integer from a position in a byte stream
03566      *
03567      * @access private
03568      */
03569     function PRVT_getBytes( &$data, $pos, $num )
03570     {
03571         // return the integer represented by $num bytes from $pos within $data
03572         $ret = 0;
03573         for ( $i = 0; $i < $num; $i++ )
03574         {
03575             $ret = $ret * 256;
03576             $ret+= ord( $data[$pos+$i] );
03577         }
03578         return $ret;
03579     }
03580 
03581     /**
03582      * add a PNG image into the document, from a file
03583      * this should work with remote files
03584      *
03585      * \return true if adding image succeded
03586      */
03587     function addPngFromFile( $file, $x, $y, $w = 0, $h = 0 )
03588     {
03589         // read in a png file, interpret it, then add to the system
03590         $error = 0;
03591         $tmp = get_magic_quotes_runtime();
03592         set_magic_quotes_runtime(0);
03593         $fp = @fopen( $file, 'rb' );
03594         if ( $fp )
03595         {
03596             $data = '';
03597             while ( !feof( $fp ) )
03598             {
03599                 $data .= fread( $fp, 1024 );
03600             }
03601             fclose( $fp );
03602         }
03603         else
03604         {
03605             $error = 1;
03606             $errormsg = 'trouble opening file: '.$file;
03607         }
03608         set_magic_quotes_runtime( $tmp );
03609 
03610         if ( !$error )
03611         {
03612             $header = chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
03613             if ( substr( $data, 0, 8 ) != $header )
03614             {
03615                 $error = 1;
03616                 $errormsg = 'this file does not have a valid header';
03617             }
03618         }
03619 
03620         if ( !$error )
03621         {
03622             // set pointer
03623             $p = 8;
03624             $len = strlen($data);
03625             // cycle through the file, identifying chunks
03626             $haveHeader = 0;
03627             $info = array();
03628             $idata = '';
03629             $pdata = '';
03630             while ( $p < $len )
03631             {
03632                 $chunkLen = $this->PRVT_getBytes($data,$p,4);
03633                 $chunkType = substr($data,$p+4,4);
03634                 //echo $chunkType.' - '.$chunkLen.'<br>';
03635 
03636                 switch ( $chunkType )
03637                 {
03638                 case 'IHDR':
03639                     // this is where all the file information comes from
03640                     $info['width'] = $this->PRVT_getBytes( $data, $p+8, 4 );
03641                     $info['height'] = $this->PRVT_getBytes( $data, $p+12, 4);
03642                     $info['bitDepth'] = ord( $data[$p+16] );
03643                     $info['colorType'] = ord( $data[$p+17] );
03644                     $info['compressionMethod'] = ord( $data[$p+18] );
03645                     $info['filterMethod'] = ord( $data[$p+19] );
03646                     $info['interlaceMethod'] = ord( $data[$p+20] );
03647                     //print_r($info);
03648                     $haveHeader = 1;
03649                     if ( $info['compressionMethod'] != 0 )
03650                     {
03651                         $error = 1;
03652                         $errormsg = 'unsupported compression method';
03653                     }
03654                     if ( $info['filterMethod'] != 0 )
03655                     {
03656                         $error = 1;
03657                         $errormsg = 'unsupported filter method';
03658                     }
03659                     break;
03660 
03661                 case 'PLTE':
03662                     $pdata .= substr( $data, $p + 8, $chunkLen );
03663                     break;
03664                 case 'IDAT':
03665                     $idata .= substr( $data, $p + 8, $chunkLen );
03666                     break;
03667                 case 'tRNS':
03668                     //this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk
03669                     //print "tRNS found, color type = ".$info['colorType']."<BR>";
03670                     $transparency = array();
03671                     if ( $info['colorType'] == 3 )
03672                     {
03673                         // indexed color, rbg
03674                         /* corresponding to entries in the plte chunk
03675                         Alpha for palette index 0: 1 byte
03676                         Alpha for palette index 1: 1 byte
03677                         ...etc...
03678                         */
03679                         // there will be one entry for each palette entry. up until the last non-opaque entry.
03680                         // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
03681                         $transparency['type'] = 'indexed';
03682                         $numPalette = strlen($pdata)/3;
03683                         $trans = 0;
03684                         for ( $i = $chunkLen; $i >= 0; $i-- )
03685                         {
03686                             if ( ord( $data[$p+8+$i] ) == 0 )
03687                             {
03688                                 $trans = $i;
03689                             }
03690                         }
03691                         $transparency['data'] = $trans;
03692 
03693                     }
03694                     elseif ( $info['colorType'] == 0 )
03695                     {
03696                         // grayscale
03697                         /* corresponding to entries in the plte chunk
03698                          Gray: 2 bytes, range 0 .. (2^bitdepth)-1
03699                         */
03700                         //$transparency['grayscale']=$this->PRVT_getBytes($data,$p+8,2); // g = grayscale
03701                         $transparency['type'] = 'indexed';
03702                         $transparency['data'] = ord( $data[$p+8+1] );
03703 
03704                     }
03705                     elseif ( $info['colorType'] == 2)
03706                     {
03707                         // truecolor
03708                         /* corresponding to entries in the plte chunk
03709                         Red: 2 bytes, range 0 .. (2^bitdepth)-1
03710                         Green: 2 bytes, range 0 .. (2^bitdepth)-1
03711                         Blue: 2 bytes, range 0 .. (2^bitdepth)-1
03712                         */
03713                         $transparency['r'] = $this->PRVT_getBytes( $data, $p+8, 2 ); // r from truecolor
03714                         $transparency['g'] = $this->PRVT_getBytes( $data, $p+10 ,2 ); // g from truecolor
03715                         $transparency['b'] = $this->PRVT_getBytes( $data, $p+12 ,2 ); // b from truecolor
03716                     }
03717                     else
03718                     {
03719                         //unsupported transparency type
03720                     }
03721                     // KS End new code
03722                     break;
03723                 default:
03724                     break;
03725                 }
03726 
03727                 $p += $chunkLen+12;
03728             }
03729 
03730             if ( !$haveHeader )
03731             {
03732                 $error = 1;
03733                 $errormsg = 'information header is missing';
03734             }
03735             if ( isset( $info['interlaceMethod']) && $info['interlaceMethod'] )
03736             {
03737                 $error = 1;
03738                 $errormsg = 'There appears to be no support for interlaced images in pdf.';
03739             }
03740         }
03741 
03742         if ( !$error && $info['bitDepth'] > 8 )
03743         {
03744             $error = 1;
03745             $errormsg = 'only bit depth of 8 or less is supported';
03746         }
03747 
03748         if ( !$error )
03749         {
03750             if ( $info['colorType'] != 2 && $info['colorType'] != 0 && $info['colorType'] != 3 )
03751             {
03752                 $error = 1;
03753                 $errormsg = 'transparancey alpha channel not supported, transparency only supported for palette images.';
03754             }
03755             else
03756             {
03757                 switch ( $info['colorType'] )
03758                 {
03759                 case 3:
03760                     $color = 'DeviceRGB';
03761                     $ncolor = 1;
03762                     break;
03763                 case 2:
03764                     $color = 'DeviceRGB';
03765                     $ncolor = 3;
03766                     break;
03767                 case 0:
03768                     $color = 'DeviceGray';
03769                     $ncolor = 1;
03770                     break;
03771                 }
03772             }
03773         }
03774         if ( $error )
03775         {
03776             eZDebug::writeError( 'Adding PNG file, '. $file .', to PDF failed: '.$errormsg, 'Cpdf::addPngFromFile' );
03777             $this->addMessage('PNG error - ('.$file.') '.$errormsg);
03778             return false;
03779         }
03780         if ( $w == 0 )
03781         {
03782             $w = $h/$info['height'] * $info['width'];
03783         }
03784         if ( $h == 0 )
03785         {
03786             $h = $w * $info['height'] / $info['width'];
03787         }
03788         //print_r($info);
03789         // so this image is ok... add it in.
03790         $this->numImages++;
03791         $im = $this->numImages;
03792         $label = 'I'.$im;
03793         $this->numObj++;
03794     //  $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$idata,'iw'=>$w,'ih'=>$h,'type'=>'png','ic'=>$info['width']));
03795         $options = array( 'label' => $label,
03796                           'data' => $idata,
03797                           'bitsPerComponent' => $info['bitDepth'],
03798                           'pdata' => $pdata,
03799                           'iw' => $info['width'],
03800                           'ih' => $info['height'],
03801                           'type' => 'png',
03802                           'color' => $color,
03803                           'ncolor' => $ncolor );
03804 
03805         if ( isset( $transparency ) )
03806         {
03807             $options['transparency'] = $transparency;
03808         }
03809         $this->o_image( $this->numObj, 'new', $options );
03810 
03811         $this->objects[$this->currentContents]['c'].="\nq";
03812         $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3F',$w)." 0 0 ".sprintf('%.3F',$h)." ".sprintf('%.3F',$x)." ".sprintf('%.3F',$y)." cm";
03813         $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
03814         $this->objects[$this->currentContents]['c'].="\nQ";
03815     }
03816 
03817     /**
03818      * add a JPEG image into the document, from a file
03819      */
03820     function addJpegFromFile( $img, $x, $y, $w = 0, $h = 0 )
03821     {
03822         // attempt to add a jpeg image straight from a file, using no GD commands
03823         // note that this function is unable to operate on a remote file.
03824 
03825         if ( !file_exists( $img ) )
03826         {
03827             //echo "FILE NOT EXISTS: $img";
03828             return;
03829         }
03830 
03831         $tmp = getimagesize( $img );
03832         $imageWidth = $tmp[0];
03833         $imageHeight = $tmp[1];
03834 
03835         if ( isset( $tmp['channels'] ) )
03836         {
03837             $channels = $tmp['channels'];
03838         }
03839         else
03840         {
03841             $channels = 3;
03842         }
03843 
03844         if ( $w <= 0 && $h <= 0 )
03845         {
03846             $w = $imageWidth;
03847         }
03848         if ( $w == 0 )
03849         {
03850             $w = $h / $imageHeight * $imageWidth;
03851         }
03852         if ( $h == 0 )
03853         {
03854             $h = $w * $imageHeight / $imageWidth;
03855         }
03856 
03857         $fp = fopen( $img, 'rb' );
03858 
03859         $tmp = get_magic_quotes_runtime();
03860         set_magic_quotes_runtime( 0 );
03861         $data = fread( $fp, filesize( $img ) );
03862         set_magic_quotes_runtime( $tmp );
03863 
03864         fclose( $fp );
03865 
03866         $this->addJpegImage_common( $data, $x, $y, $w, $h, $imageWidth, $imageHeight, $channels );
03867     }
03868 
03869     /**
03870      * add an image into the document, from a GD object
03871      * this function is not all that reliable, and I would probably encourage people to use
03872      * the file based functions
03873      */
03874     function addImage( &$img, $x, $y, $w = 0, $h = 0, $quality = 75 )
03875     {
03876         // add a new image into the current location, as an external object
03877         // add the image at $x,$y, and with width and height as defined by $w & $h
03878 
03879         // note that this will only work with full colour images and makes them jpg images for display
03880         // later versions could present lossless image formats if there is interest.
03881 
03882         // there seems to be some problem here in that images that have quality set above 75 do not appear
03883         // not too sure why this is, but in the meantime I have restricted this to 75.
03884         if ( $quality > 75 )
03885         {
03886             $quality = 75;
03887         }
03888 
03889         // if the width or height are set to zero, then set the other one based on keeping the image
03890         // height/width ratio the same, if they are both zero, then give up :)
03891         $imageWidth = imagesx( $img );
03892         $imageHeight = imagesy( $img );
03893 
03894         if ( $w <= 0 && $h <= 0 )
03895         {
03896             return;
03897         }
03898         if ( $w == 0 )
03899         {
03900             $w = $h / $imageHeight * $imageWidth;
03901         }
03902         if ( $h == 0 )
03903         {
03904             $h = $w * $imageHeight / $imageWidth;
03905         }
03906 
03907         // gotta get the data out of the img..
03908 
03909         // so I write to a temp file, and then read it back.. soo ugly, my apologies.
03910         $tmpDir = '/tmp';
03911         $tmpName = tempnam( $tmpDir, 'img' );
03912         imagejpeg( $img, $tmpName, $quality );
03913         $fp = fopen( $tmpName, 'rb' );
03914 
03915         $tmp = get_magic_quotes_runtime();
03916         set_magic_quotes_runtime( 0 );
03917         $fp = @fopen( $tmpName, 'rb' );
03918         if ( $fp )
03919         {
03920             $data = '';
03921             while ( !feof( $fp ) )
03922             {
03923                 $data .= fread( $fp, 1024 );
03924             }
03925             fclose( $fp );
03926         }
03927         else
03928         {
03929             $error = 1;
03930             $errormsg = 'trouble opening file';
03931         }
03932     //  $data = fread($fp,filesize($tmpName));
03933         set_magic_quotes_runtime( $tmp );
03934     //  fclose( $fp );
03935         unlink( $tmpName );
03936         $this->addJpegImage_common( $data, $x, $y, $w, $h, $imageWidth, $imageHeight );
03937     }
03938 
03939     /**
03940      * common code used by the two JPEG adding functions
03941      *
03942      * @access private
03943      */
03944     function addJpegImage_common( &$data, $x, $y, $w = 0, $h = 0, $imageWidth, $imageHeight, $channels = 3 )
03945     {
03946         // note that this function is not to be called externally
03947         // it is just the common code between the GD and the file options
03948         $this->numImages++;
03949         $im = $this->numImages;
03950         $label = 'I'.$im;
03951         $this->numObj++;
03952         $this->o_image( $this->numObj, 'new', array( 'label' => $label,
03953                                                      'data' => $data,
03954                                                      'iw' => $imageWidth,
03955                                                      'ih' => $imageHeight,
03956                                                      'channels' => $channels ) );
03957 
03958         $this->objects[$this->currentContents]['c'] .= "\nq";
03959         $this->objects[$this->currentContents]['c'] .= "\n".sprintf('%.3F',$w)." 0 0 ".sprintf('%.3F',$h)." ".sprintf('%.3F',$x)." ".sprintf('%.3F',$y)." cm";
03960         $this->objects[$this->currentContents]['c'] .= "\n/".$label.' Do';
03961         $this->objects[$this->currentContents]['c'] .= "\nQ";
03962     }
03963 
03964     /**
03965      * specify where the document should open when it first starts
03966      */
03967     function openHere( $style, $a = 0, $b = 0, $c = 0 )
03968     {
03969         // this function will open the document at a specified page, in a specified style
03970         // the values for style, and the required paramters are:
03971         // 'XYZ'  left, top, zoom
03972         // 'Fit'
03973         // 'FitH' top
03974         // 'FitV' left
03975         // 'FitR' left,bottom,right
03976         // 'FitB'
03977         // 'FitBH' top
03978         // 'FitBV' left
03979         $this->numObj++;
03980         $this->o_destination( $this->numObj, 'new', array( 'page' => $this->currentPage,
03981                                                            'type' => $style,
03982                                                            'p1' => $a, 'p2' => $b, 'p3' => $c ) );
03983         $id = $this->catalogId;
03984         $this->o_catalog( $id, 'openHere', $this->numObj );
03985     }
03986 
03987     /**
03988      * create a labelled destination within the document
03989      */
03990     function addDestination($label,$style,$a=0,$b=0,$c=0)
03991     {
03992         // associates the given label with the destination, it is done this way so that a destination can be specified after
03993         // it has been linked to
03994         // styles are the same as the 'openHere' function
03995         $this->numObj++;
03996         $this->o_destination( $this->numObj, 'new', array( 'page' => $this->currentPage,
03997                                                            'type' => $style,
03998                                                            'p1' => $a, 'p2' => $b, 'p3' => $c ) );
03999         $id = $this->numObj;
04000         // store the label->idf relationship, note that this means that labels can be used only once
04001         $this->destinations["$label"] = $id;
04002     }
04003 
04004     /**
04005      * define font families, this is used to initialize the font families for the default fonts
04006      * and for the user to add new ones for their fonts. The default bahavious can be overridden should
04007      * that be desired.
04008      */
04009     function setFontFamily($family,$options='')
04010     {
04011         if (!is_array($options))
04012         {
04013             if ($family=='init')
04014             {
04015                 // set the known family groups
04016                 // these font families will be used to enable bold and italic markers to be included
04017                 // within text streams. html forms will be used... <b></b> <i></i>
04018                 $this->fontFamilies['Helvetica'] = array(
04019                     'b'=>'Helvetica-Bold'
04020                     ,'i'=>'Helvetica-Oblique'
04021                     ,'bi'=>'Helvetica-BoldOblique'
04022                     ,'ib'=>'Helvetica-BoldOblique'
04023                     );
04024                 $this->fontFamilies['Courier'] = array(
04025                     'b'=>'Courier-Bold'
04026                     ,'i'=>'Courier-Oblique'
04027                     ,'bi'=>'Courier-BoldOblique'
04028                     ,'ib'=>'Courier-BoldOblique'
04029                     );
04030                 $this->fontFamilies['Times-Roman'] = array(
04031                     'b'=>'Times-Bold'
04032                     ,'i'=>'Times-Italic'
04033                     ,'bi'=>'Times-BoldItalic'
04034                     ,'ib'=>'Times-BoldItalic'
04035                     );
04036             }
04037         }
04038         else
04039         {
04040             // the user is trying to set a font family
04041             // note that this can also be used to set the base ones to something else
04042             if (strlen($family))
04043             {
04044                 $this->fontFamilies[$family] = $options;
04045             }
04046         }
04047     }
04048 
04049     /**
04050      * used to add messages for use in debugging
04051      */
04052     function addMessage( $message )
04053     {
04054         $this->messages .= $message."\n";
04055     }
04056 
04057     /**
04058      * a few functions which should allow the document to be treated transactionally.
04059      */
04060     function transaction( $action )
04061     {
04062         switch ( $action )
04063         {
04064         case 'start':
04065             // store all the data away into the checkpoint variable
04066             $data = get_object_vars( $this );
04067             $this->checkpoint = $data;
04068             unset( $data );
04069             break;
04070         case 'commit':
04071             if ( is_array( $this->checkpoint ) && isset( $this->checkpoint['checkpoint'] ) )
04072             {
04073                 $tmp = $this->checkpoint['checkpoint'];
04074                 $this->checkpoint = $tmp;
04075                 unset( $tmp );
04076             }
04077             else
04078             {
04079                 $this->checkpoint = '';
04080             }
04081             break;
04082         case 'rewind':
04083             // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
04084             if ( is_array( $this->checkpoint ) )
04085             {
04086                 // can only abort if were inside a checkpoint
04087                 $tmp = $this->checkpoint;
04088                 foreach ( $tmp as $k => $v )
04089                 {
04090                     if ( $k != 'checkpoint' )
04091                     {
04092                         $this->$k = $v;
04093                     }
04094                 }
04095                 unset($tmp);
04096             }
04097             break;
04098         case 'abort':
04099             if ( is_array( $this->checkpoint ) )
04100             {
04101                 // can only abort if were inside a checkpoint
04102                 $tmp = $this->checkpoint;
04103                 foreach ( $tmp as $k => $v )
04104                 {
04105                     $this->$k = $v;
04106                 }
04107                 unset($tmp);
04108             }
04109             break;
04110         }
04111 
04112     }
04113 
04114     /*!
04115      \static
04116 
04117      \param new cmyk array 1
04118      \param new cmyk array 2
04119 
04120      \return true if equal, false if not
04121     */
04122     function compareCMYK( $cmykArray1, $cmykArray2 )
04123     {
04124         return ( $cmykArray1['c'] == $cmykArray2['c'] &&
04125                  $cmykArray1['m'] == $cmykArray2['m'] &&
04126                  $cmykArray1['y'] == $cmykArray2['y'] &&
04127                  $cmykArray1['k'] == $cmykArray2['k'] );
04128     }
04129 
04130     /*!
04131      Push current text state, and set specified to current
04132 
04133      \param new text state
04134     */
04135     function pushTextState( $newState )
04136     {
04137         $this->textStateStack[] = $this->currentTextState;
04138         $this->currentTextState = $newState;
04139         $this->setCurrentFont();
04140     }
04141 
04142     /*!
04143      Pop text stack, and set to previous state.
04144     */
04145     function popTextState( )
04146     {
04147         $this->currentTextState = array_pop( $this->textStateStack );
04148         $this->setCurrentFont();
04149     }
04150 
04151     public $textStateStack = array( '' );
04152 } // end of class
04153 
04154 ?>