blib/lib/CSS/SpriteMaker/Layout/Packed.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 57 CC 5 SCAR 23.0
CC
5
Cov
0%
_layout_items
20.0
new
6.0
BEGIN
2.0
CRAP 10.0
1 package CSS::SpriteMaker::Layout::Packed;
2  
3 use strict;
4 use warnings;
5  
6 use base 'CSS::SpriteMaker::Layout';
7  
8 use CSS::SpriteMaker::Layout::Packed::Node;
9  
10 =head1 NAME
11  
12 CSS::SpriteMaker::Layout::Packed - Layout items trying to minimize the size of the resulting file.
13  
14 my $DirectoryBasedLayout = CSS::SpriteMaker::Layout::Packed->new(
15 {
16 "1" => {
17 width => 128,
18 height => 128,
19 pathname => '/full/path/to/file1.png',
20 parentdir => '/full/path/to',
21 },
22 ...
23 }
24 );
25  
26  
27 All items will be packed throughcontained in the same sub directory are cascaded on the same row of
28 the layout.
29  
30 Input hashref items must contain the following keys
31 for this layout to produce a result:
32  
33 - pathname : the full pathname of the file;
34  
35 - width : the width in pixels of the image;
36  
37 - height : the height in pixels of the image;
38  
39 - parentdir: the full pathname of the parent directory the image is contained
40 in.
41  
42  
43 =head1 VERSION
44  
45 Version 0.01
46  
47 =cut
48  
49 our $VERSION = '0.01';
50  
51 =head2 new
52  
53 Instantiates the layout:
54  
55 my $PackedLayout = CSS::SpriteMaker::Layout::Packed->new(
56 $rh_item_info
57 );
58  
59 =cut
60  
61 sub new {
62 my $class = shift;
63 my $rh_items_info = shift;
64  
65 my $self = bless {}, $class;
66  
67 if (!$rh_items_info) {
68 die 'no items info hashref was passed in construction to this layout';
69 }
70  
71 $self->_layout_items($rh_items_info);
72 $self->finalize();
73  
74 return $self;
75 }
76  
77 =head2 _layout_items
78  
79 see POD of super class CSS::SpriteMaker::Layout::_layout_items for more
80 information.
81  
82 =cut
83  
84 sub _layout_items {
85 my $self = shift;
86 my $rh_items_info = shift;
87  
88 # sort items by height
89 my @items_sorted =
90 sort {
91 $rh_items_info->{$b}{height}
92 <=>
93 $rh_items_info->{$a}{height}
94 }
95 keys %$rh_items_info;
96  
97 my $root = CSS::SpriteMaker::Layout::Packed::Node->new(
98 0, 0,
99 $rh_items_info->{$items_sorted[0]}{width},
100 $rh_items_info->{$items_sorted[0]}{height}
101 );
102  
103 my $max_w = 0;
104 my $max_h = 0;
105 my $node;
106 for my $image_id (@items_sorted) {
107 my $image = $rh_items_info->{$image_id};
108 $node = $root->find($root, $image->{width}, $image->{height});
109 if ($node) {
110 $node = $root->split($node, $image->{width}, $image->{height});
111 }
112 else {
113 $node = $root->grow($image->{width}, $image->{height});
114 }
115  
116 $self->set_item_coord($image_id, $node->{x}, $node->{y});
117  
118 # compute the overall width/height
119 $max_w = $image->{width} + $node->{x} if $max_w < $image->{width} + $node->{x};
120 $max_h = $image->{height} + $node->{y} if $max_h < $image->{height} + $node->{y};
121 }
122  
123 # write dimensions in the resulting layout
124 $self->{width} = $max_w;
125 $self->{height} = $max_h;
126  
127 return;
128 }
129  
130 1;