TextDrawer.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Dmitry E. Demidov                          *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: TextDrawer.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     abstract class TextDrawer extends Drawer
00017     {
00018         const SPACE_RATIO = 10;
00019         
00020         private $size = null;
00021         
00022         abstract public function draw($text);
00023         
00024         public function __construct($size)
00025         {
00026             $this->size = $size;
00027         }
00028         
00032         public function drawCraracter($angle, $x, $y, $character)
00033         {
00034             $color = $this->getTuringImage()->getOneCharacterColor();
00035             
00036             imagettftext(
00037                 $this->getTuringImage()->getImageId(),
00038                 $this->size,
00039                 $angle,
00040                 $x,
00041                 $y,
00042                 $color,
00043                 $this->getFont(),
00044                 $character
00045             );
00046             
00047             return $this;
00048         }
00049         
00050         protected function getSize()
00051         {
00052             return $this->size;
00053         }
00054         
00058         protected function showError()
00059         {
00060             $drawer = new ErrorDrawer($this->getTuringImage());
00061             $drawer->draw();
00062             
00063             return $this;
00064         }
00065 
00066         protected function getTextWidth($string)
00067         {
00068             $textWidth = 0;
00069             
00070             for ($i = 0, $length = strlen($string); $i < $length; ++$i) {
00071                 $character = $string[$i];
00072                 $textWidth += $this->getStringWidth($character) + $this->getSpace();
00073             }
00074             
00075             return $textWidth;
00076         }
00077         
00078         protected function getStringWidth($string)
00079         {
00080             $bounds = imagettfbbox($this->size, 0, $this->getFont(), $string);
00081             
00082             return $bounds[2] - $bounds[0];
00083         }
00084         
00085         protected function getStringHeight($string)
00086         {
00087             $bounds = imagettfbbox($this->size, 0, $this->getFont(), $string);
00088             
00089             return $bounds[1] - $bounds[7];
00090         }
00091         
00092         protected function getMaxCharacterHeight()
00093         {
00094             return $this->getStringHeight('W'); // bigest character
00095         }
00096         
00097         protected function getSpace()
00098         {
00099             return $this->getSize() / TextDrawer::SPACE_RATIO;
00100         }
00101         
00102         private function getFont()
00103         {
00104             return $this->getTuringImage()->getFont();
00105         }
00106     }
00107 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4