home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / class.ezpdf.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  55.5 KB  |  1,571 lines

  1. <?php
  2. /**
  3.  * @package Cpdf
  4.  */
  5. /**
  6.  * Cpdf class
  7.  */
  8. include_once('phpDocumentor/Converters/PDF/default/class.pdf.php');
  9.  
  10. /**
  11.  * This class will take the basic interaction facilities of the Cpdf class
  12.  * and make more useful functions so that the user does not have to 
  13.  * know all the ins and outs of pdf presentation to produce something pretty.
  14.  *
  15.  * IMPORTANT NOTE
  16.  * there is no warranty, implied or otherwise with this software.
  17.  * 
  18.  * @version 009 (versioning is linked to class.pdf.php)
  19.  * released under a public domain licence.
  20.  * @author Wayne Munro, R&OS Ltd, {@link http://www.ros.co.nz/pdf}
  21.  * @package Cpdf
  22.  */
  23. class Cezpdf extends Cpdf {
  24. //==============================================================================
  25. // this class will take the basic interaction facilities of the Cpdf class
  26. // and make more useful functions so that the user does not have to 
  27. // know all the ins and outs of pdf presentation to produce something pretty.
  28. //
  29. // IMPORTANT NOTE
  30. // there is no warranty, implied or otherwise with this software.
  31. // 
  32. // version 009 (versioning is linked to class.pdf.php)
  33. //
  34. // released under a public domain licence.
  35. //
  36. // Wayne Munro, R&OS Ltd, http://www.ros.co.nz/pdf
  37. //==============================================================================
  38.  
  39. var $ez=array('fontSize'=>10); // used for storing most of the page configuration parameters
  40. var $y; // this is the current vertical positon on the page of the writing point, very important
  41. var $ezPages=array(); // keep an array of the ids of the pages, making it easy to go back and add page numbers etc.
  42. var $ezPageCount=0;
  43.  
  44. // ------------------------------------------------------------------------------
  45.  
  46. function Cezpdf($paper='a4',$orientation='portrait'){
  47.     // Assuming that people don't want to specify the paper size using the absolute coordinates
  48.     // allow a couple of options:
  49.     // orientation can be 'portrait' or 'landscape'
  50.     // or, to actually set the coordinates, then pass an array in as the first parameter.
  51.     // the defaults are as shown.
  52.     // 
  53.     // -------------------------
  54.     // 2002-07-24 - Nicola Asuni (info@tecnick.com):
  55.     // Added new page formats (45 standard ISO paper formats and 4 american common formats)
  56.     // paper cordinates are calculated in this way: (inches * 72) where 1 inch = 2.54 cm
  57.     // 
  58.     // Now you may also pass a 2 values array containing the page width and height in centimeters
  59.     // -------------------------
  60.  
  61.     if (!is_array($paper)){
  62.         switch (strtoupper($paper)){
  63.             case '4A0': {$size = array(0,0,4767.87,6740.79); break;}
  64.             case '2A0': {$size = array(0,0,3370.39,4767.87); break;}
  65.             case 'A0': {$size = array(0,0,2383.94,3370.39); break;}
  66.             case 'A1': {$size = array(0,0,1683.78,2383.94); break;}
  67.             case 'A2': {$size = array(0,0,1190.55,1683.78); break;}
  68.             case 'A3': {$size = array(0,0,841.89,1190.55); break;}
  69.             case 'A4': default: {$size = array(0,0,595.28,841.89); break;}
  70.             case 'A5': {$size = array(0,0,419.53,595.28); break;}
  71.             case 'A6': {$size = array(0,0,297.64,419.53); break;}
  72.             case 'A7': {$size = array(0,0,209.76,297.64); break;}
  73.             case 'A8': {$size = array(0,0,147.40,209.76); break;}
  74.             case 'A9': {$size = array(0,0,104.88,147.40); break;}
  75.             case 'A10': {$size = array(0,0,73.70,104.88); break;}
  76.             case 'B0': {$size = array(0,0,2834.65,4008.19); break;}
  77.             case 'B1': {$size = array(0,0,2004.09,2834.65); break;}
  78.             case 'B2': {$size = array(0,0,1417.32,2004.09); break;}
  79.             case 'B3': {$size = array(0,0,1000.63,1417.32); break;}
  80.             case 'B4': {$size = array(0,0,708.66,1000.63); break;}
  81.             case 'B5': {$size = array(0,0,498.90,708.66); break;}
  82.             case 'B6': {$size = array(0,0,354.33,498.90); break;}
  83.             case 'B7': {$size = array(0,0,249.45,354.33); break;}
  84.             case 'B8': {$size = array(0,0,175.75,249.45); break;}
  85.             case 'B9': {$size = array(0,0,124.72,175.75); break;}
  86.             case 'B10': {$size = array(0,0,87.87,124.72); break;}
  87.             case 'C0': {$size = array(0,0,2599.37,3676.54); break;}
  88.             case 'C1': {$size = array(0,0,1836.85,2599.37); break;}
  89.             case 'C2': {$size = array(0,0,1298.27,1836.85); break;}
  90.             case 'C3': {$size = array(0,0,918.43,1298.27); break;}
  91.             case 'C4': {$size = array(0,0,649.13,918.43); break;}
  92.             case 'C5': {$size = array(0,0,459.21,649.13); break;}
  93.             case 'C6': {$size = array(0,0,323.15,459.21); break;}
  94.             case 'C7': {$size = array(0,0,229.61,323.15); break;}
  95.             case 'C8': {$size = array(0,0,161.57,229.61); break;}
  96.             case 'C9': {$size = array(0,0,113.39,161.57); break;}
  97.             case 'C10': {$size = array(0,0,79.37,113.39); break;}
  98.             case 'RA0': {$size = array(0,0,2437.80,3458.27); break;}
  99.             case 'RA1': {$size = array(0,0,1729.13,2437.80); break;}
  100.             case 'RA2': {$size = array(0,0,1218.90,1729.13); break;}
  101.             case 'RA3': {$size = array(0,0,864.57,1218.90); break;}
  102.             case 'RA4': {$size = array(0,0,609.45,864.57); break;}
  103.             case 'SRA0': {$size = array(0,0,2551.18,3628.35); break;}
  104.             case 'SRA1': {$size = array(0,0,1814.17,2551.18); break;}
  105.             case 'SRA2': {$size = array(0,0,1275.59,1814.17); break;}
  106.             case 'SRA3': {$size = array(0,0,907.09,1275.59); break;}
  107.             case 'SRA4': {$size = array(0,0,637.80,907.09); break;}
  108.             case 'LETTER': {$size = array(0,0,612.00,792.00); break;}
  109.             case 'LEGAL': {$size = array(0,0,612.00,1008.00); break;}
  110.             case 'EXECUTIVE': {$size = array(0,0,521.86,756.00); break;}
  111.             case 'FOLIO': {$size = array(0,0,612.00,936.00); break;}
  112.         }
  113.         switch (strtolower($orientation)){
  114.             case 'landscape':
  115.                 $a=$size[3];
  116.                 $size[3]=$size[2];
  117.                 $size[2]=$a;
  118.                 break;
  119.         }
  120.     } else {
  121.         if (count($paper)>2) {
  122.             // then an array was sent it to set the size
  123.             $size = $paper;
  124.         }
  125.         else { //size in centimeters has been passed
  126.             $size[0] = 0;
  127.             $size[1] = 0;
  128.             $size[2] = ( $paper[0] / 2.54 ) * 72;
  129.             $size[3] = ( $paper[1] / 2.54 ) * 72;
  130.         }
  131.     }
  132.     $this->Cpdf($size);
  133.     $this->ez['pageWidth']=$size[2];
  134.     $this->ez['pageHeight']=$size[3];
  135.     
  136.     // also set the margins to some reasonable defaults
  137.     $this->ez['topMargin']=30;
  138.     $this->ez['bottomMargin']=30;
  139.     $this->ez['leftMargin']=30;
  140.     $this->ez['rightMargin']=30;
  141.     
  142.     // set the current writing position to the top of the first page
  143.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  144.     // and get the ID of the page that was created during the instancing process.
  145.     $this->ezPages[1]=$this->getFirstPageId();
  146.     $this->ezPageCount=1;
  147. }
  148.  
  149. // ------------------------------------------------------------------------------
  150. // 2002-07-24: Nicola Asuni (info@tecnick.com)
  151. // Set Margins in centimeters
  152. function ezSetCmMargins($top,$bottom,$left,$right){
  153.     $top = ( $top / 2.54 ) * 72;
  154.     $bottom = ( $bottom / 2.54 ) * 72;
  155.     $left = ( $left / 2.54 ) * 72;
  156.     $right = ( $right / 2.54 ) * 72;
  157.     $this->ezSetMargins($top,$bottom,$left,$right);
  158. }
  159. // ------------------------------------------------------------------------------
  160.  
  161.  
  162. function ezColumnsStart($options=array()){
  163.   // start from the current y-position, make the set number of columne
  164.   if (isset($this->ez['columns']) && $this->ez['columns']==1){
  165.     // if we are already in a column mode then just return.
  166.     return;
  167.   }
  168.   $def=array('gap'=>10,'num'=>2);
  169.   foreach($def as $k=>$v){
  170.     if (!isset($options[$k])){
  171.       $options[$k]=$v;
  172.     }
  173.   }
  174.   // setup the columns
  175.   $this->ez['columns']=array('on'=>1,'colNum'=>1);
  176.  
  177.   // store the current margins
  178.   $this->ez['columns']['margins']=array(
  179.      $this->ez['leftMargin']
  180.     ,$this->ez['rightMargin']
  181.     ,$this->ez['topMargin']
  182.     ,$this->ez['bottomMargin']
  183.   );
  184.   // and store the settings for the columns
  185.   $this->ez['columns']['options']=$options;
  186.   // then reset the margins to suit the new columns
  187.   // safe enough to assume the first column here, but start from the current y-position
  188.   $this->ez['topMargin']=$this->ez['pageHeight']-$this->y;
  189.   $width=($this->ez['pageWidth']-$this->ez['leftMargin']-$this->ez['rightMargin']-($options['num']-1)*$options['gap'])/$options['num'];
  190.   $this->ez['columns']['width']=$width;
  191.   $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  192.   
  193. }
  194. // ------------------------------------------------------------------------------
  195. function ezColumnsStop(){
  196.   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  197.     $this->ez['columns']['on']=0;
  198.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0];
  199.     $this->ez['rightMargin']=$this->ez['columns']['margins'][1];
  200.     $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  201.     $this->ez['bottomMargin']=$this->ez['columns']['margins'][3];
  202.   }
  203. }
  204. // ------------------------------------------------------------------------------
  205. function ezInsertMode($status=1,$pageNum=1,$pos='before'){
  206.   // puts the document into insert mode. new pages are inserted until this is re-called with status=0
  207.   // by default pages wil be inserted at the start of the document
  208.   switch($status){
  209.     case '1':
  210.       if (isset($this->ezPages[$pageNum])){
  211.         $this->ez['insertMode']=1;
  212.         $this->ez['insertOptions']=array('id'=>$this->ezPages[$pageNum],'pos'=>$pos);
  213.       }
  214.       break;
  215.     case '0':
  216.       $this->ez['insertMode']=0;
  217.       break;
  218.   }
  219. }
  220. // ------------------------------------------------------------------------------
  221.  
  222. function ezNewPage(){
  223.   $pageRequired=1;
  224.   if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  225.     // check if this is just going to a new column
  226.     // increment the column number
  227. //echo 'HERE<br>';
  228.     $this->ez['columns']['colNum']++;
  229. //echo $this->ez['columns']['colNum'].'<br>';
  230.     if ($this->ez['columns']['colNum'] <= $this->ez['columns']['options']['num']){
  231.       // then just reset to the top of the next column
  232.       $pageRequired=0;
  233.     } else {
  234.       $this->ez['columns']['colNum']=1;
  235.       $this->ez['topMargin']=$this->ez['columns']['margins'][2];
  236.     }
  237.  
  238.     $width = $this->ez['columns']['width'];
  239.     $this->ez['leftMargin']=$this->ez['columns']['margins'][0]+($this->ez['columns']['colNum']-1)*($this->ez['columns']['options']['gap']+$width);
  240.     $this->ez['rightMargin']=$this->ez['pageWidth']-$this->ez['leftMargin']-$width;
  241.   }
  242. //echo 'left='.$this->ez['leftMargin'].'   right='.$this->ez['rightMargin'].'<br>';
  243.  
  244.   if ($pageRequired){
  245.     // make a new page, setting the writing point back to the top
  246.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  247.     // make the new page with a call to the basic class.
  248.     $this->ezPageCount++;
  249.     if (isset($this->ez['insertMode']) && $this->ez['insertMode']==1){
  250.       $id = $this->ezPages[$this->ezPageCount] = $this->newPage(1,$this->ez['insertOptions']['id'],$this->ez['insertOptions']['pos']);
  251.       // then manipulate the insert options so that inserted pages follow each other
  252.       $this->ez['insertOptions']['id']=$id;
  253.       $this->ez['insertOptions']['pos']='after';
  254.     } else {
  255.       $this->ezPages[$this->ezPageCount] = $this->newPage();
  256.     }
  257.   } else {
  258.     $this->y = $this->ez['pageHeight']-$this->ez['topMargin'];
  259.   }
  260. }
  261.  
  262. // ------------------------------------------------------------------------------
  263.  
  264. function ezSetMargins($top,$bottom,$left,$right){
  265.   // sets the margins to new values
  266.   $this->ez['topMargin']=$top;
  267.   $this->ez['bottomMargin']=$bottom;
  268.   $this->ez['leftMargin']=$left;
  269.   $this->ez['rightMargin']=$right;
  270.   // check to see if this means that the current writing position is outside the 
  271.   // writable area
  272.   if ($this->y > $this->ez['pageHeight']-$top){
  273.     // then move y down
  274.     $this->y = $this->ez['pageHeight']-$top;
  275.   }
  276.   if ( $this->y < $bottom){
  277.     // then make a new page
  278.     $this->ezNewPage();
  279.   }
  280. }  
  281.  
  282. // ------------------------------------------------------------------------------
  283.  
  284. function ezGetCurrentPageNumber(){
  285.   // return the strict numbering (1,2,3,4..) number of the current page
  286.   return $this->ezPageCount;
  287. }
  288.  
  289. // ------------------------------------------------------------------------------
  290.  
  291. function ezStartPageNumbers($x,$y,$size,$pos='left',$pattern='{PAGENUM} of {TOTALPAGENUM}',$num=''){
  292.   // put page numbers on the pages from here.
  293.   // place then on the 'pos' side of the coordinates (x,y).
  294.   // pos can be 'left' or 'right'
  295.   // use the given 'pattern' for display, where (PAGENUM} and {TOTALPAGENUM} are replaced
  296.   // as required.
  297.   // if $num is set, then make the first page this number, the number of total pages will
  298.   // be adjusted to account for this.
  299.   // Adjust this function so that each time you 'start' page numbers then you effectively start a different batch
  300.   // return the number of the batch, so that they can be stopped in a different order if required.
  301.   if (!$pos || !strlen($pos)){
  302.     $pos='left';
  303.   }
  304.   if (!$pattern || !strlen($pattern)){
  305.     $pattern='{PAGENUM} of {TOTALPAGENUM}';
  306.   }
  307.   if (!isset($this->ez['pageNumbering'])){
  308.     $this->ez['pageNumbering']=array();
  309.   }
  310.   $i = count($this->ez['pageNumbering']);
  311.   $this->ez['pageNumbering'][$i][$this->ezPageCount]=array('x'=>$x,'y'=>$y,'pos'=>$pos,'pattern'=>$pattern,'num'=>$num,'size'=>$size);
  312.   return $i;
  313. }
  314.  
  315. // ------------------------------------------------------------------------------
  316.  
  317. function ezWhatPageNumber($pageNum,$i=0){
  318.   // given a particular generic page number (ie, document numbered sequentially from beginning),
  319.   // return the page number under a particular page numbering scheme ($i)
  320.   $num=0;
  321.   $start=1;
  322.   $startNum=1;
  323.   if (!isset($this->ez['pageNumbering']))
  324.   {
  325.     $this->addMessage('WARNING: page numbering called for and wasn\'t started with ezStartPageNumbers');
  326.     return 0;
  327.   }
  328.   foreach($this->ez['pageNumbering'][$i] as $k=>$v){
  329.     if ($k<=$pageNum){
  330.       if (is_array($v)){
  331.         // start block
  332.         if (strlen($v['num'])){
  333.           // a start was specified
  334.           $start=$v['num'];
  335.           $startNum=$k;
  336.           $num=$pageNum-$startNum+$start;
  337.         }
  338.       } else {
  339.         // stop block
  340.         $num=0;
  341.       }
  342.     }
  343.   }
  344.   return $num;
  345. }
  346.  
  347. // ------------------------------------------------------------------------------
  348.  
  349. function ezStopPageNumbers($stopTotal=0,$next=0,$i=0){
  350.   // if stopTotal=1 then the totalling of pages for this number will stop too
  351.   // if $next=1, then do this page, but not the next, else do not do this page either
  352.   // if $i is set, then stop that particular pagenumbering sequence.
  353.   if (!isset($this->ez['pageNumbering'])){
  354.     $this->ez['pageNumbering']=array();
  355.   }
  356.   if ($next && isset($this->ez['pageNumbering'][$i][$this->ezPageCount]) && is_array($this->ez['pageNumbering'][$i][$this->ezPageCount])){
  357.     // then this has only just been started, this will over-write the start, and nothing will appear
  358.     // add a special command to the start block, telling it to stop as well
  359.     if ($stopTotal){
  360.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stoptn']=1;
  361.     } else {
  362.       $this->ez['pageNumbering'][$i][$this->ezPageCount]['stopn']=1;
  363.     }
  364.   } else {
  365.     if ($stopTotal){
  366.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stopt';
  367.     } else {
  368.       $this->ez['pageNumbering'][$i][$this->ezPageCount]='stop';
  369.     }
  370.     if ($next){
  371.       $this->ez['pageNumbering'][$i][$this->ezPageCount].='n';
  372.     }
  373.   }
  374. }
  375.  
  376. // ------------------------------------------------------------------------------
  377.  
  378. function ezPRVTpageNumberSearch($lbl,&$tmp){
  379.   foreach($tmp as $i=>$v){
  380.     if (is_array($v)){
  381.       if (isset($v[$lbl])){
  382.         return $i;
  383.       }
  384.     } else {
  385.       if ($v==$lbl){
  386.         return $i;
  387.       }
  388.     }
  389.   }
  390.   return 0;
  391. }
  392.  
  393. // ------------------------------------------------------------------------------
  394.  
  395. function ezPRVTaddPageNumbers(){
  396.   // this will go through the pageNumbering array and add the page numbers are required
  397.   if (isset($this->ez['pageNumbering'])){
  398.     $totalPages1 = $this->ezPageCount;
  399.     $tmp1=$this->ez['pageNumbering'];
  400.     $status=0;
  401.     foreach($tmp1 as $i=>$tmp){
  402.       // do each of the page numbering systems
  403.       // firstly, find the total pages for this one
  404.       $k = $this->ezPRVTpageNumberSearch('stopt',$tmp);
  405.       if ($k && $k>0){
  406.         $totalPages = $k-1;
  407.       } else {
  408.         $l = $this->ezPRVTpageNumberSearch('stoptn',$tmp);
  409.         if ($l && $l>0){
  410.           $totalPages = $l;
  411.         } else {
  412.           $totalPages = $totalPages1;
  413.         }
  414.       }
  415.       foreach ($this->ezPages as $pageNum=>$id){
  416.         if (isset($tmp[$pageNum])){
  417.           if (is_array($tmp[$pageNum])){
  418.             // then this must be starting page numbers
  419.             $status=1;
  420.             $info = $tmp[$pageNum];
  421.             $info['dnum']=$info['num']-$pageNum;
  422.             // also check for the special case of the numbering stopping and starting on the same page
  423.             if (isset($info['stopn']) || isset($info['stoptn']) ){
  424.               $status=2;
  425.             }
  426.           } else if ($tmp[$pageNum]=='stop' || $tmp[$pageNum]=='stopt'){
  427.             // then we are stopping page numbers
  428.             $status=0;
  429.           } else if ($status==1 && ($tmp[$pageNum]=='stoptn' || $tmp[$pageNum]=='stopn')){
  430.             // then we are stopping page numbers
  431.             $status=2;
  432.           }
  433.         }
  434.         if ($status){
  435.           // then add the page numbering to this page
  436.           if (strlen($info['num'])){
  437.             $num=$pageNum+$info['dnum'];
  438.           } else {
  439.             $num=$pageNum;
  440.           }
  441.           $total = $totalPages+$num-$pageNum;
  442.           $pat = str_replace('{PAGENUM}',$num,$info['pattern']);
  443.           $pat = str_replace('{TOTALPAGENUM}',$total,$pat);
  444.           $this->reopenObject($id);
  445.           switch($info['pos']){
  446.             case 'right':
  447.               $this->addText($info['x'],$info['y'],$info['size'],$pat);
  448.               break;
  449.             default:
  450.               $w=$this->getTextWidth($info['size'],$pat);
  451.               $this->addText($info['x']-$w,$info['y'],$info['size'],$pat);
  452.               break;
  453.           }
  454.           $this->closeObject();
  455.         }
  456.         if ($status==2){
  457.           $status=0;
  458.         }
  459.       }
  460.     }
  461.   }
  462. }
  463.  
  464. // ------------------------------------------------------------------------------
  465.  
  466. function ezPRVTcleanUp(){
  467.   $this->ezPRVTaddPageNumbers();
  468. }
  469.  
  470. // ------------------------------------------------------------------------------
  471.  
  472. function ezStream($options=''){
  473.   $this->ezPRVTcleanUp();
  474.   $this->stream($options);
  475. }
  476.  
  477. // ------------------------------------------------------------------------------
  478.  
  479. function ezOutput($options=0){
  480.   $this->ezPRVTcleanUp();
  481.   return $this->output($options);
  482. }
  483.  
  484. // ------------------------------------------------------------------------------
  485.  
  486. function ezSetY($y){
  487.   // used to change the vertical position of the writing point.
  488.   $this->y = $y;
  489.   if ( $this->y < $this->ez['bottomMargin']){
  490.     // then make a new page
  491.     $this->ezNewPage();
  492.   }
  493. }
  494.  
  495. // ------------------------------------------------------------------------------
  496.  
  497. function ezSetDy($dy,$mod=''){
  498.   // used to change the vertical position of the writing point.
  499.   // changes up by a positive increment, so enter a negative number to go
  500.   // down the page
  501.   // if $mod is set to 'makeSpace' and a new page is forced, then the pointed will be moved 
  502.   // down on the new page, this will allow space to be reserved for graphics etc.
  503.   $this->y += $dy;
  504.   if ( $this->y < $this->ez['bottomMargin']){
  505.     // then make a new page
  506.     $this->ezNewPage();
  507.     if ($mod=='makeSpace'){
  508.       $this->y += $dy;
  509.     }
  510.   }
  511. }
  512.  
  513. // ------------------------------------------------------------------------------
  514.  
  515. function ezPrvtTableDrawLines($pos,$gap,$x0,$x1,$y0,$y1,$y2,$col,$inner,$outer,$opt=1){
  516.   $x0=1000;
  517.   $x1=0;
  518.   $this->setStrokeColor($col[0],$col[1],$col[2]);
  519.   $cnt=0;
  520.   $n = count($pos);
  521.   foreach($pos as $x){
  522.     $cnt++;
  523.     if ($cnt==1 || $cnt==$n){
  524.       $this->setLineStyle($outer);
  525.     } else {
  526.       $this->setLineStyle($inner);
  527.     }
  528.     $this->line($x-$gap/2,$y0,$x-$gap/2,$y2);
  529.     if ($x>$x1){ $x1=$x; };
  530.     if ($x<$x0){ $x0=$x; };
  531.   }
  532.   $this->setLineStyle($outer);
  533.   $this->line($x0-$gap/2-$outer/2,$y0,$x1-$gap/2+$outer/2,$y0);
  534.   // only do the second line if it is different to the first, AND each row does not have
  535.   // a line on it.
  536.   if ($y0!=$y1 && $opt<2){
  537.     $this->line($x0-$gap/2,$y1,$x1-$gap/2,$y1);
  538.   }
  539.   $this->line($x0-$gap/2-$outer/2,$y2,$x1-$gap/2+$outer/2,$y2);
  540. }
  541.  
  542. // ------------------------------------------------------------------------------
  543.  
  544. function ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$gap,$size,&$y,$optionsAll=array()){
  545.   // uses ezText to add the text, and returns the height taken by the largest heading
  546.   // this page will move the headings to a new page if they will not fit completely on this one
  547.   // transaction support will be used to implement this
  548.  
  549.   if (isset($optionsAll['cols'])){
  550.     $options = $optionsAll['cols'];
  551.   } else {
  552.     $options = array();
  553.   }
  554.   
  555.   $mx=0;
  556.   $startPage = $this->ezPageCount;
  557.   $secondGo=0;
  558.  
  559.   // $y is the position at which the top of the table should start, so the base
  560.   // of the first text, is $y-$height-$gap-$decender, but ezText starts by dropping $height
  561.   
  562.   // the return from this function is the total cell height, including gaps, and $y is adjusted
  563.   // to be the postion of the bottom line
  564.   
  565.   // begin the transaction
  566.   $this->transaction('start');
  567.   $ok=0;
  568. //  $y-=$gap-$decender;
  569.   $y-=$gap;
  570.   while ($ok==0){
  571.     foreach($cols as $colName=>$colHeading){
  572.       $this->ezSetY($y);
  573.       if (isset($options[$colName]) && isset($options[$colName]['justification'])){
  574.         $justification = $options[$colName]['justification'];
  575.       } else {
  576.         $justification = 'left';
  577.       }
  578.       $this->ezText($colHeading,$size,array('aleft'=> $pos[$colName],'aright'=>($maxWidth[$colName]+$pos[$colName]),'justification'=>$justification));
  579.       $dy = $y-$this->y;
  580.       if ($dy>$mx){
  581.         $mx=$dy;
  582.       }
  583.     }
  584.     $y = $y - $mx - $gap + $decender;
  585. //    $y -= $mx-$gap+$decender;
  586.     
  587.     // now, if this has moved to a new page, then abort the transaction, move to a new page, and put it there
  588.     // do not check on the second time around, to avoid an infinite loop
  589.     if ($this->ezPageCount != $startPage && $secondGo==0){
  590.       $this->transaction('rewind');
  591.       $this->ezNewPage();
  592.       $y = $this->y - $gap-$decender;
  593.       $ok=0;
  594.       $secondGo=1;
  595. //      $y = $store_y;
  596.       $mx=0;
  597.  
  598.     } else {
  599.       $this->transaction('commit');
  600.       $ok=1;
  601.     }
  602.   }
  603.  
  604.   return $mx+$gap*2-$decender;
  605. }
  606.  
  607. // ------------------------------------------------------------------------------
  608.  
  609. function ezPrvtGetTextWidth($size,$text){
  610.   // will calculate the maximum width, taking into account that the text may be broken
  611.   // by line breaks.
  612.   $mx=0;
  613.   $lines = explode("\n",$text);
  614.   foreach ($lines as $line){
  615.     $w = $this->getTextWidth($size,$line);
  616.     if ($w>$mx){
  617.       $mx=$w;
  618.     }
  619.   }
  620.   return $mx;
  621. }
  622.  
  623. // ------------------------------------------------------------------------------
  624.  
  625. function ezTable(&$data,$cols='',$title='',$options=''){
  626.   // add a table of information to the pdf document
  627.   // $data is a two dimensional array
  628.   // $cols (optional) is an associative array, the keys are the names of the columns from $data
  629.   // to be presented (and in that order), the values are the titles to be given to the columns
  630.   // $title (optional) is the title to be put on the top of the table
  631.   //
  632.   // $options is an associative array which can contain:
  633.   // 'showLines'=> 0,1,2, default is 1 (show outside and top lines only), 2=> lines on each row
  634.   // 'showHeadings' => 0 or 1
  635.   // 'shaded'=> 0,1,2,3 default is 1 (1->alternate lines are shaded, 0->no shading, 2-> both shaded, second uses shadeCol2)
  636.   // 'shadeCol' => (r,g,b) array, defining the colour of the shading, default is (0.8,0.8,0.8)
  637.   // 'shadeCol2' => (r,g,b) array, defining the colour of the shading of the other blocks, default is (0.7,0.7,0.7)
  638.   // 'fontSize' => 10
  639.   // 'textCol' => (r,g,b) array, text colour
  640.   // 'titleFontSize' => 12
  641.   // 'rowGap' => 2 , the space added at the top and bottom of each row, between the text and the lines
  642.   // 'colGap' => 5 , the space on the left and right sides of each cell
  643.   // 'lineCol' => (r,g,b) array, defining the colour of the lines, default, black.
  644.   // 'xPos' => 'left','right','center','centre',or coordinate, reference coordinate in the x-direction
  645.   // 'xOrientation' => 'left','right','center','centre', position of the table w.r.t 'xPos' 
  646.   // 'width'=> <number> which will specify the width of the table, if it turns out to not be this
  647.   //    wide, then it will stretch the table to fit, if it is wider then each cell will be made
  648.   //    proportionalty smaller, and the content may have to wrap.
  649.   // 'maxWidth'=> <number> similar to 'width', but will only make table smaller than it wants to be
  650.   // 'options' => array(<colname>=>array('justification'=>'left','width'=>100,'link'=>linkDataName),<colname>=>....)
  651.   //             allow the setting of other paramaters for the individual columns
  652.   // 'minRowSpace'=> the minimum space between the bottom of each row and the bottom margin, in which a new row will be started
  653.   //                  if it is less, then a new page would be started, default=-100
  654.   // 'innerLineThickness'=>1
  655.   // 'outerLineThickness'=>1
  656.   // 'splitRows'=>0, 0 or 1, whether or not to allow the rows to be split across page boundaries
  657.   // 'protectRows'=>number, the number of rows to hold with the heading on page, ie, if there less than this number of
  658.   //                rows on the page, then move the whole lot onto the next page, default=1
  659.   //
  660.   // note that the user will have had to make a font selection already or this will not 
  661.   // produce a valid pdf file.
  662.   
  663.   if (!is_array($data)){
  664.     return;
  665.   }
  666.   
  667.   if (!is_array($cols)){
  668.     // take the columns from the first row of the data set
  669.     reset($data);
  670.     list($k,$v)=each($data);
  671.     if (!is_array($v)){
  672.       return;
  673.     }
  674.     $cols=array();
  675.     foreach($v as $k1=>$v1){
  676.       $cols[$k1]=$k1;
  677.     }
  678.   }
  679.   
  680.   if (!is_array($options)){
  681.     $options=array();
  682.   }
  683.  
  684.   $defaults = array(
  685.     'shaded'=>1,'showLines'=>1,'shadeCol'=>array(0.8,0.8,0.8),'shadeCol2'=>array(0.7,0.7,0.7),'fontSize'=>10,'titleFontSize'=>12
  686.     ,'titleGap'=>5,'lineCol'=>array(0,0,0),'gap'=>5,'xPos'=>'centre','xOrientation'=>'centre'
  687.     ,'showHeadings'=>1,'textCol'=>array(0,0,0),'width'=>0,'maxWidth'=>0,'cols'=>array(),'minRowSpace'=>-100,'rowGap'=>2,'colGap'=>5
  688.     ,'innerLineThickness'=>1,'outerLineThickness'=>1,'splitRows'=>0,'protectRows'=>1
  689.     );
  690.  
  691.   foreach($defaults as $key=>$value){
  692.     if (is_array($value)){
  693.       if (!isset($options[$key]) || !is_array($options[$key])){
  694.         $options[$key]=$value;
  695.       }
  696.     } else {
  697.       if (!isset($options[$key])){
  698.         $options[$key]=$value;
  699.       }
  700.     }
  701.   }
  702.   $options['gap']=2*$options['colGap'];
  703.   
  704.   $middle = ($this->ez['pageWidth']-$this->ez['rightMargin'])/2+($this->ez['leftMargin'])/2;
  705.   // figure out the maximum widths of the text within each column
  706.   $maxWidth=array();
  707.   foreach($cols as $colName=>$colHeading){
  708.     $maxWidth[$colName]=0;
  709.   }
  710.   // find the maximum cell widths based on the data
  711.   foreach($data as $row){
  712.     foreach($cols as $colName=>$colHeading){
  713.       $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$row[$colName])*1.01;
  714.       if ($w > $maxWidth[$colName]){
  715.         $maxWidth[$colName]=$w;
  716.       }
  717.     }
  718.   }
  719.   // and the maximum widths to fit in the headings
  720.   foreach($cols as $colName=>$colTitle){
  721.     $w = $this->ezPrvtGetTextWidth($options['fontSize'],(string)$colTitle)*1.01;
  722.     if ($w > $maxWidth[$colName]){
  723.       $maxWidth[$colName]=$w;
  724.     }
  725.   }
  726.   
  727.   // calculate the start positions of each of the columns
  728.   $pos=array();
  729.   $x=0;
  730.   $t=$x;
  731.   $adjustmentWidth=0;
  732.   $setWidth=0;
  733.   foreach($maxWidth as $colName => $w){
  734.     $pos[$colName]=$t;
  735.     // if the column width has been specified then set that here, also total the
  736.     // width avaliable for adjustment
  737.     if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['width']) && $options['cols'][$colName]['width']>0){
  738.       $t=$t+$options['cols'][$colName]['width'];
  739.       $maxWidth[$colName] = $options['cols'][$colName]['width']-$options['gap'];
  740.       $setWidth += $options['cols'][$colName]['width'];
  741.     } else {
  742.       $t=$t+$w+$options['gap'];
  743.       $adjustmentWidth += $w;
  744.       $setWidth += $options['gap'];
  745.     }
  746.   }
  747.   $pos['_end_']=$t;
  748.  
  749.   // if maxWidth is specified, and the table is too wide, and the width has not been set,
  750.   // then set the width.
  751.   if ($options['width']==0 && $options['maxWidth'] && ($t-$x)>$options['maxWidth']){
  752.     // then need to make this one smaller
  753.     $options['width']=$options['maxWidth'];
  754.   }
  755.  
  756.   if ($options['width'] && $adjustmentWidth>0 && $setWidth<$options['width']){
  757.     // first find the current widths of the columns involved in this mystery
  758.     $cols0 = array();
  759.     $cols1 = array();
  760.     $xq=0;
  761.     $presentWidth=0;
  762.     $last='';
  763.     foreach($pos as $colName=>$p){
  764.       if (!isset($options['cols'][$last]) || !isset($options['cols'][$last]['width']) || $options['cols'][$last]['width']<=0){
  765.         if (strlen($last)){
  766.           $cols0[$last]=$p-$xq -$options['gap'];
  767.           $presentWidth += ($p-$xq - $options['gap']);
  768.         }
  769.       } else {
  770.         $cols1[$last]=$p-$xq;
  771.       }
  772.       $last=$colName;
  773.       $xq=$p;
  774.     }
  775.     // $cols0 contains the widths of all the columns which are not set
  776.     $neededWidth = $options['width']-$setWidth;
  777.     // if needed width is negative then add it equally to each column, else get more tricky
  778.     if ($presentWidth<$neededWidth){
  779.       foreach($cols0 as $colName=>$w){
  780.         $cols0[$colName]+= ($neededWidth-$presentWidth)/count($cols0);
  781.       }
  782.     } else {
  783.     
  784.       $cnt=0;
  785.       while ($presentWidth>$neededWidth && $cnt<100){
  786.         $cnt++; // insurance policy
  787.         // find the widest columns, and the next to widest width
  788.         $aWidest = array();
  789.         $nWidest=0;
  790.         $widest=0;
  791.         foreach($cols0 as $colName=>$w){
  792.           if ($w>$widest){
  793.             $aWidest=array($colName);
  794.             $nWidest = $widest;
  795.             $widest=$w;
  796.           } else if ($w==$widest){
  797.             $aWidest[]=$colName;
  798.           }
  799.         }
  800.         // then figure out what the width of the widest columns would have to be to take up all the slack
  801.         $newWidestWidth = $widest - ($presentWidth-$neededWidth)/count($aWidest);
  802.         if ($newWidestWidth > $nWidest){
  803.           // then there is space to set them to this
  804.           foreach($aWidest as $colName){
  805.             $cols0[$colName] = $newWidestWidth;
  806.           }
  807.           $presentWidth=$neededWidth;
  808.         } else {
  809.           // there is not space, reduce the size of the widest ones down to the next size down, and we
  810.           // will go round again
  811.           foreach($aWidest as $colName){
  812.             $cols0[$colName] = $nWidest;
  813.           }
  814.           $presentWidth=$presentWidth-($widest-$nWidest)*count($aWidest);
  815.         }
  816.       }
  817.     }
  818.     // $cols0 now contains the new widths of the constrained columns.
  819.     // now need to update the $pos and $maxWidth arrays
  820.     $xq=0;
  821.     foreach($pos as $colName=>$p){
  822.       $pos[$colName]=$xq;
  823.       if (!isset($options['cols'][$colName]) || !isset($options['cols'][$colName]['width']) || $options['cols'][$colName]['width']<=0){
  824.         if (isset($cols0[$colName])){
  825.           $xq += $cols0[$colName] + $options['gap'];
  826.           $maxWidth[$colName]=$cols0[$colName];
  827.         }
  828.       } else {
  829.         if (isset($cols1[$colName])){
  830.           $xq += $cols1[$colName];
  831.         }
  832.       }
  833.     }
  834.  
  835.     $t=$x+$options['width'];
  836.     $pos['_end_']=$t;
  837.   }
  838.  
  839.   // now adjust the table to the correct location across the page
  840.   switch ($options['xPos']){
  841.     case 'left':
  842.       $xref = $this->ez['leftMargin'];
  843.       break;
  844.     case 'right':
  845.       $xref = $this->ez['pageWidth'] - $this->ez['rightMargin'];
  846.       break;
  847.     case 'centre':
  848.     case 'center':
  849.       $xref = $middle;
  850.       break;
  851.     default:
  852.       $xref = $options['xPos'];
  853.       break;
  854.   }
  855.   switch ($options['xOrientation']){
  856.     case 'left':
  857.       $dx = $xref-$t;
  858.       break;
  859.     case 'right':
  860.       $dx = $xref;
  861.       break;
  862.     case 'centre':
  863.     case 'center':
  864.       $dx = $xref-$t/2;
  865.       break;
  866.   }
  867.  
  868.  
  869.   foreach($pos as $k=>$v){
  870.     $pos[$k]=$v+$dx;
  871.   }
  872.   $x0=$x+$dx;
  873.   $x1=$t+$dx;
  874.  
  875.   $baseLeftMargin = $this->ez['leftMargin'];
  876.   $basePos = $pos;
  877.   $baseX0 = $x0;
  878.   $baseX1 = $x1;
  879.   
  880.   // ok, just about ready to make me a table
  881.   $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2]);
  882.   $this->setStrokeColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2]);
  883.  
  884.   $middle = ($x1+$x0)/2;
  885.  
  886.   // start a transaction which will be used to regress the table, if there are not enough rows protected
  887.   if ($options['protectRows']>0){
  888.     $this->transaction('start');
  889.     $movedOnce=0;
  890.   }
  891.   $abortTable = 1;
  892.   while ($abortTable){
  893.   $abortTable=0;
  894.  
  895.   $dm = $this->ez['leftMargin']-$baseLeftMargin;
  896.   foreach($basePos as $k=>$v){
  897.     $pos[$k]=$v+$dm;
  898.   }
  899.   $x0=$baseX0+$dm;
  900.   $x1=$baseX1+$dm;
  901.   $middle = ($x1+$x0)/2;
  902.  
  903.  
  904.   // if the title is set, then do that
  905.   if (strlen($title)){
  906.     $w = $this->getTextWidth($options['titleFontSize'],$title);
  907.     $this->y -= $this->getFontHeight($options['titleFontSize']);
  908.     if ($this->y < $this->ez['bottomMargin']){
  909.       $this->ezNewPage();
  910.         // margins may have changed on the newpage
  911.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  912.         foreach($basePos as $k=>$v){
  913.           $pos[$k]=$v+$dm;
  914.         }
  915.         $x0=$baseX0+$dm;
  916.         $x1=$baseX1+$dm;
  917.         $middle = ($x1+$x0)/2;
  918.       $this->y -= $this->getFontHeight($options['titleFontSize']);
  919.     }
  920.     $this->addText($middle-$w/2,$this->y,$options['titleFontSize'],$title);
  921.     $this->y -= $options['titleGap'];
  922.   }
  923.  
  924.         // margins may have changed on the newpage
  925.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  926.         foreach($basePos as $k=>$v){
  927.           $pos[$k]=$v+$dm;
  928.         }
  929.         $x0=$baseX0+$dm;
  930.         $x1=$baseX1+$dm;
  931.   
  932.   $y=$this->y; // to simplify the code a bit
  933.   
  934.   // make the table
  935.   $height = $this->getFontHeight($options['fontSize']);
  936.   $decender = $this->getFontDecender($options['fontSize']);
  937.  
  938.   
  939.   
  940.   $y0=$y+$decender;
  941.   $dy=0;
  942.   if ($options['showHeadings']){
  943.     // this function will move the start of the table to a new page if it does not fit on this one
  944.     $headingHeight = $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  945.     $y0 = $y+$headingHeight;
  946.     $y1 = $y;
  947.  
  948.  
  949.     $dm = $this->ez['leftMargin']-$baseLeftMargin;
  950.     foreach($basePos as $k=>$v){
  951.       $pos[$k]=$v+$dm;
  952.     }
  953.     $x0=$baseX0+$dm;
  954.     $x1=$baseX1+$dm;
  955.  
  956.   } else {
  957.     $y1 = $y0;
  958.   }
  959.   $firstLine=1;
  960.  
  961.   
  962.   // open an object here so that the text can be put in over the shading
  963.   if ($options['shaded']){
  964.     $this->saveState();
  965.     $textObjectId = $this->openObject();
  966.     $this->closeObject();
  967.     $this->addObject($textObjectId);
  968.     $this->reopenObject($textObjectId);
  969.   }
  970.   
  971.   $cnt=0;
  972.   $newPage=0;
  973.   foreach($data as $row){
  974.     $cnt++;
  975.     // the transaction support will be used to prevent rows being split
  976.     if ($options['splitRows']==0){
  977.       $pageStart = $this->ezPageCount;
  978.       if (isset($this->ez['columns']) && $this->ez['columns']['on']==1){
  979.         $columnStart = $this->ez['columns']['colNum'];
  980.       }
  981.       $this->transaction('start');
  982.       $row_orig = $row;
  983.       $y_orig = $y;
  984.       $y0_orig = $y0;
  985.       $y1_orig = $y1;
  986.     }
  987.     $ok=0;
  988.     $secondTurn=0;
  989.     while(!$abortTable && $ok == 0){
  990.  
  991.     $mx=0;
  992.     $newRow=1;
  993.     while(!$abortTable && ($newPage || $newRow)){
  994.       
  995.       $y-=$height;
  996.       if ($newPage || $y<$this->ez['bottomMargin'] || (isset($options['minRowSpace']) && $y<($this->ez['bottomMargin']+$options['minRowSpace'])) ){
  997.         // check that enough rows are with the heading
  998.         if ($options['protectRows']>0 && $movedOnce==0 && $cnt<=$options['protectRows']){
  999.           // then we need to move the whole table onto the next page
  1000.           $movedOnce = 1;
  1001.           $abortTable = 1;
  1002.         }
  1003.       
  1004.         $y2=$y-$mx+2*$height+$decender-$newRow*$height;
  1005.         if ($options['showLines']){
  1006.           if (!$options['showHeadings']){
  1007.             $y0=$y1;
  1008.           }
  1009.           $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  1010.         }
  1011.         if ($options['shaded']){
  1012.           $this->closeObject();
  1013.           $this->restoreState();
  1014.         }
  1015.         $this->ezNewPage();
  1016.         // and the margins may have changed, this is due to the possibility of the columns being turned on
  1017.         // as the columns are managed by manipulating the margins
  1018.  
  1019.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  1020.         foreach($basePos as $k=>$v){
  1021.           $pos[$k]=$v+$dm;
  1022.         }
  1023. //        $x0=$x0+$dm;
  1024. //        $x1=$x1+$dm;
  1025.         $x0=$baseX0+$dm;
  1026.         $x1=$baseX1+$dm;
  1027.   
  1028.         if ($options['shaded']){
  1029.           $this->saveState();
  1030.           $textObjectId = $this->openObject();
  1031.           $this->closeObject();
  1032.           $this->addObject($textObjectId);
  1033.           $this->reopenObject($textObjectId);
  1034.         }
  1035.         $this->setColor($options['textCol'][0],$options['textCol'][1],$options['textCol'][2],1);
  1036.         $y = $this->ez['pageHeight']-$this->ez['topMargin'];
  1037.         $y0=$y+$decender;
  1038.         $mx=0;
  1039.         if ($options['showHeadings']){
  1040.           $this->ezPrvtTableColumnHeadings($cols,$pos,$maxWidth,$height,$decender,$options['rowGap'],$options['fontSize'],$y,$options);
  1041.           $y1=$y;
  1042.         } else {
  1043.           $y1=$y0;
  1044.         }
  1045.         $firstLine=1;
  1046.         $y -= $height;
  1047.       }
  1048.       $newRow=0;
  1049.       // write the actual data
  1050.       // if these cells need to be split over a page, then $newPage will be set, and the remaining
  1051.       // text will be placed in $leftOvers
  1052.       $newPage=0;
  1053.       $leftOvers=array();
  1054.  
  1055.       foreach($cols as $colName=>$colTitle){
  1056.         $this->ezSetY($y+$height);
  1057.         $colNewPage=0;
  1058.         if (isset($row[$colName])){
  1059.           if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['link']) && strlen($options['cols'][$colName]['link'])){
  1060.             
  1061.             $lines = explode("\n",$row[$colName]);
  1062.             if (isset($row[$options['cols'][$colName]['link']]) && strlen($row[$options['cols'][$colName]['link']])){
  1063.               foreach($lines as $k=>$v){
  1064.                 $lines[$k]='<c:alink:'.$row[$options['cols'][$colName]['link']].'>'.$v.'</c:alink>';
  1065.               }
  1066.             }
  1067.           } else {
  1068.             $lines = explode("\n",$row[$colName]);
  1069.           }
  1070.         } else {
  1071.           $lines = array();
  1072.         }
  1073.         $this->y -= $options['rowGap'];
  1074.         foreach ($lines as $line){
  1075.           $line = $this->ezProcessText($line);
  1076.           $start=1;
  1077.  
  1078.           while (strlen($line) || $start){
  1079.             $start=0;
  1080.             if (!$colNewPage){
  1081.               $this->y=$this->y-$height;
  1082.             }
  1083.             if ($this->y < $this->ez['bottomMargin']){
  1084.   //            $this->ezNewPage();
  1085.               $newPage=1;  // whether a new page is required for any of the columns
  1086.               $colNewPage=1; // whether a new page is required for this column
  1087.             }
  1088.             if ($colNewPage){
  1089.               if (isset($leftOvers[$colName])){
  1090.                 $leftOvers[$colName].="\n".$line;
  1091.               } else {
  1092.                 $leftOvers[$colName] = $line;
  1093.               }
  1094.               $line='';
  1095.             } else {
  1096.               if (isset($options['cols'][$colName]) && isset($options['cols'][$colName]['justification']) ){
  1097.                 $just = $options['cols'][$colName]['justification'];
  1098.               } else {
  1099.                 $just='left';
  1100.               }
  1101.  
  1102.               $line=$this->addTextWrap($pos[$colName],$this->y,$maxWidth[$colName],$options['fontSize'],$line,$just);
  1103.             }
  1104.           }
  1105.         }
  1106.   
  1107.         $dy=$y+$height-$this->y+$options['rowGap'];
  1108.         if ($dy-$height*$newPage>$mx){
  1109.           $mx=$dy-$height*$newPage;
  1110.         }
  1111.       }
  1112.       // set $row to $leftOvers so that they will be processed onto the new page
  1113.       $row = $leftOvers;
  1114.       // now add the shading underneath
  1115.       if ($options['shaded'] && $cnt%2==0){
  1116.         $this->closeObject();
  1117.         $this->setColor($options['shadeCol'][0],$options['shadeCol'][1],$options['shadeCol'][2],1);
  1118.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1119.         $this->reopenObject($textObjectId);
  1120.       }
  1121.  
  1122.       if ($options['shaded']==2 && $cnt%2==1){
  1123.         $this->closeObject();
  1124.         $this->setColor($options['shadeCol2'][0],$options['shadeCol2'][1],$options['shadeCol2'][2],1);
  1125.         $this->filledRectangle($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1126.         $this->reopenObject($textObjectId);
  1127.       }
  1128.  
  1129.       if ($options['showLines']>1){
  1130.         // then draw a line on the top of each block
  1131. //        $this->closeObject();
  1132.         $this->saveState();
  1133.         $this->setStrokeColor($options['lineCol'][0],$options['lineCol'][1],$options['lineCol'][2],1);
  1134. //        $this->line($x0-$options['gap']/2,$y+$decender+$height-$mx,$x1-$x0,$mx);
  1135.         if ($firstLine){
  1136.           $this->setLineStyle($options['outerLineThickness']);
  1137.           $firstLine=0;
  1138.         } else {
  1139.           $this->setLineStyle($options['innerLineThickness']);
  1140.         }
  1141.         $this->line($x0-$options['gap']/2,$y+$decender+$height,$x1-$options['gap']/2,$y+$decender+$height);
  1142.         $this->restoreState();
  1143. //        $this->reopenObject($textObjectId);
  1144.       }
  1145.     } // end of while 
  1146.     $y=$y-$mx+$height;
  1147.     
  1148.     // checking row split over pages
  1149.     if ($options['splitRows']==0){
  1150.       if ( ( ($this->ezPageCount != $pageStart) || (isset($this->ez['columns']) && $this->ez['columns']['on']==1 && $columnStart != $this->ez['columns']['colNum'] ))  && $secondTurn==0){
  1151.         // then we need to go back and try that again !
  1152.         $newPage=1;
  1153.         $secondTurn=1;
  1154.         $this->transaction('rewind');
  1155.         $row = $row_orig;
  1156.         $y = $y_orig;
  1157.         $y0 = $y0_orig;
  1158.         $y1 = $y1_orig;
  1159.         $ok=0;
  1160.  
  1161.         $dm = $this->ez['leftMargin']-$baseLeftMargin;
  1162.         foreach($basePos as $k=>$v){
  1163.           $pos[$k]=$v+$dm;
  1164.         }
  1165.         $x0=$baseX0+$dm;
  1166.         $x1=$baseX1+$dm;
  1167.  
  1168.       } else {
  1169.         $this->transaction('commit');
  1170.         $ok=1;
  1171.       }
  1172.     } else {
  1173.       $ok=1;  // don't go round the loop if splitting rows is allowed
  1174.     }
  1175.     
  1176.     }  // end of while to check for row splitting
  1177.     if ($abortTable){
  1178.       if ($ok==0){
  1179.         $this->transaction('abort');
  1180.       }
  1181.       // only the outer transaction should be operational
  1182.       $this->transaction('rewind');
  1183.       $this->ezNewPage();
  1184.       break;
  1185.     }
  1186.     
  1187.   } // end of foreach ($data as $row)
  1188.   
  1189.   } // end of while ($abortTable)
  1190.  
  1191.   // table has been put on the page, the rows guarded as required, commit.
  1192.   $this->transaction('commit');
  1193.  
  1194.   $y2=$y+$decender;
  1195.   if ($options['showLines']){
  1196.     if (!$options['showHeadings']){
  1197.       $y0=$y1;
  1198.     }
  1199.     $this->ezPrvtTableDrawLines($pos,$options['gap'],$x0,$x1,$y0,$y1,$y2,$options['lineCol'],$options['innerLineThickness'],$options['outerLineThickness'],$options['showLines']);
  1200.   }
  1201.  
  1202.   // close the object for drawing the text on top
  1203.   if ($options['shaded']){
  1204.     $this->closeObject();
  1205.     $this->restoreState();
  1206.   }
  1207.   
  1208.   $this->y=$y;
  1209.   return $y;
  1210. }
  1211.  
  1212. // ------------------------------------------------------------------------------
  1213. function ezProcessText($text){
  1214.   // this function will intially be used to implement underlining support, but could be used for a range of other
  1215.   // purposes
  1216.   $search = array('<u>','<U>','</u>','</U>');
  1217.   $replace = array('<c:uline>','<c:uline>','</c:uline>','</c:uline>');
  1218.   return str_replace($search,$replace,$text);
  1219. }
  1220.  
  1221. // ------------------------------------------------------------------------------
  1222.  
  1223. function ezText($text,$size=0,$options=array(),$test=0){
  1224.   // this will add a string of text to the document, starting at the current drawing
  1225.   // position.
  1226.   // it will wrap to keep within the margins, including optional offsets from the left
  1227.   // and the right, if $size is not specified, then it will be the last one used, or
  1228.   // the default value (12 I think).
  1229.   // the text will go to the start of the next line when a return code "\n" is found.
  1230.   // possible options are:
  1231.   // 'left'=> number, gap to leave from the left margin
  1232.   // 'right'=> number, gap to leave from the right margin
  1233.   // 'aleft'=> number, absolute left position (overrides 'left')
  1234.   // 'aright'=> number, absolute right position (overrides 'right')
  1235.   // 'justification' => 'left','right','center','centre','full'
  1236.  
  1237.   // only set one of the next two items (leading overrides spacing)
  1238.   // 'leading' => number, defines the total height taken by the line, independent of the font height.
  1239.   // 'spacing' => a real number, though usually set to one of 1, 1.5, 2 (line spacing as used in word processing)
  1240.   
  1241.   // if $test is set then this should just check if the text is going to flow onto a new page or not, returning true or false
  1242.   
  1243.   // apply the filtering which will make the underlining function.
  1244.   $text = $this->ezProcessText($text);
  1245.   
  1246.   $newPage=false;
  1247.   $store_y = $this->y;
  1248.   
  1249.   if (is_array($options) && isset($options['aleft'])){
  1250.     $left=$options['aleft'];
  1251.   } else {
  1252.     $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1253.   }
  1254.   if (is_array($options) && isset($options['aright'])){
  1255.     $right=$options['aright'];
  1256.   } else {
  1257.     $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1258.   }
  1259.   if ($size<=0){
  1260.     $size = $this->ez['fontSize'];
  1261.   } else {
  1262.     $this->ez['fontSize']=$size;
  1263.   }
  1264.   
  1265.   if (is_array($options) && isset($options['justification'])){
  1266.     $just = $options['justification'];
  1267.   } else {
  1268.     $just = 'left';
  1269.   }
  1270.   
  1271.   // modifications to give leading and spacing based on those given by Craig Heydenburg 1/1/02
  1272.   if (is_array($options) && isset($options['leading'])) { ## use leading instead of spacing
  1273.     $height = $options['leading'];
  1274.     } else if (is_array($options) && isset($options['spacing'])) {
  1275.     $height = $this->getFontHeight($size) * $options['spacing'];
  1276.     } else {
  1277.         $height = $this->getFontHeight($size);
  1278.     }
  1279.  
  1280.   
  1281.   $lines = explode("\n",$text);
  1282.   foreach ($lines as $line){
  1283.     $start=1;
  1284.     while (strlen($line) || $start){
  1285.       $start=0;
  1286.       $this->y=$this->y-$height;
  1287.       if ($this->y < $this->ez['bottomMargin']){
  1288.         if ($test){
  1289.           $newPage=true;
  1290.         } else {
  1291.           $this->ezNewPage();
  1292.           // and then re-calc the left and right, in case they have changed due to columns
  1293.         }
  1294.       }
  1295.       if (is_array($options) && isset($options['aleft'])){
  1296.         $left=$options['aleft'];
  1297.       } else {
  1298.         $left = $this->ez['leftMargin'] + ((is_array($options) && isset($options['left']))?$options['left']:0);
  1299.       }
  1300.       if (is_array($options) && isset($options['aright'])){
  1301.         $right=$options['aright'];
  1302.       } else {
  1303.         $right = $this->ez['pageWidth'] - $this->ez['rightMargin'] - ((is_array($options) && isset($options['right']))?$options['right']:0);
  1304.       }
  1305.       $line=$this->addTextWrap($left,$this->y,$right-$left,$size,$line,$just,0,$test);
  1306.     }
  1307.   }
  1308.  
  1309.   if ($test){
  1310.     $this->y=$store_y;
  1311.     return $newPage;
  1312.   } else {
  1313.     return $this->y;
  1314.   }
  1315. }
  1316.  
  1317. // ------------------------------------------------------------------------------
  1318.  
  1319. function ezImage($image,$pad = 5,$width = 0,$resize = 'full',$just = 'center',$border = ''){
  1320.     //beta ezimage function
  1321.     if (stristr($image,'://'))//copy to temp file
  1322.     {
  1323.         $fp = @fopen($image,"rb");
  1324.         while(!feof($fp))
  1325.            {
  1326.                   $cont.= fread($fp,1024);
  1327.            }
  1328.            fclose($fp);
  1329.         $image = tempnam ("/tmp", "php-pdf");
  1330.         $fp2 = @fopen($image,"w");
  1331.            fwrite($fp2,$cont);
  1332.           fclose($fp2);
  1333.         $temp = true;
  1334.     }
  1335.  
  1336.     if (!(file_exists($image))) return false; //return immediately if image file does not exist
  1337.     $imageInfo = getimagesize($image);
  1338.     switch ($imageInfo[2]){
  1339.         case 2:
  1340.             $type = "jpeg";
  1341.             break;
  1342.         case 3:
  1343.             $type = "png";
  1344.             break;
  1345.         default:
  1346.             return false; //return if file is not jpg or png
  1347.     }
  1348.     if ($width == 0) $width = $imageInfo[0]; //set width
  1349.     $ratio = $imageInfo[0]/$imageInfo[1];
  1350.  
  1351.     //get maximum width of image
  1352.     if (isset($this->ez['columns']) && $this->ez['columns']['on'] == 1)
  1353.     {
  1354.         $bigwidth = $this->ez['columns']['width'] - ($pad * 2);
  1355.     }
  1356.     else
  1357.     {
  1358.         $bigwidth = $this->ez['pageWidth'] - ($pad * 2);
  1359.     }
  1360.     //fix width if larger than maximum or if $resize=full
  1361.     if ($resize == 'full' || $resize == 'width' || $width > $bigwidth)
  1362.     {
  1363.         $width = $bigwidth;
  1364.  
  1365.     }
  1366.  
  1367.     $height = ($width/$ratio); //set height
  1368.  
  1369.     //fix size if runs off page
  1370.     if ($height > ($this->y - $this->ez['bottomMargin'] - ($pad * 2)))
  1371.     {
  1372.         if ($resize != 'full')
  1373.         {
  1374.             $this->ezNewPage();
  1375.         }
  1376.         else
  1377.         {
  1378.             $height = ($this->y - $this->ez['bottomMargin'] - ($pad * 2)); //shrink height
  1379.             $width = ($height*$ratio); //fix width
  1380.         }
  1381.     }
  1382.  
  1383.     //fix x-offset if image smaller than bigwidth
  1384.     if ($width < $bigwidth)
  1385.     {
  1386.         //center if justification=center
  1387.         if ($just == 'center')
  1388.         {
  1389.             $offset = ($bigwidth - $width) / 2;
  1390.         }
  1391.         //move to right if justification=right
  1392.         if ($just == 'right')
  1393.         {
  1394.             $offset = ($bigwidth - $width);
  1395.         }
  1396.         //leave at left if justification=left
  1397.         if ($just == 'left')
  1398.         {
  1399.             $offset = 0;
  1400.         }
  1401.     }
  1402.  
  1403.  
  1404.     //call appropriate function
  1405.     if ($type == "jpeg"){
  1406.         $this->addJpegFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1407.     }
  1408.  
  1409.     if ($type == "png"){
  1410.         $this->addPngFromFile($image,$this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width);
  1411.     }
  1412.     //draw border
  1413.     if ($border != '')
  1414.     {
  1415.     if (!(isset($border['color'])))
  1416.     {
  1417.         $border['color']['red'] = .5;
  1418.         $border['color']['blue'] = .5;
  1419.         $border['color']['green'] = .5;
  1420.     }
  1421.     if (!(isset($border['width']))) $border['width'] = 1;
  1422.     if (!(isset($border['cap']))) $border['cap'] = 'round';
  1423.     if (!(isset($border['join']))) $border['join'] = 'round';
  1424.     
  1425.  
  1426.     $this->setStrokeColor($border['color']['red'],$border['color']['green'],$border['color']['blue']);
  1427.     $this->setLineStyle($border['width'],$border['cap'],$border['join']);
  1428.     $this->rectangle($this->ez['leftMargin'] + $pad + $offset, $this->y + $this->getFontHeight($this->ez['fontSize']) - $pad - $height,$width,$height);
  1429.  
  1430.     }
  1431.     // move y below image
  1432.     $this->y = $this->y - $pad - $height;
  1433.     //remove tempfile for remote images
  1434.     if ($temp == true) unlink($image);
  1435.  
  1436. }
  1437. // ------------------------------------------------------------------------------
  1438.  
  1439. // note that templating code is still considered developmental - have not really figured
  1440. // out a good way of doing this yet.
  1441. function loadTemplate($templateFile){
  1442.   // this function will load the requested template ($file includes full or relative pathname)
  1443.   // the code for the template will be modified to make it name safe, and then stored in 
  1444.   // an array for later use
  1445.   // The id of the template will be returned for the user to operate on it later
  1446.   if (!file_exists($templateFile)){
  1447.     return -1;
  1448.   }
  1449.  
  1450.   $code = implode('',file($templateFile));
  1451.   if (!strlen($code)){
  1452.     return;
  1453.   }
  1454.  
  1455.   $code = trim($code);
  1456.   if (substr($code,0,5)=='<?php'){
  1457.     $code = substr($code,5);
  1458.   }
  1459.   if (substr($code,-2)=='?>'){
  1460.     $code = substr($code,0,strlen($code)-2);
  1461.   }
  1462.   if (isset($this->ez['numTemplates'])){
  1463.     $newNum = $this->ez['numTemplates'];
  1464.     $this->ez['numTemplates']++;
  1465.   } else {
  1466.     $newNum=0;
  1467.     $this->ez['numTemplates']=1;
  1468.     $this->ez['templates']=array();
  1469.   }
  1470.  
  1471.   $this->ez['templates'][$newNum]['code']=$code;
  1472.  
  1473.   return $newNum;
  1474. }
  1475.  
  1476. // ------------------------------------------------------------------------------
  1477.  
  1478. function execTemplate($id,$data=array(),$options=array()){
  1479.   // execute the given template on the current document.
  1480.   if (!isset($this->ez['templates'][$id])){
  1481.     return;
  1482.   }
  1483.   eval($this->ez['templates'][$id]['code']);
  1484. }
  1485.  
  1486. // ------------------------------------------------------------------------------
  1487. function ilink($info){
  1488.   $this->alink($info,1);
  1489. }
  1490.  
  1491. function alink($info,$internal=0){
  1492.   // a callback function to support the formation of clickable links within the document
  1493.   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1494.   switch($info['status']){
  1495.     case 'start':
  1496.     case 'sol':
  1497.       // the beginning of the link
  1498.       // this should contain the URl for the link as the 'p' entry, and will also contain the value of 'nCallback'
  1499.       if (!isset($this->ez['links'])){
  1500.         $this->ez['links']=array();
  1501.       }
  1502.       $i = $info['nCallback'];
  1503.       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height'],'url'=>$info['p']);
  1504.         $this->saveState();
  1505.         $this->setColor(0,0,1);
  1506.         $this->setStrokeColor(0,0,1);
  1507.         $thick = $info['height']*$lineFactor;
  1508.         $this->setLineStyle($thick);
  1509.       break;
  1510.     case 'end':
  1511.     case 'eol':
  1512.       // the end of the link
  1513.       // assume that it is the most recent opening which has closed
  1514.       $i = $info['nCallback'];
  1515.       $start = $this->ez['links'][$i];
  1516.       // add underlining
  1517.         $a = deg2rad((float)$start['angle']-90.0);
  1518.         $drop = $start['height']*$lineFactor*1.5;
  1519.         $dropx = cos($a)*$drop;
  1520.         $dropy = -sin($a)*$drop;
  1521.         $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1522.         if ($internal) {
  1523.              $this->addInternalLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1524.         } else {
  1525.              $this->addLink($start['url'],$start['x'],$start['y']+$start['decender'],$info['x'],$start['y']+$start['decender']+$start['height']);
  1526.       }
  1527.       $this->restoreState();
  1528.       break;
  1529.   }
  1530. }
  1531.  
  1532. // ------------------------------------------------------------------------------
  1533.  
  1534. function uline($info){
  1535.   // a callback function to support underlining
  1536.   $lineFactor=0.05; // the thickness of the line as a proportion of the height. also the drop of the line.
  1537.   switch($info['status']){
  1538.     case 'start':
  1539.     case 'sol':
  1540.     
  1541.       // the beginning of the underline zone
  1542.       if (!isset($this->ez['links'])){
  1543.         $this->ez['links']=array();
  1544.       }
  1545.       $i = $info['nCallback'];
  1546.       $this->ez['links'][$i] = array('x'=>$info['x'],'y'=>$info['y'],'angle'=>$info['angle'],'decender'=>$info['decender'],'height'=>$info['height']);
  1547.       $this->saveState();
  1548.       $thick = $info['height']*$lineFactor;
  1549.       $this->setLineStyle($thick);
  1550.       break;
  1551.     case 'end':
  1552.     case 'eol':
  1553.       // the end of the link
  1554.       // assume that it is the most recent opening which has closed
  1555.       $i = $info['nCallback'];
  1556.       $start = $this->ez['links'][$i];
  1557.       // add underlining
  1558.       $a = deg2rad((float)$start['angle']-90.0);
  1559.       $drop = $start['height']*$lineFactor*1.5;
  1560.       $dropx = cos($a)*$drop;
  1561.       $dropy = -sin($a)*$drop;
  1562.       $this->line($start['x']-$dropx,$start['y']-$dropy,$info['x']-$dropx,$info['y']-$dropy);
  1563.       $this->restoreState();
  1564.       break;
  1565.   }
  1566. }
  1567.  
  1568. // ------------------------------------------------------------------------------
  1569.  
  1570. }
  1571. ?>