Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Text removes spaces from GetGlobalBounds, SFML 2.0  (Read 6152 times)

0 Members and 1 Guest are viewing this topic.

DeadLeaves

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« 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.
Code: [Select]

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

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #1 on: March 01, 2012, 05:42:22 am »
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

DeadLeaves

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #2 on: March 01, 2012, 07:44:49 am »
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.

Contadotempo

  • Full Member
  • ***
  • Posts: 167
  • Firelink Shrine
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #3 on: March 01, 2012, 02:56:56 pm »
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).

DeadLeaves

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #4 on: March 01, 2012, 11:54:44 pm »
Fixed it with this function.

Code: [Select]
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.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #5 on: March 02, 2012, 12:09:22 pm »
Just use this to get the size (with the spaces).
Code: [Select]
Text.FindCharacterPos(Text.GetString().GetSize()).x
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #6 on: March 02, 2012, 12:10:52 pm »
Quote
Just use this to get the size

Doesn't work for multi-line strings ;)
Laurent Gomila - SFML developer

DeadLeaves

  • Newbie
  • *
  • Posts: 8
    • View Profile
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #7 on: March 02, 2012, 09:30:50 pm »
Quote from: "texus"
Just use this to get the size (with the spaces).
Code: [Select]
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.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
sf::Text removes spaces from GetGlobalBounds, SFML 2.0
« Reply #8 on: March 03, 2012, 10:31:18 am »
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:
Quote
If index is out of range, the position of the end of the string is returned.
TGUI: C++ SFML GUI