codecs/font: comments to specs only

This commit is contained in:
4DBug committed 2026-11-01 01:57:58 -05:00
1 parent 878e1b3a44
commit f1ad32105b
7 files changed
+52 -155

No files matched your search

+5 -26
View File
@@ -1,18 +1,6 @@
--!strict
-- codecs/font/Bitmap: the standard Font value for bitmap fonts (BDF, PCF, PSF, Windows FON / FNT): a Types.BitmapStrike
-- (one pixel size, glyphs as u8 bitmaps, 0 = off / 255 = on) wrapped as a Types.Font whose units are pixels
-- (unitsPerEm = the pixel size), glyphIndex / advance / kern work, outline is nil, and rasterize(g) returns the glyph's
-- bitmap as an alpha8 Image (img.meta.left / top / advance, as codecs/font/TrueType does; the px argument is ignored,
-- img.meta.scale = px / size tells the caller how much to scale). Also: the strike of any Font (its own bitmap, or an
-- outline font rasterized at a pixel size and thresholded), which the bitmap font encoders write, and the bit-row
-- packing every bitmap font format shares. New code.
--
-- local font = Bitmap.font(strike, { family = "Fixed" }, { source = { format = "bdf" } })
-- local strike, err = Bitmap.strike(anyFont, { px = 16, chars = "abc" }) -- font.bitmap, or rasterized
-- local pixels = Bitmap.unpack(buf, off, w, h, stride) -- MSB-first 1-bit rows -> u8 0 / 255
-- local bytes = Bitmap.pack(pixels, w, h, stride) -- u8 -> MSB-first rows (buffer)
--
-- Never throws: rasterize of a bad glyph index returns nil; unpack reads only inside the buffer (missing bits are 0).
-- 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")
@@ -20,9 +8,6 @@ local Bitmap = {}
local readu8, writeu8 = buffer.readu8, buffer.writeu8
-- ------------------------------------------------------------------------------------------------ bits
-- unpack(buf, off, w, h, stride, lsbFirst?) -> u8 pixels (w * h, 0 / 255) from 1-bit rows of stride bytes (MSB = left
-- pixel unless lsbFirst); bytes past the end of buf read as 0
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)
@@ -42,7 +27,6 @@ function Bitmap.unpack(b: buffer, off: number, w: number, h: number, stride: num
return out
end
-- pack(pixels, w, h, stride?, lsbFirst?) -> 1-bit rows (stride bytes each, default ceil(w / 8)); a pixel >= 128 is on
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))
@@ -60,7 +44,6 @@ function Bitmap.pack(px: buffer, w: number, h: number, stride: number?, lsbFirst
return out
end
-- ------------------------------------------------------------------------------------------------ font
local Face = {}
Face.__index = Face
@@ -81,7 +64,6 @@ function Face.outline(_self: any, _g: number): { any }?
return nil
end
-- rasterize(g, px?) -> the glyph as an alpha8 Image (px is ignored: meta.scale = px / size)
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
@@ -100,7 +82,6 @@ function Face.rasterize(self: any, g: number, px: number?): Types.Image?
return img
end
-- font(strike, info?, meta?) -> a Types.Font over the strike (font.bitmap = strike)
function Bitmap.font(strike: Types.BitmapStrike, info: { [string]: any }?, meta: { [string]: any }?): any
local index: { [number]: number } = {}
for i, gl in strike.glyphs do
@@ -136,11 +117,10 @@ function Bitmap.font(strike: Types.BitmapStrike, info: { [string]: any }?, meta:
return f
end
-- ------------------------------------------------------------------------------------------------ strike
export type StrikeOptions = {
px: number?, -- pixel size for outline fonts (default 16)
chars: (string | { number })?, -- code points for outline fonts (default 32..126)
threshold: number?, -- coverage 0..255 at or above which a pixel is on (default 128)
px: number?,
chars: (string | { number })?,
threshold: number?,
}
local function codepoints(chars: (string | { number })?): { number }
@@ -160,7 +140,6 @@ local function codepoints(chars: (string | { number })?): { number }
return out
end
-- strike(font, opts?) -> the font's BitmapStrike: font.bitmap, else the outline font rasterized at opts.px
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")