Drawing glyph bounding box on rendered text?



Hi all,

I'm trying to accurately draw a rectangle around each glyph (not character) in rendered text. I'm very close but things aren't quite lining up correctly. I've attached an image showing this, but if the mailing list filters it, you can also see it here: http://www.phonething.com/test.png Green box shows line extents, red boxes should show bounding box for each glyph.

As you can see, widths and X starting positions of the boxes are off slightly. (So is height, but I suppose I could lock that to the bottom of the line extents?)

Relevant code that drew that is:

double pango_to_ink = (1.0 / ((double) PANGO_SCALE)); // utility

[ create surface, context, layout, then do pango_layout_set_text() ]

[ for each line in layout use pango_layout_iter_get_line() : ]

pango_layout_iter_get_line_extents(pIter, &line_r, NULL);
pangolineX = pango_to_ink * ((double) line_r.x); // start position of this line of the layout
pangolineY = pango_to_ink * ((double) line_r.y);

[ get each run in current line: ]

for (i = 0; i < pRun->glyphs->num_glyphs; i++) // loop through glyphs in current run
     {

pango_font_get_glyph_extents(font, glyphID, NULL, &glyphbox); // Just to get glyph height as it's not in the geometry

glyphX = pangolineX + pango_to_ink * ((double) pRun->glyphs->glyphs[i].geometry.x_offset); glyphY = pangolineY + pango_to_ink * ((double) pRun->glyphs->glyphs[i].geometry.y_offset); glyphW = pango_to_ink * ((double) pRun->glyphs->glyphs[i].geometry.width);
     glyphH = pango_to_ink * ((double) glyphbox.height);

      cairo_rectangle(context, glyphX, glyphY, glyphW, glyphH - glyphY);
      cairo_stroke(context);

      }
....

Where am I going wrong in getting the glyph bounding boxes accurately? (this needs to also work for Indic, Hebrew, Arabic, Chinese and so on) It doesn't matter if the box has empty space, i.e. incorporates the distance between glyphs, though knowing the actual glyph bounding box AND the space separating it from the next glyph
would be ideal.

Thanks and regards,
Alex

Attachment: test.png
Description: PNG image



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]