rename: src/Formats -> src/Rosetta, @formats -> @rosetta requires, build/luajit/rosetta + rosetta._compat, build/roblox/Rosetta, ROSETTA_* env/globals, writer vendor strings

This commit is contained in:
4DBug committed 2026-11-01 01:19:16 -05:00
1 parent 2956576d7f
commit 22ddcd78fa
941 files changed
+3364 -3364

No files matched your search

+220
View File
@@ -0,0 +1,220 @@
--!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).
local Err = require("@rosetta/core/Err")
local Types = require("@rosetta/Types")
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)
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
-- 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))
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
-- ------------------------------------------------------------------------------------------------ font
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
-- 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
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
-- 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
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
-- ------------------------------------------------------------------------------------------------ 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)
}
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
-- 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")
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