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

Author Topic: Facilitating Rich Text Formatting  (Read 648 times)

0 Members and 1 Guest are viewing this topic.

Kain Nobel

  • Newbie
  • *
  • Posts: 38
    • View Profile
Facilitating Rich Text Formatting
« on: March 02, 2023, 08:21:00 am »
Hello and good day!

This is just a general question, not a specific "tech support question", but it does relate to the SFML/Graphics library. I'm needing to do a specific task and I'm trying to brainstorm the best possible solution. The solutions I'm coming up with in my head sound OK-ish, but maybe you'd have a better one?

I'm curious if there is a way to facilitate rich text formatting with the sf::Font and the sf::Text objects, as-is, or if I'd have to come up with a solution from scratch.

Standard Font / Text Operation
Code: [Select]
//Load the font
sf::Font font;
font.loadFromFile("Impact.ttf");

// Apply text settings
sf::Text text;
text.setFont(font);
text.setString("Hello, world!");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
text.setStyle(sf::Text::Bold | sf::Text::Italic);

The way I'm understanding this is the sf::Text is actually the object that determines such things as Bold and Italic flags. As far as I see, doing something like...

Hello World!

...is perfectly feasible. But what if I want to do...

Hello World!

Is this type of operation supported without having to use multiple sf::Text (or multiple sf::Font, etc) objects?

The way I'm wanting it to work VS the way the library actually works seems a little different...

How I'd Like It To Work (Example)
Code: [Select]
Iterating Letter-By-Letter Operations...
    Font = Font(...properties...)
    Toggle Bold (On)
    Set Color(0, 96, 0)
    Write "H"
    Toggle Bold (Off)
    Set Color(48, 128, 48)
    Write "ell"
    Set Color(0, 96, 0)
    Write "o"

   //...Blah blah blah...
End Iteration

The raw sf::String itself would probably look like...

Code: [Select]
"\b\c[1]H\c[2]\bell\c[1]o \bW\b\c[2]orld\b\c[1]!\c[0]\b"
That's a dumb example, but I'd need to iterate through each letter and look for codes within the string that would change the format of the rendered text product. It would format based on escape codes such as \b (toggle bold), \i (toggle italic), \c[0] (change font color), etc. As the iterator rolls forward, each letter would be drawn and specified changes of formatting would be applied.

What I'm Needing This For...

The reason I'm needing this is so I can write a Rich Text Message System. As each character is written onto the texture, letter by letter, the properties would always be subject to change during runtime. At any moment, a Bold could be switched on, an occasional Underline may be applied, font name can change, etc.

The only workaround I can think of is using individual font.getGlyph calls and rendering them to an sf::Texture. I'm thinking the sf::Text object would also have to come into play for bolds, italics, etc. Each individual letter would probably have to be rendered on a buffer, and then transferred onto the main "text display" texture.

Hopefully I'm overthinking this and somebody is experienced enough to suggest a better solution than the one I have in my head. Not that I think my purposed solution is bad, but I feel there may be a more direct way to facilitate the behavior I'm looking for.

Thank you ahead of time to anybody who reads this, looking forward to hearing back.
« Last Edit: March 02, 2023, 08:51:57 am by Kain Nobel »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Facilitating Rich Text Formatting
« Reply #1 on: March 02, 2023, 09:44:57 am »
You may want to use https://github.com/skyrpex/RichText

But if you want to actually support that specific format, you of course would still need to write the parser yourself. At least you wouldn't have to create a custom text rendering solution for it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Kain Nobel

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Facilitating Rich Text Formatting
« Reply #2 on: March 02, 2023, 10:55:21 am »
That's too cool, thank you!

I'll have to check this out and see how it works, looks like it would facilitate the functionality I need :D