[Tutorial] Short: Calculating width of textdraws
#7

This works for ASCII:
Code:
new const characterWidths[] = {
	//Data taken from singleplayer fonts.dat file
	 0,  15,  15,  15,  15,  15,  15,  15,	//NULL is not displayable because it breaks off the string, others display as a space
	15,  15,  15,  15,  15,  15,  15,  15,  //Display as a space
	15,  15,  15,  15,  15,  15,  15,  15,  //Display as a space
	15,  15,  15,  15,  15,  15,  15,  15,  //Display as a space
	15,   9,  17,  27,  20,  34,  23,  12,	//  ! " Ј $ % & '
	12,  12,  21,  20,  12,  14,  12,  15,	//( ) * + , - . /
	23,  15,  21,  21,  21,  21,  21,  21,	//0 1 2 3 4 5 6 7
	20,  21,  12,  12,  24,  24,  24,  19,	//8 9 : ; < = > ?
	10,  22,  19,  19,  22,  16,  19,  24,	//tmA B C D E F G
	22,  11,  16,  21,  15,  28,  24,  27,	//H I J K L M N O
	20,  25,  19,  19,  18,  23,  23,  31,	//P Q R S T U V W
	23,  19,  21,  21,  13,  35,  11,  21,	//X Y Z       ! _
	10,  19,  20,  14,  20,  19,  13,  20,	//! a b c d e f g
	19,   9,   9,  19,   9,  29,  19,  21,	//h i j k l m n o
	19,  19,  15,  15,  14,  18,  19,  27,	//p q r s t u v w
	20,  20,  17,  21,  17,  20,  15,  15,	//x y z     $ [ ]
	22,  22,  22,  22,  29,  19,  16,  16,	//А Б В Г Ж З И Й
	16,  16,  11,  11,  11,  11,  27,  27,	//К Л М Н О П Т У
	27,  27,  23,  23,  23,  23,  20,  19,	//Ф Ц Щ Ъ Ы Ь Я а
	19,  19,  19,  30,  14,  19,  19,  19,	//б в г ж з и й к
	19,   9,   9,   9,   9,  21,  21,  21,	//л м н о п т у ф
	21,  18,  18,  18,  18,  24,  19,  19,	//ц щ ъ ы ь С с ї
	20,  18,  19,  19,  21,  19,  19,  19,	//0 1 2 3 4 5 6 7
	19,  19,  16,  19,  19,  19,  20,  19,	//8 9 : A B C D E
	16,  19,  19,   9,  19,  20,  14,  29,	//F G H I J K L M
	19,  19,  19,  19,  19,  19,  21,  19,	//N O P Q R S T U
	20,  32,  21,  19,  19,  19,  19,  19,	//V W X Y Z А Б В
	19,  29,  19,  19,  19,  19,  19,   9,	//Г Ж З И Й К Л М
	 9,   9,   9,  19,  19,  19,  19,  19,	//Н О П Т У Ф Ц Щ
	19,  19,  19,  19,  21,  19,  10,   9	//Ъ Ы Ь Я С ї ' .
};
stock GetCharacterWidth© {
	if(c < 0) {
	    return 0;
	}
	if(c >= sizeof(characterWidths)) {
	    return 20; //Default width, also taken from singleplayer fonts.dat file
	}
	return characterWidths[c];
}
stock GetStringWidth(const string[]) {
	new width = 0;
	for(new i = 0, size = strlen(string); i < size; i++) {
	    width += GetCharacterWidth(string[i]);
	}
	return width;
}
Btw, ! is not 15, but space is. ! is 9.
And the widths should be multiplied by the texdraw letter size, not the texdraw width.

EDIT:
Usage for a single character is:
Code:
printf("%d", GetCharacterWidth('a'));
Usage for a string is:
Code:
printf("%d", GetStringWidth("abc"));
EDIT2:
Keep in mind that the final values depend on TextDrawLetterSize and TextDrawSetOutline.
Also don't forget to set TextDrawSetProportional to 1.
Reply


Messages In This Thread
Short: Calculating width of textdraws - by Alcatrik - 10.10.2016, 20:45
Re: Short: Calculating width of textdraws - by Crayder - 10.10.2016, 22:42
Re: Short: Calculating width of textdraws - by Dutheil - 11.10.2016, 18:33
Re: Short: Calculating width of textdraws - by Eoussama - 11.10.2016, 18:35
Re: Short: Calculating width of textdraws - by Crayder - 02.01.2017, 12:44
Re: Short: Calculating width of textdraws - by Ivan_Ino - 06.01.2017, 15:56
Re: Short: Calculating width of textdraws - by Freaksken - 18.09.2017, 17:52
Re: Short: Calculating width of textdraws - by kristo - 23.10.2018, 19:36

Forum Jump:


Users browsing this thread: 1 Guest(s)