MiStAWaFFlEZZ

textalign

Apr 21st, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.03 KB | None | 0 0
  1. local text = require("text");
  2. local unicode = require("unicode");
  3.  
  4. local align = {};
  5.  
  6. align.direction = {};
  7.                                       -- VVHH
  8. align.direction.any           = 0x00; -- 0000
  9. align.direction.left          = 0x01; -- 0001
  10. align.direction.right         = 0x02; -- 0010
  11. align.direction.center        = 0x03; -- 0011
  12.  
  13. align.direction.top           = 0x04; -- 0100
  14. align.direction.topLeft       = 0x05; -- 0101
  15. align.direction.topRight      = 0x06; -- 0110
  16. align.direction.topCenter     = 0x07; -- 0111
  17.  
  18. align.direction.middle        = 0x08; -- 1000
  19. align.direction.middleLeft    = 0x09; -- 1001
  20. align.direction.middleRight   = 0x0A; -- 1010
  21. align.direction.middleCenter  = 0x0B; -- 1011
  22.  
  23. align.direction.bottom        = 0x0C; -- 1100
  24. align.direction.bottomLeft    = 0x0D; -- 1101
  25. align.direction.bottomRight   = 0x0E; -- 1110
  26. align.direction.bottomCenter  = 0x0F; -- 1111
  27.  
  28. align.center = function(str, width)
  29.  
  30.   if not str then return text.padLeft("", width); end
  31.  
  32.   str = text.trim(str);
  33.   local offset = width - unicode.len(str);
  34.  
  35.   if offset == 0 then
  36.     return str;
  37.   elseif offset > 0 then
  38.     return text.padRight(text.padLeft("", math.floor(offset / 2)) .. str, width);
  39.   else
  40.     return string.sub(str, 1 + math.floor(-offset / 2),
  41.       unicode.len(str) - math.ceil(-offset / 2));
  42.   end
  43.  
  44. end
  45.  
  46. align.left = function(str, width)
  47.   if not str then return text.padLeft("", width); end
  48.  
  49.   str = text.trim(str);
  50.   local offset = width - unicode.len(str);
  51.  
  52.   if offset == 0 then
  53.     return str;
  54.   elseif offset > 0 then
  55.     return text.padRight(str, width);
  56.   else
  57.     return string.sub(str, 1, width);
  58.   end
  59.  
  60. end
  61.  
  62. align.right = function(str, width)
  63.   if not str then return text.padLeft("", width); end;
  64.  
  65.   str = text.trim(str);
  66.   local offset = width - unicode.len(str);
  67.  
  68.   if offset == 0 then
  69.     return str;
  70.   elseif offset > 0 then
  71.     return text.padLeft(str, width);
  72.   else
  73.     return string.sub(str, 1 + -offset, unicode.len(str));
  74.   end
  75.  
  76. end
  77.  
  78. align.wrap = function(str, width)
  79.  
  80.   local retTable = {};
  81.   if str and width then
  82.     local fnc = text.wrappedLines(str, width, width);
  83.     local line = fnc();
  84.     while line do
  85.       table.insert(retTable, line);
  86.       line = fnc();
  87.     end
  88.   end
  89.   return retTable;
  90.  
  91. end
  92.  
  93. align.top = function(str, width, height, hAlign)
  94.   str = str or "";
  95.   width = width or 1;
  96.   local linesTable = align.wrap(str, width);
  97.   height = height or #linesTable
  98.   hAlign = bit32.band(hAlign or 1, 0x3);
  99.   local retTable = {};
  100.  
  101.   for n = 1, height do
  102.     local line = #linesTable >= n and linesTable[n] or "";
  103.     if (hAlign == align.direction.center) then
  104.       table.insert(retTable, align.center(line, width));
  105.      elseif (hAlign == align.direction.right) then
  106.       table.insert(retTable, align.right(line, width));
  107.      else
  108.       table.insert(retTable, align.left(line, width));
  109.      end
  110.   end
  111.  
  112.   return retTable;
  113. end
  114.  
  115. align.topLeft = function(str, width, height)
  116.   return align.top(str, width, height, align.direction.left);
  117. end
  118.  
  119. align.topRigth = function(str, width, height)
  120.   return align.top(str, width, height, align.direction.right);
  121. end
  122.  
  123. align.topCenter = function(str, width, height)
  124.   return align.top(str, width, height, align.direction.center);
  125. end
  126.  
  127. align.middle = function(str, width, height, hAlign)
  128.  
  129.   str = str or "";
  130.   width = width or 1;
  131.   local linesTable = align.wrap(str, width);
  132.   local nLines = #linesTable;
  133.   height = height or nLines;
  134.   hAlign = bit32.band(hAlign or 1, 0x3);
  135.  
  136.   local offset = height - nLines;
  137.  
  138.   local retTable = {}
  139.   local fnc = text.wrappedLines(str, width, width);
  140.  
  141.   for n = 1, math.min(height, nLines) do
  142.     local line = fnc();
  143.     local insertLine = false;
  144.     if line then
  145.       if offset == 0 then
  146.         insertLine = true;
  147.       elseif offset > 0 and n == 1 then
  148.         for bl = 1, math.ceil(offset / 2) do
  149.           table.insert(retTable, align.left(nil, width));
  150.         end
  151.         insertLine = true;
  152.       else
  153.         if n > math.floor(-offset / 2) then
  154.           insertLine = true;
  155.         end
  156.       end
  157.     end
  158.    
  159.     if (insertLine) then
  160.       if hAlign == align.direction.center then
  161.         table.insert(retTable, align.center(line, width));
  162.       elseif hAlign == align.direction.right then
  163.         table.insert(retTable, align.right(line, width));
  164.       else
  165.         table.insert(retTable, align.left(line, width));
  166.       end
  167.     end
  168.    
  169.   end
  170.  
  171.   if #retTable < height then
  172.     for n = 1, height - #retTable do
  173.       table.insert(retTable, align.left(nil, width));
  174.     end
  175.   end
  176.  
  177.   return retTable;
  178.  
  179. end
  180.  
  181. align.middleLeft = function(str, width, height)
  182.   return align.middle(str, width, height, align.direction.left);
  183. end
  184.  
  185. align.middleRigth = function(str, width, height)
  186.   return align.middle(str, width, height, align.direction.right);
  187. end
  188.  
  189. align.middleCenter = function(str, width, height)
  190.   return align.middle(str, width, height, align.direction.center);
  191. end
  192.  
  193. align.bottom = function(str, width, height, hAlign)
  194.   str = str or "";
  195.   width = width or 1;
  196.   local linesTable = align.wrap(str, width);
  197.   local nLines = #linesTable;
  198.   height = height or nLines;
  199.   hAlign = bit32.band(hAlign or 1, 0x3);
  200.  
  201.   local offset = height - nLines;
  202.  
  203.   local retTable = {}
  204.   local fnc = text.wrappedLines(str, width, width);
  205.  
  206.   for n = 1, height do
  207.     local line = fnc();
  208.     local insertLine = false;
  209.     if line then
  210.       if offset == 0 then
  211.         insertLine = true;
  212.       elseif offset > 0 and n == 1 then
  213.         for bl = 1, math.ceil(offset) do
  214.           table.insert(retTable, align.left(nil, width));
  215.         end
  216.         insertLine = true;
  217.       else
  218.         if n > math.floor(-offset) then
  219.           insertLine = true;
  220.         end
  221.       end
  222.     end
  223.    
  224.     if (insertLine) then
  225.       if hAlign == align.direction.center then
  226.         table.insert(retTable, align.center(line, width));
  227.       elseif hAlign == align.direction.right then
  228.         table.insert(retTable, align.right(line, width));
  229.       else
  230.         table.insert(retTable, align.left(line, width));
  231.       end
  232.     end
  233.    
  234.   end
  235.  
  236.   return retTable;
  237. end
  238.  
  239. align.bottomLeft = function(str, width, height)
  240.   return align.bottom(str, width, height, align.direction.left);
  241. end
  242.  
  243. align.bottomRigth = function(str, width, height)
  244.   return align.bottom(str, width, height, align.direction.right);
  245. end
  246.  
  247. align.bottomCenter = function(str, width, height)
  248.   return align.bottom(str, width, height, align.direction.center);
  249. end
  250.  
  251. align.align = function(str, width, height, direction)
  252.   local vAlign = bit32.band(direction or align.direction.top, 0xC);
  253.   local hAlign = bit32.band(direction or align.direction.left, 0x3);
  254.  
  255.   str = str or "";
  256.    
  257.   if vAlign == align.direction.middle then
  258.     return align.middle(str, width, height, hAlign);
  259.   elseif vAlign == align.direction.bottom then
  260.     return align.bottom(str, width, height, hAlign);
  261.   else
  262.     return align.top(str, width, height, hAlign);
  263.   end
  264.  
  265. end
  266.  
  267. return align;
Add Comment
Please, Sign In to add comment