phpDocumentor SpoolTemplate
[ Back ] [ class tree: SpoolTemplate ] [ index: SpoolTemplate ] [ all elements ]

Source for file sptpl_clsColumn.php

Documentation is available at sptpl_clsColumn.php

  1. <?php
  2. /**
  3. *
  4. * @copyright sptpl_clsColumn.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
  5. * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
  6. * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsColumn.php,v 2.15 2005/03/17 12:46:48 Darvin Exp $
  7. */
  8. /*
  9. * +-------------------------------------------------------------------------+
  10. * | Sptpl |
  11. * +-------------------------------------------------------------------------+
  12. * | Copyright (c) 2003-2005 Andrioli Darvin |
  13. * | Email <darvin (inside) andrioli (dot) com> |
  14. * | Web http://www.andrioli.com/en/sptpl.html |
  15. * | Download http://www.phpclasses.org/browse.html/package/1326.html |
  16. * | |
  17. * +-------------------------------------------------------------------------+
  18. * | This library is free software; you can redistribute it and/or modify |
  19. * | it under the terms of the GNU Lesser General Public License as |
  20. * | published by the Free Software Foundation; either version 2 of the |
  21. * | License, or (at your option) any later version. |
  22. * | |
  23. * | This library is distributed in the hope that it will be useful, but |
  24. * | WITHOUT ANY WARRANTY; without even the implied warranty of |
  25. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  26. * | Lesser General Public License for more details. |
  27. * | |
  28. * | You should have received a copy of the GNU Lesser General Public |
  29. * | License along with this library; if not, write to the Free Software |
  30. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  31. * +-------------------------------------------------------------------------+
  32. */
  33. class CColumn {
  34. /**
  35. * Column width
  36. * @var integer
  37. */
  38. var $Width;
  39. /**
  40. * Left position of this column inside the parent column
  41. * @var integer
  42. */
  43. var $LeftPosition;
  44. /**
  45. * Margins setting.
  46. * Allowed index: top, bottom, left, right
  47. * @var array $Margins
  48. */
  49. var $Margins;
  50.  
  51. /**
  52. * Colpos value used if not specified as parameter into function writeout
  53. * @var integer
  54. * @see WriteOut()
  55. */
  56. var $DefColPos;
  57.  
  58. /**
  59. * Alignment value used if not specified as parameter into function writeout
  60. * @var string
  61. * @see WriteOut()
  62. */
  63. var $DefAlign;
  64. /**
  65. * Default font to use to print the columns text.
  66. * The class use this font if no one is supplied
  67. * NULL if not set
  68. * var object CFont
  69. */
  70. var $clsDefaultFont;
  71. /**
  72. * Real font to use to print the columns text
  73. * NULL if not set
  74. * var object CFont
  75. */
  76. var $clsCurrentFont;
  77.  
  78. /**
  79. * Array of texts row defined inside this unit
  80. * @var array
  81. */
  82. var $innerText;
  83. /**
  84. * For each row from innerText, set the distance from
  85. * the left border of the column
  86. * @var array
  87. * @see $innerText
  88. */
  89. var $LeftText;
  90. /**
  91. * The text inside this unit is retrieved on row basis.
  92. * This variable track which row return on each call to gettext
  93. * @var integer
  94. * @see getText()
  95. */
  96. var $RowPtr;
  97. /**
  98. * Height for the current text stored into innerText
  99. * @var integer
  100. * @see $innerText
  101. * @see AddRow()
  102. */
  103. var $Height;
  104. /**
  105. * Class initializer
  106. * @param object CXml2Array
  107. * @access public
  108. */
  109. function CColumn($Cfg=NULL)
  110. {
  111. // Pad applied to the text inside the column. Default non cell padding
  112. $this->Margins=array('top' =>0,
  113. 'bottom' =>0,
  114. 'left' =>0,
  115. 'right' =>0);
  116. $this->clsDefaultFont=NULL;
  117. $this->DefAlign='left';
  118. if($Cfg!=NULL)
  119. $this->Parse($Cfg);
  120. $this->ClearText(); // reset the inner text structure
  121. }
  122.  
  123. /**
  124. * Parse the configuration node
  125. *
  126. * @param object CXml2Array
  127. * @access public
  128. */
  129. function Parse($Cfg)
  130. {
  131. // Attributes
  132. while(($ret=$Cfg->EachAttribute())!=FALSE)
  133. {
  134. list($AttribName,$value)=$ret;
  135. switch(strtolower($AttribName))
  136. {
  137. case 'leftpos' :
  138. $this->LeftPosition=$value;
  139. break;
  140. case 'width' :
  141. $this->Width=$value;
  142. break;
  143. case 'align' :
  144. $this->DefAlign=$value;
  145. break;
  146. case 'font' :
  147. $Id=$value;
  148. break;
  149. }
  150. }
  151. // Childs
  152. while(($ret=$Cfg->EachChild())!=FALSE)
  153. {
  154. list($ChildName,$Child)=$ret;
  155. switch(strtolower($ChildName)) {
  156. case 'margin' :
  157. $this->_SetMargins($Child);
  158. break;
  159. default:
  160. trigger_error('Unknown tag '.$Child->GetNodeName(),E_USER_ERROR);
  161. break;
  162. }
  163. }
  164. }
  165.  
  166. /**
  167. * Load margins setting from the configuration file
  168. * Default values are set by the class constructor.
  169. * @param object CXml2Array
  170. * @access private
  171. *
  172. */
  173. function _SetMargins($Cfg)
  174. {
  175.  
  176. while(($ret=$Cfg->EachChild())!=FALSE)
  177. {
  178. list($ChildName,$Child)=$ret;
  179. switch(strtolower($ChildName)) {
  180. case 'top' :
  181. $value=trim($Child->GetText());
  182. if(!ctype_digit($value))
  183. trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
  184. $this->Margins['top']=$value;
  185. break;
  186. case 'bottom' :
  187. $value=trim($Child->GetText());
  188. if(!ctype_digit($value))
  189. trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
  190. $this->Margins['bottom']=$value;
  191. break;
  192. case 'left' :
  193. $value=trim($Child->GetText());
  194. if(!ctype_digit($value))
  195. trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
  196. $this->Margins['left']=$value;
  197. break;
  198. case 'right' :
  199. $value=trim($Child->GetText());
  200. if(!ctype_digit($value))
  201. trigger_error('Tag '.$Child->GetNodeName(). ', numeric value expected',E_USER_ERROR);
  202. $this->Margins['right']=$value;
  203. break;
  204. default:
  205. trigger_error('Unknown tag '.$Child->GetNodeName(),E_USER_ERROR);
  206. break;
  207. }
  208. }
  209. }
  210.  
  211. /**
  212. * Function executed at the end of the xml parsing. I run the final configuration
  213. * checks and complete the configration data
  214. * @access public
  215. */
  216. function EndParseXml()
  217. {
  218. $this->_CheckMargins();
  219. }
  220. /**
  221. * Check whether the margins are placed inside or outside the page
  222. * @access private
  223. */
  224. function _CheckMargins()
  225. {
  226. if(($this->Margins['left']+$this->Margins['right'])>$this->Width)
  227. trigger_error('The left or right margin are bigger then the size of the column',E_USER_ERROR);
  228. }
  229.  
  230. /**
  231. * @param integer
  232. * @access public
  233. */
  234. function SetWidth($width)
  235. {
  236. $this->Width=$width;
  237. }
  238. /**
  239. * @param integer
  240. * @access public
  241. */
  242. function SetLeftPos($pos)
  243. {
  244. $this->LeftPosition=$pos;
  245. }
  246. /**
  247. * Return where is the left margin of the column
  248. * @return integer
  249. * @access public
  250. */
  251. function GetLeftPos()
  252. {
  253. return($this->LeftPosition);
  254. }
  255.  
  256. /**
  257. * Return where is the left margin of the text
  258. * @return integer
  259. * @access public
  260. */
  261. function GetLeftTextPos()
  262. {
  263. assert('array_key_exists($this->RowPtr,$this->LeftText)');
  264. return($this->LeftPosition+$this->LeftText[$this->RowPtr]);
  265. }
  266. /**
  267. * Set the default font for the column
  268. * @param object CFont
  269. * @return object CFont
  270. * @access public
  271. */
  272. function SetDefaultFont($NFont)
  273. {
  274. $old=$this->clsDefaultFont;
  275. $this->clsDefaultFont=$NFont;
  276. return($old);
  277. }
  278.  
  279. /**
  280. * Returns the id of the current font
  281. * @return string
  282. * @access public
  283. */
  284. function GetFontId()
  285. {
  286. return($this->clsCurrentFont->Id);
  287. }
  288. /**
  289. * retrieve the text from the column per row basis
  290. * @return string
  291. * @access public
  292. */
  293. function GetText()
  294. {
  295. if(array_key_exists($this->RowPtr,$this->innerText))
  296. return($this->innerText[$this->RowPtr++]);
  297. else
  298. return('');
  299. }
  300.  
  301. // altezza del testo contenuto nell'unita
  302.  
  303. function GetHeight()
  304. {
  305. return($this->Height);
  306. }
  307.  
  308. /**
  309. * Pass the text to the appropriate column. It returns a CUnit object holding
  310. * the text with format (alignment, font) according to the given parameters
  311. * @parameter string $txt Text to write
  312. * @parameter string $align Text alignment, left, center, right
  313. * @parameter integer $colPos
  314. * @parameter integer $Column column where place the text
  315. * @parameter object CFont Font used to print the text, if NULL, it uses the default font
  316. * for the current column
  317. * @access public
  318. */
  319. function WriteOut($txt,$align='',$colPos=0,$pFont=NULL)
  320. {
  321. // How Many row
  322. $rows = explode("\n",rtrim($txt));
  323. $FirstRow=TRUE;
  324. if($pFont!=NULL)
  325. $this->clsCurrentFont=$pFont;
  326. else
  327. $this->clsCurrentFont=$this->clsDefaultFont;
  328. // print_r($FontToUse);
  329. // echo "\n<br>rows: ".print_r_to_var($rows);
  330. $TextOut='';
  331. $alignment=($align=='')?$this->DefAlign:$align;
  332. $this->Height=0;
  333. foreach($rows as $row)
  334. {
  335. $txt=$row;
  336. // If the row haven't any character, archive it as empty row.
  337. if(strlen($txt)==0&&!$FirstRow)
  338. $this->_AddRow("",0);
  339.  
  340. $conta=0;
  341. while(($lenth=$this->_MyStrlen($txt))>0)
  342. {
  343. $conta++;
  344. assert('$conta<90'); // 90rows? reasonably too much
  345. $EndOffset=$this->_CalcEndOffset($txt);
  346. // printf("Completo text len:%u - text:%s - end off:%u<br>",$lenth,$txt,$EndOffset);
  347. if($EndOffset==0)
  348. trigger_error("The column width too small. Enlarge it to allow to cointain at least one character",E_USER_ERROR);
  349. $wkTxt=substr($txt,0,$EndOffset);
  350. $StartCol=$this->_BeginCol($alignment,$this->_MyStrlen($wkTxt),$colPos);
  351. // if($this->Margins['left']) $pad=str_repeat(' ', $this->Margins['left'] + $colPos);
  352. // else $pad='';
  353. // $TextOut=substr($wkTxt,0,$this->Width-$this->Margins['right']);
  354. // printf("Di riga text len:%u - text:%s - start col:%u, font width: %u<br>",$this->_MyStrlen($wkTxt),$wkTxt,$StartCol,$this->clsCurrentFont->Width);
  355. $this->_AddRow($wkTxt,$StartCol);
  356. if(!(strlen($txt)<$EndOffset))
  357. $txt=substr($txt,$EndOffset);
  358. else
  359. $txt='';
  360. }
  361. $FirstRow=FALSE;
  362. }
  363. return;
  364. }
  365.  
  366. function _MyStrlen($text)
  367. {
  368. assert('is_object($this->clsCurrentFont)');
  369. return($this->clsCurrentFont->strlen($text));
  370. }
  371.  
  372. function _CalcEndOffset($text)
  373. {
  374. $colWidth=$this->Width-$this->Margins['right']-$this->Margins['left'];
  375. $EndOffset=strlen($text); // I need the real number of characters
  376. if($this->_MyStrlen($text)<$colWidth)
  377. return($EndOffset);
  378. // printf("Offset: %u - colWidth: %u<br>",$EndOffset,$colWidth);
  379. do
  380. {
  381. $EndOffset--;
  382. while(substr($text,$EndOffset,1)!=' '&&$EndOffset>0)
  383. $EndOffset--;
  384. } while($this->_MyStrlen(substr($text,0,$EndOffset))>$colWidth);
  385.  
  386. // second try, split the word
  387. if($EndOffset==0)
  388. {
  389. $EndOffset=strlen($text);
  390. while(($this->_MyStrlen(substr($text,0,$EndOffset))>$colWidth)&&$EndOffset>0)
  391. $EndOffset--;
  392. }
  393. return($EndOffset);
  394. }
  395.  
  396. /**
  397. * This function compute the starting position given the
  398. * text alignment, the left margin and the starting column
  399. * @param integer $align text alignment
  400. * @param integer $textWidth text width
  401. * @param integer $col Orizontal position where start to print the text. This value
  402. * will be added to the left margin.
  403. * @access private
  404. */
  405. function _BeginCol($align,$textWidth,$col)
  406. {
  407. switch($align)
  408. {
  409. case 'left':
  410. $StartCol=$this->Margins['left']+$col;
  411. break;
  412. case 'center':
  413. if(($this->Margins['left']+$this->Margins['right']+$textWidth+$col)>$this->Width)
  414. $StartCol=$this->Margins['left'];
  415. else
  416. $StartCol=$this->Margins['left']+$col+floor(($this->Width-$this->Margins['left']-$this->Margins['right']-$textWidth-$col)/2);
  417. break;
  418. case 'right':
  419. if(($this->Margins['left']+$this->Margins['right']+$textWidth)>$this->Width)
  420. $StartCol=$this->Margins['left'];
  421. else
  422. $StartCol=$this->Width-$this->Margins['right']-$textWidth;
  423. break;
  424. default:
  425. trigger_error('Invalid alignment '.$align,E_USER_ERROR);
  426. }
  427. return($StartCol);
  428. }
  429.  
  430. /**
  431. * Append a new text row into this unit
  432. * @param string
  433. * @param integer Position from the left border of the column where to start
  434. * the text
  435. * @param object CFont font to use to print the text, NULL if not set (only clsPageManager)
  436. * @access public
  437. */
  438. function _AddRow($Text,$StartCol)
  439. {
  440. $this->Height+=$this->clsCurrentFont->GetFontHeight();
  441. $this->innerText[]=$Text;
  442. $this->LeftText[]=$StartCol;
  443. }
  444.  
  445. /**
  446. * Remove the text for this column, and prepare to receive a new one
  447. * @access public
  448. */
  449. function ClearText()
  450. {
  451. $this->innerText=array();
  452. $this->LeftText=array();
  453. $this->RowPtr=0;
  454. $this->Height=0;
  455. }
  456.  
  457. /**
  458. * This column hold some text or not
  459. * @access public
  460. * @return bool
  461. */
  462. function AlreadyHaveText()
  463. {
  464. return($this->Height);
  465. }
  466.  
  467. /**
  468. * Has this column another row of text to retrieve?
  469. * @access public
  470. * @return bool
  471. */
  472. function HasAnotherText()
  473. {
  474. return(array_key_exists($this->RowPtr,$this->innerText));
  475. }
  476.  
  477. }
  478. ?>

Documentation generated on Mon, 28 Mar 2005 15:13:12 +0200 by phpDocumentor 1.3.0RC3