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

Author Topic: [SOLVED] Strange sf::Text top margin.  (Read 2706 times)

0 Members and 2 Guests are viewing this topic.

tariel36

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SOLVED] Strange sf::Text top margin.
« on: October 08, 2016, 08:43:06 pm »
Hello everyone!

I've got problem with displaying sf::Text correctly. Below is the code I use:

// Load font in OnInit() and create text.
sf::Font * font = new sf::Font();

if (!font->loadFromFile(path)) // loads courier new
{
        // some code
}

sf::Text text = sf::Text("A+", *font, 18);
text.setColor(sf::Color::Red);
text.setOrigin(0.f, 0.f);

// ...

// Draw text in Draw()
_window.draw(text);

// ...

Provided code is just an example that contains most important parts (I store texture pointer correctly etc, so no worries).

The result is strange, because the text has strange top margin (it's not drawn exactly at top left corner, or is it?). Is it font's feature or I do something wrong? I've tried "Arial" and there was similar behaviour.

See the attachment for visual view.
« Last Edit: October 14, 2016, 06:39:50 pm by tariel36 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Strange sf::Text top margin.
« Reply #1 on: October 08, 2016, 09:24:34 pm »
What does the documentation and tutorial say?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange sf::Text top margin.
« Reply #2 on: October 08, 2016, 10:17:29 pm »
The text is not aligned on top, it is aligned on its baseline (like everywhere else, for obvious reasons). So unless your text contains the highest character of the font (most likely accentuated capitals), it will always be slightly below the top.

The offset should be text.getLocalBounds().top.
Laurent Gomila - SFML developer

tariel36

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: AW: Strange sf::Text top margin.
« Reply #3 on: October 09, 2016, 10:03:27 am »
What does the documentation and tutorial say?

I'm really sorry but I couldn't find anything about baseline alignment mentioned by Laurent except for single glyph in "Making your own text class" part.

Is it supposed to be there?
http://www.sfml-dev.org/tutorials/2.4/graphics-text.php
http://www.sfml-dev.org/documentation/2.4.0/classsf_1_1Text.php

Thank you for your replies.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Strange sf::Text top margin.
« Reply #4 on: October 09, 2016, 10:44:44 am »
Quote
I'm really sorry but I couldn't find anything about baseline alignment mentioned by Laurent
Indeed, the documentation and tutorials say nothing about it. Maybe we should add a little something.
Laurent Gomila - SFML developer

 

anything