| [ Index ] |
|
Code source de Horde 3.1.3 |
1 <?php 2 /** 3 * XML_SVG 4 * 5 * Wrapper class that provides some examples and a few convenience 6 * methods. 7 * 8 * $Horde: framework/XML_SVG/SVG.php,v 1.17.10.4 2006/01/01 21:28:44 jan Exp $ 9 * 10 * Copyright 2002-2006 Chuck Hagenbuch <chuck@horde.org> 11 * 12 * See the enclosed file COPYING for license information (LGPL). If you 13 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html. 14 * 15 * @package XML_SVG 16 */ 17 class XML_SVG { 18 19 function example() 20 { 21 // Create an instance of XML_SVG_Document. All other objects 22 // will be added to this instance for printing. Set the height 23 // and width of the viewport. 24 $svg = &new XML_SVG_Document(array('width' => 400, 25 'height' => 200)); 26 27 // Create an instance of XML_SVG_Group. Set the style, 28 // transforms for child objects. 29 $g = &new XML_SVG_Group(array('style' => 'stroke:black', 30 'transform' => 'translate(200 100)')); 31 32 // Add a parent to the g instance. 33 $g->addParent($svg); 34 35 // The same results can be accomplished by making g a child of the svg. 36 // $svg->addChild($g); 37 38 // Create and animate a circle. 39 $circle = &new XML_SVG_Circle(array('cx' => 0, 40 'cy' => 0, 41 'r' => 100, 42 'style' => 'stroke-width:3')); 43 $circle->addChild(new XML_SVG_Animate(array('attributeName' => 'r', 44 'attributeType' => 'XML', 45 'from' => 0, 46 'to' => 75, 47 'dur' => '3s', 48 'fill' => 'freeze'))); 49 $circle->addChild(new XML_SVG_Animate(array('attributeName' => 'fill', 50 'attributeType' => 'CSS', 51 'from' => 'green', 52 'to' => 'red', 53 'dur' => '3s', 54 'fill' => 'freeze'))); 55 56 // Make the circle a child of g. 57 $g->addChild($circle); 58 59 // Create and animate some text. 60 $text = &new XML_SVG_Text(array('text' => 'SVG chart!', 61 'x' => 0, 62 'y' => 0, 63 'style' => 'font-size:20;text-anchor:middle;')); 64 $text->addChild(new XML_SVG_Animate(array('attributeName' => 'font-size', 65 'attributeType' => 'auto', 66 'from' => 0, 67 'to' => 20, 68 'dur' => '3s', 69 'fill' => 'freeze'))); 70 71 // Make the text a child of g. 72 $g->addChild($text); 73 74 // Send a message to the svg instance to start printing. 75 $svg->printElement(); 76 } 77 78 } 79 80 /** 81 * XML_SVG_Element 82 * 83 * This is the base class for the different SVG Element 84 * Objects. Extend this class to create a new SVG Element. 85 * 86 * @package XML_SVG 87 */ 88 class XML_SVG_Element { 89 90 var $_elements = null; 91 var $_style = null; 92 var $_transform = null; 93 var $_id = null; 94 95 function XML_SVG_Element($params = array()) 96 { 97 foreach ($params as $p => $v) { 98 $param = '_' . $p; 99 $this->$param = $v; 100 } 101 } 102 103 /** 104 * Most SVG elements can contain child elements. This method calls 105 * the printElement method of any child element added to this 106 * object by use of the addChild method. 107 */ 108 function printElement() 109 { 110 // Loop and call. 111 if (is_array($this->_elements)) { 112 foreach ($this->_elements as $child) { 113 $child->printElement(); 114 } 115 } 116 } 117 118 /** 119 * This method adds an object reference (or value, if $copy is 120 * true) to the _elements array. 121 */ 122 function addChild(&$element, $copy = false) 123 { 124 if ($copy) { 125 $this->_elements[] = &$element->copy(); 126 } else { 127 $this->_elements[] = &$element; 128 } 129 } 130 131 /** 132 * This method sends a message to the passed element requesting to 133 * be added as a child. 134 */ 135 function addParent(&$parent) 136 { 137 if (is_subclass_of($parent, 'XML_SVG_Element')) { 138 $parent->addChild($this); 139 } 140 } 141 142 function copy() 143 { 144 if (version_compare(zend_version(), '2', '>')) { 145 return clone($this); 146 } else { 147 $xml_svg = $this; 148 return $xml_svg; 149 } 150 } 151 152 /** 153 * Print each of the passed parameters, if they are set. 154 */ 155 function printParams() 156 { 157 foreach (func_get_args() as $param) { 158 $_param = '_' . $param; 159 if (isset($this->$_param)) { 160 switch ($param) { 161 case 'filter': 162 echo ' filter="url(#' . $this->$_param . ')"'; 163 break; 164 165 default: 166 echo ' ' . str_replace('_', '-', $param) . '="' . $this->$_param . '"'; 167 break; 168 } 169 } 170 } 171 } 172 173 // Set any named attribute of an element to a value. 174 function setParam($param, $value) 175 { 176 $attr = '_' . $param; 177 $this->$attr = $value; 178 } 179 180 // Get any named attribute of an element. 181 function getParam($param) 182 { 183 $attr = '_' . $param; 184 if (isset($this->$attr)) { 185 return $this->$attr; 186 } else { 187 return null; 188 } 189 } 190 191 // Print out the object for debugging. 192 function debug() 193 { 194 echo '<pre>'; var_dump($this); echo '</pre>'; 195 } 196 197 } 198 199 /** 200 * XML_SVG_Fragment 201 * 202 * @package XML_SVG 203 */ 204 class XML_SVG_Fragment extends XML_SVG_Element { 205 206 var $_width; 207 var $_height; 208 var $_viewBox; 209 var $_x; 210 var $_y; 211 212 function printElement() 213 { 214 echo '<svg'; 215 $this->printParams('id', 'width', 'height', 'x', 'y', 'viewBox', 'style'); 216 echo ' xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">' . "\n"; 217 parent::printElement(); 218 echo "</svg>\n"; 219 } 220 221 function bufferObject() 222 { 223 ob_start(); 224 $this->printElement(); 225 $output = ob_get_contents(); 226 ob_end_clean(); 227 228 return $output; 229 } 230 } 231 232 /** 233 * XML_SVG_Document 234 * 235 * This extends the XML_SVG_Fragment class. It wraps the XML_SVG_Frament output 236 * with a content header, xml definition and doctype. 237 * 238 * @package XML_SVG 239 */ 240 class XML_SVG_Document extends XML_SVG_Fragment { 241 242 function printElement() 243 { 244 header('Content-Type: image/svg+xml'); 245 246 print('<?xml version="1.0" encoding="iso-8859-1"?>'."\n"); 247 print('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" 248 "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">' . "\n"); 249 250 parent::printElement(); 251 } 252 253 } 254 255 /** 256 * XML_SVG_Group 257 * 258 * @package XML_SVG 259 */ 260 class XML_SVG_Group extends XML_SVG_Element { 261 262 function printElement() 263 { 264 echo '<g'; 265 $this->printParams('id', 'style', 'transform', 'filter'); 266 print(">\n"); 267 parent::printElement(); 268 print("</g>\n"); 269 } 270 271 } 272 273 /** 274 * XML_SVG_Textpath 275 * 276 * @package XML_SVG 277 */ 278 class XML_SVG_Textpath extends XML_SVG_Element { 279 280 var $_text; 281 var $_x; 282 var $_y; 283 var $_dx; 284 var $_dy; 285 var $_rotate; 286 var $_textLength; 287 var $_lengthAdjust; 288 var $_charset; 289 290 function printElement($element = 'textpath') 291 { 292 echo '<' . $element; 293 $this->printParams('id', 'x', 'y', 'dx', 'dy', 'rotate', 294 'textLength', 'lengthAdjust', 'style', 'transform'); 295 echo '>'; 296 if (isset($this->_charset)) { 297 echo @htmlspecialchars($this->_text, ENT_COMPAT, $this->_charset); 298 } else { 299 echo htmlspecialchars($this->_text); 300 } 301 parent::printElement(); 302 echo "</$element>\n"; 303 } 304 305 function setShape($x, $y, $text) 306 { 307 $this->_x = $x; 308 $this->_y = $y; 309 $this->_text = $text; 310 } 311 312 } 313 314 /** 315 * XML_SVG_Text 316 * 317 * @package XML_SVG 318 */ 319 class XML_SVG_Text extends XML_SVG_Textpath { 320 321 function printElement() 322 { 323 parent::printElement('text'); 324 } 325 326 function setShape($x, $y, $text) 327 { 328 $this->_x = $x; 329 $this->_y = $y; 330 $this->_text = $text; 331 } 332 333 } 334 335 /** 336 * XML_SVG_Tspan 337 * 338 * @package XML_SVG 339 */ 340 class XML_SVG_Tspan extends XML_SVG_Element { 341 342 var $_text; 343 var $_x; 344 var $_y; 345 var $_dx; 346 var $_dy; 347 var $_rotate; 348 var $_textLength; 349 var $_lengthAdjust; 350 351 function printElement() 352 { 353 echo '<tspan'; 354 $this->printParams('id', 'x', 'y', 'dx', 'dy', 'rotate', 355 'textLength', 'lengthAdjust', 'style', 'transform'); 356 echo '>' . $this->_text; 357 if (is_array($this->_elements)) { 358 parent::printElement(); 359 } 360 echo "</tspan>\n"; 361 } 362 363 function setShape($x, $y, $text) 364 { 365 $this->_x = $x; 366 $this->_y = $y; 367 $this->_text = $text; 368 } 369 370 } 371 372 /** 373 * XML_SVG_Circle 374 * 375 * @package XML_SVG 376 */ 377 class XML_SVG_Circle extends XML_SVG_Element { 378 379 var $_cx; 380 var $_cy; 381 var $_r; 382 383 function printElement() 384 { 385 echo '<circle'; 386 387 $this->printParams('id', 'cx', 'cy', 'r', 'style', 'transform'); 388 if (is_array($this->_elements)) { 389 // Print children, start and end tag. 390 echo ">\n"; 391 parent::printElement(); 392 echo "</circle>\n"; 393 } else { 394 // Print short tag. 395 echo "/>\n"; 396 } 397 } 398 399 function setShape($cx, $cy, $r) 400 { 401 $this->_cx = $cx; 402 $this->_cy = $cy; 403 $this->_r = $r; 404 } 405 406 } 407 408 /** 409 * XML_SVG_Line 410 * 411 * @package XML_SVG 412 */ 413 class XML_SVG_Line extends XML_SVG_Element { 414 415 var $_x1; 416 var $_y1; 417 var $_x2; 418 var $_y2; 419 420 function printElement() 421 { 422 echo '<line'; 423 $this->printParams('id', 'x1', 'y1', 'x2', 'y2', 'style'); 424 if (is_array($this->_elements)) { 425 // Print children, start and end tag. 426 print(">\n"); 427 parent::printElement(); 428 print("</line>\n"); 429 } else { 430 // Print short tag. 431 print("/>\n"); 432 } 433 } 434 435 function setShape($x1, $y1, $x2, $y2) 436 { 437 $this->_x1 = $x1; 438 $this->_y1 = $y1; 439 $this->_x2 = $x2; 440 $this->_y2 = $y2; 441 } 442 443 } 444 445 /** 446 * XML_SVG_Rect 447 * 448 * @package XML_SVG 449 */ 450 class XML_SVG_Rect extends XML_SVG_Element { 451 452 var $_x; 453 var $_y; 454 var $_width; 455 var $_height; 456 var $_rx; 457 var $_ry; 458 459 function printElement() 460 { 461 echo '<rect'; 462 $this->printParams('id', 'x', 'y', 'width', 'height', 463 'rx', 'ry', 'style'); 464 if (is_array($this->_elements)) { 465 // Print children, start and end tag. 466 print(">\n"); 467 parent::printElement(); 468 print("</rect>\n"); 469 } else { 470 // Print short tag. 471 print("/>\n"); 472 } 473 } 474 475 function setShape($x, $y, $width, $height) 476 { 477 $this->_x = $x; 478 $this->_y = $y; 479 $this->_width = $width; 480 $this->_height = $height; 481 } 482 483 } 484 485 /** 486 * XML_SVG_Ellipse 487 * 488 * @package XML_SVG 489 */ 490 class XML_SVG_Ellipse extends XML_SVG_Element { 491 492 var $_cx; 493 var $_cy; 494 var $_rx; 495 var $_ry; 496 497 function printElement() 498 { 499 echo '<ellipse'; 500 $this->printParams('id', 'cx', 'cy', 'rx', 'ry', 'style', 'transform'); 501 if (is_array($this->_elements)) { 502 // Print children, start and end tag. 503 print(">\n"); 504 parent::printElement(); 505 print("</ellipse>\n"); 506 } else { 507 // Print short tag. 508 print(" />\n"); 509 } 510 } 511 512 function setShape($cx, $cy, $rx, $ry) 513 { 514 $this->_cx = $cx; 515 $this->_cy = $cy; 516 $this->_rx = $rx; 517 $this->_ry = $ry; 518 } 519 520 } 521 522 /** 523 * XML_SVG_Polyline 524 * 525 * @package XML_SVG 526 */ 527 class XML_SVG_Polyline extends XML_SVG_Element { 528 529 var $_points; 530 531 function printElement() 532 { 533 echo '<polyline'; 534 $this->printParams('id', 'points', 'style', 'transform'); 535 536 if (is_array($this->_elements)) { 537 // Print children, start and end tag. 538 print(">\n"); 539 parent::printElement(); 540 print("</polyline>\n"); 541 } else { 542 // Print short tag. 543 print("/>\n"); 544 } 545 } 546 547 function setShape($points) 548 { 549 $this->_points = $points; 550 } 551 552 } 553 554 /** 555 * XML_SVG_Polygon 556 * 557 * @package XML_SVG 558 */ 559 class XML_SVG_Polygon extends XML_SVG_Element { 560 561 var $_points; 562 563 function printElement() 564 { 565 echo '<polygon'; 566 $this->printParams('id', 'points', 'style', 'transform'); 567 if (is_array($this->_elements)) { 568 // Print children, start and end tag. 569 print(">\n"); 570 parent::printElement(); 571 print("</polygon>\n"); 572 } else { 573 // Print short tag. 574 print("/>\n"); 575 } 576 } 577 578 function setShape($points) 579 { 580 $this->_points = $points; 581 } 582 583 } 584 585 /** 586 * XML_SVG_Path 587 * 588 * @package XML_SVG 589 */ 590 class XML_SVG_Path extends XML_SVG_Element { 591 592 var $_d; 593 594 function printElement() 595 { 596 echo '<path'; 597 $this->printParams('id', 'd', 'style', 'transform'); 598 if (is_array($this->_elements)) { 599 // Print children, start and end tag. 600 print(">\n"); 601 parent::printElement(); 602 print("</path>\n"); 603 } else { 604 // Print short tag. 605 print("/>\n"); 606 } 607 } 608 609 function setShape($d) 610 { 611 $this->_d = $d; 612 } 613 614 } 615 616 /** 617 * XML_SVG_Image 618 * 619 * @package XML_SVG 620 */ 621 class XML_SVG_Image extends XML_SVG_Element { 622 623 var $_x; 624 var $_y; 625 var $_width; 626 var $_height; 627 var $_href; 628 629 function printElement() 630 { 631 echo '<image'; 632 $this->printParams('id', 'x', 'y', 'width', 'height', 'style'); 633 if (!empty($this->_href)) { 634 echo ' xlink:href="' . $this->_href . '"'; 635 } 636 if (is_array($this->_elements)) { 637 // Print children, start and end tag. 638 echo ">\n"; 639 parent::printElement(); 640 echo "</image>\n"; 641 } else { 642 // Print short tag. 643 echo " />\n"; 644 } 645 } 646 647 function setShape($x, $y, $width, $height) 648 { 649 $this->_x = $x; 650 $this->_y = $y; 651 $this->_width = $width; 652 $this->_height = $height; 653 } 654 655 } 656 657 /** 658 * XML_SVG_Animate 659 * 660 * @package XML_SVG 661 */ 662 class XML_SVG_Animate extends XML_SVG_Element { 663 664 var $_attributeName; 665 var $_attributeType; 666 var $_from; 667 var $_to; 668 var $_begin; 669 var $_dur; 670 var $_fill; 671 672 function printElement() 673 { 674 echo '<animate'; 675 $this->printParams('id', 'attributeName', 'attributeType', 'from', 'to', 676 'begin', 'dur', 'fill'); 677 if (is_array($this->_elements)) { 678 // Print children, start and end tag. 679 echo ">\n"; 680 parent::printElement(); 681 echo "</animate>\n"; 682 } else { 683 echo " />\n"; 684 } 685 } 686 687 function setShape($attributeName, $attributeType = '', $from = '', 688 $to = '', $begin = '', $dur = '', $fill = '') 689 { 690 $this->_attributeName = $attributeName; 691 $this->_attributeType = $attributeType; 692 $this->_from = $from; 693 $this->_to = $to; 694 $this->_begin = $begin; 695 $this->_dur = $dur; 696 $this->_fill = $fill; 697 } 698 699 } 700 701 /** 702 * XML_SVG_Filter 703 * 704 * @package XML_SVG 705 */ 706 class XML_SVG_Filter extends XML_SVG_Element { 707 708 function printElement() 709 { 710 echo '<filter'; 711 $this->printParams('id'); 712 if (is_array($this->_elements)) { 713 // Print children, start and end tag. 714 echo ">\n"; 715 parent::printElement(); 716 echo "</filter>\n"; 717 } else { 718 echo " />\n"; 719 } 720 } 721 722 function addPrimitive($primitive, $params) 723 { 724 $this->addChild(new XML_SVG_FilterPrimitive($primitive, $params)); 725 } 726 727 } 728 729 /** 730 * XML_SVG_FilterPrimitive 731 * 732 * @package XML_SVG 733 */ 734 class XML_SVG_FilterPrimitive extends XML_SVG_Element { 735 736 var $_primitives = array('Blend', 737 'ColorMatrix', 738 'ComponentTransfer', 739 'Composite', 740 'ConvolveMatrix', 741 'DiffuseLighting', 742 'DisplacementMap', 743 'Flood', 744 'GaussianBlur', 745 'Image', 746 'Merge', 747 'Morphology', 748 'Offset', 749 'SpecularLighting', 750 'Tile', 751 'Turbulence'); 752 753 var $_primitive; 754 755 var $_in; 756 var $_in2; 757 var $_result; 758 var $_x; 759 var $_y; 760 var $_dx; 761 var $_dy; 762 var $_width; 763 var $_height; 764 var $_mode; 765 var $_type; 766 var $_values; 767 var $_operator; 768 var $_k1; 769 var $_k2; 770 var $_k3; 771 var $_k4; 772 var $_surfaceScale; 773 var $_diffuseConstant; 774 var $_kernelUnitLength; 775 var $_floor_color; 776 var $_flood_opacity; 777 778 function XML_SVG_FilterPrimitive($primitive, $params = array()) 779 { 780 parent::XML_SVG_Element($params); 781 $this->_primitive = $primitive; 782 } 783 784 function printElement() 785 { 786 $name = 'fe' . $this->_primitive; 787 echo '<' . $name; 788 $this->printParams('id', 'x', 'y', 'dx', 'dy', 'width', 'height', 'in', 'in2', 789 'result', 'mode', 'type', 'values', 'operator', 790 'k1', 'k2', 'k3', 'k4', 'surfaceScale', 'stdDeviation', 791 'diffuseConstant', 'kernelUnitLength', 792 'flood_color', 'flood_opacity'); 793 if (is_array($this->_elements)) { 794 // Print children, start and end tag. 795 echo ">\n"; 796 parent::printElement(); 797 echo '</' . $name . '>'; 798 } else { 799 echo '/>'; 800 } 801 } 802 803 /** 804 * For feMerge elements. 805 */ 806 function addMergeNode($in) 807 { 808 $this->addChild(new XML_SVG_FilterMergeNode(array('in' => $in))); 809 } 810 811 } 812 813 /** 814 * XML_SVG_FilterMergeNode 815 * 816 * @package XML_SVG 817 */ 818 class XML_SVG_FilterMergeNode extends XML_SVG_Element { 819 820 var $_in; 821 822 function printElement() 823 { 824 echo '<feMergeNode'; 825 $this->printParams('in'); 826 echo '/>'; 827 } 828 829 } 830 831 /** 832 * XML_SVG_Use 833 * 834 * @package XML_SVG 835 */ 836 class XML_SVG_Use extends XML_SVG_Element { 837 838 var $_symbol; 839 840 function XML_SVG_Use($symbol, $params = array()) 841 { 842 parent::XML_SVG_Element($params); 843 $this->_symbol = $symbol; 844 } 845 846 function printElement() 847 { 848 echo '<use xlink:href="#' . $this->_symbol . '"/>'; 849 } 850 851 } 852 853 /** 854 * XML_SVG_Defs 855 * 856 * @package XML_SVG 857 */ 858 class XML_SVG_Defs extends XML_SVG_Element { 859 860 function printElement() 861 { 862 echo '<defs'; 863 $this->printParams('id', 'style', 'transform'); 864 echo ">\n"; 865 parent::printElement(); 866 echo "</defs>\n"; 867 } 868 869 } 870 871 /** 872 * XML_SVG_Marker 873 * 874 * @package XML_SVG 875 */ 876 class XML_SVG_Marker extends XML_SVG_Element { 877 878 var $_refX; 879 var $_refY; 880 var $_markerUnits; 881 var $_markerWidth; 882 var $_markerHeight; 883 var $_orient; 884 885 function printElement() 886 { 887 echo '<marker'; 888 $this->printParams('id', 'refX', 'refY', 'markerUnits', 889 'markerWidth', 'markerHeight', 'orient'); 890 if (is_array($this->_elements)) { // Print children, start and end tag. 891 print(">\n"); 892 parent::printElement(); 893 print("</marker>\n"); 894 } else { 895 print("/>\n"); 896 } 897 } 898 899 function setShape($refX = '', $refY = '', $markerUnits = '', 900 $markerWidth = '', $markerHeight = '', $orient = '') 901 { 902 $this->_refX = $refX; 903 $this->_refY = $refY; 904 $this->_markerUnits = $markerUnits; 905 $this->_markerWidth = $markerWidth; 906 $this->_markerHeight = $markerHeight; 907 $this->_orient = $orient; 908 } 909 910 } 911 912 /** 913 * XML_SVG_Title 914 * 915 * @package XML_SVG 916 */ 917 class XML_SVG_Title extends XML_SVG_Element { 918 919 var $_title; 920 921 function printElement() 922 { 923 echo '<title'; 924 $this->printParams('id', 'style'); 925 print(">\n"); 926 print($this->_title); 927 parent::printElement(); 928 print("</title>\n"); 929 } 930 931 } 932 933 /** 934 * XML_SVG_Desc 935 * 936 * @package XML_SVG 937 */ 938 class XML_SVG_Desc extends XML_SVG_Element { 939 940 var $_desc; 941 942 function printElement() 943 { 944 echo '<desc'; 945 $this->printParams('id', 'style'); 946 echo '>' . $this->_desc; 947 parent::printElement(); 948 echo "</desc>\n"; 949 } 950 951 } 952 953 /** 954 * XML_SVG_Tref 955 * 956 * @package XML_SVG 957 */ 958 class XML_SVG_Tref extends XML_SVG_Element { 959 960 var $_text; 961 var $_x; 962 var $_y; 963 var $_dx; 964 var $_dy; 965 var $_rotate; 966 var $_textLength; 967 var $_lengthAdjust; 968 969 function printElement() 970 { 971 echo '<tref'; 972 $this->printParams('id', 'x', 'y', 'dx', 'dy', 'rotate', 973 'textLength', 'lengthAdjust', 'style'); 974 echo '>' . $this->_text; 975 parent::printElement(); 976 echo "</tref>\n"; 977 } 978 979 }
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Sun Feb 25 18:01:28 2007 | par Balluche grâce à PHPXref 0.7 |