Debian Codegraph @

samples/align-string.pl

raw
1 #!perl -w
2 use strict;
3 use Imager;
4
5 my ($font_filename, $size, $out_filename, @text) = @ARGV;
6
7 @text
8 or usage();
9
10 $size =~ /^\d+$/ && $size >= 10
11 or die "size must be 10 or greater";
12
13 my $text = "@text";
14
15 my $mark_color = Imager::Color->new('#00F');
16 my $text_color = Imager::Color->new('#fff');
17
18 my $font = Imager::Font->new(file=>$font_filename,
19 size => $size,
20 color => $text_color,
21 aa => 1)
22 or die "Cannot create font from $font_filename: ", Imager->errstr;
23
24 my @valigns = qw(top center bottom baseline);
25 my @haligns = qw(left start center end right);
26
27 my $bounds = $font->bounding_box(string => $text);
28
29 my $text_width = $bounds->total_width;
30 my $text_height = $bounds->text_height;
31
32 my $img = Imager->new(xsize => $text_width * 2 * @haligns,
33 ysize => $text_height * 2 * @valigns);
34
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,
47 string => $text,
48 x => $xpos, y => $ypos,
49 halign => $halign,
50 valign => $valign);
51 $ypos += 2 * $text_height;
52 }
53 $xpos += 2 * $text_width;
54 }
55
56 $img->write(file => $out_filename)
57 or die "Cannot write $out_filename: ", $img->errstr, "\n";
58
59 sub usage {
60 die <<USAGE;
61 $0 fontfile size output text...
62 USAGE
63 }
64
65 =head1 NAME
66
67 align-string.pl - demo of the Imager align_string() method
68
69 =head1 SYNOPSIS
70
71 perl align-string.pl fontfile size outputfile text ...
72
73 =head1 DESCRIPTION
74
75 Create an image in output C<imagein> C<outputfile> displaying a grid of
76 the various C<valign> and C<halign> options for the Imager align_string()
77 method.
78
79 Try it with different fonts and strings to get a better understanding
80 of the effect of the different alignments.
81
82 =head1 AUTHOR
83
84 Tony Cook <[email protected]>
85
86 =head1 SEE ALSO
87
88 Imager, Imager::Font
89
90 =head1 REVISION
91
92 $Revision$
93
94 =cut
95