SFML community forums

Help => Graphics => Topic started by: ggggggggggggggg on September 18, 2015, 10:20:10 am

Title: Text.GetGlobalBounds() is lying to me!
Post by: ggggggggggggggg on September 18, 2015, 10:20:10 am
Hello. Getting the width and height isn't acting like I thought it would.

            font = new Font("arial.ttf");

            text = new Text("Text!", font, 90);
            text.Position = new Vector2f(300, 300);

            RectangleShape shape = new RectangleShape();
            shape.Size = new Vector2f(text.GetGlobalBounds().Width, text.GetGlobalBounds().Height);
            shape.Position = text.Position;
            shape.FillColor = Color.Transparent;
            shape.OutlineColor = Color.White;
            shape.OutlineThickness = 1;

What it produces: http://puu.sh/kflYc/d0f958ab90.png

There is probably something obvious that I'm missing.

Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: FRex on September 18, 2015, 10:33:37 am
The text bounds left and top are usually non zero so you must use them too.
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: ggggggggggggggg on September 18, 2015, 10:59:43 am
How do you get how much they're offset by 0? Also, why are is text left and top bounds non zero?
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Laurent on September 18, 2015, 11:12:30 am
Quote
How do you get how much they're offset by 0?
GetGlobalBounds().Left and GetGlobalBounds().Top...

Quote
Also, why are is text left and top bounds non zero?
Because text is aligned on its baseline, not on its top-left corner. So there's potential empty space on top of it, which matches the height of the tallest character of the font (accentuated upper case letters, for exemple).
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: ggggggggggggggg on September 18, 2015, 11:25:39 am
Sorry, what I meant to ask was, "How do I calculate the spacing between the absolute position/size to the intended position/size?" Which you indirectly answered with your second explanation: I have to find the tallest character and subtract it from the absolute position. Right? If so, how? I'm not sure how to get the size of a letter with the given char size and char type.
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Laurent on September 18, 2015, 11:38:09 am
Quote
How do I calculate the spacing between the absolute position/size to the intended position/size?
GetGlobalBounds().Top. Really. It's not a joke.
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: ggggggggggggggg on September 18, 2015, 11:42:44 am
Sigh. I meant the difference between the absolute position and the top of the tallest letter. But I figured it out:

int highest = 0;

            for (int i = 0; i < text.DisplayedString.Length; i++)
            {
                Glyph glyph = font.GetGlyph((uint)text.DisplayedString[i], 150, false);

                if (glyph.Bounds.Height > highest)
                    highest = (int)glyph.Bounds.Height;
            }

Thanks for the help. It's kind of lame you have to do this but I guess everything can't be done for us ;)
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Laurent on September 18, 2015, 11:58:31 am
Why do you keep thinking that GetGlobalBounds().Top is not what you want? If it doesn't work as expected then I'd like to get your feedback.
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Raptor2277 on October 06, 2015, 12:14:05 am
It's kinda weird how it works. It is implemented weirdly. If you do it normally.
t.CharacterSize = 100;
t.Position = new Vector2f(200, 200);
...
...
Draw.drawRectangle(w, gBounds, Color.Yellow);
w.Draw(new Vertex[] { new Vertex(new Vector2f(0, 200), Color.White), new Vertex(new Vector2f(900, 200), Color.White) }, PrimitiveType.Lines);
w.Draw(t);
 

And here is what it looks like. As you can see there is some white space at the top. And the getGlobalBounds() returns not 200, 200 but 201, 224. That's because of the empty space at the top. If you notice the getLocanBounds() x and y are 1, 24. So you can use that to position it properly. IDK why the it doesn't do it right the first time.
(http://i.imgur.com/hGKaKo3.png)

Here is what I use to position it properly.
t.CharacterSize = 100;
setPosition(t, new Vector2f(200, 200));
...
...
public void setPosition(Text text, Vector2f pos)
{
     Vector2f offsets = new Vector2f(text.GetLocalBounds().Left, text.GetLocalBounds().Top);

     text.Position = new Vector2f(pos.X - offsets.X, pos.Y - offsets.Y);
}
 

And here is what it looks like.
(http://i.imgur.com/NNoYNr2.png)
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Arcade on October 06, 2015, 12:51:24 am
Quote
IDK why the it doesn't do it right the first time.

I believe this was already answered by the previous posts in this thread, right?
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: AlexxanderX on October 06, 2015, 12:08:57 pm
Another method would be:
text.setOrigin(text.getLocalBounds().left, text.getLocalBounds().top);
Title: Re: Text.GetGlobalBounds() is lying to me!
Post by: Hapax on October 12, 2015, 06:35:22 pm
Another method would be:
text.setOrigin(text.getLocalBounds().left, text.getLocalBounds().top);
This must be updated whenever the string is altered.

I'm not sure what the results of getLocalBounds().left and getLocalBounds().top are when the string is empty.