Page 1 of 1

Writing a text with justify argument relative to a point

Posted: 2010-12-09T03:55:35-07:00
by cedk
Hello all,

I'm new on this forum and french, so sorry for my english... :?

So I want to write some text, relative to a certain pont, on an image, with the baseline of the text. I have found a solution but there is maybe a better one.
Here it is :

For the example, I write "jabp" to see the baseline, on an image 400x300, relative to the point 250,180. Let's see the point :

Code: Select all

convert -size 400x300 pattern:checkerboard -stroke rgba(0,0,255,.8) -draw "line 0,180 400,180" -draw "line 250,0 250,300" croix.png
Result :
Image

If I write text with "simple" position (-annotate +250+180), I wil have text written at the correct place, and justified on the left :

Code: Select all

convert -size 400x300 pattern:checkerboard -stroke rgba(0,0,255,.8) -draw "line 0,180 400,180" -draw "line 250,0 250,300" -fill rgba(255,255,255,.6) -stroke none -pointsize 50 -annotate +250+180 jabp just_left.png
Result :
Image

Now come the problems : I want to write centered or right justified... My solution is to get the metrics informations of the text box :

Code: Select all

convert xc: -pointsize 50 -debug annotate -annotate 0 japb null:
Result is :
2010-12-09T11:36:04+01:00 0:01 0.031u 6.5.6 Annotate convert[304]: annotate.c/RenderFreetype/1138/Annotate
Font c:\windows\fonts\arial.ttf; font-encoding none; text-encoding none; pointsize 50
2010-12-09T11:36:04+01:00 0:01 0.031u 6.5.6 Annotate convert[304]: annotate.c/GetTypeMetrics/732/Annotate
Metrics: text: japb; width: 95.2969; height: 58; ascent: 46; descent: -11; max advance: 100; bounds: -2.2968 8,-10 26,36; origin: 94,0; pixels per em: 50,50; underline position: -3.39063; underline thickness: 2.34375
2010-12-09T11:36:04+01:00 0:01 0.031u 6.5.6 Annotate convert[304]: annotate.c/RenderFreetype/1138/Annotate
Font c:\windows\fonts\arial.ttf; font-encoding none; text-encoding none; pointsize 50
Working in PHP, I found the "Metrics line", and then the width (95.2969). So for centered text for example, I just have to subtract the half of the width result (95.2969 / 2 ~= 47 ) to the "X" (250) annotate position (250 - 47 = 3) :

Code: Select all

convert -size 400x300 pattern:checkerboard -stroke rgba(0,0,255,.8) -draw "line 0,180 400,180" -draw "line 250,0 250,300" -fill rgba(255,255,255,.6) -stroke none -pointsize 50 -annotate +203+180 jabp just_center.png
Result is :
Image


I didn't find a solution using -gravity option (it takes the whole text box, and not relative to the baseline of the text). So if somebody have a better solution, preferably without using -debug ...

thanks

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-09T16:13:54-07:00
by anthony
This is a regular request.

The problem is that Gravity (positioning) and Justification (alignment to position) are tied together. They are related in that the default justification should depend on gravity, but there should be a separate justification setting.

I have no idea why it is still tied together, especially as in SVG (which IM low-level draw emulates) have Justification, even though it does not have gravity.

All I can suggest it request it from Cristy.

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-10T05:53:36-07:00
by cedk
Thank you for this rapide response !

Do you think it is possible to do it in one command line ?
And what do you mean by "All I can suggest it request it from Cristy."... What is Cristy ?

CedK

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-10T07:37:47-07:00
by anthony
Cristy is the development leader for Imagemagick, and is the one that knows the most about IM.
This is something that he will need to implement the core coding for such a new option, especially with regards to -annotate and -draw, functions. Later the gravity assisted image position functions also need to include the apprpriate justification handling.

Note that this is not just left,center,right justification, also but vertical justification too. (top, bottom, baseline, center)

The more people that request a separation of justification from gravity, the higher the 'job' goes on his ToDo list.

The way you doing it seems to be about the best technique for the time being.

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-10T12:07:11-07:00
by Bonzo
There is imagettfbbox( ) if you are using php.
http://php.net/manual/en/function.imagettfbbox.php

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-10T13:41:14-07:00
by cedk
Bonzo wrote:There is imagettfbbox( ) if you are using php.
http://php.net/manual/en/function.imagettfbbox.php
Thanks, Bonzo.
Actually, this is a function who uses gd library. And I want to change my php class from gd to im, for some reasons.

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-10T17:32:13-07:00
by anthony
My only suggestion is.. to DIY it. Which is basically what you are doing.


Create a large canvas. Annotate on that canvas at a known point. (no gravity)
That will give you the 'start point' and 'baseline' of the text on that canvas.
The baseline is the hard thing, and is what is lost by any gravity setting except -gravity none

Trim that image (do NOT +repage) and identify will then give you the bounds of the image relative to that start point.

With that information you can then figure out with maths where the start point should be located for a specific justification
and position.

Alternative while drawing on that canvas, extract the font information as per
http://www.imagemagick.org/Usage/text/#font_info
And figure it out from that.

However for specific things you can do some specific techniques.


There was a perl script that was doing similar things, but for the purposes of DIY word wrapping...
See IM forum discussion: viewtopic.php?f=7&t=3708

That was 2005 and users were needing the gravity-justification division way back then!

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-11T00:59:30-07:00
by javismiles
if they needed the justification-gravity division back in 2005, then, ehhh,
in 2011, is there any prediction as to when it will happen? :)

i am myself now a new FAN of the justification-gravity division :)
do we need to collect 1 million digital signatures to make it a reality? :)

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-11T10:18:43-07:00
by fmw42
I suggest that you post your suggestion to the Developers forum as an enhancement request and link back to this topic.

Re: Writing a text with justify argument relative to a point

Posted: 2010-12-12T04:01:36-07:00
by cedk
fmw42 wrote:I suggest that you post your suggestion to the Developers forum as an enhancement request and link back to this topic.
done : viewtopic.php?f=2&t=17662