[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.3.19 TTF_GlyphMetrics

int TTF_GlyphMetrics(TTF_Font *font, Uint16 ch, int *minx, int *maxx, int *miny, int *maxy, int *advance)

font
The loaded font from which to get the glyph metrics of ch.
ch
the UNICODE char to get the glyph metrics for.
minx
pointer to int to store the returned minimum X offset into, or NULL when no return value desired.
maxx
pointer to int to store the returned maximum X offset into, or NULL when no return value desired.
miny
pointer to int to store the returned minimum Y offset into, or NULL when no return value desired.
maxy
pointer to int to store the returned maximum Y offset into, or NULL when no return value desired.
advance
pointer to int to store the returned advance offset into, or NULL when no return value desired.

Get desired glyph metrics of the UNICODE chargiven in ch from the loaded font.
NOTE: Passing a NULL font into this function will cause a segfault.

Returns: 0 on success, with all non-NULL parameters set to the glyph metric as appropriate. -1 on errors, such as when the glyph named by ch does not exist in the font.

 
// get the glyph metric for the letter 'g' in a loaded font
//TTF_Font *font;
int minx,maxx,miny,maxy,advance;
if(TTF_GlyphMetrics(font,'g',&minx,&maxx,&miny,&maxy,&advance)==-1)
    printf("%s\n",TTF_GetError());
else {
    printf("minx    : %d\n",minx);
    printf("maxx    : %d\n",maxx);
    printf("miny    : %d\n",miny);
    printf("maxy    : %d\n",maxy);
    printf("advance : %d\n",advance);
}

This diagram shows the relationships between the values:

metrics

Here's how the numbers look:
 
TTF_FontHeight       : 53
TTF_FontAscent       : 38
TTF_FontDescent      : -14
TTF_FontLineSkip     : 55
TTF_GlyphMetrics('g'):
        minx=1
        maxx=15
        miny=-7
        maxy=15
        advance=16

We see from the Line Skip that each line of text is 55 pixels high, including spacing for this font.
The Ascent-Descent=52, so there seems to be 3 pixels worth of space between lines for this font.

Let's say we want to draw the surface of glyph 'g' (retrived via 3.4.4 TTF_RenderGlyph_Solid or a similar function), at coordinates (X,Y) for the top left corner of the desired location. Here's the math using glyph metrics:
 
//SDL_Surface *glyph,*screen;
SDL_Rect rect;
int minx,maxy,advance;
TTF_GlyphMetrics(font,'g',&minx,NULL,NULL,&maxy,&advance);
rect.x=X+minx;
rect.y=Y+TTF_FontAscent(font)-maxy;
SDL_BlitSurface(glyph,NULL,screen,&rect);
X+=advance;

Let's say we want to draw the same glyph at coordinates (X,Y) for the origin (on the baseline) of the desired location. Here's the math using glyph metrics:
 
//TTF_Font *font;
//SDL_Surface *glyph,*screen;
SDL_Rect rect;
int minx,maxy,advance;
TTF_GlyphMetrics(font,'g',&minx,NULL,NULL,&maxy,&advance);
rect.x=X+minx;
rect.y=Y-maxy;
SDL_BlitSurface(glyph,NULL,screen,&rect);
X+=advance;

NOTE: The only difference between these example is the +TTF_FontAscent(font) used in the top-left corner algorithm. NOTE: These examples assume that 'g' is present in the font!
NOTE: In practice you may want to also subtract TTF_GetFontOutline(font) from your X and Y coordinates to keep the glyphs in the same place no matter what outline size is set.

See the web page at The FreeType2 Documentation Tutorial for more.

Any glyph based rendering calculations will not result in accurate kerning between adjacent glyphs. (see section Kerning)

See Also:
3.3.10 TTF_FontHeight,
3.3.11 TTF_FontAscent,
3.3.12 TTF_FontDescent,
3.3.13 TTF_FontLineSkip,
3.3.20 TTF_SizeText,
3.3.21 TTF_SizeUTF8,
3.3.22 TTF_SizeUNICODE,
3.3.18 TTF_GlyphIsProvided,
3.3.4 TTF_GetFontOutline


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on November, 13 2009 using texi2html