Problems using MagickQueryFontMetrics

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
DavidChipman

Problems using MagickQueryFontMetrics

Post by DavidChipman »

Hi all,

I'm using the MagickWand C API. I am trying to get the dimensions of a text string using the MagickQueryFontMetrics function. The code involved is:

Code: Select all

			drawing_wand = NewDrawingWand();
			banner_txt_str_wand = NewMagickWand();
			font_name_size_sep_loc = g_strrstr(banner->txt_font_desc, font_name_size_sep);
			*font_name_size_sep_loc = '\0';
			font_size = g_ascii_strtod(++font_name_size_sep_loc, NULL);
			g_print("\t\tfont size = %f\n", font_size);
			MagickSetPointsize(banner_txt_str_wand, font_size);
			g_print("\t\tfont name = %s\n", banner->txt_font_desc);
			MagickSetFont(banner_txt_str_wand, banner->txt_font_desc);
			
			font_metrics = MagickQueryFontMetrics(banner_txt_str_wand, drawing_wand, banner->txt_str);
What am I doing wrong/missing? Thanks,

-David Chipman
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Problems using MagickQueryFontMetrics

Post by magick »

You must associate an image with your wand before you can compute font metrics. You can create a throw-away image with
  • MagickReadImage(magick_wand,"xc:");
DavidChipman

Re: Problems using MagickQueryFontMetrics

Post by DavidChipman »

Hi magick,

Thank you so much for your quick reply! Now, how do I get that text to show up in an image? Simply looking at the function descriptions doesn't help much, I'm afraid.

-David CHipman
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Problems using MagickQueryFontMetrics

Post by anthony »

Once you have finished your calculations on the text you would create the image the size needed (or use an existing image), and use a Annotate operator to 'draw' the text.

WARNING: Fonts lie! Sad but true. They often do not tell the truth about bounding boxes and extents.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problems using MagickQueryFontMetrics

Post by el_supremo »

Have a look at the code I posted in viewtopic.php?f=6&t=11586
It should get you started.

Pete
DavidChipman

Re: Problems using MagickQueryFontMetrics

Post by DavidChipman »

Hi Anthony,

Thanks for the info (and the warning!, I am seeing some odd numbers in the font_metrics array). Now, things still didn't work. I still end up with a 1-by-1 pixel image. What parts of the font_metrics need to be used in the MagickAnnotateImage() function? The code I currently have is:

Code: Select all

MagickAnnotateImage(banner_txt_str_wand, drawing_wand, font_metrics[7], font_metrics[10], 0.0, banner->txt_str);
"banner->txt_str" refers to a variable in a C struct that I have created to hold the information required to make the graphic I'm trying to make. It's a banner that starts with an image, has text in the middle, followed by another image. "txt_str" is the the text that goes in the middle. The array font_metrics is returned by the MagickQueryFontMetrics() function. Please help me out here.

-David Chipman
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problems using MagickQueryFontMetrics

Post by el_supremo »

DavidChipman wrote:

Code: Select all

MagickAnnotateImage(banner_txt_str_wand, drawing_wand, font_metrics[7], font_metrics[10], 0.0, banner->txt_str);
If you've added this statement to the code you previously posted, you still have a problem with the image in the magick wand because you haven't set its size so it defaults to 1x1. Also, The coordinates returned in the fontmetrics bounding box can be negative (especially [8] which is the bottom y coordinate). The width of the text will be fm[9]-fm[7] and the height will be fm[10]-fm[8]. You can then use these numbers to set the size of the wand before you do the annotate. I think your code would be something like this after you've got the fontmetrics:

Code: Select all

	MagickSetSize(banner_txt_str_wand,font_metrics[9]-font_metrics[7],font_metrics[10]-font_metrics[8]);
	MagickAnnotateImage(banner_txt_str_wand, drawing_wand, 0, 0, 0.0, banner->txt_str);
Pete
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Problems using MagickQueryFontMetrics

Post by el_supremo »

I've been playing around a bit more with font metrics and the info I gave you is wrong (sorry). But the method you're using still has problems.
When you use Annotate to write on an image, it gets all its information about the font from the *drawing* wand. Setting MagickSetFont will have no effect.
Also, using MagickSetSize will have no effect on the magick wand - it *sets* the size but does not *change* the size.
I also used the wrong info when setting the new size of the image. The bounding box gives info about the size of one letter. We need to use the information about the width and height of the text which are in elements [4] and [5] of the metrics.

This code should get you on the right track:

Code: Select all

	drawing_wand = NewDrawingwand();
	banner_txt_str_wand = NewMagickWand();
	MagickReadImage(banner_txt_str_wand,"xc:");

	// Set the font information in the drawing wand
	DrawSetFontSize(drawing_wand,font_size);
	DrawSetFont(drawing_wand,banner->txt_font_desc);
	
	// get the font metrics
	font_metrics = MagickQueryFontMetrics(banner_txt_str_wand, drawing_wand, banner->txt_str);
	// extend the size of the image - I think this is the right one to use
	// but works for me in this case
	MagickExtentImage(banner_txt_str_wand,font_metrics[4],font_metrics[5],0,0);
	// Annotate the image - Note the calculation of the y value which is 
	// because the text metrics use an origin in the lower left whereas IM has
	// its origin at top left
	MagickAnnotateImage(banner_txt_str_wand,drawing_wand, 0 ,font_metrics[8]+ font_metrics[5], 0.0 ,banner->txt_str);
	// Now write the magickwand image
Change the font name and size
Pete
DavidChipman

Re: Problems using MagickQueryFontMetrics

Post by DavidChipman »

Hi Pete, things are working now.. Thank you so much...
-David
Post Reply