00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
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');
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 ?>