Changeset 319
- Timestamp:
- 02/26/08 22:54:13 (9 months ago)
- Files:
-
- cleversvg/trunk/document/csDocument.class.php (modified) (42 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
cleversvg/trunk/document/csDocument.class.php
r318 r319 2 2 /** 3 3 * Base SVG Document class 4 * 4 * 5 5 * @author Nicolas Perriault <nperriault@gmail.com> 6 6 * @package cleversvg … … 10 10 { 11 11 12 protected 12 protected 13 13 $attributes = array(), 14 14 $definitions = array(), … … 28 28 /** 29 29 * Class constructor 30 * 30 * 31 31 * @param mixed $width Width 32 32 * @param mixed $height Height … … 42 42 $this->initDefaults(); 43 43 } 44 44 45 45 /** 46 46 * Adds a SVG shape or group to current SVG Document at a certain depth 47 * 47 * 48 48 * @param mixed $element SVG element to add 49 49 * @param mixed $depth Depth (default: auto when set to NULL) … … 64 64 } 65 65 $this->elements[$depth] = $element; 66 66 67 67 // Add DOM id to document list, if any 68 68 $id = $element->getId(); … … 72 72 } 73 73 } 74 74 75 75 /** 76 76 * Returns an array containing SVG Document Elements 77 * 78 * @return array 77 * 78 * @return array 79 79 */ 80 80 public function getElements() … … 82 82 return $this->elements; 83 83 } 84 84 85 85 /** 86 86 * Gets the namespace prefix if this is an embedded document 87 * 87 * 88 88 * @return stringPie 89 89 */ … … 97 97 return $prefix; 98 98 } 99 99 100 100 /** 101 101 * Adds a script tag in global document array of scripts 102 * 102 * 103 103 * @param string $content 104 104 * @param string $content_type … … 108 108 $this->scripts[] = array($content, $type); 109 109 } 110 110 111 111 /** 112 112 * Adds a style tag in global document array of styles 113 * 113 * 114 114 * @param string $content Style declaration content 115 * @param string $content_type 116 * @param mixed $media 117 * @param mixed $title 115 * @param string $content_type 116 * @param mixed $media 117 * @param mixed $title 118 118 */ 119 119 public function addStyleDeclaration($content, $type='text/css', $media=null, $title=null) 120 120 { 121 $this->embed_styles[] = array('content' => $content, 122 'type' => $type, 123 'media' => $media, 124 'title' => $title); 125 } 126 121 $this->embed_styles[] = array('content' => $content, 122 'type' => $type, 123 'media' => $media, 124 'title' => $title); 125 } 126 127 127 /** 128 128 * Adds a linked stylesheet global document 129 * 130 * @param string $href 131 * @param string $content_type 132 * @param mixed $media 133 * @param mixed $title 129 * 130 * @param string $href 131 * @param string $content_type 132 * @param mixed $media 133 * @param mixed $title 134 134 */ 135 135 public function addStylesheetLink($href, $type='text/css', $alternate='no', $media=null, $title=null) 136 136 { 137 $this->external_styles[] = array('href' => $href, 138 'type' => $type, 139 'alternate' => $alternate, 140 'media' => $media, 137 $this->external_styles[] = array('href' => $href, 138 'type' => $type, 139 'alternate' => $alternate, 140 'media' => $media, 141 141 'title' => $title); 142 142 } 143 143 144 144 /** 145 145 * Adds a definition to current SVG Document 146 * 146 * 147 147 * @param mixed $definition SVG definition to add 148 148 * @param string $id Override element DOM id … … 173 173 } 174 174 } 175 175 176 176 /** 177 177 * Returns an array containing SVG Document definitions 178 * 178 * 179 179 * @return array 180 180 */ … … 183 183 return $this->definitions; 184 184 } 185 185 186 186 /** 187 187 * Returns an array containing used definitions for current document 188 * 188 * 189 189 * @return array 190 190 */ … … 193 193 return $this->used_definitions; 194 194 } 195 195 196 196 /** 197 197 * Returns a defined element alias (a <use/> tag) 198 * 198 * 199 199 * @return array 200 200 */ … … 206 206 sprintf('Unable to find id "%s" in current document.')); 207 207 } 208 else 208 else 209 209 { 210 210 return $this->definitions[$id]; 211 211 } 212 212 } 213 213 214 214 /** 215 215 * Returns current DOMDOcument 216 * 216 * 217 217 * @return DOMDocument 218 218 */ … … 256 256 } 257 257 } 258 258 259 259 /** 260 260 * Uses a definition in current SVG Document 261 * 261 * 262 262 * @param mixed $element 263 263 * @param array $attrs More atributes to add to <use/> tag … … 267 267 $this->used_definitions[] = array($this->getDefinition($id), $attrs); 268 268 } 269 269 270 270 /** 271 271 * Swap respective depths of two elements 272 * 272 * 273 273 * @param sfSvgElement $element1 274 274 * @param sfSvgElement $element2 … … 283 283 $element1->setDepth($e2depth); 284 284 } 285 285 286 286 /** 287 287 * Checks if a DOM id already exists in current SVG Document 288 * 288 * 289 289 * @return boolean 290 290 */ … … 293 293 return in_array($dom_id, $this->dom_ids); 294 294 } 295 295 296 296 /** 297 297 * Initialize object with default required values if they're not present in 298 298 * the attributes array 299 * 299 * 300 300 */ 301 301 public function initDefaults() … … 314 314 } 315 315 } 316 316 317 317 /** 318 318 * Sets a description describing the SVG Document 319 * 320 * @param string $description 319 * 320 * @param string $description 321 321 */ 322 322 public function setDescription($description) … … 324 324 $this->description = $description; 325 325 } 326 326 327 327 /** 328 328 * Sets if the SVG document will be embedded or not 329 * 329 * 330 330 * @param boolean $flag 331 331 */ … … 334 334 $this->embedded = (boolean) $flag; 335 335 } 336 336 337 337 /** 338 338 * Sets if the SVG document will be stricly validated 339 * 339 * 340 340 * @param boolean $flag 341 341 */ … … 344 344 $this->strict_mode = (boolean) $flag; 345 345 } 346 346 347 347 /** 348 348 * Sets if the SVG document title 349 * 349 * 350 350 * @param string $title 351 351 */ … … 354 354 $this->title = trim($title) != '' ? $title : 'Untitled SVG Document'; 355 355 } 356 356 357 357 /** 358 358 * Standard XML attribute for identifying an XML namespace. 359 * 359 * 360 360 */ 361 361 public function setXmlns($xmlns='http://www.w3.org/2000/svg') … … 363 363 $this->attributes['xmlns'] = $xmlns; 364 364 } 365 365 366 366 /** 367 367 * The x-axis coordinate of one corner of the rectangular region into which an 368 368 * embedded 'svg' element is placed. 369 * 369 * 370 370 */ 371 371 public function setX($x) … … 373 373 $this->attributes['x'] = (string)$x; 374 374 } 375 375 376 376 /** 377 377 * The y-axis coordinate of one corner of the rectangular region into which an 378 378 * embedded 'svg' element is placed. 379 * 379 * 380 380 */ 381 381 public function setY($y) … … 383 383 $this->attributes['y'] = (string)$y; 384 384 } 385 386 /** 387 * For outermost 'svg' elements, the intrinsic width of the SVG document 388 * fragment. For embedded 'svg' elements, the width of the rectangular 385 386 /** 387 * For outermost 'svg' elements, the intrinsic width of the SVG document 388 * fragment. For embedded 'svg' elements, the width of the rectangular 389 389 * region into which the 'svg' element is placed. 390 * 390 * 391 391 */ 392 392 public function setWidth($width) … … 394 394 $this->attributes['width'] = (string)$width; 395 395 } 396 397 /** 398 * For outermost 'svg' elements, the intrinsic height of the SVG document 399 * fragment. For embedded 'svg' elements, the height of the rectangular 396 397 /** 398 * For outermost 'svg' elements, the intrinsic height of the SVG document 399 * fragment. For embedded 'svg' elements, the height of the rectangular 400 400 * region into which the 'svg' element is placed. 401 * 401 * 402 402 */ 403 403 public function setHeight($height) … … 405 405 $this->attributes['height'] = (string)$height; 406 406 } 407 408 /** 409 * Indicates the SVG language version to which this document fragment 410 * conforms. In SVG 1.0, this attribute was fixed to the value "1.0". For 407 408 /** 409 * Indicates the SVG language version to which this document fragment 410 * conforms. In SVG 1.0, this attribute was fixed to the value "1.0". For 411 411 * SVG 1.1, the attribute should have the value "1.1". 412 * 412 * 413 413 */ 414 414 public function setVersion($version='1.1') … … 416 416 $this->attributes['version'] = (string)$version; 417 417 } 418 419 /** 420 * This attribute provides a convenient way to design SVG documents to 418 419 /** 420 * This attribute provides a convenient way to design SVG documents to 421 421 * scale-to-fit into an arbitrary viewport. 422 * 422 * 423 423 */ 424 424 public function setViewBox($x, $y, $width, $height) 425 425 { 426 $this->attributes['viewBox'] = sprintf('%d %d %d %d', 426 $this->attributes['viewBox'] = sprintf('%d %d %d %d', 427 427 $x, $y, $width, $height); 428 428 } 429 430 /** 431 * Describes the minimum SVG language profile that the author believes is 429 430 /** 431 * Describes the minimum SVG language profile that the author believes is 432 432 * necessary to correctly render the content. This can be considered metadata. 433 * 433 * 434 434 */ 435 435 public function setBaseProfile($base_profile) … … 437 437 $this->attributes['baseProfile'] = $base_profile; 438 438 } 439 439 440 440 /** 441 441 * Gets SVG XML rendering output. Never forget to send image/svg+xml headers 442 442 * before outputing. 443 * 443 * 444 444 * @param boolean $embedded Is SVG Document embedded ? 445 445 * @return string … … 451 451 $this->setEmbedded($embedded); 452 452 } 453 453 454 454 $xml_prepend = ''; 455 456 $dom = $this->getDomDocument(); 457 455 456 $dom = $this->getDomDocument(); 457 458 458 // External stylesheets 459 459 $this->processExternalStyleSheets(); 460 460 461 461 // Root node creation 462 462 $this->processRootNode(); … … 467 467 $this->processElements(); 468 468 $this->processUsedDefinitions(); 469 469 470 470 // Document normalization 471 471 $dom->normalizeDocument(); 472 472 473 473 // Validation 474 474 $this->validate($dom); 475 475 476 476 // Output 477 477 if ($this->embedded === true) … … 484 484 } 485 485 } 486 486 487 487 /** 488 488 * Process definitionsn if any 489 * 489 * 490 490 */ 491 491 protected function processDefinitions() … … 506 506 } 507 507 } 508 508 509 509 /** 510 510 * Process Document elements 511 * 511 * 512 512 */ 513 513 protected function processElements() … … 521 521 } 522 522 } 523 523 524 524 /** 525 525 * Process embedded styles, if any 526 * 526 * 527 527 */ 528 528 protected function processEmbeddedStyles() … … 544 544 } 545 545 } 546 546 547 547 /** 548 548 * Process externals stylesheets, if any 549 * 549 * 550 550 */ 551 551 protected function processExternalStyleSheets() … … 557 557 { 558 558 $style_declaration = $dom->createProcessingInstruction( 559 'xml-stylesheet', 560 sprintf('href="%s" type="%s"', 561 $stylesheet['href'], 559 'xml-stylesheet', 560 sprintf('href="%s" type="%s"', 561 $stylesheet['href'], 562 562 $stylesheet['type'])); 563 563 $dom->appendChild($style_declaration); … … 565 565 } 566 566 } 567 567 568 568 /** 569 569 * Process meta description 570 * 570 * 571 571 */ 572 572 protected function processMetas() 573 573 { 574 574 $dom = $this->getDomDocument(); 575 575 576 576 // Title node 577 577 if (!is_null($this->title)) 578 578 { 579 $title_node = $dom->createElement($this->getNodeNS().'title', 579 $title_node = $dom->createElement($this->getNodeNS().'title', 580 580 strip_tags($this->title)); 581 581 $dom->documentElement->appendChild($title_node); 582 582 } 583 584 583 584 585 585 // Description node 586 586 if (!is_null($this->description)) 587 587 { 588 $desc_node = $dom->createElement($this->getNodeNS().'desc', 588 $desc_node = $dom->createElement($this->getNodeNS().'desc', 589 589 strip_tags($this->description)); 590 590 $dom->documentElement->appendChild($desc_node); 591 591 } 592 592 } 593 593 594 594 /** 595 595 * Process root node 596 * 596 * 597 597 */ 598 598 protected function processRootNode() … … 609 609 } 610 610 } 611 611 612 612 /** 613 613 * Process scripts, if any 614 * 614 * 615 615 */ 616 616 protected function processScripts() … … 630 630 } 631 631 } 632 632 633 633 /** 634 634 * Process used definitions, if any 635 * 635 * 636 636 */ 637 637 protected function processUsedDefinitions() … … 654 654 } 655 655 } 656 656 657 657 /** 658 658 * Validates document 659 * 659 * 660 660 * @throws csException 661 661 */ … … 665 665 { 666 666 set_error_handler(array('csDocument', 'handleValidationError'), E_ALL); 667 try 668 { 669 $dom->validate(); 670 } 671 catch (csException $e) 672 { 673 throw 'This SVG Document does not validate: '.$e; 667 if (!$dom->validate()) 668 { 669 throw new csException('This SVG Document does not validate'); 674 670 } 675 671 restore_error_handler(); 676 672 } 677 673 } 678 674 679 675 /** 680 676 * Handle validation errors 681 * 677 * 682 678 * @throws csException 683 679 */ … … 688 684 exit; 689 685 } 690 686 691 687 }
