Line breaks when drawing text

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
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Line breaks when drawing text

Post by SimonH »

Hi Guys!

I hope you can help me with a problem I've been knocking my head against.
I want to use ImageMagick to produce an image of a DIV on a web page. The DIV has width, height, font name and font size, so I use the command;

Code: Select all

convert -size 466x108 -background transparent -fill #000000 -font "Font.ttf" -pointsize 50 -gravity north caption:"This is a sample of TEXT" divImage.png
The trouble is that what I get doesn't seem to match up, as can be seen from this image;
Image
The ImageMagick generated text is the same size but it breaks in a different place and has a wider line spacing.
How can I make the two match up? I also need to do this with many different fonts and texts!

Any help would be much appreciated,
Simon
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Line breaks when drawing text

Post by fmw42 »

I get a similar result in IM 6.8.5.1 Q16 Mac OSX Snow Leopard using


convert -size 466x108 -background transparent -fill "#000000" -font arial -pointsize 50 -gravity north caption:"This is a sample of TEXT" show:


However, you have a conflict between the specified pointsize and the specified width and height. It cannot use the specified pointsize and fill the width exactly.

However, if you remove the pointsize it will allow it to fill the width and give you close to what you want.


convert -size 466x108 -background transparent -fill "#000000" -font arial -gravity north caption:"This is a sample of TEXT" show:

Alternately, use -annotate and put a line break where you want.

convert -size 466x108 xc:none -fill "#000000" -font arial -pointsize 50 -gravity center -annotate +0+0 "This is a sample of \nTEXT" show:
Last edited by fmw42 on 2013-04-26T11:59:31-07:00, edited 1 time in total.
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

Thanks for the swift reply!
The font I used was a google font (Bangers) and I got the file from there. Pretty similar to Arial.
Should I flag this up as a bug then? I don't want to seem bolshy on my first post!
Seems like the letter spacing and line spacing are causing the issue as individual letters are identical in size...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Line breaks when drawing text

Post by snibgo »

Different software will have slightly different rules for spacing and breaking text.

You might try "-interline-spacing" and other options.
snibgo's IM pages: im.snibgo.com
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

fmw42 wrote:However, you have a conflict between the specified pointsize and the specified width and height. It cannot use the specified pointsize and fill the width exactly.
However, if you remove the pointsize it will allow it to fill the width and give you close to what you want.
Unfortunately I have no say over the image size or font size as they are predetermined, I have to use them!
Am I going to have to make a list of interline spacings and kernings for every font? :(

Edit;
I tried the no -pointsize option but it's not that close;
Image
I don't think even kerning is going to solve this one!
Last edited by SimonH on 2013-04-26T12:38:27-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Line breaks when drawing text

Post by snibgo »

What are you trying to do? Different web browsers use different rules. If you need to reproduce the exact look of a particular browser, then screen-capture seems the obvious solution.
snibgo's IM pages: im.snibgo.com
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

snibgo wrote:What are you trying to do? Different web browsers use different rules. If you need to reproduce the exact look of a particular browser, then screen-capture seems the obvious solution.
It's for a labeling system so users can make up a label on a webpage and send them in for printing. No chance of screengrab - I wish I could! All the main browsers behave very much alike (nobody mention IE!), so I was rather expecting ImageMagick to be similar also. I don't understand why the second image above has left such a large space either side of the 'te', or why the bottom one doesn't actually fill the space?!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Line breaks when drawing text

Post by snibgo »

I suggest you use the same method to generate the label for both purposes -- for the user's approval on the web page, and for sending to the printer. If you use two different methods, you are in for a world of grief trying to ensure the two are the same. Not helped by the massively different resolutions of screen and printers.

Of course, the user might edit a form field, to type text, select the font and so on. But then you generate the image, and show that. Then "click OK to approve the image, or EDIT to re-edit the form."
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Line breaks when drawing text

Post by fmw42 »

Please identify the exact IM commands you used to make the images you are comparing.

We will need to get the same font to test your command.

You might install PANGO and use it. It is more flexible than caption:

see
http://www.imagemagick.org/Usage/text/#pango
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

fmw42 wrote:Please identify the exact IM commands you used to make the images you are comparing.
We will need to get the same font to test your command.
You might install PANGO and use it. It is more flexible than caption:
see
http://www.imagemagick.org/Usage/text/#pango
The command I used is;

Code: Select all

convert -size 113x50 -background transparent -fill #000000 -font "brawler.ttf" -pointsize 50 -gravity north caption:"text1" output.png
With and without the -pointsize option.
The font is the Google font 'brawler' available to download from here: http://www.google.com/fonts

I've downloaded pango, but I can't find any instructions on how to integrate it into ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Line breaks when drawing text

Post by fmw42 »

I've downloaded pango, but I can't find any instructions on how to integrate it into ImageMagick?
If you have PANGO installed and where IM can find it, then IM should automatically access if you reinstall IM.
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

fmw42 wrote:If you have PANGO installed and where IM can find it, then IM should automatically access if you reinstall IM.
IM is already in constant use on the server doing other tasks, so reinstalling it is not a minor task!
Unfortunately I can't even get pango to work locally on my machine (missing DLLs?) and I'm not sure that It's going to do what I need anyway...

Can anyone suggest another workaround for this problem? It's getting kinda urgent!
User avatar
GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

Re: Line breaks when drawing text

Post by GreenKoopa »

snibgo wrote:I suggest you use the same method to generate the label for both purposes -- for the user's approval on the web page, and for sending to the printer. If you use two different methods, you are in for a world of grief trying to ensure the two are the same. Not helped by the massively different resolutions of screen and printers.

Of course, the user might edit a form field, to type text, select the font and so on. But then you generate the image, and show that. Then "click OK to approve the image, or EDIT to re-edit the form."
I second this suggestion. The way a font is rendered is controlled by much more than size. If you NEED two results to be IDENTICAL, and for a proof this is a very legitimate need, you MUST generate them the same way. I can't imagine that every browser and operating system renders your web page identically. And even for one web browser, is it still true at every zoom and text size a user has set? For all past and future versions of the browser? This is not a matter of you not understanding or IM having a bug, this is a matter of there being variables beyond your control. Of course, you will still need to understand how IM handles text. fmw42 can get you pointed in the right direction better than most of us.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Line breaks when drawing text

Post by fmw42 »

My best suggestion was at the post above viewtopic.php?f=1&t=23282#p97697. You have a conflict in IM between size and pointsize and need to leave off the pointsize and let IM fill your area.
SimonH
Posts: 7
Joined: 2013-04-26T11:03:02-07:00
Authentication code: 6789

Re: Line breaks when drawing text

Post by SimonH »

GreenKoopa wrote:
snibgo wrote:I suggest you use the same method to generate the label for both purposes -- for the user's approval on the web page, and for sending to the printer. If you use two different methods, you are in for a world of grief trying to ensure the two are the same. Not helped by the massively different resolutions of screen and printers.

Of course, the user might edit a form field, to type text, select the font and so on. But then you generate the image, and show that. Then "click OK to approve the image, or EDIT to re-edit the form."
I second this suggestion. The way a font is rendered is controlled by much more than size. If you NEED two results to be IDENTICAL, and for a proof this is a very legitimate need, you MUST generate them the same way. I can't imagine that every browser and operating system renders your web page identically. And even for one web browser, is it still true at every zoom and text size a user has set? For all past and future versions of the browser? This is not a matter of you not understanding or IM having a bug, this is a matter of there being variables beyond your control. Of course, you will still need to understand how IM handles text. fmw42 can get you pointed in the right direction better than most of us.
Thanks for taking an interest! I'd love to use the same method for both but I'm pretty much tied to IM for printing, and it would be impossible to allow the user to quickly create a WYSIWYG label if the system had to download a new image for every keystroke!
I've found all major browsers to be surprisingly consistent in font rendering, easily good enough for my purposes - a pixel or two doesn't matter. Zoom isn't an issue as the actual pixel values remain the same. My problem is that IM renders fonts very differently to browsers - in every case I tried IM rendered the font with larger inter-character and inter-line spacings than the browsers did. I guess I just surprised that they are so different!
I've found a rather awkward work-around; by not automatically word-wrapping on the web page (ie forcing the user to hit enter for a new line) I can be sure that line breaks are in known places. By further creating a table of kerning and interline spacings for IM for all the fonts I use (*pant, pant!*) I can pretty closely match up the two. Not great but workable!
Post Reply