1
|
|
|
<?php |
2
|
|
|
namespace Amenadiel\JpGraph\Text; |
3
|
|
|
|
4
|
|
|
//=================================================== |
5
|
|
|
// CLASS TextProperty |
6
|
|
|
// Description: Holds properties for a text |
7
|
|
|
//=================================================== |
8
|
|
|
class TextProperty |
9
|
|
|
{ |
10
|
|
|
public $iShow = true; |
11
|
|
|
public $csimtarget = ''; |
12
|
|
|
public $csimwintarget = ''; |
13
|
|
|
public $csimalt = ''; |
14
|
|
|
private $iFFamily = FF_FONT1; |
15
|
|
|
private $iFStyle = FS_NORMAL; |
16
|
|
|
private $iFSize = 10; |
17
|
|
|
private $iFontArray = array(); |
18
|
|
|
private $iColor = "black"; |
19
|
|
|
private $iText = ""; |
20
|
|
|
private $iHAlign = "left"; |
21
|
|
|
private $iVAlign = "bottom"; |
22
|
|
|
|
23
|
|
|
//--------------- |
24
|
|
|
// CONSTRUCTOR |
25
|
|
|
public function __construct($aTxt = '') |
26
|
|
|
{ |
27
|
|
|
$this->iText = $aTxt; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
//--------------- |
31
|
|
|
// PUBLIC METHODS |
32
|
|
|
public function Set($aTxt) |
33
|
|
|
{ |
34
|
|
|
$this->iText = $aTxt; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function SetCSIMTarget($aTarget, $aAltText = '', $aWinTarget = '') |
38
|
|
|
{ |
39
|
|
|
if (is_string($aTarget)) { |
40
|
|
|
$aTarget = array($aTarget); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$this->csimtarget = $aTarget; |
44
|
|
|
|
45
|
|
|
if (is_string($aWinTarget)) { |
46
|
|
|
$aWinTarget = array($aWinTarget); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$this->csimwintarget = $aWinTarget; |
|
|
|
|
50
|
|
|
|
51
|
|
|
if (is_string($aAltText)) { |
52
|
|
|
$aAltText = array($aAltText); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->csimalt = $aAltText; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function SetCSIMAlt($aAltText) |
59
|
|
|
{ |
60
|
|
|
if (is_string($aAltText)) { |
61
|
|
|
$aAltText = array($aAltText); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->csimalt = $aAltText; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Set text color |
68
|
|
|
public function SetColor($aColor) |
69
|
|
|
{ |
70
|
|
|
$this->iColor = $aColor; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function HasTabs() |
74
|
|
|
{ |
75
|
|
|
if (is_string($this->iText)) { |
76
|
|
|
return substr_count($this->iText, "\t") > 0; |
77
|
|
|
} elseif (is_array($this->iText)) { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Get number of tabs in string |
83
|
|
|
public function GetNbrTabs() |
84
|
|
|
{ |
85
|
|
|
if (is_string($this->iText)) { |
86
|
|
|
return substr_count($this->iText, "\t"); |
87
|
|
|
} else { |
88
|
|
|
return 0; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Set alignment |
93
|
|
|
public function Align($aHAlign, $aVAlign = "bottom") |
94
|
|
|
{ |
95
|
|
|
$this->iHAlign = $aHAlign; |
96
|
|
|
$this->iVAlign = $aVAlign; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Synonym |
100
|
|
|
public function SetAlign($aHAlign, $aVAlign = "bottom") |
101
|
|
|
{ |
102
|
|
|
$this->iHAlign = $aHAlign; |
103
|
|
|
$this->iVAlign = $aVAlign; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Specify font |
107
|
|
|
public function SetFont($aFFamily, $aFStyle = FS_NORMAL, $aFSize = 10) |
108
|
|
|
{ |
109
|
|
|
$this->iFFamily = $aFFamily; |
110
|
|
|
$this->iFStyle = $aFStyle; |
111
|
|
|
$this->iFSize = $aFSize; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function SetColumnFonts($aFontArray) |
115
|
|
|
{ |
116
|
|
|
if (!is_array($aFontArray) || count($aFontArray[0]) != 3) { |
117
|
|
|
JpGraphError::RaiseL(6033); |
118
|
|
|
// 'Array of fonts must contain arrays with 3 elements, i.e. (Family, Style, Size)' |
119
|
|
|
} |
120
|
|
|
$this->iFontArray = $aFontArray; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function IsColumns() |
124
|
|
|
{ |
125
|
|
|
return is_array($this->iText); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Get width of text. If text contains several columns separated by |
129
|
|
|
// tabs then return both the total width as well as an array with a |
130
|
|
|
// width for each column. |
131
|
|
|
public function GetWidth($aImg, $aUseTabs = false, $aTabExtraMargin = 1.1) |
132
|
|
|
{ |
133
|
|
|
$extra_margin = 4; |
134
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
135
|
|
|
if (is_string($this->iText)) { |
136
|
|
|
if (strlen($this->iText) == 0) { |
137
|
|
|
return 0; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$tmp = preg_split('/\t/', $this->iText); |
141
|
|
|
if (count($tmp) <= 1 || !$aUseTabs) { |
142
|
|
|
$w = $aImg->GetTextWidth($this->iText); |
143
|
|
|
return $w + 2 * $extra_margin; |
144
|
|
|
} else { |
145
|
|
|
$tot = 0; |
146
|
|
|
$n = count($tmp); |
147
|
|
|
for ($i = 0; $i < $n; ++$i) { |
148
|
|
|
$res[$i] = $aImg->GetTextWidth($tmp[$i]); |
|
|
|
|
149
|
|
|
$tot += $res[$i] * $aTabExtraMargin; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
return array(round($tot), $res); |
152
|
|
|
} |
153
|
|
|
} elseif (is_object($this->iText)) { |
154
|
|
|
// A single icon |
155
|
|
|
return $this->iText->GetWidth() + 2 * $extra_margin; |
156
|
|
|
} elseif (is_array($this->iText)) { |
157
|
|
|
// Must be an array of texts. In this case we return the sum of the |
158
|
|
|
// length + a fixed margin of 4 pixels on each text string |
159
|
|
|
$n = count($this->iText); |
160
|
|
|
$nf = count($this->iFontArray); |
161
|
|
|
for ($i = 0, $w = 0; $i < $n; ++$i) { |
162
|
|
View Code Duplication |
if ($i < $nf) { |
|
|
|
|
163
|
|
|
$aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]); |
164
|
|
|
} else { |
165
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
166
|
|
|
} |
167
|
|
|
$tmp = $this->iText[$i]; |
168
|
|
|
if (is_string($tmp)) { |
169
|
|
|
$w += $aImg->GetTextWidth($tmp) + $extra_margin; |
170
|
|
|
} else { |
171
|
|
|
if (is_object($tmp) === false) { |
172
|
|
|
JpGraphError::RaiseL(6012); |
173
|
|
|
} |
174
|
|
|
$w += $tmp->GetWidth() + $extra_margin; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
return $w; |
178
|
|
|
} else { |
179
|
|
|
JpGraphError::RaiseL(6012); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
// for the case where we have multiple columns this function returns the width of each |
184
|
|
|
// column individually. If there is no columns just return the width of the single |
185
|
|
|
// column as an array of one |
186
|
|
|
public function GetColWidth($aImg, $aMargin = 0) |
187
|
|
|
{ |
188
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
189
|
|
|
if (is_array($this->iText)) { |
190
|
|
|
$n = count($this->iText); |
191
|
|
|
$nf = count($this->iFontArray); |
192
|
|
|
for ($i = 0, $w = array(); $i < $n; ++$i) { |
193
|
|
|
$tmp = $this->iText[$i]; |
194
|
|
|
if (is_string($tmp)) { |
195
|
|
View Code Duplication |
if ($i < $nf) { |
|
|
|
|
196
|
|
|
$aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]); |
197
|
|
|
} else { |
198
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
199
|
|
|
} |
200
|
|
|
$w[$i] = $aImg->GetTextWidth($tmp) + $aMargin; |
201
|
|
|
} else { |
202
|
|
|
if (is_object($tmp) === false) { |
203
|
|
|
JpGraphError::RaiseL(6012); |
204
|
|
|
} |
205
|
|
|
$w[$i] = $tmp->GetWidth() + $aMargin; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
return $w; |
209
|
|
|
} else { |
210
|
|
|
return array($this->GetWidth($aImg)); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
// Get total height of text |
215
|
|
|
public function GetHeight($aImg) |
216
|
|
|
{ |
217
|
|
|
$nf = count($this->iFontArray); |
218
|
|
|
$maxheight = -1; |
219
|
|
|
|
220
|
|
|
if ($nf > 0) { |
221
|
|
|
// We have to find out the largest font and take that one as the |
222
|
|
|
// height of the row |
223
|
|
|
for ($i = 0; $i < $nf; ++$i) { |
224
|
|
|
$aImg->SetFont($this->iFontArray[$i][0], $this->iFontArray[$i][1], $this->iFontArray[$i][2]); |
225
|
|
|
$height = $aImg->GetFontHeight(); |
226
|
|
|
$maxheight = max($height, $maxheight); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
231
|
|
|
$height = $aImg->GetFontHeight(); |
232
|
|
|
$maxheight = max($height, $maxheight); |
233
|
|
|
return $maxheight; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// Unhide/hide the text |
237
|
|
|
public function Show($aShow = true) |
238
|
|
|
{ |
239
|
|
|
$this->iShow = $aShow; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// Stroke text at (x,y) coordinates. If the text contains tabs then the |
243
|
|
|
// x parameter should be an array of positions to be used for each successive |
244
|
|
|
// tab mark. If no array is supplied then the tabs will be ignored. |
245
|
|
|
public function Stroke($aImg, $aX, $aY) |
246
|
|
|
{ |
247
|
|
|
if ($this->iShow) { |
248
|
|
|
$aImg->SetColor($this->iColor); |
249
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
250
|
|
|
$aImg->SetTextAlign($this->iHAlign, $this->iVAlign); |
251
|
|
|
if ($this->GetNbrTabs() < 1) { |
252
|
|
|
if (is_string($this->iText)) { |
253
|
|
|
if (is_array($aX)) { |
254
|
|
|
$aX = $aX[0]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
if (is_array($aY)) { |
258
|
|
|
$aY = $aY[0]; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$aImg->StrokeText($aX, $aY, $this->iText); |
262
|
|
|
} elseif (is_array($this->iText) && ($n = count($this->iText)) > 0) { |
263
|
|
|
$ax = is_array($aX); |
264
|
|
|
$ay = is_array($aY); |
265
|
|
|
if ($ax && $ay) { |
|
|
|
|
266
|
|
|
// Nothing; both are already arrays |
267
|
|
|
} elseif ($ax) { |
268
|
|
|
$aY = array_fill(0, $n, $aY); |
269
|
|
|
} elseif ($ay) { |
270
|
|
|
$aX = array_fill(0, $n, $aX); |
271
|
|
|
} else { |
272
|
|
|
$aX = array_fill(0, $n, $aX); |
273
|
|
|
$aY = array_fill(0, $n, $aY); |
274
|
|
|
} |
275
|
|
|
$n = min($n, count($aX)); |
276
|
|
|
$n = min($n, count($aY)); |
277
|
|
|
for ($i = 0; $i < $n; ++$i) { |
278
|
|
|
$tmp = $this->iText[$i]; |
279
|
|
|
if (is_object($tmp)) { |
280
|
|
|
$tmp->Stroke($aImg, $aX[$i], $aY[$i]); |
281
|
|
|
} else { |
282
|
|
View Code Duplication |
if ($i < count($this->iFontArray)) { |
|
|
|
|
283
|
|
|
$font = $this->iFontArray[$i]; |
284
|
|
|
$aImg->SetFont($font[0], $font[1], $font[2]); |
285
|
|
|
} else { |
286
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
287
|
|
|
} |
288
|
|
|
$aImg->StrokeText($aX[$i], $aY[$i], str_replace("\t", " ", $tmp)); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
} |
292
|
|
|
} else { |
293
|
|
|
$tmp = preg_split('/\t/', $this->iText); |
294
|
|
|
$n = min(count($tmp), count($aX)); |
295
|
|
|
for ($i = 0; $i < $n; ++$i) { |
296
|
|
View Code Duplication |
if ($i < count($this->iFontArray)) { |
|
|
|
|
297
|
|
|
$font = $this->iFontArray[$i]; |
298
|
|
|
$aImg->SetFont($font[0], $font[1], $font[2]); |
299
|
|
|
} else { |
300
|
|
|
$aImg->SetFont($this->iFFamily, $this->iFStyle, $this->iFSize); |
301
|
|
|
} |
302
|
|
|
$aImg->StrokeText($aX[$i], $aY, $tmp[$i]); |
303
|
|
|
} |
304
|
|
|
} |
305
|
|
|
} |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..