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

Author Topic: [SOLVED]Set a defined size of textfield  (Read 2622 times)

0 Members and 1 Guest are viewing this topic.

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
[SOLVED]Set a defined size of textfield
« on: September 08, 2013, 11:25:21 pm »
Hello dear community. I have a problem which i tried to solve. I want define an area where sf::Text should be drawn with a constant size of the font, methods i've tried out:
count each character with a size of the font, when limit is reached insert newline "\n" isn't working because each character has diffirent size.
Draw each character with the font and then read the property width, very perfomance heavy and bad solvage, because each character in the line must be compared and add to the width at the line lenght, and this method isnt ready yet.
Well, maybe somebody knows a better method to do so.

sorry for bad english also  ;)
« Last Edit: October 18, 2013, 11:59:20 pm by canlot »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Set a defined size of textfield
« Reply #1 on: September 08, 2013, 11:54:21 pm »
You could use a monospaced font, which will have the same char width for all chars.

Otherwise you could create your own class which keeps track of the width so you only have to check when a new character gets added. Ultimately you should be able to check every character width on its own (see the documentation).

If you have performance issues post the minimal code, so we might point out some issues.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Set a defined size of textfield
« Reply #2 on: September 09, 2013, 12:10:39 am »
Look at the sf::Glyph class, together with sf::Text and sf::Font it contains everything you need.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

canlot

  • Newbie
  • *
  • Posts: 15
    • View Profile
Re: Set a defined size of textfield
« Reply #3 on: October 13, 2013, 05:53:48 pm »
If someone has the same problem, here is the solution: (maybe not a perfekt one  ;) ):

int give_letter_size(char &letter, sf::Font &font, int font_size)
{
    return font.getGlyph(int(letter), font_size, false).advance;
}

std::string &ready_text(std::string &text, int width, int font_size, sf::Font &font)
{
    int s_width = 0;
    int last_word = 0;
    for(int i = 0; i <= text.size(); i++)
    {
        if(s_width < width)
        {
            s_width += give_letter_size(text[i], font, font_size);
        }
        else
        {
            text.replace(last_word, 1, "\n");
            i = last_word;
            s_width = 0;
        }
        if(text[i] == ' ')
            last_word = i;
    }
    return text;
}
 

 

anything