PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/lib/jpgraph/src/jpgraph_text.inc.php

https://github.com/nephie/AZL-website
PHP | 302 lines | 211 code | 44 blank | 47 comment | 31 complexity | 530655d544357d833b78b0bba131a5ec MD5 | raw file
  1. <?php
  2. //=======================================================================
  3. // File: JPGRAPH_TEXT.INC.PHP
  4. // Description: Class to handle text as object in the graph.
  5. // The low level text layout engine is handled by the GD class
  6. // Created: 2001-01-08 (Refactored to separate file 2008-08-01)
  7. // Ver: $Id: jpgraph_text.inc.php 1844 2009-09-26 17:05:31Z ljp $
  8. //
  9. // Copyright (c) Aditus Consulting. All rights reserved.
  10. //========================================================================
  11. //===================================================
  12. // CLASS Text
  13. // Description: Arbitrary text object that can be added to the graph
  14. //===================================================
  15. class Text {
  16. public $t,$margin=0;
  17. public $x=0,$y=0,$halign="left",$valign="top",$color=array(0,0,0);
  18. public $hide=false, $dir=0;
  19. public $iScalePosY=null,$iScalePosX=null;
  20. public $iWordwrap=0;
  21. public $font_family=FF_FONT1,$font_style=FS_NORMAL,$font_size=12;
  22. protected $boxed=false; // Should the text be boxed
  23. protected $paragraph_align="left";
  24. protected $icornerradius=0,$ishadowwidth=3;
  25. protected $fcolor='white',$bcolor='black',$shadow=false;
  26. protected $iCSIMarea='',$iCSIMalt='',$iCSIMtarget='',$iCSIMWinTarget='';
  27. private $iBoxType = 1; // Which variant of filled box around text we want
  28. //---------------
  29. // CONSTRUCTOR
  30. // Create new text at absolute pixel coordinates
  31. function __construct($aTxt="",$aXAbsPos=0,$aYAbsPos=0) {
  32. if( ! is_string($aTxt) ) {
  33. JpGraphError::RaiseL(25050);//('First argument to Text::Text() must be s atring.');
  34. }
  35. $this->t = $aTxt;
  36. $this->x = round($aXAbsPos);
  37. $this->y = round($aYAbsPos);
  38. $this->margin = 0;
  39. }
  40. //---------------
  41. // PUBLIC METHODS
  42. // Set the string in the text object
  43. function Set($aTxt) {
  44. $this->t = $aTxt;
  45. }
  46. // Alias for Pos()
  47. function SetPos($aXAbsPos=0,$aYAbsPos=0,$aHAlign="left",$aVAlign="top") {
  48. //$this->Pos($aXAbsPos,$aYAbsPos,$aHAlign,$aVAlign);
  49. $this->x = $aXAbsPos;
  50. $this->y = $aYAbsPos;
  51. $this->halign = $aHAlign;
  52. $this->valign = $aVAlign;
  53. }
  54. function SetScalePos($aX,$aY) {
  55. $this->iScalePosX = $aX;
  56. $this->iScalePosY = $aY;
  57. }
  58. // Specify alignment for the text
  59. function Align($aHAlign,$aVAlign="top",$aParagraphAlign="") {
  60. $this->halign = $aHAlign;
  61. $this->valign = $aVAlign;
  62. if( $aParagraphAlign != "" )
  63. $this->paragraph_align = $aParagraphAlign;
  64. }
  65. // Alias
  66. function SetAlign($aHAlign,$aVAlign="top",$aParagraphAlign="") {
  67. $this->Align($aHAlign,$aVAlign,$aParagraphAlign);
  68. }
  69. // Specifies the alignment for a multi line text
  70. function ParagraphAlign($aAlign) {
  71. $this->paragraph_align = $aAlign;
  72. }
  73. // Specifies the alignment for a multi line text
  74. function SetParagraphAlign($aAlign) {
  75. $this->paragraph_align = $aAlign;
  76. }
  77. function SetShadow($aShadowColor='gray',$aShadowWidth=3) {
  78. $this->ishadowwidth=$aShadowWidth;
  79. $this->shadow=$aShadowColor;
  80. $this->boxed=true;
  81. }
  82. function SetWordWrap($aCol) {
  83. $this->iWordwrap = $aCol ;
  84. }
  85. // Specify that the text should be boxed. fcolor=frame color, bcolor=border color,
  86. // $shadow=drop shadow should be added around the text.
  87. function SetBox($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadowColor=false,$aCornerRadius=4,$aShadowWidth=3) {
  88. if( $aFrameColor === false ) {
  89. $this->boxed=false;
  90. }
  91. else {
  92. $this->boxed=true;
  93. }
  94. $this->fcolor=$aFrameColor;
  95. $this->bcolor=$aBorderColor;
  96. // For backwards compatibility when shadow was just true or false
  97. if( $aShadowColor === true ) {
  98. $aShadowColor = 'gray';
  99. }
  100. $this->shadow=$aShadowColor;
  101. $this->icornerradius=$aCornerRadius;
  102. $this->ishadowwidth=$aShadowWidth;
  103. }
  104. function SetBox2($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadowColor=false,$aCornerRadius=4,$aShadowWidth=3) {
  105. $this->iBoxType=2;
  106. $this->SetBox($aFrameColor,$aBorderColor,$aShadowColor,$aCornerRadius,$aShadowWidth);
  107. }
  108. // Hide the text
  109. function Hide($aHide=true) {
  110. $this->hide=$aHide;
  111. }
  112. // This looks ugly since it's not a very orthogonal design
  113. // but I added this "inverse" of Hide() to harmonize
  114. // with some classes which I designed more recently (especially)
  115. // jpgraph_gantt
  116. function Show($aShow=true) {
  117. $this->hide=!$aShow;
  118. }
  119. // Specify font
  120. function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
  121. $this->font_family=$aFamily;
  122. $this->font_style=$aStyle;
  123. $this->font_size=$aSize;
  124. }
  125. // Center the text between $left and $right coordinates
  126. function Center($aLeft,$aRight,$aYAbsPos=false) {
  127. $this->x = $aLeft + ($aRight-$aLeft )/2;
  128. $this->halign = "center";
  129. if( is_numeric($aYAbsPos) )
  130. $this->y = $aYAbsPos;
  131. }
  132. // Set text color
  133. function SetColor($aColor) {
  134. $this->color = $aColor;
  135. }
  136. function SetAngle($aAngle) {
  137. $this->SetOrientation($aAngle);
  138. }
  139. // Orientation of text. Note only TTF fonts can have an arbitrary angle
  140. function SetOrientation($aDirection=0) {
  141. if( is_numeric($aDirection) )
  142. $this->dir=$aDirection;
  143. elseif( $aDirection=="h" )
  144. $this->dir = 0;
  145. elseif( $aDirection=="v" )
  146. $this->dir = 90;
  147. else
  148. JpGraphError::RaiseL(25051);//(" Invalid direction specified for text.");
  149. }
  150. // Total width of text
  151. function GetWidth($aImg) {
  152. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  153. $w = $aImg->GetTextWidth($this->t,$this->dir);
  154. return $w;
  155. }
  156. // Hight of font
  157. function GetFontHeight($aImg) {
  158. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  159. $h = $aImg->GetFontHeight();
  160. return $h;
  161. }
  162. function GetTextHeight($aImg) {
  163. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  164. $h = $aImg->GetTextHeight($this->t,$this->dir);
  165. return $h;
  166. }
  167. function GetHeight($aImg) {
  168. // Synonym for GetTextHeight()
  169. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  170. $h = $aImg->GetTextHeight($this->t,$this->dir);
  171. return $h;
  172. }
  173. // Set the margin which will be interpretated differently depending
  174. // on the context.
  175. function SetMargin($aMarg) {
  176. $this->margin = $aMarg;
  177. }
  178. function StrokeWithScale($aImg,$axscale,$ayscale) {
  179. if( $this->iScalePosX === null || $this->iScalePosY === null ) {
  180. $this->Stroke($aImg);
  181. }
  182. else {
  183. $this->Stroke($aImg,
  184. round($axscale->Translate($this->iScalePosX)),
  185. round($ayscale->Translate($this->iScalePosY)));
  186. }
  187. }
  188. function SetCSIMTarget($aURITarget,$aAlt='',$aWinTarget='') {
  189. $this->iCSIMtarget = $aURITarget;
  190. $this->iCSIMalt = $aAlt;
  191. $this->iCSIMWinTarget = $aWinTarget;
  192. }
  193. function GetCSIMareas() {
  194. if( $this->iCSIMtarget !== '' ) {
  195. return $this->iCSIMarea;
  196. }
  197. else {
  198. return '';
  199. }
  200. }
  201. // Display text in image
  202. function Stroke($aImg,$x=null,$y=null) {
  203. if( $x !== null ) $this->x = round($x);
  204. if( $y !== null ) $this->y = round($y);
  205. // Insert newlines
  206. if( $this->iWordwrap > 0 ) {
  207. $this->t = wordwrap($this->t,$this->iWordwrap,"\n");
  208. }
  209. // If position been given as a fraction of the image size
  210. // calculate the absolute position
  211. if( $this->x < 1 && $this->x > 0 ) $this->x *= $aImg->width;
  212. if( $this->y < 1 && $this->y > 0 ) $this->y *= $aImg->height;
  213. $aImg->PushColor($this->color);
  214. $aImg->SetFont($this->font_family,$this->font_style,$this->font_size);
  215. $aImg->SetTextAlign($this->halign,$this->valign);
  216. if( $this->boxed ) {
  217. if( $this->fcolor=="nofill" ) {
  218. $this->fcolor=false;
  219. }
  220. $oldweight=$aImg->SetLineWeight(1);
  221. if( $this->iBoxType == 2 && $this->font_family > FF_FONT2+2 ) {
  222. $bbox = $aImg->StrokeBoxedText2($this->x, $this->y,
  223. $this->t, $this->dir,
  224. $this->fcolor,
  225. $this->bcolor,
  226. $this->shadow,
  227. $this->paragraph_align,
  228. 2,4,
  229. $this->icornerradius,
  230. $this->ishadowwidth);
  231. }
  232. else {
  233. $bbox = $aImg->StrokeBoxedText($this->x,$this->y,$this->t,
  234. $this->dir,$this->fcolor,$this->bcolor,$this->shadow,
  235. $this->paragraph_align,3,3,$this->icornerradius,
  236. $this->ishadowwidth);
  237. }
  238. $aImg->SetLineWeight($oldweight);
  239. }
  240. else {
  241. $debug=false;
  242. $bbox = $aImg->StrokeText($this->x,$this->y,$this->t,$this->dir,$this->paragraph_align,$debug);
  243. }
  244. // Create CSIM targets
  245. $coords = $bbox[0].','.$bbox[1].','.$bbox[2].','.$bbox[3].','.$bbox[4].','.$bbox[5].','.$bbox[6].','.$bbox[7];
  246. $this->iCSIMarea = "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($this->iCSIMtarget)."\" ";
  247. if( trim($this->iCSIMalt) != '' ) {
  248. $this->iCSIMarea .= " alt=\"".$this->iCSIMalt."\" ";
  249. $this->iCSIMarea .= " title=\"".$this->iCSIMalt."\" ";
  250. }
  251. if( trim($this->iCSIMWinTarget) != '' ) {
  252. $this->iCSIMarea .= " target=\"".$this->iCSIMWinTarget."\" ";
  253. }
  254. $this->iCSIMarea .= " />\n";
  255. $aImg->PopColor($this->color);
  256. }
  257. } // Class
  258. ?>