Draw text to exact width...

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
jawzx
Posts: 11
Joined: 2013-04-24T14:17:28-07:00
Authentication code: 6789

Draw text to exact width...

Post by jawzx »

Ok, so i have a situation where i would like to ideally do the following: I have a known pixel value width (not the same for every image, but known) and a text string of variable length, not more than 15 characters, not less than 3. I have quite a bit of freedom on height (although it is ultimately contrained, not to the extent that my width is, and i don't expect it to be a limitation in this application) I would like to fit this variable text to my known width EXACTLY. using the caption function without a pointsize defined appears to take into account font metrics and leaves white space at the start and end of my string, but i want it to be exactly on the last pixels of the type... I realize i can do this with a trim and rescale, but due to variable image dimensions i would need to render my initial text very large to gurantee no visible scaling artifacts... so yes, I can do it without, but i would love to know if theres a way to EXACTLY limit the *drawn pixel* dimensions - either height, or width, with the other dynamic, and ignoring glyph metrics - of a text string? I can see other applications where this capability could be very usefull if it exits...

thanks!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Draw text to exact width...

Post by fmw42 »

Not that I know about that works perfectly for every case. Using label: or caption: without a pointsize and just specifying width (and not height) will do its best to fit the width. But there is no guarantee that it fits every width perfectly without having to do some slight trim due to the complicated iterations that it does.

If you text string is just to be on one line, then I would use label: rather than caption:

see the third example under http://www.imagemagick.org/Usage/text/#label_bestfit

I would be happy to learn that I am wrong about the above. So any one else please correct me if that is the case.
Post Reply