/lenasys/0.1

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/0.1

« back to all changes in this revision

Viewing changes to trunk/svg Converter/SVG_TO_CANVAS_III.php

  • Committer: gustav.hartvigsson at gmail
  • Date: 2013-04-02 12:13:01 UTC
  • mfrom: (4.2.7 hitlerhorabajs)
  • Revision ID: gustav.hartvigsson@gmail.com-20130402121301-ytfzuo7y6cku9s3o
Merge from implemenation group 2:s branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
//              Support for transformed gradients (??Is this in current version??)
55
55
//              Real Arc Drawing using Complex Math from values, draw arc using math in formula
56
56
 
57
 
$elementcounter=0;
58
 
$graphnodes=array();
59
 
$colorstops=array();
60
 
$clipdefs=array();
61
 
$clippaths=array();
62
 
 
63
 
$fillstyle="none";
64
 
$linestyle="none";
65
 
$opacity="1.0";
66
 
$gradientname="foo";
67
 
 
68
 
$isinkscape=false;
69
 
 
70
 
$stopcounter=0;
71
 
 
72
 
function recurseelement($element){
73
 
                global $elementcounter;
74
 
                global $graphnodes;
75
 
 
76
 
                if($element->getName()=="clipPath"){
77
 
                                        $attrs=$element->attributes();
78
 
                                        $sxe = new SimpleXMLElement("<use id='".$attrs['id']."' />");
79
 
                                        $graphnodes[$elementcounter]=$sxe;
 
57
        $elementcounter = 0;
 
58
        $graphnodes = array();
 
59
        $colorstops = array();
 
60
        $clipdefs = array();
 
61
        $clippaths = array();
 
62
        $fillstyle = "none";
 
63
        $linestyle = "none";
 
64
        $opacity = "1.0";
 
65
        $gradientname = "foo";
 
66
        $isinkscape = false;
 
67
        $stopcounter = 0;
 
68
 
 
69
function recurseelement( $element ) {
 
70
        global $elementcounter;
 
71
        global $graphnodes;
 
72
                if( $element -> getName() == "clipPath" ) {
 
73
                                        $attrs = $element -> attributes();
 
74
                                        $sxe = new SimpleXMLElement( "<use id='".$attrs['id']."' />" );
 
75
                                        $graphnodes [$elementcounter] = $sxe;
80
76
                                        $elementcounter++;
81
77
                }
82
 
                
83
 
                foreach ($element->children() as $child) {
84
 
                                if($child->getName()=="clipPath"||$child->getName()=="defs"||$child->getName()=="g"||$child->getName()=="linearGradient"||$child->getName()=="radialGradient"){
85
 
 
86
 
                                                // Most others except for clipPath are added
87
 
                                                if($child->getName()=="defs"||$child->getName()=="g"||$child->getName()=="linearGradient"||$child->getName()=="radialGradient"){
88
 
                                                                $graphnodes[$elementcounter]=$child;
89
 
                                                                $elementcounter++;
90
 
                                                }
91
 
                                                recurseelement($child);
92
 
 
93
 
                                }else if($child->getName()=="use"||$child->getName()=="ellipse"||$child->getName()=="circle"||$child->getName()=="stop"||$child->getName()=="polygon"||$child->getName()=="line"||$child->getName()=="polyline"||$child->getName()=="path"||$child->getName()=="rect"||$child->getName()=="text"){
94
 
                                                // Add element to queue
95
 
                                                $graphnodes[$elementcounter]=$child;
96
 
                                                $elementcounter++;
97
 
                                }else{
98
 
                                                echo "//Unknown element: ".$child->getName()."<br>";
 
78
                foreach( $element -> children() as $child ) {
 
79
                        if( $child -> getName() == "clipPath" || $child -> getName() == "defs" || $child -> getName() == "g" || $child -> getName() == "linearGradient" || $child -> getName() == "radialGradient" ) {
 
80
                                // Most others except for clipPath are added
 
81
                                if( $child -> getName() == "defs" || $child -> getName() == "g" || $child -> getName() == "linearGradient" || $child -> getName() == "radialGradient" ) {
 
82
                                        $graphnodes [$elementcounter] = $child;
 
83
                                        $elementcounter++;
99
84
                                }
 
85
                                recurseelement( $child );
 
86
                        } else if( $child -> getName() == "use" || $child -> getName() == "ellipse" || $child -> getName() == "circle" || $child -> getName() == "stop" || $child -> getName() == "polygon" || $child -> getName() == "line" || $child -> getName() == "polyline" || $child -> getName() == "path" || $child -> getName() == "rect" || $child -> getName() == "text" ) {
 
87
                                // Add element to queue
 
88
                                $graphnodes [$elementcounter] = $child;
 
89
                                $elementcounter++;
 
90
                        }else{
 
91
                                echo "//Unknown element: ".$child -> getName()."<br>";
 
92
                        }
100
93
                }               
101
 
 
102
 
                if($element->getName()=="g"){
103
 
                                                                        // End of group code
104
 
                                                                        $attrs=$element->attributes();                  
105
 
                                                                        $sxe = new SimpleXMLElement("<eg id='".$attrs['id']."' />");
106
 
                                                                        $graphnodes[$elementcounter]=$sxe;
107
 
                                                                        $elementcounter++;
108
 
                                                
109
 
                }
110
 
 
111
 
                if($element->getName()=="defs"){
112
 
                                                                        $sxe = new SimpleXMLElement("<defsend/>");
113
 
                                                                        $graphnodes[$elementcounter]=$sxe;
114
 
                                                                        $elementcounter++;
115
 
                                                
116
 
                }
117
 
 
 
94
                if( $element -> getName() == "g" ) {
 
95
                        // End of group code
 
96
                        $attrs = $element -> attributes();                      
 
97
                        $sxe = new SimpleXMLElement( "<eg id='".$attrs['id']."' />" );
 
98
                        $graphnodes [$elementcounter] = $sxe;
 
99
                        $elementcounter++;
 
100
                }
 
101
                if( $element -> getName() == "defs" ) {
 
102
                        $sxe = new SimpleXMLElement( "<defsend/>" );
 
103
                        $graphnodes [$elementcounter] = $sxe;
 
104
                        $elementcounter++;      
 
105
                }
118
106
}
119
107
 
120
 
if(isset($_POST['svgname'])){
121
 
 
122
 
                        $svg = simplexml_load_file($_POST['svgname']);
123
 
                        
124
 
                        // Recurse into elements and add to element stack
125
 
                        // its important that we only process hierarchies of g elements and layers
126
 
                        foreach ($svg as $element) {
127
 
                                        if($element->getName()=="clipPath"||$element->getName()=="defs"||$element->getName()=="g"||$element->getName()=="linearGradient"||$element->getName()=="defs"||$element->getName()=="radialGradient"){
128
 
                                                        if($element->getName()=="clipPath"||$element->getName()=="defs"||$element->getName()=="g"||$element->getName()=="linearGradient"||$element->getName()=="radialGradient"){
129
 
                                                                        $graphnodes[$elementcounter]=$element;
130
 
                                                                        $elementcounter++;
131
 
                                                        }
132
 
                                                        recurseelement($element);
133
 
                                        }else if($element->getName()=="use"||$element->getName()=="radialGradient"||$element->getName()=="linearGradient"||$element->getName()=="polygon"||$element->getName()=="line"||$element->getName()=="polyline"||$element->getName()=="path"||$element->getName()=="rect"||$element->getName()=="ellipse"||$element->getName()=="circle"||$element->getName()=="text"){
134
 
                                                        // Add element to queue
135
 
                                                        $graphnodes[$elementcounter]=$element;
136
 
                                                        $elementcounter++;
137
 
                                        }else{
138
 
                                                        echo "//Unknown element: ".$element->getName()."\n";
139
 
                                        }
140
 
                        }
141
 
 
142
 
                        $defsmode=0;
143
 
                        $defsstring="";
144
 
                        $defsid="";
145
 
                        $clipid="";
146
 
                        
147
 
                        // Process elements
148
 
                        foreach ($graphnodes as $graphelement) {
149
 
 
150
 
                                // Clear Line Style and Fill Styles
151
 
                                $fillstyle="none";
152
 
                                $linestyle="none";      
153
 
                                $opacity="1.0";
154
 
                                                        
155
 
                                // For text element get 
156
 
                                if($graphelement->getName()=="text"){
157
 
                                                if(isset($graphelement[0])){
158
 
                                                                $textline=$graphelement[0];
159
 
                                                }
160
 
                                }
161
 
 
162
 
                                // To get ID comment/code
163
 
                                $attrs=$graphelement->attributes();
164
 
                                $xlinkattrs=$graphelement->attributes('http://www.w3.org/1999/xlink');
165
 
 
166
 
                                // For use element get 
167
 
                                if($graphelement->getName()=="use"){
168
 
                                                if(isset($attrs['id'])){
169
 
                                                                // We are in an id use statement
170
 
                                                                $clipid=strval($attrs['id']);
171
 
                                                }else{
172
 
                                                                // We are in the reference use statement
173
 
                                                                $clippaths[$clipid]=substr(strval($xlinkattrs['href']),1);
174
 
                                                }
175
 
                                }
176
 
 
177
 
                                if($graphelement->getName()=="linearGradient"){
178
 
                                                
179
 
                                                if(isset($attrs['id'])){
180
 
                                                                $gradientname=$attrs['id'];
181
 
                                                }
182
 
                                                if(!isset($attrs['x1'])){
183
 
                                                                // Linear Gradient is not complete, this means that it is an inkscape element!
184
 
                                                                $isinkscape=true;
185
 
                                                }else if(isset($xlinkattrs['href'])){
186
 
 
187
 
                                                                echo "var ".$gradientname."=c.createLinearGradient(".$attrs['x1'].",".$attrs['y1'].",".$attrs['x2'].",".$attrs['y2'].");\n";
188
 
 
189
 
                                                                // Now we create a new gradient with the following properties
190
 
                                                                $gradientref=$xlinkattrs['href'];
191
 
                                                                $gradientref=substr($gradientref,1,strlen($gradientref)-1);
192
 
                                                                
193
 
                                                                if(isset($colorstops["$gradientref"])){
194
 
                                                                                foreach($colorstops["$gradientref"] as $key => $value){
195
 
                                                                                                echo $gradientname.".addColorStop(".$value.");\n";
196
 
                                                                                }
197
 
                                                                }
198
 
                                                                                                                                
199
 
                                                }else{
200
 
                                                                echo "var ".$gradientname."=c.createLinearGradient(".$attrs['x1'].",".$attrs['y1'].",".$attrs['x2'].",".$attrs['y2'].");\n";
201
 
                                                }
202
 
                                }else if($graphelement->getName()=="radialGradient"){
203
 
                                                if(isset($attrs['id'])){
204
 
                                                                $gradientname=$attrs['id'];
205
 
                                                }
206
 
                                                if(!isset($attrs['cx'])){
207
 
                                                                // Radial Gradient is not complete, this means that it is an inkscape element!
208
 
                                                                $isinkscape=true;
209
 
                                                }else if(isset($xlinkattrs['href'])){
210
 
                                                                echo "var ".$gradientname."=c.createRadialGradient(".$attrs['cx'].",".$attrs['cy'].",0,".$attrs['cx'].",".$attrs['cy'].",".$attrs['r'].");\n";
211
 
 
212
 
                                                                // Now we create a new gradient with the following properties
213
 
                                                                $gradientref=$xlinkattrs['href'];
214
 
                                                                $gradientref=substr($gradientref,1,strlen($gradientref)-1);
215
 
                                                                
216
 
                                                                if(isset($colorstops["$gradientref"])){
217
 
                                                                                foreach($colorstops["$gradientref"] as $key => $value){
218
 
                                                                                                echo $gradientname.".addColorStop(".$value.");\n";
219
 
                                                                                }
220
 
                                                                }
221
 
                                                                                                                                
222
 
                                                }else{
223
 
                                                                echo "var ".$gradientname."=c.createRadialGradient(".$attrs['cx'].",".$attrs['cy'].",0,".$attrs['cx'].",".$attrs['cy'].",".$attrs['r'].");\n";
224
 
                                                }
225
 
                                }else if($graphelement->getName()=="stop"){
226
 
                                                $stopcolor=$attrs['style'];
227
 
                                                if(strpos($stopcolor,";",11)!==false){
228
 
                                                                $stopcolorend=strpos($stopcolor,";",11);
229
 
                                                }else{
230
 
                                                                $stopcolorend=strlen($stopcolor);
231
 
                                                }
232
 
                                                $stopcolor=substr($stopcolor,11,$stopcolorend-11);
233
 
                                                
234
 
                                                if($isinkscape){
235
 
                                                                        if(isset($colorstops["$gradientname"])){
236
 
                                                                                        array_push($colorstops["$gradientname"],$attrs['offset'].",'".$stopcolor."'");
237
 
                                                                        }else{
238
 
                                                                                        $poo=array();
239
 
                                                                                        array_push($poo,$attrs['offset'].",'".$stopcolor."'");
240
 
                                                                                        $colorstops["$gradientname"]=$poo;
241
 
                                                                        }
242
 
                                                }else{
243
 
                                                                        echo $gradientname.".addColorStop(".$attrs['offset'].",'".$stopcolor."');\n";
244
 
                                                }
245
 
                                                
246
 
                                }else{
247
 
                                                                // We assume that defsid is the only use of ID
248
 
                                                                if(isset($attrs['id'])) $defsid=$attrs['id'];
249
 
                                }
250
 
 
251
 
                                // For each attribute of svg                                                            
252
 
 
253
 
                                // We process attributes after gradients but before the drawing elements.
254
 
 
255
 
                          foreach ($graphelement->attributes() as $key => $val) {
256
 
 
257
 
                                        // Get font parameters!
258
 
                            if ($key == "if"&&$graphelement->getName()=="text"){
259
 
                                                        $fontline=$val;
260
 
                            }           
261
 
                                
262
 
                                        // Get font parameters!
263
 
                            if ($key == "font-size"&&$graphelement->getName()=="text"){
264
 
                                                        $fontline=$val;
265
 
                            }           
266
 
                                    
267
 
                            if ($key == "font-family"&&$graphelement->getName()=="text"){
268
 
                                                        $fontfamily=$val;
269
 
                                        $fontfamily=str_replace("'","",$fontfamily);
270
 
                                        $fontstyle="";
271
 
                                        if(!(strpos($fontfamily,"-Oblique")===FALSE)){
272
 
                                                        $fontfamily=str_replace("-Oblique","",$fontfamily);
273
 
                                                        $fontstyle="Bold";                                                      
274
 
                                        }
275
 
                                        if(!(strpos($fontfamily,"-Bold")===FALSE)){
276
 
                                                        $fontfamily=str_replace("-Bold","",$fontfamily);
277
 
                                                        $fontstyle="Bold";                                                      
278
 
                                        }
279
 
                                        if(!(strpos($fontfamily,"-Italic")===FALSE)){
280
 
                                                        $fontfamily=str_replace("-Italic","",$fontfamily);
281
 
                                                        $fontstyle="Italic";                                                    
282
 
                                        }                                       
283
 
                                        if(!(strpos($fontfamily,"Bold")===FALSE)){
284
 
                                                        $fontfamily=str_replace("Bold","",$fontfamily);
285
 
                                                        $fontstyle=$fontstyle." Bold";                                                  
286
 
                                        }
287
 
                                        if(!(strpos($fontfamily,"Italic")===FALSE)){
288
 
                                                        $fontfamily=str_replace("Italic","",$fontfamily);
289
 
                                                        $fontstyle=$fontstyle." Italic";                                                        
290
 
                                        }
291
 
 
292
 
                            }
293
 
 
294
 
                            if ($graphelement->getName()=="text"){
295
 
                                                        if($key=="x") $textx=$val;
296
 
                                                        if($key=="y") $texty=$val;
297
 
                            }
298
 
 
299
 
                            if ($graphelement->getName()=="rect"){
300
 
                                                        if($key=="x") $linex1=$val;
301
 
                                                        if($key=="y") $liney1=$val;
302
 
                                                        if($key=="width") $linex2=$val;
303
 
                                                        if($key=="height") $liney2=$val;
304
 
                            }
305
 
 
306
 
                            if ($graphelement->getName()=="ellipse"||$graphelement->getName()=="circle"){
307
 
                                                        // Element is either a circle or an ellipse
308
 
                                                        if($key=="cx") $cx=$val;
309
 
                                                        if($key=="cy") $cy=$val;
310
 
                                                        if($key=="r"){
311
 
                                                                        $rx=$val;
312
 
                                                                        $ry=$val;
313
 
                                                        } 
314
 
                                                        if($key=="rx") $rx=$val;
315
 
                                                        if($key=="ry") $ry=$val;
316
 
                            }
317
 
                            
318
 
                            if (($key == "x1"||$key == "y1"||$key == "x2"||$key == "y2")&&$graphelement->getName()=="line"){
319
 
                                                        // Element is a line element with 4 coordinate parameters
320
 
                                                        if($key=="x1") $linex1=$val;
321
 
                                                        if($key=="x2") $linex2=$val;
322
 
                                                        if($key=="y1") $liney1=$val;
323
 
                                                        if($key=="y2") $liney2=$val;
324
 
                            }else if ($key == "transform"&&$graphelement->getName()=="text"){
325
 
                                
326
 
                                                        $j=0;
327
 
                                                        $dostr="";
328
 
                                                        $params=array();
329
 
                                                        $noparams=0;
330
 
                                                        $workstr=$val;
331
 
                        
332
 
                                                        do{
333
 
                                                                        $dochr=substr($workstr,$j,1);
334
 
                                                                        if($dochr==" "||$dochr=="("||$dochr==")"){
335
 
                                                                                if(trim($dostr)!=""&&$dostr!="matrix"){
336
 
                                                                                                $params[$noparams++]=$dostr;
337
 
                                                                                }
338
 
                                                                                        if($dochr=="-"){
339
 
                                                                                                        $dostr=$dochr;                                                                                          
340
 
                                                                                        }else{
341
 
                                                                                                        $dostr="";
342
 
                                                                                        }
343
 
                                                                        }else{
344
 
                                                                                        $dostr.=$dochr;
345
 
                                                                        }
346
 
                                                                        $j++;
347
 
                                                        }while($j<=strlen($workstr));                                                                           
348
 
                                                if(trim($dostr)!=""){
349
 
                                                                        $params[$noparams++]=$dostr;
350
 
                                                        }
351
 
                                                                                        
352
 
                                                        $translate="c.translate(".$params[4].",".$params[5].");\n";
353
 
                                                        $rotate="c.rotate(".$params[1].",".$params[2].");\n";
354
 
                                                        $scale="c.scale(".$params[0].",".$params[3].");\n";
355
 
                                                                                                                        
356
 
                            }elseif ($key == "stroke"){
357
 
                                                        echo "" .'c.strokeStyle = "' . $val . '";' . "\n";
358
 
                                                        $linestyle=$val;
359
 
                            }elseif ($key == "opacity"){
360
 
                                        $opacity=$val;
361
 
                            }elseif ($key == "fill"){
362
 
                                      if($val!="none"){
363
 
                                                                        if(strpos($val,"url(")===false){
364
 
                                                                                        echo "" .'c.fillStyle = "' . $val. '";' . "\n";
365
 
                                                                        }else{
366
 
                                                                                        echo "c.fillStyle=".substr($val,5,strlen($val)-6).";\n";
367
 
                                                                        }
368
 
                                                        }
369
 
                                                        $fillstyle=$val;
370
 
                            }elseif ($key == "style"){
371
 
                                        $fillpos=strpos($val,"fill:");
372
 
                                        if($fillpos!==false){
373
 
                                                        $fillposend=strpos($val,";",$fillpos);
374
 
                                                if($fillposend===false) $fillposend=strlen($val);                               
375
 
                                                        $fillstyle=substr($val,$fillpos+5,$fillposend-$fillpos-5);
376
 
                                                            if($fillstyle!="none"){
377
 
                                                                                        if(strpos($fillstyle,"url(")===false){
378
 
                                                                                                        echo "c.fillStyle = '".$fillstyle ."';\n";
379
 
                                                                                        }else{
380
 
                                                                                                        echo "c.fillStyle=".substr($fillstyle,5,strlen($fillstyle)-6).";\n";
381
 
                                                                                        }
382
 
                                                                        }       
383
 
                                        }
384
 
 
385
 
                                        $strokepos=strpos($val,"stroke:");
386
 
                                        if($strokepos!==false){
387
 
                                                        $strokeposend=strpos($val,";",$strokepos);
388
 
                                                        if($strokeposend===false) $strokeposend=strlen($val);
389
 
                                                                        $linestyle=substr($val,$strokepos+7,$strokeposend-$strokepos-7);
390
 
                                                              if($linestyle!="none"){
391
 
                                                                                                echo "" .'c.strokeStyle = "' . $linestyle . '";' . "\n";
392
 
                                                                                }
393
 
                                        }
394
 
 
395
 
                                        $strokepos=strpos($val,"stroke-width:");
396
 
                                        if($strokepos!==false){
397
 
                                                        $strokeposend=strpos($val,";",$strokepos);
398
 
                                                        if($strokeposend===false) $strokeposend=strlen($val);
399
 
                                                                        $strokewidth=substr($val,$strokepos+13,$strokeposend-$strokepos-13);
400
 
                                                                                $strokewidth=str_replace("px","",$strokewidth);
401
 
                                                                                echo "\n" . 'c.lineWidth = "' . $strokewidth . '";' . "\n";
402
 
                                        }       
403
 
 
404
 
                                        $fontsizepos=strpos($val,"font-size:");
405
 
                                        if($fontsizepos!==false){
406
 
                                                        $fontsizeend=strpos($val,";",$fontsizepos);
407
 
                                                        if($fontsizeend===false) $fontsizeend=strlen($val);
408
 
                                                                        $fontline=substr($val,$fontsizepos+10,$fontsizeend-$fontsizepos-10);
409
 
                                                                                $fontline=str_replace("px","",$fontline);
410
 
                                        }       
411
 
 
412
 
                                        $fontfamilypos=strpos($val,"font-family:");
413
 
                                        if($fontfamilypos!==false){
414
 
                                                        $fontfamilyend=strpos($val,";",$fontfamilypos);
415
 
                                                        if($fontfamilyend===false) $fontfamilyend=strlen($val);
416
 
                                                                        $fontfamily=substr($val,$fontfamilypos+12,$fontfamilyend-$fontfamilypos-12);
417
 
                                        }       
418
 
 
419
 
                                        $fontstylepos=strpos($val,"font-style:");
420
 
                                        if($fontstylepos!==false){
421
 
                                                        $fontstyleend=strpos($val,";",$fontstylepos);
422
 
                                                        if($fontstyleend===false) $fontstyleend=strlen($val);
423
 
                                                                        $fontstyle=substr($val,$fontstylepos+11,$fontstyleend-$fontstylepos-11);
424
 
                                        }       
425
 
                                                                                                                
426
 
                            }elseif ($key == "stroke-width"){
427
 
                                                        echo "\n" . 'c.lineWidth = "' . $val . '";' . "\n";
428
 
                            }elseif ($key == "points"&&($graphelement->getName()=="polygon"||$graphelement->getName()=="polyline"||$graphelement->getName()=="line")) {
429
 
                                if($defsmode){
430
 
                                                $defsstring.="c.beginPath();\n";                                                                                                
431
 
                                }else{
432
 
                                                echo "c.beginPath();\n";                                
433
 
                                }
434
 
 
435
 
                                                        // dostr loop for polygons. Bugfix: if old char is e, then we do not break string at -                                                  
436
 
                                                        $j=0;
437
 
                                                        $dostr="";
438
 
                                                        $dochr="";
439
 
                                                        $params=array();
440
 
                                                        $noparams=0;
441
 
                                                        $workstr=$val;
442
 
                                                        $oldchr="";
443
 
                                                        do{
444
 
                                                                        $oldchr=$dostr;
445
 
                                                                        $dochr=substr($workstr,$j,1);
446
 
                                                                        if(($dochr=="-" && $oldchr!="e" && $oldchr!="E")||$dochr==","||$dochr==" "){
447
 
                                                                                if(trim($dostr)!=""){
448
 
                                                                                                $params[$noparams++]=$dostr;
449
 
                                                                                }
450
 
                                                                                        if($dochr=="-"){
451
 
                                                                                                        $dostr=$dochr;                                                                                          
452
 
                                                                                        }else{
453
 
                                                                                                        $dostr="";
454
 
                                                                                        }
455
 
                                                                        }else{
456
 
                                                                                        $dostr.=$dochr;
457
 
                                                                        }
458
 
                                                                        $j++;
459
 
                                                        }while($j<=strlen($workstr));                                                                           
460
 
                                                if(trim($dostr)!=""){
461
 
                                                                        $params[$noparams++]=$dostr;
462
 
                                                        }
463
 
                                                        
464
 
                                                        for($j=0;$j<$noparams;$j+=2){
465
 
                                                                                if($j==0){
466
 
                                                                if($defsmode){
467
 
                                                                                                        $defsstring.="c.moveTo(".$params[$j].",".$params[$j+1].");\n";                                                  
468
 
                                                                }else{
469
 
                                                                                                        echo "c.moveTo(".$params[$j].",".$params[$j+1].");\n";                                                  
470
 
                                                                }
471
 
                                                                                }else{
472
 
                                                                if($defsmode){
473
 
                                                                                                        $defsstring.="c.lineTo(".$params[$j].",".$params[$j+1].");\n";                                                                                                          
474
 
                                                                }else{
475
 
                                                                                                        echo "c.lineTo(".$params[$j].",".$params[$j+1].");\n";                                                                                                          
476
 
                                                                }
477
 
                                                                                }
478
 
                                                        }
479
 
                        
480
 
                                                        // If a polygon close path if not i.e. polyline keep it open
481
 
                                                        if($noparams>=2&&$graphelement->getName()=="polygon"){
482
 
                                                if($defsmode){
483
 
                                                                                        $defsstring.="c.lineTo(".$params[0].",".$params[1].");\n";                                                                                                              
484
 
                                                }else{
485
 
                                                                                        echo "c.lineTo(".$params[0].",".$params[1].");\n";                                                                                                              
486
 
                                                }
487
 
                                                        }
488
 
                                                
489
 
                                if(!$defsmode){
490
 
                                                                        echo "c.globalAlpha = $opacity;\n";
491
 
                                }
492
 
 
493
 
                                                        if($fillstyle=="none"&&$linestyle=="none"){
494
 
                                                if(!$defsmode){
495
 
                                                                                        echo 'c.fillStyle = "#000";';
496
 
                                                                                        echo "\n";
497
 
                                                                echo "c.fill();\n\n";
498
 
                                                }
499
 
                                                        }
500
 
                                                        if($fillstyle!="none"){
501
 
                                                if(!$defsmode){
502
 
                                                                echo "c.fill();\n";
503
 
                                                                        if($linestyle=="none") echo "\n";
504
 
                                                }
505
 
                                            }
506
 
                                            if($linestyle!="none"){
507
 
                                                if(!$defsmode){
508
 
                                                                echo "c.stroke();\n\n";
509
 
                                                }
510
 
                                            }
511
 
                                            
512
 
                                            if($defsmode){
513
 
                                                        $defsstring.="c.clip();\n\n";           
514
 
                                            }
515
 
                                                                                                
516
 
                            }elseif ($key == "d") {
517
 
                                
518
 
                                $dval=$val;
519
 
                                
520
 
                            }
521
 
                          }
522
 
 
523
 
                                // Draw d line commands. This is a fix for the bug that requires the line settings to be assigned before the line commands
524
 
                                
525
 
                                if(isset($dval)){
526
 
                                
527
 
                              echo "c.beginPath();\n";
528
 
                              
529
 
                              $i=0;
530
 
                              $str=$dval;
531
 
                              $workstr="";
532
 
                              $command="";
533
 
                              $cx=0;
534
 
                              $cy=0;
535
 
                              $firstpoint=0;
536
 
                              $firstpointx=0;
537
 
                              $firstpointy=0;
538
 
                              $lastpointx=0;
539
 
                              $lastpointy=0;
540
 
                              do{
541
 
                                                $chr=substr($str,$i,1);
542
 
                                                if($chr=="H"||$chr=="h"||$chr=="V"||$chr=="v"||$chr=="M"||$chr=="m"||$chr=="C"||$chr=="c"||$chr=="L"||$chr=="A"||$chr=="a"||$chr=="l"||$chr=="z"||$chr=="Z"||$chr=="s"||$chr=="S"||$i==strlen($str)){
543
 
                                                                        // Process Parameters for any parameter command
544
 
                                                                        if($command=="M"||$command=="m"||$command=="c"||$command=="C"||$command=="v"||$command=="V"||$command=="h"||$command=="H"||$command=="s"||$command=="S"||$command=="l"||$command=="L"||$command=="a"||$command=="A"){
545
 
                                                                        $j=0;
546
 
                                                                                $dostr="";
547
 
                                                                                        $dochr="";
548
 
                                                                                $oldchr="";
549
 
                                                                                $params=array();
550
 
                                                                                $noparams=0;
551
 
                                                                                
552
 
                                                                                // dochr loop for other drawing commands. Bug fix: If we encounter - and before it is an e we do nothing, otherwise proceed as normal
553
 
                                                                                
554
 
                                                                                do{
555
 
                                                                                                        $oldchr=$dochr;
556
 
                                                                                                        $dochr=substr($workstr,$j,1);
557
 
                                                                                                        if(($dochr=="-" && $oldchr!="e" && $oldchr!="E")||$dochr==","||$dochr==" "){
558
 
                                                                                                        if(trim($dostr)!=""){
559
 
                                                                                                                        $params[$noparams++]=$dostr;
560
 
                                                                                                        }
561
 
                                                                                                                        if($dochr=="-"){
562
 
                                                                                                                                        $dostr=$dochr;                                                                                          
563
 
                                                                                                                        }else{
564
 
                                                                                                                                        $dostr="";
565
 
                                                                                                                        }
566
 
                                                                                                        }else{
567
 
                                                                                                                        $dostr.=$dochr;
568
 
                                                                                                        }
569
 
                                                                                                        $j++;
570
 
                                                                                }while($j<=strlen($workstr));                                                                           
571
 
                                                                        if(trim($dostr)!=""){
572
 
                                                                                                        $params[$noparams++]=$dostr;
573
 
                                                                                        }
574
 
                                                                        }
575
 
                        
576
 
                                                        if($command=="M" || $command=="m"){
577
 
                                                                                        for($j=0;$j<$noparams;$j+=2){
578
 
                                                                                                        if($j==0){
579
 
                                                                                                                                // Normal moveto set cx,cy
580
 
                                                                                                                                if($command=="M"){
581
 
                                                                                                                                                                $cx=$params[$j];
582
 
                                                                                                                                                                $cy=$params[$j+1];
583
 
                                                                                                                                                                echo "c.moveTo(".$cx.",".$cy.");\n";
584
 
                                                                                                                                                                $firstpoint=1;
585
 
                                                                                                                                              $firstpointx=$cx;
586
 
                                                                                                                                        $firstpointy=$cy;
587
 
                                                                                                                                }else if($command=="m"){
588
 
                                                                                                                                                if(!$firstpoint){
589
 
                                                                                                                                                                $cx=$params[$j];
590
 
                                                                                                                                                                $cy=$params[$j+1];
591
 
                                                                                                                                                                echo "c.moveTo(".$cx.",".$cy.");\n";
592
 
                                                                                                                                                                $firstpoint=1;
593
 
                                                                                                                                              $firstpointx=$cx;
594
 
                                                                                                                                        $firstpointy=$cy;
595
 
                                                                                                                                                }else{
596
 
                                                                                                                                                                $cx+=$params[$j];
597
 
                                                                                                                                                                $cy+=$params[$j+1];                                                                                                                     
598
 
                                                                                                                                                                echo "c.moveTo(".$cx.",".$cy.");\n";
599
 
                                                                                                                                                }                                                                                                       
600
 
                                                                                                                                }
601
 
                                                                                                        }else{
602
 
                                                                                                                                // Implicit lineto
603
 
                                                                                                                                if($command=="M"){
604
 
                                                                                                                                                $cx=$params[$j];
605
 
                                                                                                                                                $cy=$params[$j+1];
606
 
                                                                                                                                                echo "c.lineTo(".$cx.",".$cy.");\n";
607
 
                                                                                                                                }else if($command=="m"){
608
 
                                                                                                                                                $cx+=$params[$j];
609
 
                                                                                                                                                $cy+=$params[$j+1];                                                                                                                     
610
 
                                                                                                                                                echo "c.lineTo(".$cx.",".$cy.");\n";
611
 
                                                                                                                                }
612
 
                                                                                                        }
613
 
                                                                                        }
614
 
                                                        }else if ($command=="C"||$command=="c"){
615
 
                                                                        // Bezier Curveto
616
 
                                                                                        for($j=0;$j<$noparams;$j+=6){
617
 
                                                                                                        if($command=="C"){
618
 
                                                                                                                        $p1x=$params[$j];
619
 
                                                                                                                        $p1y=$params[$j+1];
620
 
                                                                                                                        $p2x=$params[$j+2];
621
 
                                                                                                                        $p2y=$params[$j+3];
622
 
                                                                                                                        $cx=$params[$j+4];
623
 
                                                                                                                        $cy=$params[$j+5];
624
 
                                                                                                                        $lastpointx=$p2x;
625
 
                                                                                                                        $lastpointy=$p2y;
626
 
                                                                                                                        echo "c.bezierCurveTo(".$p1x.",".$p1y.",".$p2x.",".$p2y.",".$cx.",".$cy.");\n";
627
 
                                                                                                                        // Curveto absolute set cx to final point, other coordinates are control points
628
 
                                                                                                        }else if($command=="c"){
629
 
                                                                                                                        // Curveto relative set cx to final point, other coordinates are relative control points
630
 
                                                                                                                        $p1x=$cx+$params[$j];
631
 
                                                                                                                        $p1y=$cy+$params[$j+1];
632
 
                                                                                                                        $p2x=$cx+$params[$j+2];
633
 
                                                                                                                        $p2y=$cy+$params[$j+3];
634
 
                                                                                                                        $lastpointx=$p2x;
635
 
                                                                                                                        $lastpointy=$p2y;
636
 
                                                                                                                        $cx+=$params[$j+4];
637
 
                                                                                                                        $cy+=$params[$j+5];
638
 
                                                                                                                        echo "c.bezierCurveTo(".$p1x.",".$p1y.",".$p2x.",".$p2y.",".$cx.",".$cy.");\n";
639
 
                                                                                                        }
640
 
                                                                                        }
641
 
                                                        }else if ($command=="S"||$command=="s"){
642
 
                                                                                                                        //context . quadraticCurveTo(cpx, cpy, x, y)
643
 
                                                                        // Quadratic Curveto
644
 
                                                                                        for($j=0;$j<$noparams;$j+=4){
645
 
                                                                                                        if($command=="S"){
646
 
                                                                                                                        $p1x=$params[$j];
647
 
                                                                                                                        $p1y=$params[$j+1];
648
 
                                                                                                                        $cx=$params[$j+2];
649
 
                                                                                                                        $cy=$params[$j+3];
650
 
                                                                                                                        $lastpointx=$p1x;
651
 
                                                                                                                        $lastpointy=$p1y;
652
 
                                                                                                                  echo "c.bezierCurveTo(".$lastpointx.",".$lastpointy.",".$p1x.",".$p1y.",".$cx.",".$cy.");\n";
653
 
                                                                                                                  // Curveto absolute set cx to final point, other coordinates are control points
654
 
                                                                                                        }else if($command=="s"){
655
 
                                                                                                                        // Curveto relative set cx to final point, other coordinates are relative control points
656
 
                                                                                                                        $p1x=$cx+$params[$j];
657
 
                                                                                                                        $p1y=$cy+$params[$j+1];
658
 
                                                                                                                        $cx+=$params[$j+2];
659
 
                                                                                                                        $cy+=$params[$j+3];
660
 
                                                                                                                        $lastpointx=$p1x;
661
 
                                                                                                                        $lastpointy=$p1y;
662
 
                                                                                                                        echo "c.bezierCurveTo(".$lastpointx.",".$lastpointy.",".$p1x.",".$p1y.",".$cx.",".$cy.");\n";
663
 
                                                                                                        }
664
 
                                                                                        }
665
 
                                                        }else if ($command=="V"||$command=="v"){
666
 
                                                                        // Vertical Lineto
667
 
                                                                                        for($j=0;$j<$noparams;$j++){
668
 
                                                                                                        if($command=="V"){
669
 
                                                                                                                        $cy=$params[$j];
670
 
                                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
671
 
                                                                                                        }else if($command=="v"){
672
 
                                                                                                                        // Curveto relative set cx to final point, other coordinates are relative control points
673
 
                                                                                                                        $cy+=$params[$j];
674
 
                                                                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
675
 
                                                                                                        }
676
 
                                                                                        }
677
 
                                                        }else if ($command=="H"||$command=="h"){
678
 
                                                                        // horizontal Lineto
679
 
                                                                                        for($j=0;$j<$noparams;$j++){
680
 
                                                                                                        if($command=="H"){
681
 
                                                                                                                        $cx=$params[$j];
682
 
                                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
683
 
                                                                                                        }else if($command=="h"){
684
 
                                                                                                                        // Curveto relative set cx to final point, other coordinates are relative control points
685
 
                                                                                                                        $cx+=$params[$j];
686
 
                                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
687
 
                                                                                                        }
688
 
                                                                                        }
689
 
                                                        }else if ($command=="L"||$command=="l"){
690
 
                                                                        // Lineto
691
 
                                                                                        for($j=0;$j<$noparams;$j+=2){
692
 
                                                                                                        if($command=="L"){
693
 
                                                                                                                        $cx=$params[$j];
694
 
                                                                                                                        $cy=$params[$j+1];
695
 
                                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
696
 
                                                                                                        }else if($command=="l"){
697
 
                                                                                                                        // Curveto relative set cx to final point, other coordinates are relative control points
698
 
                                                                                                                        $cx+=$params[$j];
699
 
                                                                                                                        $cy+=$params[$j+1];
700
 
                                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
701
 
                                                                                                        }
702
 
                                                                                        }
703
 
                                                        }else if ($command=="Z"||$command=="z"){
704
 
                                                                                        echo "c.lineTo(".$firstpointx.",".$firstpointy.");\n";                                  
705
 
                                                                        }else if ($command=="A"||$command=="a"){
706
 
                                                                                        // To avoid hard math - draw arc as a line
707
 
                                                                                        for($j=0;$j<$noparams;$j+=7){
708
 
                                                                                                        $rx=$params[0];
709
 
                                                                                                        $ry=$params[1];
710
 
                                                                                                        $ang=$params[2];
711
 
                                                                                                        $largeflag=$params[3];
712
 
                                                                                                        $sweepflag=$params[4];
713
 
                                                                                                        $cx=$params[$j];
714
 
                                                                                                        $cy=$params[$j+1];
715
 
                                                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
716
 
                                                                        }
717
 
                                                        }
718
 
                                                        
719
 
                                                        // Init new command!
720
 
                                                        $command=$chr;
721
 
                                                        $workstr="";
722
 
                                                }else{
723
 
                                                        $workstr.=$chr;
724
 
                                                }
725
 
                                        $i++;
726
 
                              }while($i<=strlen($str));
727
 
                                                
728
 
                                                echo "c.globalAlpha = $opacity;\n";
729
 
 
730
 
                                                if($fillstyle=="none"&&$linestyle=="none"){
731
 
                                                                echo 'c.fillStyle = "#000";';
732
 
                                        echo "\n";
733
 
                                        echo "c.fill();\n\n";
734
 
                                                }
735
 
                                                if($fillstyle!="none"){
736
 
//                                                              echo "c.save();\n\n";
737
 
//                                                              echo "c.scale(2.5,2.5);\n";                                                             
738
 
                                        echo "c.fill();\n";
739
 
//                                                              echo "c.restore();\n\n";
740
 
                                                if($linestyle=="none") echo "\n";
741
 
                                }
742
 
                                if($linestyle!="none"){
743
 
                                        echo "c.stroke();\n";
744
 
                                        echo "\n";
745
 
                              }
746
 
                              
747
 
                              unset($dval);
748
 
                                
749
 
                                }
750
 
                        
751
 
                          if($graphelement->getName()=="text"){
752
 
                        
753
 
                                                echo "c.globalAlpha = $opacity;\n";
754
 
 
755
 
                                                echo "c.beginPath();\n";
756
 
                                                echo "c.save();\n";
757
 
                                                echo "c.font = '".$fontstyle." ".$fontline."px ".$fontfamily."';\n";
758
 
 
759
 
                                                if(isset($translate)&&isset($rotate)&&isset($scale)){
760
 
                                                                echo $translate;
761
 
                                                                echo $rotate;
762
 
                                                                echo $scale;
763
 
                                                }
764
 
                                                
765
 
                                                if(isset($textx)&&isset($texty)){
766
 
                                                                echo "c.fillText('".$textline."',".$textx.",".$texty.");\n";                                            
767
 
                                                }else{
768
 
                                                                echo "c.fillText('".$textline."',0,0);\n";                                                                                              
769
 
                                                }
770
 
                                                
771
 
 
772
 
                                if($linestyle!="none"){
773
 
                                                                if(isset($textx)&&isset($texty)){
774
 
                                                        echo "c.strokeText('".$textline."',".$textx.",".$texty.");\n";
775
 
                                                        }else{
776
 
                                                        echo "c.strokeText('".$textline."',0,0);\n";                                                    
777
 
                                                        }
778
 
                                          }                                 
779
 
 
780
 
                                                echo "c.restore();\n\n";
781
 
 
782
 
                          }elseif($graphelement->getName()=="rect"){
783
 
                                                
784
 
                                                // This rectangle must be clipped!
785
 
                                                if(isset($attrs['clip-path'])){
786
 
                                                                        $clipid=substr($attrs['clip-path'],5);
787
 
                                                                        $clipid=substr($clipid,0,strlen($clipid)-1);
788
 
                                                                        $clipid=$clippaths[$clipid];
789
 
                                                                        
790
 
                                                                        echo "c.save();\n";
791
 
                                                                        
792
 
                                                                        echo $clipdefs[strval($clipid)];
793
 
                                                }
794
 
                                                
795
 
                                                echo "c.globalAlpha = $opacity;\n";
796
 
 
797
 
                                                echo "c.beginPath();\n";
798
 
                                                echo "c.moveTo(".$linex1.",".$liney1.");\n";
799
 
                                                echo "c.lineTo(".($linex1+$linex2).",".($liney1).");\n";
800
 
                                                echo "c.lineTo(".($linex1+$linex2).",".($liney1+$liney2).");\n";
801
 
                                                echo "c.lineTo(".($linex1).",".($liney1+$liney2).");\n";                        
802
 
                                                echo "c.lineTo(".($linex1).",".($liney1).");\n";                        
803
 
                                    if($fillstyle!="none"){
804
 
//                                                              echo "c.save();\n\n";
805
 
//                                                              echo "c.scale(1,1);\n";                                                         
806
 
                                        echo "c.fill();\n";
807
 
//                                                              echo "c.restore();\n\n";
808
 
 
809
 
                                                if($linestyle=="none") echo "\n";
810
 
                                }
811
 
                                    if($linestyle!="none"){
812
 
                                        echo "c.stroke();\n";
813
 
                             }
814
 
                                                // This rectangle must be clipped!
815
 
                                                if(isset($attrs['clip-path'])){
816
 
                                                                        echo "c.restore();\n";
817
 
                                                }
818
 
                                                
 
108
if( isset( $_POST ['svgname'])) {
 
109
        $svg = simplexml_load_file( $_POST ['svgname'] );
 
110
        // Recurse into elements and add to element stack
 
111
        // It is important to only process hierarchies of g elements and layers
 
112
        foreach( $svg as $element ) {
 
113
                if( $element -> getName() == "clipPath" || $element -> getName() == "defs" || $element -> getName() == "g" || $element -> getName() == "linearGradient" || $element -> getName() == "defs" || $element -> getName() == "radialGradient" ) {
 
114
                        if( $element -> getName() == "clipPath" || $element -> getName() == "defs" || $element -> getName() == "g" || $element -> getName() == "linearGradient" || $element -> getName() == "radialGradient" ) {
 
115
                                $graphnodes [$elementcounter] = $element;
 
116
                                $elementcounter++;
 
117
                        }
 
118
                        recurseelement($element);
 
119
                }else if( $element -> getName() == "use" || $element -> getName() == "radialGradient" || $element -> getName() == "linearGradient" || $element -> getName() == "polygon" || $element -> getName() == "line" || $element -> getName() == "polyline" || $element -> getName() == "path" || $element -> getName() == "rect" || $element -> getName() == "ellipse" || $element -> getName() == "circle" || $element -> getName() == "text") {
 
120
                        // Add element to queue
 
121
                        $graphnodes [$elementcounter] = $element;
 
122
                        $elementcounter++;
 
123
                }else{
 
124
                        echo "//Unknown element: ".$element -> getName()."\n";
 
125
                }
 
126
        }
 
127
        $defsmode=0;
 
128
        $defsstring="";
 
129
        $defsid="";
 
130
        $clipid="";
 
131
        // Process elements
 
132
        foreach( $graphnodes as $graphelement ) {
 
133
                // Clear Line Style and Fill Styles
 
134
                $fillstyle = "none";
 
135
                $linestyle = "none";    
 
136
                $opacity = "1.0";
 
137
                // For text element get 
 
138
                if( $graphelement -> getName() == "text" ) {
 
139
                        if( isset( $graphelement[0])) {
 
140
                                $textline = $graphelement[0];
 
141
                        }
 
142
                }
 
143
                // To get ID comment/code
 
144
                $attrs = $graphelement -> attributes();
 
145
                $xlinkattrs = $graphelement -> attributes( 'http://www.w3.org/1999/xlink' );
 
146
                // For use element get 
 
147
                if( $graphelement -> getName() == "use" ) {
 
148
                        if( isset( $attrs['id'] )) {
 
149
                                // We are in an id use statement
 
150
                                $clipid = strval( $attrs['id'] );
 
151
                        } else {
 
152
                                // We are in the reference use statement
 
153
                                $clippaths [$clipid] = substr( strval( $xlinkattrs ['href'] ),1);
 
154
                        }
 
155
                }
 
156
                if( $graphelement -> getName() == "linearGradient" ) {
 
157
                        if( isset( $attrs ['id'] )) {
 
158
                                $gradientname = $attrs ['id'];
 
159
                        }
 
160
                        if( !isset( $attrs['x1'] )) {
 
161
                                // Linear Gradient is not complete, this means that it is an inkscape element!
 
162
                                $isinkscape = true;
 
163
                        } else if( isset( $xlinkattrs ['href'] )) {
 
164
                                echo "var ".$gradientname." = c.createLinearGradient( ".$attrs['x1']." , ".$attrs['y1']." , ".$attrs['x2']." , ".$attrs['y2']." );\n";
 
165
                                // Now we create a new gradient with the following properties
 
166
                                $gradientref = $xlinkattrs ['href'];
 
167
                                $gradientref = substr( $gradientref, 1, strlen( $gradientref ) -1 );                            
 
168
                                if( isset( $colorstops ["$gradientref"] )){
 
169
                                        foreach( $colorstops ["$gradientref"] as $key => $value) {
 
170
                                                echo $gradientname.".addColorStop( ".$value." );\n";
 
171
                                        }
 
172
                                }                                                                                                               
 
173
                        } else {
 
174
                                echo " var ".$gradientname." = c.createLinearGradient( ".$attrs['x1']." , ".$attrs['y1']." , ".$attrs['x2']." , ".$attrs['y2']." );\n";
 
175
                        }
 
176
                } else if( $graphelement -> getName() == "radialGradient" ) {
 
177
                        if( isset( $attrs ['id'] )) {
 
178
                                $gradientname = $attrs ['id'];
 
179
                        }
 
180
                        if( !isset( $attrs ['cx'])) {
 
181
                                // Radial Gradient is not complete, this means that it is an inkscape element!
 
182
                                $isinkscape=true;
 
183
                        } else if( isset( $xlinkattrs ['href'])) {
 
184
                                echo " var ".$gradientname." = c.createRadialGradient( ".$attrs['cx']." , ".$attrs['cy']." , 0 , ".$attrs['cx']." , ".$attrs['cy']." , ".$attrs['r']." );\n";
 
185
                                // Now we create a new gradient with the following properties
 
186
                                $gradientref = $xlinkattrs ['href'];
 
187
                                $gradientref = substr( $gradientref, 1, strlen( $gradientref )-1);
 
188
                                if( isset( $colorstops ["$gradientref"])) {
 
189
                                        foreach( $colorstops ["$gradientref"] as $key => $value) {
 
190
                                                echo $gradientname.".addColorStop( ".$value." );\n";
 
191
                                        }
 
192
                                }                                                                                               
 
193
                        } else {
 
194
                                echo " var ".$gradientname." = c.createRadialGradient( ".$attrs['cx']." , ".$attrs['cy']." , 0, ".$attrs['cx']." , ".$attrs['cy']." , ".$attrs['r']." );\n";
 
195
                        }
 
196
                } else if( $graphelement -> getName() == "stop" ) {
 
197
                        $stopcolor = $attrs ['style'];
 
198
                        if( strpos( $stopcolor, ";" , 11) !== false ) {
 
199
                                $stopcolorend = strpos( $stopcolor, ";" , 11);
 
200
                        } else {
 
201
                                $stopcolorend = strlen( $stopcolor );
 
202
                        }
 
203
                        $stopcolor = substr( $stopcolor, 11, $stopcolorend - 11 );
 
204
                        if( $isinkscape ) {
 
205
                                if( isset( $colorstops ["$gradientname"] )) {
 
206
                                        array_push( $colorstops ["$gradientname"], $attrs['offset'].", '".$stopcolor."' " );
 
207
                                } else {
 
208
                                        $poo=array();
 
209
                                        array_push( $poo, $attrs ['offset'].", '".$stopcolor."' " );
 
210
                                        $colorstops ["$gradientname"] = $poo;
 
211
                                }
 
212
                        } else {
 
213
                                echo $gradientname.".addColorStop( ".$attrs['offset'].", '".$stopcolor."' );\n";
 
214
                        }
 
215
                } else {
 
216
                        // We assume that defsid is the only use of ID
 
217
                        if( isset( $attrs ['id'] )) $defsid = $attrs ['id'];
 
218
                }
 
219
                // For each attribute of svg                                                            
 
220
                // We process attributes after gradients but before the drawing elements.
 
221
                foreach( $graphelement -> attributes() as $key => $val ) {
 
222
                        // Get font parameters!
 
223
                        if( $key == "if" && $graphelement -> getName() == "text" ) {
 
224
                                $fontline = $val;
 
225
                        }
 
226
                        // Get font parameters!
 
227
                        if( $key == "font-size" && $graphelement -> getName() == "text" ) {
 
228
                                $fontline = $val;
 
229
                        }                       
 
230
                        if( $key == "font-family" && $graphelement -> getName() == "text" ) {
 
231
                                $fontfamily = $val;
 
232
                                $fontfamily = str_replace( "'", "", $fontfamily );
 
233
                                $fontstyle = "";
 
234
                                if( !( strpos( $fontfamily, "-Oblique" ) === FALSE )) {
 
235
                                        $fontfamily = str_replace( "-Oblique", "", $fontfamily );
 
236
                                        $fontstyle = "Bold";
 
237
                                }
 
238
                                if( !( strpos( $fontfamily, "-Bold" ) === FALSE )) {
 
239
                                        $fontfamily = str_replace( "-Bold", "", $fontfamily );
 
240
                                        $fontstyle = "Bold";
 
241
                                }
 
242
                                if( !( strpos( $fontfamily, "-Italic" ) === FALSE )) {
 
243
                                        $fontfamily = str_replace( "-Italic", "", $fontfamily );
 
244
                                        $fontstyle = "Italic";
 
245
                                }
 
246
                                if( !( strpos( $fontfamily, "Bold" ) === FALSE )) {
 
247
                                        $fontfamily = str_replace( "Bold", "", $fontfamily );
 
248
                                        $fontstyle = $fontstyle."Bold";                                                 
 
249
                                }
 
250
                                if( !( strpos( $fontfamily, "Italic" ) === FALSE )) {
 
251
                                        $fontfamily = str_replace( "Italic", "", $fontfamily );
 
252
                                        $fontstyle = $fontstyle."Italic";
 
253
                                }
 
254
                        }
 
255
                        if( $graphelement -> getName() == "text" ) {
 
256
                                if( $key == "x" ) {
 
257
                                $textx = $val;
 
258
                                }
 
259
                                if( $key == "y" ) {
 
260
                                $texty = $val;
 
261
                                }
 
262
                        }
 
263
                        if( $graphelement -> getName() == "rect" ) {
 
264
                                if( $key == "x" ) {
 
265
                                $linex1 = $val;
 
266
                                }
 
267
                                if( $key == "y" ) {
 
268
                                $liney1 = $val;
 
269
                                }
 
270
                                if( $key == "width" ) {
 
271
                                $linex2 = $val;
 
272
                                }
 
273
                                if( $key == "height" ) {
 
274
                                $liney2 = $val;
 
275
                                }
 
276
                        }
 
277
                        if( $graphelement -> getName() == "ellipse" || $graphelement -> getName() == "circle" ) {
 
278
                                // Element is either a circle or an ellipse
 
279
                                if( $key == "cx" ) {
 
280
                                $cx = $val;
 
281
                                }
 
282
                                if( $key == "cy" ) {
 
283
                                $cy = $val;
 
284
                                }
 
285
                                if( $key == "r" ) {
 
286
                                        $rx = $val;
 
287
                                        $ry = $val;
 
288
                                }
 
289
                                if( $key == "rx" ) {
 
290
                                $rx = $val;
 
291
                                }
 
292
                                if( $key == "ry" ) {
 
293
                                $ry = $val;
 
294
                                }
 
295
                        }
 
296
                        if(( $key == "x1" || $key == "y1" || $key == "x2" || $key == "y2" ) && $graphelement -> getName() == "line" ) {
 
297
                                // Element is a line element with 4 coordinate parameters
 
298
                                if( $key == "x1" ) {
 
299
                                $linex1 = $val;
 
300
                                }
 
301
                                if( $key == "x2" ) {
 
302
                                $linex2 = $val;
 
303
                                }
 
304
                                if( $key == "y1" ) {
 
305
                                $liney1 = $val;
 
306
                                }
 
307
                                if( $key == "y2" ) {
 
308
                                $liney2 = $val;
 
309
                                }
 
310
                        } else if( $key == "transform" && $graphelement -> getName() == "text" ) {
 
311
                                $j = 0;
 
312
                                $dostr = "";
 
313
                                $params = array();
 
314
                                $noparams = 0;
 
315
                                $workstr = $val;
 
316
                                do{
 
317
                                        $dochr = substr( $workstr, $j, 1 );
 
318
                                        if( $dochr == " " || $dochr == "( " || $dochr == " ) ") {
 
319
                                                if( trim( $dostr ) != "" && $dostr != "matrix" ) {
 
320
                                                        $params [$noparams++] = $dostr;
 
321
                                                }
 
322
                                                if( $dochr == "-" ) {
 
323
                                                        $dostr = $dochr;
 
324
                                                } else {
 
325
                                                        $dostr = "";
 
326
                                                }
 
327
                                        } else {
 
328
                                                $dostr. = $dochr;
 
329
                                        }
 
330
                                        $j++;
 
331
                                }while( $j <= strlen( $workstr ));
 
332
                                if( trim( $dostr ) != "" ) {
 
333
                                        $params [$noparams++] = $dostr;
 
334
                                }
 
335
                                $translate = "c.translate( ".$params[4].", ".$params[5]." );\n";
 
336
                                $rotate = "c.rotate( ".$params[1]." , ".$params[2]." );\n";
 
337
                                $scale = "c.scale( ".$params[0]." , ".$params[3]." );\n";
 
338
                        } else if( $key == "stroke" ) {
 
339
                                echo "" .'c.strokeStyle = "' . $val . '";' . "\n";
 
340
                                $linestyle = $val;
 
341
                        } else if( $key == "opacity" ) {
 
342
                                $opacity = $val;
 
343
                        } else if( $key == "fill" ) {
 
344
                                if( $val != "none" ) {
 
345
                                        if( strpos( $val, "url( ") === false) {
 
346
                                                echo "" .'c.fillStyle = "' . $val. '";' . "\n";
 
347
                                        } else {
 
348
                                                echo "c.fillStyle = ".substr( $val, 5, strlen( $val ) - 6 ).";\n";
 
349
                                        }
 
350
                                }
 
351
                                $fillstyle=$val;
 
352
                        } else if ($key == "style" ) {
 
353
                                $fillpos = strpos( $val, "fill:" );
 
354
                                if( $fillpos !== false ) {
 
355
                                        $fillposend = strpos( $val, ";", $fillpos);
 
356
                                        if( $fillposend === false ) {
 
357
                                                $fillposend = strlen( $val );
 
358
                                        }
 
359
                                        $fillstyle = substr( $val, $fillpos + 5, $fillposend - $fillpos - 5 );
 
360
                                        if( $fillstyle != "none" ) {
 
361
                                                if(strpos($fillstyle,"url(")===false){
 
362
                                                        echo "c.fillStyle = '".$fillstyle ."';\n";
 
363
                                                } else {
 
364
                                                        echo "c.fillStyle = ".substr( $fillstyle, 5, strlen( $fillstyle ) - 6 ).";\n";
 
365
                                                }
 
366
                                        }       
 
367
                                }
 
368
                                $strokepos = strpos( $val, "stroke:" );
 
369
                                if( $strokepos !== false ) {
 
370
                                        $strokeposend = strpos( $val, ";", $strokepos );
 
371
                                        if( $strokeposend === false ) {
 
372
                                                $strokeposend = strlen( $val );
 
373
                                        }
 
374
                                        $linestyle = substr( $val, $strokepos + 7, $strokeposend - $strokepos - 7 );
 
375
                                        if( $linestyle != "none" ) {
 
376
                                                echo "" .'c.strokeStyle = "' . $linestyle . '";' . "\n";
 
377
                                        }
 
378
                                }
 
379
                                $strokepos = strpos( $val, "stroke-width:" );
 
380
                                if( $strokepos !== false ) {
 
381
                                        $strokeposend = strpos( $val, ";", $strokepos );
 
382
                                        if( $strokeposend === false ) {
 
383
                                                $strokeposend = strlen( $val );
 
384
                                        }
 
385
                                        $strokewidth = substr( $val, $strokepos + 13, $strokeposend - $strokepos - 13 );
 
386
                                        $strokewidth = str_replace( "px", "", $strokewidth );
 
387
                                        echo "\n" . 'c.lineWidth = "' . $strokewidth . '";' . "\n";
 
388
                                }
 
389
                                $fontsizepos = strpos( $val, "font-size:" );
 
390
                                if( $fontsizepos !== false ) {
 
391
                                        $fontsizeend = strpos( $val, ";", $fontsizepos);
 
392
                                        if( $fontsizeend === false ) {
 
393
                                                $fontsizeend = strlen( $val );
 
394
                                        }
 
395
                                        $fontline = substr( $val, $fontsizepos + 10, $fontsizeend - $fontsizepos - 10 );
 
396
                                        $fontline = str_replace( "px", "", $fontline);
 
397
                                }
 
398
                                $fontfamilypos = strpos( $val, "font-family:" );
 
399
                                if( $fontfamilypos !== false ) {
 
400
                                        $fontfamilyend = strpos( $val, ";", $fontfamilypos);
 
401
                                        if( $fontfamilyend === false ) {
 
402
                                                $fontfamilyend=strlen($val);
 
403
                                        }
 
404
                                        $fontfamily = substr( $val, $fontfamilypos + 12, $fontfamilyend - $fontfamilypos - 12 );
 
405
                                }
 
406
                                $fontstylepos = strpos( $val, "font-style:" );
 
407
                                if( $fontstylepos !== false ) {
 
408
                                        $fontstyleend = strpos( $val, ";", $fontstylepos );
 
409
                                        if( $fontstyleend === false ) {
 
410
                                                $fontstyleend=strlen($val);
 
411
                                        }
 
412
                                        $fontstyle = substr( $val, $fontstylepos + 11, $fontstyleend - $fontstylepos - 11 );
 
413
                                }
 
414
                        } else if( $key == "stroke-width" ) {
 
415
                                echo "\n" . 'c.lineWidth = "' . $val . '";' . "\n";
 
416
                        } else if( $key == "points" &&( $graphelement -> getName() == "polygon" || $graphelement -> getName() == "polyline" || $graphelement -> getName() == "line" )) {
 
417
                                if( $defsmode ) {
 
418
                                        $defsstring. = "c.beginPath();\n";
 
419
                                } else {
 
420
                                        echo "c.beginPath();\n";
 
421
                                }
 
422
                                // dostr loop for polygons. Bugfix: if old char is e, then we do not break string at -                                                  
 
423
                                $j = 0;
 
424
                                $dostr = "";
 
425
                                $dochr = "";
 
426
                                $params = array();
 
427
                                $noparams = 0;
 
428
                                $workstr = $val;
 
429
                                $oldchr = "";
 
430
                                do{
 
431
                                        $oldchr = $dostr;
 
432
                                        $dochr = substr( $workstr, $j, 1 );
 
433
                                        if( ( $dochr == "-" && $oldchr != "e" && $oldchr != "E" ) || $dochr == "," || $dochr == " " ) {
 
434
                                                if( trim( $dostr ) != "" ) {
 
435
                                                        $params [$noparams++] = $dostr;
 
436
                                                }
 
437
                                                if( $dochr == "-" ) {
 
438
                                                        $dostr = $dochr;
 
439
                                                } else {
 
440
                                                        $dostr = "";
 
441
                                                }
 
442
                                        } else {
 
443
                                                $dostr. = $dochr;
 
444
                                        }
 
445
                                        $j++;
 
446
                                        } while( $j <= strlen( $workstr ));
 
447
                                        if( trim( $dostr ) != "") {
 
448
                                                $params [$noparams++] = $dostr;
 
449
                                        }
 
450
                                        for( $j = 0; $j < $noparams; $j += 2) {
 
451
                                                if( $j == 0 ) {
 
452
                                                        if( $defsmode ) {
 
453
                                                                $defsstring. = "c.moveTo( ".$params [$j].",".$params [$j+1].");\n";
 
454
                                                        } else {
 
455
                                                                echo "c.moveTo( ".$params[$j].",".$params[$j+1].");\n";                                                 
 
456
                                                        }
 
457
                                                } else {
 
458
                                                        if( $defsmode ) {
 
459
                                                                $defsstring. = "c.lineTo( ".$params[$j].",".$params[$j+1].");\n";
 
460
                                                        } else {
 
461
                                                                echo "c.lineTo( ".$params[$j].",".$params[$j+1].");\n";
 
462
                                                        }
 
463
                                                }
 
464
                                        }
 
465
                                        // If a polygon close path if not i.e. polyline keep it open
 
466
                                        if( $noparams >= 2 && $graphelement -> getName() == "polygon" ) {
 
467
                                                if( $defsmode ) {
 
468
                                                        $defsstring. = "c.lineTo( ".$params[0].",".$params[1]." );\n";
 
469
                                                } else {
 
470
                                                        echo "c.lineTo( ".$params[0].",".$params[1]." );\n";
 
471
                                                }
 
472
                                        }
 
473
                                        if(!$defsmode){
 
474
                                                echo "c.globalAlpha = $opacity;\n";
 
475
                                        }
 
476
                                        if(     $fillstyle      ==      "none"  &&      $linestyle      ==      "none"  ) {
 
477
                                                if(     !$defsmode) {
 
478
                                                        echo 'c.fillStyle = "#000";';
 
479
                                                        echo "\n";
 
480
                                                        echo "c.fill();\n\n";
 
481
                                                }
 
482
                                        }
 
483
                                        if( $fillstyle != "none" ) {
 
484
                                                if( !$defsmode ) {
 
485
                                                        echo "c.fill();\n";
 
486
                                                        if( $linestyle == "none" ) {
 
487
                                                                echo "\n";
 
488
                                                        }
 
489
                                                }
 
490
                                        }
 
491
                                        if( $linestyle != "none" ) {
 
492
                                                if( !$defsmode ) {
 
493
                                                        echo "c.stroke();\n\n";
 
494
                                                }
 
495
                                        }
 
496
                                        if( $defsmode ) {
 
497
                                                $defsstring. = "c.clip();\n\n";
 
498
                                        }
 
499
                        } else if ( $key == "d" ) {
 
500
                                $dval=$val;
 
501
                        }
 
502
                }
 
503
                // Draw d line commands. This is a fix for the bug that requires the line settings to be assigned before the line commands
 
504
                if( isset( $dval )) {
 
505
                        echo "c.beginPath();\n";
 
506
                        $i = 0;
 
507
                        $str = $dval;
 
508
                        $workstr = "";
 
509
                        $command = "";
 
510
                        $cx = 0;
 
511
                        $cy = 0;
 
512
                        $firstpoint = 0;
 
513
                        $firstpointx = 0;
 
514
                        $firstpointy = 0;
 
515
                        $lastpointx = 0;
 
516
                        $lastpointy = 0;
 
517
                        do{
 
518
                                $chr = substr( $str, $i, 1 );
 
519
                                if( $chr == "H" || $chr == "h" || $chr == "V" || $chr == "v" || $chr == "M" || $chr == "m" || $chr == "C" || $chr == "c" || $chr == "L" || $chr == "A" || $chr == "a" || $chr == "l" || $chr == "z" || $chr == "Z" || $chr == "s" || $chr == "S" || $i == strlen( $str )) {
 
520
                                        // Process Parameters for any parameter command
 
521
                                        if( $command == "M" || $command == "m" || $command == "c" || $command == "C" || $command == "v" || $command == "V" || $command == "h" || $command == "H" || $command == "s" || $command == "S" || $command == "l" || $command == "L" || $command == "a" || $command == "A" ) {
 
522
                                                $j = 0;
 
523
                                                $dostr = "";
 
524
                                                $dochr = "";
 
525
                                                $oldchr = "";
 
526
                                                $params = array();
 
527
                                                $noparams = 0;
 
528
                                                // dochr loop for other drawing commands. Bug fix: If we encounter - and before it is an e we do nothing, otherwise proceed as normal
 
529
                                                do{
 
530
                                                        $oldchr=$dochr;
 
531
                                                        $dochr=substr($workstr,$j,1);
 
532
                                                                if(($dochr=="-" && $oldchr!="e" && $oldchr!="E")||$dochr==","||$dochr==" "){
 
533
                                                                        if(trim($dostr)!=""){
 
534
                                                                                $params[$noparams++]=$dostr;
 
535
                                                                        }
 
536
                                                                        if($dochr=="-"){
 
537
                                                                                $dostr=$dochr;
 
538
                                                                        } else {
 
539
                                                                                $dostr="";
 
540
                                                                        }
 
541
                                                                } else {
 
542
                                                                        $dostr.=$dochr;
 
543
                                                                }
 
544
                                                                $j++;
 
545
                                                } while( $j <= strlen( $workstr ));
 
546
                                                if( trim( $dostr ) != "" ) {
 
547
                                                        $params [$noparams++] = $dostr;
 
548
                                                }
 
549
                                        }
 
550
                                        if( $command == "M" || $command == "m" ) {
 
551
                                                for( $j = 0; $j < $noparams; $j += 2 ) {
 
552
                                                        if( $j == 0 ) {
 
553
                                                                // Normal moveto set cx,cy
 
554
                                                                if( $command == "M" ) {
 
555
                                                                        $cx = $params[$j];
 
556
                                                                        $cy = $params[$j+1];
 
557
                                                                        echo "c.moveTo(".$cx.",".$cy.");\n";
 
558
                                                                        $firstpoint = 1;
 
559
                                                                        $firstpointx = $cx;
 
560
                                                                        $firstpointy = $cy;
 
561
                                                                } else if( $command == "m" ) {  
 
562
                                                                        if( !$firstpoint ) {
 
563
                                                                                $cx = $params[$j];
 
564
                                                                                $cy=$params[$j+1];
 
565
                                                                                echo "c.moveTo(".$cx.",".$cy.");\n";
 
566
                                                                                $firstpoint = 1;
 
567
                                                                                $firstpointx = $cx;
 
568
                                                                                $firstpointy = $cy;
 
569
                                                                        } else {
 
570
                                                                                $cx += $params[$j];
 
571
                                                                                $cy += $params[$j+1];
 
572
                                                                                echo "c.moveTo( ".$cx.",".$cy." );\n";
 
573
                                                                        }
 
574
                                                                }
 
575
                                                        } else {
 
576
                                                                // Implicit lineto
 
577
                                                                if( $command == "M" ) {
 
578
                                                                        $cx = $params[$j];
 
579
                                                                        $cy = $params[$j+1];
 
580
                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";
 
581
                                                                } else if( $command == "m" ) {
 
582
                                                                        $cx += $params[$j];
 
583
                                                                        $cy += $params[$j+1];
 
584
                                                                        echo "c.lineTo(".$cx.",".$cy.");\n";
 
585
                                                                }
 
586
                                                        }
 
587
                                                }
 
588
                                        } else if( $command == "C" || $command == "c" ) {
 
589
                                                // Bezier Curveto
 
590
                                                for( $j = 0; $j < $noparams; $j += 6 ) {
 
591
                                                        if( $command == "C" ) {
 
592
                                                                $p1x = $params[$j];
 
593
                                                                $p1y = $params[$j+1];
 
594
                                                                $p2x = $params[$j+2];
 
595
                                                                $p2y = $params[$j+3];
 
596
                                                                $cx = $params[$j+4];
 
597
                                                                $cy = $params[$j+5];
 
598
                                                                $lastpointx = $p2x;
 
599
                                                                $lastpointy = $p2y;
 
600
                                                                echo "c.bezierCurveTo( ".$p1x.",".$p1y.",".$p2x.",".$p2y.",".$cx.",".$cy.");\n";
 
601
                                                                // Curveto absolute set cx to final point, other coordinates are control points
 
602
                                                        } else if( $command == "c" ) {
 
603
                                                                // Curveto relative set cx to final point, other coordinates are relative control points
 
604
                                                                $p1x=$cx+$params[$j];
 
605
                                                                $p1y=$cy+$params[$j+1];
 
606
                                                                $p2x=$cx+$params[$j+2];
 
607
                                                                $p2y=$cy+$params[$j+3];
 
608
                                                                $lastpointx=$p2x;
 
609
                                                                $lastpointy=$p2y;
 
610
                                                                $cx+=$params[$j+4];
 
611
                                                                $cy+=$params[$j+5];
 
612
                                                                echo "c.bezierCurveTo(".$p1x.",".$p1y.",".$p2x.",".$p2y.",".$cx.",".$cy.");\n";
 
613
                                                        }
 
614
                                                }
 
615
                                        } else if( $command == "S" || $command == "s" ) {
 
616
                                                //context . quadraticCurveTo(cpx, cpy, x, y)
 
617
                                                // Quadratic Curveto
 
618
                                                for( $j = 0; $j < $noparams; $j += 4 ) {
 
619
                                                        if( $command == "S" ) {
 
620
                                                                $p1x=$params[$j];
 
621
                                                                $p1y=$params[$j+1];
 
622
                                                                $cx=$params[$j+2];
 
623
                                                                $cy=$params[$j+3];
 
624
                                                                $lastpointx=$p1x;
 
625
                                                                $lastpointy=$p1y;
 
626
                                                                echo "c.bezierCurveTo(".$lastpointx.",".$lastpointy.",".$p1x.",".$p1y.",".$cx.",".$cy.");\n";
 
627
                                                                // Curveto absolute set cx to final point, other coordinates are control points
 
628
                                                        } else if( $command == "s" ) {
 
629
                                                                // Curveto relative set cx to final point, other coordinates are relative control points
 
630
                                                                $p1x=$cx+$params[$j];
 
631
                                                                $p1y=$cy+$params[$j+1];
 
632
                                                                $cx+=$params[$j+2];
 
633
                                                                $cy+=$params[$j+3];
 
634
                                                                $lastpointx=$p1x;
 
635
                                                                $lastpointy=$p1y;
 
636
                                                                echo "c.bezierCurveTo(".$lastpointx.",".$lastpointy.",".$p1x.",".$p1y.",".$cx.",".$cy.");\n";
 
637
                                                        }
 
638
                                                }
 
639
                                        } else if( $command == "V" || $command == "v" ) {
 
640
                                                // Vertical Lineto
 
641
                                                for($j=0;$j<$noparams;$j++){
 
642
                                                        if($command=="V"){
 
643
                                                                $cy=$params[$j];
 
644
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
645
                                                        } else if( $command == "v" ) {
 
646
                                                                // Curveto relative set cx to final point, other coordinates are relative control points
 
647
                                                                $cy += $params[$j];
 
648
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
649
                                                        }
 
650
                                                }
 
651
                                        } else if( $command == "H" || $command == "h" ) {
 
652
                                                // horizontal Lineto
 
653
                                                for($j=0;$j<$noparams;$j++){
 
654
                                                        if($command=="H"){
 
655
                                                                $cx=$params[$j];
 
656
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
657
                                                        } else if( $command == "h" ) {
 
658
                                                                // Curveto relative set cx to final point, other coordinates are relative control points
 
659
                                                                $cx+=$params[$j];
 
660
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
661
                                                        }
 
662
                                                }
 
663
                                        } else if( $command == "L" || $command == "l" ) {
 
664
                                                // Lineto
 
665
                                                for($j=0;$j<$noparams;$j+=2){
 
666
                                                        if($command=="L"){
 
667
                                                                $cx=$params[$j];
 
668
                                                                $cy=$params[$j+1];
 
669
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
670
                                                        } else if( $command == "l" ) {
 
671
                                                                // Curveto relative set cx to final point, other coordinates are relative control points
 
672
                                                                $cx+=$params[$j];
 
673
                                                                $cy+=$params[$j+1];
 
674
                                                                echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
675
                                                        }
 
676
                                                }
 
677
                                        } else if( $command == "Z" || $command == "z" ) {
 
678
                                                echo "c.lineTo(".$firstpointx.",".$firstpointy.");\n";                                  
 
679
                                        } else if( $command == "A" || $command == "a" ) {
 
680
                                                // To avoid hard math - draw arc as a line
 
681
                                                for( $j = 0; $j < $noparams; $j += 7 ) {
 
682
                                                        $rx = $params[0];
 
683
                                                        $ry = $params[1];
 
684
                                                        $ang = $params[2];
 
685
                                                        $largeflag = $params[3];
 
686
                                                        $sweepflag = $params[4];
 
687
                                                        $cx = $params[$j];
 
688
                                                        $cy = $params[$j+1];
 
689
                                                        echo "c.lineTo(".$cx.",".$cy.");\n";                            
 
690
                                                }
 
691
                                        }
 
692
                                        // Init new command!
 
693
                                        $command=$chr;
 
694
                                        $workstr="";
 
695
                                } else {
 
696
                                        $workstr.=$chr;
 
697
                                }
 
698
                                $i++;
 
699
                        } while( $i <= strlen( $str ));
 
700
                                echo "c.globalAlpha = $opacity;\n";
 
701
                                if(     $fillstyle      ==      "none"  &&      $linestyle      ==      "none" ) {
 
702
                                        echo 'c.fillStyle = "#000";';
 
703
                                        echo "\n";
 
704
                                        echo "c.fill();\n\n";
 
705
                                }
 
706
                                if( $fillstyle != "none" ) {
 
707
//                                      echo "c.save();\n\n";
 
708
//                                      echo "c.scale(2.5,2.5);\n";                                                             
 
709
                                        echo "c.fill();\n";
 
710
//                                      echo "c.restore();\n\n";
 
711
                                        if( $linestyle == "none" ) {
819
712
                                                echo "\n";
820
 
 
821
 
                          }elseif($graphelement->getName()=="circle"||$graphelement->getName()=="ellipse"){
822
 
                                                echo "c.globalAlpha = $opacity;\n";                             
823
 
                                
824
 
                                                echo "c.beginPath();\n";
825
 
                                                
826
 
                                                $xs=$cx-$rx;
827
 
                                                $xe=$cx+$rx;
828
 
                                                $ys=$cy-$ry;
829
 
                                                $ye=$cy+$ry;
830
 
 
831
 
                                                $xsp=$cx-($rx*0.552);
832
 
                                                $xep=$cx+($rx*0.552);
833
 
                                                $ysp=$cy-($ry*0.552);
834
 
                                                $yep=$cy+($ry*0.552);
835
 
                                                
836
 
                                                echo "c.moveTo(".$cx.",".$ys.");\n";                                            
837
 
                                        echo "c.bezierCurveTo(".$xsp.",".$ys.",".$xs.",".$ysp.",".$xs.",".$cy.");\n";
838
 
                                        echo "c.bezierCurveTo(".$xs.",".$yep.",".$xsp.",".$ye.",".$cx.",".$ye.");\n";
839
 
                                        echo "c.bezierCurveTo(".$xep.",".$ye.",".$xe.",".$yep.",".$xe.",".$cy.");\n";
840
 
                                        echo "c.bezierCurveTo(".$xe.",".$ysp.",".$xep.",".$ys.",".$cx.",".$ys.");\n";
841
 
 
842
 
                                    if($fillstyle!="none"){
843
 
                                        echo "c.fill();\n";
844
 
                                                if($linestyle=="none") echo "\n";
845
 
                                }
846
 
                                    if($linestyle!="none"){
847
 
                                        echo "c.stroke();\n";
848
 
                                        echo "\n";
849
 
                             }
850
 
                                }elseif($graphelement->getName()=="line"){
851
 
                                                echo "context.beginPath();\n";
852
 
                                                echo "context.moveTo(".$linex1.",".$liney1.");\n";
853
 
                                                echo "context.lineTo(".$linex1.",".$liney1.");\n";
854
 
                                                echo "context.stroke();\n";
855
 
 
856
 
                                }elseif($graphelement->getName()=="g"){
857
 
                                                        // We only print groups that have an ID
858
 
//                                                      if(isset($attrs['id'])){
859
 
                                                                        echo "//-------------------------------\n";
860
 
                                                                        echo "// Group: ".$attrs['id']."\n";
861
 
                                                                        echo "//-------------------------------\n";
862
 
//                                                      }                                                       
863
 
                                }elseif($graphelement->getName()=="eg"){
864
 
                                                        // We only print groups that have an ID
865
 
                                                        if(isset($attrs['id'])){
866
 
                                                                        echo "//-------------------------------\n";
867
 
                                                                        echo "// GroupEnd: ".$attrs['id']."\n";
868
 
                                                                        echo "//-------------------------------\n";
869
 
                                                                        echo "\n\n\n";
870
 
                                                        }                                                       
871
 
                                }elseif($graphelement->getName()=="defs"){
872
 
                                                                        $defsmode=1;
873
 
                                                                        $defsstring="";
874
 
                                }elseif($graphelement->getName()=="defsend"){
875
 
                                                                        if($defsid!=""){
876
 
                                                                                        $clipdefs[strval($defsid)]=$defsstring;
877
 
                                                                        }
878
 
                                                                        $defsmode=0;
879
 
                        }
880
 
 
881
 
                }
 
713
                                        }
 
714
                                }
 
715
                        if(     $linestyle != "none" ) {
 
716
                                echo "c.stroke();\n";
 
717
                                echo "\n";
 
718
                        }
 
719
                        unset($dval);
 
720
                }
 
721
                if( $graphelement -> getName() == "text" ) {
 
722
                        echo "c.globalAlpha = $opacity;\n";
 
723
                        echo "c.beginPath();\n";
 
724
                        echo "c.save();\n";
 
725
                        echo "c.font = '".$fontstyle." ".$fontline."px ".$fontfamily."';\n";
 
726
                        if( isset( $translate ) && isset( $rotate ) && isset( $scale )) {
 
727
                                echo $translate;
 
728
                                echo $rotate;
 
729
                                echo $scale;
 
730
                        }
 
731
                        if( isset( $textx ) && isset( $texty )) {
 
732
                                echo "c.fillText( '".$textline."',".$textx.",".$texty." );\n";
 
733
                        } else {
 
734
                                echo "c.fillText('".$textline."',0,0);\n";
 
735
                        }
 
736
                        if( $linestyle != "none" ) {
 
737
                                if( isset( $textx ) && isset( $texty )) {
 
738
                                        echo "c.strokeText('".$textline."',".$textx.",".$texty.");\n";
 
739
                                } else {
 
740
                                        echo "c.strokeText('".$textline."',0,0);\n";
 
741
                                }
 
742
                        }
 
743
                        echo "c.restore();\n\n";
 
744
                } else if( $graphelement -> getName() == "rect" ) {
 
745
                        // This rectangle must be clipped!
 
746
                        if( isset( $attrs['clip-path'] )) {
 
747
                                $clipid = substr( $attrs['clip-path'], 5);
 
748
                                $clipid = substr( $clipid, 0, strlen( $clipid ) - 1 );
 
749
                                $clipid = $clippaths [$clipid];
 
750
                                echo "c.save();\n";
 
751
                                echo $clipdefs[strval($clipid)];
 
752
                        }
 
753
                        echo "c.globalAlpha = $opacity;\n";
 
754
                        echo "c.beginPath();\n";
 
755
                        echo "c.moveTo(".$linex1.",".$liney1.");\n";
 
756
                        echo "c.lineTo(".($linex1+$linex2).",".($liney1).");\n";
 
757
                        echo "c.lineTo(".($linex1+$linex2).",".($liney1+$liney2).");\n";
 
758
                        echo "c.lineTo(".($linex1).",".($liney1+$liney2).");\n";                        
 
759
                        echo "c.lineTo(".($linex1).",".($liney1).");\n";                        
 
760
                        if( $fillstyle != "none" ) {
 
761
//                              echo "c.save();\n\n";
 
762
//                              echo "c.scale(1,1);\n";                                                         
 
763
                                echo "c.fill();\n";
 
764
//                              echo "c.restore();\n\n";
 
765
                                if( $linestyle == "none" ) echo "\n";
 
766
                        }
 
767
                        if( $linestyle != "none" ) {
 
768
                                echo "c.stroke();\n";
 
769
                        }
 
770
                        // This rectangle must be clipped!
 
771
                        if( isset( $attrs ['clip-path'] )) {
 
772
                                echo "c.restore();\n";
 
773
                        }       
 
774
                        echo "\n";
 
775
                } else if( $graphelement -> getName() == "circle" || $graphelement -> getName() == "ellipse" ) {
 
776
                        echo "c.globalAlpha = $opacity;\n";
 
777
                        echo "c.beginPath();\n";
 
778
                        $xs = $cx - $rx;
 
779
                        $xe = $cx + $rx;
 
780
                        $ys = $cy - $ry;
 
781
                        $ye = $cy + $ry;
 
782
                        $xsp = $cx - ( $rx*0.552 );
 
783
                        $xep = $cx + ( $rx*0.552 );
 
784
                        $ysp = $cy - ( $ry*0.552 );
 
785
                        $yep = $cy + ( $ry*0.552 );
 
786
                        echo "c.moveTo(".$cx.",".$ys.");\n";
 
787
                        echo "c.bezierCurveTo(".$xsp.",".$ys.",".$xs.",".$ysp.",".$xs.",".$cy.");\n";
 
788
                        echo "c.bezierCurveTo(".$xs.",".$yep.",".$xsp.",".$ye.",".$cx.",".$ye.");\n";
 
789
                        echo "c.bezierCurveTo(".$xep.",".$ye.",".$xe.",".$yep.",".$xe.",".$cy.");\n";
 
790
                        echo "c.bezierCurveTo(".$xe.",".$ysp.",".$xep.",".$ys.",".$cx.",".$ys.");\n";
 
791
                        if( $fillstyle != "none" ) {
 
792
                                echo "c.fill();\n";
 
793
                                if( $linestyle == "none" ) echo "\n";
 
794
                        }
 
795
                        if( $linestyle != "none" ) {
 
796
                                echo "c.stroke();\n";
 
797
                                echo "\n";
 
798
                        }
 
799
                } else if( $graphelement -> getName() == "line" ) {
 
800
                        echo "context.beginPath();\n";
 
801
                        echo "context.moveTo(".$linex1.",".$liney1.");\n";
 
802
                        echo "context.lineTo(".$linex1.",".$liney1.");\n";
 
803
                        echo "context.stroke();\n";
 
804
                } else if( $graphelement -> getName() == "g" ) {
 
805
                        // We only print groups that have an ID
 
806
//                      if(isset($attrs['id'])){
 
807
                        echo "//-------------------------------\n";
 
808
                        echo "// Group: ".$attrs['id']."\n";
 
809
                        echo "//-------------------------------\n";
 
810
//                      }                                                       
 
811
                } else if( $graphelement -> getName() == "eg" ) {
 
812
                        // We only print groups that have an ID
 
813
                        if( isset( $attrs['id'] )) {
 
814
                                echo "//-------------------------------\n";
 
815
                                echo "// GroupEnd: ".$attrs['id']."\n";
 
816
                                echo "//-------------------------------\n";
 
817
                                echo "\n\n\n";
 
818
                        }
 
819
                } else if( $graphelement -> getName() == "defs" ) {
 
820
                        $defsmode = 1;
 
821
                        $defsstring = "";
 
822
                } else if( $graphelement -> getName() == "defsend" ) {
 
823
                        if( $defsid != "" ) {
 
824
                                $clipdefs[strval($defsid)]=$defsstring;
 
825
                        }
 
826
                $defsmode = 0;
 
827
                }
 
828
        }
882
829
}
883
 
 
884
 
 
885
 
?>
 
830
?> 
 
 
b'\\ No newline at end of file'