Files
rosetta/src/Rosetta/codecs/font/Bitmap.luau
T

200 lines
5.8 KiB
Luau

--!strict
-- codecs/font/Bitmap: the standard Font value for bitmap fonts (BDF, PCF, PSF, Windows FON / FNT) and their 1-bit rows.
-- Bit rows are MSB = left pixel unless lsbFirst; a pixel >= 128 is on.
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
local Bitmap = {}
local readu8, writeu8 = buffer.readu8, buffer.writeu8
function Bitmap.unpack(b: buffer, off: number, w: number, h: number, stride: number, lsbFirst: boolean?): buffer
local out = buffer.create(math.max(0, w * h))
local n = buffer.len(b)
for y = 0, h - 1 do
local row = off + y * stride
for x = 0, w - 1 do
local p = row + bit32.rshift(x, 3)
if p >= 0 and p < n then
local byte = readu8(b, p)
local bitn = if lsbFirst then bit32.band(x, 7) else 7 - bit32.band(x, 7)
if bit32.band(bit32.rshift(byte, bitn), 1) ~= 0 then
writeu8(out, y * w + x, 255)
end
end
end
end
return out
end
function Bitmap.pack(px: buffer, w: number, h: number, stride: number?, lsbFirst: boolean?): buffer
local s = stride or math.ceil(w / 8)
local out = buffer.create(math.max(0, s * h))
local n = buffer.len(px)
for y = 0, h - 1 do
for x = 0, w - 1 do
local i = y * w + x
if i < n and readu8(px, i) >= 128 then
local p = y * s + bit32.rshift(x, 3)
local bitn = if lsbFirst then bit32.band(x, 7) else 7 - bit32.band(x, 7)
writeu8(out, p, bit32.bor(readu8(out, p), bit32.lshift(1, bitn)))
end
end
end
return out
end
local Face = {}
Face.__index = Face
function Face.glyphIndex(self: any, cp: number): number
return self._index[cp] or 0
end
function Face.advance(self: any, g: number): number
local gl = self.bitmap.glyphs[g + 1]
return if gl then gl.advance else 0
end
function Face.kern(_self: any, _a: number, _b: number): number
return 0
end
function Face.outline(_self: any, _g: number): { any }?
return nil
end
function Face.rasterize(self: any, g: number, px: number?): Types.Image?
local gl: Types.BitmapGlyph? = self.bitmap.glyphs[g + 1]
if not gl or gl.width < 0 or gl.height < 0 then
return nil
end
local img = Types.Image.new(gl.width, gl.height, "alpha8")
local n = math.min(buffer.len(img.pixels), buffer.len(gl.pixels))
if n > 0 then
buffer.copy(img.pixels, 0, gl.pixels, 0, n)
end
img.meta.left = gl.left
img.meta.top = gl.top
img.meta.advance = gl.advance
img.meta.glyph = g
img.meta.scale = if type(px) == "number" and self.bitmap.size > 0 then px / self.bitmap.size else 1
return img
end
function Bitmap.font(strike: Types.BitmapStrike, info: { [string]: any }?, meta: { [string]: any }?): any
local index: { [number]: number } = {}
for i, gl in strike.glyphs do
local cp = gl.codepoint
if cp and index[cp] == nil then
index[cp] = i - 1
end
end
if strike.map then
for cp, g in strike.map do
if index[cp] == nil and g >= 0 and g < #strike.glyphs then
index[cp] = g
end
end
end
local f = setmetatable({
kind = "font",
info = info or {},
unitsPerEm = strike.size,
ascent = strike.ascent,
descent = -strike.descent,
lineGap = 0,
numGlyphs = #strike.glyphs,
bitmap = strike,
meta = meta or {},
_index = index,
}, Face)
if strike.default then
setmetatable(index, { __index = function()
return strike.default
end })
end
return f
end
export type StrikeOptions = {
px: number?,
chars: (string | { number })?,
threshold: number?,
}
local function codepoints(chars: (string | { number })?): { number }
if type(chars) == "table" then
return chars
end
local out = {}
if type(chars) == "string" then
for _, c in utf8.codes(chars) do
table.insert(out, c)
end
return out
end
for c = 32, 126 do
table.insert(out, c)
end
return out
end
function Bitmap.strike(font: any, opts: StrikeOptions?): (Types.BitmapStrike?, Err.Err?)
if type(font) ~= "table" or font.kind ~= "font" then
return nil, Err.new(Err.ARG, "expected a Font")
end
if type(font.bitmap) == "table" and type(font.bitmap.glyphs) == "table" then
return font.bitmap, nil
end
if type(font.rasterize) ~= "function" or type(font.glyphIndex) ~= "function" then
return nil, Err.new(Err.ARG, "font has no glyphs to rasterize")
end
local o: StrikeOptions = opts or {}
local px = o.px or 16
if type(px) ~= "number" or px < 1 or px > 1024 then
return nil, Err.new(Err.ARG, "px must be 1..1024")
end
local threshold = o.threshold or 128
local upm = if type(font.unitsPerEm) == "number" and font.unitsPerEm > 0 then font.unitsPerEm else px
local scale = px / upm
local ascent = math.ceil((font.ascent or upm) * scale)
local descent = math.ceil(math.abs(font.descent or 0) * scale)
local glyphs: { Types.BitmapGlyph } = {}
local function add(cp: number?, g: number)
local ok, img = pcall(font.rasterize, font, g, px)
local adv = 0
local aok, a = pcall(font.advance, font, g)
if aok and type(a) == "number" then
adv = math.floor(a * scale + 0.5)
end
local gl: Types.BitmapGlyph = { codepoint = cp, width = 0, height = 0, left = 0, top = 0, advance = adv, pixels = buffer.create(0) }
if ok and type(img) == "table" and type(img.pixels) == "buffer" and img.format == "alpha8" then
local n = img.width * img.height
local pxs = buffer.create(n)
for i = 0, math.min(n, buffer.len(img.pixels)) - 1 do
if readu8(img.pixels, i) >= threshold then
writeu8(pxs, i, 255)
end
end
gl.width, gl.height, gl.pixels = img.width, img.height, pxs
gl.left = math.floor(img.meta.left or 0)
gl.top = math.floor(img.meta.top or 0)
if type(img.meta.advance) == "number" then
gl.advance = math.floor(img.meta.advance + 0.5)
end
end
table.insert(glyphs, gl)
end
add(nil, 0)
for _, cp in codepoints(o.chars) do
local ok, g = pcall(font.glyphIndex, font, cp)
if ok and type(g) == "number" and g > 0 then
add(cp, g)
end
end
return { size = px, ascent = ascent, descent = descent, glyphs = glyphs, default = 0 }, nil
end
return Bitmap