#!/usr/bin/luajit local smolhershey = require "smolhershey" local rotate, filename = ... -- command-line arguments local font = smolhershey.load(filename or "imlac-pds-1-ssvchr.22.jhf") local canvas_methods = {} -- Set a pixel in the canvas. function canvas_methods.pset(canvas, x, y) if canvas[x] == nil then canvas[x] = {} end canvas[x][y] = 1 end local function round(x) return math.floor(x + 0.5) end -- Default coordinate transformation is identity function canvas_methods.xform(canvas, x1, y1) return x1, y1 end -- Draw a line using Amit’s straightforward lerp method from -- . function canvas_methods.line(canvas, x1, y1, x2, y2) x1, y1 = canvas:xform(x1, y1) x2, y2 = canvas:xform(x2, y2) local Δz = math.max(math.abs(x1 - x2), math.abs(y1 - y2)) if Δz == 0 then -- Avoid division by zero errors canvas:pset(x1, y1) return end local mx = (x2 - x1) / Δz -- Slope in X local my = (y2 - y1) / Δz -- Slope in Y for z = 0, Δz do canvas:pset(round(mx*z + x1), round(my*z + y1)) end end function canvas_methods.draw(canvas) local xmin, xmax, ymin, ymax for x, col in pairs(canvas) do if type(x) ~= "string" then -- handle .xform override case if xmin == nil or x < xmin then xmin = x end if xmax == nil or x > xmax then xmax = x end for y in pairs(col) do if ymin == nil or y < ymin then ymin = y end if ymax == nil or y > ymax then ymax = y end end end end for y = ymin, ymax do for x = xmin, xmax do if canvas[x] and canvas[x][y] then io.write "@@" else io.write " " end end io.write "\n" end end local function canvas() return setmetatable({}, {__index = canvas_methods}) end local cvs = canvas() if rotate and rotate ~= "-nr" then cvs.xform = function (canvas, x, y) return x + y, y - x end end local function cvsline(...) cvs:line(...) end local gc = font:gc(0, 0, cvsline) local x = gc.cp.x -- Save X position gc:show("Hello, Mina") gc.cp = {x=x, y=gc.cp.y + 28} -- Move down 28 units gc:show("{}@&-/\\") cvs:draw()