line text justify
BlitzPlus Forums/BlitzPlus Programming/line text justify
| ||
i've an bitmap font (8x12 px), i want justify the displayed line text. How to determine the blank width between the words ? i've already a function 'StringWidth' which determine the width (pixels) of any string displayed with my bitmap font (with or without spaces). For example the line text is displayed into a window. The window width size is a little longer than the length of the displayed string text ! Many Thanks ! |
| ||
What you need to know are: 1. The width of the screen 2. The width of your text 3. The number of spaces in your line of text Subtract the length of the text from the width of the screen. The difference then needs to be divided by the number of spaces you have. The last figure is then the additional amount you need to add whenever you find a space during the drawing of your text. I'd calculate the x co-ordinates as floats, so any rounding won't mess up the positioning of your text. Hope that helps. |
| ||
Ok, as I thought this would be a nice little function to write when I had a spare few minutes, I put one together. The function I have written will accept an entire paragraph as one long text string and will display on screen as it needs to. The code is below, but you can also download the source and bitmap font that I used from here. ; now for the function ; p$ = paragraph text ; y = y position on screen ; x = x position on screen (optional - defaults to 0) ; width = column width (optionl - defaults to screen width, as defined in constant) Function ShowPara(p$,y,x#=0,width=scr_width) ; check that BOTH optional parameters were used and amend width as necessary If x>0 And width=scr_width Then width=scr_width-x p=Trim(p) ; remove any spaces char_width=width/8 ; work out number of chars (assume 8 pixel wide font) text_line$=Left(p,char_width) ; grab initial number of characters from string Repeat z=char_width ; z is used as the length of the current text line If Instr(text_line," ") Then ; check that there are spaces While Mid(text_line,z,1)<>" " ; and back track until one is found z=z-1 ; length of text line is 1 less than where the space was found Wend EndIf text_line=Left(text_line,z-1) ; remove split word at end of line text_line=Trim(text_line) ; allow for double spaces by using Trim z=Len(text_line) ; adjust text length to account for any trimming p=Mid(p,z+2) ; remove this text from the paragraph text spaces=0 ; counter for number of spaces in line For i=1 To Len(text_line) ; step through entire string If Mid(text_line,i,1)=" " Then spaces=spaces+1 ; increase count of space found Next s#=8.0+(Float(width-z*8)/spaces) ; calculate width required for each space ShowText(x,y,text_line,s) ; show text on screen text_line=Left(p,char_width) ; fetch next row from paragraph y=y+8 ; lower position of text on screen Until Len(text_line)<char_width ; keep going until next line is shorter than required width If Len(text_line)>0 Then ShowText(x,y,Trim(p)):y=y+8 ; print last line if necessary Return y ; return y for additional position outside of function End Function ; simple show text routine Function ShowText(x#,y,t$,s#=8) For i=1 To Len(t) ; step through each character in turn char=Asc(Mid(t,i,1))-32 ; calculate frame to display ; if character is space (frame 0), then just increase x, otherwise print character If char=0 Then x=x+s Else DrawImage font,x,y,char:x=x+8 Next End Function It may not be the best written way of doing things, but it works and is a starting point if you wish to adapt it. |
| ||
Many thanks JazzieB for this piece of code ! You should put this into the code archive ! |