| 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.002'; | |||||||||||||||||||||||||||
| 8 | 1 | our @EXPORT = qw/make_sprite/; | |||||||||||||||||||||||||||
| 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 | 1 | ▶ | sub make_sprite { | ||||||||||||||||||||||||||
|
Subroutine
make_sprite: called CC=17 SCAR=28.8
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 1 | my ($prefix, @images) = @_; | |||||||||||||||||||||||||||
| 22 | 2 | ▶ | my $sub = ref $prefix eq 'CODE' ? $prefix : sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | my $base = scalar fileparse $_[0], qr/[.].*/; | |||||||||||||||||||||||||||
| 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 | 1 | my %ids = map { $_->[0] => 1 } @images; # start with image names | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 1 | for (@images) { | |||||||||||||||||||||||||||
| 33 | 2 | my ($img, $doc) = @$_; | |||||||||||||||||||||||||||
| 34 | 2 | my $svg = $doc->getFirstChild; | |||||||||||||||||||||||||||
| 35 | 2 | ▶ | my ($w) = $svg->attr('width') =~ /([0-9.]*)/ or carp "Image $img has no width"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my ($h) = $svg->attr('height') =~ /([0-9.]*)/ or carp "Image $img has no height"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | $mh = $h if $h > $mh; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 2 | $svg->attr(x => $x); | |||||||||||||||||||||||||||
| 39 | 2 | $svg->attr(version => undef); | |||||||||||||||||||||||||||
| 40 | 2 | my $view = $sprite->view(id => $img, viewBox => "$x 0 $w $h"); | |||||||||||||||||||||||||||
| 41 | 2 | $x += $w + 5; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 2 | my @all_elems = $svg->getElements; | |||||||||||||||||||||||||||
| 44 | 2 | my @duplicate_ids; | |||||||||||||||||||||||||||
| 45 | 2 | for my $elem (@all_elems) { | |||||||||||||||||||||||||||
| 46 | 4 | my $id = $elem->attr('id'); | |||||||||||||||||||||||||||
| 47 | 4 | ▶ | next unless $id; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | if ($ids{$id}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | 1 | push @duplicate_ids, $id; | |||||||||||||||||||||||||||
| 50 | } else { | ||||||||||||||||||||||||||||
| 51 | 1 | $ids{$id} = 1; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | warn <<"EOF" if @duplicate_ids && !$ENV{SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@duplicate_ids and not $ENV{"SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS"}
MC/DC0%0/2
@duplicate_ids and not $ENV{"SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS"}
@duplicate_ids
not $ENV{"SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS"}
Truth table66%2/3
A and not(B)
A= @duplicate_idsB= not $ENV{"SVG_SPRITEMAKER_NO_DUPLICATE_WARNINGS"}
| |||||||||||||||||||||||||||||
| 56 | Some IDs (@duplicate_ids) in $img also exist in previous images. | ||||||||||||||||||||||||||||
| 57 | Trying to fix automatically, but this might produce a broken SVG. | ||||||||||||||||||||||||||||
| 58 | Fix IDs manually to avoid incorrect output. | ||||||||||||||||||||||||||||
| 59 | EOF | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | for my $oid (@duplicate_ids) { | |||||||||||||||||||||||||||
| 62 | 1 | my $nid = $oid; | |||||||||||||||||||||||||||
| 63 | 1 | ▶ | $nid .= '_' while $ids{$nid}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 1 | $svg->getElementByID($oid)->attr(id => $nid); | |||||||||||||||||||||||||||
| 65 | 1 | for my $elem (@all_elems) { | |||||||||||||||||||||||||||
| 66 | 2 | my %attribs = %{$elem->getAttributes}; | |||||||||||||||||||||||||||
| 67 | 2 | for my $key (keys %attribs) { | |||||||||||||||||||||||||||
| 68 | 14 | ▶ | if ($attribs{$key} =~ /#$oid\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 0 | $attribs{$key} =~ s/#$oid\b/#$nid/g; | |||||||||||||||||||||||||||
| 70 | 0 | $elem->attr($key => $attribs{$key}); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if ($elem->cdata =~ /#$oid\b/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $elem->cdata($elem->cdata =~ s/#$oid\b/#$nid/gr); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | $view->getParent->insertAfter($svg, $view); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Keep a reference to the documents to prevent garbage collection | ||||||||||||||||||||||||||||
| 83 | 1 | $sprite->{'--images'} = \@images; | |||||||||||||||||||||||||||
| 84 | 1 | $sprite->getFirstChild->attr(viewBox => "0 0 $x $mh"); | |||||||||||||||||||||||||||
| 85 | 1 | $sprite | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 1 | 1; | |||||||||||||||||||||||||||
| 89 | __END__ | ||||||||||||||||||||||||||||