]> git.imager.perl.org - imager.git/commitdiff
- added samples/align-string.pl
authorTony Cook <tony@develop=help.com>
Sat, 3 Dec 2005 13:12:47 +0000 (13:12 +0000)
committerTony Cook <tony@develop=help.com>
Sat, 3 Dec 2005 13:12:47 +0000 (13:12 +0000)
Changes
samples/README
samples/align-string.pl [new file with mode: 0644]

diff --git a/Changes b/Changes
index aaad5ae81450893286dca0bab1627f21705c5269..9f05d6e401d7aa3a65ec5df7bd2dda42df9f7669 100644 (file)
--- 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
   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
 
 =================================================================
 
 
 =================================================================
 
index ff8ce48690ed7136b9b830622ff7dbebe13203eb..5f94c7f771c7265526efa4afd3655b14ccd2dd31 100644 (file)
@@ -74,3 +74,16 @@ slant_text.pl
 tk-photo.pl
 
   Simple example of making a Tk::Photo object using Imager data.
 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 (file)
index 0000000..815a98d
--- /dev/null
@@ -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 <<USAGE;
+$0 fontfile size output text...
+USAGE
+}
+
+=head1 NAME
+
+align-string.pl - demo of the Imager align_string() method
+
+=head1 SYNOPSIS
+
+  perl align-string.pl fontfile size outputfile text ...
+
+=head1 DESCRIPTION
+
+Create an image in output imagein I<outputfile> 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 <tony@imager.perl.org>
+
+=head1 SEE ALSO
+
+Imager, Imager::Font
+
+=head1 REVISION
+
+$Revision$
+
+=cut
+