CurvedStringDrawer.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     final class CurvedStringDrawer extends TextDrawer
00016     {
00017         const MAX_ANGLE_CHANGE              = 40;
00018         const MAX_ANGLE                     = 45;
00019         const MAX_VERTIVAL_POSITION_CHANGE  = 1.5;
00020         
00024         public function draw($string)
00025         {
00026             $turingImage = $this->getTuringImage();
00027             
00028             $textWidth =
00029                 $this->getTextWidth($string)
00030                 + (strlen($string) - 1)
00031                 * $this->getSize() / 2;
00032             
00033             if ($turingImage->getWidth() <= $textWidth)
00034                 return $this->showError();
00035             
00036             $angle =
00037                 mt_rand(
00038                     -CurvedStringDrawer::MAX_ANGLE_CHANGE / 2,
00039                     CurvedStringDrawer::MAX_ANGLE_CHANGE / 2
00040                 );
00041             
00042             $maxHeight = $this->getMaxCharacterHeight();
00043             
00044             $y = round(($turingImage->getHeight() + $maxHeight) / 2);
00045             $x = round(($turingImage->getWidth() - $textWidth) / 2);
00046             
00047             for ($size = strlen($string), $i = 0; $i < $size; ++$i) {
00048                 $angle +=
00049                     mt_rand(
00050                         -CurvedStringDrawer::MAX_ANGLE_CHANGE / 2,
00051                         CurvedStringDrawer::MAX_ANGLE_CHANGE / 2
00052                     );
00053                 
00054                 if ($angle > CurvedStringDrawer::MAX_ANGLE)
00055                     $angle = CurvedStringDrawer::MAX_ANGLE;
00056                 elseif ($angle < -CurvedStringDrawer::MAX_ANGLE)
00057                     $angle = -CurvedStringDrawer::MAX_ANGLE;
00058 
00059                 $y +=
00060                     mt_rand(
00061                         -$turingImage->getHeight() / 2,
00062                         $turingImage->getHeight() / 2
00063                     );
00064                 
00065                 if ($y < ($maxHeight * CurvedStringDrawer::MAX_VERTIVAL_POSITION_CHANGE))
00066                     $y = $maxHeight * CurvedStringDrawer::MAX_VERTIVAL_POSITION_CHANGE;
00067                 
00068                 if ($y > ($turingImage->getHeight() - $maxHeight))
00069                     $y = $turingImage->getHeight() - $maxHeight;
00070 
00071                 $character = $string[$i];
00072                 $this->drawCraracter($angle, $x, $y, $character);
00073                 
00074                 $x += $this->getStringWidth($character) + $this->getSize() / 2;
00075             }
00076             
00077             return $this;
00078         }
00079     }
00080 ?>