| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# annotate_words.pl |
| 4 |
# |
| 5 |
# Take the internal string, split it into words and try to annotate each |
| 6 |
# individual word correctly, so as to control spacing between the words |
| 7 |
# under program control. |
| 8 |
# |
| 9 |
# A demonstration of using QueryFontMetrics(), by passing it exactly the same |
| 10 |
# arguments as you would for Annotate(), to determine the location of the |
| 11 |
# text that is/was drawn. |
| 12 |
# |
| 13 |
# Example script from Zentara |
| 14 |
# http://zentara.net/Remember_How_Lucky_You_Are.html |
| 15 |
# |
| 16 |
use warnings; |
| 17 |
use strict; |
| 18 |
use Image::Magick; |
| 19 |
|
| 20 |
my $image = Image::Magick->new; |
| 21 |
$image->Set(size=>'500x200'); |
| 22 |
my $rc = $image->Read("xc:white"); |
| 23 |
|
| 24 |
my $str = 'Just Another Perl Hacker'; |
| 25 |
my (@words) = split ' ',$str; |
| 26 |
#print join "\n",@words,"\n"; |
| 27 |
|
| 28 |
my ($x,$y) = (50,50); |
| 29 |
|
| 30 |
foreach my $word (@words){ |
| 31 |
|
| 32 |
$image->Annotate(font=>'Generic.ttf', |
| 33 |
pointsize => 24, |
| 34 |
fill => '#000000ff', #last 2 digits transparency in hex ff=max |
| 35 |
text => $word, |
| 36 |
gravity => 'NorthWest', |
| 37 |
align => 'left', |
| 38 |
x => $x, |
| 39 |
y => $y, |
| 40 |
); |
| 41 |
|
| 42 |
my ( $character_width,$character_height,$ascender,$descender,$text_width, |
| 43 |
$text_height,$maximum_horizontal_advance, $boundsx1, $boundsy1, |
| 44 |
$boundsx2, $boundsy2,$originx,$originy) = |
| 45 |
$image->QueryFontMetrics( |
| 46 |
pointsize => 24, |
| 47 |
text => $word, |
| 48 |
gravity => 'NorthWest', |
| 49 |
align => 'left', |
| 50 |
x => $x, |
| 51 |
y => $y, |
| 52 |
); |
| 53 |
|
| 54 |
print "$word ( $character_width, $character_height, |
| 55 |
$ascender,$descender, |
| 56 |
$text_width, $text_height, |
| 57 |
$maximum_horizontal_advance, |
| 58 |
$boundsx1, $boundsy1, |
| 59 |
$boundsx2, $boundsy2, |
| 60 |
$originx,$originy)\n"; |
| 61 |
|
| 62 |
my $n = $x + $originx + $character_width/3; # add a space |
| 63 |
print "Next word at: $x + $originx + $character_width/3 => $n\n"; |
| 64 |
$x = $n; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
$image->Write(magick=>'SHOW',title=>"Annotate Words"); |