[magick-users] Locating end of a text (perl magick)
zentara
zentara1 at sbcglobal.net
Wed Dec 3 09:45:15 PST 2008
On Tue, 02 Dec 2008 18:08:18 +0100
Benoit VARVENNE <varvenne at genoway.com> wrote:
>Hello,
>
>I'm a quite new Image Magick user and I wish to locate ending position of a
>text on the image (text annotated through "Annotate" method).
> $image->Annotate(
> x => $X,
> y => $Y,
> font => 'arial.ttf',
> pointsize => $fontSize,
> undercolor => "white",
> skewX => 0,
> skewY => 0,
> fill => 'black',
> text => $myText
> );
>What I wish to obtain is something like :
> It's beginning position ($X) + length_of_text*fontSize
> == ending pixel on the image ???
>The above formula does not work and I'm not really used to such text
>manipulation.
>
>Can someone help me ?
>
>Thanks a lot
>Benoît
Here is an example, the secret is to give the same options to QueryFontMetrics
as you do in the Annotate. Please disregard the gravity suggestion is my other response...
the correct term in IM is align. Gravity positions the origin on the page.
See QueryMultilineFontMetrics() for multiline text; and always check the return
array values from it, to make sure what they are. They may change from IM version,
or the docs may be obsolete.
This example will split a sentence into words an automatically position the words.
#################################################3
#!/usr/bin/perl
use warnings;
use strict;
use Image::Magick;
my $image = Image::Magick->new;
$image->Set(size=>'500x600');
my $rc = $image->Read("xc:white");
my $str = 'Just Another Perl Hacker';
my (@words) = split ' ',$str;
#print join "\n", at words,"\n";
my ($x,$y) = (100,100);
foreach my $word (@words){
$image->Annotate(
pointsize => 24,
fill => '#000000ff', #last 2 digits transparency in hex ff=max
text => $word,
gravity => 'NorthWest',
align => 'left',
x => $x,
y => $y,
);
my ( $character_width,$character_height,$ascender,$descender,$text_width,
$text_height,$maximum_horizontal_advance, $boundsx1, $boundsy1,
$boundsx2, $boundsy2,$originx,$originy) =
$image->QueryFontMetrics(
pointsize => 24,
text => $word,
gravity => 'NorthWest',
align => 'left',
x => $x,
y => $y,
);
print "( $character_width, $character_height,
$ascender,$descender,
$text_width, $text_height,
$maximum_horizontal_advance,
$boundsx1, $boundsy1,
$boundsx2, $boundsy2,
$originx,$originy)\n";
$x = $x + $originx + $character_width/3; # add a space
print "_________________________________$x\n";
}
$image->Write("$0.png");
exit;
__END__
zentara
--
I'm not really a human, but I play one on earth.
http://zentara.net/Remember_How_Lucky_You_Are.html
More information about the Magick-users
mailing list