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 
00015     abstract class TextDrawer extends Drawer
00016     {
00017         const SPACE_RATIO = 10;
00018         
00019         private $size = null;
00020         
00021         abstract public function draw($text);
00022         
00023         public function __construct($size)
00024         {
00025             $this->size = $size;
00026         }
00027         
00031         public function drawCraracter($angle, $x, $y, $character)
00032         {
00033             $color = $this->getTuringImage()->getOneCharacterColor();
00034             
00035             imagettftext(
00036                 $this->getTuringImage()->getImageId(),
00037                 $this->size,
00038                 $angle,
00039                 $x,
00040                 $y,
00041                 $color,
00042                 $this->getFont(),
00043                 $character
00044             );
00045             
00046             return $this;
00047         }
00048         
00049         protected function getSize()
00050         {
00051             return $this->size;
00052         }
00053         
00057         protected function showError()
00058         {
00059             $drawer = new ErrorDrawer($this->getTuringImage());
00060             $drawer->draw();
00061             
00062             return $this;
00063         }
00064         
00065         protected function getTextWidth($string)
00066         {
00067             $textWidth = 0;
00068             
00069             for ($i = 0, $length = strlen($string); $i < $length; ++$i) {
00070                 $character = $string[$i];
00071                 $textWidth += $this->getStringWidth($character) + $this->getSpace();
00072             }
00073             
00074             return $textWidth;
00075         }
00076         
00077         protected function getStringWidth($string)
00078         {
00079             $bounds = imagettfbbox($this->size, 0, $this->getFont(), $string);
00080             
00081             return $bounds[2] - $bounds[0];
00082         }
00083         
00084         protected function getStringHeight($string)
00085         {
00086             $bounds = imagettfbbox($this->size, 0, $this->getFont(), $string);
00087             
00088             return $bounds[1] - $bounds[7];
00089         }
00090         
00091         protected function getMaxCharacterHeight()
00092         {
00093             return $this->getStringHeight('W'); // bigest character
00094         }
00095         
00096         protected function getSpace()
00097         {
00098             return $this->getSize() / TextDrawer::SPACE_RATIO;
00099         }
00100         
00101         private function getFont()
00102         {
00103             return $this->getTuringImage()->getFont();
00104         }
00105     }
00106 ?>