'===========================================================================
' Subject: Text Alignment Routine             Date: 07-09-01 (  :  )       
'  Author: Richard Kelly                      Code: PB, QB, firstbasic     
'  Origin: newson@37.com                    Packet: ALGOR.ABC
'===========================================================================
'Author:  Richard Kelly              Public Domain - 07/09/01
'Name: Text alignment routine        Category: Algorithms
'Compatible with QB v1.1 - v7.1, FirstBASIC, and PB (I think).
'
'This will take a string of text (A$), fits as many words as possible in the
'specified number of columns (40 or 80 usually, unless you use an irregular
'font size like I do), then goes to the next line.  The number of columns
'is specified in the variable WH.  Line 24 is a subroutine to display the
'text, which can be easily modified to use with a font instead.
GOTO Sample
'First line: Change all [^]s to ["]s.
11 A = INSTR(A$, "^"): IF A > 0 THEN A$ = LEFT$(A$, A - 1) + CHR$(34) + MID$(A$, A + 1, 254): GOTO 11
'Now search for the next (or first) occurence of a "space" in the string.
12 L = INSTR(A$, " "): B$ = LEFT$(A$, L - 1): GOTO 21
'C$ will eventually become a "word" (or a portion of a word separated by a "-"
'   symbol), then is added to D$.  D$ is eventually added to B$, or will equal
'   B$ if the B$ string would go "off the WH margin" if D$ is added.
13 C$ = MID$(A$, L, 99): A3 = 0: A0 = INSTR(C$, " "): A1 = INSTR(C$, "-"): IF A0 = 0 AND A1 = 0 THEN A2 = LEN(C$): A3 = 1: GOTO 16
14 IF A0 > 0 AND A1 > 0 AND A1 < A0 THEN A2 = A1: GOTO 16
15 A2 = A0 - 1
16 C$ = LEFT$(C$, A2): D$ = D$ + C$: LL = LEN(B$ + D$): IF A3 = 0 THEN GOTO 19
17 IF LL > WH THEN GOSUB 24: B$ = C$: GOSUB 24: RETURN
18 B$ = B$ + D$: GOSUB 24: RETURN
19 L = L + A2: IF LL > WH THEN GOSUB 24: B$ = C$: GOTO 21
20 B$ = B$ + D$
21 D$ = ""
'Scan for spaces again.
22 IF MID$(A$, L, 1) = " " THEN L = L + 1: D$ = D$ + " ": GOTO 22
'Space found, generate the next word.
23 GOTO 13
'The message is printed out in the following line, but it displays B$, not A$.
'    This subroutine is here so it can be easier for users to use their fonts.
24 IF LEN(B$) < WH THEN PRINT B$: RETURN ELSE PRINT B$; : RETURN

Sample:
WH = 80
A$ = "This is a simple message to demonstrate the word alignment routine.  The program will properly align any text placed in the A$ string, so the programmer doesn't have to align the text himself."
GOSUB 11: PRINT
A$ = "Without the text being properly aligned, the first part of some words might be chopped off, with the rest of the word being put at the beginning of the next line."
GOSUB 11: PRINT
A$ = "In the past, the author had to align the text himself to keep the ^chopping off^ from happening.  Now, this program can do the aligning for him."
GOSUB 11: PRINT
A$ = "One more thing:  The program changes all ^Up arrow symbols^ (drawn with a [Shift]+[6]) into quotes, so it's easier to enclose something in quotes in your text messages."
GOSUB 11: PRINT
