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

Author Topic: SFML Text Origin Y Question  (Read 1799 times)

0 Members and 1 Guest are viewing this topic.

kpulv

  • Newbie
  • *
  • Posts: 18
    • View Profile
SFML Text Origin Y Question
« on: September 23, 2013, 02:05:56 am »
Hello!  I was wondering if this was the intended behavior of the text class when adjusting the Y origin for purposes of scaling and rotating:



Usually for images adjusting the Y origin to half the height of the image will guarantee the perfect center of the image, but for text this doesn't seem to be the case.  In the image above, I'm rendering a red pixel at the X and Y coordinate of the text to show how centered it is.

The first text at the top uses GetLocalBounds().Height * 0.5f.  It doesn't look vertically centered though when scaling.  The second text uses the same thing except 0.75f instead of 0.5f, and ends up looking more vertically centered than the 0.5f one. The third uses 0 just as a reference.

Here's all the code I used to make this example (SFML 2.1 in C#):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SFML.Graphics;
using SFML.Window;

namespace SFMLTextTest {
    class Program {
        static void Main(string[] args) {
            RenderWindow window = new RenderWindow(new VideoMode(1000, 750), "Test Application");

            Font font = new Font("arial.ttf");
            string testString = "Testing Font yGpQ`";

            Text text = new Text(testString, font);
            text.Origin = new Vector2f(text.GetLocalBounds().Width / 2f, text.GetLocalBounds().Height / 2f);
            text.Position = new Vector2f(window.Size.X / 2f, window.Size.Y * 0.25f);

            text.Color = Color.Yellow;

            CircleShape textCircle = new CircleShape(1f);
            textCircle.Position = text.Position;
            textCircle.FillColor = Color.Red;

            Text text2 = new Text(testString, font);
            text2.Origin = new Vector2f(text2.GetLocalBounds().Width / 2f, text2.GetLocalBounds().Height * .75f);
            text2.Position = new Vector2f(window.Size.X / 2f, window.Size.Y * 0.5f);

            text2.Color = Color.Cyan;

            CircleShape text2Circle = new CircleShape(1f);
            text2Circle.Position = text2.Position;
            text2Circle.FillColor = Color.Red;

            Text text3 = new Text(testString, font);
            text3.Origin = new Vector2f(text2.GetLocalBounds().Width / 2f, 0);
            text3.Position = new Vector2f(window.Size.X / 2f, window.Size.Y * 0.75f);

            text3.Color = Color.Green;

            CircleShape text3Circle = new CircleShape(1f);
            text3Circle.Position = text3.Position;
            text3Circle.FillColor = Color.Red;

            window.Closed += new EventHandler(OnClosed);

            float timePassed = 0;

            float scaleText = 1f;

            while (window.IsOpen()) {
                window.DispatchEvents();

                window.Clear();

                scaleText = (float)(((Math.Sin(timePassed * 0.025f) + 1f) / 2f) * 2.5f);

                text.Scale = text2.Scale = text3.Scale = new Vector2f(scaleText, scaleText);
                timePassed += 0.1f;

                window.Draw(text);
                window.Draw(text2);
                window.Draw(text3);

                window.Draw(textCircle);
                window.Draw(text2Circle);
                window.Draw(text3Circle);

                window.Display();

            }
        }

        static void OnClosed(object sender, EventArgs e) {
            Window window = (Window)sender;
            window.Close();
        }
    }
}
 

Here's another screenshot using characters that display better the bounds of possible characters:



Even here, using Origin Y as half the height seems misaligned.
« Last Edit: September 23, 2013, 03:05:27 am by kpulv »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: SFML Text Origin Y Question
« Reply #1 on: September 23, 2013, 07:45:12 am »
sf::Text's bounds have non-zero left and top coordinates, you must take them in account in your calculations.
Laurent Gomila - SFML developer