Treemap/Output/Imager.pm

filter: Statementstmt 43.7% 63 / 144 Branchbran 6.4% 4 / 62 Conditioncond 22.7% 10 / 44 MC/DCmcdc 0.0% 0 / 24 Subroutinesub 62.5% 10 / 16 Podpod 22.2% 2 / 9 totaltotal 29.7% 89 / 299 CC 50 SCAR 61.7
CC
50
Cov
31%
_font_fit
992.0
text
22.1
_init
14.8
CRAP 478.4
↑ Summary Treemap/Squarified.pm »
1 package Treemap::Output::Imager;
2  
3 1 ▶use 5.006;
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 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 require Exporter;
9 1 require Treemap::Output;
10 1 require Imager;
11 1 require Imager::Font;
12 1 require Imager::Color;
13  
14 1 our @ISA = qw( Treemap::Output Exporter );
15 1 our @EXPORT_OK = ( );
16 1 our @EXPORT = qw( );
17 1 our $VERSION = '0.01';
18  
19  
20 # ------------------------------------------
21 # Methods:
22 # ------------------------------------------
23 sub new
24 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
25 1 my $classname = shift;
26 1 my $self = $classname->SUPER::new( @_ ); # Call parent constructor
27 1 $self->_init( @_ ); # Initialize child variables
28 1 return $self;
29 }
30  
31 sub _init
32 1 ▶{
Subroutine _init: called CC=12 SCAR=26.9
33 1 my $self = shift;
34 1 ▶ $self->{WIDTH} = $self->{WIDTH} || 400;
Condition50%1/2
$self->{'WIDTH'} || 400
l!l
10
MC/DC0%0/1
$self->{'WIDTH'} || 400
$self->{'WIDTH'}
Truth table50%1/2
A
A= $self->{'WIDTH'}
Aresult
11
00
35 1 ▶ $self->{HEIGHT} = $self->{HEIGHT} || 300;
Condition50%1/2
$self->{'HEIGHT'} || 300
l!l
10
MC/DC0%0/1
$self->{'HEIGHT'} || 300
$self->{'HEIGHT'}
Truth table50%1/2
A
A= $self->{'HEIGHT'}
Aresult
11
00
36 1 ▶ $self->{PADDING} = $self->{PADDING} || 5;
Condition50%1/2
$self->{'PADDING'} || 5
l!l
01
MC/DC0%0/1
$self->{'PADDING'} || 5
$self->{'PADDING'}
Truth table50%1/2
A
A= $self->{'PADDING'}
Aresult
11
00
37 1 ▶ $self->{SPACING} = $self->{SPACING} || 5;
Condition50%1/2
$self->{'SPACING'} || 5
l!l
01
MC/DC0%0/1
$self->{'SPACING'} || 5
$self->{'SPACING'}
Truth table50%1/2
A
A= $self->{'SPACING'}
Aresult
11
00
38 1 ▶ $self->{BORDER_COLOUR} = $self->{BORDER_COLOUR} || "#000000";
Condition50%1/2
$self->{'BORDER_COLOUR'} || '#000000'
l!l
01
MC/DC0%0/1
$self->{'BORDER_COLOUR'} || '#000000'
$self->{'BORDER_COLOUR'}
Truth table50%1/2
A
A= $self->{'BORDER_COLOUR'}
Aresult
11
00
39 1 ▶ $self->{FONT_COLOUR} = $self->{FONT_COLOUR} || "#000000";
Condition50%1/2
$self->{'FONT_COLOUR'} || '#000000'
l!l
01
MC/DC0%0/1
$self->{'FONT_COLOUR'} || '#000000'
$self->{'FONT_COLOUR'}
Truth table50%1/2
A
A= $self->{'FONT_COLOUR'}
Aresult
11
00
40 1 ▶ $self->{MIN_FONT_SIZE} = $self->{MIN_FONT_SIZE} || 5;
Condition50%1/2
$self->{'MIN_FONT_SIZE'} || 5
l!l
01
MC/DC0%0/1
$self->{'MIN_FONT_SIZE'} || 5
$self->{'MIN_FONT_SIZE'}
Truth table50%1/2
A
A= $self->{'MIN_FONT_SIZE'}
Aresult
11
00
41 1 ▶ $self->{FONT_FILE} = $self->{FONT_FILE} || "ImUgly.ttf";
Condition50%1/2
$self->{'FONT_FILE'} || 'ImUgly.ttf'
l!l
01
MC/DC0%0/1
$self->{'FONT_FILE'} || 'ImUgly.ttf'
$self->{'FONT_FILE'}
Truth table50%1/2
A
A= $self->{'FONT_FILE'}
Aresult
11
00
42 1 ▶ $self->{TEXT_DEBUG} = $self->{TEXT_DEBUG} || 0;
Condition50%1/2
$self->{'TEXT_DEBUG'} || 0
l!l
01
MC/DC0%0/1
$self->{'TEXT_DEBUG'} || 0
$self->{'TEXT_DEBUG'}
Truth table50%1/2
A
A= $self->{'TEXT_DEBUG'}
Aresult
11
00
43 1 ▶ $self->{DEBUG} = $self->{DEBUG} || 0;
Condition50%1/2
$self->{'DEBUG'} || 0
l!l
01
MC/DC0%0/1
$self->{'DEBUG'} || 0
$self->{'DEBUG'}
Truth table50%1/2
A
A= $self->{'DEBUG'}
Aresult
11
00
44  
45 ## aggregate resource variables:
46 $self->{IMAGE} = Imager->new( xsize => $self->{WIDTH},
47 1 ysize => $self->{HEIGHT} );
48 1 $self->{ALPHA} = Imager->new();# xsize => $self->{WIDTH},
49 # ysize => $self->{HEIGHT},
50 # channels => 4 );
51  
52 1 ▶ $self->{DEBUG} && print STDERR "Created a new image object.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DEBUG"} and print STDERR "Created a new image object.\n" 0 1 1
53  
54 # init cache with border colour and font colours:
55 $self->{COLOUR_CACHE}->{$self->{BORDER_COLOUR}} = Imager::Color->new(
56 1 $self->{BORDER_COLOUR} );
57 1 $self->{ALPHA_FONT} = Imager::Color->new( 0, 0, 0, 110 );
58 1 $self->{SOLID_FONT} = Imager::Color->new( $self->{FONT_COLOUR} );
59  
60 $self->{FONT} = Imager::Font->new(
61 file => $self->{FONT_FILE},
62 color => $self->{SOLID_FONT},
63 1 aa => 1,
64 type => 'ft2' );
65  
66 # for profiling:
67 1 $self->{font_iters} = 0;
68 }
69  
70 sub save
71 0 ▶{
Subroutine save: not called CC=1 SCAR=6.9
Pod undocumented
72 0 my $self = shift;
73 0 my ( $filename ) = @_;
74 0 $self->{IMAGE}->write( file=>$filename );
75 0 return 1;
76 }
77  
78 sub rect
79 1 ▶{
Subroutine rect: called CC=3 SCAR=11.3
Pod documented
80 1 my $self = shift;
81 1 my ( $x1, $y1, $x2, $y2, $colour ) = @_;
82 1 my $area = ( $x2 - $x1 ) * ( $y2 - $y1 );
83  
84 # cache any colour object that is created; Imager::Color is an expensive
85 # operation
86 1 ▶ if ( ! $self->{COLOUR_CACHE}->{$colour} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"COLOUR_CACHE"}{$colour}) 0 1 1
87 {
88 0 $self->{COLOUR_CACHE}->{$colour} = Imager::Color->new( $colour );
89 }
90  
91 # draw inner box (filled):
92 1 $self->{IMAGE}->box( color => $self->{COLOUR_CACHE}->{$colour},
93 xmin => $x1,
94 ymin => $y1,
95 xmax => $x2,
96 ymax => $y2,
97 filled => 1 );
98
99 1 ▶ return 1 if ( $area < 3 );
Branch: 1 branch
BranchTrueFalseTotal
if $area < 3 0 1 1
100  
101 # draw outer "outline" box (stroked):
102 $self->{IMAGE}->box( color => $self->{COLOUR_CACHE}->{$self->{BORDER_COLOUR}},
103 1 xmin => $x1,
104 ymin => $y1,
105 xmax => $x2,
106 ymax => $y2,
107 filled => 0 );
108  
109 1 return 1;
110 }
111  
112 ## text label drawing method:
113 # new "guessing" method
114 # see old method below -- fishy
115 sub text
116 1 ▶{
Subroutine text: called CC=6 SCAR=31.0
Pod documented
117 1 my $self = shift;
118 1 my ( $x1, $y1, $x2, $y2, $text, $children ) = @_;
119  
120 1 my $x = $x1 + ( $x2 - $x1 ) / 2;
121 1 my $y = $y1 + ( $y2 - $y1 ) / 2;
122  
123 1 my $width = abs( $x2 - $x1 );
124 1 my $height = abs( $y2 - $y1 );
125  
126 # It's not worth trying to print text in here, it's too narrow
127 1 ▶ return 1 if ( $width < 20 );
Branch: 1 branch
BranchTrueFalseTotal
if $width < 20 1 0 1
128  
129 # It's not worth trying to print text in here, it's too short
130 0 ▶ return 1 if ( $height < 10 );
Branch: 1 branch
BranchTrueFalseTotal
if $height < 10 0 0 0
131  
132 0 my $size = $self->_font_fit( $width, $height, $text );
133  
134 0 ▶ return 1 if ( ! $size );
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 0 0
135  
136 # write string into image:
137 my @metrix = $self->{FONT}->bounding_box(
138 0 string => $text,
139 size => $size,
140 canon => 1 );
141  
142 # alpha transparent fonts, using rub-throughs
143 0 ▶ if( $children )
Branch: 1 branch
BranchTrueFalseTotal
if ($children) { } 0 0 0
144 {
145 # $x -= $metrix[2]/2;
146 # $y += $metrix[3]/3;
147 # $self->{ALPHA}->img_set( xsize => $self->{WIDTH},
148 # ysize => $self->{HEIGHT},
149 # channels => 4 );
150 # $self->{ALPHA}->string(
151 # font => $self->{FONT},
152 # text => $text,
153 # x => $x,
154 # y => $y,
155 # color => $self->{ALPHA_FONT},
156 # size => $size );
157 #
158 # $self->{IMAGE}->rubthrough( src => $self->{ALPHA} );
159 0 $x -= $metrix[2]/2;
160 0 $y -= $metrix[3]/2;
161 0 $self->{ALPHA}->img_set( xsize => $metrix[2],
162 ysize => $metrix[3],
163 channels => 4 );
164 $self->{ALPHA}->string(
165 font => $self->{FONT},
166 text => $text,
167 x => 0,
168 y => $metrix[3]+$metrix[1],
169 color => $self->{ALPHA_FONT},
170 0 size => $size );
171  
172 0 $self->{IMAGE}->rubthrough( src => $self->{ALPHA}, tx=>$x, ty=>$y );
173 }
174 else
175 {
176 0 $x -= $metrix[2]/2;
177 0 $y += $metrix[3]/3;
178 # position at top, in black:
179  
180 # try to get a reasonable top-padding, if available:
181 0 my $top_pad = int(( $height - $metrix[5] ) * 0.1 );
182 0 ▶ $top_pad = ( $top_pad > 5 ) ? 5 : $top_pad;
Branch: 1 branch
BranchTrueFalseTotal
$top_pad > 5 ? : 0 0 0
183 0 $y = $y1 + $metrix[5] + $top_pad;
184  
185 $self->{IMAGE}->string(
186 font => $self->{FONT},
187 text => $text,
188 x => $x,
189 y => $y,
190 color => $self->{SOLID_FONT},
191 0 size => $size );
192 }
193 0 return 1;
194 }
195  
196 ## font fitting algorhythm
197 # moved to seperate function, merged with guessing function
198 sub _font_fit
199 0 ▶{
Subroutine _font_fit: not called CC=31 SCAR=69.0
200 0 my $self = shift;
201 0 my ( $width, $height, $text ) = @_;
202 0 my $DEBUG = $self->{TEXT_DEBUG};
203
204 0 ▶ return unless $text && ( length( $text ) ) && $height && $width;
Branch: 1 branch
BranchTrueFalseTotal
unless $text and length $text and $height and $width 0 0 0
Condition0%0/9
$text and length $text and $height and $width
!ll&&!rl&&r
000
$text and length $text and $height
!ll&&!rl&&r
000
$text and length $text
!ll&&!rl&&r
000
MC/DC0%0/4
$text and length $text and $height and $width
$text length $text $height $width
Truth table0%0/5
A and B and C and D
A= $textB= length $textC= $heightD= $width
ABCDresult
0XXX0
10XX0
110X0
11100
11111
205  
206 0 my $local_iters = 0;
207  
208 # Search for suitable font size
209 0 ▶ $self->{TEXT_DEBUG} && print STDERR "$text:\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "${text}:\n" 0 0 0
210  
211 # fetch a guess at the starting point:
212 # if not initialized:
213 0 ▶ unless ( $self->{ACWPP} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ACWPP"}) 0 0 0
214 {
215 # find average character width per point
216 0 $self->{ACWPP} = $self->_calc_avg_char_weight_per_pt();
217 croak( "Initialization of font fitting algorhythm failed." )
218 0 ▶ unless ( $self->{ACWPP} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"ACWPP"} 0 0 0
219 }
220  
221 0 my $size = int( ( $width / length( $text ) ) / $self->{ACWPP} );
222
223 # because it is guaranteed to be not worth it:
224 0 ▶ return if ( $size <= ( $self->{MIN_FONT_SIZE} - 2 ) );
Branch: 1 branch
BranchTrueFalseTotal
if $size <= $self->{"MIN_FONT_SIZE"} - 2 0 0 0
225  
226 # test guess:
227 my @metrix = $self->{FONT}->bounding_box(
228 0 string => $text,
229 size => $size,
230 canon => 1 );
231  
232 # two corrective measures:
233
234 # 1. if the width fits, but not the height, then we have a height
235 # restricted case. These tend to be expensive, so we "correct" our
236 # guess.
237 0 ▶ if (( $metrix[2] <= $width ) && ( $metrix[3] > $height ))
Branch: 2 branches
BranchTrueFalseTotal
if ($metrix[2] <= $width and $metrix[3] > $height) { } 0 0 0
elsif (abs($width - $metrix[2]) / $width * $size >= 3) { } 0 0 0
Condition0%0/3
$metrix[2] <= $width and $metrix[3] > $height
!ll&&!rl&&r
000
MC/DC0%0/2
$metrix[2] <= $width and $metrix[3] > $height
$metrix[2] <= $width $metrix[3] > $height
Truth table0%0/3
A and B
A= $metrix[2] <= $widthB= $metrix[3] > $height
ABresult
0X0
100
111
238 {
239 # if there is a major difference in height, correct guess
240 0 ▶ if (( abs( $height - $metrix[3] ) / $height ) * $size >= 3 )
Branch: 1 branch
BranchTrueFalseTotal
if (abs($height - $metrix[3]) / $height * $size >= 3) 0 0 0
241 {
242 0 $self->{font_iters}++; $local_iters++; # track iterations
243
244 0 ▶ $self->{TEXT_DEBUG} && print STDERR "\tHeight restricted, changing $size =>";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "\tHeight restricted, changing $size =>" 0 0 0
245 0 $size = int( $size * ( $height / $metrix[3] ));
246 0 ▶ $self->{TEXT_DEBUG} && print STDERR "$size.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "$size.\n" 0 0 0
247
248 @metrix = $self->{FONT}->bounding_box(
249 0 string => $text,
250 size => $size,
251 canon => 1 );
252 }
253 }
254 # 2. if our guess is way off width-wise, correct:
255 # if a correction would yeild a size change of more than 3,
256 # it is obviously worth it.
257 elsif ( ( abs( $width - $metrix[2] ) / $width ) * $size >= 3 )
258 {
259 0 $self->{font_iters}++; $local_iters++; # track iterations
260 0 ▶ $self->{TEXT_DEBUG} && print STDERR "\tOff by 3pts+, changing $size =>";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "\tOff by 3pts+, changing $size =>" 0 0 0
261 0 $size = int( $size * ( $width / $metrix[2] ));
262 0 ▶ $self->{TEXT_DEBUG} && print STDERR "$size.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "$size.\n" 0 0 0
263 @metrix = $self->{FONT}->bounding_box(
264 0 string => $text,
265 size => $size,
266 canon => 1 );
267 }
268  
269 # if our guess was too large, try smaller values until there is a fit:
270 0 ▶ if (( $metrix[2] > $width ) || ( $metrix[3] > $height ))
Branch: 2 branches
BranchTrueFalseTotal
if ($metrix[2] > $width or $metrix[3] > $height) { } 0 0 0
elsif ($metrix[2] <= $width and $metrix[3] <= $height) { } 0 0 0
Condition0%0/3
$metrix[2] > $width or $metrix[3] > $height
l!l&&r!l&&!r
000
MC/DC0%0/2
$metrix[2] > $width or $metrix[3] > $height
$metrix[2] > $width $metrix[3] > $height
Truth table0%0/3
A or B
A= $metrix[2] > $widthB= $metrix[3] > $height
ABresult
1X1
011
000
271 {
272 0 ▶ $self->{TEXT_DEBUG} && print STDERR "\tGuess ($size) too large.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "\tGuess ($size) too large.\n" 0 0 0
273 0 ▶ while ( ( $metrix[2] > $width ) || ( $metrix[3] > $height ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($metrix[2] > $width or $metrix[3] > $height) 0 0 0
Condition0%0/3
$metrix[2] > $width or $metrix[3] > $height
l!l&&r!l&&!r
000
MC/DC0%0/2
$metrix[2] > $width or $metrix[3] > $height
$metrix[2] > $width $metrix[3] > $height
Truth table0%0/3
A or B
A= $metrix[2] > $widthB= $metrix[3] > $height
ABresult
1X1
011
000
274 {
275 0 $self->{font_iters}++; $local_iters++; # track iterations
276 0 $size--;
277  
278 0 ▶ return if ( $size < 5 );
Branch: 1 branch
BranchTrueFalseTotal
if $size < 5 0 0 0
279  
280 @metrix = $self->{FONT}->bounding_box(
281 0 string => $text,
282 size => $size,
283 canon => 1 );
284 }
285 }
286 # if our guess is too small, try larger values until there is a -no- fit:
287 ▶ elsif ( ( $metrix[2] <= $width ) && ( $metrix[3] <= $height ))
Condition0%0/3
$metrix[2] <= $width and $metrix[3] <= $height
!ll&&!rl&&r
000
MC/DC0%0/2
$metrix[2] <= $width and $metrix[3] <= $height
$metrix[2] <= $width $metrix[3] <= $height
Truth table0%0/3
A and B
A= $metrix[2] <= $widthB= $metrix[3] <= $height
ABresult
0X0
100
111
288 {
289 0 ▶ $self->{TEXT_DEBUG} && print STDERR "\tGuess ($size) fits, adjusting.\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "\tGuess ($size) fits, adjusting.\n" 0 0 0
290 0 ▶ while ( ( $metrix[2] <= $width ) && ( $metrix[3] <= $height ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($metrix[2] <= $width and $metrix[3] <= $height) 0 0 0
Condition0%0/3
$metrix[2] <= $width and $metrix[3] <= $height
!ll&&!rl&&r
000
MC/DC0%0/2
$metrix[2] <= $width and $metrix[3] <= $height
$metrix[2] <= $width $metrix[3] <= $height
Truth table0%0/3
A and B
A= $metrix[2] <= $widthB= $metrix[3] <= $height
ABresult
0X0
100
111
291 {
292 0 $self->{font_iters}++; $local_iters++; # track iterations
293
294 0 $size++;
295 0 ▶ $size++ if ( $size > 50 ); # grow a bit faster for big fonts
Branch: 1 branch
BranchTrueFalseTotal
if $size > 50 0 0 0
296
297 @metrix = $self->{FONT}->bounding_box(
298 0 string => $text,
299 size => $size,
300 canon => 1 );
301 }
302 0 $size--; # because this overshoots
303 }
304  
305 0 ▶ $self->{TEXT_DEBUG} && print STDERR "\t$local_iters :: " . $self->{font_iters} . " => $size\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"TEXT_DEBUG"} and print STDERR "\t$local_iters :: " . $self->{"font_iters"} . " => $size\n" 0 0 0
306  
307 0 $size = int( $size * 0.9 ); # reduce size to fit comfortably
308  
309 0 ▶ return if ( $size < $self->{MIN_FONT_SIZE} );
Branch: 1 branch
BranchTrueFalseTotal
if $size < $self->{"MIN_FONT_SIZE"} 0 0 0
310  
311 0 return $size;
312 }
313  
314 ###############################################
315 #
316 # private: _calc_avg_char_weight_per_pt
317 # input: none
318 # output: ACWPP
319 #
320 # pardon the size of this function name
321 # it only needs to be called in one place
322 #
323 sub _calc_avg_char_weight_per_pt
324 0 ▶{
Subroutine _calc_avg_char_weight_per_pt: not called CC=2 SCAR=17.9
325 0 my $self = shift;
326 0 my $wieghting_string = "rstlnaei0RST.-";
327 0 my $sample_size = 50;
328  
329 # get metrix for sample:
330 my @metrix = $self->{FONT}->bounding_box(
331 0 string => $wieghting_string,
332 size => $sample_size,
333 canon => 1 );
334  
335 0 my $sample_width = $metrix[2];
336 0 ▶ return unless ( $sample_width );
Branch: 1 branch
BranchTrueFalseTotal
unless $sample_width 0 0 0
337  
338 # avg width per character per point
339 0 return ( $sample_width / length( $wieghting_string ) / $sample_size );
340 }
341  
342  
343 sub width
344 1 ▶{
Subroutine width: called CC=1 SCAR=0.0
Pod undocumented
345 1 my $self = shift;
346 1 return $self->{WIDTH};
347 }
348  
349 sub height
350 1 ▶{
Subroutine height: called CC=1 SCAR=0.0
Pod undocumented
351 1 my $self = shift;
352 1 return $self->{HEIGHT};
353 }
354  
355 sub font_height
356 0 ▶{
Subroutine font_height: not called CC=1 SCAR=6.9
Pod undocumented
357 0 my $self = shift;
358 0 return "12";
359 }
360  
361 sub padding
362 0 ▶{
Subroutine padding: not called CC=1 SCAR=6.9
Pod undocumented
363 0 my $self = shift;
364 0 return $self->{PADDING};
365 }
366  
367 sub spacing
368 0 ▶{
Subroutine spacing: not called CC=1 SCAR=6.9
Pod undocumented
369 0 my $self = shift;
370 0 return $self->{SPACING};
371 }
372  
373 1 1;
374  
375 __END__
↑ Summary Treemap/Squarified.pm »