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

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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!
Last edited by sethcall on 2015-10-23T08:20:42-07:00, edited 1 time in total.
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post by sethcall »

Hi Snibgo, thanks for responding. I'll play with these settings and see how much closer I can get.
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

What version of Imagemagick are you using? An update might fix the issue.
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

centering on the image not the undercolor
sethcall
Posts: 15
Joined: 2015-10-23T05:47:36-07:00
Authentication code: 1151

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

Post 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).
Post Reply