From: Tony Cook Date: Sat, 3 Dec 2005 13:12:47 +0000 (+0000) Subject: - added samples/align-string.pl X-Git-Tag: Imager-0.48^2~81 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/085910c65968948e028054e019e115e6b5ba1b58?ds=sidebyside - added samples/align-string.pl --- diff --git a/Changes b/Changes index aaad5ae8..9f05d6e4 100644 --- a/Changes +++ b/Changes @@ -1195,6 +1195,7 @@ Revision history for Perl extension Imager. causing minor artifacts at the inner and external corners - implemented valign=>'end' for Imager::Font->align - added $img->align_string() as a way to call Imager::Font->align +- added samples/align-string.pl ================================================================= diff --git a/samples/README b/samples/README index ff8ce486..5f94c7f7 100644 --- a/samples/README +++ b/samples/README @@ -74,3 +74,16 @@ slant_text.pl tk-photo.pl Simple example of making a Tk::Photo object using Imager data. + +align-string.pl + + Simple demonstration of the align_string() method. Usage: + + perl align-string.pl fontfile size outputfile text... + + eg. + + perl align-string.pl fontfiles/ImUgly.ttf 60 test.ppm .A. + + This demonstrates how the various values of halign and valign + behave. diff --git a/samples/align-string.pl b/samples/align-string.pl new file mode 100644 index 00000000..815a98d3 --- /dev/null +++ b/samples/align-string.pl @@ -0,0 +1,95 @@ +#!perl -w +use strict; +use Imager; + +my ($font_filename, $size, $out_filename, @text) = @ARGV; + +@text + or usage(); + +$size =~ /^\d+$/ && $size >= 10 + or die "size must be 10 or greater"; + +my $text = "@text"; + +my $mark_color = Imager::Color->new('#00F'); +my $text_color = Imager::Color->new('#fff'); + +my $font = Imager::Font->new(file=>$font_filename, + size => $size, + color => $text_color, + aa => 1) + or die "Cannot create font from $font_filename: ", Imager->errstr; + +my @valigns = qw(top center bottom baseline); +my @haligns = qw(left start center end right); + +my $bounds = $font->bounding_box(string => $text); + +my $text_width = $bounds->total_width; +my $text_height = $bounds->text_height; + +my $img = Imager->new(xsize => $text_width * 2 * @haligns, + ysize => $text_height * 2 * @valigns); + +my $xpos = $text_width; +for my $halign (@haligns) { + my $ypos = $text_height; + for my $valign (@valigns) { + # mark the align point + $img->line(x1 => $xpos - $size, y1 => $ypos, + x2 => $xpos + $size, y2 => $ypos, + color => $mark_color); + $img->line(x1 => $xpos, y1 => $ypos - $size, + x2 => $xpos, y2 => $ypos + $size, + color => $mark_color); + $img->align_string(font => $font, + string => $text, + x => $xpos, y => $ypos, + halign => $halign, + valign => $valign); + $ypos += 2 * $text_height; + } + $xpos += 2 * $text_width; +} + +$img->write(file => $out_filename) + or die "Cannot write $out_filename: ", $img->errstr, "\n"; + +sub usage { + die < displaying a grid of +the various valign and halign options for the Imager align_string +method. + +Try it with different fonts and strings to get a better understanding +of the effect of the different alignments. + +=head1 AUTHOR + +Tony Cook + +=head1 SEE ALSO + +Imager, Imager::Font + +=head1 REVISION + +$Revision$ + +=cut +