Issues (459)

src/text/TextProperty.php (1 issue)

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