Advertisement
Guest User

Untitled

a guest
Dec 19th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. -- Colored Text
  2. -- By: Valgo
  3.  
  4. -- Use this function to perform your initial setup
  5. function setup()
  6.     -- Put your text in the renderText()
  7.     img = renderText("#red #extra_large ok #newl robtop is a BANANA")
  8.     pasteboard.copy(img)
  9. end
  10.  
  11. -- This function gets called once every frame
  12. function draw()
  13.     background(127, 127, 127, 255)
  14.     sprite(img, WIDTH / 2, HEIGHT / 2)
  15. end
  16.  
  17. function renderText(t)
  18.     local ren = TextToImage(t)
  19.     ren:parse()
  20.     return ren:render()
  21. end
  22.  
  23. TextToImage = class()
  24.  
  25. function TextToImage:init(t)
  26.     currentSize = 24
  27.     currentColor = color(26, 26, 26, 255)
  28.     newLine = false
  29.     self.t = t
  30.     self.data = {}
  31.     self.tokens = {
  32.     ["red"] = "currentColor = color(204, 66, 52, 255)",
  33.     ["orange"] = "currentColor = color(238, 139, 32, 255)",
  34.     ["yellow"] = "currentColor = color(237, 210, 32, 255)",
  35.     ["green"] = "currentColor = color(29, 193, 78, 255)",
  36.     ["blue"] = "currentColor = color(47, 108, 195, 255)",
  37.     ["purple"] = "currentColor = color(194, 46, 195, 255)",
  38.     ["black"] = "currentColor = color(26, 26, 26, 255)",
  39.     ["extra_large"] = "currentSize = 36",
  40.     ["large"] = "currentSize = 30",
  41.     ["medium"] = "currentSize = 24",
  42.     ["small"] = "currentSize = 18",
  43.     ["extra_small"] = "currentSize = 12",
  44.     ["newl"] = "newLine = true"
  45.     }
  46. end
  47.  
  48. function TextToImage:parse()
  49.     currentSize = 24
  50.     currentColor = color(26, 26, 26, 255)
  51.     newLine = false
  52.     local i = 1
  53.     local isParsingToken = false
  54.     local token = ""
  55.     local t = ""
  56.     while i <= #self.t do
  57.         local c = string.sub(self.t, i, i)
  58.         if c == "#" then
  59.             if #t > 0 then
  60.                 table.insert(self.data, {currentSize, currentColor, newLine, t})
  61.             end
  62.             token = ""
  63.             t = ""
  64.             newLine = false
  65.             isParsingToken = true
  66.         elseif c == " " and isParsingToken then
  67.             isParsingToken = false
  68.             if self.tokens[token] ~= nil then
  69.                 loadstring(self.tokens[token])()
  70.             end
  71.         else
  72.             if isParsingToken then
  73.                 token = token .. c
  74.             else
  75.                 if c == " " then
  76.                     table.insert(self.data, {currentSize, currentColor, newLine, t})
  77.                     t = ""
  78.                     newLine = false
  79.                 else
  80.                     t = t .. c
  81.                 end
  82.             end
  83.         end
  84.         i = i + 1
  85.     end
  86.     if #t > 0 then
  87.         table.insert(self.data, {currentSize, currentColor, newLine, t})
  88.     end
  89. end
  90.  
  91. function TextToImage:render()
  92.     pushStyle()
  93.     local img = image(512, 1024)
  94.     local y = 1024 - self.data[1][1] - 8
  95.     local x = 0
  96.     textAlign(LEFT)
  97.     local prevSize = self.data[1][1]
  98.     setContext(img)
  99.     textMode(CORNER)
  100.     font("Avenir")
  101.     for k, v in pairs(self.data) do
  102.         fill(v[2])
  103.         fontSize(v[1])
  104.         local size = textSize(v[4])
  105.         if size + x >= 512 or prevSize ~= v[1] or v[3] then
  106.             x = 0
  107.             y = y - v[1]
  108.         end
  109.         text(v[4], x, y)
  110.         x = x + size + 6
  111.         prevSize = v[1]
  112.     end
  113.     setContext()
  114.     popStyle()
  115.     img = img:copy(0, y, 512, 1024 - y)
  116.     return img
  117. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement