Here is the text wrapping function I wrote. It is heavily cut down from my own version I used, as it processed tags and what not, and therefore might be on the heavy side code wise:
All you need is a string of text you wish to wrap, an image to write to, and a font. I have put the need of a font as I changed the font quite alot in my game. You could replace this with stringheight() if need be. Take it as it is :)
Graphics 800,600
SetBuffer BackBuffer()
; you are best creating a duplicate sized image to display the text on.
Global test_image = CreateImage(400,400)
Global test_text$ = "Hello, I want this piece of text to be wrapped across the image, and across multiple line. Here's hoping it works! Only spaces are used as word seperators."
Global font = LoadFont("Arial",12)
While Not KeyHit(1)
Cls
If KeyHit(57) Then
text_wrap(test_text,font,test_image)
End If
DrawImage test_image,10,10
Flip
Wend
End
;--------------------------------------------------------------------------;
; text_to_wrap = the entire text line you want to wrap
; font = the current font used. This must be provided.
; image = the image the text is being drawn onto. The text will wrap to the image width. Doesn't check height, so be generous!
; line_spacing = the line spacing between lines. OPTIONAL
; left_margin, top_margin and right margin. All specify distance from the edge of the image.
Function text_wrap(text_to_wrap$,font,image,line_spacing = -1, left_margin = 5, top_margin = 5, right_margin = 5)
;item_count ; number of items in the array. Sent from another function, directly based of of no_of_current_items
Local start_x = left_margin
Local start_y = top_margin
Local text_char = 1
Local text_line = 0
Local max_line_width = ImageWidth(image) - left_margin - right_margin
Local word_spacing = -1 ; -1 = use the strings current spacing, any other number, use that distance
Local word$ = "" ; use this to return a word from "read_next_word" function
Local tstring$ = ""
Local finish = 0
Local font_height = FontHeight()
SetBuffer ImageBuffer(image)
Repeat
word$ = read_next_word$(text_to_wrap$,text_char)
If word = "<NOMOREWORD>" Then
finish = 1
tstring = trim_space_from_front(tstring)
Text start_x,start_y ,tstring$ ; draw string to image
Else
;---------------------------------------------;
; If word takes line length over maximum then ;
;---------------------------------------------;
If StringWidth(tstring+word) > max_line_width Then
tstring = trim_space_from_front(tstring)
Text start_x,start_y ,tstring$ ; draw string to image
text_line = text_line + 1
If line_spacing = -1 Then
start_y = start_y + font_height
Else
start_y = start_y + line_spacing
End If
text_char = text_char + Len(word)
tstring = ""
tstring = tstring + word
Else
tstring = tstring + word
text_char = text_char + Len(word)
End If
End If
Until finish = 1
SetBuffer BackBuffer()
End Function
Function trim_space_from_front$(t_string$)
If Mid$(t_string,1,1) = " " Then
;DebugLog(" line starts with a space #"+t_string)
t_string = Mid(t_string,2,Len(t_string)-1)
;DebugLog(" line now reads #"+t_string)
End If
Return t_string
End Function
;-----------------------------------------------------------;
; This function will return the next whole word as a string ;
;-----------------------------------------------------------;
Function read_next_word$(search_text$,char)
Local start_char = char
Local finish_char = char - 1
Local finish = 0
Local return_string$ = ""
Repeat
finish_char = finish_char + 1
If Mid$(search_text,finish_char,1) = " " Then ; if the last char is a space then
finish = 1
If finish_char = start_char Then
return_string = " "
DebugLog(" read_next_word() - Returning - '"+return_string+"'. Returned because of first char space")
Else
DebugLog(" read_next_word() - Returning - '"+return_string+"'")
return_string = Mid(search_text,start_char,finish_char-start_char)
End If
End If
If finish = 0 Then
If start_char => Len(search_text) Then
If Mid(search_text,start_char,1) = "." Then
return_string = "."
Else
return_string = "<NOMOREWORD>"
End If
DebugLog(" read_next_word() - Returning - "+return_string)
Return return_string
End If
If finish_char > (Len(search_text)-1) Then
return_string = Mid(search_text,start_char,finish_char-start_char+1)
;DebugLog(" read_next_word() - Returning - '"+return_string+"'. Returned because of end of string.")
Return return_string
End If
End If
Until finish = 1
Return return_string
End Function
|