Issues (459)

src/graph/Shape.php (1 issue)

1
<?php
2
3
/**
4
 * JPGraph v4.0.3
5
 */
6
7
namespace Amenadiel\JpGraph\Graph;
8
9
/*
10
 * File:        JPGRAPH_CANVTOOLS.PHP
11
 * // Description: Some utilities for text and shape drawing on a canvas
12
 * // Created:     2002-08-23
13
 * // Ver:         $Id: jpgraph_canvtools.php 1857 2009-09-28 14:38:14Z ljp $
14
 * //
15
 * // Copyright (c) Asial Corporation. All rights reserved.
16
 */
17
define('CORNER_TOPLEFT', 0);
18
define('CORNER_TOPRIGHT', 1);
19
define('CORNER_BOTTOMRIGHT', 2);
20
define('CORNER_BOTTOMLEFT', 3);
21
22
/**
23
 * @class Shape
24
 * // Description: Methods to draw shapes on canvas
25
 */
26
class Shape
27
{
28
    private $img;
29
    private $scale;
30
31
    public function __construct($aGraph, $scale)
32
    {
33
        $this->img = $aGraph->img;
34
        $this->img->SetColor('black');
35
        $this->scale = $scale;
36
    }
37
38
    public function SetColor($aColor)
39
    {
40
        $this->img->SetColor($aColor);
41
    }
42
43
    public function Line($x1, $y1, $x2, $y2)
44
    {
45
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
46
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
47
        $this->img->Line($x1, $y1, $x2, $y2);
48
    }
49
50
    public function SetLineWeight($aWeight)
51
    {
52
        $this->img->SetLineWeight($aWeight);
53
    }
54
55
    public function Polygon($p, $aClosed = false)
56
    {
57
        $n = safe_count($p);
58
        for ($i = 0; $i < $n; $i += 2) {
59
            $p[$i]     = $this->scale->TranslateX($p[$i]);
60
            $p[$i + 1] = $this->scale->TranslateY($p[$i + 1]);
61
        }
62
        $this->img->Polygon($p, $aClosed);
63
    }
64
65
    public function FilledPolygon($p)
66
    {
67
        $n = safe_count($p);
68
        for ($i = 0; $i < $n; $i += 2) {
69
            $p[$i]     = $this->scale->TranslateX($p[$i]);
70
            $p[$i + 1] = $this->scale->TranslateY($p[$i + 1]);
71
        }
72
        $this->img->FilledPolygon($p);
73
    }
74
75
    // Draw a bezier curve with defining points in the $aPnts array
76
    // using $aSteps steps.
77
    // 0=x0, 1=y0
78
    // 2=x1, 3=y1
79
    // 4=x2, 5=y2
80
    // 6=x3, 7=y3
81
    public function Bezier($p, $aSteps = 40)
82
    {
83
        $x0 = $p[0];
84
        $y0 = $p[1];
85
        // Calculate coefficients
86
        $cx = 3 * ($p[2] - $p[0]);
87
        $bx = 3 * ($p[4] - $p[2]) - $cx;
88
        $ax = $p[6] - $p[0] - $cx - $bx;
89
        $cy = 3 * ($p[3] - $p[1]);
90
        $by = 3 * ($p[5] - $p[3]) - $cy;
91
        $ay = $p[7] - $p[1] - $cy - $by;
92
93
        // Step size
94
        $delta = 1.0 / $aSteps;
95
96
        $x_old = $x0;
97
        $y_old = $y0;
98
        for ($t = $delta; $t <= 1.0; $t += $delta) {
99
            $tt  = $t * $t;
100
            $ttt = $tt * $t;
101
            $x   = $ax * $ttt + $bx * $tt + $cx * $t + $x0;
102
            $y   = $ay * $ttt + $by * $tt + $cy * $t + $y0;
103
            $this->Line($x_old, $y_old, $x, $y);
104
            $x_old = $x;
105
            $y_old = $y;
106
        }
107
        $this->Line($x_old, $y_old, $p[6], $p[7]);
108
    }
109
110
    public function Rectangle($x1, $y1, $x2, $y2)
111
    {
112
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
113
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
114
        $this->img->Rectangle($x1, $y1, $x2, $y2);
115
    }
116
117
    public function FilledRectangle($x1, $y1, $x2, $y2)
118
    {
119
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
120
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
121
        $this->img->FilledRectangle($x1, $y1, $x2, $y2);
122
    }
123
124
    public function Circle($x1, $y1, $r)
125
    {
126
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
127
        if ($r >= 0) {
128
            $r = $this->scale->TranslateX($r);
129
        } else {
130
            $r = -$r;
131
        }
132
133
        $this->img->Circle($x1, $y1, $r);
134
    }
135
136
    public function FilledCircle($x1, $y1, $r)
137
    {
138
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
139
        if ($r >= 0) {
140
            $r = $this->scale->TranslateX($r);
141
        } else {
142
            $r = -$r;
143
        }
144
145
        $this->img->FilledCircle($x1, $y1, $r);
146
    }
147
148
    public function RoundedRectangle($x1, $y1, $x2, $y2, $r = null)
149
    {
150
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
151
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
152
153
        if ($r == null) {
154
            $r = 5;
155
        } elseif ($r >= 0) {
156
            $r = $this->scale->TranslateX($r);
157
        } else {
158
            $r = -$r;
159
        }
160
161
        $this->img->RoundedRectangle($x1, $y1, $x2, $y2, $r);
162
    }
163
164
    public function FilledRoundedRectangle($x1, $y1, $x2, $y2, $r = null)
165
    {
166
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
167
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
168
169
        if ($r == null) {
170
            $r = 5;
171
        } elseif ($r > 0) {
172
            $r = $this->scale->TranslateX($r);
173
        } else {
174
            $r = -$r;
175
        }
176
177
        $this->img->FilledRoundedRectangle($x1, $y1, $x2, $y2, $r);
178
    }
179
180
    public function ShadowRectangle($x1, $y1, $x2, $y2, $fcolor = false, $shadow_width = null, $shadow_color = [102, 102, 102])
181
    {
182
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
183
        list($x2, $y2) = $this->scale->Translate($x2, $y2);
184
        if ($shadow_width == null) {
185
            $shadow_width = 4;
186
        } else {
187
            $shadow_width = $this->scale->TranslateX($shadow_width);
188
        }
189
190
        $this->img->ShadowRectangle($x1, $y1, $x2, $y2, $fcolor, $shadow_width, $shadow_color);
191
    }
192
193
    public function SetTextAlign($halign, $valign = 'bottom')
0 ignored issues
show
The parameter $valign is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

193
    public function SetTextAlign($halign, /** @scrutinizer ignore-unused */ $valign = 'bottom')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
194
    {
195
        $this->img->SetTextAlign($halign, $valign = 'bottom');
196
    }
197
198
    public function StrokeText($x1, $y1, $txt, $dir = 0, $paragraph_align = 'left')
199
    {
200
        list($x1, $y1) = $this->scale->Translate($x1, $y1);
201
        $this->img->StrokeText($x1, $y1, $txt, $dir, $paragraph_align);
202
    }
203
204
    // A rounded rectangle where one of the corner has been moved "into" the
205
    // rectangle 'iw' width and 'ih' height. Corners:
206
    // 0=Top left, 1=top right, 2=bottom right, 3=bottom left
207
    public function IndentedRectangle($xt, $yt, $w, $h, $iw = 0, $ih = 0, $aCorner = 3, $aFillColor = '', $r = 4)
208
    {
209
        list($xt, $yt) = $this->scale->Translate($xt, $yt);
210
        list($w, $h)   = $this->scale->Translate($w, $h);
211
        list($iw, $ih) = $this->scale->Translate($iw, $ih);
212
213
        $xr = $xt + $w - 0;
214
        $yl = $yt + $h - 0;
215
216
        switch ($aCorner) {
217
            case 0:
218
                // Bottom line, left &  right arc
219
                $this->img->Line($xt + $r, $yl, $xr - $r, $yl);
220
                $this->img->Arc($xt + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
221
                $this->img->Arc($xr - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
222
223
                // Right line, Top right arc
224
                $this->img->Line($xr, $yt + $r, $xr, $yl - $r);
225
                $this->img->Arc($xr - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
226
227
                // Top line, Top left arc
228
                $this->img->Line($xt + $iw + $r, $yt, $xr - $r, $yt);
229
                $this->img->Arc($xt + $iw + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
230
231
                // Left line
232
                $this->img->Line($xt, $yt + $ih + $r, $xt, $yl - $r);
233
234
                // Indent horizontal, Lower left arc
235
                $this->img->Line($xt + $r, $yt + $ih, $xt + $iw - $r, $yt + $ih);
236
                $this->img->Arc($xt + $r, $yt + $ih + $r, $r * 2, $r * 2, 180, 270);
237
238
                // Indent vertical, Indent arc
239
                $this->img->Line($xt + $iw, $yt + $r, $xt + $iw, $yt + $ih - $r);
240
                $this->img->Arc($xt + $iw - $r, $yt + $ih - $r, $r * 2, $r * 2, 0, 90);
241
242
                if ($aFillColor != '') {
243
                    $bc = $this->img->current_color_name;
244
                    $this->img->PushColor($aFillColor);
245
                    $this->img->FillToBorder($xr - $r, $yl - $r, $bc);
246
                    $this->img->PopColor();
247
                }
248
249
                break;
250
            case 1:
251
                // Bottom line, left &  right arc
252
                $this->img->Line($xt + $r, $yl, $xr - $r, $yl);
253
                $this->img->Arc($xt + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
254
                $this->img->Arc($xr - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
255
256
                // Left line, Top left arc
257
                $this->img->Line($xt, $yt + $r, $xt, $yl - $r);
258
                $this->img->Arc($xt + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
259
260
                // Top line, Top right arc
261
                $this->img->Line($xt + $r, $yt, $xr - $iw - $r, $yt);
262
                $this->img->Arc($xr - $iw - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
263
264
                // Right line
265
                $this->img->Line($xr, $yt + $ih + $r, $xr, $yl - $r);
266
267
                // Indent horizontal, Lower right arc
268
                $this->img->Line($xr - $iw + $r, $yt + $ih, $xr - $r, $yt + $ih);
269
                $this->img->Arc($xr - $r, $yt + $ih + $r, $r * 2, $r * 2, 270, 360);
270
271
                // Indent vertical, Indent arc
272
                $this->img->Line($xr - $iw, $yt + $r, $xr - $iw, $yt + $ih - $r);
273
                $this->img->Arc($xr - $iw + $r, $yt + $ih - $r, $r * 2, $r * 2, 90, 180);
274
275
                if ($aFillColor != '') {
276
                    $bc = $this->img->current_color_name;
277
                    $this->img->PushColor($aFillColor);
278
                    $this->img->FillToBorder($xt + $r, $yl - $r, $bc);
279
                    $this->img->PopColor();
280
                }
281
282
                break;
283
            case 2: // Lower right
284
                // Top line, Top left & Top right arc
285
                $this->img->Line($xt + $r, $yt, $xr - $r, $yt);
286
                $this->img->Arc($xt + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
287
                $this->img->Arc($xr - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
288
289
                // Left line, Bottom left arc
290
                $this->img->Line($xt, $yt + $r, $xt, $yl - $r);
291
                $this->img->Arc($xt + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
292
293
                // Bottom line, Bottom right arc
294
                $this->img->Line($xt + $r, $yl, $xr - $iw - $r, $yl);
295
                $this->img->Arc($xr - $iw - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
296
297
                // Right line
298
                $this->img->Line($xr, $yt + $r, $xr, $yl - $ih - $r);
299
300
                // Indent horizontal, Lower right arc
301
                $this->img->Line($xr - $r, $yl - $ih, $xr - $iw + $r, $yl - $ih);
302
                $this->img->Arc($xr - $r, $yl - $ih - $r, $r * 2, $r * 2, 0, 90);
303
304
                // Indent vertical, Indent arc
305
                $this->img->Line($xr - $iw, $yl - $r, $xr - $iw, $yl - $ih + $r);
306
                $this->img->Arc($xr - $iw + $r, $yl - $ih + $r, $r * 2, $r * 2, 180, 270);
307
308
                if ($aFillColor != '') {
309
                    $bc = $this->img->current_color_name;
310
                    $this->img->PushColor($aFillColor);
311
                    $this->img->FillToBorder($xt + $r, $yt + $r, $bc);
312
                    $this->img->PopColor();
313
                }
314
315
                break;
316
            case 3: // Lower left
317
                // Top line, Top left & Top right arc
318
                $this->img->Line($xt + $r, $yt, $xr - $r, $yt);
319
                $this->img->Arc($xt + $r, $yt + $r, $r * 2, $r * 2, 180, 270);
320
                $this->img->Arc($xr - $r, $yt + $r, $r * 2, $r * 2, 270, 360);
321
322
                // Right line, Bottom right arc
323
                $this->img->Line($xr, $yt + $r, $xr, $yl - $r);
324
                $this->img->Arc($xr - $r, $yl - $r, $r * 2, $r * 2, 0, 90);
325
326
                // Bottom line, Bottom left arc
327
                $this->img->Line($xt + $iw + $r, $yl, $xr - $r, $yl);
328
                $this->img->Arc($xt + $iw + $r, $yl - $r, $r * 2, $r * 2, 90, 180);
329
330
                // Left line
331
                $this->img->Line($xt, $yt + $r, $xt, $yl - $ih - $r);
332
333
                // Indent horizontal, Lower left arc
334
                $this->img->Line($xt + $r, $yl - $ih, $xt + $iw - $r, $yl - $ih);
335
                $this->img->Arc($xt + $r, $yl - $ih - $r, $r * 2, $r * 2, 90, 180);
336
337
                // Indent vertical, Indent arc
338
                $this->img->Line($xt + $iw, $yl - $ih + $r, $xt + $iw, $yl - $r);
339
                $this->img->Arc($xt + $iw - $r, $yl - $ih + $r, $r * 2, $r * 2, 270, 360);
340
341
                if ($aFillColor != '') {
342
                    $bc = $this->img->current_color_name;
343
                    $this->img->PushColor($aFillColor);
344
                    $this->img->FillToBorder($xr - $r, $yt + $r, $bc);
345
                    $this->img->PopColor();
346
                }
347
348
                break;
349
        }
350
    }
351
}
352