--!strict -- font/pcf: X11 Portable Compiled Format bitmap fonts (.pcf; .pcf.gz loads through the api's gzip unwrap) as the -- standard Font with a bitmap strike (codecs/font/Bitmap): the table of contents, PROPERTIES (meta.properties), -- ACCELERATORS / BDF_ACCELERATORS (font ascent / descent), METRICS (compressed and not), BITMAPS in any glyph pad -- (1 / 2 / 4 / 8), scan unit and bit / byte order, BDF_ENCODINGS (one- and two-byte), SWIDTHS and GLYPH_NAMES. -- Code points as font/bdf: the encoding is Unicode for ISO10646, ISO8859-1 and registry-less fonts; other charsets -- keep glyphs unmapped (meta.encodings). Written from the PCF description in libXfont / FreeType's pcf driver -- documentation (no code copied). Not supported: writing (write BDF and compile it with bdftopcf), INK_METRICS. -- ------------------------------------------------------------------------------------------------ requires local Err = require("@formats/core/Err") local Bytes = require("@formats/core/Bytes") local Types = require("@formats/Types") local Bitmap = require("@formats/codecs/font/Bitmap") -- ------------------------------------------------------------------------------------------------ contract local Pcf = { name = "pcf", category = "font", extensions = { "pcf" }, mime = { "application/x-font-pcf" }, deps = { "core/Err", "core/Bytes", "Types", "codecs/font/Bitmap" }, caps = { decode = true, encode = false, bitmap = true }, } export type DecodeOptions = { maxGlyphs: number? } -- default 1114112 -- ------------------------------------------------------------------------------------------------ constants local PROPERTIES, ACCELERATORS, METRICS, BITMAPS = 1, 2, 4, 8 local BDF_ENCODINGS, SWIDTHS, GLYPH_NAMES, BDF_ACCELERATORS = 32, 64, 128, 256 local COMPRESSED = 0x100 -- ------------------------------------------------------------------------------------------------ sniff function Pcf.sniff(r: Bytes.Reader): number return if r:peek(4) == "\1fcp" then 1 else 0 end -- ------------------------------------------------------------------------------------------------ decode type Table = { format: number, size: number, offset: number } -- a reader positioned after a table's format word, with the integer readers for its byte order type TR = { r: Bytes.Reader, format: number, big: boolean } local function open(r: Bytes.Reader, t: Table?): TR? if not t or t.offset + 4 > r:size() then return nil end local s = r:sub(t.offset, t.size) local format = s:u32le() return { r = s, format = format, big = bit32.band(format, 4) ~= 0 } end local function u32(t: TR): number return if t.big then t.r:u32be() else t.r:u32le() end local function i32(t: TR): number return if t.big then t.r:i32be() else t.r:i32le() end local function i16(t: TR): number return if t.big then t.r:i16be() else t.r:i16le() end local function u16(t: TR): number return if t.big then t.r:u16be() else t.r:u16le() end local REV = buffer.create(256) for i = 0, 255 do local v, rv = i, 0 for _ = 1, 8 do rv = rv * 2 + v % 2 v //= 2 end buffer.writeu8(REV, i, rv) end function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?) local o: DecodeOptions = opts or {} local maxGlyphs = o.maxGlyphs or 1114112 if r:string(4) ~= "\1fcp" then return nil, Err.new(if r.err then Err.TRUNCATED else Err.CORRUPT, "not a PCF file") end local count = r:u32le() if count > 64 then return nil, Err.new(Err.CORRUPT, "too many tables") end local tables: { [number]: Table } = {} for _ = 1, count do local ty, format, size, offset = r:u32le(), r:u32le(), r:u32le(), r:u32le() tables[ty] = { format = format, size = size, offset = offset } end if r.err then return nil, r.err end -- properties local props: { [string]: any } = {} local pt = open(r, tables[PROPERTIES]) if pt then local n = i32(pt) if n < 0 or n > 4096 then return nil, Err.new(Err.CORRUPT, "bad property count") end local raw = {} for k = 1, n do raw[k] = { i32(pt), pt.r:u8(), i32(pt) } end if n % 4 ~= 0 then pt.r:skip(4 - n % 4) end local strSize = i32(pt) local strAt = pt.r:tell() local function str(off: number): string if off < 0 or off >= strSize then return "" end local s = pt.r:sub(strAt + off, strSize - off) return s:cstring() end for _, p in raw do props[str(p[1])] = if p[2] ~= 0 then str(p[3]) else p[3] end if pt.r.err then return nil, Err.new(Err.TRUNCATED, "properties table") end end -- accelerators: ascent / descent local ascent, descent = nil, nil local at = open(r, tables[BDF_ACCELERATORS] or tables[ACCELERATORS]) if at then at.r:skip(8) ascent, descent = i32(at), i32(at) end -- metrics local mt = open(r, tables[METRICS]) if not mt then return nil, Err.new(Err.CORRUPT, "no METRICS table") end local metrics = {} local compressed = bit32.band(mt.format, COMPRESSED) ~= 0 local nm = if compressed then u16(mt) else u32(mt) if nm > maxGlyphs then return nil, Err.new(Err.LIMIT, "more than maxGlyphs glyphs") end for k = 1, nm do if compressed then local b = mt.r metrics[k] = { b:u8() - 0x80, b:u8() - 0x80, b:u8() - 0x80, b:u8() - 0x80, b:u8() - 0x80 } else metrics[k] = { i16(mt), i16(mt), i16(mt), i16(mt), i16(mt) } mt.r:skip(2) end end if mt.r.err then return nil, Err.new(Err.TRUNCATED, "metrics table") end -- bitmaps local bt = open(r, tables[BITMAPS]) if not bt then return nil, Err.new(Err.CORRUPT, "no BITMAPS table") end local nb = u32(bt) if nb ~= nm then return nil, Err.new(Err.CORRUPT, "bitmap count differs from metrics count") end local offsets = table.create(nb, 0) for k = 1, nb do offsets[k] = u32(bt) end local sizes = { u32(bt), u32(bt), u32(bt), u32(bt) } local padIndex = bit32.band(bt.format, 3) local pad = bit32.lshift(1, padIndex) local unit = bit32.lshift(1, bit32.band(bit32.rshift(bt.format, 4), 3)) local msbBit = bit32.band(bt.format, 8) ~= 0 local dataAt = bt.r:tell() local dataSize = sizes[padIndex + 1] if bt.r.err or dataAt + dataSize > bt.r:size() then return nil, Err.new(Err.TRUNCATED, "bitmaps table") end -- the bitmap data normalised to MSB-first bits and bytes local bits = bt.r:sub(dataAt, dataSize):bytes(dataSize) if not msbBit then for k = 0, dataSize - 1 do buffer.writeu8(bits, k, buffer.readu8(REV, buffer.readu8(bits, k))) end end if bt.big ~= msbBit and unit > 1 then for k = 0, dataSize - unit, unit do for j = 0, unit // 2 - 1 do local x, y = buffer.readu8(bits, k + j), buffer.readu8(bits, k + unit - 1 - j) buffer.writeu8(bits, k + j, y) buffer.writeu8(bits, k + unit - 1 - j, x) end end end local glyphs: { Types.BitmapGlyph } = table.create(nm) for k = 1, nm do local m = metrics[k] local w, h = m[2] - m[1], m[4] + m[5] if w < 0 or h < 0 or w > 4096 or h > 4096 then return nil, Err.new(Err.CORRUPT, "bad glyph metrics") end local stride = math.ceil(w / (pad * 8)) * pad local px = Bitmap.unpack(bits, offsets[k], w, h, stride) glyphs[k] = { width = w, height = h, left = m[1], top = m[4], advance = m[3], pixels = px } end -- encodings local encodings: { [number]: number } = {} local default: number? = nil local et = open(r, tables[BDF_ENCODINGS]) if et then local min2, max2, min1, max1, def = i16(et), i16(et), i16(et), i16(et), i16(et) if min2 < 0 or max2 > 255 or min1 < 0 or max1 > 255 or max2 < min2 or max1 < min1 then return nil, Err.new(Err.CORRUPT, "bad encoding ranges") end for b1 = min1, max1 do for b2 = min2, max2 do local g = u16(et) if g ~= 0xFFFF and g < nm and encodings[g + 1] == nil then encodings[g + 1] = b1 * 256 + b2 end end end if et.r.err then return nil, Err.new(Err.TRUNCATED, "encodings table") end local dcols = max2 - min2 + 1 local d1, d2 = def // 256, def % 256 if d1 >= min1 and d1 <= max1 and d2 >= min2 and d2 <= max2 then local slot = (d1 - min1) * dcols + (d2 - min2) local dr = r:sub(tables[BDF_ENCODINGS].offset + 14 + slot * 2, 2) local g = if et.big then dr:u16be() else dr:u16le() if g ~= 0xFFFF and g < nm then default = g end end end -- glyph names local nt = open(r, tables[GLYPH_NAMES]) if nt then local n = u32(nt) if n == nm then local offs = table.create(n, 0) for k = 1, n do offs[k] = u32(nt) end local strSize = u32(nt) local strAt = nt.r:tell() for k = 1, n do if offs[k] < strSize then glyphs[k].name = nt.r:sub(strAt + offs[k], strSize - offs[k]):cstring() end end end end local registry = if type(props.CHARSET_REGISTRY) == "string" then string.upper(props.CHARSET_REGISTRY) else nil local cset = if props.CHARSET_ENCODING ~= nil then tostring(props.CHARSET_ENCODING) else nil local unicode = registry == nil or registry == "ISO10646" or (registry == "ISO8859" and cset == "1") local encList = table.create(nm, -1) for k = 1, nm do local enc = encodings[k] encList[k] = enc or -1 if unicode and enc then glyphs[k].codepoint = enc end end local pixelSize = if type(props.PIXEL_SIZE) == "number" then props.PIXEL_SIZE else (ascent or 0) + (descent or 0) local strike: Types.BitmapStrike = { size = pixelSize, ascent = if type(props.FONT_ASCENT) == "number" then props.FONT_ASCENT else ascent or 0, descent = if type(props.FONT_DESCENT) == "number" then props.FONT_DESCENT else descent or 0, glyphs = glyphs, default = default, } local weight = props.WEIGHT_NAME local style = if type(weight) == "string" and string.lower(weight) ~= "medium" and string.lower(weight) ~= "regular" then weight else "" if props.SLANT == "I" or props.SLANT == "O" then style = (if style ~= "" then style .. " " else "") .. "Italic" end return Bitmap.font(strike, { family = if type(props.FAMILY_NAME) == "string" then props.FAMILY_NAME else nil, style = if style ~= "" then style else "Regular", fullName = if type(props.FONT) == "string" then props.FONT else nil, }, { properties = props, encodings = encList, unicode = unicode, source = { format = "pcf", pad = pad, unit = unit, msbBit = msbBit, msbByte = bt.big, charset = if registry then registry .. "-" .. tostring(cset) else nil }, }), nil end return Pcf