Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 94 lines (67 sloc) 2.141 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
#!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
Something went wrong with that request. Please try again.