c++ - How to get text width in FreeType? -


i inherited code author prints text (not in monospace font) using freetype , opengl.

i need calculate printed text width can align properly.

here code wrote:

freetype::font_data font; font.init(fontpath.c_str(), fontsize); freetype::print(font, x, y, "%s", str.c_str()); 

here freetype source print function.

i couldn't think of way text width modifying print function , tried editing font's init function (also in mentioned file) return face->glyph->metrics.width there exception saying face->glyph null. don't think should trying edit sources of libraries.

since have no clue how text width i'm considering somehow printing text, getting width of printed , printing on it. ideas on maybe?

if confined using latin characters, here simple , dirty way it.

you can iterate on glyphs, load each glyph , calculate bounding box:

int xmx, xmn, ymx, ymn;  xmn = ymn = int_max; xmx = ymx = int_min;  ft_glyphslot  slot = face->glyph;  /* small shortcut */ int           pen_x, pen_y, n;   ... initialize library ... ... create face object ... ... set character size ...  pen_x = x; pen_y = y;  ( n = 0; n < num_chars; n++ ) {   ft_uint  glyph_index;     /* retrieve glyph index character code */   glyph_index = ft_get_char_index( face, text[n] );    /* load glyph image slot (erase previous one) */   error = ft_load_glyph( face, glyph_index, ft_load_default );   if ( error )     continue;  /* ignore errors */    /* convert anti-aliased bitmap */   error = ft_render_glyph( face->glyph, ft_render_mode_normal );   if ( error )     continue;    /* now, draw our target surface */   my_draw_bitmap( &slot->bitmap,                   pen_x + slot->bitmap_left,                   pen_y - slot->bitmap_top );    if (pen_x < xmn) xmn = pen_x;   if (pen_y < ymn) ymn = pen_y;    /* increment pen position */   pen_x += slot->advance.x >> 6;   pen_y += slot->advance.y >> 6; /* not useful */    if (pen_x > xmx) xmx = pen_x;   if (pen_y > ymx) ymx = pen_y;  } 

but if want more professionally, think must use harfbuzz (or complex text shaping library). one-size-fits-all soultion, meaning once compiled, can use draw , measure not latin strings unicode ones. recommend use one.


Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -