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