phpDocumentor Cpdf
[ class tree: Cpdf ] [ index: Cpdf ] [ all elements ]

Source for file class.pdf.php

Documentation is available at class.pdf.php

  1. <?php
  2. /**
  3. * Cpdf
  4. *
  5. *
  6. * A PHP class to provide the basic functionality to create a pdf document without
  7. * any requirement for additional modules.
  8. *
  9. * Note that they companion class CezPdf can be used to extend this class and dramatically
  10. * simplify the creation of documents.
  11. *
  12. * IMPORTANT NOTE
  13. * there is no warranty, implied or otherwise with this software.
  14. * LICENCE
  15. * This code has been placed in the Public Domain for all to enjoy.
  16. *
  17. @author        Wayne Munro <[email protected]>
  18. @version     009
  19. @package Cpdf
  20. @link http://www.ros.co.nz/pdf
  21. */
  22. class Cpdf {
  23.  
  24. /**
  25. * the current number of pdf objects in the document
  26. */
  27. var $numObj=0;
  28. /**
  29. * this array contains all of the pdf objects, ready for final assembly
  30. */
  31. var $objects = array();
  32. /**
  33. * the objectId (number within the objects array) of the document catalog
  34. */
  35. /**
  36. * array carrying information about the fonts that the system currently knows about
  37. * used to ensure that a font is not loaded twice, among other things
  38. */
  39. var $fonts=array()
  40. /**
  41. * a record of the current font
  42. */
  43. var $currentFont='';
  44. /**
  45. * the current base font
  46. */
  47. /**
  48. * the number of the current font within the font array
  49. */
  50. /**
  51. */
  52. /**
  53. * object number of the current page
  54. */
  55. /**
  56. * object number of the currently active contents block
  57. */
  58. /**
  59. * number of fonts within the system
  60. */
  61. var $numFonts=0;
  62. /**
  63. * current colour for fill operations, defaults to inactive value, all three components should be between 0 and 1 inclusive when active
  64. */
  65. var $currentColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  66. /**
  67. * current colour for stroke operations (lines etc.)
  68. */
  69. var $currentStrokeColour=array('r'=>-1,'g'=>-1,'b'=>-1);
  70. /**
  71. * current style that lines are drawn in
  72. */
  73. /**
  74. * an array which is used to save the state of the document, mainly the colours and styles
  75. * it is used to temporarily change to another state, the change back to what it was before
  76. */
  77. var $stateStack = array();
  78. /**
  79. * number of elements within the state stack
  80. */
  81. var $nStateStack = 0;
  82. /**
  83. * number of page objects within the document
  84. */
  85. var $numPages=0;
  86. /**
  87. * object Id storage stack
  88. */
  89. var $stack=array();
  90. /**
  91. * number of elements within the object Id storage stack
  92. */
  93. var $nStack=0;
  94. /**
  95. * an array which contains information about the objects which are not firmly attached to pages
  96. * these have been added with the addObject function
  97. */
  98. var $looseObjects=array();
  99. /**
  100. * array contains infomation about how the loose objects are to be added to the document
  101. */
  102. var $addLooseObjects=array();
  103. /**
  104. * the objectId of the information object for the document
  105. * this contains authorship, title etc.
  106. */
  107. var $infoObject=0;
  108. /**
  109. * number of images being tracked within the document
  110. */
  111. var $numImages=0;
  112. /**
  113. * an array containing options about the document
  114. * it defaults to turning on the compression of the objects
  115. */
  116. var $options=array('compression'=>1);
  117. /**
  118. * the objectId of the first page of the document
  119. */
  120. /**
  121. * used to track the last used value of the inter-word spacing, this is so that it is known
  122. * when the spacing is changed.
  123. */
  124. /**
  125. * the object Id of the procset object
  126. */
  127. /**
  128. * store the information about the relationship between font families
  129. * this used so that the code knows which font is the bold version of another font, etc.
  130. * the value of this array is initialised in the constuctor function.
  131. */
  132. var $fontFamilies = array();
  133. /**
  134. * track if the current font is bolded or italicised
  135. */
  136. var $currentTextState = ''
  137. /**
  138. * messages are stored here during processing, these can be selected afterwards to give some useful debug information
  139. */
  140. var $messages='';
  141. /**
  142. * the ancryption array for the document encryption is stored here
  143. */
  144. var $arc4='';
  145. /**
  146. * the object Id of the encryption information
  147. */
  148. var $arc4_objnum=0;
  149. /**
  150. * the file identifier, used to uniquely identify a pdf document
  151. */
  152. /**
  153. * a flag to say if a document is to be encrypted or not
  154. */
  155. var $encrypted=0;
  156. /**
  157. * the ancryption key for the encryption of all the document content (structure is not encrypted)
  158. */
  159. /**
  160. * array which forms a stack to keep track of nested callback functions
  161. */
  162. var $callback = array();
  163. /**
  164. * the number of callback functions in the callback array
  165. */
  166. var $nCallback = 0;
  167. /**
  168. * store label->id pairs for named destinations, these will be used to replace internal links
  169. * done this way so that destinations can be defined after the location that links to them
  170. */
  171. var $destinations = array();
  172. /**
  173. * store the stack for the transaction commands, each item in here is a record of the values of all the
  174. * variables within the class, so that the user can rollback at will (from each 'start' command)
  175. * note that this includes the objects array, so these can be large.
  176. */
  177. var $checkpoint = '';
  178. /**
  179. * class constructor
  180. * this will start a new document
  181. @var array array of 4 numbers, defining the bottom left and upper right corner of the page. first two are normally zero.
  182. */
  183. function Cpdf ($pageSize=array(0,0,612,792)){
  184.   $this->newDocument($pageSize);
  185.   
  186.   // also initialize the font families that are known about already
  187.   $this->setFontFamily('init');
  188. //  $this->fileIdentifier = md5('xxxxxxxx'.time());
  189.  
  190. }
  191.  
  192. /**
  193. * Document object methods (internal use only)
  194. *
  195. * There is about one object method for each type of object in the pdf document
  196. * Each function has the same call list ($id,$action,$options).
  197. * $id = the object ID of the object, or what it is to be if it is being created
  198. * $action = a string specifying the action to be performed, though ALL must support:
  199. *           'new' - create the object with the id $id
  200. *           'out' - produce the output for the pdf object
  201. * $options = optional, a string or array containing the various parameters for the object
  202. *
  203. * These, in conjunction with the output function are the ONLY way for output to be produced
  204. * within the pdf 'file'.
  205. */
  206.  
  207. /**
  208. *destination object, used to specify the location for the user to jump to, presently on opening
  209. */
  210. function o_destination($id,$action,$options=''){
  211.   if ($action!='new'){
  212.     $o =$this->objects[$id];
  213.   }
  214.   switch($action){
  215.     case 'new':
  216.       $this->objects[$id]=array('t'=>'destination','info'=>array());
  217.       $tmp '';
  218.       switch ($options['type']){
  219.         case 'XYZ':
  220.         case 'FitR':
  221.           $tmp =  ' '.$options['p3'].$tmp;
  222.         case 'FitH':
  223.         case 'FitV':
  224.         case 'FitBH':
  225.         case 'FitBV':
  226.           $tmp =  ' '.$options['p1'].' '.$options['p2'].$tmp;
  227.         case 'Fit':
  228.         case 'FitB':
  229.           $tmp =  $options['type'].$tmp;
  230.           $this->objects[$id]['info']['string']=$tmp;
  231.           $this->objects[$id]['info']['page']=$options['page'];
  232.       }
  233.       break;
  234.     case 'out':
  235.       $tmp $o['info'];
  236.       $res="\n".$id." 0 obj\n".'['.$tmp['page'].' 0 R /'.$tmp['string']."]\nendobj\n";
  237.       return $res;
  238.       break;
  239.   }
  240. }
  241.  
  242. /**
  243. * set the viewer preferences
  244. */
  245. function o_viewerPreferences($id,$action,$options=''){
  246.   if ($action!='new'){
  247.     $o =$this->objects[$id];
  248.   }
  249.   switch ($action){
  250.     case 'new':
  251.       $this->objects[$id]=array('t'=>'viewerPreferences','info'=>array());
  252.       break;
  253.     case 'add':
  254.       foreach($options as $k=>$v){
  255.         switch ($k){
  256.           case 'HideToolbar':
  257.           case 'HideMenubar':
  258.           case 'HideWindowUI':
  259.           case 'FitWindow':
  260.           case 'CenterWindow':
  261.           case 'NonFullScreenPageMode':
  262.           case 'Direction':
  263.             $o['info'][$k]=$v;
  264.           break;
  265.         }
  266.       }
  267.       break;
  268.     case 'out':
  269.  
  270.       $res="\n".$id." 0 obj\n".'<< ';
  271.       foreach($o['info'as $k=>$v){
  272.         $res.="\n/".$k.' '.$v;
  273.       }
  274.       $res.="\n>>\n";
  275.       return $res;
  276.       break;
  277.   }
  278. }
  279.  
  280. /**
  281. * define the document catalog, the overall controller for the document
  282. */
  283. function o_catalog($id,$action,$options=''){
  284.   if ($action!='new'){
  285.     $o =$this->objects[$id];
  286.   }
  287.   switch ($action){
  288.     case 'new':
  289.       $this->objects[$id]=array('t'=>'catalog','info'=>array());
  290.       $this->catalogId=$id;
  291.       break;
  292.     case 'outlines':
  293.     case 'pages':
  294.     case 'openHere':
  295.       $o['info'][$action]=$options;
  296.       break;
  297.     case 'viewerPreferences':
  298.       if (!isset($o['info']['viewerPreferences'])){
  299.         $this->numObj++;
  300.         $this->o_viewerPreferences($this->numObj,'new');
  301.         $o['info']['viewerPreferences']=$this->numObj;
  302.       }
  303.       $vp $o['info']['viewerPreferences'];
  304.       $this->o_viewerPreferences($vp,'add',$options);
  305.       break;
  306.     case 'out':
  307.       $res="\n".$id." 0 obj\n".'<< /Type /Catalog';
  308.       foreach($o['info'as $k=>$v){
  309.         switch($k){
  310.           case 'outlines':
  311.             $res.="\n".'/Outlines '.$v.' 0 R';
  312.             break;
  313.           case 'pages':
  314.             $res.="\n".'/Pages '.$v.' 0 R';
  315.             break;
  316.           case 'viewerPreferences':
  317.             $res.="\n".'/ViewerPreferences '.$o['info']['viewerPreferences'].' 0 R';
  318.             break;
  319.           case 'openHere':
  320.             $res.="\n".'/OpenAction '.$o['info']['openHere'].' 0 R';
  321.             break;
  322.         }
  323.       }
  324.       $res.=" >>\nendobj";
  325.       return $res;
  326.       break;
  327.   }
  328. }
  329.  
  330. /**
  331. * object which is a parent to the pages in the document
  332. */
  333. function o_pages($id,$action,$options=''){
  334.   if ($action!='new'){
  335.     $o =$this->objects[$id];
  336.   }
  337.   switch ($action){
  338.     case 'new':
  339.       $this->objects[$id]=array('t'=>'pages','info'=>array());
  340.       $this->o_catalog($this->catalogId,'pages',$id);
  341.       break;
  342.     case 'page':
  343.       if (!is_array($options)){
  344.         // then it will just be the id of the new page
  345.         $o['info']['pages'][]=$options;
  346.       else {
  347.         // then it should be an array having 'id','rid','pos', where rid=the page to which this one will be placed relative
  348.         // and pos is either 'before' or 'after', saying where this page will fit.
  349.         if (isset($options['id']&& isset($options['rid']&& isset($options['pos'])){
  350.           $i array_search($options['rid'],$o['info']['pages']);
  351.           if (isset($o['info']['pages'][$i]&& $o['info']['pages'][$i]==$options['rid']){
  352.             // then there is a match
  353.             // make a space
  354.             switch ($options['pos']){
  355.               case 'before':
  356.                 $k $i;
  357.                 break;
  358.               case 'after':
  359.                 $k=$i+1;
  360.                 break;
  361.               default:
  362.                 $k=-1;
  363.                 break;
  364.             }
  365.             if ($k>=0){
  366.               for ($j=count($o['info']['pages'])-1;$j>=$k;$j--){
  367.                 $o['info']['pages'][$j+1]=$o['info']['pages'][$j];
  368.               }
  369.               $o['info']['pages'][$k]=$options['id'];
  370.             }
  371.           }
  372.         
  373.       }
  374.       break;
  375.     case 'procset':
  376.       $o['info']['procset']=$options;
  377.       break;
  378.     case 'mediaBox':
  379.       $o['info']['mediaBox']=$options// which should be an array of 4 numbers
  380.       break;
  381.     case 'font':
  382.       $o['info']['fonts'][]=array('objNum'=>$options['objNum'],'fontNum'=>$options['fontNum']);
  383.       break;
  384.     case 'xObject':
  385.       $o['info']['xObjects'][]=array('objNum'=>$options['objNum'],'label'=>$options['label']);
  386.       break;
  387.     case 'out':
  388.       if (count($o['info']['pages'])){
  389.         $res="\n".$id." 0 obj\n<< /Type /Pages\n/Kids [";
  390.         foreach($o['info']['pages'as $k=>$v){
  391.           $res.=$v." 0 R\n";
  392.         }
  393.         $res.="]\n/Count ".count($this->objects[$id]['info']['pages']);
  394.         if ((isset($o['info']['fonts']&& count($o['info']['fonts'])) || isset($o['info']['procset'])){
  395.           $res.="\n/Resources <<";
  396.           if (isset($o['info']['procset'])){
  397.             $res.="\n/ProcSet ".$o['info']['procset']." 0 R";
  398.           }
  399.           if (isset($o['info']['fonts']&& count($o['info']['fonts'])){
  400.             $res.="\n/Font << ";
  401.             foreach($o['info']['fonts'as $finfo){
  402.               $res.="\n/F".$finfo['fontNum']." ".$finfo['objNum']." 0 R";
  403.             }
  404.             $res.=" >>";
  405.           }
  406.           if (isset($o['info']['xObjects']&& count($o['info']['xObjects'])){
  407.             $res.="\n/XObject << ";
  408.             foreach($o['info']['xObjects'as $finfo){
  409.               $res.="\n/".$finfo['label']." ".$finfo['objNum']." 0 R";
  410.             }
  411.             $res.=" >>";
  412.           }
  413.           $res.="\n>>";
  414.           if (isset($o['info']['mediaBox'])){
  415.             $tmp=$o['info']['mediaBox'];
  416.             $res.="\n/MediaBox [".sprintf('%.3f',$tmp[0]).' '.sprintf('%.3f',$tmp[1]).' '.sprintf('%.3f',$tmp[2]).' '.sprintf('%.3f',$tmp[3]).']';
  417.           }
  418.         }
  419.         $res.="\n >>\nendobj";
  420.       else {
  421.         $res="\n".$id." 0 obj\n<< /Type /Pages\n/Count 0\n>>\nendobj";
  422.       }
  423.       return $res;
  424.     break;
  425.   }
  426. }
  427.  
  428. /**
  429. * define the outlines in the doc, empty for now
  430. */
  431. function o_outlines($id,$action,$options=''){
  432.   if ($action!='new'){
  433.     $o =$this->objects[$id];
  434.   }
  435.   switch ($action){
  436.     case 'new':
  437.       $this->objects[$id]=array('t'=>'outlines','info'=>array('outlines'=>array()));
  438.       $this->o_catalog($this->catalogId,'outlines',$id);
  439.       break;
  440.     case 'outline':
  441.       $o['info']['outlines'][]=$options;
  442.       break;
  443.     case 'out':
  444.       if (count($o['info']['outlines'])){
  445.         $res="\n".$id." 0 obj\n<< /Type /Outlines /Kids [";
  446.         foreach($o['info']['outlines'as $k=>$v){
  447.           $res.=$v." 0 R ";
  448.         }
  449.         $res.="] /Count ".count($o['info']['outlines'])." >>\nendobj";
  450.       else {
  451.         $res="\n".$id." 0 obj\n<< /Type /Outlines /Count 0 >>\nendobj";
  452.       }
  453.       return $res;
  454.       break;
  455.   }
  456. }
  457.  
  458. /**
  459. * an object to hold the font description
  460. */
  461. function o_font($id,$action,$options=''){
  462.   if ($action!='new'){
  463.     $o =$this->objects[$id];
  464.   }
  465.   switch ($action){
  466.     case 'new':
  467.       $this->objects[$id]=array('t'=>'font','info'=>array('name'=>$options['name'],'SubType'=>'Type1'));
  468.       $fontNum=$this->numFonts;
  469.       $this->objects[$id]['info']['fontNum']=$fontNum;
  470.       // deal with the encoding and the differences
  471.       if (isset($options['differences'])){
  472.         // then we'll need an encoding dictionary
  473.         $this->numObj++;
  474.         $this->o_fontEncoding($this->numObj,'new',$options);
  475.         $this->objects[$id]['info']['encodingDictionary']=$this->numObj;
  476.       else if (isset($options['encoding'])){
  477.         // we can specify encoding here
  478.         switch($options['encoding']){
  479.           case 'WinAnsiEncoding':
  480.           case 'MacRomanEncoding':
  481.           case 'MacExpertEncoding':
  482.             $this->objects[$id]['info']['encoding']=$options['encoding'];
  483.             break;
  484.           case 'none':
  485.             break;
  486.           default:
  487.             $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  488.             break;
  489.         }
  490.       else {
  491.         $this->objects[$id]['info']['encoding']='WinAnsiEncoding';
  492.       }
  493.       // also tell the pages node about the new font
  494.       $this->o_pages($this->currentNode,'font',array('fontNum'=>$fontNum,'objNum'=>$id));
  495.       break;
  496.     case 'add':
  497.       foreach ($options as $k=>$v){
  498.         switch ($k){
  499.           case 'BaseFont':
  500.             $o['info']['name'$v;
  501.             break;
  502.           case 'FirstChar':
  503.           case 'LastChar':
  504.           case 'Widths':
  505.           case 'FontDescriptor':
  506.           case 'SubType':
  507.           $this->addMessage('o_font '.$k." : ".$v);
  508.             $o['info'][$k$v;
  509.             break;
  510.         }
  511.      }
  512.       break;
  513.     case 'out':
  514.       $res="\n".$id." 0 obj\n<< /Type /Font\n/Subtype /".$o['info']['SubType']."\n";
  515.       $res.="/Name /F".$o['info']['fontNum']."\n";
  516.       $res.="/BaseFont /".$o['info']['name']."\n";
  517.       if (isset($o['info']['encodingDictionary'])){
  518.         // then place a reference to the dictionary
  519.         $res.="/Encoding ".$o['info']['encodingDictionary']." 0 R\n";
  520.       else if (isset($o['info']['encoding'])){
  521.         // use the specified encoding
  522.         $res.="/Encoding /".$o['info']['encoding']."\n";
  523.       }
  524.       if (isset($o['info']['FirstChar'])){
  525.         $res.="/FirstChar ".$o['info']['FirstChar']."\n";
  526.       }
  527.       if (isset($o['info']['LastChar'])){
  528.         $res.="/LastChar ".$o['info']['LastChar']."\n";
  529.       }
  530.       if (isset($o['info']['Widths'])){
  531.         $res.="/Widths ".$o['info']['Widths']." 0 R\n";
  532.       }
  533.       if (isset($o['info']['FontDescriptor'])){
  534.         $res.="/FontDescriptor ".$o['info']['FontDescriptor']." 0 R\n";
  535.       }
  536.       $res.=">>\nendobj";
  537.       return $res;
  538.       break;
  539.   }
  540. }
  541.  
  542. /**
  543. * a font descriptor, needed for including additional fonts
  544. */
  545. function o_fontDescriptor($id,$action,$options=''){
  546.   if ($action!='new'){
  547.     $o =$this->objects[$id];
  548.   }
  549.   switch ($action){
  550.     case 'new':
  551.       $this->objects[$id]=array('t'=>'fontDescriptor','info'=>$options);
  552.       break;
  553.     case 'out':
  554.       $res="\n".$id." 0 obj\n<< /Type /FontDescriptor\n";
  555.       foreach ($o['info'as $label => $value){
  556.         switch ($label){
  557.           case 'Ascent':
  558.           case 'CapHeight':
  559.           case 'Descent':
  560.           case 'Flags':
  561.           case 'ItalicAngle':
  562.           case 'StemV':
  563.           case 'AvgWidth':
  564.           case 'Leading':
  565.           case 'MaxWidth':
  566.           case 'MissingWidth':
  567.           case 'StemH':
  568.           case 'XHeight':
  569.           case 'CharSet':
  570.             if (strlen($value)){
  571.               $res.='/'.$label.' '.$value."\n";
  572.             }
  573.             break;
  574.           case 'FontFile':
  575.           case 'FontFile2':
  576.           case 'FontFile3':
  577.             $res.='/'.$label.' '.$value." 0 R\n";
  578.             break;
  579.           case 'FontBBox':
  580.             $res.='/'.$label.' ['.$value[0].' '.$value[1].' '.$value[2].' '.$value[3]."]\n";
  581.             break;
  582.           case 'FontName':
  583.             $res.='/'.$label.' /'.$value."\n";
  584.             break;
  585.         }
  586.       }
  587.       $res.=">>\nendobj";
  588.       return $res;
  589.       break;
  590.   }
  591. }
  592.  
  593. /**
  594. * the font encoding
  595. */
  596. function o_fontEncoding($id,$action,$options=''){
  597.   if ($action!='new'){
  598.     $o =$this->objects[$id];
  599.   }
  600.   switch ($action){
  601.     case 'new':
  602.       // the options array should contain 'differences' and maybe 'encoding'
  603.       $this->objects[$id]=array('t'=>'fontEncoding','info'=>$options);
  604.       break;
  605.     case 'out':
  606.       $res="\n".$id." 0 obj\n<< /Type /Encoding\n";
  607.       if (!isset($o['info']['encoding'])){
  608.         $o['info']['encoding']='WinAnsiEncoding';
  609.       }
  610.       if ($o['info']['encoding']!='none'){
  611.         $res.="/BaseEncoding /".$o['info']['encoding']."\n";
  612.       }
  613.       $res.="/Differences \n[";
  614.       $onum=-100;
  615.       foreach($o['info']['differences'as $num=>$label){
  616.         if ($num!=$onum+1){
  617.           // we cannot make use of consecutive numbering
  618.           $res.= "\n".$num." /".$label;
  619.         else {
  620.           $res.= " /".$label;
  621.         }
  622.         $onum=$num;
  623.       }
  624.       $res.="\n]\n>>\nendobj";
  625.       return $res;
  626.       break;
  627.   }
  628. }
  629.  
  630. /**
  631. * the document procset, solves some problems with printing to old PS printers
  632. */
  633. function o_procset($id,$action,$options=''){
  634.   if ($action!='new'){
  635.     $o =$this->objects[$id];
  636.   }
  637.   switch ($action){
  638.     case 'new':
  639.       $this->objects[$id]=array('t'=>'procset','info'=>array('PDF'=>1,'Text'=>1));
  640.       $this->o_pages($this->currentNode,'procset',$id);
  641.       $this->procsetObjectId=$id;
  642.       break;
  643.     case 'add':
  644.       // this is to add new items to the procset list, despite the fact that this is considered
  645.       // obselete, the items are required for printing to some postscript printers
  646.       switch ($options{
  647.         case 'ImageB':
  648.         case 'ImageC':
  649.         case 'ImageI':
  650.           $o['info'][$options]=1;
  651.           break;
  652.       }
  653.       break;
  654.     case 'out':
  655.       $res="\n".$id." 0 obj\n[";
  656.       foreach ($o['info'as $label=>$val){
  657.         $res.='/'.$label.' ';
  658.       }
  659.       $res.="]\nendobj";
  660.       return $res;
  661.       break;
  662.   }
  663. }
  664.  
  665. /**
  666. * define the document information
  667. */
  668. function o_info($id,$action,$options=''){
  669.   if ($action!='new'){
  670.     $o =$this->objects[$id];
  671.   }
  672.   switch ($action){
  673.     case 'new':
  674.       $this->infoObject=$id;
  675.       $date='D:'.date('Ymd');
  676.       $this->objects[$id]=array('t'=>'info','info'=>array('Creator'=>'R and OS php pdf writer, http://www.ros.co.nz','CreationDate'=>$date));
  677.       break;
  678.     case 'Title':
  679.     case 'Author':
  680.     case 'Subject':
  681.     case 'Keywords':
  682.     case 'Creator':
  683.     case 'Producer':
  684.     case 'CreationDate':
  685.     case 'ModDate':
  686.     case 'Trapped':
  687.       $o['info'][$action]=$options;
  688.       break;
  689.     case 'out':
  690.       if ($this->encrypted){
  691.         $this->encryptInit($id);
  692.       }
  693.       $res="\n".$id." 0 obj\n<<\n";
  694.       foreach ($o['info']  as $k=>$v){
  695.         $res.='/'.$k.' (';
  696.         if ($this->encrypted){
  697.           $res.=$this->filterText($this->ARC4($v));
  698.         else {
  699.           $res.=$this->filterText($v);
  700.         }
  701.         $res.=")\n";
  702.       }
  703.       $res.=">>\nendobj";
  704.       return $res;
  705.       break;
  706.   }
  707. }
  708.  
  709. /**
  710. * an action object, used to link to URLS initially
  711. */
  712. function o_action($id,$action,$options=''){
  713.   if ($action!='new'){
  714.     $o =$this->objects[$id];
  715.   }
  716.   switch ($action){
  717.     case 'new':
  718.       if (is_array($options)){
  719.         $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>$options['type']);
  720.       else {
  721.         // then assume a URI action
  722.         $this->objects[$id]=array('t'=>'action','info'=>$options,'type'=>'URI');
  723.       }
  724.       break;
  725.     case 'out':
  726.       if ($this->encrypted){
  727.         $this->encryptInit($id);
  728.       }
  729.       $res="\n".$id." 0 obj\n<< /Type /Action";
  730.       switch($o['type']){
  731.         case 'ilink':
  732.           // there will be an 'label' setting, this is the name of the destination
  733.           $res.="\n/S /GoTo\n/D ".$this->destinations[(string)$o['info']['label']]." 0 R";
  734.           break;
  735.         case 'URI':
  736.           $res.="\n/S /URI\n/URI (";
  737.           if ($this->encrypted){
  738.             $res.=$this->filterText($this->ARC4($o['info']));
  739.           else {
  740.             $res.=$this->filterText($o['info']);
  741.           }
  742.           $res.=")";
  743.           break;
  744.       }
  745.       $res.="\n>>\nendobj";
  746.       return $res;
  747.       break;
  748.   }
  749. }
  750.  
  751. /**
  752. * an annotation object, this will add an annotation to the current page.
  753. * initially will support just link annotations
  754. */
  755. function o_annotation($id,$action,$options=''){
  756.   if ($action!='new'){
  757.     $o =$this->objects[$id];
  758.   }
  759.   switch ($action){
  760.     case 'new':
  761.       // add the annotation to the current page
  762.       $pageId $this->currentPage;
  763.       $this->o_page($pageId,'annot',$id);
  764.       // and add the action object which is going to be required
  765.       switch($options['type']){
  766.         case 'link':
  767.           $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  768.           $this->numObj++;
  769.           $this->o_action($this->numObj,'new',$options['url']);
  770.           $this->objects[$id]['info']['actionId']=$this->numObj;
  771.           break;
  772.         case 'ilink':
  773.           // this is to a named internal link
  774.           $label $options['label'];
  775.           $this->objects[$id]=array('t'=>'annotation','info'=>$options);
  776.           $this->numObj++;
  777.           $this->o_action($this->numObj,'new',array('type'=>'ilink','label'=>$label));
  778.           $this->objects[$id]['info']['actionId']=$this->numObj;
  779.           break;
  780.       }
  781.       break;
  782.     case 'out':
  783.       $res="\n".$id." 0 obj\n<< /Type /Annot";
  784.       switch($o['info']['type']){
  785.         case 'link':
  786.         case 'ilink':
  787.           $res.= "\n/Subtype /Link";
  788.           break;
  789.       }
  790.       $res.="\n/A ".$o['info']['actionId']." 0 R";
  791.       $res.="\n/Border [0 0 0]";
  792.       $res.="\n/H /I";
  793.       $res.="\n/Rect [ ";
  794.       foreach($o['info']['rect'as $v){
  795.         $res.= sprintf("%.4f ",$v);
  796.       }
  797.       $res.="]";
  798.       $res.="\n>>\nendobj";
  799.       return $res;
  800.       break;
  801.   }
  802. }
  803.  
  804. /**
  805. * a page object, it also creates a contents object to hold its contents
  806. */
  807. function o_page($id,$action,$options=''){
  808.   if ($action!='new'){
  809.     $o =$this->objects[$id];
  810.   }
  811.   switch ($action){
  812.     case 'new':
  813.       $this->numPages++;
  814.       $this->objects[$id]=array('t'=>'page','info'=>array('parent'=>$this->currentNode,'pageNum'=>$this->numPages));
  815.       if (is_array($options)){
  816.         // then this must be a page insertion, array shoudl contain 'rid','pos'=[before|after]
  817.         $options['id']=$id;
  818.         $this->o_pages($this->currentNode,'page',$options);
  819.       else {
  820.         $this->o_pages($this->currentNode,'page',$id);
  821.       }
  822.       $this->currentPage=$id;
  823.       //make a contents object to go with this page
  824.       $this->numObj++;
  825.       $this->o_contents($this->numObj,'new',$id);
  826.       $this->currentContents=$this->numObj;
  827.       $this->objects[$id]['info']['contents']=array();
  828.       $this->objects[$id]['info']['contents'][]=$this->numObj;
  829.       $match ($this->numPages%'odd' 'even');
  830.       foreach($this->addLooseObjects as $oId=>$target){
  831.         if ($target=='all' || $match==$target){
  832.           $this->objects[$id]['info']['contents'][]=$oId;
  833.         }
  834.       }
  835.       break;
  836.     case 'content':
  837.       $o['info']['contents'][]=$options;
  838.       break;
  839.     case 'annot':
  840.       // add an annotation to this page
  841.       if (!isset($o['info']['annot'])){
  842.         $o['info']['annot']=array();
  843.       }
  844.       // $options should contain the id of the annotation dictionary
  845.       $o['info']['annot'][]=$options;
  846.       break;
  847.     case 'out':
  848.       $res="\n".$id." 0 obj\n<< /Type /Page";
  849.       $res.="\n/Parent ".$o['info']['parent']." 0 R";
  850.       if (isset($o['info']['annot'])){
  851.         $res.="\n/Annots [";
  852.         foreach($o['info']['annot'as $aId){
  853.           $res.=" ".$aId." 0 R";
  854.         }
  855.         $res.=" ]";
  856.       }
  857.       $count count($o['info']['contents']);
  858.       if ($count==1){
  859.         $res.="\n/Contents ".$o['info']['contents'][0]." 0 R";
  860.       else if ($count>1){
  861.         $res.="\n/Contents [\n";
  862.         foreach ($o['info']['contents'as $cId){
  863.           $res.=$cId." 0 R\n";
  864.         }
  865.         $res.="]";
  866.       }
  867.       $res.="\n>>\nendobj";
  868.       return $res;
  869.       break;
  870.   }
  871. }
  872.  
  873. /**
  874. * the contents objects hold all of the content which appears on pages
  875. */
  876. function o_contents($id,$action,$options=''){
  877.   if ($action!='new'){
  878.     $o =$this->objects[$id];
  879.   }
  880.   switch ($action){
  881.     case 'new':
  882.       $this->objects[$id]=array('t'=>'contents','c'=>'','info'=>array());
  883.       if (strlen($options&& intval($options)){
  884.         // then this contents is the primary for a page
  885.         $this->objects[$id]['onPage']=$options;
  886.       else if ($options=='raw'){
  887.         // then this page contains some other type of system object
  888.         $this->objects[$id]['raw']=1;
  889.       }
  890.       break;
  891.     case 'add':
  892.       // add more options to the decleration
  893.       foreach ($options as $k=>$v){
  894.         $o['info'][$k]=$v;
  895.       }
  896.     case 'out':
  897.       $tmp=$o['c'];
  898.       $res"\n".$id." 0 obj\n";
  899.       if (isset($this->objects[$id]['raw'])){
  900.         $res.=$tmp;
  901.       else {
  902.         $res.= "<<";
  903.         if (function_exists('gzcompress'&& $this->options['compression']){
  904.           // then implement ZLIB based compression on this content stream
  905.           $res.=" /Filter /FlateDecode";
  906.           $tmp gzcompress($tmp);
  907.         }
  908.         if ($this->encrypted){
  909.           $this->encryptInit($id);
  910.           $tmp $this->ARC4($tmp);
  911.         }
  912.         foreach($o['info'as $k=>$v){
  913.           $res .= "\n/".$k.' '.$v;
  914.         }
  915.         $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream";
  916.       }
  917.       $res.="\nendobj\n";
  918.       return $res;
  919.       break;
  920.   }
  921. }
  922.  
  923. /**
  924. * an image object, will be an XObject in the document, includes description and data
  925. */
  926. function o_image($id,$action,$options=''){
  927.   if ($action!='new'){
  928.     $o =$this->objects[$id];
  929.   }
  930.   switch($action){
  931.     case 'new':
  932.       // make the new object
  933.       $this->objects[$id]=array('t'=>'image','data'=>$options['data'],'info'=>array());
  934.       $this->objects[$id]['info']['Type']='/XObject';
  935.       $this->objects[$id]['info']['Subtype']='/Image';
  936.       $this->objects[$id]['info']['Width']=$options['iw'];
  937.       $this->objects[$id]['info']['Height']=$options['ih'];
  938.       if (!isset($options['type']|| $options['type']=='jpg'){
  939.         if (!isset($options['channels'])){
  940.           $options['channels']=3;
  941.         }
  942.         switch($options['channels']){
  943.           case 1:
  944.             $this->objects[$id]['info']['ColorSpace']='/DeviceGray';
  945.             break;
  946.           default:
  947.             $this->objects[$id]['info']['ColorSpace']='/DeviceRGB';
  948.             break;
  949.         }
  950.         $this->objects[$id]['info']['Filter']='/DCTDecode';
  951.         $this->objects[$id]['info']['BitsPerComponent']=8;
  952.       else if ($options['type']=='png'){
  953.         $this->objects[$id]['info']['Filter']='/FlateDecode';
  954.         $this->objects[$id]['info']['DecodeParms']='<< /Predictor 15 /Colors '.$options['ncolor'].' /Columns '.$options['iw'].' /BitsPerComponent '.$options['bitsPerComponent'].'>>';
  955.         if (strlen($options['pdata'])){
  956.           $tmp ' [ /Indexed /DeviceRGB '.(strlen($options['pdata'])/3-1).' ';
  957.           $this->numObj++;
  958.           $this->o_contents($this->numObj,'new');
  959.           $this->objects[$this->numObj]['c']=$options['pdata'];
  960.           $tmp.=$this->numObj.' 0 R';
  961.           $tmp .=' ]';
  962.           $this->objects[$id]['info']['ColorSpace'$tmp;
  963.           if (isset($options['transparency'])){
  964.             switch($options['transparency']['type']){
  965.               case 'indexed':
  966.                 $tmp=' [ '.$options['transparency']['data'].' '.$options['transparency']['data'].'] ';
  967.                 $this->objects[$id]['info']['Mask'$tmp;
  968.                 break;
  969.             }
  970.           }
  971.         else {
  972.           $this->objects[$id]['info']['ColorSpace']='/'.$options['color'];
  973.         }
  974.         $this->objects[$id]['info']['BitsPerComponent']=$options['bitsPerComponent'];
  975.       }
  976.       // assign it a place in the named resource dictionary as an external object, according to
  977.       // the label passed in with it.
  978.       $this->o_pages($this->currentNode,'xObject',array('label'=>$options['label'],'objNum'=>$id));
  979.       // also make sure that we have the right procset object for it.
  980.       $this->o_procset($this->procsetObjectId,'add','ImageC');
  981.       break;
  982.     case 'out':
  983.       $tmp=$o['data'];
  984.       $res"\n".$id." 0 obj\n<<";
  985.       foreach($o['info'as $k=>$v){
  986.         $res.="\n/".$k.' '.$v;
  987.       }
  988.       if ($this->encrypted){
  989.         $this->encryptInit($id);
  990.         $tmp $this->ARC4($tmp);
  991.       }
  992.       $res.="\n/Length ".strlen($tmp)." >>\nstream\n".$tmp."\nendstream\nendobj\n";
  993.       return $res;
  994.       break;
  995.   }
  996. }
  997.  
  998. /**
  999. * encryption object.
  1000. */
  1001. function o_encryption($id,$action,$options=''){
  1002.   if ($action!='new'){
  1003.     $o =$this->objects[$id];
  1004.   }
  1005.   switch($action){
  1006.     case 'new':
  1007.       // make the new object
  1008.       $this->objects[$id]=array('t'=>'encryption','info'=>$options);
  1009.       $this->arc4_objnum=$id;
  1010.       // figure out the additional paramaters required
  1011.       $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);
  1012.       $len strlen($options['owner']);
  1013.       if ($len>32){
  1014.         $owner substr($options['owner'],0,32);
  1015.       else if ($len<32){
  1016.         $owner $options['owner'].substr($pad,0,32-$len);
  1017.       else {
  1018.         $owner $options['owner'];
  1019.       }
  1020.       $len strlen($options['user']);
  1021.       if ($len>32){
  1022.         $user substr($options['user'],0,32);
  1023.       else if ($len<32){
  1024.         $user $options['user'].substr($pad,0,32-$len);
  1025.       else {
  1026.         $user $options['user'];
  1027.       }
  1028.       $tmp $this->md5_16($owner);
  1029.       $okey substr($tmp,0,5);
  1030.       $this->ARC4_init($okey);
  1031.       $ovalue=$this->ARC4($user);
  1032.       $this->objects[$id]['info']['O']=$ovalue;
  1033.       // now make the u value, phew.
  1034.       $tmp $this->md5_16($user.$ovalue.chr($options['p']).chr(255).chr(255).chr(255).$this->fileIdentifier);
  1035.       $ukey substr($tmp,0,5);
  1036.  
  1037.       $this->ARC4_init($ukey);
  1038.       $this->encryptionKey = $ukey;
  1039.       $this->encrypted=1;
  1040.       $uvalue=$this->ARC4($pad);
  1041.  
  1042.       $this->objects[$id]['info']['U']=$uvalue;
  1043.       $this->encryptionKey=$ukey;
  1044.      
  1045.       // initialize the arc4 array
  1046.       break;
  1047.     case 'out':
  1048.       $res"\n".$id." 0 obj\n<<";
  1049.       $res.="\n/Filter /Standard";
  1050.       $res.="\n/V 1";
  1051.       $res.="\n/R 2";
  1052.       $res.="\n/O (".$this->filterText($o['info']['O']).')';
  1053.       $res.="\n/U (".$this->filterText($o['info']['U']).')';
  1054.       // and the p-value needs to be converted to account for the twos-complement approach
  1055.       $o['info']['p'(($o['info']['p']^255)+1)*-1;
  1056.       $res.="\n/P ".($o['info']['p']);
  1057.       $res.="\n>>\nendobj\n";
  1058.       
  1059.       return $res;
  1060.       break;
  1061.   }
  1062. }
  1063.       
  1064. /**
  1065. * ARC4 functions
  1066. * A series of function to implement ARC4 encoding in PHP
  1067. */
  1068.  
  1069. /**
  1070. * calculate the 16 byte version of the 128 bit md5 digest of the string
  1071. */
  1072. function md5_16($string){
  1073.   $tmp md5($string);
  1074.   $out='';
  1075.   for ($i=0;$i<=30;$i=$i+2){
  1076.     $out.=chr(hexdec(substr($tmp,$i,2)));
  1077.   }
  1078.   return $out;
  1079. }
  1080.  
  1081. /**
  1082. * initialize the encryption for processing a particular object
  1083. */
  1084. function encryptInit($id){
  1085.   $tmp $this->encryptionKey;
  1086.   $hex dechex($id);
  1087.   if (strlen($hex)<6){
  1088.     $hex substr('000000',0,6-strlen($hex)).$hex;
  1089.   }
  1090.   $tmp.= chr(hexdec(substr($hex,4,2))).chr(hexdec(substr($hex,2,2))).chr(hexdec(substr($hex,0,2))).chr(0).chr(0);
  1091.   $key $this->md5_16($tmp);
  1092.   $this->ARC4_init(substr($key,0,10));
  1093. }
  1094.  
  1095. /**
  1096. * initialize the ARC4 encryption
  1097. */
  1098. function ARC4_init($key=''){
  1099.   $this->arc4 = '';
  1100.   // setup the control array
  1101.   if (strlen($key)==0){
  1102.     return;
  1103.   }
  1104.   $k '';
  1105.   while(strlen($k)<256){
  1106.     $k.=$key;
  1107.   }
  1108.   $k=substr($k,0,256);
  1109.   for ($i=0;$i<256;$i++){
  1110.     $this->arc4 .= chr($i);
  1111.   }
  1112.   $j=0;
  1113.   for ($i=0;$i<256;$i++){
  1114.     $t $this->arc4[$i];
  1115.     $j ($j ord($tord($k[$i]))%256;
  1116.     $this->arc4[$i]=$this->arc4[$j];
  1117.     $this->arc4[$j]=$t;
  1118.   }    
  1119. }
  1120.  
  1121. /**
  1122. * ARC4 encrypt a text string
  1123. */
  1124. function ARC4($text){
  1125.   $len=strlen($text);
  1126.   $a=0;
  1127.   $b=0;
  1128.   $c $this->arc4;
  1129.   $out='';
  1130.   for ($i=0;$i<$len;$i++){
  1131.     $a ($a+1)%256;
  1132.     $t$c[$a];
  1133.     $b ($b+ord($t))%256;
  1134.     $c[$a]=$c[$b];
  1135.     $c[$b]=$t;
  1136.     $k ord($c[(ord($c[$a])+ord($c[$b]))%256]);
  1137.     $out.=chr(ord($text[$i]$k);
  1138.   }
  1139.   
  1140.   return $out;
  1141. }
  1142.  
  1143. /**
  1144. * functions which can be called to adjust or add to the document
  1145. */
  1146.  
  1147. /**
  1148. * add a link in the document to an external URL
  1149. */
  1150. function addLink($url,$x0,$y0,$x1,$y1){
  1151.   $this->numObj++;
  1152.   $info array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
  1153.   $this->o_annotation($this->numObj,'new',$info);
  1154. }
  1155.  
  1156. /**
  1157. * add a link in the document to an internal destination (ie. within the document)
  1158. */
  1159. function addInternalLink($label,$x0,$y0,$x1,$y1){
  1160.   $this->numObj++;
  1161.   $info array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
  1162.   $this->o_annotation($this->numObj,'new',$info);
  1163. }
  1164.  
  1165. /**
  1166. * set the encryption of the document
  1167. * can be used to turn it on and/or set the passwords which it will have.
  1168. * also the functions that the user will have are set here, such as print, modify, add
  1169. */
  1170. function setEncryption($userPass='',$ownerPass='',$pc=array()){
  1171.   $p=bindec(11000000);
  1172.  
  1173.   $options array(
  1174.      'print'=>4
  1175.     ,'modify'=>8
  1176.     ,'copy'=>16
  1177.     ,'add'=>32
  1178.   );
  1179.   foreach($pc as $k=>$v){
  1180.     if ($v && isset($options[$k])){
  1181.       $p+=$options[$k];
  1182.     else if (isset($options[$v])){
  1183.       $p+=$options[$v];
  1184.     }
  1185.   }
  1186.   // implement encryption on the document
  1187.   if ($this->arc4_objnum == 0){
  1188.     // then the block does not exist already, add it.
  1189.     $this->numObj++;
  1190.     if (strlen($ownerPass)==0){
  1191.       $ownerPass=$userPass;
  1192.     }
  1193.     $this->o_encryption($this->numObj,'new',array('user'=>$userPass,'owner'=>$ownerPass,'p'=>$p));
  1194.   }
  1195. }
  1196.  
  1197. /**
  1198. * should be used for internal checks, not implemented as yet
  1199. */
  1200. function checkAllHere(){
  1201. }
  1202.  
  1203. /**
  1204. * return the pdf stream as a string returned from the function
  1205. */
  1206. function output($debug=0){
  1207.  
  1208.   if ($debug){
  1209.     // turn compression off
  1210.     $this->options['compression']=0;
  1211.   }
  1212.  
  1213.   if ($this->arc4_objnum){
  1214.     $this->ARC4_init($this->encryptionKey);
  1215.   }
  1216.  
  1217.   $this->checkAllHere();
  1218.  
  1219.   $xref=array();
  1220.   $content="%PDF-1.3\n%âãÏÓ\n";
  1221. //  $content="%PDF-1.3\n";
  1222.   $pos=strlen($content);
  1223.   foreach($this->objects as $k=>$v){
  1224.     $tmp='o_'.$v['t'];
  1225.     $cont=$this->$tmp($k,'out');
  1226.     $content.=$cont;
  1227.     $xref[]=$pos;
  1228.     $pos+=strlen($cont);
  1229.   }
  1230.   $content.="\nxref\n0 ".(count($xref)+1)."\n0000000000 65535 f \n";
  1231.   foreach($xref as $p){
  1232.     $content.=substr('0000000000',0,10-strlen($p)).$p." 00000 n \n";
  1233.   }
  1234.   $content.="\ntrailer\n  << /Size ".(count($xref)+1)."\n     /Root 1 0 R\n     /Info ".$this->infoObject." 0 R\n";
  1235.   // if encryption has been applied to this document then add the marker for this dictionary
  1236.   if ($this->arc4_objnum > 0){
  1237.     $content .= "/Encrypt ".$this->arc4_objnum." 0 R\n";
  1238.   }
  1239.   if (strlen($this->fileIdentifier)){
  1240.     $content .= "/ID[<".$this->fileIdentifier."><".$this->fileIdentifier.">]\n";
  1241.   }
  1242.   $content .= "  >>\nstartxref\n".$pos."\n%%EOF\n";
  1243.   return $content;
  1244. }
  1245.  
  1246. /**
  1247. * intialize a new document
  1248. * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  1249. * this function is called automatically by the constructor function
  1250. *
  1251. @access private
  1252. */
  1253. function newDocument($pageSize=array(0,0,612,792)){
  1254.   $this->numObj=0;
  1255.   $this->objects = array();
  1256.  
  1257.   $this->numObj++;
  1258.   $this->o_catalog($this->numObj,'new');
  1259.  
  1260.   $this->numObj++;
  1261.   $this->o_outlines($this->numObj,'new');
  1262.  
  1263.   $this->numObj++;
  1264.   $this->o_pages($this->numObj,'new');
  1265.  
  1266.   $this->o_pages($this->numObj,'mediaBox',$pageSize);
  1267.   $this->currentNode = 3;
  1268.  
  1269.   $this->numObj++;
  1270.   $this->o_procset($this->numObj,'new');
  1271.  
  1272.   $this->numObj++;
  1273.   $this->o_info($this->numObj,'new');
  1274.  
  1275.   $this->numObj++;
  1276.   $this->o_page($this->numObj,'new');
  1277.  
  1278.   // need to store the first page id as there is no way to get it to the user during 
  1279.   // startup
  1280.   $this->firstPageId = $this->currentContents;
  1281. }
  1282.  
  1283. /**
  1284. * open the font file and return a php structure containing it.
  1285. * first check if this one has been done before and saved in a form more suited to php
  1286. * note that if a php serialized version does not exist it will try and make one, but will
  1287. * require write access to the directory to do it... it is MUCH faster to have these serialized
  1288. * files.
  1289. *
  1290. @access private
  1291. */
  1292. function openFont($font){
  1293.   // assume that $font contains both the path and perhaps the extension to the file, split them
  1294.   $pos=strrpos($font,'/');
  1295.   if ($pos===false){
  1296.     $dir './';
  1297.     $name $font;
  1298.   else {
  1299.     $dir=substr($font,0,$pos+1);
  1300.     $name=substr($font,$pos+1);
  1301.   }
  1302.  
  1303.   if (substr($name,-4)=='.afm'){
  1304.     $name=substr($name,0,strlen($name)-4);
  1305.   }
  1306.   $this->addMessage('openFont: '.$font.' - '.$name);
  1307.   if (file_exists($dir.'php_'.$name.'.afm')){
  1308.     $this->addMessage('openFont: php file exists '.$dir.'php_'.$name.'.afm');
  1309.     $tmp file($dir.'php_'.$name.'.afm');
  1310.     $this->fonts[$font]=unserialize($tmp[0]);
  1311.     if (!isset($this->fonts[$font]['_version_']|| $this->fonts[$font]['_version_']<1){
  1312.       // if the font file is old, then clear it out and prepare for re-creation
  1313.       $this->addMessage('openFont: clear out, make way for new version.');
  1314.       unset($this->fonts[$font]);
  1315.     }
  1316.   }
  1317.   if (!isset($this->fonts[$font]&& file_exists($dir.$name.'.afm')){
  1318.     // then rebuild the php_<font>.afm file from the <font>.afm file
  1319.     $this->addMessage('openFont: build php file from '.$dir.$name.'.afm');
  1320.     $data array();
  1321.     $file file($dir.$name.'.afm');
  1322.     foreach ($file as $rowA){
  1323.       $row=trim($rowA);
  1324.       $pos=strpos($row,' ');
  1325.       if ($pos){
  1326.         // then there must be some keyword
  1327.         $key substr($row,0,$pos);
  1328.         switch ($key){
  1329.           case 'FontName':
  1330.           case 'FullName':
  1331.           case 'FamilyName':
  1332.           case 'Weight':
  1333.           case 'ItalicAngle':
  1334.           case 'IsFixedPitch':
  1335.           case 'CharacterSet':
  1336.           case 'UnderlinePosition':
  1337.           case 'UnderlineThickness':
  1338.           case 'Version':
  1339.           case 'EncodingScheme':
  1340.           case 'CapHeight':
  1341.           case 'XHeight':
  1342.           case 'Ascender':
  1343.           case 'Descender':
  1344.           case 'StdHW':
  1345.           case 'StdVW':
  1346.           case 'StartCharMetrics':
  1347.             $data[$key]=trim(substr($row,$pos));
  1348.             break;
  1349.           case 'FontBBox':
  1350.             $data[$key]=explode(' ',trim(substr($row,$pos)));
  1351.             break;
  1352.           case 'C':
  1353.             //C 39 ; WX 222 ; N quoteright ; B 53 463 157 718 ;
  1354.             $bits=explode(';',trim($row));
  1355.             $dtmp=array();
  1356.             foreach($bits as $bit){
  1357.               $bits2 explode(' ',trim($bit));
  1358.               if (strlen($bits2[0])){
  1359.                 if (count($bits2)>2){
  1360.                   $dtmp[$bits2[0]]=array();
  1361.                   for ($i=1;$i<count($bits2);$i++){
  1362.                     $dtmp[$bits2[0]][]=$bits2[$i];
  1363.                   }
  1364.                 else if (count($bits2)==2){
  1365.                   $dtmp[$bits2[0]]=$bits2[1];
  1366.                 }
  1367.               }
  1368.             }
  1369.             if ($dtmp['C']>=0){
  1370.               $data['C'][$dtmp['C']]=$dtmp;
  1371.               $data['C'][$dtmp['N']]=$dtmp;
  1372.             else {
  1373.               $data['C'][$dtmp['N']]=$dtmp;
  1374.             }
  1375.             break;
  1376.           case 'KPX':
  1377.             //KPX Adieresis yacute -40
  1378.             $bits=explode(' ',trim($row));
  1379.             $data['KPX'][$bits[1]][$bits[2]]=$bits[3];
  1380.             break;
  1381.         }
  1382.       }
  1383.     }
  1384.     $data['_version_']=1;
  1385.     $this->fonts[$font]=$data;
  1386.     $fp fopen($dir.'php_'.$name.'.afm','w');
  1387.     fwrite($fp,serialize($data));
  1388.     fclose($fp);
  1389.   else if (!isset($this->fonts[$font])){
  1390.     $this->addMessage('openFont: no font file found');
  1391. //    echo 'Font not Found '.$font;
  1392.   }
  1393. }
  1394.  
  1395. /**
  1396. * if the font is not loaded then load it and make the required object
  1397. * else just make it the current font
  1398. * the encoding array can contain 'encoding'=> 'none','WinAnsiEncoding','MacRomanEncoding' or 'MacExpertEncoding'
  1399. * note that encoding='none' will need to be used for symbolic fonts
  1400. * and 'differences' => an array of mappings between numbers 0->255 and character names.
  1401. *
  1402. */
  1403. function selectFont($fontName,$encoding='',$set=1){
  1404.   if (!isset($this->fonts[$fontName])){
  1405.     // load the file
  1406.     $this->openFont($fontName);
  1407.     if (isset($this->fonts[$fontName])){
  1408.       $this->numObj++;
  1409.       $this->numFonts++;
  1410.       $pos=strrpos($fontName,'/');
  1411. //      $dir=substr($fontName,0,$pos+1);
  1412.       $name=substr($fontName,$pos+1);
  1413.       if (substr($name,-4)=='.afm'){
  1414.         $name=substr($name,0,strlen($name)-4);
  1415.       }
  1416.       $options=array('name'=>$name);
  1417.       if (is_array($encoding)){
  1418.         // then encoding and differences might be set
  1419.         if (isset($encoding['encoding'])){
  1420.           $options['encoding']=$encoding['encoding'];
  1421.         }
  1422.         if (isset($encoding['differences'])){
  1423.           $options['differences']=$encoding['differences'];
  1424.         }
  1425.       else if (strlen($encoding)){
  1426.         // then perhaps only the encoding has been set
  1427.         $options['encoding']=$encoding;
  1428.       }
  1429.       $fontObj $this->numObj;
  1430.       $this->o_font($this->numObj,'new',$options);
  1431.       $this->fonts[$fontName]['fontNum']=$this->numFonts;
  1432.       // if this is a '.afm' font, and there is a '.pfa' file to go with it ( as there
  1433.       // should be for all non-basic fonts), then load it into an object and put the
  1434.       // references into the font object
  1435.       $basefile substr($fontName,0,strlen($fontName)-4);
  1436.       if (file_exists($basefile.'.pfb')){
  1437.         $fbtype 'pfb';
  1438.       else if (file_exists($basefile.'.ttf')){
  1439.         $fbtype 'ttf';
  1440.       else {
  1441.         $fbtype='';
  1442.       }
  1443.       $fbfile $basefile.'.'.$fbtype;
  1444.       
  1445. //      $pfbfile = substr($fontName,0,strlen($fontName)-4).'.pfb';
  1446. //      $ttffile = substr($fontName,0,strlen($fontName)-4).'.ttf';
  1447.       $this->addMessage('selectFont: checking for - '.$fbfile);
  1448.       if (substr($fontName,-4)=='.afm' && strlen($fbtype) ){
  1449.         $adobeFontName $this->fonts[$fontName]['FontName'];
  1450. //        $fontObj = $this->numObj;
  1451.         $this->addMessage('selectFont: adding font file - '.$fbfile.' - '.$adobeFontName);
  1452.         // find the array of fond widths, and put that into an object.
  1453.         $firstChar = -1;
  1454.         $lastChar 0;
  1455.         $widths array();
  1456.         foreach ($this->fonts[$fontName]['C'as $num=>$d){
  1457.           if (intval($num)>|| $num=='0'){
  1458.             if ($lastChar>&& $num>$lastChar+1){
  1459.               for($i=$lastChar+1;$i<$num;$i++){
  1460.                 $widths[0;
  1461.               }
  1462.             }
  1463.             $widths[$d['WX'];
  1464.             if ($firstChar==-1){
  1465.               $firstChar $num;
  1466.             }
  1467.             $lastChar $num;
  1468.           }
  1469.         }
  1470.         // also need to adjust the widths for the differences array
  1471.         if (isset($options['differences'])){
  1472.           foreach($options['differences'as $charNum=>$charName){
  1473.             if ($charNum>$lastChar){
  1474.               for($i=$lastChar+1;$i<=$charNum;$i++){
  1475.                 $widths[]=0;
  1476.               }
  1477.               $lastChar=$charNum;
  1478.             }
  1479.             if (isset($this->fonts[$fontName]['C'][$charName])){
  1480.               $widths[$charNum-$firstChar]=$this->fonts[$fontName]['C'][$charName]['WX'];
  1481.             }
  1482.           }
  1483.         }
  1484.         $this->addMessage('selectFont: FirstChar='.$firstChar);
  1485.         $this->addMessage('selectFont: LastChar='.$lastChar);
  1486.         $this->numObj++;
  1487.         $this->o_contents($this->numObj,'new','raw');
  1488.         $this->objects[$this->numObj]['c'].='[';
  1489.         foreach($widths as $width){
  1490.           $this->objects[$this->numObj]['c'].=' '.$width;
  1491.         }
  1492.         $this->objects[$this->numObj]['c'].=' ]';
  1493.         $widthid $this->numObj;
  1494.  
  1495.         // load the pfb file, and put that into an object too.
  1496.         // note that pdf supports only binary format type 1 font files, though there is a 
  1497.         // simple utility to convert them from pfa to pfb.
  1498.         $fp fopen($fbfile,'rb');
  1499.         $tmp get_magic_quotes_runtime();
  1500.         set_magic_quotes_runtime(0);
  1501.         $data fread($fp,filesize($fbfile));
  1502.         set_magic_quotes_runtime($tmp);
  1503.         fclose($fp);
  1504.  
  1505.         // create the font descriptor
  1506.         $this->numObj++;
  1507.         $fontDescriptorId $this->numObj;
  1508.         $this->numObj++;
  1509.         $pfbid $this->numObj;
  1510.         // determine flags (more than a little flakey, hopefully will not matter much)
  1511.         $flags=0;
  1512.         if ($this->fonts[$fontName]['ItalicAngle']!=0)$flags+=pow(2,6)}
  1513.         if ($this->fonts[$fontName]['IsFixedPitch']=='true')$flags+=1}
  1514.         $flags+=pow(2,5)// assume non-sybolic
  1515.  
  1516.         $list array('Ascent'=>'Ascender','CapHeight'=>'CapHeight','Descent'=>'Descender','FontBBox'=>'FontBBox','ItalicAngle'=>'ItalicAngle');
  1517.         $fdopt array(
  1518.          'Flags'=>$flags
  1519.          ,'FontName'=>$adobeFontName
  1520.          ,'StemV'=>100  // don't know what the value for this should be!
  1521.         );
  1522.         foreach($list as $k=>$v){
  1523.           if (isset($this->fonts[$fontName][$v])){
  1524.             $fdopt[$k]=$this->fonts[$fontName][$v];
  1525.           }
  1526.         }
  1527.  
  1528.         if ($fbtype=='pfb'){
  1529.           $fdopt['FontFile']=$pfbid;
  1530.         else if ($fbtype=='ttf'){
  1531.           $fdopt['FontFile2']=$pfbid;
  1532.         }
  1533.         $this->o_fontDescriptor($fontDescriptorId,'new',$fdopt);        
  1534.  
  1535.         // embed the font program
  1536.         $this->o_contents($this->numObj,'new');
  1537.         $this->objects[$pfbid]['c'].=$data;
  1538.         // determine the cruicial lengths within this file
  1539.         if ($fbtype=='pfb'){
  1540.           $l1 strpos($data,'eexec')+6;
  1541.           $l2 strpos($data,'00000000')-$l1;
  1542.           $l3 strlen($data)-$l2-$l1;
  1543.           $this->o_contents($this->numObj,'add',array('Length1'=>$l1,'Length2'=>$l2,'Length3'=>$l3));
  1544.         else if ($fbtype=='ttf'){
  1545.           $l1 strlen($data);
  1546.           $this->o_contents($this->numObj,'add',array('Length1'=>$l1));
  1547.         }
  1548.  
  1549.  
  1550.         // tell the font object about all this new stuff
  1551.         $tmp array('BaseFont'=>$adobeFontName,'Widths'=>$widthid
  1552.                                       ,'FirstChar'=>$firstChar,'LastChar'=>$lastChar
  1553.                                       ,'FontDescriptor'=>$fontDescriptorId);
  1554.         if ($fbtype=='ttf'){
  1555.           $tmp['SubType']='TrueType';
  1556.         }
  1557.         $this->addMessage('adding extra info to font.('.$fontObj.')');
  1558.         foreach($tmp as $fk=>$fv){
  1559.           $this->addMessage($fk." : ".$fv);
  1560.         }
  1561.         $this->o_font($fontObj,'add',$tmp);
  1562.  
  1563.       else {
  1564.         $this->addMessage('selectFont: pfb or ttf file not found, ok if this is one of the 14 standard fonts');
  1565.       }
  1566.  
  1567.  
  1568.       // also set the differences here, note that this means that these will take effect only the 
  1569.       //first time that a font is selected, else they are ignored
  1570.       if (isset($options['differences'])){
  1571.         $this->fonts[$fontName]['differences']=$options['differences'];
  1572.       }
  1573.     }
  1574.   }
  1575.   if ($set && isset($this->fonts[$fontName])){
  1576.     // so if for some reason the font was not set in the last one then it will not be selected
  1577.     $this->currentBaseFont=$fontName;
  1578.     // the next line means that if a new font is selected, then the current text state will be
  1579.     // applied to it as well.
  1580.     $this->setCurrentFont();
  1581.   }
  1582.   return $this->currentFontNum;
  1583. }
  1584.  
  1585. /**
  1586. * sets up the current font, based on the font families, and the current text state
  1587. * note that this system is quite flexible, a <<b>><<i>> font can be completely different to a
  1588. * <<i>><<b>> font, and even <<b>><<b>> will have to be defined within the family to have meaning
  1589. * This function is to be called whenever the currentTextState is changed, it will update
  1590. * the currentFont setting to whatever the appropriatte family one is.
  1591. * If the user calls selectFont themselves then that will reset the currentBaseFont, and the currentFont
  1592. * This function will change the currentFont to whatever it should be, but will not change the
  1593. * currentBaseFont.
  1594. *
  1595. @access private
  1596. */
  1597. function setCurrentFont(){
  1598.   if (strlen($this->currentBaseFont)==0){
  1599.     // then assume an initial font
  1600.     $this->selectFont('./fonts/Helvetica.afm');
  1601.   }
  1602.   $cf substr($this->currentBaseFont,strrpos($this->currentBaseFont,'/')+1);
  1603.   if (strlen($this->currentTextState)
  1604.     && isset($this->fontFamilies[$cf]
  1605.       && isset($this->fontFamilies[$cf][$this->currentTextState])){
  1606.     // then we are in some state or another
  1607.     // and this font has a family, and the current setting exists within it
  1608.     // select the font, then return it
  1609.     $nf substr($this->currentBaseFont,0,strrpos($this->currentBaseFont,'/')+1).$this->fontFamilies[$cf][$this->currentTextState];
  1610.     $this->selectFont($nf,'',0);
  1611.     $this->currentFont = $nf;
  1612.     $this->currentFontNum = $this->fonts[$nf]['fontNum'];
  1613.   else {
  1614.     // the this font must not have the right family member for the current state
  1615.     // simply assume the base font
  1616.     $this->currentFont = $this->currentBaseFont;
  1617.     $this->currentFontNum = $this->fonts[$this->currentFont]['fontNum'];    
  1618.   }
  1619. }
  1620.  
  1621. /**
  1622. * function for the user to find out what the ID is of the first page that was created during
  1623. * startup - useful if they wish to add something to it later.
  1624. */
  1625. function getFirstPageId(){
  1626.   return $this->firstPageId;
  1627. }
  1628.  
  1629. /**
  1630. * add content to the currently active object
  1631. *
  1632. @access private
  1633. */
  1634. function addContent($content){
  1635.   $this->objects[$this->currentContents]['c'].=$content;
  1636. }
  1637.  
  1638. /**
  1639. * sets the colour for fill operations
  1640. */
  1641. function setColor($r,$g,$b,$force=0){
  1642.   if ($r>=&& ($force || $r!=$this->currentColour['r'|| $g!=$this->currentColour['g'|| $b!=$this->currentColour['b'])){
  1643.     $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' rg';
  1644.     $this->currentColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1645.   }
  1646. }
  1647.  
  1648. /**
  1649. * sets the colour for stroke operations
  1650. */
  1651. function setStrokeColor($r,$g,$b,$force=0){
  1652.   if ($r>=&& ($force || $r!=$this->currentStrokeColour['r'|| $g!=$this->currentStrokeColour['g'|| $b!=$this->currentStrokeColour['b'])){
  1653.     $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$r).' '.sprintf('%.3f',$g).' '.sprintf('%.3f',$b).' RG';
  1654.     $this->currentStrokeColour=array('r'=>$r,'g'=>$g,'b'=>$b);
  1655.   }
  1656. }
  1657.  
  1658. /**
  1659. * draw a line from one set of coordinates to another
  1660. */
  1661. function line($x1,$y1,$x2,$y2){
  1662.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' m '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' l S';
  1663. }
  1664.  
  1665. /**
  1666. * draw a bezier curve based on 4 control points
  1667. */
  1668. function curve($x0,$y0,$x1,$y1,$x2,$y2,$x3,$y3){
  1669.   // in the current line style, draw a bezier curve from (x0,y0) to (x3,y3) using the other two points
  1670.   // as the control points for the curve.
  1671.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' m '.sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1);
  1672.   $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',$x2).' '.sprintf('%.3f',$y2).' '.sprintf('%.3f',$x3).' '.sprintf('%.3f',$y3).' c S';
  1673. }
  1674.  
  1675. /**
  1676. * draw a part of an ellipse
  1677. */
  1678. function partEllipse($x0,$y0,$astart,$afinish,$r1,$r2=0,$angle=0,$nSeg=8){
  1679.   $this->ellipse($x0,$y0,$r1,$r2,$angle,$nSeg,$astart,$afinish,0);
  1680. }
  1681.  
  1682. /**
  1683. * draw a filled ellipse
  1684. */
  1685. function filledEllipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360){
  1686.   return $this->ellipse($x0,$y0,$r1,$r2=0,$angle,$nSeg,$astart,$afinish,1,1);
  1687. }
  1688.  
  1689. /**
  1690. * draw an ellipse
  1691. * note that the part and filled ellipse are just special cases of this function
  1692. *
  1693. * draws an ellipse in the current line style
  1694. * centered at $x0,$y0, radii $r1,$r2
  1695. * if $r2 is not set, then a circle is drawn
  1696. * nSeg is not allowed to be less than 2, as this will simply draw a line (and will even draw a
  1697. * pretty crappy shape at 2, as we are approximating with bezier curves.
  1698. */
  1699. function ellipse($x0,$y0,$r1,$r2=0,$angle=0,$nSeg=8,$astart=0,$afinish=360,$close=1,$fill=0){
  1700.   if ($r1==0){
  1701.     return;
  1702.   }
  1703.   if ($r2==0){
  1704.     $r2=$r1;
  1705.   }
  1706.   if ($nSeg<2){
  1707.     $nSeg=2;
  1708.   }
  1709.  
  1710.   $astart deg2rad((float)$astart);
  1711.   $afinish deg2rad((float)$afinish);
  1712.   $totalAngle =$afinish-$astart;
  1713.  
  1714.   $dt $totalAngle/$nSeg;
  1715.   $dtm $dt/3;
  1716.  
  1717.   if ($angle != 0){
  1718.     $a = -1*deg2rad((float)$angle);
  1719.     $tmp "\n q ";
  1720.     $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  1721.     $tmp .= sprintf('%.3f',$x0).' '.sprintf('%.3f',$y0).' cm';
  1722.     $this->objects[$this->currentContents]['c'].= $tmp;
  1723.     $x0=0;
  1724.     $y0=0;
  1725.   }
  1726.  
  1727.   $t1 $astart;
  1728.   $a0 $x0+$r1*cos($t1);
  1729.   $b0 $y0+$r2*sin($t1);
  1730.   $c0 = -$r1*sin($t1);
  1731.   $d0 $r2*cos($t1);
  1732.  
  1733.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$a0).' '.sprintf('%.3f',$b0).' m ';
  1734.   for ($i=1;$i<=$nSeg;$i++){
  1735.     // draw this bit of the total curve
  1736.     $t1 $i*$dt+$astart;
  1737.     $a1 $x0+$r1*cos($t1);
  1738.     $b1 $y0+$r2*sin($t1);
  1739.     $c1 = -$r1*sin($t1);
  1740.     $d1 $r2*cos($t1);
  1741.     $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',($a0+$c0*$dtm)).' '.sprintf('%.3f',($b0+$d0*$dtm));
  1742.     $this->objects[$this->currentContents]['c'].= ' '.sprintf('%.3f',($a1-$c1*$dtm)).' '.sprintf('%.3f',($b1-$d1*$dtm)).' '.sprintf('%.3f',$a1).' '.sprintf('%.3f',$b1).' c';
  1743.     $a0=$a1;
  1744.     $b0=$b1;
  1745.     $c0=$c1;
  1746.     $d0=$d1;    
  1747.   }
  1748.   if ($fill){
  1749.     $this->objects[$this->currentContents]['c'].=' f';
  1750.   else {
  1751.     if ($close){
  1752.       $this->objects[$this->currentContents]['c'].=' s'// small 's' signifies closing the path as well
  1753.     else {
  1754.       $this->objects[$this->currentContents]['c'].=' S';
  1755.     }
  1756.   }
  1757.   if ($angle !=0){
  1758.     $this->objects[$this->currentContents]['c'].=' Q';
  1759.   }
  1760. }
  1761.  
  1762. /**
  1763. * this sets the line drawing style.
  1764. * width, is the thickness of the line in user units
  1765. * cap is the type of cap to put on the line, values can be 'butt','round','square'
  1766. *    where the diffference between 'square' and 'butt' is that 'square' projects a flat end past the
  1767. *    end of the line.
  1768. * join can be 'miter', 'round', 'bevel'
  1769. * dash is an array which sets the dash pattern, is a series of length values, which are the lengths of the
  1770. *   on and off dashes.
  1771. *   (2) represents 2 on, 2 off, 2 on , 2 off ...
  1772. *   (2,1) is 2 on, 1 off, 2 on, 1 off.. etc
  1773. * phase is a modifier on the dash pattern which is used to shift the point at which the pattern starts.
  1774. */
  1775. function setLineStyle($width=1,$cap='',$join='',$dash='',$phase=0){
  1776.  
  1777.   // this is quite inefficient in that it sets all the parameters whenever 1 is changed, but will fix another day
  1778.   $string '';
  1779.   if ($width>0){
  1780.     $string.= $width.' w';
  1781.   }
  1782.   $ca array('butt'=>0,'round'=>1,'square'=>2);
  1783.   if (isset($ca[$cap])){
  1784.     $string.= ' '.$ca[$cap].' J';
  1785.   }
  1786.   $ja array('miter'=>0,'round'=>1,'bevel'=>2);
  1787.   if (isset($ja[$join])){
  1788.     $string.= ' '.$ja[$join].' j';
  1789.   }
  1790.   if (is_array($dash)){
  1791.     $string.= ' [';
  1792.     foreach ($dash as $len){
  1793.       $string.=' '.$len;
  1794.     }
  1795.     $string.= ' ] '.$phase.' d';
  1796.   }
  1797.   $this->currentLineStyle = $string;
  1798.   $this->objects[$this->currentContents]['c'].="\n".$string;
  1799. }
  1800.  
  1801. /**
  1802. * draw a polygon, the syntax for this is similar to the GD polygon command
  1803. */
  1804. function polygon($p,$np,$f=0){
  1805.   $this->objects[$this->currentContents]['c'].="\n";
  1806.   $this->objects[$this->currentContents]['c'].=sprintf('%.3f',$p[0]).' '.sprintf('%.3f',$p[1]).' m ';
  1807.   for ($i=2;$i<$np*2;$i=$i+2){
  1808.     $this->objects[$this->currentContents]['c'].= sprintf('%.3f',$p[$i]).' '.sprintf('%.3f',$p[$i+1]).' l ';
  1809.   }
  1810.   if ($f==1){
  1811.     $this->objects[$this->currentContents]['c'].=' f';
  1812.   else {
  1813.     $this->objects[$this->currentContents]['c'].=' S';
  1814.   }
  1815. }
  1816.  
  1817. /**
  1818. * a filled rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1819. * the coordinates of the upper-right corner
  1820. */
  1821. function filledRectangle($x1,$y1,$width,$height){
  1822.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re f';
  1823. }
  1824.  
  1825. /**
  1826. * draw a rectangle, note that it is the width and height of the rectangle which are the secondary paramaters, not
  1827. * the coordinates of the upper-right corner
  1828. */
  1829. function rectangle($x1,$y1,$width,$height){
  1830.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$x1).' '.sprintf('%.3f',$y1).' '.sprintf('%.3f',$width).' '.sprintf('%.3f',$height).' re S';
  1831. }
  1832.  
  1833. /**
  1834. * add a new page to the document
  1835. * this also makes the new page the current active object
  1836. */
  1837. function newPage($insert=0,$id=0,$pos='after'){
  1838.  
  1839.   // if there is a state saved, then go up the stack closing them
  1840.   // then on the new page, re-open them with the right setings
  1841.   
  1842.   if ($this->nStateStack){
  1843.     for ($i=$this->nStateStack;$i>=1;$i--){
  1844.       $this->restoreState($i);
  1845.     }
  1846.   }
  1847.  
  1848.   $this->numObj++;
  1849.   if ($insert){
  1850.     // the id from the ezPdf class is the od of the contents of the page, not the page object itself
  1851.     // query that object to find the parent
  1852.     $rid $this->objects[$id]['onPage'];
  1853.     $optarray('rid'=>$rid,'pos'=>$pos);
  1854.     $this->o_page($this->numObj,'new',$opt);
  1855.   else {
  1856.     $this->o_page($this->numObj,'new');
  1857.   }
  1858.   // if there is a stack saved, then put that onto the page
  1859.   if ($this->nStateStack){
  1860.     for ($i=1;$i<=$this->nStateStack;$i++){
  1861.       $this->saveState($i);
  1862.     }
  1863.   }  
  1864.   // and if there has been a stroke or fill colour set, then transfer them
  1865.   if ($this->currentColour['r']>=0){
  1866.     $this->setColor($this->currentColour['r'],$this->currentColour['g'],$this->currentColour['b'],1);
  1867.   }
  1868.   if ($this->currentStrokeColour['r']>=0){
  1869.     $this->setStrokeColor($this->currentStrokeColour['r'],$this->currentStrokeColour['g'],$this->currentStrokeColour['b'],1);
  1870.   }
  1871.  
  1872.   // if there is a line style set, then put this in too
  1873.   if (strlen($this->currentLineStyle)){
  1874.     $this->objects[$this->currentContents]['c'].="\n".$this->currentLineStyle;
  1875.   }
  1876.  
  1877.   // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
  1878.   return $this->currentContents;
  1879. }
  1880.  
  1881. /**
  1882. * output the pdf code, streaming it to the browser
  1883. * the relevant headers are set so that hopefully the browser will recognise it
  1884. */
  1885. function stream($options=''){
  1886.   // setting the options allows the adjustment of the headers
  1887.   // values at the moment are:
  1888.   // 'Content-Disposition'=>'filename'  - sets the filename, though not too sure how well this will 
  1889.   //        work as in my trial the browser seems to use the filename of the php file with .pdf on the end
  1890.   // 'Accept-Ranges'=>1 or 0 - if this is not set to 1, then this header is not included, off by default
  1891.   //    this header seems to have caused some problems despite tha fact that it is supposed to solve
  1892.   //    them, so I am leaving it off by default.
  1893.   // 'compress'=> 1 or 0 - apply content stream compression, this is on (1) by default
  1894.   if (!is_array($options)){
  1895.     $options=array();
  1896.   }
  1897.   if isset($options['compress']&& $options['compress']==0){
  1898.     $tmp $this->output(1);
  1899.   else {
  1900.     $tmp $this->output();
  1901.   }
  1902.   header("Content-type: application/pdf");
  1903.   header("Content-Length: ".strlen(ltrim($tmp)));
  1904.   $fileName (isset($options['Content-Disposition'])?$options['Content-Disposition']:'file.pdf');
  1905.   header("Content-Disposition: inline; filename=".$fileName);
  1906.   if (isset($options['Accept-Ranges']&& $options['Accept-Ranges']==1){
  1907.     header("Accept-Ranges: ".strlen(ltrim($tmp)))
  1908.   }
  1909.   echo ltrim($tmp);
  1910. }
  1911.  
  1912. /**
  1913. * return the height in units of the current font in the given size
  1914. */
  1915. function getFontHeight($size){
  1916.   if (!$this->numFonts){
  1917.     $this->selectFont('./fonts/Helvetica');
  1918.   }
  1919.   // for the current font, and the given size, what is the height of the font in user units
  1920.   $h $this->fonts[$this->currentFont]['FontBBox'][3]-$this->fonts[$this->currentFont]['FontBBox'][1];
  1921.   return $size*$h/1000;
  1922. }
  1923.  
  1924. /**
  1925. * return the font decender, this will normally return a negative number
  1926. * if you add this number to the baseline, you get the level of the bottom of the font
  1927. * it is in the pdf user units
  1928. */
  1929. function getFontDecender($size){
  1930.   // note that this will most likely return a negative value
  1931.   if (!$this->numFonts){
  1932.     $this->selectFont('./fonts/Helvetica');
  1933.   }
  1934.   $h $this->fonts[$this->currentFont]['FontBBox'][1];
  1935.   return $size*$h/1000;
  1936. }
  1937.  
  1938. /**
  1939. * filter the text, this is applied to all text just before being inserted into the pdf document
  1940. * it escapes the various things that need to be escaped, and so on
  1941. *
  1942. @access private
  1943. */
  1944. function filterText($text){
  1945.   $text str_replace('\\','\\\\',$text);
  1946.   $text str_replace('(','\(',$text);
  1947.   $text str_replace(')','\)',$text);
  1948.   $text str_replace('&lt;','<',$text);
  1949.   $text str_replace('&gt;','>',$text);
  1950.   $text str_replace('&#039;','\'',$text);
  1951.   $text str_replace('&quot;','"',$text);
  1952.   $text str_replace('&amp;','&',$text);
  1953.  
  1954.   return $text;
  1955. }
  1956.  
  1957. /**
  1958. * given a start position and information about how text is to be laid out, calculate where
  1959. * on the page the text will end
  1960. *
  1961. @access private
  1962. */
  1963. function PRVTgetTextPosition($x,$y,$angle,$size,$wa,$text){
  1964.   // given this information return an array containing x and y for the end position as elements 0 and 1
  1965.   $w $this->getTextWidth($size,$text);
  1966.   // need to adjust for the number of spaces in this text
  1967.   $words explode(' ',$text);
  1968.   $nspaces=count($words)-1;
  1969.   $w += $wa*$nspaces;
  1970.   $a deg2rad((float)$angle);
  1971.   return array(cos($a)*$w+$x,-sin($a)*$w+$y);
  1972. }
  1973.  
  1974. /**
  1975. * wrapper function for PRVTcheckTextDirective1
  1976. *
  1977. @access private
  1978. */
  1979. function PRVTcheckTextDirective(&$text,$i,&$f){
  1980.   $x=0;
  1981.   $y=0;
  1982.   return $this->PRVTcheckTextDirective1($text,$i,$f,0,$x,$y);
  1983. }
  1984.  
  1985. /**
  1986. * checks if the text stream contains a control directive
  1987. * if so then makes some changes and returns the number of characters involved in the directive
  1988. * this has been re-worked to include everything neccesary to fins the current writing point, so that
  1989. * the location can be sent to the callback function if required
  1990. * if the directive does not require a font change, then $f should be set to 0
  1991. *
  1992. @access private
  1993. */
  1994. function PRVTcheckTextDirective1(&$text,$i,&$f,$final,&$x,&$y,$size=0,$angle=0,$wordSpaceAdjust=0){
  1995.   $directive 0;
  1996.   $j=$i;
  1997.   if ($text[$j]=='<'){
  1998.     $j++;
  1999.     switch($text[$j]){
  2000.       case '/':
  2001.         $j++;
  2002.         if (strlen($text<= $j){
  2003.           return $directive;
  2004.         }
  2005.         switch($text[$j]){
  2006.           case 'b':
  2007.           case 'i':
  2008.             $j++;
  2009.             if ($text[$j]=='>'){
  2010.               $p strrpos($this->currentTextState,$text[$j-1]);
  2011.               if ($p !== false){
  2012.                 // then there is one to remove
  2013.                 $this->currentTextState = substr($this->currentTextState,0,$p).substr($this->currentTextState,$p+1);
  2014.               }
  2015.               $directive=$j-$i+1;
  2016.             }
  2017.             break;
  2018.           case 'c':
  2019.             // this this might be a callback function
  2020.             $j++;
  2021.             $k strpos($text,'>',$j);
  2022.             if ($k!==false && $text[$j]==':'){
  2023.               // then this will be treated as a callback directive
  2024.               $directive $k-$i+1;
  2025.               $f=0;
  2026.               // split the remainder on colons to get the function name and the paramater
  2027.               $tmp substr($text,$j+1,$k-$j-1);
  2028.               $b1 strpos($tmp,':');
  2029.               if ($b1!==false){
  2030.                 $func substr($tmp,0,$b1);
  2031.                 $parm substr($tmp,$b1+1);
  2032.               else {
  2033.                 $func=$tmp;
  2034.                 $parm='';
  2035.               }
  2036.               if (!isset($func|| !strlen(trim($func))){
  2037.                 $directive=0;
  2038.               else {
  2039.                 // only call the function if this is the final call
  2040.                 if ($final){
  2041.                   // need to assess the text position, calculate the text width to this point
  2042.                   // can use getTextWidth to find the text width I think
  2043.                   $tmp $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  2044.                   $info array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'end','p'=>$parm,'nCallback'=>$this->nCallback);
  2045.                   $x=$tmp[0];
  2046.                   $y=$tmp[1];
  2047.                   $ret $this->$func($info);
  2048.                   if (is_array($ret)){
  2049.                     // then the return from the callback function could set the position, to start with, later will do font colour, and font
  2050.                     foreach($ret as $rk=>$rv){
  2051.                       switch($rk){
  2052.                         case 'x':
  2053.                         case 'y':
  2054.                           $$rk=$rv;
  2055.                           break;
  2056.                       }
  2057.                     }
  2058.                   }
  2059.                   // also remove from to the stack
  2060.                   // for simplicity, just take from the end, fix this another day
  2061.                   $this->nCallback--;
  2062.                   if ($this->nCallback<0){
  2063.                     $this->nCallBack=0;
  2064.                   }
  2065.                 }
  2066.               }
  2067.             }
  2068.             break;
  2069.         }
  2070.         break;
  2071.       case 'b':
  2072.       case 'i':
  2073.         $j++;
  2074.         if ($text[$j]=='>'){
  2075.           $this->currentTextState.=$text[$j-1];
  2076.           $directive=$j-$i+1;
  2077.         }
  2078.         break;
  2079.       case 'C':
  2080.         $noClose=1;
  2081.       case 'c':
  2082.         // this this might be a callback function
  2083.         $j++;
  2084.         $k strpos($text,'>',$j);
  2085.         if ($k!==false && $text[$j]==':'){
  2086.           // then this will be treated as a callback directive
  2087.           $directive $k-$i+1;
  2088.           $f=0;
  2089.           // split the remainder on colons to get the function name and the paramater
  2090. //          $bits = explode(':',substr($text,$j+1,$k-$j-1));
  2091.           $tmp substr($text,$j+1,$k-$j-1);
  2092.           $b1 strpos($tmp,':');
  2093.           if ($b1!==false){
  2094.             $func substr($tmp,0,$b1);
  2095.             $parm substr($tmp,$b1+1);
  2096.           else {
  2097.             $func=$tmp;
  2098.             $parm='';
  2099.           }
  2100.           if (!isset($func|| !strlen(trim($func))){
  2101.             $directive=0;
  2102.           else {
  2103.             // only call the function if this is the final call, ie, the one actually doing printing, not measurement
  2104.             if ($final){
  2105.               // need to assess the text position, calculate the text width to this point
  2106.               // can use getTextWidth to find the text width I think
  2107.               // also add the text height and decender
  2108.               $tmp $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,substr($text,0,$i));
  2109.               $info array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'start','p'=>$parm,'f'=>$func,'height'=>$this->getFontHeight($size),'decender'=>$this->getFontDecender($size));
  2110.               $x=$tmp[0];
  2111.               $y=$tmp[1];
  2112.               if (!isset($noClose|| !$noClose){
  2113.                 // only add to the stack if this is a small 'c', therefore is a start-stop pair
  2114.                 $this->nCallback++;
  2115.                 $info['nCallback']=$this->nCallback;
  2116.                 $this->callback[$this->nCallback]=$info;
  2117.               }
  2118.               $ret $this->$func($info);
  2119.               if (is_array($ret)){
  2120.                 // then the return from the callback function could set the position, to start with, later will do font colour, and font
  2121.                 foreach($ret as $rk=>$rv){
  2122.                   switch($rk){
  2123.                     case 'x':
  2124.                     case 'y':
  2125.                       $$rk=$rv;
  2126.                       break;
  2127.                   }
  2128.                 }
  2129.               }
  2130.             }
  2131.           }
  2132.         }
  2133.         break;
  2134.     }
  2135.   
  2136.   return $directive;
  2137. }
  2138.  
  2139. /**
  2140. * add text to the document, at a specified location, size and angle on the page
  2141. */
  2142. function addText($x,$y,$size,$text,$angle=0,$wordSpaceAdjust=0){
  2143.   if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2144.  
  2145.   // if there are any open callbacks, then they should be called, to show the start of the line
  2146.   if ($this->nCallback>0){
  2147.     for ($i=$this->nCallback;$i>0;$i--){
  2148.       // call each function
  2149.       $info array('x'=>$x,'y'=>$y,'angle'=>$angle,'status'=>'sol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2150.       $func $this->callback[$i]['f'];
  2151.       $this->$func($info);
  2152.     }
  2153.   }
  2154.   if ($angle==0){
  2155.     $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Td';
  2156.   else {
  2157.     $a deg2rad((float)$angle);
  2158.     $tmp "\n".'BT ';
  2159.     $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2160.     $tmp .= sprintf('%.3f',$x).' '.sprintf('%.3f',$y).' Tm';
  2161.     $this->objects[$this->currentContents]['c'.= $tmp;
  2162.   }
  2163.   if ($wordSpaceAdjust!=|| $wordSpaceAdjust != $this->wordSpaceAdjust){
  2164.     $this->wordSpaceAdjust=$wordSpaceAdjust;
  2165.     $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2166.   }
  2167.   $len=strlen($text);
  2168.   $start=0;
  2169.   for ($i=0;$i<$len;$i++){
  2170.     $f=1;
  2171.     $directive $this->PRVTcheckTextDirective($text,$i,$f);
  2172.     if ($directive){
  2173.       // then we should write what we need to
  2174.       if ($i>$start){
  2175.         $part substr($text,$start,$i-$start);
  2176.         $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2177.         $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2178.       }
  2179.       if ($f){
  2180.         // then there was nothing drastic done here, restore the contents
  2181.         $this->setCurrentFont();
  2182.       else {
  2183.         $this->objects[$this->currentContents]['c'.= ' ET';
  2184.         $f=1;
  2185.         $xp=$x;
  2186.         $yp=$y;
  2187.         $directive $this->PRVTcheckTextDirective1($text,$i,$f,1,$xp,$yp,$size,$angle,$wordSpaceAdjust);
  2188.         
  2189.         // restart the text object
  2190.           if ($angle==0){
  2191.             $this->objects[$this->currentContents]['c'].="\n".'BT '.sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Td';
  2192.           else {
  2193.             $a deg2rad((float)$angle);
  2194.             $tmp "\n".'BT ';
  2195.             $tmp .= sprintf('%.3f',cos($a)).' '.sprintf('%.3f',(-1.0*sin($a))).' '.sprintf('%.3f',sin($a)).' '.sprintf('%.3f',cos($a)).' ';
  2196.             $tmp .= sprintf('%.3f',$xp).' '.sprintf('%.3f',$yp).' Tm';
  2197.             $this->objects[$this->currentContents]['c'.= $tmp;
  2198.           }
  2199.           if ($wordSpaceAdjust!=|| $wordSpaceAdjust != $this->wordSpaceAdjust){
  2200.             $this->wordSpaceAdjust=$wordSpaceAdjust;
  2201.             $this->objects[$this->currentContents]['c'].=' '.sprintf('%.3f',$wordSpaceAdjust).' Tw';
  2202.           }
  2203.       }
  2204.       // and move the writing point to the next piece of text
  2205.       $i=$i+$directive-1;
  2206.       $start=$i+1;
  2207.     }
  2208.     
  2209.   }
  2210.   if ($start<$len){
  2211.     $part substr($text,$start);
  2212.     $this->objects[$this->currentContents]['c'].=' /F'.$this->currentFontNum.' '.sprintf('%.1f',$size).' Tf ';
  2213.     $this->objects[$this->currentContents]['c'].=' ('.$this->filterText($part).') Tj';
  2214.   }
  2215.   $this->objects[$this->currentContents]['c'].=' ET';
  2216.  
  2217.   // if there are any open callbacks, then they should be called, to show the end of the line
  2218.   if ($this->nCallback>0){
  2219.     for ($i=$this->nCallback;$i>0;$i--){
  2220.       // call each function
  2221.       $tmp $this->PRVTgetTextPosition($x,$y,$angle,$size,$wordSpaceAdjust,$text);
  2222.       $info array('x'=>$tmp[0],'y'=>$tmp[1],'angle'=>$angle,'status'=>'eol','p'=>$this->callback[$i]['p'],'nCallback'=>$this->callback[$i]['nCallback'],'height'=>$this->callback[$i]['height'],'decender'=>$this->callback[$i]['decender']);
  2223.       $func $this->callback[$i]['f'];
  2224.       $this->$func($info);
  2225.     }
  2226.   }
  2227.  
  2228. }
  2229.  
  2230. /**
  2231. * calculate how wide a given text string will be on a page, at a given size.
  2232. * this can be called externally, but is alse used by the other class functions
  2233. */
  2234. function getTextWidth($size,$text){
  2235.   // this function should not change any of the settings, though it will need to
  2236.   // track any directives which change during calculation, so copy them at the start
  2237.   // and put them back at the end.
  2238.   $store_currentTextState $this->currentTextState;
  2239.  
  2240.   if (!$this->numFonts){
  2241.     $this->selectFont('./fonts/Helvetica');
  2242.   }
  2243.  
  2244.   // converts a number or a float to a string so it can get the width
  2245.   $text "$text";
  2246.  
  2247.   // hmm, this is where it all starts to get tricky - use the font information to
  2248.   // calculate the width of each character, add them up and convert to user units
  2249.   $w=0;
  2250.   $len=strlen($text);
  2251.   $cf $this->currentFont;
  2252.   for ($i=0;$i<$len;$i++){
  2253.     $f=1;
  2254.     $directive $this->PRVTcheckTextDirective($text,$i,$f);
  2255.     if ($directive){
  2256.       if ($f){
  2257.         $this->setCurrentFont();
  2258.         $cf $this->currentFont;
  2259.       }
  2260.       $i=$i+$directive-1;
  2261.     else {
  2262.       $char=ord($text[$i]);
  2263.       if (isset($this->fonts[$cf]['differences'][$char])){
  2264.         // then this character is being replaced by another
  2265.         $name $this->fonts[$cf]['differences'][$char];
  2266.         if (isset($this->fonts[$cf]['C'][$name]['WX'])){
  2267.           $w+=$this->fonts[$cf]['C'][$name]['WX'];
  2268.         }
  2269.       else if (isset($this->fonts[$cf]['C'][$char]['WX'])){
  2270.         $w+=$this->fonts[$cf]['C'][$char]['WX'];
  2271.       }
  2272.     }
  2273.   }
  2274.   
  2275.   $this->currentTextState = $store_currentTextState;
  2276.   $this->setCurrentFont();
  2277.  
  2278.   return $w*$size/1000;
  2279. }
  2280.  
  2281. /**
  2282. * do a part of the calculation for sorting out the justification of the text
  2283. *
  2284. @access private
  2285. */
  2286. function PRVTadjustWrapText($text,$actual,$width,&$x,&$adjust,$justification){
  2287.   switch ($justification){
  2288.     case 'left':
  2289.       return;
  2290.       break;
  2291.     case 'right':
  2292.       $x+=$width-$actual;
  2293.       break;
  2294.     case 'center':
  2295.     case 'centre':
  2296.       $x+=($width-$actual)/2;
  2297.       break;
  2298.     case 'full':
  2299.       // count the number of words
  2300.       $words explode(' ',$text);
  2301.       $nspaces=count($words)-1;
  2302.       if ($nspaces>0){
  2303.         $adjust ($width-$actual)/$nspaces;
  2304.       else {
  2305.         $adjust=0;
  2306.       }
  2307.       break;
  2308.   }
  2309. }
  2310.  
  2311. /**
  2312. * add text to the page, but ensure that it fits within a certain width
  2313. * if it does not fit then put in as much as possible, splitting at word boundaries
  2314. * and return the remainder.
  2315. * justification and angle can also be specified for the text
  2316. */
  2317. function addTextWrap($x,$y,$width,$size,$text,$justification='left',$angle=0,$test=0){
  2318.   // this will display the text, and if it goes beyond the width $width, will backtrack to the 
  2319.   // previous space or hyphen, and return the remainder of the text.
  2320.  
  2321.   // $justification can be set to 'left','right','center','centre','full'
  2322.  
  2323.   // need to store the initial text state, as this will change during the width calculation
  2324.   // but will need to be re-set before printing, so that the chars work out right
  2325.   $store_currentTextState $this->currentTextState;
  2326.  
  2327.   if (!$this->numFonts){$this->selectFont('./fonts/Helvetica');}
  2328.   if ($width<=0){
  2329.     // error, pretend it printed ok, otherwise risking a loop
  2330.     return '';
  2331.   }
  2332.   $w=0;
  2333.   $break=0;
  2334.   $breakWidth=0;
  2335.   $len=strlen($text);
  2336.   $cf $this->currentFont;
  2337.   $tw $width/$size*1000;
  2338.   for ($i=0;$i<$len;$i++){
  2339.     $f=1;
  2340.     $directive $this->PRVTcheckTextDirective($text,$i,$f);
  2341.     if ($directive){
  2342.       if ($f){
  2343.         $this->setCurrentFont();
  2344.         $cf $this->currentFont;
  2345.       }
  2346.       $i=$i+$directive-1;
  2347.     else {
  2348.       $cOrd ord($text[$i]);
  2349.       if (isset($this->fonts[$cf]['differences'][$cOrd])){
  2350.         // then this character is being replaced by another
  2351.         $cOrd2 $this->fonts[$cf]['differences'][$cOrd];
  2352.       else {
  2353.         $cOrd2 $cOrd;
  2354.       }
  2355.   
  2356.       if (isset($this->fonts[$cf]['C'][$cOrd2]['WX'])){
  2357.         $w+=$this->fonts[$cf]['C'][$cOrd2]['WX'];
  2358.       }
  2359.       if ($w>$tw){
  2360.         // then we need to truncate this line
  2361.         if ($break>0){
  2362.           // then we have somewhere that we can split :)
  2363.           if ($text[$break]==' '){
  2364.             $tmp substr($text,0,$break);
  2365.           else {
  2366.             $tmp substr($text,0,$break+1);
  2367.           }
  2368.           $adjust=0;
  2369.           $this->PRVTadjustWrapText($tmp,$breakWidth,$width,$x,$adjust,$justification);
  2370.  
  2371.           // reset the text state
  2372.           $this->currentTextState = $store_currentTextState;
  2373.           $this->setCurrentFont();
  2374.           if (!$test){
  2375.             $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2376.           }
  2377.           return substr($text,$break+1);
  2378.         else {
  2379.           // just split before the current character
  2380.           $tmp substr($text,0,$i);
  2381.           $adjust=0;
  2382.           $ctmp=ord($text[$i]);
  2383.           if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2384.             $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2385.           }
  2386.           $tmpw=($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2387.           $this->PRVTadjustWrapText($tmp,$tmpw,$width,$x,$adjust,$justification);
  2388.           // reset the text state
  2389.           $this->currentTextState = $store_currentTextState;
  2390.           $this->setCurrentFont();
  2391.           if (!$test){
  2392.             $this->addText($x,$y,$size,$tmp,$angle,$adjust);
  2393.           }
  2394.           return substr($text,$i);
  2395.         }
  2396.       }
  2397.       if ($text[$i]=='-'){
  2398.         $break=$i;
  2399.         $breakWidth $w*$size/1000;
  2400.       }
  2401.       if ($text[$i]==' '){
  2402.         $break=$i;
  2403.         $ctmp=ord($text[$i]);
  2404.         if (isset($this->fonts[$cf]['differences'][$ctmp])){
  2405.           $ctmp=$this->fonts[$cf]['differences'][$ctmp];
  2406.         }
  2407.         $breakWidth ($w-$this->fonts[$cf]['C'][$ctmp]['WX'])*$size/1000;
  2408.       }
  2409.     }
  2410.   }
  2411.   // then there was no need to break this line
  2412.   if ($justification=='full'){
  2413.     $justification='left';
  2414.   }
  2415.   $adjust=0;
  2416.   $tmpw=$w*$size/1000;
  2417.   $this->PRVTadjustWrapText($text,$tmpw,$width,$x,$adjust,$justification);
  2418.   // reset the text state
  2419.   $this->currentTextState = $store_currentTextState;
  2420.   $this->setCurrentFont();
  2421.   if (!$test){
  2422.     $this->addText($x,$y,$size,$text,$angle,$adjust,$angle);
  2423.   }
  2424.   return '';
  2425. }
  2426.  
  2427. /**
  2428. * this will be called at a new page to return the state to what it was on the
  2429. * end of the previous page, before the stack was closed down
  2430. * This is to get around not being able to have open 'q' across pages
  2431. *
  2432. */
  2433. function saveState($pageEnd=0){
  2434.   if ($pageEnd){
  2435.     // this will be called at a new page to return the state to what it was on the 
  2436.     // end of the previous page, before the stack was closed down
  2437.     // This is to get around not being able to have open 'q' across pages
  2438.     $opt $this->stateStack[$pageEnd]// ok to use this as stack starts numbering at 1
  2439.     $this->setColor($opt['col']['r'],$opt['col']['g'],$opt['col']['b'],1);
  2440.     $this->setStrokeColor($opt['str']['r'],$opt['str']['g'],$opt['str']['b'],1);
  2441.     $this->objects[$this->currentContents]['c'].="\n".$opt['lin'];
  2442. //    $this->currentLineStyle = $opt['lin'];
  2443.   else {
  2444.     $this->nStateStack++;
  2445.     $this->stateStack[$this->nStateStack]=array(
  2446.       'col'=>$this->currentColour
  2447.      ,'str'=>$this->currentStrokeColour
  2448.      ,'lin'=>$this->currentLineStyle
  2449.     );
  2450.   }
  2451.   $this->objects[$this->currentContents]['c'].="\nq";
  2452. }
  2453.  
  2454. /**
  2455. * restore a previously saved state
  2456. */
  2457. function restoreState($pageEnd=0){
  2458.   if (!$pageEnd){
  2459.     $n $this->nStateStack;
  2460.     $this->currentColour = $this->stateStack[$n]['col'];
  2461.     $this->currentStrokeColour = $this->stateStack[$n]['str'];
  2462.     $this->objects[$this->currentContents]['c'].="\n".$this->stateStack[$n]['lin'];
  2463.     $this->currentLineStyle = $this->stateStack[$n]['lin'];
  2464.     unset($this->stateStack[$n]);
  2465.     $this->nStateStack--;
  2466.   }
  2467.   $this->objects[$this->currentContents]['c'].="\nQ";
  2468. }
  2469.  
  2470. /**
  2471. * make a loose object, the output will go into this object, until it is closed, then will revert to
  2472. * the current one.
  2473. * this object will not appear until it is included within a page.
  2474. * the function will return the object number
  2475. */
  2476. function openObject(){
  2477.   $this->nStack++;
  2478.   $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2479.   // add a new object of the content type, to hold the data flow
  2480.   $this->numObj++;
  2481.   $this->o_contents($this->numObj,'new');
  2482.   $this->currentContents=$this->numObj;
  2483.   $this->looseObjects[$this->numObj]=1;
  2484.   
  2485.   return $this->numObj;
  2486. }
  2487.  
  2488. /**
  2489. * open an existing object for editing
  2490. */
  2491. function reopenObject($id){
  2492.    $this->nStack++;
  2493.    $this->stack[$this->nStack]=array('c'=>$this->currentContents,'p'=>$this->currentPage);
  2494.    $this->currentContents=$id;
  2495.    // also if this object is the primary contents for a page, then set the current page to its parent
  2496.    if (isset($this->objects[$id]['onPage'])){
  2497.      $this->currentPage = $this->objects[$id]['onPage'];
  2498.    }
  2499. }
  2500.  
  2501. /**
  2502. * close an object
  2503. */
  2504. function closeObject(){
  2505.   // close the object, as long as there was one open in the first place, which will be indicated by
  2506.   // an objectId on the stack.
  2507.   if ($this->nStack>0){
  2508.     $this->currentContents=$this->stack[$this->nStack]['c'];
  2509.     $this->currentPage=$this->stack[$this->nStack]['p'];
  2510.     $this->nStack--;
  2511.     // easier to probably not worry about removing the old entries, they will be overwritten
  2512.     // if there are new ones.
  2513.   }
  2514. }
  2515.  
  2516. /**
  2517. * stop an object from appearing on pages from this point on
  2518. */
  2519. function stopObject($id){
  2520.   // if an object has been appearing on pages up to now, then stop it, this page will
  2521.   // be the last one that could contian it.
  2522.   if (isset($this->addLooseObjects[$id])){
  2523.     $this->addLooseObjects[$id]='';
  2524.   }
  2525. }
  2526.  
  2527. /**
  2528. * after an object has been created, it wil only show if it has been added, using this function.
  2529. */
  2530. function addObject($id,$options='add'){
  2531.   // add the specified object to the page
  2532.   if (isset($this->looseObjects[$id]&& $this->currentContents!=$id){
  2533.     // then it is a valid object, and it is not being added to itself
  2534.     switch($options){
  2535.       case 'all':
  2536.         // then this object is to be added to this page (done in the next block) and 
  2537.         // all future new pages. 
  2538.         $this->addLooseObjects[$id]='all';
  2539.       case 'add':
  2540.         if (isset($this->objects[$this->currentContents]['onPage'])){
  2541.           // then the destination contents is the primary for the page
  2542.           // (though this object is actually added to that page)
  2543.           $this->o_page($this->objects[$this->currentContents]['onPage'],'content',$id);
  2544.         }
  2545.         break;
  2546.       case 'even':
  2547.         $this->addLooseObjects[$id]='even';
  2548.         $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2549.         if ($this->objects[$pageObjectId]['info']['pageNum']%2==0){
  2550.           $this->addObject($id)// hacky huh :)
  2551.         }
  2552.         break;
  2553.       case 'odd':
  2554.         $this->addLooseObjects[$id]='odd';
  2555.         $pageObjectId=$this->objects[$this->currentContents]['onPage'];
  2556.         if ($this->objects[$pageObjectId]['info']['pageNum']%2==1){
  2557.           $this->addObject($id)// hacky huh :)
  2558.         }
  2559.         break;
  2560.       case 'next':
  2561.         $this->addLooseObjects[$id]='all';
  2562.         break;
  2563.       case 'nexteven':
  2564.         $this->addLooseObjects[$id]='even';
  2565.         break;
  2566.       case 'nextodd':
  2567.         $this->addLooseObjects[$id]='odd';
  2568.         break;
  2569.     }
  2570.   }
  2571. }
  2572.  
  2573. /**
  2574. * add content to the documents info object
  2575. */
  2576. function addInfo($label,$value=0){
  2577.   // this will only work if the label is one of the valid ones.
  2578.   // modify this so that arrays can be passed as well.
  2579.   // if $label is an array then assume that it is key=>value pairs
  2580.   // else assume that they are both scalar, anything else will probably error
  2581.   if (is_array($label)){
  2582.     foreach ($label as $l=>$v){
  2583.       $this->o_info($this->infoObject,$l,$v);
  2584.     }
  2585.   else {
  2586.     $this->o_info($this->infoObject,$label,$value);
  2587.   }
  2588. }
  2589.  
  2590. /**
  2591. * set the viewer preferences of the document, it is up to the browser to obey these.
  2592. */
  2593. function setPreferences($label,$value=0){
  2594.   // this will only work if the label is one of the valid ones.
  2595.   if (is_array($label)){
  2596.     foreach ($label as $l=>$v){
  2597.       $this->o_catalog($this->catalogId,'viewerPreferences',array($l=>$v));
  2598.     }
  2599.   else {
  2600.     $this->o_catalog($this->catalogId,'viewerPreferences',array($label=>$value));
  2601.   }
  2602. }
  2603.  
  2604. /**
  2605. * extract an integer from a position in a byte stream
  2606. *
  2607. @access private
  2608. */
  2609. function PRVT_getBytes(&$data,$pos,$num){
  2610.   // return the integer represented by $num bytes from $pos within $data
  2611.   $ret=0;
  2612.   for ($i=0;$i<$num;$i++){
  2613.     $ret=$ret*256;
  2614.     $ret+=ord($data[$pos+$i]);
  2615.   }
  2616.   return $ret;
  2617. }
  2618.  
  2619. /**
  2620. * add a PNG image into the document, from a file
  2621. * this should work with remote files
  2622. */
  2623. function addPngFromFile($file,$x,$y,$w=0,$h=0){
  2624.   // read in a png file, interpret it, then add to the system
  2625.   $error=0;
  2626.   $tmp get_magic_quotes_runtime();
  2627.   $fp @fopen($file,'rb');
  2628.   if ($fp){
  2629.     $data='';
  2630.     while(!feof($fp)){
  2631.       $data .= fread($fp,1024);
  2632.     }
  2633.     fclose($fp);
  2634.   else {
  2635.     $error 1;
  2636.     $errormsg 'trouble opening file: '.$file;
  2637.   }
  2638.   
  2639.   if (!$error){
  2640.     $header chr(137).chr(80).chr(78).chr(71).chr(13).chr(10).chr(26).chr(10);
  2641.     if (substr($data,0,8)!=$header){
  2642.       $error=1;
  2643.       $errormsg 'this file does not have a valid header';
  2644.     }
  2645.   }
  2646.  
  2647.   if (!$error){
  2648.     // set pointer
  2649.     $p 8;
  2650.     $len strlen($data);
  2651.     // cycle through the file, identifying chunks
  2652.     $haveHeader=0;
  2653.     $info=array();
  2654.     $idata='';
  2655.     $pdata='';
  2656.     while ($p<$len){
  2657.       $chunkLen $this->PRVT_getBytes($data,$p,4);
  2658.       $chunkType substr($data,$p+4,4);
  2659. //      echo $chunkType.' - '.$chunkLen.'<br>';
  2660.     
  2661.       switch($chunkType){
  2662.         case 'IHDR':
  2663.           // this is where all the file information comes from
  2664.           $info['width']=$this->PRVT_getBytes($data,$p+8,4);
  2665.           $info['height']=$this->PRVT_getBytes($data,$p+12,4);
  2666.           $info['bitDepth']=ord($data[$p+16]);
  2667.           $info['colorType']=ord($data[$p+17]);
  2668.           $info['compressionMethod']=ord($data[$p+18]);
  2669.           $info['filterMethod']=ord($data[$p+19]);
  2670.           $info['interlaceMethod']=ord($data[$p+20]);
  2671. //print_r($info);
  2672.           $haveHeader=1;
  2673.           if ($info['compressionMethod']!=0){
  2674.             $error=1;
  2675.             $errormsg 'unsupported compression method';
  2676.           }
  2677.           if ($info['filterMethod']!=0){
  2678.             $error=1;
  2679.             $errormsg 'unsupported filter method';
  2680.           }
  2681.           break;
  2682.         case 'PLTE':
  2683.           $pdata.=substr($data,$p+8,$chunkLen);
  2684.           break;
  2685.         case 'IDAT':
  2686.           $idata.=substr($data,$p+8,$chunkLen);
  2687.           break;
  2688.         case 'tRNS'
  2689.           //this chunk can only occur once and it must occur after the PLTE chunk and before IDAT chunk 
  2690.           //print "tRNS found, color type = ".$info['colorType']."<BR>"; 
  2691.           $transparency array();
  2692.           if ($info['colorType'== 3// indexed color, rbg 
  2693.           /* corresponding to entries in the plte chunk 
  2694.           Alpha for palette index 0: 1 byte 
  2695.           Alpha for palette index 1: 1 byte 
  2696.           ...etc... 
  2697.           */ 
  2698.             // there will be one entry for each palette entry. up until the last non-opaque entry.
  2699.             // set up an array, stretching over all palette entries which will be o (opaque) or 1 (transparent)
  2700.             $transparency['type']='indexed';
  2701.             $numPalette strlen($pdata)/3;
  2702.             $trans=0;
  2703.             for ($i=$chunkLen;$i>=0;$i--){
  2704.               if (ord($data[$p+8+$i])==0){
  2705.                 $trans=$i;
  2706.               }
  2707.             }
  2708.             $transparency['data'$trans;
  2709.             
  2710.           elseif($info['colorType'== 0// grayscale 
  2711.           /* corresponding to entries in the plte chunk 
  2712.           Gray: 2 bytes, range 0 .. (2^bitdepth)-1 
  2713.           */ 
  2714. //            $transparency['grayscale']=$this->PRVT_getBytes($data,$p+8,2); // g = grayscale 
  2715.             $transparency['type']='indexed';
  2716.             $transparency['data'ord($data[$p+8+1]);
  2717.           
  2718.           elseif($info['colorType'== 2// truecolor 
  2719.           /* corresponding to entries in the plte chunk 
  2720.           Red: 2 bytes, range 0 .. (2^bitdepth)-1 
  2721.           Green: 2 bytes, range 0 .. (2^bitdepth)-1 
  2722.           Blue: 2 bytes, range 0 .. (2^bitdepth)-1 
  2723.           */ 
  2724.             $transparency['r']=$this->PRVT_getBytes($data,$p+8,2)// r from truecolor 
  2725.             $transparency['g']=$this->PRVT_getBytes($data,$p+10,2)// g from truecolor 
  2726.             $transparency['b']=$this->PRVT_getBytes($data,$p+12,2)// b from truecolor 
  2727.           
  2728.           else 
  2729.           //unsupported transparency type 
  2730.           
  2731.           // KS End new code 
  2732.           break
  2733.         default:
  2734.           break;
  2735.       }
  2736.     
  2737.       $p += $chunkLen+12;
  2738.     }
  2739.     
  2740.     if(!$haveHeader){
  2741.       $error 1;
  2742.       $errormsg 'information header is missing';
  2743.     }
  2744.     if (isset($info['interlaceMethod']&& $info['interlaceMethod']){
  2745.       $error 1;
  2746.       $errormsg 'There appears to be no support for interlaced images in pdf.';
  2747.     }
  2748.   }
  2749.  
  2750.   if (!$error && $info['bitDepth'8){
  2751.     $error 1;
  2752.     $errormsg 'only bit depth of 8 or less is supported';
  2753.   }
  2754.  
  2755.   if (!$error){
  2756.     if ($info['colorType']!=&& $info['colorType']!=&& $info['colorType']!=3){
  2757.       $error 1;
  2758.       $errormsg 'transparancey alpha channel not supported, transparency only supported for palette images.';
  2759.     else {
  2760.       switch ($info['colorType']){
  2761.         case 3:
  2762.           $color 'DeviceRGB';
  2763.           $ncolor=1;
  2764.           break;
  2765.         case 2:
  2766.           $color 'DeviceRGB';
  2767.           $ncolor=3;
  2768.           break;
  2769.         case 0:
  2770.           $color 'DeviceGray';
  2771.           $ncolor=1;
  2772.           break;
  2773.       }
  2774.     }
  2775.   }
  2776.   if ($error){
  2777.     $this->addMessage('PNG error - ('.$file.') '.$errormsg);
  2778.     return;
  2779.   }
  2780.   if ($w==0){
  2781.     $w=$h/$info['height']*$info['width'];
  2782.   }
  2783.   if ($h==0){
  2784.     $h=$w*$info['height']/$info['width'];
  2785.   }
  2786. //print_r($info);
  2787.   // so this image is ok... add it in.
  2788.   $this->numImages++;
  2789.   $im=$this->numImages;
  2790.   $label='I'.$im;
  2791.   $this->numObj++;
  2792. //  $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$idata,'iw'=>$w,'ih'=>$h,'type'=>'png','ic'=>$info['width']));
  2793.   $options array('label'=>$label,'data'=>$idata,'bitsPerComponent'=>$info['bitDepth'],'pdata'=>$pdata
  2794.                                       ,'iw'=>$info['width'],'ih'=>$info['height'],'type'=>'png','color'=>$color,'ncolor'=>$ncolor);
  2795.   if (isset($transparency)){
  2796.     $options['transparency']=$transparency;
  2797.   }
  2798.   $this->o_image($this->numObj,'new',$options);
  2799.  
  2800.   $this->objects[$this->currentContents]['c'].="\nq";
  2801.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2802.   $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2803.   $this->objects[$this->currentContents]['c'].="\nQ";
  2804. }
  2805.  
  2806. /**
  2807. * add a JPEG image into the document, from a file
  2808. */
  2809. function addJpegFromFile($img,$x,$y,$w=0,$h=0){
  2810.   // attempt to add a jpeg image straight from a file, using no GD commands
  2811.   // note that this function is unable to operate on a remote file.
  2812.  
  2813.   if (!file_exists($img)){
  2814.     return;
  2815.   }
  2816.  
  2817.   $tmp=getimagesize($img);
  2818.   $imageWidth=$tmp[0];
  2819.   $imageHeight=$tmp[1];
  2820.  
  2821.   if (isset($tmp['channels'])){
  2822.     $channels $tmp['channels'];
  2823.   else {
  2824.     $channels 3;
  2825.   }
  2826.  
  2827.   if ($w<=&& $h<=0){
  2828.     $w=$imageWidth;
  2829.   }
  2830.   if ($w==0){
  2831.     $w=$h/$imageHeight*$imageWidth;
  2832.   }
  2833.   if ($h==0){
  2834.     $h=$w*$imageHeight/$imageWidth;
  2835.   }
  2836.  
  2837.   $fp=fopen($img,'rb');
  2838.  
  2839.   $tmp get_magic_quotes_runtime();
  2840.   $data fread($fp,filesize($img));
  2841.   
  2842.   fclose($fp);
  2843.  
  2844.   $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight,$channels);
  2845. }
  2846.  
  2847. /**
  2848. * add an image into the document, from a GD object
  2849. * this function is not all that reliable, and I would probably encourage people to use
  2850. * the file based functions
  2851. */
  2852. function addImage(&$img,$x,$y,$w=0,$h=0,$quality=75){
  2853.   // add a new image into the current location, as an external object
  2854.   // add the image at $x,$y, and with width and height as defined by $w & $h
  2855.   
  2856.   // note that this will only work with full colour images and makes them jpg images for display
  2857.   // later versions could present lossless image formats if there is interest.
  2858.   
  2859.   // there seems to be some problem here in that images that have quality set above 75 do not appear
  2860.   // not too sure why this is, but in the meantime I have restricted this to 75.  
  2861.   if ($quality>75){
  2862.     $quality=75;
  2863.   }
  2864.  
  2865.   // if the width or height are set to zero, then set the other one based on keeping the image
  2866.   // height/width ratio the same, if they are both zero, then give up :)
  2867.   $imageWidth=imagesx($img);
  2868.   $imageHeight=imagesy($img);
  2869.   
  2870.   if ($w<=&& $h<=0){
  2871.     return;
  2872.   }
  2873.   if ($w==0){
  2874.     $w=$h/$imageHeight*$imageWidth;
  2875.   }
  2876.   if ($h==0){
  2877.     $h=$w*$imageHeight/$imageWidth;
  2878.   }
  2879.   
  2880.   // gotta get the data out of the img..
  2881.  
  2882.   // so I write to a temp file, and then read it back.. soo ugly, my apologies.
  2883.   $tmpDir='/tmp';
  2884.   $tmpName=tempnam($tmpDir,'img');
  2885.   imagejpeg($img,$tmpName,$quality);
  2886.   $fp=fopen($tmpName,'rb');
  2887.  
  2888.   $tmp get_magic_quotes_runtime();
  2889.   $fp @fopen($tmpName,'rb');
  2890.   if ($fp){
  2891.     $data='';
  2892.     while(!feof($fp)){
  2893.       $data .= fread($fp,1024);
  2894.     }
  2895.     fclose($fp);
  2896.   else {
  2897.     $error 1;
  2898.     $errormsg 'trouble opening file';
  2899.   }
  2900. //  $data = fread($fp,filesize($tmpName));
  2901. //  fclose($fp);
  2902.   unlink($tmpName);
  2903.   $this->addJpegImage_common($data,$x,$y,$w,$h,$imageWidth,$imageHeight);
  2904. }
  2905.  
  2906. /**
  2907. * common code used by the two JPEG adding functions
  2908. *
  2909. @access private
  2910. */
  2911. function addJpegImage_common(&$data,$x,$y,$w=0,$h=0,$imageWidth,$imageHeight,$channels=3){
  2912.   // note that this function is not to be called externally
  2913.   // it is just the common code between the GD and the file options
  2914.   $this->numImages++;
  2915.   $im=$this->numImages;
  2916.   $label='I'.$im;
  2917.   $this->numObj++;
  2918.   $this->o_image($this->numObj,'new',array('label'=>$label,'data'=>$data,'iw'=>$imageWidth,'ih'=>$imageHeight,'channels'=>$channels));
  2919.  
  2920.   $this->objects[$this->currentContents]['c'].="\nq";
  2921.   $this->objects[$this->currentContents]['c'].="\n".sprintf('%.3f',$w)." 0 0 ".sprintf('%.3f',$h)." ".sprintf('%.3f',$x)." ".sprintf('%.3f',$y)." cm";
  2922.   $this->objects[$this->currentContents]['c'].="\n/".$label.' Do';
  2923.   $this->objects[$this->currentContents]['c'].="\nQ";
  2924. }
  2925.  
  2926. /**
  2927. * specify where the document should open when it first starts
  2928. */
  2929. function openHere($style,$a=0,$b=0,$c=0){
  2930.   // this function will open the document at a specified page, in a specified style
  2931.   // the values for style, and the required paramters are:
  2932.   // 'XYZ'  left, top, zoom
  2933.   // 'Fit'
  2934.   // 'FitH' top
  2935.   // 'FitV' left
  2936.   // 'FitR' left,bottom,right
  2937.   // 'FitB'
  2938.   // 'FitBH' top
  2939.   // 'FitBV' left
  2940.   $this->numObj++;
  2941.   $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2942.   $id $this->catalogId;
  2943.   $this->o_catalog($id,'openHere',$this->numObj);
  2944. }
  2945.  
  2946. /**
  2947. * create a labelled destination within the document
  2948. */
  2949. function addDestination($label,$style,$a=0,$b=0,$c=0){
  2950.   // associates the given label with the destination, it is done this way so that a destination can be specified after
  2951.   // it has been linked to
  2952.   // styles are the same as the 'openHere' function
  2953.   $this->numObj++;
  2954.   $this->o_destination($this->numObj,'new',array('page'=>$this->currentPage,'type'=>$style,'p1'=>$a,'p2'=>$b,'p3'=>$c));
  2955.   $id $this->numObj;
  2956.   // store the label->idf relationship, note that this means that labels can be used only once
  2957.   $this->destinations["$label"]=$id;
  2958. }
  2959.  
  2960. /**
  2961. * define font families, this is used to initialize the font families for the default fonts
  2962. * and for the user to add new ones for their fonts. The default bahavious can be overridden should
  2963. * that be desired.
  2964. */
  2965. function setFontFamily($family,$options=''){
  2966.   if (!is_array($options)){
  2967.     if ($family=='init'){
  2968.       // set the known family groups
  2969.       // these font families will be used to enable bold and italic markers to be included
  2970.       // within text streams. html forms will be used... <b></b> <i></i>
  2971.       $this->fontFamilies['Helvetica.afm']=array(
  2972.          'b'=>'Helvetica-Bold.afm'
  2973.         ,'i'=>'Helvetica-Oblique.afm'
  2974.         ,'bi'=>'Helvetica-BoldOblique.afm'
  2975.         ,'ib'=>'Helvetica-BoldOblique.afm'
  2976.       );
  2977.       $this->fontFamilies['Courier.afm']=array(
  2978.          'b'=>'Courier-Bold.afm'
  2979.         ,'i'=>'Courier-Oblique.afm'
  2980.         ,'bi'=>'Courier-BoldOblique.afm'
  2981.         ,'ib'=>'Courier-BoldOblique.afm'
  2982.       );
  2983.       $this->fontFamilies['Times-Roman.afm']=array(
  2984.          'b'=>'Times-Bold.afm'
  2985.         ,'i'=>'Times-Italic.afm'
  2986.         ,'bi'=>'Times-BoldItalic.afm'
  2987.         ,'ib'=>'Times-BoldItalic.afm'
  2988.       );
  2989.     }
  2990.   else {
  2991.     // the user is trying to set a font family
  2992.     // note that this can also be used to set the base ones to something else
  2993.     if (strlen($family)){
  2994.       $this->fontFamilies[$family$options;
  2995.     }
  2996.   }
  2997. }
  2998.  
  2999. /**
  3000. * used to add messages for use in debugging
  3001. */
  3002. function addMessage($message){
  3003.   $this->messages.=$message."\n";
  3004. }
  3005.  
  3006. /**
  3007. * a few functions which should allow the document to be treated transactionally.
  3008. */
  3009. function transaction($action){
  3010.   switch ($action){
  3011.     case 'start':
  3012.       // store all the data away into the checkpoint variable
  3013.       $data get_object_vars($this);
  3014.       $this->checkpoint = $data;
  3015.       unset($data);
  3016.       break;
  3017.     case 'commit':
  3018.       if (is_array($this->checkpoint&& isset($this->checkpoint['checkpoint'])){
  3019.         $tmp $this->checkpoint['checkpoint'];
  3020.         $this->checkpoint = $tmp;
  3021.         unset($tmp);
  3022.       else {
  3023.         $this->checkpoint='';
  3024.       }
  3025.       break;
  3026.     case 'rewind':
  3027.       // do not destroy the current checkpoint, but move us back to the state then, so that we can try again
  3028.       if (is_array($this->checkpoint)){
  3029.         // can only abort if were inside a checkpoint
  3030.         $tmp $this->checkpoint;
  3031.         foreach ($tmp as $k=>$v){
  3032.           if ($k != 'checkpoint'){
  3033.             $this->$k=$v;
  3034.           }
  3035.         }
  3036.         unset($tmp);
  3037.       }
  3038.       break;
  3039.     case 'abort':
  3040.       if (is_array($this->checkpoint)){
  3041.         // can only abort if were inside a checkpoint
  3042.         $tmp $this->checkpoint;
  3043.         foreach ($tmp as $k=>$v){
  3044.           $this->$k=$v;
  3045.         }
  3046.         unset($tmp);
  3047.       }
  3048.       break;
  3049.   }
  3050.  
  3051. }
  3052.  
  3053. // end of class
  3054.  
  3055. ?>

Documentation generated on Tue, 24 Oct 2006 09:21:05 -0500 by phpDocumentor 1.3.1