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

Author Topic: best implementation of word wrapping in a container?  (Read 4441 times)

0 Members and 3 Guests are viewing this topic.

dubesinhower

  • Newbie
  • *
  • Posts: 6
    • View Profile
best implementation of word wrapping in a container?
« on: April 29, 2013, 05:54:20 pm »
i've looked now for hours to find a simple implementation of text wrapping to a new line if the text is too long to fill a specific width (namely the width of the window).  i found this thread (http://en.sfml-dev.org/forums/index.php?topic=1939.0), but the project has been abandoned, and the source files are unobtainable.

does anyone have any thoughts on creating a system for either automatically parsing strings to include newline characters, or separate the original strings into new, smaller strings? it seems very silly to have to apply a string to a text element just to find it's width, only to deconstruct the string again.

any suggestions would be helpful!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: best implementation of word wrapping in a container?
« Reply #1 on: April 29, 2013, 10:35:57 pm »
A font consists of glyphs, which are the visual representations of the characters. The classes sf::Font and sf::Glyph allow you to compute a string's width without first creating a sf::Text instance.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

dubesinhower

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: best implementation of word wrapping in a container?
« Reply #2 on: April 30, 2013, 01:01:27 am »
ah okay.

so what's the best way to go about this? iterate through the string (should i use the standard library string or sf::string?), keep a running tally of the total size, and if it goes over the width that i want, iterate back to the last space and insert a new line?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: best implementation of word wrapping in a container?
« Reply #3 on: April 30, 2013, 09:16:05 am »
You can also store the index of the last whitespace (not only space), instead of iterating back.

By the way, you could look how SFGUI and TGUI implemented wraparound text, they probably both have some kind of input field.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

dubesinhower

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: best implementation of word wrapping in a container?
« Reply #4 on: April 30, 2013, 09:17:27 am »
so i messed around with a function to tally the sizes of the glyphs that create my string to find how many glyphs fit in a line, and i noticed a few things.

for one, there is no glyph width for spaces. this throws off the calculation of the line width. is there any quick way to calculate this?

also, does the glyph not include whitespace between characters? this may affect the line calculation as well.

lastly, what number does the iterator start at to access the string contents via string.operator[]? i started at 0, and it seems to not count the first letter.

if anyone has any answers i would appreciate it!

edit: thank you nexus, i will check their implementation in the morning.
« Last Edit: April 30, 2013, 09:19:16 am by dubesinhower »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: best implementation of word wrapping in a container?
« Reply #5 on: April 30, 2013, 09:43:02 am »
Quote
for one, there is no glyph width for spaces. this throws off the calculation of the line width. is there any quick way to calculate this?
Yes, space has no width in some fonts. But you must use the advance member of the glyphs, not the width. That will solve almost all your problems, but not all: for other whitespace glyphs (vertical and horizontal tab, new line) you must handle them manually. Have a look at the source code of sf::Text if you need some inspiration.

Quote
also, does the glyph not include whitespace between characters? this may affect the line calculation as well.
Again, use the advance, not the width ;)
Note that you should also use the kerning to get the same result as in sf::Text.

Quote
lastly, what number does the iterator start at to access the string contents via string.operator[]? i started at 0, and it seems to not count the first letter.
It should start at 0.

I know that playing with fonts and glyphs directly can be tedious, and I'd like to write a simple wrapper to ease this kind of calculations; like a glyph iterator that would automatically calculate the glyphs positions in the final text. But, you know, there are so many other things to do... ;)
Laurent Gomila - SFML developer

dubesinhower

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: best implementation of word wrapping in a container?
« Reply #6 on: April 30, 2013, 10:21:40 pm »
VICTORY! haha i figured it out!

once i complete my function, i can post a code snippet of how i implemented it.

thanks again nexus and laurent!

special thanks to laurent for developing this kick ass library!