-- 5x7 bitmap font drawn with filled rectangles, so a compiled Lua program can -- label its interface without linking any font library. local M = {} local GLYPH_WIDTH = 5 local GLYPH_HEIGHT = 7 local SPACING = 1 local GLYPHS = { [" "] = { "00000", "00000", "00000", "00000", "00000", "00000", "00000" }, ["A"] = { "01110", "10001", "10001", "11111", "10001", "10001", "10001" }, ["B"] = { "11110", "10001", "10001", "11110", "10001", "10001", "11110" }, ["C"] = { "01110", "10001", "10000", "10000", "10000", "10001", "01110" }, ["D"] = { "11110", "10001", "10001", "10001", "10001", "10001", "11110" }, ["E"] = { "11111", "10000", "10000", "11110", "10000", "10000", "11111" }, ["F"] = { "11111", "10000", "10000", "11110", "10000", "10000", "10000" }, ["G"] = { "01110", "10001", "10000", "10111", "10001", "10001", "01111" }, ["H"] = { "10001", "10001", "10001", "11111", "10001", "10001", "10001" }, ["I"] = { "01110", "00100", "00100", "00100", "00100", "00100", "01110" }, ["J"] = { "00111", "00010", "00010", "00010", "00010", "10010", "01100" }, ["K"] = { "10001", "10010", "10100", "11000", "10100", "10010", "10001" }, ["L"] = { "10000", "10000", "10000", "10000", "10000", "10000", "11111" }, ["M"] = { "10001", "11011", "10101", "10101", "10001", "10001", "10001" }, ["N"] = { "10001", "11001", "10101", "10011", "10001", "10001", "10001" }, ["O"] = { "01110", "10001", "10001", "10001", "10001", "10001", "01110" }, ["P"] = { "11110", "10001", "10001", "11110", "10000", "10000", "10000" }, ["Q"] = { "01110", "10001", "10001", "10001", "10101", "10010", "01101" }, ["R"] = { "11110", "10001", "10001", "11110", "10100", "10010", "10001" }, ["S"] = { "01111", "10000", "10000", "01110", "00001", "00001", "11110" }, ["T"] = { "11111", "00100", "00100", "00100", "00100", "00100", "00100" }, ["U"] = { "10001", "10001", "10001", "10001", "10001", "10001", "01110" }, ["V"] = { "10001", "10001", "10001", "10001", "10001", "01010", "00100" }, ["W"] = { "10001", "10001", "10001", "10101", "10101", "11011", "10001" }, ["X"] = { "10001", "10001", "01010", "00100", "01010", "10001", "10001" }, ["Y"] = { "10001", "10001", "01010", "00100", "00100", "00100", "00100" }, ["Z"] = { "11111", "00001", "00010", "00100", "01000", "10000", "11111" }, ["0"] = { "01110", "10001", "10011", "10101", "11001", "10001", "01110" }, ["1"] = { "00100", "01100", "00100", "00100", "00100", "00100", "01110" }, ["2"] = { "01110", "10001", "00001", "00010", "00100", "01000", "11111" }, ["3"] = { "11111", "00010", "00100", "00010", "00001", "10001", "01110" }, ["4"] = { "00010", "00110", "01010", "10010", "11111", "00010", "00010" }, ["5"] = { "11111", "10000", "11110", "00001", "00001", "10001", "01110" }, ["6"] = { "00110", "01000", "10000", "11110", "10001", "10001", "01110" }, ["7"] = { "11111", "00001", "00010", "00100", "01000", "01000", "01000" }, ["8"] = { "01110", "10001", "10001", "01110", "10001", "10001", "01110" }, ["9"] = { "01110", "10001", "10001", "01111", "00001", "00010", "01100" }, [":"] = { "00000", "00100", "00100", "00000", "00100", "00100", "00000" }, ["."] = { "00000", "00000", "00000", "00000", "00000", "00100", "00100" }, [","] = { "00000", "00000", "00000", "00000", "00100", "00100", "01000" }, ["-"] = { "00000", "00000", "00000", "11111", "00000", "00000", "00000" }, ["/"] = { "00001", "00010", "00010", "00100", "01000", "01000", "10000" }, ["("] = { "00010", "00100", "01000", "01000", "01000", "00100", "00010" }, [")"] = { "01000", "00100", "00010", "00010", "00010", "00100", "01000" }, ["%"] = { "11001", "11010", "00010", "00100", "01000", "01011", "10011" }, ["#"] = { "01010", "01010", "11111", "01010", "11111", "01010", "01010" }, ["!"] = { "00100", "00100", "00100", "00100", "00100", "00000", "00100" }, ["?"] = { "01110", "10001", "00001", "00010", "00100", "00000", "00100" }, ["+"] = { "00000", "00100", "00100", "11111", "00100", "00100", "00000" }, ["="] = { "00000", "00000", "11111", "00000", "11111", "00000", "00000" }, ["'"] = { "00100", "00100", "00000", "00000", "00000", "00000", "00000" }, ["<"] = { "00010", "00100", "01000", "10000", "01000", "00100", "00010" }, [">"] = { "01000", "00100", "00010", "00001", "00010", "00100", "01000" }, ["*"] = { "00000", "10101", "01110", "11111", "01110", "10101", "00000" }, ["|"] = { "00100", "00100", "00100", "00100", "00100", "00100", "00100" }, } -- Compile every glyph into horizontal pixel runs once, so drawing a label -- does not rescan its bitmap strings on every frame. local function compile_glyph(rows) local compiled = {} for index = 1, GLYPH_HEIGHT do local line = rows[index] local runs = {} local column = 1 while column <= GLYPH_WIDTH do if string.sub(line, column, column) == "1" then local run_end = column while run_end < GLYPH_WIDTH and string.sub(line, run_end + 1, run_end + 1) == "1" do run_end = run_end + 1 end runs[#runs + 1] = column - 1 runs[#runs + 1] = run_end - column + 1 column = run_end + 1 else column = column + 1 end end compiled[index] = runs end return compiled end for character, rows in pairs(GLYPHS) do GLYPHS[character] = compile_glyph(rows) end M.glyph_width = GLYPH_WIDTH M.glyph_height = GLYPH_HEIGHT M.spacing = SPACING function M.width(text, scale) local factor = scale or 1 if text == nil or #text == 0 then return 0 end return (#text * (GLYPH_WIDTH + SPACING) - SPACING) * factor end function M.height(scale) return GLYPH_HEIGHT * (scale or 1) end -- Draws uppercase ASCII text with one rectangle per horizontal run of pixels. function M.draw(surface, x, y, text, r, g, b, scale) if text == nil or surface == nil then return 0 end local factor = scale or 1 local upper = string.upper(text) local cursor = x local drawn = 0 for index = 1, #upper do local glyph = GLYPHS[string.sub(upper, index, index)] if glyph ~= nil then for row = 1, GLYPH_HEIGHT do local runs = glyph[row] local row_y = y + (row - 1) * factor for run = 1, #runs, 2 do surface.rect(cursor + runs[run] * factor, row_y, runs[run + 1] * factor, factor, r, g, b) drawn = drawn + 1 end end end cursor = cursor + (GLYPH_WIDTH + SPACING) * factor end return drawn end return M