Your Digital Media Has Never Looked So Good

 
jlfreund
Topic Author
Posts: 24
Joined: Sat Dec 15, 2012 10:53 am
Location: Cupertino, CA

Word wrapped DrawText

Sat Nov 30, 2013 2:30 am

Hi,

I am using DrawText on an roScreen, and was wondering if there is any utility function to handle word wrapping? Currently, I have to measure and layout each line. I see nice text layout and justification in other screens (like roParagraphScreen) so there must be some internal utility code for this somewhere, but I need the performance and flexibility of drawing on roScreen.
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Word wrapped DrawText

Sat Nov 30, 2013 10:15 am

There isn't any builtin function that does this, but if it helps, here's one that I wrote.

--Mark

function SplitBlockText(text as String, width as Integer, font as Object) as Object
    lines = []
    line = 0
    while true
        while text.Mid(line,1) = " "
            line = line + 1
        end while
        end_line = line
        while true
            if end_line >= text.Len() then
                if end_line > line then lines.Push(text.Mid(line, end_line-line))
                return lines
            end if
            e = EndWord(text, end_line)
            if font.GetOneLineWidth(text.Mid(line, e-line), width) >= width then
                lines.Push(text.Mid(line, end_line-line))
                line = end_line
                exit while
            end if
            end_line = e
        end while
    end while
end function

function EndWord(text as String, index as Integer) as Integer
    for p = index to text.Len()
        if text.Mid(p,1) <> " " then exit for
    end for
    for p = p to text.Len()
        if text.Mid(p,1) = " " then exit for
    end for
    return p
end function
 
User avatar
Komag
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Re: Word wrapped DrawText

Mon Aug 25, 2014 4:41 am

I could really use this ability, but I'm not quite sure how to use the functions. I know the basic idea of calling a function, passing parameters, but could you (or someone) show a full example code of these word wrapping functions in use?
 
User avatar
RokuMarkn
Roku Engineering
Posts: 1581
Joined: Mon Jun 09, 2008 9:20 am

Re: Word wrapped DrawText

Mon Aug 25, 2014 10:38 am

Something like this (untested):
' screen is an roScreen
' font is an roFont
' text is the string you want to draw
' text_color is the color of the text
' xpos,ypos is the position at which you want to draw
' width is the width you want to draw

lines = SplitBlockText(text, width, font)
font_height = font.GetOneLineHeight()
foreach line in lines
    screen.DrawText(line, xpos, ypos, font_color, font)
    ypos = ypos + font_height
end for
 
User avatar
Komag
Posts: 808
Joined: Fri Aug 22, 2014 3:42 am

Re: Word wrapped DrawText

Mon Aug 25, 2014 2:38 pm

Thank you, I'll work on that :)

Who is online

Users browsing this forum: No registered users and 5 guests