/usr/share/perl5/SVG/Element.pm is in libsvg-perl 2.59-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 | =pod
=encoding ISO8859-1
=head1 NAME
SVG::Element - Generate the element bits for SVG.pm
=head1 AUTHOR
Ronan Oger, cpan@roitsystems.com
=head1 SEE ALSO
L<SVG>,L<SVG::XML>,L<SVG::Element>,L<SVG::Parser>
L<http://www.roitsystems.com/> ROASP.com: Serverside SVG server
L<http://www.roitsystems.com/> ROIT Systems: Commercial SVG perl solutions
L<http://www.w3c.org/Graphics/SVG/> SVG at the W3C
=cut
package SVG::Element;
use strict;
use warnings;
use SVG::XML;
use SVG::DOM;
use SVG::Extension;
use Scalar::Util qw/weaken/;
our $AUTOLOAD;
our $VERSION = '2.59';
my @autosubs = qw(
animateMotion animateColor animateTransform circle ellipse rect polyline
path polygon line title desc defs
altGlyph altGlyphDef altGlyphItem clipPath color-profile
cursor definition-src font-face-format font-face-name
font-face-src font-face-url foreignObject glyph
glyphRef hkern marker mask metadata missing-glyph
mpath switch symbol textPath tref tspan view vkern marker textbox
flowText style script
image a g
);
our %autosubs = map { $_ => 1 } @autosubs;
#-------------------------------------------------------------------------------
sub new {
my ( $proto, $name, %attrs ) = @_;
my $class = ref($proto) || $proto;
my $self = { -name => $name };
foreach my $key ( keys %attrs ) {
#handle escapes for special elements such as anchor
if ( $key =~ /^-/ ) {
if ( $key eq '-href' ) {
$self->{'xlink:href'} = $attrs{$key};
$self->{'xlink:type'} = $attrs{-type} if $attrs{-type};
$self->{'xlink:role'} = $attrs{-role} if $attrs{-role};
$self->{'xlink:title'} = $attrs{-title} if $attrs{-title};
$self->{'xlink:show'} = $attrs{-show} if $attrs{-show};
$self->{'xlink:arcrole'} = $attrs{-arcrole} if $attrs{-arcrole};
$self->{'xlink:actuate'} = $attrs{-actuate} if $attrs{-actuate};
next;
}
}
$self->{$key} = $attrs{$key};
}
return bless( $self, $class );
}
#-------------------------------------------------------------------------------
sub release {
my $self = shift;
foreach my $key ( keys( %{$self} ) ) {
next if $key =~ /^-/;
if ( ref( $self->{$key} ) =~ /^SVG/ ) {
eval { $self->{$key}->release; };
}
delete( $self->{$key} );
}
return $self;
}
sub xmlify {
my $self = shift;
my $ns = $self->{-namespace} || $self->{-docref}->{-namespace} || undef;
my $xml = '';
#prep the attributes
my %attrs;
foreach my $k ( keys( %{$self} ) ) {
if ( $k =~ /^-/ ) { next; }
if ( ref( $self->{$k} ) eq 'ARRAY' ) {
$attrs{$k} = join( ', ', @{ $self->{$k} } );
} elsif ( ref( $self->{$k} ) eq 'HASH' ) {
$attrs{$k} = cssstyle( %{ $self->{$k} } );
} elsif ( ref( $self->{$k} ) eq '' ) {
$attrs{$k} = $self->{$k};
}
}
#prep the tag
if ( $self->{-comment} ) {
$xml .= $self->xmlcomment( $self->{-comment} );
return $xml;
} elsif ( $self->{-name} eq 'document' ) {
#write the xml header
$xml .= $self->xmldecl unless $self->{-inline};
$xml .= $self->xmlpi( $self->{-document}->{-pi} )
if $self->{-document}->{-pi};
#and write the dtd if this is inline
$xml .= $self->dtddecl unless $self->{-inline};
#rest of the xml
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$xml .= $k->xmlify($ns);
}
}
return $xml;
}
my $is_cdataish = defined $self->{-cdata}
|| defined $self->{-CDATA}
|| defined $self->{-cdata_noxmlesc};
if ( defined $self->{-childs} || $is_cdataish ) {
$xml .= $self->{-docref}->{-elsep} unless ($self->{-inline} && $self->{-name});
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$xml .= xmltagopen_ln( $self->{-name}, $ns, %attrs );
$self->{-docref}->{-level}++;
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$xml .= $k->xmlify($ns);
}
}
if ( defined $self->{-cdata} ) {
$xml .= $self->xmlescp( $self->{-cdata} );
}
if ( defined $self->{-CDATA} ) {
$xml .= '<![CDATA[' . $self->{-CDATA} . ']]>';
}
if ( defined $self->{-cdata_noxmlesc} ) {
$xml .= $self->{-cdata_noxmlesc};
}
#return without writing the tag out if it the document tag
$self->{-docref}->{-level}--;
unless( $is_cdataish ) {
$xml .= $self->{-docref}->{-elsep};
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
}
$xml .= xmltagclose_ln( $self->{-name}, $ns );
} else {
$xml .= $self->{-docref}->{-elsep};
$xml .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$xml .= xmltag_ln( $self->{-name}, $ns, %attrs );
}
#return the finished tag
return $xml;
}
sub perlify {
my $self = shift;
my $code = '';
#prep the attributes
my %attrs;
foreach my $k ( keys( %{$self} ) ) {
next if $k =~ /^-/;
if ( ref( $self->{$k} ) eq 'ARRAY' ) {
$attrs{$k} = join( ', ', @{ $self->{$k} } );
} elsif ( ref( $self->{$k} ) eq 'HASH' ) {
$attrs{$k} = cssstyle( %{ $self->{$k} } );
} elsif ( ref( $self->{$k} ) eq '' ) {
$attrs{$k} = $self->{$k};
}
}
if ( $self->{-comment} ) {
$code .= "->comment($self->{-comment})";
return $code;
} elsif ( $self->{-pi} ) {
$code .= "->pi($self->{-pi})";
return $code;
} elsif ( $self->{-name} eq 'document' ) {
#write the xml header
#$xml .= $self->xmldecl;
#and write the dtd if this is inline
#$xml .= $self->dtddecl unless $self->{-inline};
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$code .= $k->perlify();
}
}
return $code;
}
if ( defined $self->{-childs} ) {
$code .= $self->{-docref}->{-elsep};
$code .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$code .=
$self->{-name} . '('
. ( join ', ', ( map { "$_=>'$attrs{$_}'" } sort keys %attrs ) )
. ')';
if ( $self->{-cdata} ) {
$code .= "->cdata($self->{-cdata})";
} elsif ( $self->{-CDATA} ) {
$code .= "->CDATA($self->{-CDATA})";
} elsif ( $self->{-cdata_noxmlesc} ) {
$code .= "->cdata_noxmlesc($self->{-cdata_noxmlesc})";
}
$self->{-docref}->{-level}++;
foreach my $k ( @{ $self->{-childs} } ) {
if ( ref($k) =~ /^SVG::Element/ ) {
$code .= $k->perlify();
}
}
$self->{-docref}->{-level}--;
} else {
$code .= $self->{-docref}->{-elsep};
$code .= $self->{-docref}->{-indent} x $self->{-docref}->{-level};
$code .=
$self->{-name} . '('
. ( join ', ', ( map { "$_=>'$attrs{$_}'" } sort keys %attrs ) )
. ')';
}
return $code;
}
*toperl = \&perlify;
sub addchilds {
my $self = shift;
push @{ $self->{-childs} }, @_;
return $self;
}
=pod
=head2 tag (alias: element)
$tag = $SVG->tag($name, %attributes)
Generic element generator. Creates the element named $name with the attributes
specified in %attributes. This method is the basis of most of the explicit
element generators.
B<Example:>
my $tag = $SVG->tag('g', transform=>'rotate(-45)');
=cut
sub tag {
my ( $self, $name, %attrs ) = @_;
unless ( $self->{-parent} ) {
#traverse down the tree until you find a non-document entry
while ( $self->{-document} ) { $self = $self->{-document} }
}
my $tag = new SVG::Element( $name, %attrs );
#define the element namespace
$tag->{-namespace} = $attrs{-namespace} if ( $attrs{-namespace} );
#add the tag to the document element
$tag->{-docref} = $self->{-docref};
weaken( $tag->{-docref} );
#create the empty idlist hash ref unless it already exists
$tag->{-docref}->{-idlist} = {}
unless ( defined $tag->{-docref}->{-idlist} );
#verify that the current id is unique. compain on exception
#>>>TBD: add -strictids option to disable this check if desired
if ( $tag->{id} ) {
if ( $self->getElementByID( $tag->{id} ) ) {
$self->error( $tag->{id} => "ID already exists in document" );
return;
}
}
#add the current id reference to the document id hash
if ( defined($tag->{id}) ) {
$tag->{-docref}->{-idlist}->{ $tag->{id} } = $tag;
}
#create the empty idlist hash ref unless it already exists
$tag->{-docref}->{-elist} = {}
unless ( defined $tag->{-docref}->{-elist} );
#create the empty idlist hash ref unless it already exists
$tag->{-docref}->{-elist}->{ $tag->{-name} } = []
unless ( defined $tag->{-docref}->{-elist}->{ $tag->{-name} } );
#add the current element ref to the corresponding element-hash array
# -elist is a hash of element names. key name is element, content is object ref.
# add the reference to $tag to the array of refs that belong to the
# key $tag->{-name}.
unshift @{ $tag->{-docref}->{-elist}->{ $tag->{-name} } }, $tag;
# attach element to the DOM of the document
$tag->{-parent} = $self;
weaken($tag->{-parent});
$tag->{-parentname} = $self->{-name};
$self->addchilds($tag);
return ($tag);
}
*element = \&tag;
=pod
=head2 anchor
$tag = $SVG->anchor(%attributes)
Generate an anchor element. Anchors are put around objects to make them
'live' (i.e. clickable). It therefore requires a drawn object or group element
as a child.
=head3 optional anchor attributes
the following attributes are expected for anchor tags (any any tags which use -href links):
=head2 -href required
=head2 -type optional
=head2 -role optional
=head2 -title optional
=head2 -show optional
=head2 -arcrole optional
=head2 -actuate optional
=head2 target optional
For more information on the options, refer to the w3c XLink specification at
L<http://www.w3.org/TR/xlink/>
B<Example:>
# generate an anchor
$tag = $SVG->anchor(
-href=>'http://here.com/some/simpler/SVG.SVG'
-title => 'new window 2 example title',
-actuate => 'onLoad',
-show=> 'embed',
);
for more information about the options above, refer to Link section in the SVG recommendation: L<http://www.w3.org/TR/SVG11/linking.html#Links>
# add a circle to the anchor. The circle can be clicked on.
$tag->circle(cx=>10,cy=>10,r=>1);
# more complex anchor with both URL and target
$tag = $SVG->anchor(
-href => 'http://somewhere.org/some/other/page.html',
target => 'new_window'
);
=cut
sub anchor {
my ( $self, %attrs ) = @_;
my $an = $self->tag( 'a', %attrs );
#$an->{'xlink:href'}=$attrs{-href} if(defined $attrs{-href});
#$an->{'target'}=$attrs{-target} if(defined $attrs{-target});
return ($an);
}
sub svg {
my ( $self, %attrs ) = @_;
my $svg = $self->tag( 'svg', %attrs );
$svg->{'height'} = '100%' unless ( $svg->{'height'} );
$svg->{'width'} = '100%' unless ( $svg->{'width'} );
return ($svg);
}
=pod
=head2 circle
$tag = $SVG->circle(%attributes)
Draw a circle at (cx,cy) with radius r.
B<Example:>
my $tag = $SVG->circlecx=>4, cy=>2, r=>1);
=cut
=pod
=head2 ellipse
$tag = $SVG->ellipse(%attributes)
Draw an ellipse at (cx,cy) with radii rx,ry.
B<Example:>
my $tag = $SVG->ellipse(
cx=>10, cy=>10,
rx=>5, ry=>7,
id=>'ellipse',
style=>{
'stroke'=>'red',
'fill'=>'green',
'stroke-width'=>'4',
'stroke-opacity'=>'0.5',
'fill-opacity'=>'0.2'
}
);
=cut
=pod
=head2 rectangle (alias: rect)
$tag = $SVG->rectangle(%attributes)
Draw a rectangle at (x,y) with width 'width' and height 'height' and side radii
'rx' and 'ry'.
B<Example:>
$tag = $SVG->rectangle(
x=>10, y=>20,
width=>4, height=>5,
rx=>5.2, ry=>2.4,
id=>'rect_1'
);
=cut
sub rectangle {
my ( $self, %attrs ) = @_;
return $self->tag( 'rect', %attrs );
}
=pod
=head2 image
$tag = $SVG->image(%attributes)
Draw an image at (x,y) with width 'width' and height 'height' linked to image
resource '-href'. See also L<"use">.
B<Example:>
$tag = $SVG->image(
x=>100, y=>100,
width=>300, height=>200,
'-href'=>"image.png", #may also embed SVG, e.g. "image.SVG"
id=>'image_1'
);
B<Output:>
<image xlink:href="image.png" x="100" y="100" width="300" height="200"/>
=cut
#sub image {
# my ($self,%attrs)=@_;
# my $im=$self->tag('image',%attrs);
# #$im->{'xlink:href'}=$attrs{-href} if(defined $attrs{-href});
# return $im;
#}
=pod
=head2 use
$tag = $SVG->use(%attributes)
Retrieve the content from an entity within an SVG document and apply it at
(x,y) with width 'width' and height 'height' linked to image resource '-href'.
B<Example:>
$tag = $SVG->use(
x=>100, y=>100,
width=>300, height=>200,
'-href'=>"pic.SVG#image_1",
id=>'image_1'
);
B<Output:>
<use xlink:href="pic.SVG#image_1" x="100" y="100" width="300" height="200"/>
According to the SVG specification, the 'use' element in SVG can point to a
single element within an external SVG file.
=cut
sub use {
my ( $self, %attrs ) = @_;
my $u = $self->tag( 'use', %attrs );
$u->{'xlink:href'} = $attrs{-href} if ( defined $attrs{-href} );
return $u;
}
=pod
=head2 polygon
$tag = $SVG->polygon(%attributes)
Draw an n-sided polygon with vertices at points defined by a string of the form
'x1,y1,x2,y2,x3,y3,... xy,yn'. The L<"get_path"> method is provided as a
convenience to generate a suitable string from coordinate data.
B<Example:>
# a five-sided polygon
my $xv = [0,2,4,5,1];
my $yv = [0,0,2,7,5];
$points = $a->get_path(
x=>$xv, y=>$yv,
-type=>'polygon'
);
$c = $a->polygon(
%$points,
id=>'pgon1',
style=>\%polygon_style
);
SEE ALSO:
L<"polyline">, L<"path">, L<"get_path">.
=cut
=pod
=head2 polyline
$tag = $SVG->polyline(%attributes)
Draw an n-point polyline with points defined by a string of the form
'x1,y1,x2,y2,x3,y3,... xy,yn'. The L<"get_path"> method is provided as a
convenience to generate a suitable string from coordinate data.
B<Example:>
# a 10-pointsaw-tooth pattern
my $xv = [0,1,2,3,4,5,6,7,8,9];
my $yv = [0,1,0,1,0,1,0,1,0,1];
$points = $a->get_path(
x=>$xv, y=>$yv,
-type=>'polyline',
-closed=>'true' #specify that the polyline is closed.
);
my $tag = $a->polyline (
%$points,
id=>'pline_1',
style=>{
'fill-opacity'=>0,
'stroke-color'=>'rgb(250,123,23)'
}
);
=head2 line
$tag = $SVG->line(%attributes)
Draw a straight line between two points (x1,y1) and (x2,y2).
B<Example:>
my $tag = $SVG->line(
id=>'l1',
x1=>0, y1=>10,
x2=>10, y2=>0
);
To draw multiple connected lines, use L<"polyline">.
=head2 text
$text = $SVG->text(%attributes)->cdata();
$text_path = $SVG->text(-type=>'path');
$text_span = $text_path->text(-type=>'span')->cdata('A');
$text_span = $text_path->text(-type=>'span')->cdata('B');
$text_span = $text_path->text(-type=>'span')->cdata('C');
define the container for a text string to be drawn in the image.
B<Input:>
-type = path type (path | polyline | polygon)
-type = text element type (path | span | normal [default])
B<Example:>
my $text1 = $SVG->text(
id=>'l1', x=>10, y=>10
)->cdata('hello, world');
my $text2 = $SVG->text(
id=>'l1', x=>10, y=>10, -cdata=>'hello, world');
my $text = $SVG->text(
id=>'tp', x=>10, y=>10 -type=>path)
->text(id=>'ts' -type=>'span')
->cdata('hello, world');
SEE ALSO:
L<"desc">, L<"cdata">.
=cut
sub text {
my ( $self, %attrs ) = @_;
my $pre = '';
$pre = $attrs{-type} || 'std';
my %get_pre = (
std => 'text',
path => 'textPath',
span => 'tspan',
);
$pre = $get_pre{ lc($pre) };
my $text = $self->tag( $pre, %attrs );
$text->{'xlink:href'} = $attrs{-href} if ( defined $attrs{-href} );
$text->{'target'} = $attrs{-target} if ( defined $attrs{-target} );
return ($text);
}
=pod
=head2 title
$tag = $SVG->title(%attributes)
Generate the title of the image.
B<Example:>
my $tag = $SVG->title(id=>'document-title')->cdata('This is the title');
=cut
=pod
=head2 desc
$tag = $SVG->desc(%attributes)
Generate the description of the image.
B<Example:>
my $tag = $SVG->desc(id=>'document-desc')->cdata('This is a description');
=head2 comment
$tag = $SVG->comment(@comments)
Generate the description of the image.
B<Example:>
my $tag = $SVG->comment('comment 1','comment 2','comment 3');
=cut
sub comment {
my ( $self, @text ) = @_;
my $tag = $self->tag('comment');
$tag->{ -comment } = [@text];
return $tag;
}
=pod
$tag = $SVG->pi(@pi)
Generate (or adds) a set of processing instructions which go at
the beginning of the document after the xml start tag
B<Example:>
my $tag = $SVG->pi('instruction one','instruction two','instruction three');
returns:
<?instruction one?>
<?instruction two?>
<?instruction three?>
=cut
#add
sub pi {
my ( $self, @text ) = @_;
return $self->{-document}->{ -pi } unless scalar @text;
my @pi;
@pi = @{ $self->{-document}->{ -pi } } if $self->{-document}->{ -pi };
unshift( @text, @pi ) if @pi;
$self->{-document}->{ -pi } = \@text;
my $tag = $self->tag('pi');
return $tag;
}
=pod
=head2 script
$tag = $SVG->script(%attributes)
Generate a script container for dynamic (client-side) scripting using
ECMAscript, Javascript or other compatible scripting language.
B<Example:>
my $tag = $SVG->script(type=>"text/ecmascript");
# populate the script tag with cdata
# be careful to manage the javascript line ends.
# qq|text| or qq§text§ where text is the script
# works well for this.
$tag->CDATA(qq|function d(){
//simple display function
for(cnt = 0; cnt < d.length; cnt++)
document.write(d[cnt]);//end for loop
document.write("<BR>");//write a line break
}|
);
# create an svg external script reference to an outside file
my $tag2 = SVG->script(type=>"text/ecmascript", -href="/scripts/example.es");
=pod
=head2 style
$tag = $SVG->style(%attributes)
Generate a style container for inline or xlink:href based styling instructions
B<Example:>
my $tag = $SVG->style(type=>"text/css");
# Populate the style tag with cdata.
# Be careful to manage the line ends.
# qq|text| or qq§text§, where text is the script
# work well for this.
$tag1->CDATA(qq|
rect fill:red;stroke:green;
circle fill:red;stroke:orange;
ellipse fill:none;stroke:yellow;
text fill:black;stroke:none;
|);
# Create a external CSS stylesheet reference
my $tag2 = $SVG->style(type=>"text/css", -href="/resources/example.css");
=pod
=head2 path
$tag = $SVG->path(%attributes)
Draw a path element. The path vertices may be imputed as a parameter or
calculated usingthe L<"get_path"> method.
B<Example:>
# a 10-pointsaw-tooth pattern drawn with a path definition
my $xv = [0,1,2,3,4,5,6,7,8,9];
my $yv = [0,1,0,1,0,1,0,1,0,1];
$points = $a->get_path(
x => $xv,
y => $yv,
-type => 'path',
-closed => 'true' #specify that the polyline is closed
);
$tag = $SVG->path(
%$points,
id => 'pline_1',
style => {
'fill-opacity' => 0,
'fill-color' => 'green',
'stroke-color' => 'rgb(250,123,23)'
}
);
SEE ALSO:
L<"get_path">.
=head2 get_path
$path = $SVG->get_path(%attributes)
Returns the text string of points correctly formatted to be incorporated into
the multi-point SVG drawing object definitions (path, polyline, polygon)
B<Input:> attributes including:
-type = path type (path | polyline | polygon)
x = reference to array of x coordinates
y = reference to array of y coordinates
B<Output:> a hash reference consisting of the following key-value pair:
points = the appropriate points-definition string
-type = path|polygon|polyline
-relative = 1 (define relative position rather than absolute position)
-closed = 1 (close the curve - path and polygon only)
B<Example:>
#generate an open path definition for a path.
my ($points,$p);
$points = $SVG->get_path(x=>\@x,y=>\@y,-relative=>1,-type=>'path');
#add the path to the SVG document
my $p = $SVG->path(%$path, style=>\%style_definition);
#generate an closed path definition for a a polyline.
$points = $SVG->get_path(
x=>\@x,
y=>\@y,
-relative=>1,
-type=>'polyline',
-closed=>1
); # generate a closed path definition for a polyline
# add the polyline to the SVG document
$p = $SVG->polyline(%$points, id=>'pline1');
B<Aliases:> get_path set_path
=cut
sub get_path {
my ( $self, %attrs ) = @_;
my $type = $attrs{-type} || 'path';
my @x = @{ $attrs{x} };
my @y = @{ $attrs{y} };
my $points;
# we need a path-like point string returned
if ( lc($type) eq 'path' ) {
my $char = 'M';
$char = ' m '
if ( defined $attrs{-relative} && lc( $attrs{-relative} ) );
while (@x) {
#scale each value
my $x = shift @x;
my $y = shift @y;
#append the scaled value to the graph
$points .= "$char $x $y ";
$char = ' L ';
$char = ' l '
if ( defined $attrs{-relative}
&& lc( $attrs{-relative} ) );
}
$points .= ' z '
if ( defined $attrs{-closed} && lc( $attrs{-closed} ) );
my %out = ( d => $points );
return \%out;
} elsif ( lc($type) =~ /^poly/ ) {
while (@x) {
#scale each value
my $x = shift @x;
my $y = shift @y;
#append the scaled value to the graph
$points .= "$x,$y ";
}
}
my %out = ( points => $points );
return \%out;
}
sub make_path {
my ( $self, %attrs ) = @_;
return get_path(%attrs);
}
sub set_path {
my ( $self, %attrs ) = @_;
return get_path(%attrs);
}
=pod
=head2 animate
$tag = $SVG->animate(%attributes)
Generate an SMIL animation tag. This is allowed within any nonempty tag. Refer\
to the W3C for detailed information on the subtleties of the animate SMIL
commands.
B<Inputs:> -method = Transform | Motion | Color
my $an_ellipse = $SVG->ellipse(
cx=>30,cy=>150,rx=>10,ry=>10,id=>'an_ellipse',
stroke=>'rgb(130,220,70)',fill=>'rgb(30,20,50)');
$an_ellipse-> animate(
attributeName=>"cx",values=>"20; 200; 20",dur=>"10s", repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"rx",values=>"10;30;20;100;50",
dur=>"10s", repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"ry",values=>"30;50;10;20;70;150",
dur=>"15s", repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"rx",values=>"30;75;10;100;20;20;150",
dur=>"20s", repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"fill",values=>"red;green;blue;cyan;yellow",
dur=>"5s", repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"fill-opacity",values=>"0;1;0.5;0.75;1",
dur=>"20s",repeatDur=>'indefinite');
$an_ellipse-> animate(
attributeName=>"stroke-width",values=>"1;3;2;10;5",
dur=>"20s",repeatDur=>'indefinite');
=cut
sub animate {
my ( $self, %attrs ) = @_;
my %rtr = %attrs;
my $method = $rtr{'-method'}; # Set | Transform | Motion | Color
$method = lc($method);
# we do not want this to pollute the generation of the tag
delete $rtr{-method}; #bug report from briac.
my %animation_method = (
transform => 'animateTransform',
motion => 'animateMotion',
color => 'animateColor',
set => 'set',
attribute => 'animate'
);
my $name = $animation_method{$method} || 'animate';
#list of legal entities for each of the 5 methods of animations
my %legal = (
animate => qq§ begin dur end min max restart repeatCount
repeatDur fill attributeType attributeName additive
accumulate calcMode values keyTimes keySplines
from to by §,
animateTransform => qq§ begin dur end min max restart repeatCount
repeatDur fill additive accumulate calcMode values
keyTimes keySplines from to by calcMode path keyPoints
rotate origin type attributeName attributeType §,
animateMotion => qq§ begin dur end min max restart repeatCount
repeatDur fill additive accumulate calcMode values
to by keyTimes keySplines from path keyPoints
rotate origin §,
animateColor => qq§ begin dur end min max restart repeatCount
repeatDur fill additive accumulate calcMode values
keyTimes keySplines from to by §,
set => qq§ begin dur end min max restart repeatCount repeatDur
fill to §
);
foreach my $k ( keys %rtr ) {
next if ( $k =~ /\-/ );
if ( $legal{$name} !~ /\b$k\b/ ) {
$self->error( "$name.$k" => "Illegal animation command" );
}
}
return $self->tag( $name, %rtr );
}
=pod
=head2 group
$tag = $SVG->group(%attributes)
Define a group of objects with common properties. groups can have style,
animation, filters, transformations, and mouse actions assigned to them.
B<Example:>
$tag = $SVG->group(
id => 'xvs000248',
style => {
'font' => [ qw( Arial Helvetica sans ) ],
'font-size' => 10,
'fill' => 'red',
},
transform => 'rotate(-45)'
);
=cut
sub group {
my ( $self, %attrs ) = @_;
return $self->tag( 'g', %attrs );
}
=pod
=head2 defs
$tag = $SVG->defs(%attributes)
define a definition segment. A Defs requires children when defined using SVG.pm
B<Example:>
$tag = $SVG->defs(id => 'def_con_one',);
=head2 style
$SVG->style(%styledef)
Sets/Adds style-definition for the following objects being created.
Style definitions apply to an object and all its children for all properties for
which the value of the property is not redefined by the child.
=cut
sub STYLE {
my ( $self, %attrs ) = @_;
$self->{style} = $self->{style} || {};
foreach my $k ( keys %attrs ) {
$self->{style}->{$k} = $attrs{$k};
}
return $self;
}
=pod
=head2 mouseaction
$SVG->mouseaction(%attributes)
Sets/Adds mouse action definitions for tag
=cut
sub mouseaction {
my ( $self, %attrs ) = @_;
$self->{mouseaction} = $self->{mouseaction} || {};
foreach my $k ( keys %attrs ) {
$self->{mouseaction}->{$k} = $attrs{$k};
}
return $self;
}
=pod
$SVG->attrib($name, $value)
Sets/Adds attributes of an element.
Retrieve an attribute:
$svg->attrib($name);
Set a scalar attribute:
$SVG->attrib $name, $value
Set a list attribute:
$SVG->attrib $name, \@value
Set a hash attribute (i.e. style definitions):
$SVG->attrib $name, \%value
Remove an attribute:
$svg->attrib($name,undef);
B<Aliases:> attr attribute
=cut
sub attrib {
my ( $self, $name, $val ) = @_;
#verify that the current id is unique. compain on exception
if ( $name eq "id" ) {
if ( $self->getElementByID($val) ) {
$self->error( $val => "ID already exists in document" );
return;
}
}
if ( not defined $val ) {
if ( scalar(@_) == 2 ) {
# two arguments only - retrieve
return $self->{$name};
} else {
# 3rd argument is undef - delete
delete $self->{$name};
}
} else {
# 3 defined arguments - set
$self->{$name} = $val;
}
return $self;
}
*attr = \&attrib;
*attribute = \&attrib;
=pod
=head2 cdata
$SVG->cdata($text)
Sets cdata to $text. SVG.pm allows you to set cdata for any tag. If the tag is
meant to be an empty tag, SVG.pm will not complain, but the rendering agent will
fail. In the SVG DTD, cdata is generally only meant for adding text or script
content.
B<Example:>
$SVG->text(
style => {
'font' => 'Arial',
'font-size' => 20
})->cdata('SVG.pm is a perl module on CPAN!');
my $text = $SVG->text(style=>{'font'=>'Arial','font-size'=>20});
$text->cdata('SVG.pm is a perl module on CPAN!');
B<Result:>
E<lt>text style="font: Arial; font-size: 20" E<gt>SVG.pm is a perl module on CPAN!E<lt>/text E<gt>
SEE ALSO:
L<"CDATA"> L<"desc">, L<"title">, L<"text">, L<"script">.
=cut
sub cdata {
my ( $self, @txt ) = @_;
$self->{ -cdata } = join( ' ', @txt );
return ($self);
}
=pod
=head2 CDATA
$script = $SVG->script();
$script->CDATA($text);
Generates a <![CDATA[ ... ]]> tag with the contents of $text rendered exactly as supplied. SVG.pm allows you to set cdata for any tag. If the tag is
meant to be an empty tag, SVG.pm will not complain, but the rendering agent will
fail. In the SVG DTD, cdata is generally only meant for adding text or script
content.
B<Example:>
my $text = qq§
var SVGDoc;
var groups = new Array();
var last_group;
/*****
*
* init
*
* Find this SVG's document element
* Define members of each group by id
*
*****/
function init(e) {
SVGDoc = e.getTarget().getOwnerDocument();
append_group(1, 4, 6); // group 0
append_group(5, 4, 3); // group 1
append_group(2, 3); // group 2
}§;
$SVG->script()->CDATA($text);
B<Result:>
E<lt>script E<gt>
<gt>![CDATA[
var SVGDoc;
var groups = new Array();
var last_group;
/*****
*
* init
*
* Find this SVG's document element
* Define members of each group by id
*
*****/
function init(e) {
SVGDoc = e.getTarget().getOwnerDocument();
append_group(1, 4, 6); // group 0
append_group(5, 4, 3); // group 1
append_group(2, 3); // group 2
}
]]E<gt>
SEE ALSO:
L<"cdata">, L<"script">.
=cut
sub CDATA {
my ( $self, @txt ) = @_;
$self->{ -CDATA } = join( '\n', @txt );
return ($self);
}
sub cdata_noxmlesc {
my ( $self, @txt ) = @_;
$self->{ -cdata_noxmlesc } = join( '\n', @txt );
return ($self);
}
=pod
=head2 filter
$tag = $SVG->filter(%attributes)
Generate a filter. Filter elements contain L<"fe"> filter sub-elements.
B<Example:>
my $filter = $SVG->filter(
filterUnits=>"objectBoundingBox",
x=>"-10%",
y=>"-10%",
width=>"150%",
height=>"150%",
filterUnits=>'objectBoundingBox'
);
$filter->fe();
SEE ALSO:
L<"fe">.
=cut
sub filter {
my ( $self, %attrs ) = @_;
return $self->tag( 'filter', %attrs );
}
=pod
=head2 fe
$tag = $SVG->fe(-type=>'type', %attributes)
Generate a filter sub-element. Must be a child of a L<"filter"> element.
B<Example:>
my $fe = $SVG->fe(
-type => 'diffuselighting' # required - element name in lower case omiting 'fe'
id => 'filter_1',
style => {
'font' => [ qw(Arial Helvetica sans) ],
'font-size' => 10,
'fill' => 'red',
},
transform => 'rotate(-45)'
);
Note that the following filter elements are currently supported:
Also note that the elelemts are defined in lower case in the module, but as of version 2.441, any case combination is allowed.
=head2 * feBlend
=head2 * feColorMatrix
=head2 * feComponentTransfer
=head2 * feComposite
=head2 * feConvolveMatrix
=head2 * feDiffuseLighting
=head2 * feDisplacementMap
=head2 * feDistantLight
=head2 * feFlood
=head2 * feFuncA
=head2 * feFuncB
=head2 * feFuncG
=head2 * feFuncR
=head2 * feGaussianBlur
=head2 * feImage
=head2 * feMerge
=head2 * feMergeNode
=head2 * feMorphology
=head2 * feOffset
=head2 * fePointLight
=head2 * feSpecularLighting
=head2 * feSpotLight
=head2 * feTile
=head2 * feTurbulence
SEE ALSO:
L<"filter">.
=cut
sub fe {
my ( $self, %attrs ) = @_;
return 0 unless ( $attrs{'-type'} );
my %allowed = (
blend => 'feBlend',
colormatrix => 'feColorMatrix',
componenttrans => 'feComponentTrans',
Componenttrans => 'feComponentTrans',
composite => 'feComposite',
convolvematrix => 'feConvolveMatrix',
diffuselighting => 'feDiffuseLighting',
displacementmap => 'feDisplacementMap',
distantlight => 'feDistantLight',
flood => 'feFlood',
funca => 'feFuncA',
funcb => 'feFuncB',
funcg => 'feFuncG',
funcr => 'feFuncR',
gaussianblur => 'feGaussianBlur',
image => 'feImage',
merge => 'feMerge',
mergenode => 'feMergeNode',
morphology => 'feMorphology',
offset => 'feOffset',
pointlight => 'fePointLight',
specularlighting => 'feSpecularLighting',
spotlight => 'feSpotLight',
tile => 'feTile',
turbulence => 'feTurbulence'
);
my $key = lc( $attrs{'-type'} );
my $fe_name = $allowed{ lc($key) } || 'error:illegal_filter_element';
delete $attrs{'-type'};
return $self->tag( $fe_name, %attrs );
}
=pod
=head2 pattern
$tag = $SVG->pattern(%attributes)
Define a pattern for later reference by url.
B<Example:>
my $pattern = $SVG->pattern(
id => "Argyle_1",
width => "50",
height => "50",
patternUnits => "userSpaceOnUse",
patternContentUnits => "userSpaceOnUse"
);
=cut
sub pattern {
my ( $self, %attrs ) = @_;
return $self->tag( 'pattern', %attrs );
}
=pod
=head2 set
$tag = $SVG->set(%attributes)
Set a definition for an SVG object in one section, to be referenced in other
sections as needed.
B<Example:>
my $set = $SVG->set(
id => "Argyle_1",
width => "50",
height => "50",
patternUnits => "userSpaceOnUse",
patternContentUnits => "userSpaceOnUse"
);
=cut
sub set {
my ( $self, %attrs ) = @_;
return $self->tag( 'set', %attrs );
}
=pod
=head2 stop
$tag = $SVG->stop(%attributes)
Define a stop boundary for L<"gradient">
B<Example:>
my $pattern = $SVG->stop(
id => "Argyle_1",
width => "50",
height => "50",
patternUnits => "userSpaceOnUse",
patternContentUnits => "userSpaceOnUse"
);
=cut
sub stop {
my ( $self, %attrs ) = @_;
return $self->tag( 'stop', %attrs );
}
=pod
$tag = $SVG->gradient(%attributes)
Define a color gradient. Can be of type B<linear> or B<radial>
B<Example:>
my $gradient = $SVG->gradient(
-type => "linear",
id => "gradient_1"
);
=cut
sub gradient {
my ( $self, %attrs ) = @_;
my $type = $attrs{'-type'} || 'linear';
unless ( $type =~ /^(linear|radial)$/ ) {
$type = 'linear';
}
delete $attrs{'-type'};
return $self->tag( $type . 'Gradient', %attrs );
}
=pod
=head1 GENERIC ELEMENT METHODS
The following elements are generically supported by SVG:
=head2 * altGlyph
=head2 * altGlyphDef
=head2 * altGlyphItem
=head2 * clipPath
=head2 * color-profile
=head2 * cursor
=head2 * definition-src
=head2 * font-face-format
=head2 * font-face-name
=head2 * font-face-src
=head2 * font-face-url
=head2 * foreignObject
=head2 * glyph
=head2 * glyphRef
=head2 * hkern
=head2 * marker
=head2 * mask
=head2 * metadata
=head2 * missing-glyph
=head2 * mpath
=head2 * switch
=head2 * symbol
=head2 * tref
=head2 * view
=head2 * vkern
See e.g. L<"pattern"> for an example of the use of these methods.
=cut
#-------------------------------------------------------------------------------
# Internal methods
sub error {
my ( $self, $command, $error ) = @_;
if ( $self->{-docref}->{-raiseerror} ) {
die "$command: $error\n";
} elsif ( $self->{-docref}->{-printerror} ) {
print STDERR "$command: $error\n";
}
$self->{errors}{$command} = $error;
}
# This AUTOLOAD method is activated when '-auto' is passed to SVG.pm
sub autoload {
my $self = shift;
my ( $package, $sub ) = ( $AUTOLOAD =~ /(.*)::([^:]+)$/ );
if ( $sub eq 'DESTROY' ) {
return $self->release();
} else {
# the import routine may call us with a tag name involving '-'s
my $tag = $sub;
$sub =~ tr/-/_/;
# N.B.: The \ on \@_ makes sure that the incoming arguments are
# used and not the ones passed when the subroutine was created.
# eval "sub $package\:\:$sub (\$;\@) { return shift->tag('$tag',\@_) }";
#per rt.perl.org comment by slaven.
if (!$package->can($sub)) {
## no critic (TestingAndDebugging::ProhibitNoStrict)
no strict 'refs';
*{$package.'::'.$sub} = sub { return shift->tag($tag, @_) };
}
return $self->$sub(@_) if $self;
}
}
#-------------------------------------------------------------------------------
# GD Routines
sub colorAllocate {
my ( $self, $red, $green, $blue ) = @_;
return 'rgb(' . int($red) . ',' . int($green) . ',' . int($blue) . ')';
}
#-------------------------------------------------------------------------------
1;
|