glyph bounding boxes

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
ocitalis
Posts: 3
Joined: 2009-09-17T14:13:07-07:00
Authentication code: 8675309

glyph bounding boxes

Post by ocitalis »

I am a new user of ImageMagick++. I am attempting to create test images for optical character recognition analysis. For each glyph, I want to determine the bounding box coordinates of that glyph as it is added to the image. Some glyphs may be rotated slightly, and they will not necessary be aligned on a straight baseline. How do I determine the bounding box pixel coordinates for a single glyph that has been drawn onto my main image?
Using the code below, I can specify where the corner of the glyph begins, but after it is drawn I do not know the bounding box coordinates (the height and width of the glyph).

Code: Select all

	Image myimage("200x200", "white");
		myimage.magick("PNG");

		myimage.fillColor("black");
		myimage.fontPointsize(16);

		myimage.boxColor("orange");
		list<Drawable> draw_list;
		DrawableFont myfont("Arial");
		draw_list.push_back(myfont);
		DrawableText drawText(101, 50, "X");
		draw_list.push_back(drawText);
		myimage.draw(draw_list);
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: glyph bounding boxes

Post by anthony »

See Im examples... Determining Font Metrics
http://www.imagemagick.org/Usage/text/#font_info

The Font metrics in APIs can be retrieved using the appropriate QueryFont()
call for you API.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ocitalis
Posts: 3
Joined: 2009-09-17T14:13:07-07:00
Authentication code: 8675309

Re: glyph bounding boxes

Post by ocitalis »

Thank you for your reply. I think the QueryFont() call you are referring to in the C++ code is Image::fontTypeMetrics(..). When I call this, the information returned is not quite what I need.

Code: Select all

	Image myimage("100x50", "black");
	myimage.magick("PNG");
	myimage.antiAlias(false);
	myimage.matteColor("yellow");
	myimage.frame("1x1");
	myimage.fillColor("white");
	myimage.fontPointsize(16);
	myimage.font("Arial");
	myimage.boxColor("orange");

	TypeMetric typemetric;
	myimage.fontTypeMetrics("a", &typemetric);
	cout<<"Width: "<<typemetric.textWidth()<<" Height: "<<typemetric.textHeight();
	cout<<" Ascent: "<<typemetric.ascent()<<" Descent: "<<typemetric.descent();
	cout<<" MaxHorizAdvance: "<<typemetric.maxHorizontalAdvance()<<endl;
	myimage.draw( DrawableText(1, 16, "a"));

	myimage.fontTypeMetrics("f", &typemetric);
	cout<<"Width: "<<typemetric.textWidth()<<" Height: "<<typemetric.textHeight();
	cout<<" Ascent: "<<typemetric.ascent()<<" Descent: "<<typemetric.descent();
	cout<<" MaxHorizAdvance: "<<typemetric.maxHorizontalAdvance()<<endl;
	myimage.draw( DrawableText(14, 16, "f"));

	myimage.fontTypeMetrics("g", &typemetric);
	cout<<"Width: "<<typemetric.textWidth()<<" Height: "<<typemetric.textHeight();
	cout<<" Ascent: "<<typemetric.ascent()<<" Descent: "<<typemetric.descent();
	cout<<" MaxHorizAdvance: "<<typemetric.maxHorizontalAdvance()<<endl;
	myimage.draw( DrawableText(22, 16, "g"));

	myimage.write("forumpic.png");
In my example, the only value that varies from glyph to glyph is width. However, the glyphs clearly are not all the same height (see output image). "a", "f", and "g" do not appear to have the same ascent, descent, and height, yet fontTypeMetrics() says they do.

This is the screen output of the code.
Image

This is the image the code creates.
Image

How can I obtain metric information like what is shown in this pic?
Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: glyph bounding boxes

Post by anthony »

Ascent and Descent is the overall height of the font and the position of the baseline.
It is global over the whole font.

Same goes for MaxHorizonalAdvance.

You basically are drawing your font as a fixed with font. and probably with a far too big horizontal spacing!

I hope you don't mind my using that diagram on
http://www.imagemagick.org/Usage/text/#font_info
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply