multiple lines of 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
Wolfman

multiple lines of text

Post by Wolfman »

Hello,

I'm having troubles aligning text the way I want.

What I'd like to achieve is this:
I have an empty image that holds 1 to 6 lines of text. Each line of text can be of a different font size.

I was fiddling around for quite a while and ended up with this:

Code: Select all

convert -size 400x118 xc:#ffffff -fill black -draw "rectangle 0,22 400,97" -gravity north -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -draw "translate 0,22 text 0,0 'alea iacta est'" -font fonts/arial.ttf -pointsize 15 -fill "#ff0000" -draw "translate 0,52 text 0,0 'alea iacta est'" -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -draw "translate 0,67 text 0,0 'alea iacta est'" -quality 100 test.jpg
It's getting close but the alignment isn't 100% correct. Of course I could fix that by further fiddling with translate, but I'm wondering if there is a better solution.

Any help would be appreciated.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: multiple lines of text

Post by Bonzo »

Try -annotate:

Code: Select all

convert -size 400x118 xc:#ffffff -fill black -draw "rectangle 0,22 400,97" -gravity north -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -annotate +0+22 "aiacta est" -font fonts/arial.ttf -pointsize 15 -fill "#ff0000" -annotate +0+52 "alea iacta est" -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -annotate +0+67 "alea iacta est" -quality 100 test.jpg
Wolfman

Re: multiple lines of text

Post by Wolfman »

Thank you, but somehow I fail to see the difference. Or were you just trying to offer a different way of getting the same result?

My problem is that apparently the fonts come with some sort of margin... and this margin changes by pointsize, which makes it very hard to dynamically align the text. For example, the spacing between the first and second line is greater than between the second and the third. Somehow I fail to see why so I was wondering if there is some sort of magick command so I don't have to manually calculate the position of each line.

I even though of creating 3 images... each containing one line, and then copying those into the final image, thus avoiding the spacing problem. Yet this isn't very usable because I'm calling IM through PHP and the server blocks too long for several calls of convert, resulting in very low responsiveness.

Any ideas?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: multiple lines of text

Post by Bonzo »

With -annotate you specify the position of the font.
+0+0 is the x and y positions and you can overlap the text if you wanted.

As you are using php you could calculate the positions and use variables for the +0+0 e.g.
$offset = "+5";
-annotate {$offset}+0
( athough I have not tried this).
Wolfman

Re: multiple lines of text

Post by Wolfman »

Right, this is exactly what I'm doing but the problem is that the font comes with some sort of margin. If you specify pointsize 30, you have 28 pixels from the top of a capital i to the bottom of a g plus a margin of 6 pixels on top of the i.

Let me illustrate if you can spare the time to try this:

Code: Select all

convert -size 400x118 xc:#000000 -fill black -gravity north -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -draw "text 0,0 'alea iacta est'" -quality 100 test1.jpg
As you can see, the text is not hitting the upper border of the image as it should (or I think it should).

In order to achieve this, you have to do

Code: Select all

convert -size 400x118 xc:#000000 -fill black -gravity north -font fonts/arial.ttf -pointsize 30 -fill "#ff0000" -draw "translate 0,-6 text 0,0 'alea iacta est'" -quality 100 test2.jpg
Now if you try the first example with pointsize 15, you will have to translate by -3, which is the problem. As I don't know the font sizes I will have to use (these are set by the user), I would have to check the translation values for all font sizes and maybe even for all fonts I want to use.

The question is, is this intended, perhaps being a feature of ttf fonts that I don't understand, or could this even be a bug in IM?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: multiple lines of text

Post by anthony »

It isn't a margin.

The pointsize of a font actually has nothing to do with the look or the size of the drawn text in the font. You can have a 100 point font and have text that is barely readable!!!! It would not be a very good or useful font, but it is a perfectally legal font.

The pointsize of a font defined the distande from one line of that font to the next line of the font on a page. The draw size, within that can can be anything at all!!!!

The size of the text in that space is purely up to the manufacturer of the font.
See Resolution, Pointsize, and Actual Font Size
http://imagemagick.org/Usage/text/#pointsize

If you want to control the font more you will have to somehow read the other attibutes of the font. Specifically the fonts accent and decend dimentions (draw area above and below the baseline), and adjust the point size until you get want you want.

You can read these attibutes in a ImageMagcik API like PerlMagick.

However even then the size of the drawn object or shape does not have to follow the reported attributes!!!! It could exceed those dimentions or be very small within that drawing area. Think for example of a 'period' character.

The only way to find out the real drawn area is to draw the font.
Examples of doing this is are in IM Examples...
http://imagemagick.org/Usage/text/#font_info

Basically how well a font 'behaves' itself depends on how good the font maker is. Special Shape or 'dingbat' type fonts for example are notoriously bad as the maker usually just wants the shape and does not take the time to get the attributes correct.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Wolfman

Re: multiple lines of text

Post by Wolfman »

Thanks for your reply, I had already feared it comes down to the font file. Obviously, this is quite odd as this makes it impossible to reliably place multiple lines of text in a way that will keep the same margin between each line.

Now I just need to sell this fact to my customer. :shock:
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: multiple lines of text

Post by anthony »

If you want you can create each line as a separate image, and 'trim' it. Then you can append with a fixed margin between each line.
"montage" can do that very simply with the right options.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Wolfman

Re: multiple lines of text

Post by Wolfman »

I was thinking of that even though it would be a major performance issue.

Also, when looking at this sample pic here
http://www.mediafire.com/imageview.php? ... ix&thumb=6,
how would you deal with the offset problem? I guess I could try and make the image 20% larger than the pointsize, but how would I 'trim' it? Can IM do that?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: multiple lines of text

Post by anthony »

Read Im Examples. Text to Image and Cutting and Bordering sections.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Wolfman

Re: multiple lines of text

Post by Wolfman »

I would like to thank you for your help. I now have two solutions: the first being the one created by a single command but with the downside that lines appear to be offset a bit, the second works with up to seven files with the advantage that lines are properly aligned, even though at the cost of six times the processing time.

As a note: wouldn't it be worth the consideration to change the font rendering functions of IM in a way that automatically trims the fonts for proper alignment? I might even give it shot myself even though my C is a bit rusty. :D
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: multiple lines of text

Post by Bonzo »

There is this function built into php but it depends on how php was installed with ttf support.

http://uk2.php.net/imageftbbox
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: multiple lines of text

Post by anthony »

Wolfman wrote:As a note: wouldn't it be worth the consideration to change the font rendering functions of IM in a way that automatically trims the fonts for proper alignment? I might even give it shot myself even though my C is a bit rusty. :D
IM is doing the right thing. What you are requesting is something that is not normal text handling usage.

You can do the job in a single command.

Hmmm where did you get the "works with up to seven files" bit from?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Wolfman

Re: multiple lines of text

Post by Wolfman »

IM is doing the right thing.
What is this statement based on? Is there a special reason I am not aware of or do you basically mean "it's always been this way, so it must be right"? I guess the space above the A could be reserved for Ä or Á, but even then it would make perfectly sense to me to properly aling the text. After all, when someone places a text with gravity center, he wouldn't want to have the text offset by a couple of pixels.
Hmmm where did you get the "works with up to seven files" bit from?
One command for each line, looking like this

Code: Select all

convert -size 400x46 xc:#cccccc -fill "#990000" -gravity center -pointsize 35 -annotate +0+0 "Alea iacta est g" -trim +repage abc1.gif
And then a command to put it all together

Code: Select all

convert -size 400x118 xc:#cccccc -gravity north abc1.gif -geometry +0+2 -composite abc2.gif -geometry +0+40 -composite abc3.gif -geometry +0+57 -composite abc4.gif -geometry +0+74 -composite abc5.gif -geometry +0+85 -composite abc6.gif -geometry +0+96 -composite -quality 100 final.jpg
Don't think there's a way to do that with a single command, but then again I'm not an IM expert.
Post Reply