#!/usr/bin/perl -w
#
# svg_dropshadow
#
# Creates drop shadows for all svg elements specified by --id, or
# whole file if no ids are given.
#
# Authors: Daniel Goude (goude@dtek.chalmers.se)
#

use strict;
use warnings;

use File::Basename();
use lib File::Basename::dirname($0);

use SpSVG;

my $sp = new SpSVG;

# Set the script name, used when displaying --help
$sp->set_name($0);

# Set usage string (options are handled separately).
my $usage = <<EOF;
Creates drop shadows from svg group(s)
EOF
$sp->set_usage($usage);

# Set script specific options and description (used for --help)
# SpSVG will hasdle in/out files, and help
my @opt_vals = ( 
    {
        "opt" => "color=s",
        "desc" => "Shadow color (default black)",
    }, 
    

    {
        "opt" => "opacity=s",
        "desc" => "Shadow offset (0-1, default 0.5)",
    }, 
    
    {
        "opt" => "offset=s",
        "desc" => "Shadow offset, default 10",
    },
);

my %opts = $sp->get_opts(@opt_vals);

my $color = $opts{'color'} || 'black';
my $opacity = $opts{'opacity'} || '0.5';
my $offset= $opts{'offset'} || '10';

# Read input file (from --file or STDIN)
$sp->parse;

# Apply make_shadow to selected ids, or whole file
$sp->process_ids(\&make_shadow);

# Dump the file (to --output or STDOUT)
$sp->dump;

# That's it!

# make_shadow takes an svg fragment and returns named fragment 
# with a shadow added
sub make_shadow {
    my $element = shift;

    # Duplicate element
    my $shadow = $element;

    # Set shadow color
    $shadow =~ s/(stroke|fill):[^;]+;/$1:$color;/ig;

    my $svg = <<EOF;
        <svg:g id="fooz" style="opacity:$opacity;" transform="translate($offset,
$offset)">
            $shadow
        </svg:g>
        $element
EOF
    return $svg;
}


