Page 1 of 1

Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T05:51:05-07:00
by sethcall
This part of the ImageMagick usage manual is very relevant to my question:
http://www.imagemagick.org/Usage/text/#overflow

Let's say I'm using a very weird font that is 'spilling' out of it's bounding box (as the font does in the linked part of the manual). I'd like to be able to 'best fit to max size possible' as well as do a true center with such a font.

This means, to me, that ImageMagick would have to ignore the raw font info, and instead center/size based on what the actual rendered font does with the given text. Is this possible?

By the way, I have already thought of some workarounds; and they are OK, but I wanted to make sure I'm not missing out on a feature of ImageMagick...

Thanks!

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T06:56:24-07:00
by sethcall
This problem isn't really limited to strange fonts (they just really bring it out).

Take an example with Arial:

Code: Select all

convert -background '#dcdcdc'  -fill black  -size 300x150 -undercolor dodgerblue -gravity center   -font arial.ttf -alpha on label:"Arial" arial.bmp
Image


I colored the 'undercolor'. I'm not sure what ImageMagick is centering on, but it's clearly not just the blue 'undercolor' area.

But even assuming that the text were centered exactly on the undercolor area, the text would not 'true center' vertically (i.e., even if the bounding box highlighted in blue were centered exactly, still the visible font would sit a little high.

And so this is the feature I'm looking for; center based on what's visible... or barring that, centering based on the bounding box.

Any suggestions?

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T08:45:18-07:00
by snibgo
It depends on exactly what you mean by centering. For many purposes, font metrics are irrelevant, and only the bounding box matters. "-trim" or "-format %@" gives that, so you than have text that fits tightly in a box, to which you can add a border or extent, or resize it, or composite over another image.

IM won't choose the largest size that fits a bounding box. You can create a version you know will be larger than required, then resize down.

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T11:07:46-07:00
by sethcall
Hi Snibgo, thanks for responding. I'll play with these settings and see how much closer I can get.

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T14:57:23-07:00
by sethcall
OK, got it.
Use ImageMagick to render the font, and just crop it out (-trim) and layer it (-composite) it where I need it to be.

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-23T17:01:48-07:00
by fmw42
What version of Imagemagick are you using? An update might fix the issue.

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-24T04:20:30-07:00
by sethcall
@fmw42 A very recent version: 6.9.1-10 -- I tried MacOSX (via homebrew) as well as Ubuntu (from source).

The issue seems fundamental to me. am I the first to be bothered by it? That would seem unlikely.

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-24T11:33:05-07:00
by fmw42
IM is going to fill the box, in general, as closely as it can give the available fonts in the font set, as I understand it. So there may not be a font size that works perfectly. There were some recent fixes, so you might try checking the changelog or simply upgrading to the latest version.

For this command:

Code: Select all

convert -size 300x150 -background "#dcdcdc" -fill black -gravity center \
-undercolor dodgerblue -font arial label:"Arial" arial.gif
I get:
Image

The image seems to me to be vertically centered, but off slightly horizontally, which could just be due to the font glyphs and the fonts sizes available.

Is your result different?

I am using IM 6.9.2.4 Q16 Mac OSX

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-24T12:18:39-07:00
by sethcall
Man. I'm embarrassed. I should have upgraded earlier. So, yes, after upgrading, I get exactly what you get on both MacOSX and Ubuntu 12.

The bounding box is much more centered now (still, I do wonder... what exactly is IM centering on, if not the bounding box)?

Thanks @fmw42. Now I'll give my funkier fonts a try, too...

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-24T19:56:47-07:00
by fmw42
centering on the image not the undercolor

Re: Centering text based on what's visible (vs glyph info)

Posted: 2015-10-25T08:51:01-07:00
by sethcall
You mean on the rendered text? I'm sure it's not doing that based on the behavior of 'weird' fonts I've tried; by weird, I mean fonts that have their rendered font spill out of the undercolor area by a large amount. They come out *very* uncentered. I'll try to find a font that's on the internet and free so I can demo it here. I think it's more about where the 'cursor' starts from, and ultimately ends up at, if the user were to type it out in a text editor (I don't know the correct font terminology for this).