[ class tree: phpchartPlus ] [ index: phpchartPlus ] [ all elements ]

Source for file Font.php

Documentation is available at Font.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: Font.php
  6. **************************************************
  7. Author: Tsiavos Chris <jaames@freemail.gr>
  8. Date: October 2004
  9. **************************************************/
  10.  
  11. /**
  12. *Draws characters in the chart image
  13. *This class is used to draw characters in the chart image according to a set
  14. *of user-defined properties
  15. *@author Tsiavos Chris <jaames@freemail.gr>
  16. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  17. *@final
  18. */
  19.  
  20. final class Font {
  21. /**
  22. *reference to the image handler the font will be used in
  23. *@access private
  24. *@var mixed
  25. */
  26. private $Canvas;
  27. /**
  28. *reference to the ColorAllocator the class will use for allocating the font color
  29. *@access private
  30. *@var ColorAllocator
  31. */
  32. private $ColorAllocator;
  33. /**
  34. *the file path containing the desired FreeType2 font
  35. *@access private
  36. *@var string
  37. */
  38. private $FontFileLocation;
  39. /**
  40. *the font color the class will use for drawing characters in the chart image
  41. *@access private
  42. *@var string
  43. */
  44. private $FontColor;
  45. /**
  46. *the font size
  47. *@access private
  48. *@var integer
  49. */
  50. private $FontSize;
  51. /**
  52. *the font width of the built-in font
  53. *@access private
  54. *@var integer
  55. */
  56. private $FontWidth;
  57. /**
  58. *the font height of the built-in font
  59. *@access private
  60. *vvar integer
  61. */
  62. private $FontHeight;
  63. /**
  64. *Constructor
  65. */
  66. function __construct() { }
  67. /**
  68. *Sets the font properties
  69. *@param mixed $Canvas Reference to the image handler
  70. *@param ColorAllocator $ColorAllocator Reference to the ColorAllocator instance
  71. *@param FontFileLocation $FontFileLocation The file path containing the desired FreeType2 font
  72. *@param integer $FontFize The font size
  73. *@param integer $FontWidth The font width of the built-in font used when $FontFileLocation is null
  74. *@param integer $FontHeight The font height of the built-in font used when $FontFileLocation is null
  75. *@return void
  76. */
  77. function set_Properties(&$Canvas,ColorAllocator &$ColorAllocator,$FontFileLocation,$FontSize,$FontWidth,$FontHeight) {
  78. $this->Canvas=$Canvas;
  79. $this->ColorAllocator=$ColorAllocator;
  80. $this->FontFileLocation=$FontFileLocation;
  81. $this->FontSize=$FontSize;
  82. $this->FontWidth=$FontWidth;
  83. $this->FontHeight=$FontHeight;
  84. }
  85. /**
  86. *Returns the font metrics (font width,font height) of the selected font (FreeType2 or built-in)
  87. *@param string $Text
  88. *@return associative array
  89. *@access public
  90. */
  91. public function get_FontMetrics($Text)
  92. {
  93. if ($this->FontFileLocation!=NULL) {
  94. $FontMetrics=Imageftbbox($this->FontSize,0,$this->FontFileLocation,$Text);
  95. $Width=$FontMetrics[4]-1;
  96. $Height=$FontMetrics[5];
  97. }
  98. else
  99. {
  100. $Width=ImageFontWidth($this->FontWidth)*strlen($Text);
  101. $Height=ImageFontHeight($this->FontHeight);
  102. }
  103. return array("FontWidth"=>$Width,"FontHeight"=>abs($Height));
  104. }
  105. /**
  106. *@param integer $XPos the x-value of the position the string will be drawn
  107. *@param integet $Ypos the y-value of the position the string will be drawn
  108. *@param string $Text
  109. *@param string $Color the color of the drawn string
  110. *@return void
  111. *@access public
  112. */
  113. public function draw_String($XPos,$YPos,$Text,$Color)
  114. {
  115. $ColorHandler=$this->ColorAllocator->Allocate($this->Canvas,$Color,$Color,0,1);
  116. if ($this->FontFileLocation!=NULL) {
  117. $FontMetrics=$this->get_FontMetrics($Text);
  118. //ttf uses bottom corner as starting point whereas imagestring the top corner
  119. $YPos+=$FontMetrics["FontHeight"];
  120. Imagefttext($this->Canvas,$this->FontSize,0,$XPos,$YPos,$ColorHandler,$this->FontFileLocation,$Text);
  121. }
  122. else
  123. ImageString($this->Canvas,$this->FontWidth,$XPos,$YPos,$Text,$ColorHandler);
  124. }
  125.  
  126. }
  127.  
  128.  
  129. ?>

Documentation generated on Sun, 3 Oct 2004 14:59:16 +0300 by phpDocumentor 1.3.0RC3