/lenasys/trunk

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