info = { type = "app", title = "Text", author = "BRRRO", license = "MIT", desc = "Your own line of text on the panel", category = "text", api = 42, } permissions = { "images" } local W, H, FPS = gfx.W, gfx.H, gfx.FPS local clear, text, text_center, clip, rect = gfx.clear, gfx.text, gfx.text_center, gfx.clip, gfx.rect local text_width, text_ink, text_bounds = gfx.text_width, gfx.text_ink, gfx.text_bounds local ascii, upper, gmatch, sub = string.ascii, string.upper, string.gmatch, string.sub local floor, min, max = math.floor, math.min, math.max local hash32, umod = math.hash32, math.umod local ulen, uoff = utf8.len, utf8.offset txt = { msg = "Text", msg_h = "63 bytes - a letter with an accent takes two", font = "Font", font_h = "the small ones have no accents", auto = "largest that fits", upper = "Uppercase", upper_h = "accents stay", nodia = "Without accents", nodia_h = "the small fonts have none anyway", ink = "Text colour", bg = "Background", bg_h = "over an image it outlines the text", glint = "Highlight", run = "Long text", run_h = "when it does not fit on one line", run_o = { "on lines", "runs around", "bounces" }, speed = "Running speed", speed_h = "only when the text runs" } for k, v in pairs(({ sk = { msg = "Text", msg_h = "63 bajtov - písmeno s diakritikou zaberie dva", font = "Písmo", font_h = "malé nemajú diakritiku", auto = "najväčšie, čo sa zmestí", upper = "Veľké písmená", upper_h = "diakritika ostane", nodia = "Bez diakritiky", nodia_h = "malé písmo ju nemá aj tak", ink = "Farba textu", bg = "Pozadie", bg_h = "nad obrázkom obrys textu", glint = "Odlesk", run = "Dlhý text", run_h = "keď sa nezmestí do riadku", run_o = { "na riadky", "beží dokola", "odráža sa" }, speed = "Rýchlosť behu", speed_h = "len keď text beží" }, cs = { msg = "Text", msg_h = "63 bajtů - písmeno s diakritikou zabere dva", font = "Písmo", font_h = "malá nemají diakritiku", auto = "největší, co se vejde", upper = "Velká písmena", upper_h = "diakritika zůstane", nodia = "Bez diakritiky", nodia_h = "malé písmo ji stejně nemá", ink = "Barva textu", bg = "Pozadí", bg_h = "nad obrázkem obrys textu", glint = "Odlesk", run = "Dlouhý text", run_h = "když se nevejde do řádku", run_o = { "na řádky", "běží dokola", "odráží se" }, speed = "Rychlost běhu", speed_h = "jen když text běží" }, })[lang] or {}) do txt[k] = v end local FONTS = gfx.FONTS settings = { { key = "msg", type = "text", default = "HELLO WORLD", label = txt.msg, hint = txt.msg_h }, { key = "font", type = "int", default = "auto", label = txt.font, hint = txt.font_h, opts = { keys = { "auto", table.unpack(FONTS) }, txt.auto, table.unpack(FONTS) } }, { key = "upper", type = "bool", default = false, label = txt.upper, hint = txt.upper_h }, { key = "nodia", type = "bool", default = false, label = txt.nodia, hint = txt.nodia_h }, { key = "ink", type = "color", default = "#FFCF47", label = txt.ink }, { key = "bg", type = "color", default = "#000000", label = txt.bg, hint = txt.bg_h }, { key = "glint", type = "bool", default = true, label = txt.glint }, { key = "run", type = "int", default = "lines", label = txt.run, hint = txt.run_h, opts = { keys = { "lines", "loop", "bounce" }, table.unpack(txt.run_o) } }, { key = "speed", type = "int", min = 2, max = 40, default = 14, unit = "px/s", label = txt.speed, hint = txt.speed_h }, } local O, G = {}, {} local OUTL = { 0, 0, 0 } local rows, RUN = nil, { s = "", y = 0 } local itop, ibot = 0, -1 local ctop, cbot, cw = 0, -1, 1 local total, last, t0 = 0, nil, 0 local function ink_w(s) local lo, hi = text_bounds(s, O) return hi - lo + 1, lo end local function wrap(s) local out, cur = {}, nil for w in gmatch(s, "%S+") do if ink_w(w) > W then return nil end local t = cur and (cur .. " " .. w) or w if cur and ink_w(t) > W then out[#out + 1] = cur; cur = w else cur = t end end out[#out + 1] = cur return out end local function lay_out(list) itop, ibot = 99, -99 for _, s in ipairs(list) do local a, b = text_ink(s, O) if b >= a then itop, ibot = min(itop, a), max(ibot, b) end end if ibot < itop then return nil end local ih, f = ibot - itop + 1, gfx.font(O.font) local pitch = max(ih + 1, min(f.h, ih + max(2, f.cap // 3))) local bh = (#list - 1) * pitch + ih if bh > H then return nil end local top, out = (H - bh) // 2, {} for i, s in ipairs(list) do local w, lo = ink_w(s) out[i] = { s = s, x = (W - w) // 2 - lo, y = top + (i - 1) * pitch - itop, n = ulen(s) or #s } end return out end local function stand(s, font, multi) O = { font = font } local list = ink_w(s) <= W and { s } or (multi and wrap(s)) return list and lay_out(list) end function on_settings() local s = cfg.msg or "" if cfg.upper then s = upper(s) end if cfg.nodia then s = ascii(s) end local multi = cfg.run == "lines" if cfg.font ~= "auto" then rows = stand(s, cfg.font, multi) else rows = nil for i = #FONTS, 1, -1 do rows = stand(s, FONTS[i], multi) if rows or FONTS[i] == "6x10" then break end end if not rows then O = { font = "8x13b" } end end if not rows then O.scroll = { cfg.speed or 14, mode = cfg.run == "bounce" and "bounce" or "loop" } local a, b = text_ink(s, O) itop, ibot = a, b RUN = { s = s, y = (H - (b - a + 1)) // 2 - a } end G = { font = O.font, scroll = O.scroll, alpha = 1 } local b = cfg.bg OUTL[1], OUTL[2], OUTL[3] = b.r, b.g, b.b ctop, cbot = text_ink("H", O) cw = gfx.font(O.font).w >= 9 and 2 or 1 total = 0 for _, r in ipairs(rows or {}) do total = total + r.n end local key = O.font .. "\0" .. s .. "\0" .. tostring(rows ~= nil) if key ~= last then last, t0 = key, frame or 0 end end local function per_char() return min(0.12, 1.4 / max(total, 1)) end local GL_V, GL_WIN = 56, 8 local SPAN = W + 4 + H // 2 local function glint_head(f) local done = (f - t0) / FPS - total * per_char() if done < 0 then return nil end local s if done * GL_V < SPAN then s = done else local t = data.time if not (t and t.ok) then return nil end local win = t.unix // GL_WIN local h = hash32(win * 0x9E3779B1 + 7) s = t.unix % GL_WIN + t.ms / 1000 - umod(h, floor((GL_WIN - SPAN / GL_V) * 100)) / 100 if s < 0 or s * GL_V >= SPAN then return nil end end return floor(s * GL_V) - 4 - H // 2 end local GL_BAND = { { 0, 4, 0.35 }, { 1, 2, 0.77 } } local function glint(head, r) local y, yb = r.y + itop, r.y + ibot while y <= yb do local q = (H - 1 - y) // 2 local y2 = min(yb, H - 1 - 2 * q) local cx = head + q if cx > -4 and cx < W then for _, k in ipairs(GL_BAND) do clip(cx + k[1], y, k[2], y2 - y + 1) G.alpha = k[3] if r.x then text(r.x, r.y, r.s, 255, 255, 255, G) else text_center(r.y, r.s, 255, 255, 255, G) end end end y = y2 + 1 end clip() end local prev_f = math.huge function render() if frame < prev_f then t0 = data.arrive and 0 or -3600 * FPS end prev_f = frame local img = gfx.bg() local b = cfg.bg if not img then clear(b.r, b.g, b.b) end O.outline = img and OUTL or nil local c = cfg.ink local head = cfg.glint and glint_head(frame) if not rows then if RUN.s == "" then return end text_center(RUN.y, RUN.s, c.r, c.g, c.b, O) if head then glint(head, RUN) end return end local left = floor((frame - t0) / FPS / per_char()) for _, r in ipairs(rows) do if left <= 0 then break end if left >= r.n then text(r.x, r.y, r.s, c.r, c.g, c.b, O) if head then glint(head, r) end else local part = sub(r.s, 1, uoff(r.s, left + 1) - 1) text(r.x, r.y, part, c.r, c.g, c.b, O) rect(r.x + text_width(part, O), r.y + ctop, cw, cbot - ctop + 1, c.r, c.g, c.b) end left = left - r.n end end