A Slide-Show Presentation via SVG - Part 1 of Chapter 7 from Perl Graphics Programming (4/4)
[previous] |
Perl Graphics Programming, Chapter 7: Creating SVG with Perl
Now create the final, top-level SVG graphic. Add an image (if specified in the original template) to the upper left corner.
my $toplevel = new IO::Scalar;
$writer = XML::Writer->new(OUTPUT=>$toplevel);
$writer->setDataMode(1);
$writer->setDataIndent(2);
$writer->startTag('svg',
height => $height,
width => $width,
'xmlns:xlink'=>"https://www.w3.org/1999/xlink");
$writer->emptyTag('rect', # Draw a colored background
height => $height,
width => $width,
fill => '#EEEEEE');
if (defined($s->{'image'})) { # Add the image
$writer->startTag('image',
x => 20,
y => 20,
width => 100,
height => 100,
'xlink:href' => $s->{'image'});
$writer->endTag('image');
}
Add a scaled group that contains the text block SVG. We have the complete SVG code sitting in the $textblock
scalar; add it to the top-level SVG. We have to print it directly because the characters( )
method escapes all of the special characters.
$writer->startTag('g',
id => 'SlideBody',
transform => "translate(130, 20) scale($scale)");
print $toplevel $textblock;
$writer->endTag('g'); # End the group
Add the Next and Previous navigation buttons:
my $prev = $n-1;
my $next = $n+1;
unless ($prev < 1) { # Previous button
$writer->startTag('a',
'xlink:href' => "slide$prev.svg"
);
$writer->startTag('text',
transform => "translate(20, 380)",
style => 'font-size:72;fill:#000000');
print $toplevel "←"; # A Unicode left arrow
$writer->endTag('text');
$writer->endTag('a');
}
unless ($next > $max) { # Next button
$writer->startTag('a',
'xlink:href' => "slide$next.svg"
);
$writer->startTag('text',
transform => "translate(20, 360)",
style => 'font-size:72;fill:#000000');
print $toplevel "→"; # A Unicode right arrow
$writer->endTag('text');
$writer->endTag('a');
}
$writer->endTag('svg');
$writer->end( );
Finally, create the slide SVG in the specified subdirectory:
open(OUT, ">$dir/slide$n.svg") or
die "Couldn't open $dir/slide$n.svg!";
print OUT $toplevel;
close OUT;
}
The dereference( )
subroutine is needed because of the slightly inconsistent manner in which XML::Simple represents the XML tree. If the parameter is an array reference, dereference and return it. Otherwise, return the parameter unchanged.
sub dereference {
my $ref = shift;
if (ref($ref) eq 'ARRAY') {
return (@$ref);
} else {
return ($ref);
}
}
The next section deals with the specifics of the animation tags. [To be continued in part 2. -Ed.]
[previous] |
Created: February 12, 2003
Revised: February 12, 2003
URL: https://webreference.com/programming/perl/chap7/1/4.html