Word Wrapping Text at 80 characters

OpenScript ››

problem

I have a chunk of text such as:


There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humor, or randomized words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text.


which word-wraps naturally based on the width of the display area. But I want to force wrap it to 80 characters, by adding carriage returns at the end of each line, to produce this:


There are many variations of passages of Lorem Ipsum available, but the

majority have suffered alteration in some form, by injected humor, or

randomized words which don't look even slightly believable. If you are going

to use a passage of Lorem Ipsum, you need to be sure there isn't anything

embarrassing hidden in the middle of text.


Possible Solution

ToolBook does not have a built in feature to do this, but using the OpenScript language it is possible to program this behavior.


This process of adding CRLF (carriage returns) characters to the end of each line can be mimicked using this OpenScript code below. It is written as a function call, so all you need to do is pass to the function the string of text you want to "parse", and also at which character to break each line.


This script will properly break the line at the nearest space, and will not break a word in half.

Sample:

text of field "abc" = parseIT(text of field "xyz",65)

The two parameters are

Put this following script in your Book Script
(don't modify this logic, it works as-is):


to get parseIT allText, maxChars

  newText = null

  x = charcount(allText)

  if x = 0

     return null

  end

  ptr = 0

  while allText is not null

     increment ptr

     if ptr = charcount(allText)

        maxChars = ptr

     end

     chr = char ptr of allText

     if chr = space

        remember = ptr - 1

     end

     if chr = CR

        put chars 1 to ptr of AllText & LF after newText

        clear chars 1 to ptr+1 of allText

        ptr = 0

     end

     if ptr = maxChars

        nextchr = char ptr + 1 of allText

        if nextchr is in SPACE & CR or nextchr = null

           put chars 1 to ptr of allText & CRLF after newText

           clear chars 1 to ptr + 1 of allText

        else

           put chars 1 to remember of allText & CRLF after newText

           clear chars 1 to remember +1 of allText

        end

        ptr = 0

     end

  end

  return newText

end



KEYWORDS: P2760 wordwrapping word-wrap




Created with the Personal Edition of HelpNDoc: Produce electronic books easily