Page 1 of 1

ttf fonts are offset

Posted: 2008-01-13T21:04:41-07:00
by Wolfman
I've stumbled across this issue and as noone was able to pin down the problem, I can only assume that this is a bug, even though it would surprise me, as someone else would certainly have come across it before. Sorry if it is no bug and I'm just being ignorant. ;)

Nevertheless, here's the problem:
when drawing ttf-fonts on an image, the text seems to be vertically offset by a few pixels. This offset seems to vary by font and and font size. As I have to calculate font positions, this leaves me with some ugly spaces where none should be.

Examples are based on this code:

Code: Select all

convert -size 800x120 xc:#000000 -gravity center -font fonts/arial.ttf -pointsize 120 -fill "#ffffff" -draw "text 0,0 'Alea iacta est g'"  -quality 100 test.jpg
(the g is added for demonstration purposes)

http://www.mediafire.com/imageview.php? ... ix&thumb=6
http://www.mediafire.com/imageview.php? ... hx&thumb=6
http://www.mediafire.com/imageview.php? ... tm&thumb=6

(current version of IM running under Win2k)

Re: ttf fonts are offset

Posted: 2008-01-30T17:55:56-07:00
by anthony
This is NOT a bug!

Your canvas is just not big enough to contain that text, at that point size.

IM centers fonts based on the fonts defined 'drawing box' for the font. If you would like to see this box set "-undercolor red" which gets the font library to draw the drawing area in red.

The large amount of space above the text is actuall for line spacing as defined by the pointsize. Remember pointsize actually represents line spacing, and NOT text size. though they are related.
See IM Examples, Resolution, Pointsize, and Actual Font Size
http://imagemagick.org/Usage/text/#pointsize

IM can size the image to fit that drawing box by using "label:" (this ignored the -undercolor setting)

Code: Select all

   convert -background black  -fill white -font Arial -pointsize 120 label:'Alea iacta est g' image.jpg
this you will see did contain ALL the text, but the image is larger.

If you want a true center regardless of the fonts 'drawing area' the best way is to create a label image, the -trim that image. this can then be center overlaid onto a canvas the size you want. Of course if the canvas is not big enough, then the overlay will still be cropped, but it will be correctly centered.

Code: Select all

convert -background black  -fill white -font Arial \
    -pointsize 120 label:'Alea iacta est g' -trim +repage \
    -size 800x120 xc:black +swap -gravity center -composite \
    image.jpg
For more information on what the above is doing, see IM examples. Text handling, Cutting and Bordering, and Alpha Composition.