| 1 | package SVG::SpriteMaker; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.014000; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $VERSION = '0.001'; | |||||||||
| 8 | 1 | our @EXPORT = qw/make_sprite make_sprite_prefix/; | |||||||||
| 9 | 1 | our @EXPORT_OK = @EXPORT; | |||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use parent qw/Exporter/; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use re '/s'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use File::Basename; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 1 | ▶ | use SVG -indent => '', -elsep => ''; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use SVG::Parser; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | sub make_sprite { | ||||||||||
| 21 | 1 | ▶ | my ($prefix, @images) = @_; | ||||||||
|
Subroutine
make_sprite: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||
| 22 | my $sub = ref $prefix eq 'CODE' ? $prefix : sub { | ||||||||||
| 23 | 2 | ▶ | my $base = scalar fileparse $_[0], qr/[.].*/; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 24 | 2 | "$prefix-$base" | |||||||||
| 25 | 1 | ▶ | }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | 1 | my $sprite = SVG->new(-inline => 1); | |||||||||
| 27 | 1 | my $parser = SVG::Parser->new; | |||||||||
| 28 | 1 | @images = map {[ $sub->($_) => $parser->parse_file($_) ]} @images; | |||||||||
| 29 | 1 | my ($x, $mh) = (0, 0); | |||||||||
| 30 | |||||||||||
| 31 | 1 | for (@images) { | |||||||||
| 32 | 2 | my ($img, $doc) = @$_; | |||||||||
| 33 | 2 | my $svg = $doc->getFirstChild; | |||||||||
| 34 | 2 | ▶ | my ($w) = $svg->attr('width') =~ /([0-9.]*)/ or carp "Image $img has no width"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | ▶ | my ($h) = $svg->attr('height') =~ /([0-9.]*)/ or carp "Image $img has no height"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | 2 | ▶ | $mh = $h if $h > $mh; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 2 | $svg->attr(x => $x); | |||||||||
| 38 | 2 | $svg->attr(version => undef); | |||||||||
| 39 | 2 | my $view = $sprite->view(id => $img, viewBox => "$x 0 $w $h"); | |||||||||
| 40 | 2 | $x += $w + 5; | |||||||||
| 41 | 2 | $view->getParent->insertAfter($svg, $view); | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | # Keep a reference to the documents to prevent garbage collection | ||||||||||
| 45 | 1 | $sprite->{'--images'} = \@images; | |||||||||
| 46 | 1 | $sprite->getFirstChild->attr(viewBox => "0 0 $x $mh"); | |||||||||
| 47 | 1 | $sprite | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | 1; | |||||||||
| 51 | __END__ | ||||||||||