1 #!/usr/bin/perl -w
2 #
3 # svg_dropshadow
4 #
5 # Creates drop shadows for all svg elements specified by --id, or
6 # whole file if no ids are given.
7 #
8 # Authors: Daniel Goude (goude@dtek.chalmers.se)
9 #
11 use strict;
12 use warnings;
14 use File::Basename();
15 use lib File::Basename::dirname($0);
17 use SpSVG;
19 my $sp = new SpSVG;
21 # Set the script name, used when displaying --help
22 $sp->set_name($0);
24 # Set usage string (options are handled separately).
25 my $usage = <<EOF;
26 Creates drop shadows from svg group(s)
27 EOF
28 $sp->set_usage($usage);
30 # Set script specific options and description (used for --help)
31 # SpSVG will hasdle in/out files, and help
32 my @opt_vals = (
33 {
34 "opt" => "color=s",
35 "desc" => "Shadow color (default black)",
36 },
39 {
40 "opt" => "opacity=s",
41 "desc" => "Shadow offset (0-1, default 0.5)",
42 },
44 {
45 "opt" => "offset=s",
46 "desc" => "Shadow offset, default 10",
47 },
48 );
50 my %opts = $sp->get_opts(@opt_vals);
52 my $color = $opts{'color'} || 'black';
53 my $opacity = $opts{'opacity'} || '0.5';
54 my $offset= $opts{'offset'} || '10';
56 # Read input file (from --file or STDIN)
57 $sp->parse;
59 # Apply make_shadow to selected ids, or whole file
60 $sp->process_ids(\&make_shadow);
62 # Dump the file (to --output or STDOUT)
63 $sp->dump;
65 # That's it!
67 # make_shadow takes an svg fragment and returns named fragment
68 # with a shadow added
69 sub make_shadow {
70 my $element = shift;
72 # Duplicate element
73 my $shadow = $element;
75 # Set shadow color
76 $shadow =~ s/(stroke|fill):[^;]+;/$1:$color;/ig;
78 my $svg = <<EOF;
79 <svg:g id="fooz" style="opacity:$opacity;" transform="translate($offset,
80 $offset)">
81 $shadow
82 </svg:g>
83 $element
84 EOF
85 return $svg;
86 }