Formatted Print function

Previous topic - Next topic

fuzzy70

This was only meant to just center text but ended up growing into something bigger  :D. Apologies if another one has been posted but I couldn't see one.

The code is commented above the function but will explain it here anyway. Basically it allows you to print text but adds the following over the standard PRINT command.

  • Text can be justified left, centered or right
  • You can specify a width in pixels if you want to limit text to an area, see next point
  • Text will be wrapped if its wider than the screen or the specified width

Call the function fprint(text$,x%,y%,justify%,width%)

text$ = Text to be printed (obviously) :D
x%,y% = Coordinates of where to print text
justify% = Justify the text, 0=Same as normal print(left), 1=Centered, 2=Right
width%=limit text area width, for example if you wanted to limit the text to 200pixels

The text will automatically wrap if

  • The text goes offscreen, only in the x direction
  • The text is longer than the amount of pixels set in width

The Splitter function is what does the wrapping if the text is to wide to fit, It will search for spaces in the string & split the text at that point so as not to cut off words in the middle. It was part of the original print function but to save a lot of code duplication as well as easy maintenance(<- read as easier debugging  :) ) I split it out into a function of its own.

A word of warning, NO checks are made to see if the text goes off the bottom of the screen.

One strange thing, I built this as an external file to be used with other programs of mine but if i try do DIM the type array I  get an error "(10) error : command not inside function or sub". Only way I found to get it to work was to use a REDIM in the splitter function. If there is another or better way please let me know.

Also let me know if you have any problems with this function or ways to improve it. I have tested it with random text, diff fonts etc & not had any troubles but you might send it something which I have not tried  =D

Lee

Code (glbasic) Select
// Start of a textlibrary

TYPE textlines
txt$
y%
ENDTYPE

GLOBAL wraptext[] AS textlines


// FormatPrint
// ===========
// text$=Text to print
// x%,y%=Position to print text
// justify%=0 left, 1 centre, 2 right
// width%=limit width(in pixels), if 0 then will default to screenwidth
// if you send an x%=0,width%=0 & justify%=1 text will be screen centered

FUNCTION fprint: text$, x%, y%, justify%, width%
LOCAL screenw%,screenh%,fontw%,fonth%
LOCAL textlength%
LOCAL tempx%,wrap%=FALSE

GETSCREENSIZE screenw%,screenh%
GETFONTSIZE fontw%,fonth%

//  Uncomment below if you want to send character widths to function rather than pixels
// width%=width%*fontw%

textlength%=LEN(text$,1)

IF width%=0 THEN width%=screenw%

IF justify%=0 OR justify%=1 AND x%+width%>screenw% THEN width%=screenw%-x
IF justify%=2 AND x%-width%<0 THEN width%=x

IF textlength%>width% THEN wrap%=TRUE
IF width%<fontw% THEN RETURN

SELECT wrap%
CASE 0 // No Wrap
IF justify=1
PRINT text$,x%+((width%-textlength%)/2),y% // Centered
ELSEIF justify=2
PRINT text$,x%-textlength%,y% // Right
ELSE
PRINT text$,x%,y% // Normal(left)
ENDIF
CASE 1 // Wrap needed
IF justify=1 // Centered
splittext(text$,width%)
FOREACH line IN wraptext[]
tempx%=(width%-LEN(line.txt$,1))/2
PRINT line.txt$,x%+tempx%,y%   
INC y%,line.y%
NEXT
ELSEIF justify=2 // Right
splittext(text$,width%)
FOREACH line IN wraptext[]
tempx%=LEN(line.txt$,1)
PRINT line.txt$,x%-tempx%,y%
INC y%,line.y%
NEXT
ELSE // Normal(left)
splittext(text$,width%)
FOREACH line IN wraptext[]
PRINT line.txt$,x%,y%
INC y%,line.y%
NEXT
ENDIF
ENDSELECT

ENDFUNCTION

FUNCTION splittext: text$,linewidth%
LOCAL textlength%,space%,fontw%,fonth%,split$,chrsperline%,loop%,temptext$,splitpoint%
LOCAL done%=FALSE,currentline%=0

GETFONTSIZE fontw%,fonth%
chrsperline%=linewidth%/fontw%

REPEAT
IF LEN(text$)<=chrsperline% THEN done%=TRUE
REDIM wraptext[currentline%+1]
textlength%=LEN(text$)
temptext$=LEFT$(text$,chrsperline%)
splitpoint%=chrsperline%
space%=INSTR(text$," ") //look for spaces in string, for neater splitting

IF space%<>-1 AND done%=FALSE
FOR loop%=LEN(temptext$) TO 1 STEP -1
split$=MID$(temptext$,loop%-1,1)
IF split$=" "
splitpoint%=loop%
BREAK
ENDIF
NEXT
ENDIF

wraptext[currentline%].txt$=LEFT$(temptext$,splitpoint%)
wraptext[currentline%].y%=fonth%
text$=TRIM$(RIGHT$(text$,textlength%-splitpoint%)," ")
INC currentline%,1

UNTIL done%

ENDFUNCTION


FUNCTION printerror: error$
LOCAL txt$

txt$="Program halted, Press anykey to quit"

fprint(error$,0,0,1,0)
fprint(txt$,0,30,1,0)
SHOWSCREEN
KEYWAIT
END

ENDFUNCTION



"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

Moru

Nice work :-)

Actually there is a number of versions before you though:
http://www.glbasic.com/forum/index.php?topic=1340.msg8768#msg8768

I did some changes to it for colored and polystripes on my homepage, see link in my signature.

Edit: It's almost four years ago now, how? what happened?

fuzzy70

Thanks Moru, I thought there might of been similar functions posted but I tend to get hit & miss results from using the forum search sometimes  :D.

Will check out your site asap, I'm still learning GLB so am sucking in all the info I can.

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

pinete

Hi!
thanks for the snippet!
text wrapping doesn't work for me, sorry :( I've tried vith several widths but it seems to ignore it completely ...

fuzzy70

Thats odd?.

I have not really looked at the code much after reading Moru's post above, that function is way better than mine lol

QuoteActually there is a number of versions before you though:
http://www.glbasic.com/forum/index.php?topic=1340.msg8768#msg8768

Lee
"Why don't you just make ten louder and make ten be the top number and make that a little louder?"
- "These go to eleven."

This Is Spinal Tap (1984)

okee

Moru you should post a topic on that fontroutines9 library in 2D Snippets
just so people know about it as it's very useful
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)