Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local worditer
- local CK_NONE = 0
- local CK_SPACE = 1
- local CK_TAB = 3
- local CK_LINE = 2
- do ---- Word iterator ------------------
- local whitespace = {
- [0x0009] = CK_TAB,
- [0x000A] = CK_LINE,
- [0x000B] = CK_SPACE,
- [0x000C] = CK_SPACE,
- [0x000D] = CK_LINE,
- [0x0020] = CK_SPACE,
- }
- function worditer(str)
- local iterator = utf8.codes(str)
- local ltPos, ltChar = iterator()
- local ltKind = whitespace[ltChar]
- local rtPos, rtChar, rtKind
- local wordPos, nxPos = 1, 1
- return function()
- while true do
- if not wordPos then break end
- local word, isWs = nil
- rtPos, rtChar = iterator()
- rtKind = whitespace[rtChar]
- if not rtPos or rtKind ~= ltKind then
- nxPos = (rtPos or 0) - 1
- word = str:sub(wordPos, nxPos)
- isWs = ltKind or CK_NONE
- wordPos = rtPos
- end
- ltPos, ltChar, ltKind = rtPos, rtChar, rtKind
- if word then return word, isWs end
- end
- return nil
- end
- end
- end ------------------------------------
- function mt_wraptxt:_UpdateWrap()
- local objW = self._size[1]
- if objW < 1 then return end
- local lines, cW, cBuf = {}, 0, ""
- for word, wType in worditer(self._text) do
- if wType == CK_NONE then
- local w0 = scr.TextSize(word, self._font)
- if cW + w0 <= objW then
- cBuf = cBuf .. word
- cW = cW + w0
- elseif w0 > objW then
- for j, ci in utf8.codes(word) do
- local ch = utf8.char(ci)
- local ncBuf = cBuf .. ch
- local w1 = scr.TextSize(ncBuf, self._font)
- if w1 > objW then
- lines[#lines+1] = cBuf
- local csz = scr.TextSize(ch, self._font)
- cW, cBuf = csz, ch
- else
- cW, cBuf = w1, ncBuf
- end
- end
- else
- lines[#lines+1] = cBuf
- cW, cBuf = w0, word
- end
- elseif wType == CK_TAB
- or wType == CK_SPACE then
- if wType == CK_TAB then
- word = string.rep(" ", #word * self._tabwidth)
- end
- local w0 = scr.TextSize(word, self._font)
- if cW + w0 <= objW then
- cBuf = cBuf .. word
- end
- cW = cW + w0
- elseif wType == CK_LINE then
- lines[#lines+1] = cBuf
- cW, cBuf = 0, ""
- for i = 1, #word-1 do
- lines[#lines+1] = false
- end
- end
- end
- if #cBuf > 0 then
- lines[#lines+1] = cBuf
- end
- local _, hgt = scr.TextSize(" ", self._font)
- self._lines, self._size[2] = lines, #lines * hgt
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement