# InclinedStringDrawer.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 InclinedStringDrawer extends TextDrawer
    00016     {
    00017         const MAX_ANGLE = 70;
    00018
    00022         public function draw($string)
    00023         {
    00024             $textWidth = $this->getTextWidth($string);
    00025             $textHeight = $this->getMaxCharacterHeight();
    00026
    00027             if ($textWidth < $this->getTuringImage()->getHeight()) {
    00028                 $maxAngle = 45;
    00029             } else {
    00030                 $maxAngle =
    00031                     rad2deg(
    00032                         asin(
    00033                             ($this->getTuringImage()->getHeight() - $textHeight)
    00034                             / $textWidth
    00035                         )
    00036                     );
    00037             }
    00038
    00039             $angle = mt_rand(-$maxAngle / 2, $maxAngle / 2);
    00040
    00041             if ($angle > self::MAX_ANGLE)
    00042                 $angle = self::MAX_ANGLE;
    00043
    00044             if ($angle < -self::MAX_ANGLE)
    00045                 $angle = -self::MAX_ANGLE;
    00046
    00047             if ($this->getTuringImage()->getWidth() > $textWidth) {
    00048                 $x = round(
    00049                     (
    00050                         ($this->getTuringImage()->getWidth() - $textWidth)
    00051                         * cos(deg2rad($angle))
    00052                     )
    00053                     / 2
    00054                 );
    00055
    00056                 $y = round(
    00057                     (
    00058                         ($this->getTuringImage()->getHeight() + $textWidth)
    00059                         * sin(deg2rad($angle))
    00060                     )
    00061                     / 2
    00062                     + ($textHeight / 2)
    00063                 );
    00064
    00065                 for ($i = 0, $length = strlen($string); $i < $length; ++$i) {
    00066                     $character = $string[$i];
    00067
    00068                     $this->drawCraracter($angle, $x, $y, $character);
    00069
    00070                     $charWidth =
    00071                         $this->getStringWidth($character)
    00072                         + $this->getSpace();
    00073
    00074                     $y -= $charWidth * sin(deg2rad($angle));
    00075                     $x += $charWidth * cos(deg2rad($angle));
    00076                 }
    00077             } else
    00078                 return $this->showError();
    00079
    00080             return $this;
    00081         }
    00082     }
    00083 ?>