<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!perl -w
use strict;
use Imager;

my ($font_filename, $size, $out_filename, @text) = @ARGV;

@text
  or usage();

$size =~ /^\d+$/ &amp;&amp; $size &gt;= 10
  or die "size must be 10 or greater";

my $text = "@text";

my $mark_color = Imager::Color-&gt;new('#00F');
my $text_color = Imager::Color-&gt;new('#fff');

my $font = Imager::Font-&gt;new(file=&gt;$font_filename, 
			     size =&gt; $size, 
			     color =&gt; $text_color,
			     aa =&gt; 1)
  or die "Cannot create font from $font_filename: ", Imager-&gt;errstr;

my @valigns = qw(top center bottom baseline);
my @haligns = qw(left start center end right);

my $bounds = $font-&gt;bounding_box(string =&gt; $text);

my $text_width = $bounds-&gt;total_width;
my $text_height = $bounds-&gt;text_height;

my $img = Imager-&gt;new(xsize =&gt; $text_width * 2 * @haligns,
		      ysize =&gt; $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-&gt;line(x1 =&gt; $xpos - $size, y1 =&gt; $ypos, 
	       x2 =&gt; $xpos + $size, y2 =&gt; $ypos,
	       color =&gt; $mark_color);
    $img-&gt;line(x1 =&gt; $xpos, y1 =&gt; $ypos - $size, 
	       x2 =&gt; $xpos, y2 =&gt; $ypos + $size,
	       color =&gt; $mark_color);
    $img-&gt;align_string(font =&gt; $font,
		       string =&gt; $text,
		       x =&gt; $xpos, y =&gt; $ypos,
		       halign =&gt; $halign,
		       valign =&gt; $valign);
    $ypos += 2 * $text_height;
  }
  $xpos += 2 * $text_width;
}

$img-&gt;write(file =&gt; $out_filename)
  or die "Cannot write $out_filename: ", $img-&gt;errstr, "\n";

sub usage {
  die &lt;&lt;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 C&lt;imagein&gt; C&lt;outputfile&gt; displaying a grid of
the various C&lt;valign&gt; and C&lt;halign&gt; 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 &lt;tonyc@cpan.org&gt;

=head1 SEE ALSO

Imager, Imager::Font

=head1 REVISION

$Revision$

=cut

</pre></body></html>