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

Source for file sptpl_clsFont.php

Documentation is available at sptpl_clsFont.php

  1. <?
  2. /**
  3. * Class CFont definition module
  4. *
  5. * @copyright sptpl_clsFont.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
  6. * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
  7. * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsFont.php,v 2.7 2005/03/17 12:46:48 Darvin Exp $
  8. */
  9. /*
  10. * +-------------------------------------------------------------------------+
  11. * | Sptpl |
  12. * +-------------------------------------------------------------------------+
  13. * | Copyright (c) 2003-2005 Andrioli Darvin |
  14. * | Email <darvin (inside) andrioli (dot) com> |
  15. * | Web http://www.andrioli.com/en/sptpl.html |
  16. * | Download http://www.phpclasses.org/browse.html/package/1326.html |
  17. * | |
  18. * +-------------------------------------------------------------------------+
  19. * | This library is free software; you can redistribute it and/or modify |
  20. * | it under the terms of the GNU Lesser General Public License as |
  21. * | published by the Free Software Foundation; either version 2 of the |
  22. * | License, or (at your option) any later version. |
  23. * | |
  24. * | This library is distributed in the hope that it will be useful, but |
  25. * | WITHOUT ANY WARRANTY; without even the implied warranty of |
  26. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  27. * | Lesser General Public License for more details. |
  28. * | |
  29. * | You should have received a copy of the GNU Lesser General Public |
  30. * | License along with this library; if not, write to the Free Software |
  31. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  32. * +-------------------------------------------------------------------------+
  33. */
  34.  
  35. class CFont {
  36.  
  37. /**
  38. * Font identifier, used as key to access to this font
  39. * from CRow
  40. * @var string $Id
  41. */
  42. var $Id;
  43. /**
  44. * Font face
  45. * @var string $Face
  46. */
  47. var $Face;
  48. /**
  49. * Font size
  50. * @var int $Size
  51. */
  52. var $Size;
  53. /**
  54. * Font width
  55. * @var int $Size
  56. */
  57. var $width;
  58. /**
  59. * Font height
  60. * @var int $Height
  61. */
  62. var $Height;
  63. /**
  64. * Is the font bold
  65. * @var boolean $Bold
  66. */
  67. var $Bold;
  68. /**
  69. * Is the font underlined
  70. * @var boolean $Underline
  71. */
  72. var $Underline;
  73. /**
  74. * Is the font italic
  75. * @var boolean $Italic
  76. */
  77. var $Italic;
  78.  
  79. /**
  80. * class initializer
  81. *
  82. * @param object DomNode $node DOMXML node holding the configuration information
  83. * for the font
  84. * @access public
  85. */
  86. function CFont($node=NULL)
  87. {
  88. $this->Id='';
  89. $this->Face='Courier New';
  90. $this->Size='12';
  91. $this->Height=ceil($this->Size/2);
  92. $this->Bold=FALSE;
  93. $this->Underline=FALSE;
  94. $this->Italic=FALSE;
  95. if($node!=NULL)
  96. $this->ParseXml($node);
  97. }
  98.  
  99. /**
  100. * Parse the configuration file
  101. *
  102. * @param object CXml2Array $Cfg CXml2Array node holding the configuration information
  103. * for the font
  104. * @access public
  105. */
  106. function ParseXml($Cfg)
  107. {
  108. // Check some attribute
  109. while(($ret=$Cfg->EachAttribute())!=FALSE)
  110. {
  111. list($AttribName,$value)=$ret;
  112. switch(strtolower($AttribName))
  113. {
  114. case 'id' :
  115. $this->Id=$value;
  116. break;
  117. case 'face' :
  118. $this->Face=$value;
  119. break;
  120. case 'size' :
  121. $this->Size=$value;
  122. $this->Height=$value;
  123. if(!is_numeric($this->Size))
  124. trigger_error('Configuration error: font with attribute size not numeric',E_USER_ERROR);
  125. $this->Width=$this->Size;
  126. break;
  127. case 'underline' :
  128. $this->Underline=(strtolower($value)=='n')?FALSE:TRUE;
  129. break;
  130. case 'bold' :
  131. $this->Bold=(strtolower($value)=='n')?FALSE:TRUE;
  132. break;
  133. case 'italic' :
  134. $this->Italic=(strtolower($value)=='n')?FALSE:TRUE;
  135. break;
  136. }
  137. }
  138. if($this->Id=='')
  139. trigger_error('Configuration error: font without attribute id',E_USER_ERROR);
  140. if(!is_numeric($this->Size))
  141. trigger_error('Configuration error: font with attribute size not numeric',E_USER_ERROR);
  142. }
  143.  
  144. /**
  145. * Return the identifier for this font. The id is the key to
  146. * access to this font from the row
  147. *
  148. * @access public
  149. */
  150.  
  151. function GetId()
  152. {
  153. return($this->Id);
  154. }
  155.  
  156. /**
  157. * Property Face get value
  158. * @access public
  159. * @return string
  160. */
  161. function GetFace()
  162. {
  163. return($this->Face);
  164. }
  165. /**
  166. * Property Size get value
  167. * @access public
  168. * @return string
  169. */
  170. function GetSize()
  171. {
  172. return($this->Size);
  173. }
  174. /**
  175. * Property width get value
  176. * @access public
  177. * @return string
  178. */
  179. function GetWidth()
  180. {
  181. return($this->width);
  182. }
  183. /**
  184. * Property Height get value
  185. * @access public
  186. * @return string
  187. */
  188. function GetHeight()
  189. {
  190. return($this->Height);
  191. }
  192. /**
  193. * Property Bold get value
  194. * @access public
  195. * @return string
  196. */
  197. function GetBold()
  198. {
  199. return($this->Bold);
  200. }
  201. /**
  202. * Property Underline get value
  203. * @access public
  204. * @return string
  205. */
  206. function GetUnderline()
  207. {
  208. return($this->Underline);
  209. }
  210. /**
  211. * Property Italic get value
  212. * @access public
  213. * @return string
  214. */
  215. function GetItalic()
  216. {
  217. return($this->Italic);
  218. }
  219.  
  220. /**
  221. * return the string length
  222. * @param string
  223. * @return integer
  224. * @access public
  225. */
  226. function strlen($text)
  227. {
  228. return(strlen($text)*$this->Width);
  229. }
  230.  
  231. /**
  232. * return the high the characters
  233. * @return integer
  234. * @access public
  235. */
  236. function GetFontHeight($text='')
  237. {
  238. return($this->Height);
  239. }
  240. }
  241.  
  242. /**
  243. * Extension of the font class used to support the Printer output. Used by CPagePrinter
  244. */
  245. class CPrFont extends CFont
  246. {
  247.  
  248. function CPrFont($node=NULL)
  249. {
  250. parent::CFont($node);
  251. }
  252.  
  253. /**
  254. * Parse the configuration file. Call the parent parsing function.
  255. * The real difference betwenn the base class: the size si multiply by 20
  256. *
  257. * @param object CXml2Array $Cfg CXml2Array node holding the configuration information
  258. * for the font
  259. * @access public
  260. */
  261. function ParseXml($Cfg)
  262. {
  263. parent::ParseXml($Cfg);
  264. $this->Width=ceil($this->Size/2);
  265. $this->Height*=20;
  266. $this->Width*=20;
  267. $this->Size*=20;
  268. }
  269.  
  270. }
  271. /
  272. * Extension of the font class used the PostScript output
  273. */
  274.  
  275. class CPsFont extends CFont
  276. {
  277. /**
  278. * private handler of the ps module used to get the real size of the
  279. * font
  280. * @var object Ps
  281. */
  282. var $clsPs;
  283. /**
  284. * Postscript font handler
  285. * @var resource
  286. */
  287. var $psfont;
  288. function CPsFont($node=NULL)
  289. {
  290. parent::CFont($node);
  291. if(strtolower(substr($this->Face,-4))=='.afm')
  292. {
  293. // wipe out the extension from the fonr name. the ps module doesn't need it.
  294. $this->Face=substr($this->Face,0,-4);
  295. }
  296. }
  297. /**
  298. * Set the PostScript module handler from pagemanager
  299. * @var resource
  300. */
  301. function SetPsHandler($Handler)
  302. {
  303. $this->clsPs=$Handler;
  304. $this->psfont=ps_findfont($this->clsPs, $this->Face, "", 0);
  305. $text='Wgq';
  306. ps_setfont($this->clsPs, $this->psfont, $this->Size);
  307. $size=ps_string_geometry($this->clsPs,$text);
  308. $this->Height=floor((abs($size['descender'])+$size['ascender']+5)*20);
  309. }
  310. /**
  311. * Property Size get value
  312. * @access public
  313. * @return string
  314. */
  315. function GetSize()
  316. {
  317. return($this->Size*20);
  318. }
  319. /**
  320. * Property width get value
  321. * @access public
  322. * @return string
  323. */
  324. function GetWidth()
  325. {
  326. return($this->width*20);
  327. }
  328. /**
  329. * Property Height get value
  330. * @access public
  331. * @return string
  332. */
  333. function GetHeight()
  334. {
  335. return($this->Height);
  336. }
  337. /**
  338. * Parse the configuration file. Call the parent parsing function. At the end
  339. * set the font into its own Cpdf istance
  340. *
  341. * @param object CXml2Array $Cfg CXml2Array node holding the configuration information
  342. * for the font
  343. * @access public
  344. */
  345. function ParseXml($Cfg)
  346. {
  347. parent::ParseXml($Cfg);
  348. }
  349. /**
  350. * return the string length
  351. * @param string
  352. * @return integer
  353. * @access public
  354. */
  355. function strlen($text)
  356. {
  357. $o=ps_setfont($this->clsPs, $this->psfont, $this->Size);
  358.  
  359. if(strlen($text)>0)
  360. {
  361. switch($this->Size)
  362. {
  363. case 8: $fix=1.6;
  364. break;
  365. case 9: $fix=1.6;
  366. break;
  367. case 10: $fix=1.5;
  368. break;
  369. case 11: $fix=1.5;
  370. break;
  371. case 12: $fix=1.4;
  372. break;
  373. default:
  374. if($this->Size<8)
  375. $fix=1.6;
  376. if($this->Size>12)
  377. $fix=1.3;
  378. }
  379. $txtlen=ps_stringwidth($this->clsPs,$text)*$fix;
  380. }
  381. else
  382. $txtlen=0;
  383. // echo "\n->$text<- $o - ".$this->Size." - ".$this->Face." - ".floor($txtlen*20);
  384. return(floor($txtlen*20));
  385. }
  386.  
  387. /**
  388. * return the high the characters
  389. * @return integer
  390. * @access public
  391. */
  392. function GetFontHeight($text='Wgq')
  393. {
  394. ps_setfont($this->clsPs, $this->psfont, $this->Size);
  395. $size=ps_string_geometry($this->clsPs,$text);
  396. $this->Height=floor((abs($size['descender'])+$size['ascender']+10)*20);
  397. return($this->Height);
  398. }
  399. }
  400.  
  401. /**
  402. * Extension of the font class used to support the Pdf output. Used by CPagePdf
  403. */
  404. class CPdfFont extends CFont
  405. {
  406.  
  407. /**
  408. * private instance of the pdf class used to get the real size of the
  409. * font
  410. * @var object Cpdf
  411. */
  412. var $clsPdf;
  413. function CPdfFont($node=NULL)
  414. {
  415. parent::CFont($node);
  416. }
  417.  
  418. /**
  419. * Parse the configuration file. Call the parent parsing function. At the end
  420. * set the font into its own Cpdf istance
  421. *
  422. * @param object CXml2Array $Cfg CXml2Array node holding the configuration information
  423. * for the font
  424. * @access public
  425. */
  426. function ParseXml($Cfg)
  427. {
  428. parent::ParseXml($Cfg);
  429. $this->clsPdf=new Cpdf();
  430. $this->clsPdf->selectFont(PDFFONTDIR.$this->Face);
  431. }
  432.  
  433. /**
  434. * return the string length
  435. * @param string
  436. * @return integer
  437. * @access public
  438. */
  439. function strlen($text)
  440. {
  441. if(strlen($text)>0)
  442. $txtlen=$this->clsPdf->getTextWidth($this->Size,$text);
  443. else
  444. $txtlen=0;
  445. return($txtlen);
  446. }
  447.  
  448. /**
  449. * return the high the characters
  450. * @return integer
  451. * @access public
  452. */
  453. function GetFontHeight($text='')
  454. {
  455. return($this->clsPdf->getFontHeight($this->Size));
  456. }
  457. /
  458.  
  459. }
  460. ?>

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