• Advertisement

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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; i<strlen(text); i++)
{
char character = text[i];

sx = m_aCharPos[character].x;
sy = m_aCharPos[character].y;

dx = x+xinc;
dy = y+yinc;

sprite_width = m_aCharSize[character].cx;
sprite_height = m_aCharSize[character].cy;

m_ptDxDraw->Blit(m_tDDSurf, surf, dx, dy, sx, sy, sprite_width, sprite_height, p_bTransparent);

xinc += (sprite_width);
}
}


Zeblar Nagrim, Lord of Chaos

Share this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement