- <?php
- /**
- * Class file.
- *
- * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
- * @copyright 2005, diemeisterei GmbH. All rights reserved.
- * @author $Author: schmunk $
- * @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 $
- * @package Lithron
- * @subpackage none
- */
-
- /**
- * LFontgen
- *
- * Adds the ability to render custom font text images
- *
- * @package System.Web.UI.WebControls
- * @subpackage None
- */
- class LFontgen extends TControl
- {
- protected $OutputDir = "";
- protected $RealOutputDir = "";
- protected $BinPath = "";
-
- protected $HTCUrl = "";
-
- protected $FontID;
- protected $AscenderHeight;
- protected $DescenderHeight;
- protected $SpaceWidth;
-
- protected $FR = 0;
- protected $FG = 0;
- protected $FB = 0;
- protected $FA = 0;
-
- protected $BR = 0;
- protected $BG = 0;
- protected $BB = 0;
- protected $BA = 127;
-
-
- public function getText()
- {
- return $this->getViewState('Text','');
- }
- public function setText($value)
- {
- $this->setViewState('Text',$value,'');
- }
-
- public function getFont()
- {
- return $this->getViewState('Font','');
- }
- public function setFont($value)
- {
- $this->setViewState('Font',$value,'');
- }
-
- public function getFontSize()
- {
- return $this->getViewState('FontSize',60);
- }
- public function setFontSize($value)
- {
- $this->setViewState('FontSize',$value,60);
- }
-
- public function getLineHeight()
- {
- return $this->getViewState('LineHeight',$this->getFontSize());
- }
- public function setLineHeight($value)
- {
- $this->setViewState('LineHeight',$value,$this->getFontSize());
- }
-
- public function getColor()
- {
- return $this->getViewState('Color',"");
- }
- public function setColor($value)
- {
- $this->setViewState('Color',$value,"");
- }
-
- public function getBackgroundColor()
- {
- return $this->getViewState('BackgroundColor',"");
- }
- public function setBackgroundColor($value)
- {
- $this->setViewState('BackgroundColor',$value,"");
- }
-
- public function getSpace()
- {
- return $this->getViewState('Space',0);
- }
- public function setSpace($value)
- {
- $this->setViewState('Space',$value,0);
- }
-
- public function getTightness()
- {
- return $this->getViewState('Tightness',0);
- }
- public function setTightness($value)
- {
- $this->setViewState('Tightness',$value,0);
- }
-
- public function getAngle()
- {
- return $this->getViewState('Angle',0);
- }
- public function setAngle($value)
- {
- $this->setViewState('Angle',$value,0);
- }
-
- public function getAASteps()
- {
- return $this->getViewState('AASteps',16);
- }
- public function setAASteps($value)
- {
- $this->setViewState('AASteps',$value,16);
- }
-
- public function extractColor($colString, &$R, &$G, &$B, &$A)
- {
- $matches = array();
- $single = true;
- preg_match("/^#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$/", $colString, $matches);
- if (count($matches) == 0)
- {
- preg_match("/^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/", $colString, $matches);
- $single = false;
- }
- if (count($matches) == 0)
- return false;
-
- $R = hexdec($matches[1]);
- $G = hexdec($matches[2]);
- $B = hexdec($matches[3]);
- $A = 0;
- if ($single)
- {
- $R *= 17;
- $G *= 17;
- $B *= 17;
- }
- return true;
- }
-
- public function onPreRender($param)
- {
- parent::onPreRender($param);
-
- $this->HTCUrl = $this->publishAsset("png_ie.htc");
-
- $param = $this->Application->getParameters();
- $this->OutputDir= $param->itemAt("OutputDir");
- $this->RealOutputDir = realpath($this->OutputDir);
-
- //echo "font:{$this->Font}<br>";
- $this->FontID = imagepsloadfont($this->getFont());
-
- // get ascender & descender & space width
- list($lx,$ly,$rx,$ry) = imagepsbbox("A", $this->FontID, $this->getFontSize());
- $this->AscenderHeight = $ry - $ly;
- list($lx,$ly,$rx,$ry) = imagepsbbox("g", $this->FontID, $this->getFontSize());
- $this->DescenderHeight = - $ly;
- list($lx,$ly,$rx,$ry) = imagepsbbox("x", $this->FontID, $this->getFontSize());
- $this->SpaceWidth = $rx;
- //echo "asc:{$this->AscenderHeight},desc:{$this->DescenderHeight},sw:{$this->SpaceWidth}<br>";
-
- // get color
- $fgAvail = $this->extractColor($this->getColor(), $this->FR, $this->FG, $this->FB, $this->FA);
- $bgAvail = $this->extractColor($this->getBackgroundColor(), $this->BR, $this->BG, $this->BB, $this->BA);
- if (!$bgAvail && $fgAvail)
- {
- $this->BR = $this->FR;
- $this->BG = $this->FG;
- $this->BB = $this->FB;
- }
- }
-
-
- public function renderWordGD($word)
- {
- $id = array();
- $id[] = $word; $id[] = $this->getFont(); $id[] = $this->getFontSize(); $id[] = $this->getLineHeight();
- $id[] = $this->getSpace(); $id[] = $this->getTightness(); $id[] = $this->getAngle(); $id[] = $this->getAASteps();
- $id[] = $this->FR; $id[] = $this->FG; $id[] = $this->FB; $id[] = $this->FA;
- $id[] = $this->BR; $id[] = $this->BG; $id[] = $this->BB; $id[] = $this->BA;
-
- $hash = md5(implode("|", $id));
- $name = $this->OutputDir."/".$hash.".png";
- $realname = $this->RealOutputDir."/".$hash.".png";
- if (file_exists($realname)) return $name;
-
- //get the left lower corner and the right upper
- list($lx,$ly,$rx,$ry) = imagepsbbox($word, $this->FontID, $this->getFontSize(), $this->getSpace(), $this->getTightness(), $this->getAngle());
-
- $im = imagecreatetruecolor($rx+1, $this->getLineHeight()+1);
- imagealphablending($im, false);
- imagesavealpha($im, true);
- $fgcol = imagecolorallocatealpha($im, $this->FR, $this->FG, $this->FB, $this->FA);
- $bgcol = imagecolorallocatealpha($im, $this->BR, $this->BG, $this->BB, $this->BA);
- imagefill($im, 0, 0, $bgcol);
- imagepstext($im, $word, $this->FontID, $this->getFontSize(), $fgcol, $bgcol, 0, $this->getLineHeight() - $this->DescenderHeight, $this->getSpace(), $this->getTightness(), $this->getAngle(), $this->getAASteps());
- imagepng($im, $realname);
- imagedestroy($im);
-
- return $name;
- }
-
-
- /**
- * Renders body content.
- * @param THtmlWriter writer
- */
- public function render($writer)
- {
- if ($this->FontID === false) return;
-
- $text = $this->getText();
- $words = split(" ", $text);
- foreach($words as $i => $word)
- {
- $words[$i] = trim($word);
- if ($words[$i] == "") unset($words[$i]);
- }
-
- foreach($words as $wnum => $word)
- {
- //echo "generating word $word<br>";
- $file = $this->renderWordGD($word);
-
- $writer->addAttribute('src', $file);
- $writer->addAttribute('alt', $word);
- $writer->addAttribute('border', '0');
- //$writer->addStyleAttribute('border-width','0px');
- if ($wnum < count($words)-1)
- $writer->addStyleAttribute('margin-right',($this->SpaceWidth+$this->getSpace()-1).'px');
- $writer->addStyleAttribute('position','relative');
- $writer->addStyleAttribute('top', ($this->DescenderHeight+1).'px');
- $writer->addStyleAttribute('behavior','url(\''.$this->HTCUrl.'\')');
- $writer->renderBeginTag('img');
- $writer->renderEndTag();
- }
-
- imagepsfreefont($this->FontID);
- }
- }
- ?>