Hey all, it's been a while since I've played with Dark Basic and my skills are a tad rusty.
At the moment I am trying to code a function that will create an image from some supplied text (and a bitmap font). If the text has control codes written in, such as \n, the function bumps the folowing text down a line.
The purpose of the function is to be able to produce a scrollable textbox for any given supplied text.
My function is clever enough to introduce new lines when the desired textbox width is reached, but this happens in the middle of words, which I want to avoid. I.e., at the moment I may have:
This is a te
st section o
f text!
being displayed.
I'd rather end up with:
This is a
test section
of text!
An ideally even that would be 'fully justified' (i.e. spaced out to the margins)
Has anyone got any ideas how I could do this?
Cheers!
My function code at the moment is:
function L5RFE_TextBoxSetup(TextboxText$,TextboxMainImageNum,TextboxWidth)
create bitmap 1,1280,1024
set current bitmap 1
EndStr=0: TextCounter=1: CurrentLine=0: MaxLineWidth=0: LineX=0 `Sets the variables to their initial values
if mid$(TextboxText$,TextCounter)<>"\"
LineX=LineX-FontCharacterLeft(asc(mid$(TextboxText$,TextCounter)))+2
endif
while EndStr=0 `Until the end of the string
if mid$(TextboxText$,TextCounter)="\" `if the control character is found, deal with it:
if mid$(TextboxText$,TextCounter+1)="z" then EndStr=1 `\z signifies the end of the text
if mid$(TextboxText$,TextCounter+1)="n" `\n signifies a forced new line
CurrentLine=CurrentLine+1 ` so drop down a line
LineX=0 ` and reset to the beginning of the line
TextCounter=TextCounter+1
if TextCounter<=len(TextboxText$) then LineX=LineX-FontCharacterLeft(asc(mid$(TextboxText$,TextCounter)))+2
endif
if mid$(TextboxText$,TextCounter+1)="t" `\t signifies a tab
LineX=LineX+8*int(FONTWIDTH/3.0) ` so add 8 characters
if LineX>TextboxWidth
CurrentLine=CurrentLine+1 ` so drop down a line
LineX=0 ` and reset to the beginning of the line
if TextCounter<=len(TextboxText$) then LineX=LineX-FontCharacterLeft(asc(mid$(TextboxText$,TextCounter)))+2
endif
TextCounter=TextCounter+1
endif
else
Letter=asc(mid$(TextboxText$,TextCounter))
paste image FONTIMAGEOFFSET+Letter,LineX,CurrentLine*FONTHEIGHT,1 `display the next character
LineX=LineX+FontCharacterWidth(Letter)+2
if LineX>TextboxWidth
CurrentLine=CurrentLine+1 ` so drop down a line
LineX=0 ` and reset to the beginning of the line
if TextCounter<=len(TextboxText$) then LineX=LineX-FontCharacterLeft(asc(mid$(TextboxText$,TextCounter)))+2
endif
if TextCounter=len(TextboxText$) then EndStr=1 `double check that the end of the text hasn't been hit (e.g. if \z is missing)
endif
TextCounter=TextCounter+1 `Increase the counter
if MaxLineWidth<LineX then MaxLineWidth=LineX `Note the widest line
endwhile
CurrentLine=CurrentLine+1
sync
get image TextboxMainImageNum, 0,0,MaxLineWidth+15,CurrentLine*FONTHEIGHT
set current bitmap 0
delete bitmap 1
endfunction
"You get what everyone gets, you get a lifetime!" - Death, The Sandman Library
First you Dream, then you ... - Neil Gaiman, 2001