Button/Rect.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Button.pm ↑ Summary
1 package Image::Button::Rect;
2  
3 # $Id: Rect.pm,v 1.5 2003/03/04 15:58:15 joanmg Exp $
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 $VERSION = "0.53";
9  
10  
11 0 ▶use Image::Button;
Subroutine BEGIN: called CC=1 SCAR=2.6
12 @ISA = qw(Image::Button);
13  
14 use GD 1.20;
15  
16 sub print
17 {
18 my $self = shift;
19 my %args = (file => $self->{file},
20 prefix => '',
21 postfix => '',
22 @_,);
23  
24 my $file = $args{file}; die "Need output file\n" unless $file;
25 $file = $self->buildFile(%args);
26  
27 my $text = $self->{text};
28  
29 my $font = $self->{font}; die "Need font\n" unless $font;
30 my $size = $self->{fontsize}; die "Need font size\n" unless $size;
31 my $btcol = $self->{btcolor}; die "Need button color\n" unless $btcol;
32 my $bgcol = $self->{bgcolor}; die "Need bg color\n" unless $bgcol;
33 my $fgcol = $self->{fgcolor}; die "Need fg color\n" unless $fgcol;
34 my $vmarg = $self->{vmargin}; $vmarg = 4 unless defined $vmarg;
35 my $hmarg = $self->{hmargin}; $hmarg = 4 unless defined $hmarg;
36  
37 $font = $self->getFont($font);
38  
39 # Compute bounds
40 my @bounds = &GD::Image::stringFT('GD::Image', 0, $font,
41 $size, 0, 0, 0, $text);
42 # @bounds[0,1] Lower left corner (x,y)
43 # @bounds[2,3] Lower right corner (x,y)
44 # @bounds[4,5] Upper right corner (x,y)
45 # @bounds[6,7] Upper left corner (x,y)
46  
47 if (!@bounds) {
48 die "Error figuring out bounds for '$text': $@\n";
49 }
50  
51 # We need the text width to center it later on in case tw has come
52 # defined (this happens when we want several buttons to share the
53 # same width).
54 my $thisTw = $bounds[2] - $bounds[0];
55 my $thisTh = $bounds[1] - $bounds[7];
56 my $th = $self->{texth};
57 my $tw = $self->{textw};
58 $th = $th ? $th : $thisTh;
59 $tw = $tw ? $tw : $thisTw;
60  
61 # How much it goes down under the reference line
62 my $moveUp = $bounds[1] / 2;
63 if ($th != $thisTh) {
64 $moveUp += ($th - $thisTh)/2;
65 }
66  
67 # Figure out sizes and build image
68 my $w = $tw + 2 * $hmarg + 4; # shadows are 2 pixels wide
69 my $h = $th + 2 * $vmarg + 5; # shadows are 2 and 3 pixels tall
70  
71 my $img = new GD::Image($w, $h);
72  
73 # Build and allocate colors
74 my @dk = map { int($_/1.75) } @$btcol; # dark (shadows)
75 my @lt = map { int($_ * 1.2) } @dk; # light shadow
76 my @vlt = map { int($_ * 1.5) } @dk; # very light shadow
77  
78 my $abt = $img->colorAllocate(@$btcol);
79 my $abg = $img->colorAllocate(@$bgcol);
80 my $afg = $img->colorAllocate(@$fgcol);
81 my $adk = $img->colorAllocate(@dk);
82 my $alt = $img->colorAllocate(@lt);
83 my $avlt = $img->colorAllocate(@vlt);
84 my $awhite = $img->colorAllocate(255, 255, 255);
85  
86 $img->fill(0, 0, $abg);
87  
88 # Dark border
89 $img->line(1, 0, $w-3, 0, $adk); # top
90 $img->line(0, 1, 0, $h-2, $adk); # left
91 $img->line(2, $h-2, $w-2, $h-2, $adk); # bottom
92 $img->line($w-2, $h-2, $w-2, 1, $adk); # right
93  
94 # Light shadow
95 $img->line(1, $h-1, $w-2, $h-1, $alt); # bottom
96  
97 # Very light shadow
98 $img->line(3, $h-3, $w-4, $h-3, $avlt); # bottom
99 $img->line($w-1, 2, $w-1, $h-2, $avlt); # right
100
101 # Fill up the button
102 $img->fill(int($w/2), int($h/2), $abt);
103  
104 # White shadow
105 $img->line(1, 1, 1, $h-2, $awhite); # left
106 $img->line(1, 1, $w-3, 1, $awhite); # left
107  
108 $img->stringFT($afg, $font, $size, 0,
109 $w/2 - $thisTw/2, $h-3-$vmarg-$moveUp, $text) ||
110 die "Rendering of '$text' did not work: $@\n";
111  
112 open FOUT, ">$file" || die "Could not open $file for writing\n";
113 binmode FOUT;
114 print FOUT $img->png;
115 close FOUT;
116 }
117  
118 # Overrides Button::getSize.
119 sub getSize
120 {
121 my $self = shift;
122 my ($w, $h) = $self->SUPER::getSize;
123 return ($w+4, $h+5); # Increase with the shadow size
124 }
125  
126 # Overrides Button::textSize
127 sub textSize
128 {
129 my $self = shift;
130 my %args = (texth => undef,
131 textw => undef,
132 @_,);
133 $self->{texth} = $args{texth}; $self->{texth} -= 4 if $self->{texth};
134 $self->{textw} = $args{textw}; $self->{textw} -= 5 if $self->{textw};
135 }
136  
137 1;
138  
139 =head1 NAME
140  
141 Image::Button::Rect - Builds rectangular PNG buttons
142  
143 =head1 SYNOPSIS
144  
145 use Image::Button::Rect;
146  
147 my $b1 = new Image::Button::Rect(text => 'text b1',
148 font => 'newsgotn.ttf',
149 fontsize => 20,
150 file => 'b1.png');
151  
152 $b1->print;
153  
154 =head1 DESCRIPTION
155  
156 Builds reasonably good looking rectangular buttons, with shadows and
157 all, using GD with TrueType support. See F<Image::Button> for more
158 details about things you can do with Buttons.
159  
160 =head2 Constructor
161  
162 my $b = new Image::Button::Rect(text => 'text',
163 font => 'newsgotn.ttf',
164 fontsize => 20,
165 file => 'file.png');
166  
167 The arguments are
168  
169 =over 4
170  
171 =item text => 'button text'
172  
173 Defaults to ''.
174  
175 =item font => 'something.ttf'
176  
177 The TrueType font with which to print. It should be located either
178 in the current directory or in the directory pointed at by the
179 environment variable I<TTFONTS>. Mandatory.
180  
181 =item fontsize => 12
182  
183 Font size. Defaults to 12 points.
184  
185 =item file => 'button.png'
186  
187 File where the button will be saved. Can be overridden in the print
188 function. Mandatory if you plan to use F<Image::Button::Set> to
189 output several related buttons at a time. Defaults to the button
190 text, replacing spaces by '-'. Use '>-' if you want to send the image
191 to standard output.
192  
193 =item btcolor => [ 238, 238, 204 ]
194  
195 Button color in RGB (so [255,255,255] is white and [0,0,0] is black).
196 Defaults to [238,238,204], which is as nice a color as any other.
197  
198 =item fgcolor => [ 0, 0, 0 ]
199  
200 Foreground (text) color in RGB. Defaults to [0,0,0].
201  
202 =item bgcolor => [ 255, 255, 255 ]
203  
204 Background color in RGB. This is the color you want to be equal to
205 your page background. Defaults to [255,255,255]. You might want to
206 set this transparent, but (1) it's not implemented---in Button.pm, GD
207 knows how to do it--- and (2) IE, or at least some versions of it,
208 doesn't handle transparent PNGs correctly.
209  
210 =item vmargin => 4
211  
212 Vertical margin under and over the text, more or less. Defaults to 4
213 pixels.
214  
215 =item hmargin => 4
216  
217 Horizontal margin left and right of the text, more or less. Defaults
218 to 4 pixels.
219  
220 =back
221  
222 =head1 SEE ALSO
223  
224 F<Image::Button> for a description of how to make buttons, and other
225 functions you can use with Image::Button::Rect.
226  
227 F<Image::Button::Set> for building sets of related buttons.
228  
229 =head1 AUTHOR
230  
231 Juan M. García-Reyero E<lt>joanmg@twostones.orgE<gt>
232  
233 =head1 COPYRIGHT
234  
235 Copyright (C) 2003 Juan M. García-Reyero. All Rights Reserved.
236  
237 This module is free software; you can redistribute it and/or
238 modify it under the same terms as Perl itself.
239  
240  
« Button.pm ↑ Summary