Files
rosetta/tests/codecs/bitmap.luau
T

72 lines
3.0 KiB
Luau

-- codecs/font/Bitmap tests: 1-bit row pack / unpack, the Font over a strike, Bitmap.strike, argument errors.
local T = require("@tests/T")
local Bitmap = require("@rosetta/codecs/font/Bitmap")
local Ttf = require("@rosetta/font/ttf")
local Bytes = require("@rosetta/core/Bytes")
local Err = require("@rosetta/core/Err")
local Fx = require("@tests/gen/fonts")
local t = T.suite("bitmap")
local rand = T.rng(3)
for k = 1, 30 do
local w, h = rand(40), rand(20)
local px = buffer.create(w * h)
for i = 0, w * h - 1 do
if rand(2) == 1 then
buffer.writeu8(px, i, 255)
end
end
local stride = math.ceil(w / 8) + (k % 3)
local lsb = k % 2 == 0
local packed = Bitmap.pack(px, w, h, stride, lsb)
t:eq(buffer.len(packed), stride * h, "pack size " .. k)
t:eq(buffer.tostring(Bitmap.unpack(packed, 0, w, h, stride, lsb)), buffer.tostring(px), "round trip " .. k)
end
-- 0x81 = left and right pixel on (MSB first)
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0x81 }), 0, 8, 1, 1)), "\255\0\0\0\0\0\0\255", "msb order")
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0x01 }), 0, 2, 1, 1, true)), "\255\0", "lsb order")
t:eq(buffer.tostring(Bitmap.unpack(T.bytes({ 0xFF }), 0, 8, 2, 1)), string.rep("\255", 8) .. string.rep("\0", 8), "past the end reads 0")
local A = { codepoint = 65, width = 2, height = 2, left = 1, top = 7, advance = 4, pixels = T.bytes({ 255, 0, 0, 255 }) }
local strike = { size = 8, ascent = 7, descent = 1, glyphs = { { width = 0, height = 0, left = 0, top = 0, advance = 3, pixels = buffer.create(0) }, A }, default = 0 }
local font = Bitmap.font(strike, { family = "Test" })
t:eq(font.kind, "font", "kind")
t:eq(font.unitsPerEm, 8, "unitsPerEm")
t:eq(font.descent, -1, "descent")
t:eq(font:glyphIndex(65), 1, "glyphIndex")
t:eq(font:glyphIndex(66), 0, "default glyph")
t:eq(font:advance(1), 4, "advance")
t:eq(font:outline(1), nil, "no outline")
local img = font:rasterize(1, 16)
t:check(img and img.format == "alpha8" and img.width == 2 and buffer.tostring(img.pixels) == "\255\0\0\255", "rasterize")
t:eq(img and img.meta.top, 7, "rasterize top")
t:eq(img and img.meta.scale, 2, "rasterize scale")
t:eq(font:rasterize(9, 8), nil, "rasterize out of range")
t:eq(Bitmap.strike(font), strike, "strike of a bitmap font")
local s1, e1 = Bitmap.strike({})
t:check(s1 == nil and e1 and e1.code == Err.ARG, "strike of a non-font")
if Fx.fonts[1] then
local ttf = Ttf.decode(Bytes.reader(buffer.fromstring(Fx.fonts[1].data)))
local s, err = Bitmap.strike(ttf, { px = 16, chars = "AB" })
t:check(s, "strike of " .. Fx.fonts[1].name .. ": " .. tostring(err))
if s then
t:eq(#s.glyphs, 3, "strike glyphs")
local g = s.glyphs[2]
t:eq(g.codepoint, 65, "strike codepoint")
t:check(g.width > 4 and g.height > 6 and g.top > 6 and g.advance > 4, "strike A metrics")
local on = 0
for i = 0, buffer.len(g.pixels) - 1 do
local v = buffer.readu8(g.pixels, i)
t:check(v == 0 or v == 255, "binary pixels")
if v == 255 then
on += 1
end
end
t:check(on > 10, "A has ink")
end
t:check(select(2, Bitmap.strike(ttf, { px = 0 })) ~= nil, "px 0 refused")
end
t:done()