/var/www/hkosl.com/imusiccircle/webadmin/libraies/phenx/php-svg-lib/src/Svg/Document.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?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;

use 
Svg\Surface\SurfaceInterface;
use 
Svg\Tag\AbstractTag;
use 
Svg\Tag\Anchor;
use 
Svg\Tag\Circle;
use 
Svg\Tag\Ellipse;
use 
Svg\Tag\Group;
use 
Svg\Tag\Image;
use 
Svg\Tag\Line;
use 
Svg\Tag\LinearGradient;
use 
Svg\Tag\Path;
use 
Svg\Tag\Polygon;
use 
Svg\Tag\Polyline;
use 
Svg\Tag\Rect;
use 
Svg\Tag\Stop;
use 
Svg\Tag\Text;

class 
Document extends AbstractTag
{
    protected 
$filename;
    protected 
$inDefs false;

    protected 
$x;
    protected 
$y;
    protected 
$width;
    protected 
$height;

    protected 
$subPathInit;
    protected 
$pathBBox;
    protected 
$viewBox;

    
/** @var resource */
    
protected $parser;

    
/** @var SurfaceInterface */
    
protected $surface;

    
/** @var AbstractTag[] */
    
protected $stack = array();

    
/** @var AbstractTag[] */
    
protected $defs = array();

    public function 
loadFile($filename)
    {
        
$this->filename $filename;
    }

    protected function 
initParser() {
        
$parser xml_parser_create("utf-8");
        
xml_parser_set_option($parserXML_OPTION_CASE_FOLDINGfalse);
        
xml_set_element_handler(
            
$parser,
            array(
$this"_tagStart"),
            array(
$this"_tagEnd")
        );
        
xml_set_character_data_handler(
            
$parser,
            array(
$this"_charData")
        );

        return 
$this->parser $parser;
    }

    public function 
__construct() {

    }

    
/**
     * @return SurfaceInterface
     */
    
public function getSurface()
    {
        return 
$this->surface;
    }

    public function 
getStack()
    {
        return 
$this->stack;
    }

    public function 
getWidth()
    {
        return 
$this->width;
    }

    public function 
getHeight()
    {
        return 
$this->height;
    }

    public function 
getDimensions() {
        
$rootAttributes null;

        
$parser xml_parser_create("utf-8");
        
xml_parser_set_option($parserXML_OPTION_CASE_FOLDINGfalse);
        
xml_set_element_handler(
            
$parser,
            function (
$parser$name$attributes) use (&$rootAttributes) {
                if (
$name === "svg" && $rootAttributes === null) {
                    
$attributes array_change_key_case($attributesCASE_LOWER);

                    
$rootAttributes $attributes;
                }
            },
            function (
$parser$name) {}
        );

        
$fp fopen($this->filename"r");
        while (
$line fread($fp8192)) {
            
xml_parse($parser$linefalse);

            if (
$rootAttributes !== null) {
                break;
            }
        }

        
xml_parser_free($parser);

        return 
$this->handleSizeAttributes($rootAttributes);
    }

    public function 
handleSizeAttributes($attributes){
        if (
$this->width === null) {
            if (isset(
$attributes["width"])) {
                
$width  = (int)$attributes["width"];
                
$this->width  $width;
            }

            if (isset(
$attributes["height"])) {
                
$height = (int)$attributes["height"];
                
$this->height $height;
            }

            if (isset(
$attributes['viewbox'])) {
                
$viewBox preg_split('/[\s,]+/is'trim($attributes['viewbox']));
                if (
count($viewBox) == 4) {
                    
$this->$viewBox[0];
                    
$this->$viewBox[1];

                    if (!
$this->width) {
                        
$this->width $viewBox[2];
                    }
                    if (!
$this->height) {
                        
$this->height $viewBox[3];
                    }
                }
            }
        }

        return array(
            
0        => $this->width,
            
1        => $this->height,

            
"width"  => $this->width,
            
"height" => $this->height,
        );
    }

    protected function 
getDocument(){
        return 
$this;
    }

    protected function 
before($attribs)
    {
        
$surface $this->getSurface();

        
$style = new DefaultStyle();
        
$style->inherit($this);
        
$style->fromAttributes($attribs);

        
$this->setStyle($style);

        
$surface->setStyle($style);
    }

    public function 
render(SurfaceInterface $surface)
    {
        
$this->inDefs false;
        
$this->surface $surface;

        
$parser $this->initParser();

        if (
$this->|| $this->y) {
            
$surface->translate(-$this->x, -$this->y);
        }

        
$fp fopen($this->filename"r");
        while (
$line fread($fp8192)) {
            
xml_parse($parser$linefalse);
        }

        
xml_parse($parser""true);

        
xml_parser_free($parser);
    }

    protected function 
svgOffset($attributes)
    {
        
$this->attributes $attributes;

        
$this->handleSizeAttributes($attributes);
    }

    private function 
_tagStart($parser$name$attributes)
    {
        
$this->0;
        
$this->0;

        
$tag null;

        
$attributes array_change_key_case($attributesCASE_LOWER);

        switch (
strtolower($name)) {
            case 
'defs':
                
$this->inDefs true;
                return;

            case 
'svg':
                if (
count($this->attributes)) {
                    
$tag = new Group($this);
                }
                else {
                    
$tag $this;
                    
$this->svgOffset($attributes);
                }
                break;

            case 
'path':
                
$tag = new Path($this);
                break;

            case 
'rect':
                
$tag = new Rect($this);
                break;

            case 
'circle':
                
$tag = new Circle($this);
                break;

            case 
'ellipse':
                
$tag = new Ellipse($this);
                break;

            case 
'image':
                
$tag = new Image($this);
                break;

            case 
'line':
                
$tag = new Line($this);
                break;

            case 
'polyline':
                
$tag = new Polyline($this);
                break;

            case 
'polygon':
                
$tag = new Polygon($this);
                break;

            case 
'lineargradient':
                
$tag = new LinearGradient($this);
                break;

            case 
'radialgradient':
                
$tag = new LinearGradient($this);
                break;

            case 
'stop':
                
$tag = new Stop($this);
                break;

            case 
'a':
                
$tag = new Anchor($this);
                break;

            case 
'g':
                
$tag = new Group($this);
                break;

            case 
'text':
                
$tag = new Text($this);
                break;
        }

        if (
$tag) {
            if (!
$this->inDefs) {
                
$this->stack[] = $tag;
                
$tag->handle($attributes);
            }
            else {
                if (isset(
$attributes["id"])) {
                    
$this->defs[$attributes["id"]] = $tag;
                }
            }
        } else {
            echo 
"Unknown: '$name'\n";
        }
    }

    function 
_charData($parser$data)
    {
        
$stack_top end($this->stack);

        if (
$stack_top instanceof Text) {
            
$stack_top->appendText($data);
        }
    }

    function 
_tagEnd($parser$name)
    {
        
/** @var AbstractTag $tag */
        
$tag null;
        switch (
strtolower($name)) {
            case 
'defs':
                
$this->inDefs false;
                return;

            case 
'svg':
            case 
'path':
            case 
'rect':
            case 
'circle':
            case 
'ellipse':
            case 
'image':
            case 
'line':
            case 
'polyline':
            case 
'polygon':
            case 
'radialgradient':
            case 
'lineargradient':
            case 
'stop':
            case 
'text':
            case 
'g':
            case 
'a':
                if (!
$this->inDefs) {
                    
$tag array_pop($this->stack);
                }
                break;
        }

        if (
$tag) {
            
$tag->handleEnd();
        }
    }