source: trunk/client/modules/Elezioni/grafici/jpgraph_text.inc.php@ 265

Last change on this file since 265 was 265, checked in by roby, 5 years ago
File size: 10.7 KB
RevLine 
[2]1<?php
2//=======================================================================
[265]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 $
[2]8//
[265]9// Copyright (c) Asial Corporation. All rights reserved.
[2]10//========================================================================
11
12
13//===================================================
14// CLASS Text
15// Description: Arbitrary text object that can be added to the graph
16//===================================================
17class Text {
[265]18 public $t;
[2]19 public $x=0,$y=0,$halign="left",$valign="top",$color=array(0,0,0);
20 public $hide=false, $dir=0;
21 public $iScalePosY=null,$iScalePosX=null;
22 public $iWordwrap=0;
[265]23 public $font_family=FF_DEFAULT,$font_style=FS_NORMAL; // old. FF_FONT1
24 protected $boxed=false; // Should the text be boxed
[2]25 protected $paragraph_align="left";
26 protected $icornerradius=0,$ishadowwidth=3;
27 protected $fcolor='white',$bcolor='black',$shadow=false;
28 protected $iCSIMarea='',$iCSIMalt='',$iCSIMtarget='',$iCSIMWinTarget='';
[265]29 private $iBoxType = 1; // Which variant of filled box around text we want
[2]30
[265]31 // for __get, __set
32 private $_margin;
33 private $_font_size=8; // old. 12
[2]34
[265]35 //---------------
36 // CONSTRUCTOR
37
[2]38 // Create new text at absolute pixel coordinates
[265]39 function __construct($aTxt="",$aXAbsPos=0,$aYAbsPos=0) {
40 if( ! is_string($aTxt) ) {
41 JpGraphError::RaiseL(25050);//('First argument to Text::Text() must be s atring.');
42 }
43 $this->t = $aTxt;
44 $this->x = round($aXAbsPos);
45 $this->y = round($aYAbsPos);
46 $this->margin = 0;
[2]47 }
[265]48 //---------------
49 // PUBLIC METHODS
[2]50 // Set the string in the text object
51 function Set($aTxt) {
[265]52 $this->t = $aTxt;
[2]53 }
[265]54
[2]55 // Alias for Pos()
56 function SetPos($aXAbsPos=0,$aYAbsPos=0,$aHAlign="left",$aVAlign="top") {
[265]57 //$this->Pos($aXAbsPos,$aYAbsPos,$aHAlign,$aVAlign);
58 $this->x = $aXAbsPos;
59 $this->y = $aYAbsPos;
60 $this->halign = $aHAlign;
61 $this->valign = $aVAlign;
[2]62 }
63
64 function SetScalePos($aX,$aY) {
[265]65 $this->iScalePosX = $aX;
66 $this->iScalePosY = $aY;
[2]67 }
[265]68
[2]69 // Specify alignment for the text
70 function Align($aHAlign,$aVAlign="top",$aParagraphAlign="") {
[265]71 $this->halign = $aHAlign;
72 $this->valign = $aVAlign;
73 if( $aParagraphAlign != "" )
74 $this->paragraph_align = $aParagraphAlign;
75 }
76
[2]77 // Alias
78 function SetAlign($aHAlign,$aVAlign="top",$aParagraphAlign="") {
[265]79 $this->Align($aHAlign,$aVAlign,$aParagraphAlign);
[2]80 }
81
82 // Specifies the alignment for a multi line text
83 function ParagraphAlign($aAlign) {
[265]84 $this->paragraph_align = $aAlign;
[2]85 }
86
87 // Specifies the alignment for a multi line text
88 function SetParagraphAlign($aAlign) {
[265]89 $this->paragraph_align = $aAlign;
[2]90 }
91
92 function SetShadow($aShadowColor='gray',$aShadowWidth=3) {
[265]93 $this->ishadowwidth=$aShadowWidth;
94 $this->shadow=$aShadowColor;
95 $this->boxed=true;
[2]96 }
97
98 function SetWordWrap($aCol) {
[265]99 $this->iWordwrap = $aCol ;
[2]100 }
[265]101
[2]102 // Specify that the text should be boxed. fcolor=frame color, bcolor=border color,
103 // $shadow=drop shadow should be added around the text.
104 function SetBox($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadowColor=false,$aCornerRadius=4,$aShadowWidth=3) {
[265]105 if( $aFrameColor === false ) {
106 $this->boxed=false;
107 }
108 else {
109 $this->boxed=true;
110 }
111 $this->fcolor=$aFrameColor;
112 $this->bcolor=$aBorderColor;
113 // For backwards compatibility when shadow was just true or false
114 if( $aShadowColor === true ) {
115 $aShadowColor = 'gray';
116 }
117 $this->shadow=$aShadowColor;
118 $this->icornerradius=$aCornerRadius;
119 $this->ishadowwidth=$aShadowWidth;
[2]120 }
[265]121
122 function SetBox2($aFrameColor=array(255,255,255),$aBorderColor=array(0,0,0),$aShadowColor=false,$aCornerRadius=4,$aShadowWidth=3) {
123 $this->iBoxType=2;
124 $this->SetBox($aFrameColor,$aBorderColor,$aShadowColor,$aCornerRadius,$aShadowWidth);
125 }
126
[2]127 // Hide the text
128 function Hide($aHide=true) {
[265]129 $this->hide=$aHide;
[2]130 }
[265]131
132 // This looks ugly since it's not a very orthogonal design
[2]133 // but I added this "inverse" of Hide() to harmonize
[265]134 // with some classes which I designed more recently (especially)
[2]135 // jpgraph_gantt
136 function Show($aShow=true) {
[265]137 $this->hide=!$aShow;
[2]138 }
[265]139
[2]140 // Specify font
141 function SetFont($aFamily,$aStyle=FS_NORMAL,$aSize=10) {
[265]142 $this->font_family=$aFamily;
143 $this->font_style=$aStyle;
144 $this->font_size=$aSize;
[2]145 }
[265]146
[2]147 // Center the text between $left and $right coordinates
148 function Center($aLeft,$aRight,$aYAbsPos=false) {
[265]149 $this->x = $aLeft + ($aRight-$aLeft )/2;
150 $this->halign = "center";
151 if( is_numeric($aYAbsPos) )
152 $this->y = $aYAbsPos;
[2]153 }
[265]154
[2]155 // Set text color
156 function SetColor($aColor) {
[265]157 $this->color = $aColor;
[2]158 }
[265]159
[2]160 function SetAngle($aAngle) {
[265]161 $this->SetOrientation($aAngle);
[2]162 }
[265]163
[2]164 // Orientation of text. Note only TTF fonts can have an arbitrary angle
165 function SetOrientation($aDirection=0) {
[265]166 if( is_numeric($aDirection) )
167 $this->dir=$aDirection;
168 elseif( $aDirection=="h" )
169 $this->dir = 0;
170 elseif( $aDirection=="v" )
171 $this->dir = 90;
172 else
173 JpGraphError::RaiseL(25051);//(" Invalid direction specified for text.");
[2]174 }
[265]175
[2]176 // Total width of text
177 function GetWidth($aImg) {
[265]178 $aImg->SetFont($this->font_family,$this->font_style,$this->raw_font_size);
179 $w = $aImg->GetTextWidth($this->t,$this->dir);
180 return $w;
[2]181 }
[265]182
[2]183 // Hight of font
184 function GetFontHeight($aImg) {
[265]185 $aImg->SetFont($this->font_family,$this->font_style,$this->raw_font_size);
186 $h = $aImg->GetFontHeight();
187 return $h;
[2]188
189 }
190
191 function GetTextHeight($aImg) {
[265]192 $aImg->SetFont($this->font_family,$this->font_style,$this->raw_font_size);
193 $h = $aImg->GetTextHeight($this->t,$this->dir);
194 return $h;
[2]195 }
196
197 function GetHeight($aImg) {
[265]198 // Synonym for GetTextHeight()
199 $aImg->SetFont($this->font_family,$this->font_style,$this->raw_font_size);
200 $h = $aImg->GetTextHeight($this->t,$this->dir);
201 return $h;
[2]202 }
203
204 // Set the margin which will be interpretated differently depending
205 // on the context.
206 function SetMargin($aMarg) {
[265]207 $this->margin = $aMarg;
[2]208 }
209
210 function StrokeWithScale($aImg,$axscale,$ayscale) {
[265]211 if( $this->iScalePosX === null || $this->iScalePosY === null ) {
212 $this->Stroke($aImg);
213 }
214 else {
215 $this->Stroke($aImg,
216 round($axscale->Translate($this->iScalePosX)),
217 round($ayscale->Translate($this->iScalePosY)));
218 }
[2]219 }
220
221 function SetCSIMTarget($aURITarget,$aAlt='',$aWinTarget='') {
[265]222 $this->iCSIMtarget = $aURITarget;
223 $this->iCSIMalt = $aAlt;
224 $this->iCSIMWinTarget = $aWinTarget;
[2]225 }
226
227 function GetCSIMareas() {
[265]228 if( $this->iCSIMtarget !== '' ) {
229 return $this->iCSIMarea;
230 }
231 else {
232 return '';
233 }
[2]234 }
235
236 // Display text in image
237 function Stroke($aImg,$x=null,$y=null) {
238
[265]239 if( $x !== null ) $this->x = round($x);
240 if( $y !== null ) $this->y = round($y);
[2]241
[265]242 // Insert newlines
243 if( $this->iWordwrap > 0 ) {
244 $this->t = wordwrap($this->t,$this->iWordwrap,"\n");
245 }
[2]246
[265]247 // If position been given as a fraction of the image size
248 // calculate the absolute position
249 if( $this->x < 1 && $this->x > 0 ) $this->x *= $aImg->width;
250 if( $this->y < 1 && $this->y > 0 ) $this->y *= $aImg->height;
[2]251
[265]252 $aImg->PushColor($this->color);
253 $aImg->SetFont($this->font_family,$this->font_style,$this->raw_font_size);
254 $aImg->SetTextAlign($this->halign,$this->valign);
[2]255
[265]256 if( $this->boxed ) {
257 if( $this->fcolor=="nofill" ) {
258 $this->fcolor=false;
259 }
[2]260
[265]261 $oldweight=$aImg->SetLineWeight(1);
[2]262
[265]263 if( $this->iBoxType == 2 && $this->font_family > FF_FONT2+2 ) {
264
265 $bbox = $aImg->StrokeBoxedText2($this->x, $this->y,
266 $this->t, $this->dir,
267 $this->fcolor,
268 $this->bcolor,
269 $this->shadow,
270 $this->paragraph_align,
271 2,4,
272 $this->icornerradius,
273 $this->ishadowwidth);
274 }
275 else {
276 $bbox = $aImg->StrokeBoxedText($this->x,$this->y,$this->t,
277 $this->dir,$this->fcolor,$this->bcolor,$this->shadow,
278 $this->paragraph_align,3,3,$this->icornerradius,
279 $this->ishadowwidth);
280 }
281
282 $aImg->SetLineWeight($oldweight);
283 }
284 else {
285 $debug=false;
286 $bbox = $aImg->StrokeText($this->x,$this->y,$this->t,$this->dir,$this->paragraph_align,$debug);
287 }
288
289 // Create CSIM targets
290 $coords = $bbox[0].','.$bbox[1].','.$bbox[2].','.$bbox[3].','.$bbox[4].','.$bbox[5].','.$bbox[6].','.$bbox[7];
291 $this->iCSIMarea = "<area shape=\"poly\" coords=\"$coords\" href=\"".htmlentities($this->iCSIMtarget)."\" ";
292 if( trim($this->iCSIMalt) != '' ) {
293 $this->iCSIMarea .= " alt=\"".$this->iCSIMalt."\" ";
294 $this->iCSIMarea .= " title=\"".$this->iCSIMalt."\" ";
295 }
296 if( trim($this->iCSIMWinTarget) != '' ) {
297 $this->iCSIMarea .= " target=\"".$this->iCSIMWinTarget."\" ";
298 }
299 $this->iCSIMarea .= " />\n";
300
301 $aImg->PopColor($this->color);
[2]302 }
[265]303
304 function __get($name) {
305
306 if (strpos($name, 'raw_') !== false) {
307 // if $name == 'raw_left_margin' , return $this->_left_margin;
308 $variable_name = '_' . str_replace('raw_', '', $name);
309 return $this->$variable_name;
310 }
311
312 $variable_name = '_' . $name;
313
314 if (isset($this->$variable_name)) {
315 return $this->$variable_name * SUPERSAMPLING_SCALE;
316 } else {
317 JpGraphError::RaiseL('25132', $name);
318 }
319 }
320
321 function __set($name, $value) {
322 $this->{'_'.$name} = $value;
323 }
[2]324} // Class
325
326
327?>
Note: See TracBrowser for help on using the repository browser.