SpriteBuilder/Sprite.pm

filter: Statementstmt 66.6% 86 / 129 Branchbran 26.5% 17 / 64 Conditioncond 26.3% 10 / 38 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 86.3% 19 / 22 Podpod 15.3% 2 / 13 totaltotal 47.3% 134 / 283 CC 48 SCAR 34.1
CC
48
Cov
49%
process_repeated_images
100.6
allocate_space
52.3
search_images
29.1
CRAP 30.4
« SpriteBuilder.pm ↑ Summary SpriteBuilder/SpriteItem.pm »
1 package CSS::SpriteBuilder::Sprite;
2  
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use File::Glob ":glob";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use CSS::SpriteBuilder::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use CSS::SpriteBuilder::SpriteItem;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use base 'CSS::SpriteBuilder::ImageDriver::Auto';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub max { $_[ $_[0] < $_[1] ] }
Subroutine max: not called CC=1 SCAR=6.9
Pod undocumented
13  
14 3 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
15 3 my ($class, @args) = @_;
16  
17 3 my $self = $class->SUPER::new(
18 source_dir => undef,
19 source_images => undef,
20 target_file => undef,
21  
22 image_quality => DEF_IMAGE_QUALITY,
23 max_image_size => DEF_MAX_IMAGE_SIZE,
24 max_image_width => DEF_MAX_IMAGE_WIDTH,
25 max_image_height => DEF_MAX_IMAGE_HEIGHT,
26 max_sprite_width => DEF_MAX_SPRITE_WIDTH,
27 max_sprite_height => DEF_MAX_SPRITE_HEIGHT,
28 margin => DEF_MARGIN,
29 transparent_color => undef,
30 is_background => 0,
31 layout => DEF_LAYOUT,
32 css_selector_prefix => DEF_CSS_SELECTOR_PREFIX,
33 css_url_prefix => '',
34 is_add_timestamp => 1,
35  
36 _sprite_no => 1,
37 _repeated_images => [],
38 _sprites => {},
39 _x => 0,
40 _y => 0,
41  
42 @args,
43 );
44  
45 3 ▶ unless ( grep { $_ eq $self->{layout} } @{ LAYOUT_LIST() } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $self->{"layout"};} @{["packed","horizontal","vertical"];}) 0 3 3
46 0 die "Invalid 'layout' parameter: $self->{layout}";
47 }
48  
49 3 return $self;
50 }
51  
52 6 ▶sub target_file { sprintf( $_[0]->{target_file}, $_[0]->{_sprite_no} ) }
Subroutine target_file: called CC=1 SCAR=0.0
Pod undocumented
53  
54 0 ▶sub _init {
Subroutine _init: not called CC=3 SCAR=24.8
55 0 my ($self, $image) = @_;
56  
57 0 ▶ die "The 'target_file' parameter is not defined" unless $self->{target_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"target_file"} 0 0 0
58 0 ▶ die "The 'source_images' parameter is not defined" unless $self->{source_images};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"source_images"} 0 0 0
59  
60 0 $self->SUPER::_init($image);
61  
62 0 $self->{_x} = $self->{_y} = 0;
63  
64 0 return;
65 }
66  
67 3 ▶sub build {
Subroutine build: called CC=2 SCAR=6.9
Pod undocumented
68 3 my ($self) = @_;
69  
70 3 my $images = $self->search_images();
71 3 foreach my $image (@$images) {
72 3 $self->process_image($image);
73 }
74  
75 3 $self->write();
76  
77 3 return $self->{_sprites};
78 }
79  
80 3 ▶sub search_images {
Subroutine search_images: called CC=14 SCAR=33.7
Pod undocumented
81 3 my ($self) = @_;
82  
83 3 my $save_dir;
84 3 ▶ if ( $self->{source_dir} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"source_dir"}) 3 0 3
85 3 $save_dir = Cwd::getcwd();
86 3 ▶ chdir $self->{source_dir} or die "Cannot chdir to $self->{source_dir}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $self->{"source_dir"} or die qq[Cannot chdir to $self->{"source_dir"}: $!\n] 3 0 3
87 }
88  
89 my @images = grep {
90 ( !$self->{max_image_size} || -s $_->source_file() <= $self->{max_image_size} )
91 && ( !$self->{max_image_width} || $_->width() <= $self->{max_image_width} )
92 && ( !$self->{max_image_height} || $_->height() <= $self->{max_image_height} )
93 && ( $_->width() <= $self->{max_sprite_width} && $_->height <= $self->{max_sprite_height}
94 3 ▶ or die "The image resolution is bigger than maximum configured value for '". $_->source_file() ."'"
Condition34%8/23
(!$self->{'max_image_size'} || -s $_->source_file <= $self->{'max_image_size'}) && (!$self->{'max_image_width'} || $_->width <= $self->{'max_image_width'}) && (!$self->{'max_image_height'} || $_->height <= $self->{'max_image_height'}) && ($_->width <= $self->{'max_sprite_width'} && $_->height <= $self->{'max_sprite_height'} || die(q[The image resolution is bigger than maximum configured value for '] . $_->source_file . q[']))
!ll&&!rl&&r
003
!$self->{"max_image_size"} || -s $_->source_file <= $self->{"max_image_size"} and !$self->{"max_image_width"} || $_->width <= $self->{"max_image_width"} and !$self->{"max_image_height"} || $_->height <= $self->{"max_image_height"}
!ll&&!rl&&r
003
!$self->{"max_image_size"} || -s $_->source_file <= $self->{"max_image_size"} and !$self->{"max_image_width"} || $_->width <= $self->{"max_image_width"}
!ll&&!rl&&r
003
not $self->{"max_image_size"} or -s $_->source_file <= $self->{"max_image_size"}
l!l&&r!l&&!r
030
not $self->{"max_image_width"} or $_->width <= $self->{"max_image_width"}
l!l&&r!l&&!r
030
not $self->{"max_image_height"} or $_->height <= $self->{"max_image_height"}
l!l&&r!l&&!r
030
$_->width <= $self->{"max_sprite_width"} and $_->height <= $self->{"max_sprite_height"} or die q[The image resolution is bigger than maximum configured value for '] . $_->source_file . q[']
l!l
30
$_->width <= $self->{"max_sprite_width"} and $_->height <= $self->{"max_sprite_height"}
!ll&&!rl&&r
003
MC/DC0%0/8
(!$self->{'max_image_size'} || -s $_->source_file <= $self->{'max_image_size'}) && (!$self->{'max_image_width'} || $_->width <= $self->{'max_image_width'}) && (!$self->{'max_image_height'} || $_->height <= $self->{'max_image_height'}) && ($_->width <= $self->{'max_sprite_width'} && $_->height <= $self->{'max_sprite_height'} || die(q[The image resolution is bigger than maximum configured value for '] . $_->source_file . q[']))
not $self->{"max_image_size"} -s $_->source_file <= $self->{"max_image_size"} not $self->{"max_image_width"} $_->width <= $self->{"max_image_width"} not $self->{"max_image_height"} $_->height <= $self->{"max_image_height"} $_->width <= $self->{"max_sprite_width"} $_->height <= $self->{"max_sprite_height"}
Truth table3%1/31
not(A) or B and not(C) or D and not(E) or F && G and H
A= not $self->{"max_image_size"}B= -s $_->source_file <= $self->{"max_image_size"}C= not $self->{"max_image_width"}D= $_->width <= $self->{"max_image_width"}E= not $self->{"max_image_height"}F= $_->height <= $self->{"max_image_height"}G= $_->width <= $self->{"max_sprite_width"}H= $_->height <= $self->{"max_sprite_height"}
ABCDEFGHresult
00XXXXXX0
1X00XXXX0
0100XXXX0
1X1X00XX0
1X0100XX0
011X00XX0
010100XX0
1X1X1X0X0
1X1X1X100
1X1X010X0
1X1X01100
1X011X0X0
1X011X100
1X01010X0
1X0101100
011X1X0X0
011X1X100
011X010X0
011X01100
01011X0X0
01011X100
0101010X0
010101100
1X1X1X111
1X1X01111
1X011X111
1X0101111
011X1X111
011X01111
01011X111
010101111
95 )
96 } map {
97 3 my $image_opts = $_;
98 map {
99 3 CSS::SpriteBuilder::SpriteItem->new(
100 %$image_opts,
101 source_file => $_,
102 )
103 3 } bsd_glob( $image_opts->{file}, File::Glob::GLOB_ERR );
104 3 } @{ $self->{source_images} };
105  
106 3 ▶ if ($save_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save_dir) 3 0 3
107 3 ▶ chdir $save_dir or die "Cannot chdir to $save_dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $save_dir or die "Cannot chdir to ${save_dir}: $!\n" 3 0 3
108 }
109  
110 3 ▶ return wantarray ? @images: \@images;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
111 }
112  
113 3 ▶sub process_image {
Subroutine process_image: called CC=3 SCAR=11.6
Pod undocumented
114 3 my ($self, $image) = @_;
115  
116 3 $self->allocate_space($image);
117  
118 3 $self->align_image($image);
119  
120 3 $self->save_info($image);
121  
122 $self->save_repeated_images($image)
123 3 ▶ if $self->{layout} ne PACKED_LAYOUT && $image->{is_repeat};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"layout"} ne "packed" and $image->{"is_repeat"} 0 3 3
Condition33%1/3
$self->{"layout"} ne "packed" and $image->{"is_repeat"}
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"layout"} ne "packed" and $image->{"is_repeat"}
$self->{"layout"} ne "packed" $image->{"is_repeat"}
Truth table33%1/3
A and B
A= $self->{"layout"} ne "packed"B= $image->{"is_repeat"}
ABresult
0X0
100
111
124  
125 3 $self->post_process($image);
126 }
127  
128 3 ▶sub align_image {
Subroutine align_image: called CC=3 SCAR=15.0
Pod undocumented
129 3 my ($self, $image) = @_;
130  
131 3 ▶ if ( $self->is_blank() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_blank) { } 3 0 3
132 3 $self->reset($image);
133 }
134 else {
135 0 ▶ $self->{_x} += $self->{margin} if $self->{_x};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_x"} 0 0 0
136  
137 $self->extent(
138 max($self->width(), $self->{_x} + $image->width() ),
139 0 max($self->height(), $self->{_y} + $image->height()),
140 );
141  
142 0 $self->composite( $image, $self->{_x}, $self->{_y} );
143 }
144  
145 3 return;
146 }
147  
148 3 ▶sub allocate_space {
Subroutine allocate_space: called CC=8 SCAR=39.6
Pod undocumented
149 3 my ($self, $image) = @_;
150  
151 3 ▶ return if $self->is_blank();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_blank 3 0 3
152  
153 0 ▶ my $x_margin = $self->{_x} == 0 ? 0 : $self->{margin};
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_x'} == 0 ? : 0 0 0
154  
155 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"layout"} eq "vertical" or $self->{"_x"} + $image->width + $x_margin > $self->{"max_sprite_width"}) 0 0 0
Condition0%0/3
$self->{"layout"} eq "vertical" or $self->{"_x"} + $image->width + $x_margin > $self->{"max_sprite_width"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"layout"} eq "vertical" or $self->{"_x"} + $image->width + $x_margin > $self->{"max_sprite_width"}
$self->{"layout"} eq "vertical" $self->{"_x"} + $image->width + $x_margin > $self->{"max_sprite_width"}
Truth table0%0/3
A or B
A= $self->{"layout"} eq "vertical"B= $self->{"_x"} + $image->width + $x_margin > $self->{"max_sprite_width"}
ABresult
1X1
011
000
156 $self->{layout} eq VERTICAL_LAYOUT
157 || ($self->{_x} + $image->width() + $x_margin) > $self->{max_sprite_width}
158 ) {
159 0 $self->{_x} = 0;
160 0 $self->{_y} = $self->height() + $self->{margin};
161 }
162  
163 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_y"} > 0 and $self->{"layout"} eq "horizontal" || $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}) 0 0 0
Condition0%0/6
$self->{"_y"} > 0 and $self->{"layout"} eq "horizontal" || $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}
!ll&&!rl&&r
000
$self->{"layout"} eq "horizontal" or $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->{"_y"} > 0 and $self->{"layout"} eq "horizontal" || $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}
$self->{"_y"} > 0 $self->{"layout"} eq "horizontal" $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}
Truth table0%0/4
A and B or C
A= $self->{"_y"} > 0B= $self->{"layout"} eq "horizontal"C= $self->{"_y"} + $image->height + $self->{"margin"} > $self->{"max_sprite_height"}
ABCresult
0XX0
1000
11X1
1011
164 $self->{_y} > 0 && (
165 $self->{layout} eq HORIZONTAL_LAYOUT
166 || ($self->{_y} + $image->height() + $self->{margin}) > $self->{max_sprite_height}
167 )
168 ) {
169 0 $self->write();
170 }
171  
172 0 return;
173 }
174  
175 3 ▶sub save_info {
Subroutine save_info: called CC=7 SCAR=23.2
Pod undocumented
176 3 my ($self, $image) = @_;
177  
178 3 my (undef, undef, $filename) = File::Spec->splitpath( $self->target_file() );
179 3 my $sprite_image = $self->{css_url_prefix} . $filename;
180  
181 3 ▶ $sprite_image .= '?' . time() if $self->{is_add_timestamp};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_add_timestamp"} 0 3 3
182  
183 3 ▶ push @{ $self->{_sprites}{$sprite_image} }, {
Branch: 4 branches
BranchTrueFalseTotal
(defined $image->is_background ? $image->is_background : $self->{'is_background'}) ? : 0 3 3
defined $image->is_background ? : 0 3 3
$self->{'layout'} ne 'packed' && $image->is_repeat ? : 0 3 3
$self->{'layout'} eq 'horizontal' ? : 0 0 0
Condition33%1/3
$self->{"layout"} ne "packed" and $image->is_repeat
!ll&&!rl&&r
300
MC/DC0%0/2
$self->{"layout"} ne "packed" and $image->is_repeat
$self->{"layout"} ne "packed" $image->is_repeat
Truth table33%1/3
A and B
A= $self->{"layout"} ne "packed"B= $image->is_repeat
ABresult
0X0
100
111
184 image => $image->source_file(),
185 width => $image->width(),
186 height => $image->height(),
187 x => $self->{_x},
188 y => $self->{_y},
189 selector => $image->get_css_selector( $self->{css_selector_prefix} ),
190 is_background => (
191 defined $image->is_background()
192 ? $image->is_background()
193 : $self->{is_background}
194 ) ? 1 : 0,
195 repeat => $self->{layout} ne PACKED_LAYOUT && $image->is_repeat()
196 3 ? $self->{layout} eq HORIZONTAL_LAYOUT ? 'y' : 'x'
197 : 'no'
198 ,
199 };
200  
201 3 return;
202 }
203  
204 0 ▶sub save_repeated_images {
Subroutine save_repeated_images: not called CC=1 SCAR=6.9
Pod undocumented
205 0 my ($self, $image) = @_;
206  
207 0 push @{ $self->{_repeated_images} }, {
208 image => $image,
209 x => $self->{_x},
210 y => $self->{_y},
211 0 };
212  
213 0 return;
214 }
215  
216 3 ▶sub post_process {
Subroutine post_process: called CC=2 SCAR=7.1
Pod undocumented
217 3 my ($self, $image) = @_;
218  
219 3 ▶ $self->{ $self->{layout} eq VERTICAL_LAYOUT ? '_y' : '_x' } += $image->width;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'layout'} eq 'vertical' ? : 0 3 3
220  
221 3 return;
222 }
223  
224 3 ▶sub write {
Subroutine write: called CC=3 SCAR=11.1
Pod documented
225 3 my ($self) = @_;
226  
227 3 ▶ return if $self->is_blank();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_blank 0 3 3
228  
229 3 $self->process_repeated_images();
230  
231 $self->set_transparent_color( $self->{transparent_color} )
232 3 ▶ if $self->{transparent_color};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"transparent_color"} 0 3 3
233  
234 3 $self->set_quality( $self->{image_quality} );
235  
236 3 $self->SUPER::write( $self->target_file() );
237  
238 3 $self->{_sprite_no}++;
239  
240 3 $self->reset();
241  
242 3 return;
243 }
244  
245 3 ▶sub process_repeated_images {
Subroutine process_repeated_images: called CC=11 SCAR=46.1
Pod undocumented
246 3 my ($self) = @_;
247  
248 3 ▶ return unless scalar @{ $self->{_repeated_images} };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$self->{"_repeated_images"};} 3 0 3
249  
250 0 ▶ my $meter = $self->{layout} eq VERTICAL_LAYOUT ? 'width' : 'height';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'layout'} eq 'vertical' ? : 0 0 0
251 0 my $original_sprite_size = $self->$meter();
252 0 ▶ my $max_sprite_size = $self->{ $self->{layout} eq VERTICAL_LAYOUT ? 'max_sprite_width' : 'max_sprite_height' };
Branch: 1 branch
BranchTrueFalseTotal
$self->{'layout'} eq 'vertical' ? : 0 0 0
253 0 my @images_size = reverse sort map { $_->{image}->$meter() } @{ $self->{_repeated_images} };
254 0 my $max_image_size = shift @images_size;
255  
256 0 ▶ for (
Branch: 2 branches
BranchTrueFalseTotal
$original_sprite_size % $max_image_size ? : 0 0 0
if ($k * $max_image_size <= $max_sprite_size) 0 0 0
257 my $k = int($original_sprite_size / $max_image_size) + ($original_sprite_size % $max_image_size ? 1 : 0);
258 $k * $max_image_size <= $max_sprite_size;
259 $k++
260 ) {
261 0 my $extented_sprite_size = $k * $max_image_size;
262 0 ▶ unless (grep { $extented_sprite_size % $_ } @images_size) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$extented_sprite_size % $_;} @images_size) 0 0 0
263 0 ▶ $self->extent(
Branch: 1 branch
BranchTrueFalseTotal
$meter eq 'width' ? : 0 0 0
264 $meter eq 'width'
265 ? ( $extented_sprite_size, $self->height() )
266 : ( $self->width() , $extented_sprite_size )
267 );
268  
269 0 foreach my $image_info (@{ $self->{_repeated_images} }) {
270 0 my $image = $image_info->{image};
271 0 my $size = $image->$meter();
272  
273 0 ▶ while (($image_info->{ $self->{layout} eq VERTICAL_LAYOUT ? 'x' : 'y' } += $size) < $extented_sprite_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (($$image_info{$self->{"layout"} eq "vertical" ? "x" : "y"} += $size) < $extented_sprite_size) 0 0 0
$self->{'layout'} eq 'vertical' ? : 0 0 0
274 $self->composite( $image, $image_info->{x}, $image_info->{y} )
275 0 }
276 }
277  
278 0 return;
279 }
280 }
281  
282 0 $self->{_repeated_images} = [];
283  
284 0 die "Can't process repeated images";
285 }
286  
287 4 1;
« SpriteBuilder.pm ↑ Summary SpriteBuilder/SpriteItem.pm »