str_width improvements

Posted By: FlorianP

str_width improvements - 12/18/08 01:12

It would be really helpful to have the following functions in addition to str_width

foo(string, font, len)
Gives the width of the first [len] chars of string

and

foo(string, font, width)
Gives the number of chars that fit in [width] pixels

I know that this can be done quite easy with str_width but i think an intern solution would be much faster
Posted By: Joozey

Re: str_width improvements - 12/18/08 12:22

Code:
int str_widthpart( STRING* str, FONT* font, int len ) {
	STRING* temp="";
	str_cpy(temp, str);
	str_trunc( temp, str_len(str) - len );
	return str_width( temp, font );
}

int str_fit( STRING* str, FONT* font, int width ) {
	STRING* temp="";
	str_cpy(temp, str);
	
	var length=str_width(st, font);
	while( length > width && str_len( temp ) > 0) {
		str_trunc (temp, 1);
		length = str_width( temp, font );
	}
	
	return str_len( temp );
}

I don't have the option to use str_width, so I can't test. But that should work.
Posted By: FlorianP

Re: str_width improvements - 12/18/08 13:50

Thats not the point.
These functions are terribly slow and both should need less calculations than one str_width command - So it is like going from NY to LA with a stop in Moscow
Posted By: jcl

Re: str_width improvements - 12/18/08 14:04

Well, Joozey posted the functions you wanted. I don't see why you think ours whould be faster.
Posted By: FlorianP

Re: str_width improvements - 12/18/08 20:26

The way i think str_width works (simplified):

function str_width(string, font)
{
while (i < str_len(string) )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return width
}

so an internal solution could do the following

Give the number of chars that fit in [max_width]:
function str_widthanything (string, font, max_width)
{
while (width < max_width && i < str_len(string) )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return i - 1
}

Adds the sizes of the first [char_num] chars:
function str_widthanything (string, font, char_num)
{
while ( i < char_num )
{
width += Char_Size[ string[i] ] //Adding the width of each char of the string
i++
}
return width
}


So, if str_width works this way - don't u think it would be senseless calling str_width that many times instead of adding two modified versions of it?
Tell me if im wrong...
Posted By: Joozey

Re: str_width improvements - 12/18/08 21:22

What you can do to maximize speed; Store the width's of all the characters (e.g. via their ascii value) once at game start, and read the values out when nessecary.
Posted By: FlorianP

Re: str_width improvements - 12/18/08 21:37

Originally Posted By: Joozey
What you can do to maximize speed; Store the width's of all the characters (e.g. via their ascii value) once at game start, and read the values out when nessecary.


Of course - i can also write my own engine. I just thought it to be a useful feature and didnt ask for workarounds.
Please dont get me wrong, i appreciate that you want to help me, but it was just a suggestion to 'improve' a7
Posted By: jcl

Re: str_width improvements - 12/19/08 07:33

There is no defined "character width". The space of a character depends on its environment in a text and involves several parameters and look up tables.

For str_width we're just calling an OS function. We had to use it for any related functions just in the way Joozey suggested.
© 2024 lite-C Forums