Hi,
this is a function for word wrap of a line or complete paragraphs in a text file, or in a window with a fixed-width font. It can even indent bullet lists correctly.
See also the corresponding function in the 3rd post.
I hope the code contains enough comments. If you wonder why the function deals with "soft wraps" and "hard wraps", you can read the background in this discussion.
Have fun!
Include file:
Code: Select all
; v1.01, tested with PB 5.10 on Windows XP x86 (should be cross-platform, though)
Procedure.s LineWrap (line$, softWrapPosn.i, hardWrapPosn.i=-1, delimList$=" "+Chr(9), nl$=#LF$, indent$="")
; -- Word wrap in *one line* of a text file, or in a window with a fixed-width font
; in: line$ : line which is to be wrapped
; indent$: "" or a string consisting of blanks, used for indenting lines of list items
;
; For the meaning of the other parameters see function WordWrap().
Protected.i posn, found, i, firstChar=Len(indent$)+1
Protected ret$=""
If softWrapPosn < firstChar
ProcedureReturn line$
EndIf
posn = Len(line$)
While posn > softWrapPosn
; search for rightmost delimiter <= softWrapPosn:
For i = softWrapPosn To firstChar Step -1
found = FindString(delimList$, Mid(line$,i,1))
If found
posn = i
Break
EndIf
Next
If found = 0 ; if there is no delimiter <= softWrapPosn
If hardWrapPosn < 0
; insert hard wrap at position of soft wrap:
posn = softWrapPosn
Else
; search for leftmost delimiter > softWrapPosn:
For i = softWrapPosn+1 To posn
found = FindString(delimList$, Mid(line$,i,1))
If found
posn = i
Break
EndIf
Next
If hardWrapPosn > 0 And hardWrapPosn < posn
; insert hard wrap at given position:
posn = hardWrapPosn
EndIf
EndIf
EndIf
ret$ + RTrim(Left(line$, posn)) + nl$
line$ = LTrim(Mid(line$, posn+1))
If line$ <> ""
line$ = indent$ + line$
EndIf
posn = Len(line$)
Wend
ProcedureReturn ret$ + line$
EndProcedure
Procedure.s WordWrap (text$, softWrapPosn.i, hardWrapPosn.i=-1, delimList$=" "+Chr(9), nl$=#LF$, liStart$="")
; ## Main function ##
; -- Word wrap in *one or more lines* of a text file, or in a window with a fixed-width font
; in : text$ : text which is to be wrapped;
; may contain #CRLF$ (Windows), or #LF$ (Linux and modern Mac systems) as line breaks
; softWrapPosn: the desired maximum length (number of characters) of each resulting line
; if a delimiter was found (not counting the length of the inserted nl$);
; if no delimiter was found at a position <= softWrapPosn, a line might
; still be longer if hardWrapPosn = 0 or > softWrapPosn
; hardWrapPosn: guaranteed maximum length (number of characters) of each resulting line
; (not counting the length of the inserted nl$);
; if hardWrapPosn <> 0, each line will be wrapped at the latest at
; hardWrapPosn, even if it doesn't contain a delimiter;
; default setting: hardWrapPosn = softWrapPosn
; delimList$ : list of characters which are used as delimiters;
; any delimiter in line$ denotes a position where a soft wrap is allowed
; nl$ : string to be used as line break (normally #CRLF$ or #LF$)
; liStart$ : string at the beginning of each list item
; (providing this information makes proper indentation possible)
;
; out: return value: text$ with given nl$ inserted at appropriate positions
;
; <http://www.purebasic.fr/english/viewtopic.php?f=12&t=53800>
Protected.i numLines, i, indentLen=-1
Protected line$, indent$, ret$=""
numLines = CountString(text$, #LF$) + 1
For i = 1 To numLines
line$ = RTrim(StringField(text$, i, #LF$), #CR$)
If FindString(line$, liStart$) = 1
If indentLen = -1
indentLen = Len(liStart$)
EndIf
indent$ = Space(indentLen)
Else
indent$ = ""
EndIf
ret$ + LineWrap(line$, softWrapPosn, hardWrapPosn, delimList$, nl$, indent$)
If i < numLines
ret$ + nl$
EndIf
Next
ProcedureReturn ret$
EndProcedure
Code: Select all
EnableExplicit
XIncludeFile "WordWrap.pbi"
Define line$, text$
line$ = "Wrap me baby but don't hurt me, I'll be good like a long stringy should and would! And here is a URL: http://www.purebasic.com/download.php"
MessageRequester("Word wrap demo 1/7", WordWrap(line$, 8, 0)) ; no hard wrap
MessageRequester("Word wrap demo 2/7", WordWrap(line$, 8, 998)) ; "security hard wrap" (will normally not emerge)
MessageRequester("Word wrap demo 3/7", WordWrap(line$, 8)) ; default: hard wrap position = soft wrap position
MessageRequester("Word wrap demo 4/7", WordWrap(line$, 1)) ; extreme wrapping :-)
text$ = "PureBasic is a native 32 bit and 64 bit programming language based on established BASIC rules. The key features of PureBasic are portability (Windows, Linux, MacOS X and AmigaOS are currently supported), the production of very fast and highly optimized executables and, of course, the very simple BASIC syntax. PureBasic has been created for the beginner and expert alike. We have put a lot of effort into its realization to produce a fast, reliable system friendly language." + #LF$
text$ + #LF$
text$ + "In spite of its beginner-friendly syntax, the possibilities are endless with PureBasic's advanced features such as pointers, structures, procedures, dynamically linked lists and much more. Experienced coders will have no problem gaining access to any of the legal OS structures or API objects and PureBasic even allows inline ASM." + #LF$
text$ + #LF$
text$ + "The main features of PureBasic" + #LF$
text$ + #LF$
text$ + " - Huge set of internal commands (1100+) to quickly and easily build any application or game" + #LF$
text$ + " - All BASIC keywords are supported" + #LF$
text$ + " - Very fast compiler which creates highly optimized executables" + #LF$
text$ + " - No external DLLs, runtime interpreter or anything else required when creating executables" + #LF$
text$ + " - Procedure support for structured programming with local and global variables" + #LF$
text$ + " - Full unicode support" + #LF$
text$ + " - Access to full OS API for advanced programmers" + #LF$
text$ + " - Easy but very fast 2D game support through dedicated libraries (DirectX, SDL, ...)" + #LF$
text$ + " - Easy and high quality 3D support based on OGRE" + #LF$
text$ + " - Optimal use of the available hardware by using highly optimized (assembly) commands" + #LF$
text$ + " - Source code is portable between AmigaOS, Windows, MacOS X and Linux" + #LF$
text$ + " - Dedicated editor and development environment" + #LF$
text$ + " - Powerful integrated debugger and profiler to easily trace and analyze code" + #LF$
text$ + #LF$
text$ + " - And here is a long URL: http://www.prettylongdomainname.com/level1/level2/level3/level4/level5/index.php"
MessageRequester("Word wrap demo 5/7", WordWrap(text$, 50))
MessageRequester("Word wrap demo 6/7", WordWrap(text$, 50, -1, " "+Chr(9), #LF$, " - "))
MessageRequester("Word wrap demo 7/7", WordWrap(text$, 50, 998, " "+Chr(9), #LF$, " - "))