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

3.3.12 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 from
ch
the UNICODE char of which to get the glyph metrics for
minx
poiter to int to store the retuned minimum X offset into, or NULL when no return value desired.
maxx
poiter to int to store the retuned maximum X offset into, or NULL when no return value desired.
miny
poiter to int to store the retuned minimum Y offset into, or NULL when no return value desired.
maxy
poiter to int to store the retuned maximum Y offset into, or NULL when no return value desired.
advance
poiter to int to store the retuned 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 digram shows the relationships between the values:

metrics

Here's how the numbers look:
 
TTF_FontHeight       : 33
TTF_FontAscent       : 26
TTF_FontDescent      : -6
TTF_FontLineSkip     : 33
TTF_GlyphMetrics('g'):
        minx=0
        maxx=21
        miny=0
        maxy=21
        advance=24

We see from the Line Skip that each line of text is 33 pixels high, including spacing.
The Ascent-Descent=32, so there seems to be one pixel worth of space minimum between lines.

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,miny,advance;
TTF_GlyphMetrics(font,'g',&minx,NULL,&miny,NULL,&advance);
rect.x=X+minx;
rect.y=Y+miny;
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,miny,advance;
TTF_GlyphMetrics(font,'g',&minx,NULL,&miny,NULL,&advance);
rect.x=X+minx;
rect.y=Y-TTF_FontAscent(font)+miny;
SDL_BlitSurface(glyph,NULL,screen,&rect);
X+=advance;

NOTE: These examples assume that 'g' is present in the font!

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.4 TTF_FontHeight,
3.3.5 TTF_FontAscent,
3.3.6 TTF_FontDescent,
3.3.7 TTF_FontLineSkip,
3.3.13 TTF_SizeText,
3.3.14 TTF_SizeUTF8,
3.3.15 TTF_SizeUNICODE,


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

This document was generated on May, 2 2005 using texi2html