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);
34
35my $y=$svg->group( id=>'group_generated_group',style=>{ stroke=>'red', fill=>'green' });
36
37my $z=$svg->tag('g',  id=>'tag_generated_group',style=>{ stroke=>'red', fill=>'black' });
38
39
40my $ya = $y -> anchor(
41		-href   => 'http://somewhere.org/some/line.html',
42		-target => 'new_window_0');
43
44
45my $line_transform = 'matrix(0.774447 0.760459 0 0.924674 357.792 -428.792)';
46
47my $line = $svg->line(id=>'l1',x1=>(rand()*$width+5),
48          y1=>(rand()*$height+5),
49          x2=>(rand()*$width-5),
50          y2=>(rand()*$height-5),
51          style=>&obj_style,);
52
53#---------
54foreach  (1..&round_up(rand(20))) {
55    my $myX = $width-rand(2*$width);
56    my $myY = $height-rand(2*$height);
57
58    my $rect = $y->rectangle (x=>$width/2,
59                   y=>$height/2,
60                   width=>(50+50*rand()),
61                   height=>(50+50*rand()),
62                   rx=>20*rand(),
63                   ry=>20*rand(),
64                   id=>'rect_1',
65                   style=>&obj_style);
66
67    $rect->animate(attributeName=>'transform',
68                attributeType=>'XML',
69                from=>'0 0',
70                to=>$myX.' '.$myY,
71                dur=>&round_up(rand(20),2).'s',
72                repeatCount=>&round_up(rand(30)),
73                restart=>'always',
74                #-method=>'transform',
75                );
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
178;
179
180exit;
181
182
183#################
184# Subroutine to round up the value of a number or of a text representation of number
185#
186sub round_up {
187    my ($x, $precision) = shift;
188    $x =~ s/^\s+//g;
189    $x =~ s/\s+$//g;
190    $x =~ s/,//g;
191
192    my $y;
193    $precision = 0 unless $precision;
194    ($x, $y) =  split( /\./, $x) if $x =~ /\./;
195    my $y1 = substr($y, 0, $precision);
196    my $y2 = substr($y, $precision, 1);
197
198    if ($y2 >= 5) {
199        $precision?$y1++:$x++;
200    }
201
202    return "$x$y1";
203
204} # sub round_val
205
206sub obj_style {
207
208    my $style = {'stroke-miterlimit'=>(4*rand()),
209          'stroke-linejoin'=>'miter',
210          'stroke-linecap'=>'round',
211          'stroke-width'=>(0.1+0.5*rand()),
212          'stroke-opacity'=>(0.5+0.5*rand()),
213          'stroke'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')',
214          'fill-opacity'=>(0.5+0.5*rand()),
215          'fill'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')',
216          'opacity'=>(0.5+0.5*rand()) };
217
218    return $style;
219
220}
221
222sub text_style {
223
224    my $style = {'font-family'=>'Arial',
225          'font-size'=>8+5*rand(),
226          'stroke-width'=>1+2*rand(),
227          'stroke-opacity'=>(0.2+0.5*rand()),
228          'stroke'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')',
229          'fill-opacity'=>1,
230          'fill'=>'rgb('.255*round_up(rand()).','.255*round_up(rand()).','.255*round_up(rand()).')',
231          'opacity'=>(0.5+0.5*rand()) };
232
233    return $style;
234
235}
236
237#---------
238