ws-arch2: font/hershey (JHF stroke fonts, ascii / id mapping, stroked rasterization; read)
This commit is contained in:
1 parent
0a3f8b3f5d
commit
375140ece2
5 files changed
+334
No files matched your search
@@ -530,6 +530,18 @@ local list: { Entry } = {
|
||||
return require("@formats/font/fon")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "hershey",
|
||||
category = "font",
|
||||
path = "font/hershey",
|
||||
extensions = { "jhf" },
|
||||
mime = { "application/x-hershey-font" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Raster" },
|
||||
caps = { decode = true, encode = false, strokes = true },
|
||||
load = function()
|
||||
return require("@formats/font/hershey")
|
||||
end,
|
||||
},
|
||||
{
|
||||
name = "pcf",
|
||||
category = "font",
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
--!strict
|
||||
-- font/hershey: Hershey vector fonts in the JHF text layout (James Hurt's distribution of the Hershey glyphs: per glyph
|
||||
-- a 5-column id, a 3-column vertex count, the left / right extents and the vertices as character pairs offset by "R",
|
||||
-- " R" lifting the pen; records continue over 72-column lines) as the standard Font: outlines are open stroke paths
|
||||
-- (moveTo / lineTo, no close; meta.strokes = true), rasterize strokes them through codecs/image/Raster
|
||||
-- (opts.strokeWidth, default 2 units). Units are Hershey units (unitsPerEm 32, y up, baseline at the Hershey y = 9 of
|
||||
-- the Roman fonts: ascent 21, descent 7 for capitals / descenders). Code points: opts.map = "ascii" (default: glyph i
|
||||
-- of the file is code point 32 + i, as the romans / futural / ... .jhf files are laid out) or "id" (the Hershey glyph
|
||||
-- number is the code point). Written from the JHF layout notes of the Hershey font distribution. Not supported:
|
||||
-- writing, the Hershey "occidental" index files (.hmp), filled rendering.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@formats/core/Err")
|
||||
local Bytes = require("@formats/core/Bytes")
|
||||
local Types = require("@formats/Types")
|
||||
local Raster = require("@formats/codecs/image/Raster")
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ contract
|
||||
local Hershey = {
|
||||
name = "hershey",
|
||||
category = "font",
|
||||
extensions = { "jhf" },
|
||||
mime = { "application/x-hershey-font" },
|
||||
deps = { "core/Err", "core/Bytes", "Types", "codecs/image/Raster" },
|
||||
caps = { decode = true, encode = false, strokes = true },
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
map: ("ascii" | "id")?, -- how glyphs map to code points (default "ascii")
|
||||
strokeWidth: number?, -- stroke width in font units for rasterize (default 2)
|
||||
maxGlyphs: number?, -- default 65536
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
local BASELINE = 9 -- the Hershey y of the Roman fonts' baseline (y grows downwards)
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
-- the first record: 5-digit id, 3-digit count, then coordinate characters in " " .. "~" (the pen-up pair " R")
|
||||
local function header(line: string): (number?, number?)
|
||||
if #line < 10 then
|
||||
return nil, nil
|
||||
end
|
||||
local id = tonumber((string.gsub(string.sub(line, 1, 5), "^%s+", "")))
|
||||
local n = tonumber((string.gsub(string.sub(line, 6, 8), "^%s+", "")))
|
||||
if not id or not n or n < 1 or string.find(string.sub(line, 1, 8), "[^%d ]") then
|
||||
return nil, nil
|
||||
end
|
||||
return id, n
|
||||
end
|
||||
|
||||
function Hershey.sniff(r: Bytes.Reader): number
|
||||
local head = r:peek(256)
|
||||
local line = string.match(head, "^([^\r\n]*)")
|
||||
local id, n = header(line or "")
|
||||
if not id or not n then
|
||||
return 0
|
||||
end
|
||||
local body = string.sub(line :: string, 9)
|
||||
if string.find(body, "[^ -~]") then
|
||||
return 0
|
||||
end
|
||||
return if #body == math.min(2 * n, 64) or #body == 2 * n then 0.7 else 0.3
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
type Glyph = { id: number, left: number, right: number, cmds: { any } }
|
||||
|
||||
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._glyphs[g + 1]
|
||||
return if gl then gl.right - gl.left else 0
|
||||
end
|
||||
|
||||
function Face.kern(_self: any, _a: number, _b: number): number
|
||||
return 0
|
||||
end
|
||||
|
||||
function Face.outline(self: any, g: number): { any }?
|
||||
local gl = self._glyphs[g + 1]
|
||||
if not gl then
|
||||
return nil
|
||||
end
|
||||
local out = table.create(#gl.cmds)
|
||||
for i, c in gl.cmds do
|
||||
out[i] = table.clone(c)
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
-- rasterize(g, px) -> alpha8 Image of the stroked glyph (img.meta.left / top / advance in pixels)
|
||||
function Face.rasterize(self: any, g: number, px: number): Types.Image?
|
||||
local gl = self._glyphs[g + 1]
|
||||
if not gl or type(px) ~= "number" or not (px > 0) or px > 4096 then
|
||||
return nil
|
||||
end
|
||||
local s = px / self.unitsPerEm
|
||||
local stroke = Raster.stroke(gl.cmds, { width = self.meta.strokeWidth, join = "round", cap = "round" })
|
||||
local m = Raster.mask(stroke, { transform = { s, 0, 0, -s, 0, 0 }, maxPixels = 2 ^ 24 })
|
||||
local img
|
||||
if m then
|
||||
img = Types.Image.new(m.width, m.height, "alpha8")
|
||||
buffer.copy(img.pixels, 0, m.coverage)
|
||||
img.meta.left = m.x
|
||||
img.meta.top = -m.y
|
||||
else
|
||||
img = Types.Image.new(0, 0, "alpha8")
|
||||
img.meta.left, img.meta.top = 0, 0
|
||||
end
|
||||
img.meta.advance = (gl.right - gl.left) * s
|
||||
img.meta.glyph = g
|
||||
return img
|
||||
end
|
||||
|
||||
function Hershey.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local o: DecodeOptions = opts or {}
|
||||
local maxGlyphs = o.maxGlyphs or 65536
|
||||
local text = buffer.readstring(r.buf, r.base, r.limit - r.base)
|
||||
local lines = string.split((string.gsub(text, "\r", "")), "\n")
|
||||
local glyphs: { Glyph } = {}
|
||||
local i = 1
|
||||
while i <= #lines do
|
||||
local line = lines[i]
|
||||
i += 1
|
||||
if string.match(line, "^%s*$") then
|
||||
continue
|
||||
end
|
||||
local id, n = header(line)
|
||||
if not id or not n then
|
||||
return nil, Err.new(Err.CORRUPT, "bad record header on line " .. (i - 1))
|
||||
end
|
||||
local data = string.sub(line, 9)
|
||||
while #data < 2 * n and i <= #lines do
|
||||
data ..= lines[i]
|
||||
i += 1
|
||||
end
|
||||
if #data < 2 * n then
|
||||
return nil, Err.new(Err.TRUNCATED, "glyph " .. id .. " has fewer vertices than its count")
|
||||
end
|
||||
if #glyphs >= maxGlyphs then
|
||||
return nil, Err.new(Err.LIMIT, "more than maxGlyphs glyphs")
|
||||
end
|
||||
local left = string.byte(data, 1) - 82
|
||||
local right = string.byte(data, 2) - 82
|
||||
local cmds: { any } = {}
|
||||
local pen = false
|
||||
for k = 1, n - 1 do
|
||||
local a, b = string.byte(data, 2 * k + 1), string.byte(data, 2 * k + 2)
|
||||
if a == 32 and b == 82 then
|
||||
pen = false
|
||||
else
|
||||
local x, y = a - 82 - left, BASELINE - (b - 82)
|
||||
local cmd: any = { op = if pen then "lineTo" else "moveTo" }
|
||||
cmd[1], cmd[2] = x, y
|
||||
table.insert(cmds, cmd)
|
||||
pen = true
|
||||
end
|
||||
end
|
||||
table.insert(glyphs, { id = id, left = left, right = right, cmds = cmds })
|
||||
end
|
||||
if #glyphs == 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "no glyphs")
|
||||
end
|
||||
local index: { [number]: number } = {}
|
||||
local byId = o.map == "id"
|
||||
for k, g in glyphs do
|
||||
local cp = if byId then g.id else 31 + k
|
||||
if index[cp] == nil then
|
||||
index[cp] = k - 1
|
||||
end
|
||||
end
|
||||
local font = setmetatable({
|
||||
kind = "font",
|
||||
info = { family = "Hershey", style = "Regular" },
|
||||
unitsPerEm = 32,
|
||||
ascent = 21,
|
||||
descent = -7,
|
||||
lineGap = 4,
|
||||
numGlyphs = #glyphs,
|
||||
meta = {
|
||||
strokes = true,
|
||||
strokeWidth = o.strokeWidth or 2,
|
||||
ids = table.create(#glyphs, 0),
|
||||
source = { format = "hershey", map = if byId then "id" else "ascii" },
|
||||
},
|
||||
_glyphs = glyphs,
|
||||
_index = index,
|
||||
}, Face)
|
||||
for k, g in glyphs do
|
||||
font.meta.ids[k] = g.id
|
||||
end
|
||||
return font, nil
|
||||
end
|
||||
|
||||
return Hershey
|
||||
Reference in new issue
Block a user