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

Author Topic: Splitting up text  (Read 3772 times)

0 Members and 1 Guest are viewing this topic.

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Splitting up text
« on: July 29, 2008, 09:19:47 pm »
Hi, this isen't a specific sfml question, but here it comes.

Let's say I want to display a bunch of text, but the text is not allowed to be wider than say 400px, which means I need to insert line breaks somehow. How would you solve this? I know how it could be done but I figure it would be really slow.

remi.k2620

  • Full Member
  • ***
  • Posts: 186
    • View Profile
    • http://remi.tuxfamily.org
Splitting up text
« Reply #1 on: July 29, 2008, 09:44:02 pm »
I think you'll have to use the method GetRect to know the width of the string, or wait a bit : in the roadmap there is "Add functions in sf::Font / sf::String to get the size of single characters"

In python you could do :
Code: [Select]
from PySFML import sf

text = 'Seconds before the Earth is demolished to make way for a galactic freeway, Arthur Dent is plucked off the planet by his friend Ford Prefect, a researcher for the revised edition of The Hitchhiker\'s Guide to the Galaxy who, for the last fifteen years, has been posing as an out-of-work actor.'

res = ''
newline = ''
s = sf.String()
for word in text.split(' '):
s.SetText(newline+word)
if s.GetRect().GetWidth() > 400:
res = res + '\n' + newline
newline = word
else:
newline = newline + ' ' + word

res = res[2:] + '\n' + newline

s.SetText(res)
# draw s...

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Splitting up text
« Reply #2 on: July 29, 2008, 10:00:35 pm »
Thanks, that's the way I was thinking about but I was afraid it would be slow, but maybe it's not.

remi.k2620

  • Full Member
  • ***
  • Posts: 186
    • View Profile
    • http://remi.tuxfamily.org
Splitting up text
« Reply #3 on: July 29, 2008, 10:11:11 pm »
It took 16 secondes to do it 1000 times, I don't know if it gives you an idea :/

dabo

  • Sr. Member
  • ****
  • Posts: 260
    • View Profile
    • http://www.dabostudios.net
Splitting up text
« Reply #4 on: July 29, 2008, 11:54:14 pm »
Ok good, I came up with an algorithm that is fast enough, I guess I underestimated sfml :)