I went to the store and bought:/nMilk, Eggs, and Bread."
FUNCTION SplitBlockText(text AS STRING, width AS INTEGER, font AS OBJECT) AS OBJECT ' trig by prepText(1) ' From RokuMarkn on the dev forums
'timer = createObject("roTimespan") ' For measuring performance
splitTextLines = []
linePos = 0
WHILE TRUE
WHILE text.Mid(linePos,1) = " "
linePos = linePos + 1 ' If it ever finds a Space, move the linePos ahead 1
END WHILE
end_line = linePos
WHILE TRUE
IF end_line >= text.Len() THEN
IF end_line > linePos THEN splitTextLines.Push(text.Mid(linePos, end_line - linePos))
'? "SplitBlockText() took " timer.totalMilliseconds() ' On 2XS one example takes 18-23ms, sometimes up to 44ms, very time consuming. ALWAYS do this ahead of time, not every frame
RETURN splitTextLines
END IF
e = EndWord(text, end_line)
IF font.GetOneLineWidth(text.Mid(linePos, e - linePos), width) >= width THEN
splitTextLines.Push(text.Mid(linePos, end_line - linePos))
linePos = 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 'trig by SplitBlockText(1), from RokuMarkn on the dev forums
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
lines = text.tokenize(chr(10))
splittext = []
for each line in lines
splittext.Append(SplitBlockText(line,width,font))
end for
function SplitBlockText(text as String, width as Integer, font as Object) as Object
lines = []
line = 0
while true
' Skip spaces.
while text.Mid(line,1) = " "
line = line + 1
end while
end_line = line
' Output line starts here; step thru words.
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
newline = (text.Mid(end_line,1) = Chr(10))
if not newline then e = EndWord(text, end_line)
if newline or font.GetOneLineWidth(text.Mid(line, e-line), width) >= width then
lines.Push(text.Mid(line, end_line-line))
line = end_line
if newline then line = line + 1
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) = " " or text.Mid(p,1) = Chr(10) then exit for
end for
return p
end function
"I went to the store and bought:"+Chr(10)+"Milk, Eggs, and Bread."
FUNCTION SplitBlockText(text AS STRING, width AS INTEGER, font AS OBJECT) ' trig by prepText(1) ' From renojim on the dev forums
lines = text.tokenize(Chr(10)) ' Ascii 10 is New Line or Line Feed. Enter in strings like: "Hello."+Chr(10)+"Goodbye." ' Don't use "/n" because both / and n are delims, so all n's cause split!
splitText = []
FOR EACH line IN lines ' Original text was one big string, got broken down by delimiter Chr(10) into multiple lines
splitText.Append(SplitBlockTextWork(line, width, font)) ' Break up each line that is still too wide into an array of multiple lines, and add (Append) each new array into splitText
END FOR
RETURN splitText
END FUNCTION
FUNCTION SplitBlockTextWork(text AS STRING, width AS INTEGER, font AS OBJECT) AS OBJECT ' trig by SplitBlockText(1) ' From RokuMarkn on the dev forums
'timer = createObject("roTimespan") ' For measuring performance
splitTextLines = []
linePos = 0
WHILE TRUE
WHILE text.Mid(linePos,1) = " " ' Mid(start_index as Integer, num_chars as Integer) as String. Ex: s.Mid(0, 1) returns the first letter of s, while s.Mid(2, 2) returns the 3rd and 4th letters
linePos = linePos + 1 ' If it ever finds a Space, move the linePos ahead 1
END WHILE
end_line = linePos
WHILE TRUE
IF end_line >= text.Len() THEN
IF end_line > linePos THEN splitTextLines.Push(text.Mid(linePos, end_line - linePos))
'? "SplitBlockText() took " timer.totalMilliseconds() ' On 2XS takes 18-23ms, sometimes up to 44ms, very time consuming. ALWAYS do this ahead of time, not every frame
RETURN splitTextLines
END IF
e = EndWord(text, end_line)
IF font.GetOneLineWidth(text.Mid(linePos, e - linePos), width) >= width THEN
splitTextLines.Push(text.Mid(linePos, end_line - linePos))
linePos = 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 'trig by SplitBlockText(1), from RokuMarkn on the dev forums
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
"Komag" wrote:
Is there a more natural way of including line breaks in my strings than inserting +Chr(10)+ ?
s = "this\nthat\nthose"
s = s.replace("\n", chr(10))
s = "this\nthat\nthose"
s = CreateObject ("roRegex", "\\n", "").ReplaceAll (s, Chr (10))