Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Colored Text
- -- By: Valgo
- -- Use this function to perform your initial setup
- function setup()
- -- Put your text in the renderText()
- img = renderText("#red #extra_large ok #newl robtop is a BANANA")
- pasteboard.copy(img)
- end
- -- This function gets called once every frame
- function draw()
- background(127, 127, 127, 255)
- sprite(img, WIDTH / 2, HEIGHT / 2)
- end
- function renderText(t)
- local ren = TextToImage(t)
- ren:parse()
- return ren:render()
- end
- TextToImage = class()
- function TextToImage:init(t)
- currentSize = 24
- currentColor = color(26, 26, 26, 255)
- newLine = false
- self.t = t
- self.data = {}
- self.tokens = {
- ["red"] = "currentColor = color(204, 66, 52, 255)",
- ["orange"] = "currentColor = color(238, 139, 32, 255)",
- ["yellow"] = "currentColor = color(237, 210, 32, 255)",
- ["green"] = "currentColor = color(29, 193, 78, 255)",
- ["blue"] = "currentColor = color(47, 108, 195, 255)",
- ["purple"] = "currentColor = color(194, 46, 195, 255)",
- ["black"] = "currentColor = color(26, 26, 26, 255)",
- ["extra_large"] = "currentSize = 36",
- ["large"] = "currentSize = 30",
- ["medium"] = "currentSize = 24",
- ["small"] = "currentSize = 18",
- ["extra_small"] = "currentSize = 12",
- ["newl"] = "newLine = true"
- }
- end
- function TextToImage:parse()
- currentSize = 24
- currentColor = color(26, 26, 26, 255)
- newLine = false
- local i = 1
- local isParsingToken = false
- local token = ""
- local t = ""
- while i <= #self.t do
- local c = string.sub(self.t, i, i)
- if c == "#" then
- if #t > 0 then
- table.insert(self.data, {currentSize, currentColor, newLine, t})
- end
- token = ""
- t = ""
- newLine = false
- isParsingToken = true
- elseif c == " " and isParsingToken then
- isParsingToken = false
- if self.tokens[token] ~= nil then
- loadstring(self.tokens[token])()
- end
- else
- if isParsingToken then
- token = token .. c
- else
- if c == " " then
- table.insert(self.data, {currentSize, currentColor, newLine, t})
- t = ""
- newLine = false
- else
- t = t .. c
- end
- end
- end
- i = i + 1
- end
- if #t > 0 then
- table.insert(self.data, {currentSize, currentColor, newLine, t})
- end
- end
- function TextToImage:render()
- pushStyle()
- local img = image(512, 1024)
- local y = 1024 - self.data[1][1] - 8
- local x = 0
- textAlign(LEFT)
- local prevSize = self.data[1][1]
- setContext(img)
- textMode(CORNER)
- font("Avenir")
- for k, v in pairs(self.data) do
- fill(v[2])
- fontSize(v[1])
- local size = textSize(v[4])
- if size + x >= 512 or prevSize ~= v[1] or v[3] then
- x = 0
- y = y - v[1]
- end
- text(v[4], x, y)
- x = x + size + 6
- prevSize = v[1]
- end
- setContext()
- popStyle()
- img = img:copy(0, y, 512, 1024 - y)
- return img
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement