| 1 | package CSS::SpriteBuilder; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use File::Spec; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use Scalar::Util qw(openhandle); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use CSS::SpriteBuilder::Constants; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 4 | ▶ | use CSS::SpriteBuilder::Sprite; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | 4 | our $VERSION = '0.03'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 3 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 13 | 3 | my ($class, @args) = @_; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 3 | my $self = bless { | |||||||||||||
| 16 | source_dir => undef, | ||||||||||||||
| 17 | output_dir => undef, | ||||||||||||||
| 18 | |||||||||||||||
| 19 | image_quality => DEF_IMAGE_QUALITY, | ||||||||||||||
| 20 | max_image_size => DEF_MAX_IMAGE_SIZE, | ||||||||||||||
| 21 | max_image_width => DEF_MAX_IMAGE_WIDTH, | ||||||||||||||
| 22 | max_image_height => DEF_MAX_IMAGE_HEIGHT, | ||||||||||||||
| 23 | max_sprite_width => DEF_MAX_SPRITE_WIDTH, | ||||||||||||||
| 24 | max_sprite_height => DEF_MAX_SPRITE_HEIGHT, | ||||||||||||||
| 25 | margin => DEF_MARGIN, | ||||||||||||||
| 26 | transparent_color => undef, | ||||||||||||||
| 27 | is_background => 0, | ||||||||||||||
| 28 | layout => DEF_LAYOUT, | ||||||||||||||
| 29 | css_selector_prefix => DEF_CSS_SELECTOR_PREFIX, | ||||||||||||||
| 30 | css_url_prefix => '', | ||||||||||||||
| 31 | is_add_timestamp => 1, | ||||||||||||||
| 32 | |||||||||||||||
| 33 | _sprites => {}, | ||||||||||||||
| 34 | |||||||||||||||
| 35 | @args, | ||||||||||||||
| 36 | }, $class; | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 3 | return $self; | |||||||||||||
| 39 | } | ||||||||||||||
| 40 | |||||||||||||||
| 41 | 3 | ▶ | sub build { | ||||||||||||
|
Subroutine
build: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 42 | 3 | my ($self, %args) = @_; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 3 | my $sprites; | |||||||||||||
| 45 | 3 | ▶ | if ( my $config = delete $args{config} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 0 | $sprites = $self->_parse_xml_config($config); | |||||||||||||
| 47 | } | ||||||||||||||
| 48 | else { | ||||||||||||||
| 49 | 3 | $sprites = delete $args{sprites}; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | 3 | foreach my $sprite_opts (@$sprites) { | |||||||||||||
| 53 | 3 | my $images = delete $sprite_opts->{images}; | |||||||||||||
| 54 | my $output_file = $self->{output_dir} | ||||||||||||||
| 55 | ? File::Spec->catfile( $self->{output_dir}, delete $sprite_opts->{file} ) | ||||||||||||||
| 56 | : delete $sprite_opts->{file} | ||||||||||||||
| 57 | 3 | ▶ | ; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | |||||||||||||||
| 59 | my $sprite = CSS::SpriteBuilder::Sprite->new( | ||||||||||||||
| 60 | 3 | ( map { $_ => $self->{$_} } grep { exists $self->{$_} } @{ SPRITE_OPTS() } ), | |||||||||||||
| 61 | %$sprite_opts, | ||||||||||||||
| 62 | source_images => $images, | ||||||||||||||
| 63 | target_file => $output_file, | ||||||||||||||
| 64 | ); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 3 | my $sprites = $sprite->build(); | |||||||||||||
| 67 | 3 | $self->{_sprites} = {%{ $self->{_sprites} }, %$sprites}; | |||||||||||||
| 68 | } | ||||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | return $self->{_sprites}; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | ▶ | sub get_sprites_info { | ||||||||||||
|
Subroutine
get_sprites_info: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||||||
| 75 | 0 | return $self->{_sprites}; | |||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 1 | ▶ | sub write_css { | ||||||||||||
|
Subroutine
write_css: called CC=10 SCAR=26.6
Pod
documented
| |||||||||||||||
| 79 | 1 | my ($self, $filename) = @_; | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | my ($fh, $str); | |||||||||||||
| 82 | |||||||||||||||
| 83 | 1 | ▶ | if ($filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | 0 | ▶ | if ( openhandle($filename) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 0 | $fh = $filename; | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | else { | ||||||||||||||
| 88 | 0 | ▶ | open($fh, '>', $filename) or die "Can't to open file '$filename'!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | else { | ||||||||||||||
| 92 | 1 | $str = ''; | |||||||||||||
| 93 | 1 | ▶ | open($fh, '>', \$str) or die "Can't to create file handle!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | } | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 1 | ▶ | while (my ($sprite_image, $images) = each %{ $self->{_sprites} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 1 | my @selectors = map { $_->{selector} } @$images; | |||||||||||||
| 98 | 1 | print $fh sprintf( | |||||||||||||
| 99 | "%s{background-image: url('%s') !important;}\n", | ||||||||||||||
| 100 | join(',', @selectors), | ||||||||||||||
| 101 | $sprite_image, | ||||||||||||||
| 102 | ); | ||||||||||||||
| 103 | |||||||||||||||
| 104 | 1 | foreach my $image_info (@$images) { | |||||||||||||
| 105 | print $fh sprintf( | ||||||||||||||
| 106 | "%s{background-position:%dpx %dpx !important;", | ||||||||||||||
| 107 | $image_info->{selector}, | ||||||||||||||
| 108 | -$image_info->{x}, | ||||||||||||||
| 109 | -$image_info->{y}, | ||||||||||||||
| 110 | 1 | ); | |||||||||||||
| 111 | |||||||||||||||
| 112 | 1 | ▶ | if ( $image_info->{repeat} ne 'no' ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | print $fh sprintf( | ||||||||||||||
| 114 | "background-repeat:repeat-%s;", | ||||||||||||||
| 115 | $image_info->{repeat}, | ||||||||||||||
| 116 | 0 | ); | |||||||||||||
| 117 | } | ||||||||||||||
| 118 | |||||||||||||||
| 119 | 1 | ▶ | unless ( $image_info->{is_background} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 120 | print $fh sprintf( | ||||||||||||||
| 121 | "width:%dpx;height:%dpx;", | ||||||||||||||
| 122 | $image_info->{width}, | ||||||||||||||
| 123 | $image_info->{height}, | ||||||||||||||
| 124 | 1 | ); | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 1 | print $fh "}\n"; | |||||||||||||
| 128 | } | ||||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 1 | ▶ | unless ($filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 132 | 1 | close $fh; | |||||||||||||
| 133 | 1 | return $str; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | return; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 0 | ▶ | sub write_xml { | ||||||||||||
|
Subroutine
write_xml: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||
| 140 | 0 | my ($self, $filename) = @_; | |||||||||||||
| 141 | |||||||||||||||
| 142 | 0 | eval "require XML::LibXML"; | |||||||||||||
| 143 | 0 | ▶ | die "XML::LibXML module is required!" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | |||||||||||||||
| 145 | 0 | my $dom = XML::LibXML::Document->new(); | |||||||||||||
| 146 | 0 | my $root_el = $dom->createElement('root'); | |||||||||||||
| 147 | 0 | $dom->setDocumentElement($root_el); | |||||||||||||
| 148 | |||||||||||||||
| 149 | 0 | ▶ | while (my ($sprite_image, $images) = each %{ $self->{_sprites} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 0 | my $sprite_el = $dom->createElement('sprite'); | |||||||||||||
| 151 | 0 | $root_el->appendChild($sprite_el); | |||||||||||||
| 152 | 0 | $sprite_el->setAttribute('src', $sprite_image); | |||||||||||||
| 153 | |||||||||||||||
| 154 | 0 | foreach my $image (@$images) { | |||||||||||||
| 155 | 0 | my $image_el = $dom->createElement('image'); | |||||||||||||
| 156 | 0 | $sprite_el->appendChild($image_el); | |||||||||||||
| 157 | 0 | ▶ | while (my ($key, $value) = each %$image) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 0 | $image_el->setAttribute($key, $value); | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | } | ||||||||||||||
| 161 | } | ||||||||||||||
| 162 | |||||||||||||||
| 163 | 0 | ▶ | if ($filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 0 | ▶ | if ( openhandle($filename) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | 0 | return $dom->toFH($filename, 2); | |||||||||||||
| 166 | } | ||||||||||||||
| 167 | else { | ||||||||||||||
| 168 | 0 | return $dom->toFile($filename, 2); | |||||||||||||
| 169 | } | ||||||||||||||
| 170 | } | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | return $dom->toString(2); | |||||||||||||
| 173 | } | ||||||||||||||
| 174 | |||||||||||||||
| 175 | 0 | ▶ | sub write_html { | ||||||||||||
|
Subroutine
write_html: not called CC=12 SCAR=50.5
Pod
documented
| |||||||||||||||
| 176 | 0 | my ($self, $filename) = @_; | |||||||||||||
| 177 | |||||||||||||||
| 178 | 0 | my ($fh, $str); | |||||||||||||
| 179 | |||||||||||||||
| 180 | 0 | ▶ | if ($filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | 0 | ▶ | if ( openhandle($filename) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 182 | 0 | $fh = $filename; | |||||||||||||
| 183 | } | ||||||||||||||
| 184 | else { | ||||||||||||||
| 185 | 0 | ▶ | open($fh, '>', $filename) or die "Can't to open file '$filename'!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | } | ||||||||||||||
| 187 | } | ||||||||||||||
| 188 | else { | ||||||||||||||
| 189 | 0 | $str = ''; | |||||||||||||
| 190 | 0 | ▶ | open($fh, '>', \$str) or die "Can't to create file handle!"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 0 | print $fh <<HTML; | |||||||||||||
| 194 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | ||||||||||||||
| 195 | <html xmlns="http://www.w3.org/1999/xhtml"> | ||||||||||||||
| 196 | <head> | ||||||||||||||
| 197 | <title>CSS::SpriteBuilder generated page</title> | ||||||||||||||
| 198 | <style type="text/css"> | ||||||||||||||
| 199 | .container { | ||||||||||||||
| 200 | width: 800px; | ||||||||||||||
| 201 | margin: 0 auto; | ||||||||||||||
| 202 | text-align: center; | ||||||||||||||
| 203 | font-size: 12px; | ||||||||||||||
| 204 | } | ||||||||||||||
| 205 | table { | ||||||||||||||
| 206 | border-collapse: collapse; | ||||||||||||||
| 207 | width: 100%; | ||||||||||||||
| 208 | } | ||||||||||||||
| 209 | th, td { | ||||||||||||||
| 210 | border: 1px solid; | ||||||||||||||
| 211 | text-align: center; | ||||||||||||||
| 212 | padding: 8px; | ||||||||||||||
| 213 | } | ||||||||||||||
| 214 | .spr { | ||||||||||||||
| 215 | display: -moz-inline-stack; | ||||||||||||||
| 216 | display: inline-block; | ||||||||||||||
| 217 | zoom: 1; | ||||||||||||||
| 218 | *display: inline; | ||||||||||||||
| 219 | background-repeat: no-repeat; | ||||||||||||||
| 220 | } | ||||||||||||||
| 221 | HTML | ||||||||||||||
| 222 | |||||||||||||||
| 223 | 0 | $self->write_css($fh); | |||||||||||||
| 224 | |||||||||||||||
| 225 | 0 | print $fh <<HTML; | |||||||||||||
| 226 | </style> | ||||||||||||||
| 227 | </head> | ||||||||||||||
| 228 | <body> | ||||||||||||||
| 229 | <div class="container"> | ||||||||||||||
| 230 | <h3>CSS::SpriteBuilder generated page</h3> | ||||||||||||||
| 231 | <table> | ||||||||||||||
| 232 | <thead> | ||||||||||||||
| 233 | <tr> | ||||||||||||||
| 234 | <th>Image</th> | ||||||||||||||
| 235 | <th>CSS selector</th> | ||||||||||||||
| 236 | <th>Sprite</th> | ||||||||||||||
| 237 | <th>Pos</th> | ||||||||||||||
| 238 | </tr> | ||||||||||||||
| 239 | </thead> | ||||||||||||||
| 240 | <tbody> | ||||||||||||||
| 241 | HTML | ||||||||||||||
| 242 | |||||||||||||||
| 243 | 0 | ▶ | while (my ($sprite_image, $images) = each %{ $self->{_sprites} }) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 244 | 0 | ▶ | for (my $i = 0; $i < scalar @$images; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 245 | 0 | my $image_info = $images->[$i]; | |||||||||||||
| 246 | 0 | my $selector = $image_info->{selector}; | |||||||||||||
| 247 | |||||||||||||||
| 248 | 0 | ▶ | my $rule = $selector =~ /^#/ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 249 | ? 'class="spr" id="'. substr($selector, 1) .'"' | ||||||||||||||
| 250 | : 'class="spr '. substr($selector, 1) .'"' | ||||||||||||||
| 251 | ; | ||||||||||||||
| 252 | |||||||||||||||
| 253 | 0 | ▶ | if ($image_info->{repeat} eq 'x') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 254 | 0 | $rule .= ' style="width:'. $image_info->{width} * 3 .'px;"'; | |||||||||||||
| 255 | } | ||||||||||||||
| 256 | elsif ($image_info->{repeat} eq 'y') { | ||||||||||||||
| 257 | 0 | $rule .= ' style="height:'. $image_info->{height} * 3 .'px;"'; | |||||||||||||
| 258 | } | ||||||||||||||
| 259 | |||||||||||||||
| 260 | 0 | print $fh <<HTML; | |||||||||||||
| 261 | <tr> | ||||||||||||||
| 262 | <td> | ||||||||||||||
| 263 | <span $rule> </span><br/> | ||||||||||||||
| 264 | $image_info->{image}<br/> | ||||||||||||||
| 265 | $image_info->{width} x $image_info->{height} (repeat-$image_info->{repeat}) | ||||||||||||||
| 266 | </td> | ||||||||||||||
| 267 | <td>$selector</td> | ||||||||||||||
| 268 | HTML | ||||||||||||||
| 269 | 0 | ▶ | if ($i == 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 270 | 0 | my $rowspan = scalar @$images; | |||||||||||||
| 271 | 0 | print $fh <<HTML; | |||||||||||||
| 272 | <td rowspan="$rowspan"> | ||||||||||||||
| 273 | <img alt="" src="$sprite_image" /><br/> | ||||||||||||||
| 274 | $sprite_image | ||||||||||||||
| 275 | </td> | ||||||||||||||
| 276 | HTML | ||||||||||||||
| 277 | } | ||||||||||||||
| 278 | |||||||||||||||
| 279 | 0 | print $fh <<HTML; | |||||||||||||
| 280 | <td>$image_info->{x} x $image_info->{y}</td> | ||||||||||||||
| 281 | </tr> | ||||||||||||||
| 282 | HTML | ||||||||||||||
| 283 | } | ||||||||||||||
| 284 | } | ||||||||||||||
| 285 | |||||||||||||||
| 286 | 0 | print $fh <<HTML; | |||||||||||||
| 287 | </tbody> | ||||||||||||||
| 288 | </table> | ||||||||||||||
| 289 | </div> | ||||||||||||||
| 290 | </body> | ||||||||||||||
| 291 | </html> | ||||||||||||||
| 292 | HTML | ||||||||||||||
| 293 | |||||||||||||||
| 294 | 0 | ▶ | unless ($filename) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 295 | 0 | close $fh; | |||||||||||||
| 296 | 0 | return $str; | |||||||||||||
| 297 | } | ||||||||||||||
| 298 | |||||||||||||||
| 299 | 0 | return; | |||||||||||||
| 300 | } | ||||||||||||||
| 301 | |||||||||||||||
| 302 | 0 | ▶ | sub _parse_xml_config { | ||||||||||||
|
Subroutine
_parse_xml_config: not called CC=6 SCAR=37.4
| |||||||||||||||
| 303 | 0 | my ($self, $filename) = @_; | |||||||||||||
| 304 | |||||||||||||||
| 305 | 0 | eval "require XML::LibXML"; | |||||||||||||
| 306 | 0 | ▶ | die "XML::LibXML module is required!" if $@; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 307 | |||||||||||||||
| 308 | 0 | my $xml_parser = XML::LibXML->new(); | |||||||||||||
| 309 | |||||||||||||||
| 310 | 0 | my $dom; | |||||||||||||
| 311 | 0 | ▶ | if ( openhandle($filename) ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 312 | 0 | $dom = $xml_parser->parse_fh($filename); | |||||||||||||
| 313 | } | ||||||||||||||
| 314 | elsif ( $filename =~ /^</ ) { | ||||||||||||||
| 315 | 0 | $dom = $xml_parser->parse_string($filename); | |||||||||||||
| 316 | } | ||||||||||||||
| 317 | else { | ||||||||||||||
| 318 | 0 | $dom = $xml_parser->parse_file($filename); | |||||||||||||
| 319 | } | ||||||||||||||
| 320 | |||||||||||||||
| 321 | my %global_opts = ( | ||||||||||||||
| 322 | 0 | ( map { $_ => $self->{$_} } grep { exists $self->{$_} } @{ SPRITE_OPTS() } ), | |||||||||||||
| 323 | 0 | ( map { $_->nodeName() => $_->textContent() } $dom->findnodes("/root/global_opts/*") ), | |||||||||||||
| 324 | ); | ||||||||||||||
| 325 | |||||||||||||||
| 326 | 0 | my @sprites; | |||||||||||||
| 327 | 0 | foreach my $sprite_node ( $dom->findnodes("/root/sprites/sprite") ) { | |||||||||||||
| 328 | my %sprite_opts = ( | ||||||||||||||
| 329 | %global_opts, | ||||||||||||||
| 330 | 0 | map { $_->nodeName() => $_->textContent() } $sprite_node->findnodes("@*"), | |||||||||||||
| 331 | ); | ||||||||||||||
| 332 | |||||||||||||||
| 333 | my @images = map +{ | ||||||||||||||
| 334 | 0 | ( map { $_ => $sprite_opts{$_} } grep { exists $sprite_opts{$_} } @{ IMAGE_OPTS() } ), | |||||||||||||
| 335 | 0 | ( map { $_->nodeName() => $_->textContent() } $_->findnodes("@*") ), | |||||||||||||
| 336 | }, $sprite_node->findnodes('image'); | ||||||||||||||
| 337 | |||||||||||||||
| 338 | 0 | push @sprites, { | |||||||||||||
| 339 | %sprite_opts, | ||||||||||||||
| 340 | images => \@images, | ||||||||||||||
| 341 | }; | ||||||||||||||
| 342 | } | ||||||||||||||
| 343 | |||||||||||||||
| 344 | 0 | ▶ | return wantarray ? @sprites : \@sprites; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 345 | } | ||||||||||||||
| 346 | |||||||||||||||
| 347 | 4 | 1; | |||||||||||||
| 348 | __END__ | ||||||||||||||