/var/www/hkosl.com/imusiccircle/webadmin/libraies/phenx/php-svg-lib/src/Svg/Surface/SurfaceGmagick.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
/**
 * @package php-svg-lib
 * @link    http://github.com/PhenX/php-svg-lib
 * @author  Fabien Ménager <fabien.menager@gmail.com>
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 */

namespace Svg\Surface;

use 
Svg\Style;

class 
SurfaceGmagick implements SurfaceInterface
{
    const 
DEBUG false;

    
/** @var \GmagickDraw */
    
private $canvas;

    private 
$width;
    private 
$height;

    
/** @var Style */
    
private $style;

    public function 
__construct($w$h)
    {
        if (
self::DEBUG) {
            echo 
__FUNCTION__ "\n";
        }
        
$this->width $w;
        
$this->height $h;

        
$canvas = new \GmagickDraw();

        
$this->canvas $canvas;
    }

    function 
out()
    {
        if (
self::DEBUG) {
            echo 
__FUNCTION__ "\n";
        }

        
$image = new \Gmagick();
        
$image->newimage($this->width$this->height);
        
$image->drawimage($this->canvas);

        
$tmp tempnam("""gm");

        
$image->write($tmp);

        return 
file_get_contents($tmp);
    }

    public function 
save()
    {
        if (
self::DEBUG) {
            echo 
__FUNCTION__ "\n";
        }
        
$this->canvas->save();
    }

    public function 
restore()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->restore();
    }

    public function 
scale($x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->scale($x$y);
    }

    public function 
rotate($angle)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->rotate($angle);
    }

    public function 
translate($x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->translate($x$y);
    }

    public function 
transform($a$b$c$d$e$f)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->concat($a$b$c$d$e$f);
    }

    public function 
beginPath()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
// TODO: Implement beginPath() method.
    
}

    public function 
closePath()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->closepath();
    }

    public function 
fillStroke()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->fill_stroke();
    }

    public function 
clip()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->clip();
    }

    public function 
fillText($text$x$y$maxWidth null)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->set_text_pos($x$y);
        
$this->canvas->show($text);
    }

    public function 
strokeText($text$x$y$maxWidth null)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
// TODO: Implement drawImage() method.
    
}

    public function 
drawImage($image$sx$sy$sw null$sh null$dx null$dy null$dw null$dh null)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";

        if (
strpos($image"data:") === 0) {
            
$data substr($imagestrpos($image";") + 1);
            if (
strpos($data"base64") === 0) {
                
$data base64_decode(substr($data7));
            }

            
$image tempnam("""svg");
            
file_put_contents($image$data);
        }

        
$img $this->canvas->load_image("auto"$image"");

        
$sy $sy $sh;
        
$this->canvas->fit_image($img$sx$sy'boxsize={' "$sw $sh'} fitmethod=entire');
    }

    public function 
lineTo($x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->lineto($x$y);
    }

    public function 
moveTo($x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->moveto($x$y);
    }

    public function 
quadraticCurveTo($cpx$cpy$x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
// TODO: Implement quadraticCurveTo() method.
    
}

    public function 
bezierCurveTo($cp1x$cp1y$cp2x$cp2y$x$y)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->curveto($cp1x$cp1y$cp2x$cp2y$x$y);
    }

    public function 
arcTo($x1$y1$x2$y2$radius)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
    }

    public function 
arc($x$y$radius$startAngle$endAngle$anticlockwise false)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->arc($x$y$radius$startAngle$endAngle);
    }

    public function 
circle($x$y$radius)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->circle($x$y$radius);
    }

    public function 
ellipse($x$y$radiusX$radiusY$rotation$startAngle$endAngle$anticlockwise)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->ellipse($x$y$radiusX$radiusY);
    }

    public function 
fillRect($x$y$w$h)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->rect($x$y$w$h);
        
$this->fill();
    }

    public function 
rect($x$y$w$h)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->rect($x$y$w$h);
    }

    public function 
fill()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->fill();
    }

    public function 
strokeRect($x$y$w$h)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->rect($x$y$w$h);
        
$this->stroke();
    }

    public function 
stroke()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$this->canvas->stroke();
    }

    public function 
endPath()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
//$this->canvas->endPath();
    
}

    public function 
measureText($text)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";
        
$style $this->getStyle();
        
$font $this->getFont($style->fontFamily$style->fontStyle);

        return 
$this->canvas->stringwidth($text$font$this->getStyle()->fontSize);
    }

    public function 
getStyle()
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";

        return 
$this->style;
    }

    public function 
setStyle(Style $style)
    {
        if (
self::DEBUG) echo __FUNCTION__ "\n";

        
$this->style $style;
        
$canvas $this->canvas;

        if (
$stroke $style->stroke) {
            
$canvas->setcolor("stroke""rgb"$stroke[0] / 255$stroke[1] / 255$stroke[2] / 255null);
        }

        if (
$fill $style->fill) {
           
// $canvas->setcolor("fill", "rgb", $fill[0] / 255, $fill[1] / 255, $fill[2] / 255, null);
        
}

        
$opts = array();
        if (
$style->strokeWidth 0.000001) {
            
$opts[] = "linewidth=$style->strokeWidth";
        }

        if (
in_array($style->strokeLinecap, array("butt""round""projecting"))) {
            
$opts[] = "linecap=$style->strokeLinecap";
        }

        if (
in_array($style->strokeLinejoin, array("miter""round""bevel"))) {
            
$opts[] = "linejoin=$style->strokeLinejoin";
        }

        
$canvas->set_graphics_option(implode(" "$opts));

        
$font $this->getFont($style->fontFamily$style->fontStyle);
        
$canvas->setfont($font$style->fontSize);
    }

    private function 
getFont($family$style)
    {
        
$map = array(
            
"serif"      => "Times",
            
"sans-serif" => "Helvetica",
            
"fantasy"    => "Symbol",
            
"cursive"    => "serif",
            
"monospance" => "Courier",
        );

        
$family strtolower($family);
        if (isset(
$map[$family])) {
            
$family $map[$family];
        }

        return 
$this->canvas->load_font($family"unicode""fontstyle=$style");
    }
}