#### Archived

This topic is now archived and is closed to further replies.

# Calc font dim

This topic is 5828 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I try calculate the width of diffrent characters in a font before I blit them to a surface, but GetTextExtentPoint32 don´t give me the correct dimension. When I blit all the characters to the screen they came to close to each other.
  m_aCharSize[256]; ... char buffer[2]; for(int i=0; i<256; i++) { buffer[0] = (char) i; GetTextExtentPoint32(l_thDCScreen, buffer, 1, &m_aCharSize[i]); if(m_aCharSize[i].cy > l_szBiggestChar.cy) l_szBiggestChar.cy = m_aCharSize[i].cy; if(m_aCharSize[i].cx > l_szBiggestChar.cx) l_szBiggestChar.cx = m_aCharSize[i].cx; } 
How you do when you compute the width and height of a character? Zeblar Nagrim, Lord of Chaos

##### Share on other sites
Don''t call GetExtent on each character. Instead, call it on the entire string. That way, it will correctly calculate the character spacing in addition to the widths of each individual character.

##### Share on other sites
Thanks, but my DrawText function need to know the dimension of each character:

  void CGUIFont::DrawText(LPDIRECTDRAWSURFACE7 surf, char *text, int x, int y, bool p_bTransparent){ int sx=0, sy=0, dx=0, dy=0, sprite_width=m_szSurfDim.cx, sprite_height=m_szSurfDim.cy; int xinc=0, yinc=0; for(int i=0; iBlit(m_tDDSurf, surf, dx, dy, sx, sy, sprite_width, sprite_height, p_bTransparent); xinc += (sprite_width); }}

Zeblar Nagrim, Lord of Chaos

##### Share on other sites
OK, then you have a few options:

1) Use a constant spacing between letters.

2) Use a constant spacing that you calculate by taking, say, half of the first letter.

3) Use a constant spacing that you calculate by finding the entire string length with GetExtent minus the sum of the individual char widths divided by the number of chars - 1.

4) Calculate each char space exactly by doing a GetExtent on a char and the one next to it, determining the space by subtracting the width of the two chars.

I''m sure there are more, but this should give you a few ideas.

##### Share on other sites
Sorry my fault! I didn´t select the font object into the device context when I created the blit surface. I was nothing wrong with the win32 function.

Thanks.

Zeblar Nagrim, Lord of Chaos