GD text positioning

I

IanW

I am writing some text on an image like this:

$im->stringFT($colour,"c:/winnt/fonts/verdana.ttf",8,0,10,10,"Processor
Usage");

The problem is the positioning. It seems to start 10 pixels from the left of
the image OK, but only 2 pixels down on the y coordinate. In fact, the
coords of the bottom left edge of the "P" in "Processor" is 10,10. Does it
really render it starting from there? That isn't even the bottom-left edge
of the bounding box either, because there's the tail of the "g" to consider.

Any ideas how can I accurately position the text?

Ian
 
D

Dave

IanW said:
I am writing some text on an image like this:

$im->stringFT($colour,"c:/winnt/fonts/verdana.ttf",8,0,10,10,"Processor
Usage");

The problem is the positioning. It seems to start 10 pixels from the left
of the image OK, but only 2 pixels down on the y coordinate. In fact, the
coords of the bottom left edge of the "P" in "Processor" is 10,10. Does it
really render it starting from there? That isn't even the bottom-left edge
of the bounding box either, because there's the tail of the "g" to
consider.

Any ideas how can I accurately position the text?

Ian

Yes it does start from there. It also returns the bounding box but you are
throwing away the return value, try:

my $bounds =
$im->stringFT($colour,"c:/winnt/fonts/verdana.ttf",8,0,10,10,"Processor
Usage");

and you will find that:
@bounds[0,1] Lower left corner (x,y)
@bounds[2,3] Lower right corner (x,y)
@bounds[4,5] Upper right corner (x,y)
@bounds[6,7] Upper left corner (x,y)(see the man page of GD for further
details)Dave
 
I

IanW

Yes it does start from there. It also returns the bounding box but you are
throwing away the return value, try:

my $bounds =
$im->stringFT($colour,"c:/winnt/fonts/verdana.ttf",8,0,10,10,"Processor
Usage");

that gives:

9,0-------------101,0
| |
| |
9,13-----------101,13

but the bottom-left edge of the P isn't the same as the bottom left edge of
the bounding box, partly because of the tail on the "g" but also because the
bounding box seems to add an extra pixel or two padding round the text, even
accounting for the anti-aliasing. So I'm still stuck on how to position it
accurately.

Regards
Ian
 
D

Dave

IanW said:
that gives:

9,0-------------101,0
| |
| |
9,13-----------101,13

but the bottom-left edge of the P isn't the same as the bottom left edge
of the bounding box, partly because of the tail on the "g" but also
because the bounding box seems to add an extra pixel or two padding round
the text, even accounting for the anti-aliasing. So I'm still stuck on how
to position it accurately.

Regards
Ian

With a true type font your starting position is effectively x where the
first character will start being drawn from and the y the baseline, the font
metrics will determine the bounding rectangle (imagine an invisible box
around each character and a line in the box showing where the baseline is).

The off-by-one issue you mention is not so related to fonts but is an
inherant issue in the idea of bounding rectangles (inclusive or
exclusive...) and is decided by convention in the tool you are using.

You should probably ignore the bounding box as such and just think in terms
of where you want the baseline (rather than the top corner) of your string
to start relative to the image.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,196
Messages
2,571,036
Members
47,631
Latest member
kukuh

Latest Threads

Top