RosettaCodeData/Task/Word-wrap/Lingo/word-wrap-1.lingo
2016-12-05 23:44:36 +01:00

42 lines
1.1 KiB
Text

-- in some movie script
----------------------------------------
-- Wraps specified text into lines of specified width (in px), returns lines as list of strings
-- @param {string} str
-- @param {integer} pixelWidth
-- @param {propList} [style]
-- @return {list}
----------------------------------------
on hardWrapText (str, pixelWidth, style)
if voidP(style) then style = [:]
lines = []
-- create a new field member
m = new(#field)
m.text = str
m.rect = rect(0,0,pixelWidth,0)
-- assign style props (if not specified, defaults are used)
repeat with i = 1 to style.count
m.setProp(style.getPropAt(i), style[i])
end repeat
-- create an invisible temporary sprite
s = channel(1).makeScriptedSprite(m)
s.loc = point(0,0)
s.visible = false
_movie.updateStage()
-- get the wrapped lines
charPos = 0
repeat with y = 0 to s.height-1
n = s.pointToChar(point(pixelWidth-1, y))
if n<>charPos then
lines.add(str.char[charPos+1..n])
charPos = n
end if
end repeat
channel(1).removeScriptedSprite()
return lines
end