/
slant_text.pl
217 lines (160 loc) · 5.03 KB
/
slant_text.pl
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!perl -w
use strict;
use Imager;
use Imager::Matrix2d;
use Getopt::Long;
use constant PI => 4 * atan2(1,1);
# this sample requires Freetype 2.x
$Imager::formats{"ft2"}
or die "This sample require Freetype 2.x to be configured in Imager\n";
Getopt::Long::Configure("bundling");
my $angle = 30;
my $fg = 'white';
my $bg = 'black';
my $size = 20;
my $rotate;
GetOptions('angle|a=f' => \$angle,
'size|s=i' => \$size,
'foreground|fg|f=s' => \$fg,
'background|bg|b=s' => \$bg,
'rotate|r' => \$rotate)
or usage();
# check for sanity
if ($angle < -45 or $angle > 45) {
# while values outside this range are valid, the text would be hard
# to read
die "--angle is limited to the range -45 through +45\n";
}
elsif ($size < 10) {
die "--size must be 10 or greater\n";
}
my $fontfile = shift;
my $outfile = shift;
@ARGV
or usage();
my $text = "@ARGV";
my $angle_rads = $angle * (PI / 180);
my $trans;
# this is the only difference between rotation and shearing: the
# transformation matrix
if ($rotate) {
$trans = Imager::Matrix2d->rotate(radians => $angle_rads);
}
else {
$trans = Imager::Matrix2d->shear(x=>sin($angle_rads)/cos($angle_rads));
}
# only the Freetype 2.x driver supports transformations for now
my $font = Imager::Font->new(file=>$fontfile, type=>'ft2')
or die "Cannot load font $fontfile: ", Imager->errstr, "\n";
$font->transform(matrix=>$trans);
my $bbox = $font->bounding_box(string=>$text, size=>$size);
# these are in font co-ordinates, so y is flipped
my ($left, $miny, $right, $maxy) =
transformed_bounds($bbox, $trans);
# convert to image relative co-ordinates
my ($top, $bottom) = (-$maxy, -$miny);
my ($width, $height) = ($right - $left, $bottom - $top);
my $img = Imager->new(xsize=>$width, ysize=>$height);
# fill with the background
$img->box(filled=>1, color=>$bg);
# and draw our string in the right place
$img->string(text => $text,
color => Imager::Color->new('white'),
x => -$left,
y => -$top,
color => $fg,
font => $font,
size => $size);
$img->write(file=>$outfile)
or die "Cannot save $outfile: ",$img->errstr,"\n";
sub transformed_bounds {
my ($bbox, $matrix) = @_;
my $bounds;
for my $point ([ $bbox->start_offset, $bbox->ascent ],
[ $bbox->start_offset, $bbox->descent ],
[ $bbox->end_offset, $bbox->ascent ],
[ $bbox->end_offset, $bbox->descent ]) {
$bounds = add_bound($bounds, transform_point(@$point, $matrix));
}
@$bounds;
}
sub transform_point {
my ($x, $y, $matrix) = @_;
return
(
$x * $matrix->[0] + $y * $matrix->[1] + $matrix->[2],
$x * $matrix->[3] + $y * $matrix->[4] + $matrix->[5]
);
}
sub add_bound {
my ($bounds, $x, $y) = @_;
$bounds or return [ $x, $y, $x, $y ];
$x < $bounds->[0] and $bounds->[0] = $x;
$y < $bounds->[1] and $bounds->[1] = $y;
$x > $bounds->[2] and $bounds->[2] = $x;
$y > $bounds->[3] and $bounds->[3] = $y;
$bounds;
}
sub usage {
print <<EOS;
Usage: $0 [options] fontfile outfile text...
Options:
--angle <angle> | -a <angle>
Set the slant angle in degrees, limited to -45 to +45. Default 30.
--size <pixels> | -s <angle>
Set the text size in pixels. Must be 10 or greater. Default: 20.
--foreground <color> | --fg <color> | -f <color>
Set the text foreground color. Default: white.
--background <color> | --bg <color> | -b <color>
Set the image background color. Default: black
--rotate | -r
Rotate instead of shearing. Default: shear
eg.
# shear
$0 -a 45 fontfiles/ImUgly.ttf output.ppm "something to say"
# rotate at 100 pixel font size, blue foregroune, white background
$0 -rs 100 -b white -f blue fontfiles/ImUgly.ttf output.ppm Imager
EOS
exit 1;
}
=head1 NAME
slant_text.pl - sample for drawing transformed text
=head1 SYNOPSIS
perl slant_text.pl [options] fontfile output text
Run without arguments for option details.
=head1 DESCRIPTION
This is a sample for drawing transformed text.
It's complicated by the need to create an image to put the text into,
if you have text, a font, and a good idea where it belongs, it's
simple to create the transformation matrix:
use Imager::Matrix2d;
# or call another method for shearing, etc
my $matrix = Imager::Matrix2d->rotate(radians=>$some_angle);
Feed the transformation matrix to the font:
$font->transform(matrix=>$font);
then draw the text as normal:
$image->string(string=>$some_text,
x => $where_x,
y => $where_y,
font => $font,
size => $size);
But if you do need the bounds, the code above does show you how to do
it.
=head1 FUNCTIONS
=over
=item transformed_bounds
Returns a list of bounds:
(minx, miny, maxx, maxy)
These are offsets from the text's starting point in font co-ordinates
- so positive y is I<up>.
Note: this returns the bounds of the transformed bounding box, in most
cases the actual text will not be touching these boundaries.
=cut
=back
=head1 AUTHOR
Tony Cook <tony@imager.perl.org>
=head1 REVISION
$Revision$
=head1 SEE ALSO
Imager(1), Imager::Cookbook, Imager::Matrix2d
=cut