Phundament-Components
[ class tree: Phundament-Components ] [ index: Phundament-Components ] [ all elements ]

Source for file LFontgen.php

Documentation is available at LFontgen.php

  1. <?php
  2.  
  3.  
  4. /**
  5.  * Class file.
  6.  *
  7.  * @license http://opensource.org/licenses/mozilla1.1.php Mozilla Public License
  8.  * @copyright 2005, diemeisterei GmbH. All rights reserved.
  9.  * @author $Author: schmunk $
  10.  * @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 $
  11.  * @package Phundament.Components
  12.  * @subpackage WebControls
  13.  */
  14.  
  15. /**
  16.  * LFontgen
  17.  *
  18.  * Adds the ability to render custom font text images
  19.  *
  20.  * @package System.Web.UI.WebControls
  21.  * @subpackage Graphics
  22.  */
  23. class LFontgen extends TControl
  24. {
  25.     private $OutputDir"";
  26.     private $RealOutputDir"";
  27.     private $BinPath"";
  28.  
  29.     private $HTCUrl"";
  30.  
  31.     private $FontID;
  32.     private $AscenderHeight;
  33.     private $DescenderHeight;
  34.     private $SpaceWidth;
  35.  
  36.     private $FR0;
  37.     private $FG0;
  38.     private $FB0;
  39.     private $FA0;
  40.  
  41.     private $BR0;
  42.     private $BG0;
  43.     private $BB0;
  44.     private $BA127;
  45.  
  46.     private $LoadedFontsarray ();
  47.  
  48.     public function getText()
  49.     {
  50.         return $this->getViewState('Text''');
  51.     }
  52.     public function setText($value)
  53.     {
  54.         $this->setViewState('Text'$value'');
  55.     }
  56.  
  57.     public function getFont()
  58.     {
  59.         return $this->getViewState('Font''');
  60.     }
  61.     public function setFont($value)
  62.     {
  63.         $this->setViewState('Font'$value'');
  64.     }
  65.  
  66.     public function getFontSize()
  67.     {
  68.         return $this->getViewState('FontSize'60);
  69.     }
  70.     public function setFontSize($value)
  71.     {
  72.         $this->setViewState('FontSize'$value60);
  73.     }
  74.  
  75.     public function getLineHeight()
  76.     {
  77.         return $this->getViewState('LineHeight'$this->getFontSize());
  78.     }
  79.     public function setLineHeight($value)
  80.     {
  81.         $this->setViewState('LineHeight'$value$this->getFontSize());
  82.     }
  83.  
  84.     public function getColor()
  85.     {
  86.         return $this->getViewState('Color'"");
  87.     }
  88.     public function setColor($value)
  89.     {
  90.         $this->setViewState('Color'$value"");
  91.     }
  92.  
  93.     public function getBackgroundColor()
  94.     {
  95.         return $this->getViewState('BackgroundColor'"");
  96.     }
  97.     public function setBackgroundColor($value)
  98.     {
  99.         $this->setViewState('BackgroundColor'$value"");
  100.     }
  101.  
  102.     public function getSpace()
  103.     {
  104.         return $this->getViewState('Space'0);
  105.     }
  106.     public function setSpace($value)
  107.     {
  108.         $this->setViewState('Space'$value0);
  109.     }
  110.  
  111.     public function getType()
  112.     {
  113.         return $this->getViewState('Type''png');
  114.     }
  115.     public function setType($value)
  116.     {
  117.         $this->setViewState('Type'strtolower($value));
  118.     }
  119.  
  120.     public function getTightness()
  121.     {
  122.         return $this->getViewState('Tightness'0);
  123.     }
  124.     public function setTightness($value)
  125.     {
  126.         $this->setViewState('Tightness'$value0);
  127.     }
  128.  
  129.     public function getAngle()
  130.     {
  131.         return $this->getViewState('Angle'0);
  132.     }
  133.     public function setAngle($value)
  134.     {
  135.         $this->setViewState('Angle'$value0);
  136.     }
  137.  
  138.     public function getAASteps()
  139.     {
  140.         return $this->getViewState('AASteps'16);
  141.     }
  142.     public function setAASteps($value)
  143.     {
  144.         $this->setViewState('AASteps'$value16);
  145.     }
  146.  
  147.     public function getIEFix()
  148.     {
  149.         return $this->getViewState('IEFix'1);
  150.     }
  151.     public function setIEFix($value)
  152.     {
  153.         $this->setViewState('IEFix'$value1);
  154.     }
  155.  
  156.     public function getAdjustBaseline()
  157.     {
  158.         return $this->getViewState('AdjustBaseline'true);
  159.     }
  160.     public function setAdjustBaseline($value)
  161.     {
  162.         $this->setViewState('AdjustBaseline'TPropertyValue :: ensureBoolean($value)true);
  163.     }
  164.  
  165.     public function extractColor($colString$R$G$B$A)
  166.     {
  167.         $matchesarray ();
  168.         $singletrue;
  169.         preg_match("/^#([0-9A-Fa-f])([0-9A-Fa-f])([0-9A-Fa-f])$/"$colString$matches);
  170.         if (count($matches== 0)
  171.         {
  172.             preg_match("/^#([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})([0-9A-Fa-f]{2})$/"$colString$matches);
  173.             $singlefalse;
  174.         }
  175.         if (count($matches== 0)
  176.             return false;
  177.  
  178.         $Rhexdec($matches[1]);
  179.         $Ghexdec($matches[2]);
  180.         $Bhexdec($matches[3]);
  181.         $A0;
  182.         if ($single)
  183.         {
  184.             $R *= 17;
  185.             $G *= 17;
  186.             $B *= 17;
  187.         }
  188.         return true;
  189.     }
  190.  
  191.     public function onPreRender($param)
  192.     {
  193.         parent :: onPreRender($param);
  194.         try
  195.         {
  196.             $this->prepareFont();
  197.         }
  198.         catch (Exception $e)
  199.         {
  200.             Prado :: log($e->getMessage()8"Lithron.LFontgen");
  201.         }
  202.  
  203.         # only add transparent background behaviour if IEFix is not set
  204.         if ($this->getIEFix(== 1)
  205.         {
  206.             $script"img {behavior: url(" $this->publishAsset("LFontgen/iefix.htc"")}";
  207.             $scriptMgr$this->getPage()->getClientScript();
  208.             $scriptMgr->registerStyleSheet("LFontgenIEFix"$script);
  209.         }
  210.     }
  211.  
  212.     public function prepareFont()
  213.     {
  214.         static $loadedFonts;
  215.  
  216.         if (is_file($this->Font))
  217.         {
  218.             #Prado :: trace("Preparing font (" .
  219.             #$this->SkinId . ") ...", "Lithron.LFontGen");
  220.         }
  221.         else
  222.         {
  223.             Prado :: log("Font not found (" $this->Font ") ..."16"Lithron.LFontGen");
  224.             return;
  225.         }
  226.  
  227.         if (isset($loadedFonts[$this->Font]))
  228.         {
  229.             $this->FontID$loadedFonts[$this->Font];
  230.             #Prado :: trace("Using static font var ...", "Lithron.LFontGen");
  231.         }
  232.         else
  233.         {
  234.             Prado :: trace("Loading font (" $this->Font ") ...""Lithron.LFontGen");
  235.             $loadedFonts[$this->Font]$this->FontIDimagepsloadfont($this->Font);
  236.         }
  237.  
  238.         $this->HTCUrl$this->publishAsset("LFontgen/iefix.htc");
  239.  
  240.         $param$this->Application->getParameters();
  241.         $this->OutputDir$param->itemAt("OutputDir");
  242.         $this->RealOutputDirrealpath($this->OutputDir);
  243.  
  244.         imagepsencodefont($this->FontIDdirname(__FILE__'/LFontgen/isolatin1.enc');
  245.  
  246.         list ($lx$ly$rx$ry)imagepsbbox("A"$this->FontID$this->getFontSize());
  247.         $this->AscenderHeight$ry $ly;
  248.         list ($lx$ly$rx$ry)imagepsbbox("g"$this->FontID$this->getFontSize());
  249.         $this->DescenderHeight= - $ly;
  250.         $_swimagepsbbox("x"$this->FontID$this->getFontSize());
  251.         list ($lx$ly$rx$ry)$_sw;
  252.         $this->SpaceWidth$rx;
  253.  
  254.         // get color
  255.         $fgAvail$this->extractColor($this->getColor()$this->FR$this->FG$this->FB$this->FA);
  256.         $bgAvail$this->extractColor($this->getBackgroundColor()$this->BR$this->BG$this->BB$this->BA);
  257.         if (!$bgAvail && $fgAvail)
  258.         {
  259.             $this->BR$this->FR;
  260.             $this->BG$this->FG;
  261.             $this->BB$this->FB;
  262.         }
  263.         #Prado::trace("Font prepared.", "Lithron.LFontGen");
  264.     }
  265.  
  266.     public function renderWordGD($word)
  267.     {
  268.         $idarray ();
  269.         $id[]$word;
  270.         $id[]$this->getFont();
  271.         $id[]$this->getFontSize();
  272.         $id[]$this->getLineHeight();
  273.         $id[]$this->getSpace();
  274.         $id[]$this->getTightness();
  275.         $id[]$this->getAngle();
  276.         $id[]$this->getAASteps();
  277.         $id[]$this->FR;
  278.         $id[]$this->FG;
  279.         $id[]$this->FB;
  280.         $id[]$this->FA;
  281.         $id[]$this->BR;
  282.         $id[]$this->BG;
  283.         $id[]$this->BB;
  284.         $id[]$this->BA;
  285.  
  286.         $hashmd5(implode("|"$id));
  287.         $name$this->OutputDir "/" $hash "." $this->getType();
  288.         $realname$this->RealOutputDir "/" $hash "." $this->getType();
  289.         if (file_exists($realname))
  290.             return $name;
  291.  
  292.         //get the left lower corner and the right upper
  293.         list ($lx$ly$rx$ry)imagepsbbox($word$this->FontID$this->getFontSize()$this->getSpace()$this->getTightness()$this->getAngle());
  294.  
  295.         $imimagecreatetruecolor($rx +1$this->getLineHeight(1);
  296.         imagealphablending($imfalse);
  297.         imagesavealpha($imtrue);
  298.         $fgcolimagecolorallocatealpha($im$this->FR$this->FG$this->FB$this->FA);
  299.         $bgcolimagecolorallocatealpha($im$this->BR$this->BG$this->BB$this->BA);
  300.         imagefill($im00$bgcol);
  301.         imagepstext($im$word$this->FontID$this->getFontSize()$fgcol$bgcol0$this->getLineHeight($this->DescenderHeight$this->getSpace()$this->getTightness()$this->getAngle()$this->getAASteps());
  302.  
  303.         if ($this->getType(== "jpg")
  304.             imagejpeg($im$realname90);
  305.         else
  306.             imagepng($im$realname);
  307.         imagedestroy($im);
  308.  
  309.         return $name;
  310.     }
  311.  
  312.     /**
  313.      * Renders body content.
  314.      * @param THtmlWriter writer
  315.      */
  316.     public function render($writer)
  317.     {
  318.         //$this->prepareFont();
  319.         if (!$this->FontID)
  320.         {
  321.             Prado :: log("Font rendering aborted."8"Lithron.LFontGen");
  322.             return;
  323.         }
  324.  
  325.         $textutf8_decode($this->getText());
  326.  
  327.         $wordssplit(" "$text);
  328.         foreach ($words as $i => $word)
  329.         {
  330.             $words[$i]trim($word);
  331.             if ($words[$i== "")
  332.                 unset ($words[$i]);
  333.         }
  334.  
  335.         foreach ($words as $wnum => $word)
  336.         {
  337.             try
  338.             {
  339.                 $file$this->renderWordGD($word);
  340.             }
  341.             catch (Exception $e)
  342.             {
  343.                 Prado :: log($e->getMessage()8"Lithron.LFontgen");
  344.                 return;
  345.             }
  346.  
  347.             $imgsizegetimagesize($file);
  348.  
  349.             $writer->addAttribute('src'$file);
  350.             $writer->addAttribute('width'$imgsize[0]);
  351.             $writer->addAttribute('height'$imgsize[1]);
  352.             $writer->addAttribute('alt'utf8_encode($word));
  353.             $writer->addStyleAttribute('border-width','0px');
  354.  
  355.             $writer->addStyleAttribute('width'$imgsize[0"px");
  356.             $writer->addStyleAttribute('height'$imgsize[1"px");
  357.             $writer->addStyleAttribute('background-image'$this->publishAsset("LFontgen/space.gif"));
  358.             $writer->addStyleAttribute('background''transparent');
  359.             #$writer->addStyleAttribute('margin', '0');
  360.  
  361.             if ($wnum count($words1)
  362.                 $writer->addStyleAttribute('margin-right'($this->SpaceWidth $this->getSpace(1.
  363.                 'px');
  364.  
  365.             if ($this->getAdjustBaseline())
  366.             {
  367.                 $writer->addStyleAttribute('position''relative');
  368.                 $writer->addStyleAttribute('top'($this->DescenderHeight 1'px');
  369.             }
  370.  
  371.             $writer->renderBeginTag('img');
  372.             $writer->renderEndTag();
  373.         }
  374.  
  375.         // not needed ? TODO ?
  376.         #Prado::trace("Freeing font (".$this->SkinId.")", "Lithron.LFontgen");
  377.         #imagepsfreefont($this->FontID);
  378.     }
  379.  
  380. }
  381. ?>

Documentation generated on Sun, 25 Feb 2007 16:10:27 +0100 by phpDocumentor 1.3.1