PDA

View Full Version : Finding text length



Rob
04-09-2001, 11:41 AM
Is there a way to find the length of a bitmap font string? For example, if I want bto display a string of arbitrary number of characters and have it appear centered about a point, how do I find where to set my rasterPos?

If there is no OpenGL approach to this, can any Windows gurus give me some clues to find the length of a string printed using a proportional font?

holocaust
04-09-2001, 03:04 PM
Well (Use the beginner forum for these kind of questions ;) ),

since you are using a non-proportionnal font (width and height are the same for every chars) the easiest way is to use something like strlen(my_string) * char_fixed_width.
ex. :



// p_font is a pointer on a font info structure
char * p_string = "opengl";
long len, char_fixed_width;

char_fixed_width = fontGetCharWidth(p_font);
len = strlen(p_string) * char_fixed_width;


if char_fixed_width value is 16 you'll get 96, easy....

[This message has been edited by holocaust (edited 04-09-2001).]

For proportionnal font, your font structure should keep track of every char width and height. Then you code your own version of strlen that compute a simple sum with each char of the string passed an argument. Something like :



long my_strlen (font_t * p_font, char * p_string)
{
long sum, string_len, i;

sum = 0;
string_len = strlen(p_string);

for (i = 0 ; i < string_len ; i++)
sum += p_font->char_info[p_string[i]].width;

return sum;
}


It is quite basic but the idea is there...

[This message has been edited by holocaust (edited 04-09-2001).]