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