How to use textGravity?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

How to use textGravity?

Post by Airon65 » 2017-04-04T20:35:32-07:00

My attempt is:

Code: Select all

	int w = 800, h = 600;
	
	Magick::Image img5( Magick::Geometry( w, h ), Magick::Color( "white" ) );	
	img5.font( "ComicSans" );
	img5.fontPointsize( 50 );
	img5.fillColor( Magick::Color( "maroon" ) );
	img5.strokeColor( Magick::Color( "red" ) );
	img5.strokeWidth( 2 );
	img5.textGravity( MagickCore::CenterGravity );
	img5.draw( Magick::DrawableText( w / 2, h / 2,  "Some text!" ) );
	img5.write( "images/output.png" );
	
Placing the text with the textGravity = CenterGravity at the center of image: w / 2, h / 2, I expect that image will be placed at the center of the image but it's not :) It placed at the right bottom side. Why does it work like this?

IM: 7.0.5-2 Q16 x86_64 2017-03-11

User avatar
dlemstra
Posts: 1383
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: How to use textGravity?

Post by dlemstra » 2017-04-04T23:00:44-07:00

I think w /2 and h /2 need to be 0 and 0 when you set the textGravity.
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate

Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to use textGravity?

Post by Airon65 » 2017-04-05T00:01:29-07:00

Hm... yeah looks like it works like this :)

That's not what I was looking for :) Is it possible to align the text at the center of given coordinates? I mean something like: anchorPointX = 0.5f, anchorPointY = 0.5f. When I use that command: img5.draw( Magick::DrawableText( 100, 200, "Some text!" ) ); I see the text starts drawing from 100x200. I wish to have the center of the text at 100x200.

I can do these steps to achieve that goal:
- Draw text on the temporary image
- trim() it
- Calculate: newX = 100 - ( thatTempImage.columns() / 2 ) and for rows() respectively
- mainImage.composite( thatTempImage, newX, newY, Magick::OverCompositeOp );

but it's.... something strange to me :) looks too hard for just aligning the text at the center of certain coordinates..

Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to use textGravity?

Post by Airon65 » 2017-04-05T00:37:28-07:00

Well... instead of it I've just developed a formula :)

Code: Select all

img5.textGravity( MagickCore::CenterGravity );
img5.draw( Magick::DrawableText( 100 - ( w / 2 ), 200 - ( h / 2 ),  "Some text!" ) );
it places a text at the 100x200 by center of it. (anchor point at the center of a text).

Airon65
Posts: 75
Joined: 2017-02-26T02:19:38-07:00
Authentication code: 1151

Re: How to use textGravity?

Post by Airon65 » 2017-04-05T01:27:31-07:00

I have a strange problem. Why this code doesn't work?

Code: Select all

std::cout << "Size: " << img5.columns() << "x" << img5.rows() << "\n";	
img5.textGravity( MagickCore::CenterGravity );
img5.draw( Magick::DrawableText( 100 - ( img5.columns() / 2 ), 0,  "Some text!" ) );
It returns me:
libc++abi.dylib: terminating with uncaught exception of type Magick::ErrorDraw: first: non-conforming drawing primitive definition `path' @ error/draw.c/DrawImage/3269
Abort trap: 6
If I have:

Code: Select all

std::cout << "Size: " << img5.columns() << "x" << img5.rows() << "\n";	
img5.textGravity( MagickCore::CenterGravity );
img5.draw( Magick::DrawableText( 100 - ( w / 2 ), 0,  "Some text!" ) ); // "w" defined above and equals to 800
It returns me:
Size: 800x600
But why the first case doesn't work? Why can't I use "img5.columns()" in my calculations as the argument? What's the magic?

Post Reply