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