SFML community forums
Help => Graphics => Topic started by: DeadLeaves on February 29, 2012, 11:10:11 pm
-
When calling sf::Text.GetGlobalBounds().width It will not count in starting or ending white spaces. Is this intentional? If so, is there a way to get the width with the added white space.
In this test, they all print the same.
sf::Text test1(" test");
sf::Text test2("test ");
sf::Text test3(" test ");
sf::Text test4("test");
printf("%f\n",test1.GetGlobalBounds().Width);
printf("%f\n",test2.GetGlobalBounds().Width);
printf("%f\n",test3.GetGlobalBounds().Width);
printf("%f\n",test4.GetGlobalBounds().Width);
Tested with SFML 2.0 Build 204
-
Heh, posted this a while ago as well:
http://www.sfml-dev.org/forum/viewtopic.php?t=6980
Answer is here as posted by Laurent: http://www.sfml-dev.org/forum/viewtopic.php?t=6672
-
I guess I also used the wrong search words. So basically, I have to write some stuff to compensate for it since it will not be fixed until an unknown date or not fixed at all.
-
Try adding the ASCII character with the dec code of 255. It's supposed to be a blank character I think and it seems to work for me (under windows).
-
Fixed it with this function.
int GetWidth( sf::Text *Text )
{
//Make sure its not empty, because this would crash it later otherwise.
if( Text->GetString().GetSize() == 0 )
return 0;
//Temp variables that use the same font and size as the original text.
//This is to get the width of one '#'
sf::Text Temp1( "#", Text->GetFont(), Text->GetCharacterSize() );
sf::Text Temp2( "# #", Text->GetFont(), Text->GetCharacterSize() );
//Now we can get the width of the whitespace by subtracting the width of 2 '#' from "# #".
int Width = Temp2.GetGlobalBounds().Width - Temp1.GetGlobalBounds().Width*2;
//The width of the string without taking into consideration for spaces left at the beggining or end.
int TotalWidth = Text->GetGlobalBounds().Width;
//Get the text from the sf::Text as an std::string.
std::string string = Text->GetString();
//Bool to see if we encounter a string consisting of only spaces.
bool OnlySpaces = false;
//Go through all characters in the string from the start.
for( unsigned int i = 0; i < string.size(); i++ )
{
if( string.at( i ) == ' ' )
//Add each space's width to the sum of it all.
TotalWidth += Width;
else
break;
//Check if we have gone through the whole string.
if( i == string.size()-1 )
OnlySpaces = true;
}
//Go through all characters in the string from the end, as long as it wasn't only spaces.
if( !OnlySpaces )
{
for( unsigned int i = string.size()-1; i > 0; i-- )
{
if( string.at( i ) == ' ' )
//Add each space's width to the sum of it all.
TotalWidth += Width;
else
break;
}
}
return TotalWidth;
}
Feel free to use it for whatever.
-
Just use this to get the size (with the spaces).
Text.FindCharacterPos(Text.GetString().GetSize()).x
-
Just use this to get the size
Doesn't work for multi-line strings ;)
-
Just use this to get the size (with the spaces).
Text.FindCharacterPos(Text.GetString().GetSize()).x
Wont that return the top left of the character. In other words you'd still have to get the width of the last character counted in aswell.
-
I have been using that code for a while and it works perfect.
But I am of course not using multi-line strings.
The parameter starts counting from 0, not from 1.
So when calling FindCharacterPos(1) you will get the left position of the second character.
FindCharacterPos(length) will give the left position of the character on the right of the last character (which of course doesn't exist). This is also the right position of the last character.
You can find this in the documentation as well: If index is out of range, the position of the end of the string is returned.