SpriteMaker/Layout/Packed.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 62 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.02
46  
47 =cut
48  
49 our $VERSION = '0.02';
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 || $a <=> $b
96 }
97 keys %$rh_items_info;
98  
99 my $root = CSS::SpriteMaker::Layout::Packed::Node->new(
100 0, 0,
101 $rh_items_info->{$items_sorted[0]}{width},
102 $rh_items_info->{$items_sorted[0]}{height}
103 );
104  
105 my $max_w = 0;
106 my $max_h = 0;
107 my $node;
108 for my $image_id (@items_sorted) {
109 my $image = $rh_items_info->{$image_id};
110 $node = $root->find($root, $image->{width}, $image->{height});
111 if ($node) {
112 $node = $root->split($node, $image->{width}, $image->{height});
113 }
114 else {
115 $node = $root->grow($image->{width}, $image->{height});
116 }
117  
118 $self->set_item_coord($image_id, $node->{x}, $node->{y});
119  
120 # compute the overall width/height
121 $max_w = $image->{width} + $node->{x} if $max_w < $image->{width} + $node->{x};
122 $max_h = $image->{height} + $node->{y} if $max_h < $image->{height} + $node->{y};
123 }
124  
125 # write dimensions in the resulting layout
126 $self->{width} = $max_w;
127 $self->{height} = $max_h;
128  
129 return;
130 }
131  
132 1;