Page 1 of 1

glyph bounding boxes

Posted: 2009-09-17T16:18:42-07:00
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);

Re: glyph bounding boxes

Posted: 2009-09-18T00:05:15-07:00
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.

Re: glyph bounding boxes

Posted: 2009-09-18T17:03:20-07:00
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

Re: glyph bounding boxes

Posted: 2009-09-20T17:11:49-07:00
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