Here's a text wrapping program that is a part of a larger project of mine. It accepts a string of any font style or size, the x and y location of the string, and a text width limiter value to determine when to wrap text. It also calculates and draws a bounding box around the text. Then you can paste the new, soon to be better text anywhere the mouse is located by pressing enter or left click. Press spacebar or right click to reset everthing.
`Initialize
set display mode 1024, 768, 16
sync on
sync rate 0
randomize timer()
perform checklist for fonts
nf=checklist quantity()
do
`Randomize the font, number of words, the target width, and textx/texty
ink rgb(rnd(255), rnd(255), rnd(255)), rgb(0, 0, 0)
sf$=checklist string$(rnd(nf-1)+1)
set text font sf$
ts=(rnd(5)*2)+12
set text size ts
words=rnd(25)+25
rp$=""
sl=0
i=0
tw=rnd(400)+200
tx=10
ty=10
`Generate some jibberish to parse, or manually set rp$
rp$="No matter what type of game you plan to make, by providing rapid development solutions, Dark Basic Professional has the power to handle them all. It does all this and remains the easiest programming language available. The original Dark Basic was designed in 1999 to facilitate the easy programming of 3D computer games on the PC. The combination of ease and power was an instant hit, and a vibrant and creative community grew up around the language. With the emergence of new technology and techniques, it was clear the evolution of the language should race to meet these new advances. After two years of development, Dark Basic Professional is born. For those of you who have jumped right in, you will want to know where to go first. From the introduction help page, we suggest you go to the main menu and select GETTING STARTED."
remstart
for p=1 to words
letters=rnd(5)+3
rw$=""
for l=1 to letters
rl$=chr$(rnd(25)+65)
rw$=rw$+rl$
next l
rp$=rp$+rw$+" "
next p
remend
th=text height(rp$)+3
`Display a summary of this run
remstart
text 1,th*1, "RAW STRING: "+rp$
text 1,th*2, "TOTAL WIDTH: "+str$(text width(rp$))
text 1,th*3, "NUM CHARS: "+str$(len(rp$))
text 1,th*4, "TARGET WIDTH: "+str$(tw)
text 1,th*5, "RND TEXT X,Y: "+str$(tx)+", "+str$(ty)
text 1,th*6, "TEXT FONT: "+sf$
text 1,th*7, "TEXT SIZE: "+str$(ts)
remend
`Determine an appropriate number of entires for the temporary array
appr=int((text width(rp$)/tw)*2)
dim temp$(appr) as string
spacing=text height(rp$)-2
`Parse the jibberish and display results
for l=1 to appr
ts$=""
do
do
sl=sl+1
templ$=mid$(rp$, sl)
ts$=ts$+templ$
if templ$=chr$(32) or sl>=len(rp$) then exit
loop
if text width(ts$)>=tw or sl>=len(rp$) then exit
cl=sl
cs$=ts$
do
cl=cl+1
checkl$=mid$(rp$, cl)
cs$=cs$+checkl$
if checkl$=chr$(32) or cl>=len(rp$) then exit
loop
if text width(cs$)>=tw then exit
loop
if sl<=len(rp$) then i=i+1
temp$(i)=ts$
text tx, ty+((i-1)*spacing), temp$(i)
next l
`Draw bounding box and sync it all
lrpad=text width("A")
udpad=text height("A")/2
line tx-lrpad, ty-udpad, tx-lrpad, ty+(i*spacing)+udpad
line tx+tw, ty-udpad, tx+tw, ty+(i*spacing)+udpad
line tx-lrpad, ty-udpad, tx+tw, ty-udpad
line tx-lrpad, ty+(i*spacing)+udpad, tx+tw+1, ty+(i*spacing)+udpad
sync
`Grab image of text manually
px=(tx+tw-1)-(tx-lrpad+1)-lrpad
py=(ty+(i*spacing)+udpad-1)-(ty-udpad+1)-udpad
make memblock 1, px*py*4
offset=0
pa=rnd(255)
for dx=1 to px
for dy=1 to py
pc=point(dx+tx-1, dy+ty-1)
rp=rgbr(pc)
gp=rgbg(pc)
bp=rgbb(pc)
write memblock byte 1, offset, rp
write memblock byte 1, offset+1, gp
write memblock byte 1, offset+2, bp
write memblock byte 1, offset+3, pa
offset=offset+4
next dy
next dx
for rb=1 to 20
box rnd(512), rnd(300)+84, rnd(512)+512, rnd(384)+384, rgb(rnd(255), rnd(255), rnd(255)), rgb(rnd(255), rnd(255), rnd(255)), rgb(rnd(255), rnd(255), rnd(255)), rgb(rnd(255), rnd(255), rnd(255))
next rb
text 1, 1, "Alpha: "+str$(pa)
`Wait for the spacekey to be pressed before restarting
do
sync
if returnkey()=1 or mouseclick()=1
offset=0
for dx=1 to px
for dy=1 to py
rp=memblock byte(1, offset)
gp=memblock byte(1, offset+1)
bp=memblock byte(1, offset+2)
pa=memblock byte(1, offset+3)
tdx=mousex()+dx
tdy=mousey()+dy
if rp<>0 or bp<>0 or gp<>0
oc=point(tdx, tdy)
ocr=rgbr(oc)
ocg=rgbg(oc)
ocb=rgbb(oc)
rem start for alphablending
ncr=ocr+(rp*pa)/256
ncg=ocg+(gp*pa)/256
ncb=ocb+(bp*pa)/256
rem end
remstart for negative blend
ncr=255-ocr
ncg=255-ocg
ncb=255-ocb
remend
if ncr>255 then ncr=255
if ncg>255 then ncg=255
if ncb>255 then ncb=255
if ncr<0 then ncr=0
if ncg<0 then ncg=0
if ncb<0 then ncb=0
dot tdx, tdy, rgb(ncr, ncg, ncb)
endif
offset=offset+4
next dy
next dx
endif
if spacekey()=1 or mouseclick()=2 then exit
loop
`Make sure spacekey is released before continuing - to accomodate for slow fingers looping the program too fast
do
if spacekey()=0 and mouseclick()=0 then exit
loop
undim temp$(appr)
delete memblock 1
cls
loop
The code is free for anyone to use and/or learn from. Right now, most everything is randomly generated, but can easily be tailored to suit most needs. Just set rp$ to whatever and same with tx, ty, tw, etc..
Comments and suggestions are welcome too...
"Creativity is knowing how to hide your sources" - Einstein