Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Word wrap function -- is there already one?

Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
12-18-2007 16:10
You've got a string such as:

"All instances of each separator will be used to parse the string"

64 characters

using Xyzzy text (10 char variation), two prims per line, i can display on a sign board i'm making up to 20 chars per line.

So with 64 characters in above for instance (string just an example, will change daily), I need 4 lines (64 / 20 = 3, plus 1 line for the leftover 4 characters.

But I don't want to just messily break a word right in the middle like th
is.

I've already started on a function, experimenting with

llParseString2List(wordstringstart, [" "], []);

to break out words based on spaces after having broke stuff down into sets of 20 characters...

started because i didn't want to be lazy....


but nor do I want to be dumb and re-invent the wheel, if anyone knows of a piece of script already out there that does this.

(I know there's not a lot of text manipulation functions as SL is not what you would normally define as a document-rich environment, hehe.)
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
12-18-2007 17:50
Try this function:

CODE

list WrapText(string pcText, integer piWidth) {
list laLines = [];
integer liIndex;
integer liKeep; // Specifies if we keep the char pointed at or not

while (llStringLength(pcText) > 0) {
liIndex = piWidth;
liKeep = 1;
if (llStringLength(pcText) > piWidth) {
liKeep = 0;
while ((liIndex >= 0) && (llGetSubString(pcText,liIndex,liIndex) != " "))
--liIndex;
if (liIndex <= 0) {
liIndex = piWidth;
liKeep = 1;
}
}
laLines += [llGetSubString(pcText,0,liIndex - 1)];
pcText = llDeleteSubString(pcText,0,liIndex - liKeep);
}
return laLines;
}


Probably can be optimized a bit, but it works.
Chaz Longstaff
Registered User
Join date: 11 Oct 2006
Posts: 685
12-18-2007 21:01
>> Probably can be optimized a bit, but it works

Optimized more?? hehe. Before I left to go out for dinner, I was already at like 497 lines :}

Well, not quite, but that script is quite optimized compared to the hoops I was leaping through! grin.

Many thanks, works like a charm!
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
12-18-2007 23:52
using string handling to add a word at a time up to your limit, then inserting a new line might work better.

or pararsing the string to a list, loop and count until you hit your limit, insert a newline, reset your count, and then continue.... until you dump the list to a string

all depends on how much text is coming in
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -