5 my ($font_filename, $size, $out_filename, @text) = @ARGV;
10 $size =~ /^\d+$/ && $size >= 10
11 or die "size must be 10 or greater";
15 my $mark_color = Imager::Color->new('#00F');
16 my $text_color = Imager::Color->new('#fff');
18 my $font = Imager::Font->new(file=>$font_filename,
22 or die "Cannot create font from $font_filename: ", Imager->errstr;
24 my @valigns = qw(top center bottom baseline);
25 my @haligns = qw(left start center end right);
27 my $bounds = $font->bounding_box(string => $text);
29 my $text_width = $bounds->total_width;
30 my $text_height = $bounds->text_height;
32 my $img = Imager->new(xsize => $text_width * 2 * @haligns,
33 ysize => $text_height * 2 * @valigns);
35 my $xpos = $text_width;
36 for my $halign (@haligns) {
37 my $ypos = $text_height;
38 for my $valign (@valigns) {
39 # mark the align point
40 $img->line(x1 => $xpos - $size, y1 => $ypos,
41 x2 => $xpos + $size, y2 => $ypos,
42 color => $mark_color);
43 $img->line(x1 => $xpos, y1 => $ypos - $size,
44 x2 => $xpos, y2 => $ypos + $size,
45 color => $mark_color);
46 $img->align_string(font => $font,
48 x => $xpos, y => $ypos,
51 $ypos += 2 * $text_height;
53 $xpos += 2 * $text_width;
56 $img->write(file => $out_filename)
57 or die "Cannot write $out_filename: ", $img->errstr, "\n";
61 $0 fontfile size output text...
67 align-string.pl - demo of the Imager align_string() method
71 perl align-string.pl fontfile size outputfile text ...
75 Create an image in output imagein I<outputfile> displaying a grid of
76 the various valign and halign options for the Imager align_string
79 Try it with different fonts and strings to get a better understanding
80 of the effect of the different alignments.
84 Tony Cook <tony@imager.perl.org>