Difference between revisions of "WrapText"

From Second Life Wiki
Jump to navigation Jump to search
(New page: {{LSL Header}} __NOTOC__ <div id="box"> == Function: string WrapText(string {{LSL Param|source}},integer {{LSL Param|length}}); == <div style="padding: 0.5em;"> Splits a long t...)
 
m (Glutton for punishment, thats me. Should be faster.)
Line 1: Line 1:
{{LSL Header}} __NOTOC__
{{LSL Header}} __NOTOC__
<div id="box">
<div id="box">
== Function: [[string]] WrapText([[string]] {{LSL Param|source}},[[integer]] {{LSL Param|length}}); ==
== Function: [[string]] WrapText([[string]] {{LSL Param|source}}, [[integer]] {{LSL Param|length}}); ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
Splits a long text string into lines whose character length you get to specify. Useful, for instance, for long floating text to make it tidier. It will not split words at odd places but instead look for the space in the text nearest to the length you have specified.
Splits a long text string into lines whose character length you get to specify. Useful, for instance, for long floating text to make it tidier. It will not split words at odd places but instead look for the space in the text nearest to the length you have specified.


Example: string myWrappedText = WrapText(Title,25);
Example: <code>string myWrappedText = WrapText(Title, 25);</code>


</div></div>
</div></div>
Line 11: Line 11:
== Specification ==
== Specification ==
<div style="padding: 0.5em;">
<div style="padding: 0.5em;">
<lsl>
<lsl>string WrapText(string pcText, integer piWidth) {
string WrapText(string pcText, integer piWidth) {
     list    laLines = [];
     list    laLines = [];
     integer  liIndex;
     integer  liIndex;
     integer  liKeep;       // Specifies if we keep the char pointed at or not
     integer  liKeep; // Specifies if we keep the char pointed at or not
    integer  liLen    = llStringLength(pcText);
    list    llSearch = [" ", "\n"];
      
      
     while (llStringLength(pcText) > 0) {
     while (liLen > 0) {
         liIndex = piWidth;
         liIndex = piWidth;
         liKeep = 1;
         if (!(liKeep = (liLen <= piWidth))) {
        if (llStringLength(pcText) > piWidth) {
             while ((liIndex >= 0) && (-1 == llListFindList(llSearch, (list)llGetSubString(pcText, liIndex, liIndex))))
            liKeep = 0;
             while ((liIndex >= 0) && (llGetSubString(pcText,liIndex,liIndex) != " "))
                 --liIndex;
                 --liIndex;
             if (liIndex <= 0) {
             if (liIndex <= 0) {
Line 29: Line 28:
             }
             }
         }
         }
         laLines += [llGetSubString(pcText,0,liIndex - 1)];
         laLines += llGetSubString(pcText, 0, liIndex - 1);
         pcText = llDeleteSubString(pcText,0,liIndex - liKeep);
         pcText = llDeleteSubString(pcText, 0, liIndex - liKeep);
        liLen -= (1 + liIndex - liKeep);
     }
     }
     return llDumpList2String(laLines,"\n");
     return llDumpList2String(laLines,"\n");
}
}</lsl>
</lsl>
</div></div>
</div></div>


{{LSLC|Examples|SplitLine}}
{{LSLC|Examples|WrapText}}

Revision as of 16:40, 7 July 2008

Function: string WrapText(string source, integer length);

Splits a long text string into lines whose character length you get to specify. Useful, for instance, for long floating text to make it tidier. It will not split words at odd places but instead look for the space in the text nearest to the length you have specified.

Example: string myWrappedText = WrapText(Title, 25);

Specification

<lsl>string WrapText(string pcText, integer piWidth) {

   list     laLines  = [];
   integer  liIndex;
   integer  liKeep;  // Specifies if we keep the char pointed at or not
   integer  liLen    = llStringLength(pcText);
   list     llSearch = [" ", "\n"];
   
   while (liLen > 0) {
       liIndex = piWidth;
       if (!(liKeep = (liLen <= piWidth))) {
           while ((liIndex >= 0) && (-1 == llListFindList(llSearch, (list)llGetSubString(pcText, liIndex, liIndex))))
               --liIndex;
           if (liIndex <= 0) {
               liIndex = piWidth;
               liKeep = 1;
           }
       }
       laLines += llGetSubString(pcText, 0, liIndex - 1);
       pcText = llDeleteSubString(pcText, 0, liIndex - liKeep);
       liLen -= (1 + liIndex - liKeep);
   }
   return llDumpList2String(laLines,"\n");

}</lsl>