1#!/usr/local/bin/perl -w 2 3use strict; 4use CGI; 5 6BEGIN { 7 push @INC , '../'; 8 push @INC , '../SVG'; 9} 10 11use SVG; 12 13my $VERSION = 3; 14 15#---------Create the CGI object which is required to handle the header 16my $p = CGI->new(); 17 18$| = 1; 19 20 21#---------print the header just before outputting to screen 22 23 24 25 26#--------- 27 28#---------Create the svg object 29 30my $height = $p->param('h') || 400; 31my $width = $p->param('w') || 800; 32 33my $svg= SVG->new(width=>$width,height=>$height,-inline=>1, 34 -namespace=>'abc'); 35 36my $y=$svg->group( id=>'group_generated_group',style=>{ stroke=>'red', fill=>'green' }); 37 38my $z=$svg->tag('g', id=>'tag_generated_group',style=>{ stroke=>'red', fill=>'black' }); 39 40 41my $ya = $y -> anchor( 42 -href => 'http://somewhere.org/some/line.html', 43 -target => 'new_window_0'); 44 45 46my $line_transform = 'matrix(0.774447 0.760459 0 0.924674 357.792 -428.792)'; 47 48my $line = $svg->line(id=>'l1',x1=>(rand()*$width+5), 49 y1=>(rand()*$height+5), 50 x2=>(rand()*$width-5), 51 y2=>(rand()*$height-5), 52 style=>&obj_style,); 53 54#--------- 55foreach (1..&round_up(rand(20))) { 56 my $myX = $width-rand(2*$width); 57 my $myY = $height-rand(2*$height); 58 59 my $rect = $y->rectangle (x=>$width/2, 60 y=>$height/2, 61 width=>(50+50*rand()), 62 height=>(50+50*rand()), 63 rx=>20*rand(), 64 ry=>20*rand(), 65 id=>'rect_1', 66 style=>&obj_style); 67 68 $rect->animate(attributeName=>'transform', 69 attributeType=>'XML', 70 from=>'0 0', 71 to=>$myX.' '.$myY, 72 dur=>&round_up(rand(20),2).'s', 73 repeatCount=>&round_up(rand(30)), 74 restart=>'always', 75 -method=>'Transform',); 76} 77my $a = $z -> anchor( 78 -href => 'http://somewhere.org/some/other/page.html', 79 -target => 'new_window_0', 80 id=>'anchor a'); 81 82my $a1 = $z -> anchor( 83 -href => '/index.html', 84 -target => 'new_window_1', 85 id=>'anchor a1'); 86 87my $a2 = $z -> anchor( 88 -href => '/svg/index.html', 89 -target => 'new_window_2', 90 id=>'anchor a2'); 91 92#--------- 93 94my $c; 95foreach (1..&round_up(rand(5))) { 96 97 $c= $a->circle(cx=>($width-20)*rand(), 98 cy=>($height-20)*rand(), 99 r=>100*rand(), 100 id=>'c1', 101 style=>&obj_style); 102 103 $c = $a1->circle(cx=>($width-20)*rand(), 104 cy=>($height-20)*rand(), 105 r=>100*rand(), 106 id=>'c2', 107 style=>&obj_style); 108} 109#--------- 110 111my $xv = [$width*rand(), $width*rand(), $width*rand(), $width*rand()]; 112 113my $yv = [$height*rand(), $height*rand(), $height*rand() ,$height*rand()]; 114 115my $points = $a->get_path(x=>$xv, 116 y=>$yv, 117 -type=>'polyline', 118 -closed=>'true',); 119 120 121$c = $a1->polyline (%$points, 122 id=>'pline1', 123 style=>&obj_style); 124 125#--------- 126 127$xv = [$width*rand(), $width*rand(), $width*rand(), $width*rand()]; 128 129$yv = [$height*rand(), $height*rand(), $height*rand() ,$height*rand()]; 130 131$points = $a->get_path(x=>$xv, 132 y=>$yv, 133 -type=>'polygon',); 134 135 136$c = $a->polygon (%$points, 137 id=>'pgon1', 138 style=>&obj_style); 139#--------- 140 141 142my $t=$a2->text(id=>'t1', 143 transform=>'rotate(-45)', 144 style=>&text_style); 145#--------- 146 147 148my $u=$a2->text(id=>'t3', 149 x=>$width/2*rand(), 150 y=>($height-80)*rand(), 151 transform=>'rotate('.(-2.5*5*rand()).')', 152 style=>&text_style); 153 154 155 156my $v=$a2->tag('text', 157 id=>'t5', 158 x=>$width/2*rand(), 159 y=>$height-40+5*rand(), 160 transform=>'rotate('.(-2.5*5*rand()).')', 161 style=>&text_style); 162 163my $w=$a2->text(id=>'t5', 164 x=>$width/2*rand(), 165 y=>$height-20+5*rand(), 166 transform=>'rotate('.(-2.5*5*rand()).')', 167 style=>&text_style); 168 169 170$t->cdata('Text generated using the high-level "text" tag'); 171$t->cdata('Courtesy of RO IT Systems GmbH'); 172$v->cdata('Text generated using the low-level "tag" tag'); 173$w->cdata('But what about inline SVG? Yes, we do that too'); 174$w->cdata('All this with SVG.pm? Wow.'); 175 176print $p->header('image/svg-xml'); 177print $svg->render(-inline=>1); 178 179exit; 180 181 182################# 183# Subroutine to round up the value of a number or of a text representation of number 184# 185sub round_up { 186 my ($x, $precision) = shift; 187 $x =~ s/^\s+//g; 188 $x =~ s/\s+$//g; 189 $x =~ s/,//g; 190 191 my $y; 192 $precision = 0 unless $precision; 193 ($x, $y) = split( /\./, $x) if $x =~ /\./; 194 my $y1 = substr($y, 0, $precision); 195 my $y2 = substr($y, $precision, 1); 196 197 if ($y2 >= 5) { 198 $precision?$y1++:$x++; 199 } 200 201 return "$x$y1"; 202 203} # sub round_val 204 205sub obj_style { 206 207 my $style = {'stroke-miterlimit'=>(4*rand()), 208 'stroke-linejoin'=>'miter', 209 'stroke-linecap'=>'round', 210 'stroke-width'=>(0.1+0.5*rand()), 211 'stroke-opacity'=>(0.5+0.5*rand()), 212 'stroke'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')', 213 'fill-opacity'=>(0.5+0.5*rand()), 214 'fill'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')', 215 'opacity'=>(0.5+0.5*rand()) }; 216 217 return $style; 218 219} 220 221sub text_style { 222 223 my $style = {'font-family'=>'Arial', 224 'font-size'=>8+5*rand(), 225 'stroke-width'=>1+2*rand(), 226 'stroke-opacity'=>(0.2+0.5*rand()), 227 'stroke'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')', 228 'fill-opacity'=>1, 229 'fill'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')', 230 'opacity'=>(0.5+0.5*rand()) }; 231 232 return $style; 233 234} 235 236#--------- 237