- <?php
-
- /*
- **************************************************
- Class: Font.php
- **************************************************
- Author: Tsiavos Chris <jaames@freemail.gr>
- Date: October 2004
- **************************************************/
-
- /**
- *Draws characters in the chart image
- *This class is used to draw characters in the chart image according to a set
- *of user-defined properties
- *@author Tsiavos Chris <jaames@freemail.gr>
- *@license http://opensource.org/licenses/gpl-license.php GNU Public License
- *@final
- */
-
- final class Font {
-
- /**
- *reference to the image handler the font will be used in
- *@access private
- *@var mixed
- */
- private $Canvas;
-
- /**
- *reference to the ColorAllocator the class will use for allocating the font color
- *@access private
- *@var ColorAllocator
- */
- private $ColorAllocator;
-
- /**
- *the file path containing the desired FreeType2 font
- *@access private
- *@var string
- */
- private $FontFileLocation;
-
- /**
- *the font color the class will use for drawing characters in the chart image
- *@access private
- *@var string
- */
- private $FontColor;
-
- /**
- *the font size
- *@access private
- *@var integer
- */
- private $FontSize;
-
- /**
- *the font width of the built-in font
- *@access private
- *@var integer
- */
- private $FontWidth;
-
- /**
- *the font height of the built-in font
- *@access private
- *vvar integer
- */
- private $FontHeight;
-
- /**
- *Constructor
- */
- function __construct() { }
-
- /**
- *Sets the font properties
- *@param mixed $Canvas Reference to the image handler
- *@param ColorAllocator $ColorAllocator Reference to the ColorAllocator instance
- *@param FontFileLocation $FontFileLocation The file path containing the desired FreeType2 font
- *@param integer $FontFize The font size
- *@param integer $FontWidth The font width of the built-in font used when $FontFileLocation is null
- *@param integer $FontHeight The font height of the built-in font used when $FontFileLocation is null
- *@return void
- */
- function set_Properties(&$Canvas,ColorAllocator &$ColorAllocator,$FontFileLocation,$FontSize,$FontWidth,$FontHeight) {
-
- $this->Canvas=$Canvas;
- $this->ColorAllocator=$ColorAllocator;
- $this->FontFileLocation=$FontFileLocation;
- $this->FontSize=$FontSize;
- $this->FontWidth=$FontWidth;
- $this->FontHeight=$FontHeight;
- }
-
- /**
- *Returns the font metrics (font width,font height) of the selected font (FreeType2 or built-in)
- *@param string $Text
- *@return associative array
- *@access public
- */
- public function get_FontMetrics($Text)
- {
- if ($this->FontFileLocation!=NULL) {
- $FontMetrics=Imageftbbox($this->FontSize,0,$this->FontFileLocation,$Text);
- $Width=$FontMetrics[4]-1;
- $Height=$FontMetrics[5];
- }
- else
- {
- $Width=ImageFontWidth($this->FontWidth)*strlen($Text);
- $Height=ImageFontHeight($this->FontHeight);
- }
-
- return array("FontWidth"=>$Width,"FontHeight"=>abs($Height));
- }
-
- /**
- *@param integer $XPos the x-value of the position the string will be drawn
- *@param integet $Ypos the y-value of the position the string will be drawn
- *@param string $Text
- *@param string $Color the color of the drawn string
- *@return void
- *@access public
- */
-
- public function draw_String($XPos,$YPos,$Text,$Color)
- {
- $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$Color,$Color,0,1);
-
- if ($this->FontFileLocation!=NULL) {
- $FontMetrics=$this->get_FontMetrics($Text);
- //ttf uses bottom corner as starting point whereas imagestring the top corner
- $YPos+=$FontMetrics["FontHeight"];
- Imagefttext($this->Canvas,$this->FontSize,0,$XPos,$YPos,$ColorHandler,$this->FontFileLocation,$Text);
- }
- else
- ImageString($this->Canvas,$this->FontWidth,$XPos,$YPos,$Text,$ColorHandler);
- }
-
- }
-
-
- ?>