Advertisement
Mijyuoon

Word wrap shit

Aug 30th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.29 KB | None | 0 0
  1. local worditer
  2.  
  3. local CK_NONE = 0
  4. local CK_SPACE = 1
  5. local CK_TAB = 3
  6. local CK_LINE = 2
  7.  
  8. do ---- Word iterator ------------------
  9.     local whitespace = {
  10.         [0x0009] = CK_TAB,
  11.         [0x000A] = CK_LINE,
  12.         [0x000B] = CK_SPACE,
  13.         [0x000C] = CK_SPACE,
  14.         [0x000D] = CK_LINE,
  15.         [0x0020] = CK_SPACE,
  16.     }
  17.    
  18.     function worditer(str)
  19.         local iterator = utf8.codes(str)
  20.         local ltPos, ltChar = iterator()
  21.         local ltKind = whitespace[ltChar]
  22.         local rtPos, rtChar, rtKind
  23.         local wordPos, nxPos = 1, 1
  24.         return function()
  25.             while true do
  26.                 if not wordPos then break end
  27.                 local word, isWs = nil
  28.                 rtPos, rtChar = iterator()
  29.                 rtKind = whitespace[rtChar]
  30.                 if not rtPos or rtKind ~= ltKind then
  31.                     nxPos = (rtPos or 0) - 1
  32.                     word = str:sub(wordPos, nxPos)
  33.                     isWs = ltKind or CK_NONE
  34.                     wordPos = rtPos
  35.                 end
  36.                 ltPos, ltChar, ltKind = rtPos, rtChar, rtKind
  37.                 if word then return word, isWs end
  38.             end
  39.             return nil
  40.         end
  41.     end
  42. end ------------------------------------
  43.  
  44. function mt_wraptxt:_UpdateWrap()
  45.     local objW = self._size[1]
  46.     if objW < 1 then return end
  47.     local lines, cW, cBuf = {}, 0, ""
  48.     for word, wType in worditer(self._text) do
  49.         if wType == CK_NONE then
  50.             local w0 = scr.TextSize(word, self._font)
  51.             if cW + w0 <= objW then
  52.                 cBuf = cBuf .. word
  53.                 cW = cW + w0
  54.             elseif w0 > objW then
  55.                 for j, ci in utf8.codes(word) do
  56.                     local ch = utf8.char(ci)
  57.                     local ncBuf = cBuf .. ch
  58.                     local w1 = scr.TextSize(ncBuf, self._font)
  59.                     if w1 > objW then
  60.                         lines[#lines+1] = cBuf
  61.                         local csz = scr.TextSize(ch, self._font)
  62.                         cW, cBuf = csz, ch
  63.                     else
  64.                         cW, cBuf = w1, ncBuf   
  65.                     end
  66.                 end
  67.             else
  68.                 lines[#lines+1] = cBuf
  69.                 cW, cBuf = w0, word
  70.             end
  71.         elseif wType == CK_TAB
  72.         or wType == CK_SPACE then
  73.             if wType == CK_TAB then
  74.                 word = string.rep(" ", #word * self._tabwidth)
  75.             end
  76.             local w0 = scr.TextSize(word, self._font)
  77.             if cW + w0 <= objW then
  78.                 cBuf = cBuf .. word
  79.             end
  80.             cW = cW + w0
  81.         elseif wType == CK_LINE then
  82.             lines[#lines+1] = cBuf
  83.             cW, cBuf = 0, ""
  84.             for i = 1, #word-1 do
  85.                 lines[#lines+1] = false
  86.             end
  87.         end
  88.     end
  89.     if #cBuf > 0 then
  90.         lines[#lines+1] = cBuf
  91.     end
  92.    
  93.     local _, hgt = scr.TextSize(" ", self._font)
  94.     self._lines, self._size[2] = lines, #lines * hgt
  95. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement