Something which I'm needing for a current project of mine, and I thought I'd share, in case anyone else might ever have use for it.
text$ = "This is a bunch of foobar! "
text$ = text$ + text$
Calculate_WordWrap text$, 300, foo()
LINE (301, 0)-(640, 480), &HFFFF0000, BF
'and now to try with a different font
PRINT "NOTE: This test is with a non-monospaced font, so if your PC is like mine, expect your fans to kick in, CPU usage to skyrocket, and for this process to take a few seconds."
Calculate_WordWrap text$, 300, foo()
LINE (301, 0)-(640, 480), &HFFFF0000, BF
'and now to try with a different font
PRINT "NOTE: This test is with the same font" PRINT " set as MONOSPACE, just to test" PRINT " the speed difference."
Calculate_WordWrap text$, 300, foo()
LINE (301, 0)-(640, 480), &HFFFF0000, BF
SUB Calculate_WordWrap
(temp$
, wide%
, result
() AS STRING) BreakPoint = ",./- ;:!" 'I consider all these to be valid breakpoints. If you want something else, change them.
'first find the natural length of the line
lineend = i - 1
t$
= RTRIM$(LEFT$(text$
, lineend
)) 'at most, our line can't be any longer than what fits the screen. count = count + 1
clean_exit:
Usage is rather simple:
1) REDIM and array AS STRING
REDIM foo(0) AS STRING
2) Call the routine with the text you want wrapped, the size you want the wrapping to fit in, and then the array to store the wrapped text in.
Calculate_WordWrap text$, 300, foo()
That's all there is to it! ;)
Enjoy, test it out, and let me know if you manage to break it with something odd, and I'll correct whatever the glitch is ASAP.
EDIT: Make certain your desired font is set before calling, as it works with whatever you currently are using. This does nothing to allow on the fly font changes. I kept it more or less as simple as possible.