- <?php
- /**
- *
- * @copyright sptpl_clsColumn.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
- * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
- * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsColumn.php,v 2.15 2005/03/17 12:46:48 Darvin Exp $
- */
- /*
- * +-------------------------------------------------------------------------+
- * | Sptpl |
- * +-------------------------------------------------------------------------+
- * | Copyright (c) 2003-2005 Andrioli Darvin |
- * | Email <darvin (inside) andrioli (dot) com> |
- * | Web http://www.andrioli.com/en/sptpl.html |
- * | Download http://www.phpclasses.org/browse.html/package/1326.html |
- * | |
- * +-------------------------------------------------------------------------+
- * | This library is free software; you can redistribute it and/or modify |
- * | it under the terms of the GNU Lesser General Public License as |
- * | published by the Free Software Foundation; either version 2 of the |
- * | License, or (at your option) any later version. |
- * | |
- * | This library is distributed in the hope that it will be useful, but |
- * | WITHOUT ANY WARRANTY; without even the implied warranty of |
- * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
- * | Lesser General Public License for more details. |
- * | |
- * | You should have received a copy of the GNU Lesser General Public |
- * | License along with this library; if not, write to the Free Software |
- * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
- * +-------------------------------------------------------------------------+
- */
-
- class CColumn {
- /**
- * Column width
- * @var integer
- */
- var $Width;
- /**
- * Left position of this column inside the parent column
- * @var integer
- */
- var $LeftPosition;
-
- /**
- * Margins setting.
- * Allowed index: top, bottom, left, right
- * @var array $Margins
- */
- var $Margins;
-
- /**
- * Colpos value used if not specified as parameter into function writeout
- * @var integer
- * @see WriteOut()
- */
- var $DefColPos;
-
- /**
- * Alignment value used if not specified as parameter into function writeout
- * @var string
- * @see WriteOut()
- */
- var $DefAlign;
- /**
- * Default font to use to print the columns text.
- * The class use this font if no one is supplied
- * NULL if not set
- * var object CFont
- */
- var $clsDefaultFont;
- /**
- * Real font to use to print the columns text
- * NULL if not set
- * var object CFont
- */
- var $clsCurrentFont;
-
- /**
- * Array of texts row defined inside this unit
- * @var array
- */
- var $innerText;
- /**
- * For each row from innerText, set the distance from
- * the left border of the column
- * @var array
- * @see $innerText
- */
- var $LeftText;
- /**
- * The text inside this unit is retrieved on row basis.
- * This variable track which row return on each call to gettext
- * @var integer
- * @see getText()
- */
- var $RowPtr;
- /**
- * Height for the current text stored into innerText
- * @var integer
- * @see $innerText
- * @see AddRow()
- */
- var $Height;
- /**
- * Class initializer
- * @param object CXml2Array
- * @access public
- */
- function CColumn($Cfg=NULL)
- {
- // Pad applied to the text inside the column. Default non cell padding
- $this->Margins=array('top' =>0,
- 'bottom' =>0,
- 'left' =>0,
- 'right' =>0);
- $this->clsDefaultFont=NULL;
- $this->DefAlign='left';
- if($Cfg!=NULL)
- $this->Parse($Cfg);
- $this->ClearText(); // reset the inner text structure
- }
-
- /**
- * Parse the configuration node
- *
- * @param object CXml2Array
- * @access public
- */
- function Parse($Cfg)
- {
- // Attributes
- while(($ret=$Cfg->EachAttribute())!=FALSE)
- {
- list($AttribName,$value)=$ret;
- switch(strtolower($AttribName))
- {
- case 'leftpos' :
- $this->LeftPosition=$value;
- break;
- case 'width' :
- $this->Width=$value;
- break;
- case 'align' :
- $this->DefAlign=$value;
- break;
- case 'font' :
- $Id=$value;
- break;
- }
- }
- // Childs
- while(($ret=$Cfg->EachChild())!=FALSE)
- {
- list($ChildName,$Child)=$ret;
- switch(strtolower($ChildName)) {
- case 'margin' :
- $this->_SetMargins($Child);
- break;
- default:
- trigger_error('Unknown tag '.$Child->GetNodeName(),E_USER_ERROR);
- break;
- }
- }
- }
-
- /**
- * Load margins setting from the configuration file
- * Default values are set by the class constructor.
- * @param object CXml2Array
- * @access private
- *
- */
- function _SetMargins($Cfg)
- {
-
- while(($ret=$Cfg->EachChild())!=FALSE)
- {
- list($ChildName,$Child)=$ret;
- switch(strtolower($ChildName)) {
- case 'top' :
- $value=trim($Child->GetText());
- if(!ctype_digit($value))
- trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
- $this->Margins['top']=$value;
- break;
- case 'bottom' :
- $value=trim($Child->GetText());
- if(!ctype_digit($value))
- trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
- $this->Margins['bottom']=$value;
- break;
- case 'left' :
- $value=trim($Child->GetText());
- if(!ctype_digit($value))
- trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
- $this->Margins['left']=$value;
- break;
- case 'right' :
- $value=trim($Child->GetText());
- if(!ctype_digit($value))
- trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
- $this->Margins['right']=$value;
- break;
- default:
- trigger_error('Unknown tag '.$Child->GetNodeName(),E_USER_ERROR);
- break;
- }
- }
- }
-
- /**
- * Function executed at the end of the xml parsing. I run the final configuration
- * checks and complete the configration data
- * @access public
- */
- function EndParseXml()
- {
- $this->_CheckMargins();
- }
- /**
- * Check whether the margins are placed inside or outside the page
- * @access private
- */
- function _CheckMargins()
- {
- if(($this->Margins['left']+$this->Margins['right'])>$this->Width)
- trigger_error('The left or right margin are bigger then the size of the column',E_USER_ERROR);
- }
-
- /**
- * @param integer
- * @access public
- */
- function SetWidth($width)
- {
- $this->Width=$width;
- }
- /**
- * @param integer
- * @access public
- */
- function SetLeftPos($pos)
- {
- $this->LeftPosition=$pos;
- }
- /**
- * Return where is the left margin of the column
- * @return integer
- * @access public
- */
- function GetLeftPos()
- {
- return($this->LeftPosition);
- }
-
- /**
- * Return where is the left margin of the text
- * @return integer
- * @access public
- */
- function GetLeftTextPos()
- {
- assert('array_key_exists($this->RowPtr,$this->LeftText)');
- return($this->LeftPosition+$this->LeftText[$this->RowPtr]);
- }
- /**
- * Set the default font for the column
- * @param object CFont
- * @return object CFont
- * @access public
- */
- function SetDefaultFont($NFont)
- {
- $old=$this->clsDefaultFont;
- $this->clsDefaultFont=$NFont;
- return($old);
- }
-
- /**
- * Returns the id of the current font
- * @return string
- * @access public
- */
- function GetFontId()
- {
- return($this->clsCurrentFont->Id);
- }
- /**
- * retrieve the text from the column per row basis
- * @return string
- * @access public
- */
- function GetText()
- {
- if(array_key_exists($this->RowPtr,$this->innerText))
- return($this->innerText[$this->RowPtr++]);
- else
- return('');
- }
-
- // altezza del testo contenuto nell'unita
-
- function GetHeight()
- {
- return($this->Height);
- }
-
- /**
- * Pass the text to the appropriate column. It returns a CUnit object holding
- * the text with format (alignment, font) according to the given parameters
- * @parameter string $txt Text to write
- * @parameter string $align Text alignment, left, center, right
- * @parameter integer $colPos
- * @parameter integer $Column column where place the text
- * @parameter object CFont Font used to print the text, if NULL, it uses the default font
- * for the current column
- * @access public
- */
- function WriteOut($txt,$align='',$colPos=0,$pFont=NULL)
- {
- // How Many row
- $rows = explode("\n",rtrim($txt));
- $FirstRow=TRUE;
- if($pFont!=NULL)
- $this->clsCurrentFont=$pFont;
- else
- $this->clsCurrentFont=$this->clsDefaultFont;
- // print_r($FontToUse);
- // echo "\n<br>rows: ".print_r_to_var($rows);
- $TextOut='';
- $alignment=($align=='')?$this->DefAlign:$align;
- $this->Height=0;
- foreach($rows as $row)
- {
- $txt=$row;
- // If the row haven't any character, archive it as empty row.
- if(strlen($txt)==0&&!$FirstRow)
- $this->_AddRow("",0);
-
- $conta=0;
- while(($lenth=$this->_MyStrlen($txt))>0)
- {
- $conta++;
- assert('$conta<90'); // 90rows? reasonably too much
- $EndOffset=$this->_CalcEndOffset($txt);
- // printf("Completo text len:%u - text:%s - end off:%u<br>",$lenth,$txt,$EndOffset);
- if($EndOffset==0)
- trigger_error("The column width too small. Enlarge it to allow to cointain at least one character",E_USER_ERROR);
- $wkTxt=substr($txt,0,$EndOffset);
- $StartCol=$this->_BeginCol($alignment,$this->_MyStrlen($wkTxt),$colPos);
- // if($this->Margins['left']) $pad=str_repeat(' ', $this->Margins['left'] + $colPos);
- // else $pad='';
- // $TextOut=substr($wkTxt,0,$this->Width-$this->Margins['right']);
- // printf("Di riga text len:%u - text:%s - start col:%u, font width: %u<br>",$this->_MyStrlen($wkTxt),$wkTxt,$StartCol,$this->clsCurrentFont->Width);
- $this->_AddRow($wkTxt,$StartCol);
- if(!(strlen($txt)<$EndOffset))
- $txt=substr($txt,$EndOffset);
- else
- $txt='';
- }
- $FirstRow=FALSE;
- }
- return;
- }
-
- function _MyStrlen($text)
- {
- assert('is_object($this->clsCurrentFont)');
- return($this->clsCurrentFont->strlen($text));
- }
-
- function _CalcEndOffset($text)
- {
- $colWidth=$this->Width-$this->Margins['right']-$this->Margins['left'];
- $EndOffset=strlen($text); // I need the real number of characters
- if($this->_MyStrlen($text)<$colWidth)
- return($EndOffset);
- // printf("Offset: %u - colWidth: %u<br>",$EndOffset,$colWidth);
- do
- {
- $EndOffset--;
- while(substr($text,$EndOffset,1)!=' '&&$EndOffset>0)
- $EndOffset--;
- } while($this->_MyStrlen(substr($text,0,$EndOffset))>$colWidth);
-
- // second try, split the word
- if($EndOffset==0)
- {
- $EndOffset=strlen($text);
- while(($this->_MyStrlen(substr($text,0,$EndOffset))>$colWidth)&&$EndOffset>0)
- $EndOffset--;
- }
- return($EndOffset);
- }
-
- /**
- * This function compute the starting position given the
- * text alignment, the left margin and the starting column
- * @param integer $align text alignment
- * @param integer $textWidth text width
- * @param integer $col Orizontal position where start to print the text. This value
- * will be added to the left margin.
- * @access private
- */
- function _BeginCol($align,$textWidth,$col)
- {
- switch($align)
- {
- case 'left':
- $StartCol=$this->Margins['left']+$col;
- break;
- case 'center':
- if(($this->Margins['left']+$this->Margins['right']+$textWidth+$col)>$this->Width)
- $StartCol=$this->Margins['left'];
- else
- $StartCol=$this->Margins['left']+$col+floor(($this->Width-$this->Margins['left']-$this->Margins['right']-$textWidth-$col)/2);
- break;
- case 'right':
- if(($this->Margins['left']+$this->Margins['right']+$textWidth)>$this->Width)
- $StartCol=$this->Margins['left'];
- else
- $StartCol=$this->Width-$this->Margins['right']-$textWidth;
- break;
- default:
- trigger_error('Invalid alignment '.$align,E_USER_ERROR);
- }
- return($StartCol);
- }
-
- /**
- * Append a new text row into this unit
- * @param string
- * @param integer Position from the left border of the column where to start
- * the text
- * @param object CFont font to use to print the text, NULL if not set (only clsPageManager)
- * @access public
- */
- function _AddRow($Text,$StartCol)
- {
- $this->Height+=$this->clsCurrentFont->GetFontHeight();
- $this->innerText[]=$Text;
- $this->LeftText[]=$StartCol;
- }
-
- /**
- * Remove the text for this column, and prepare to receive a new one
- * @access public
- */
- function ClearText()
- {
- $this->innerText=array();
- $this->LeftText=array();
- $this->RowPtr=0;
- $this->Height=0;
- }
-
- /**
- * This column hold some text or not
- * @access public
- * @return bool
- */
- function AlreadyHaveText()
- {
- return($this->Height);
- }
-
- /**
- * Has this column another row of text to retrieve?
- * @access public
- * @return bool
- */
- function HasAnotherText()
- {
- return(array_key_exists($this->RowPtr,$this->innerText));
- }
-
- }
- ?>