System.Web.UI.WebControls
[ class tree: System.Web.UI.WebControls ] [ index: System.Web.UI.WebControls ] [ all elements ]

Source for file LFontgen.php

Documentation is available at LFontgen.php

  1. <?php
  2. /**
  3. * Class file.
  4. *
  5. * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  6. * @copyright 2005, diemeisterei GmbH. All rights reserved.
  7. * @author $Author: schmunk $
  8. * @version $Revision: 236 $ $Date: 2006-05-30 23:39:46 +0200 (Di, 30 Mai 2006) $ $HeadURL: https://svn.sourceforge.net/svnroot/lithron/trunk/packages/LContainer/LBrick.php $
  9. * @package Lithron
  10. * @subpackage none
  11. */
  12.  
  13. /**
  14. * LFontgen
  15. *
  16. * Adds the ability to render custom font text images
  17. *
  18. * @package System.Web.UI.WebControls
  19. * @subpackage None
  20. */
  21. class LFontgen extends TControl
  22. {
  23. protected $OutputDir = "";
  24. protected $RealOutputDir = "";
  25. protected $BinPath = "";
  26.  
  27. protected $HTCUrl = "";
  28.  
  29. protected $FontID;
  30. protected $AscenderHeight;
  31. protected $DescenderHeight;
  32. protected $SpaceWidth;
  33. protected $FR = 0;
  34. protected $FG = 0;
  35. protected $FB = 0;
  36. protected $FA = 0;
  37. protected $BR = 0;
  38. protected $BG = 0;
  39. protected $BB = 0;
  40. protected $BA = 127;
  41. public function getText()
  42. {
  43. return $this->getViewState('Text','');
  44. }
  45. public function setText($value)
  46. {
  47. $this->setViewState('Text',$value,'');
  48. }
  49.  
  50. public function getFont()
  51. {
  52. return $this->getViewState('Font','');
  53. }
  54. public function setFont($value)
  55. {
  56. $this->setViewState('Font',$value,'');
  57. }
  58.  
  59. public function getFontSize()
  60. {
  61. return $this->getViewState('FontSize',60);
  62. }
  63. public function setFontSize($value)
  64. {
  65. $this->setViewState('FontSize',$value,60);
  66. }
  67.  
  68. public function getLineHeight()
  69. {
  70. return $this->getViewState('LineHeight',$this->getFontSize());
  71. }
  72. public function setLineHeight($value)
  73. {
  74. $this->setViewState('LineHeight',$value,$this->getFontSize());
  75. }
  76.  
  77. public function getColor()
  78. {
  79. return $this->getViewState('Color',"");
  80. }
  81. public function setColor($value)
  82. {
  83. $this->setViewState('Color',$value,"");
  84. }
  85.  
  86. public function getBackgroundColor()
  87. {
  88. return $this->getViewState('BackgroundColor',"");
  89. }
  90. public function setBackgroundColor($value)
  91. {
  92. $this->setViewState('BackgroundColor',$value,"");
  93. }
  94.  
  95. public function getSpace()
  96. {
  97. return $this->getViewState('Space',0);
  98. }
  99. public function setSpace($value)
  100. {
  101. $this->setViewState('Space',$value,0);
  102. }
  103.  
  104. public function getTightness()
  105. {
  106. return $this->getViewState('Tightness',0);
  107. }
  108. public function setTightness($value)
  109. {
  110. $this->setViewState('Tightness',$value,0);
  111. }
  112.  
  113. public function getAngle()
  114. {
  115. return $this->getViewState('Angle',0);
  116. }
  117. public function setAngle($value)
  118. {
  119. $this->setViewState('Angle',$value,0);
  120. }
  121.  
  122. public function getAASteps()
  123. {
  124. return $this->getViewState('AASteps',16);
  125. }
  126. public function setAASteps($value)
  127. {
  128. $this->setViewState('AASteps',$value,16);
  129. }
  130.  
  131. public function extractColor($colString, &$R, &$G, &$B, &$A)
  132. {
  133. $matches = array();
  134. $single = true;
  135. preg_match("/^#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$/", $colString, $matches);
  136. if (count($matches) == 0)
  137. {
  138. preg_match("/^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/", $colString, $matches);
  139. $single = false;
  140. }
  141. if (count($matches) == 0)
  142. return false;
  143. $R = hexdec($matches[1]);
  144. $G = hexdec($matches[2]);
  145. $B = hexdec($matches[3]);
  146. $A = 0;
  147. if ($single)
  148. {
  149. $R *= 17;
  150. $G *= 17;
  151. $B *= 17;
  152. }
  153. return true;
  154. }
  155.  
  156. public function onPreRender($param)
  157. {
  158. parent::onPreRender($param);
  159.  
  160. $this->HTCUrl = $this->publishAsset("png_ie.htc");
  161.  
  162. $param = $this->Application->getParameters();
  163. $this->OutputDir= $param->itemAt("OutputDir");
  164. $this->RealOutputDir = realpath($this->OutputDir);
  165.  
  166. //echo "font:{$this->Font}<br>";
  167. $this->FontID = imagepsloadfont($this->getFont());
  168.  
  169. // get ascender & descender & space width
  170. list($lx,$ly,$rx,$ry) = imagepsbbox("A", $this->FontID, $this->getFontSize());
  171. $this->AscenderHeight = $ry - $ly;
  172. list($lx,$ly,$rx,$ry) = imagepsbbox("g", $this->FontID, $this->getFontSize());
  173. $this->DescenderHeight = - $ly;
  174. list($lx,$ly,$rx,$ry) = imagepsbbox("x", $this->FontID, $this->getFontSize());
  175. $this->SpaceWidth = $rx;
  176. //echo "asc:{$this->AscenderHeight},desc:{$this->DescenderHeight},sw:{$this->SpaceWidth}<br>";
  177. // get color
  178. $fgAvail = $this->extractColor($this->getColor(), $this->FR, $this->FG, $this->FB, $this->FA);
  179. $bgAvail = $this->extractColor($this->getBackgroundColor(), $this->BR, $this->BG, $this->BB, $this->BA);
  180. if (!$bgAvail && $fgAvail)
  181. {
  182. $this->BR = $this->FR;
  183. $this->BG = $this->FG;
  184. $this->BB = $this->FB;
  185. }
  186. }
  187.  
  188.  
  189. public function renderWordGD($word)
  190. {
  191. $id = array();
  192. $id[] = $word; $id[] = $this->getFont(); $id[] = $this->getFontSize(); $id[] = $this->getLineHeight();
  193. $id[] = $this->getSpace(); $id[] = $this->getTightness(); $id[] = $this->getAngle(); $id[] = $this->getAASteps();
  194. $id[] = $this->FR; $id[] = $this->FG; $id[] = $this->FB; $id[] = $this->FA;
  195. $id[] = $this->BR; $id[] = $this->BG; $id[] = $this->BB; $id[] = $this->BA;
  196. $hash = md5(implode("|", $id));
  197. $name = $this->OutputDir."/".$hash.".png";
  198. $realname = $this->RealOutputDir."/".$hash.".png";
  199. if (file_exists($realname)) return $name;
  200.  
  201. //get the left lower corner and the right upper
  202. list($lx,$ly,$rx,$ry) = imagepsbbox($word, $this->FontID, $this->getFontSize(), $this->getSpace(), $this->getTightness(), $this->getAngle());
  203. $im = imagecreatetruecolor($rx+1, $this->getLineHeight()+1);
  204. imagealphablending($im, false);
  205. imagesavealpha($im, true);
  206. $fgcol = imagecolorallocatealpha($im, $this->FR, $this->FG, $this->FB, $this->FA);
  207. $bgcol = imagecolorallocatealpha($im, $this->BR, $this->BG, $this->BB, $this->BA);
  208. imagefill($im, 0, 0, $bgcol);
  209. imagepstext($im, $word, $this->FontID, $this->getFontSize(), $fgcol, $bgcol, 0, $this->getLineHeight() - $this->DescenderHeight, $this->getSpace(), $this->getTightness(), $this->getAngle(), $this->getAASteps());
  210. imagepng($im, $realname);
  211. imagedestroy($im);
  212. return $name;
  213. }
  214.  
  215.  
  216. /**
  217. * Renders body content.
  218. * @param THtmlWriter writer
  219. */
  220. public function render($writer)
  221. {
  222. if ($this->FontID === false) return;
  223.  
  224. $text = $this->getText();
  225. $words = split(" ", $text);
  226. foreach($words as $i => $word)
  227. {
  228. $words[$i] = trim($word);
  229. if ($words[$i] == "") unset($words[$i]);
  230. }
  231.  
  232. foreach($words as $wnum => $word)
  233. {
  234. //echo "generating word $word<br>";
  235. $file = $this->renderWordGD($word);
  236.  
  237. $writer->addAttribute('src', $file);
  238. $writer->addAttribute('alt', $word);
  239. $writer->addAttribute('border', '0');
  240. //$writer->addStyleAttribute('border-width','0px');
  241. if ($wnum < count($words)-1)
  242. $writer->addStyleAttribute('margin-right',($this->SpaceWidth+$this->getSpace()-1).'px');
  243. $writer->addStyleAttribute('position','relative');
  244. $writer->addStyleAttribute('top', ($this->DescenderHeight+1).'px');
  245. $writer->addStyleAttribute('behavior','url(\''.$this->HTCUrl.'\')');
  246. $writer->renderBeginTag('img');
  247. $writer->renderEndTag();
  248. }
  249.  
  250. imagepsfreefont($this->FontID);
  251. }
  252. }
  253. ?>

Documentation generated on Tue, 20 Jun 2006 05:15:10 +0200 by phpDocumentor 1.3.0RC4