-- font/type1 tests (and codecs/font/GlyphNames): Latin Modern PFB fonts and a PFA (t1ascii) against fontTools -- (outline control boxes, areas, advances of sample glyphs including flex, hint replacement, seac accents and -- ligatures; code points from glyph names) and FreeType's unhinted renders (PSNR); errors; the fuzz pass. -- Fixtures: tools/fixtures/type1.py. local T = require("@tests/T") local F = require("@rosetta") local Type1 = require("@rosetta/font/type1") local GlyphNames = require("@rosetta/codecs/font/GlyphNames") local Raster = require("@rosetta/codecs/image/Raster") local Bytes = require("@rosetta/core/Bytes") local Err = require("@rosetta/core/Err") local Fx = require("@tests/gen/type1") local t = T.suite("type1") t:eq(Type1.name, "type1", "name") t:eq(GlyphNames.unicode("Aacute"), 0xC1, "agl") t:eq(GlyphNames.unicode("uni20AC"), 0x20AC, "uniXXXX") t:eq(GlyphNames.unicode("u1F600"), 0x1F600, "uXXXXX") t:eq(GlyphNames.unicode("a.sc"), 0x61, "suffix") t:eq(GlyphNames.unicode("f_f_i"), 0x66, "ligature name") t:eq(GlyphNames.unicode("nosuchname"), nil, "unknown") t:eq(GlyphNames.STANDARD[65], "A", "StandardEncoding") -- shoelace area of the flattened outline (font units) local function area(cmds: any): number local polys = Raster.flatten(cmds, nil, 0.02) local a = 0 for _, p in polys do local n = #p local x0, y0 = p[n - 1], p[n] for i = 1, n - 1, 2 do a += x0 * p[i + 1] - p[i] * y0 x0, y0 = p[i], p[i + 1] end end return a / 2 end local function psnr(img: any, r: any): (number, number) -- union of both bitmaps, aligned on the pen position local ax0, ay0 = img.meta.left, -img.meta.top local bx0, by0 = r.left, -r.top local x0, y0 = math.min(ax0, bx0), math.min(ay0, by0) local x1, y1 = math.max(ax0 + img.width, bx0 + r.width), math.max(ay0 + img.height, by0 + r.height) local se, n, ink = 0, 0, 0 for y = y0, y1 - 1 do for x = x0, x1 - 1 do local a, b = 0, 0 local ix, iy = x - ax0, y - ay0 if ix >= 0 and iy >= 0 and ix < img.width and iy < img.height then a = buffer.readu8(img.pixels, iy * img.width + ix) end local jx, jy = x - bx0, y - by0 if jx >= 0 and jy >= 0 and jx < r.width and jy < r.height then b = string.byte(r.pixels, jy * r.width + jx + 1) end se += (a - b) * (a - b) ink += b n += 1 end end if n == 0 or se == 0 then return 99, ink end return 10 * math.log10(255 * 255 / (se / n)), ink end local worst = 99 local samples = {} for _, c in Fx.fonts do local font, err = Type1.decode(Bytes.reader(buffer.fromstring(c.data))) t:check(font, c.name .. ": " .. tostring(err)) if font then t:eq(font.unitsPerEm, c.upem, c.name .. ": units per em") t:eq(font.numGlyphs, c.numGlyphs, c.name .. ": glyph count") t:eq(font.info.family, c.family, c.name .. ": family") local byName = {} for k, n in font.meta.glyphNames do byName[n] = k - 1 end local maxErr = 0 for _, g in c.glyphs do local gi = byName[g.name] t:check(gi, c.name .. ": has " .. g.name) if gi then if g.cp then t:eq(font:glyphIndex(g.cp), gi, c.name .. ": cmap " .. g.name) end t:eq(font:advance(gi), g.advance, c.name .. ": advance " .. g.name) local cmds = font:outline(gi) :: any t:check(cmds, c.name .. ": outline " .. g.name) if g.bounds then local x0, y0, x1, y1 = math.huge, math.huge, -math.huge, -math.huge for _, cmd in cmds do for i = 1, #cmd, 2 do x0, x1 = math.min(x0, cmd[i]), math.max(x1, cmd[i]) y0, y1 = math.min(y0, cmd[i + 1]), math.max(y1, cmd[i + 1]) end end local ok = math.abs(x0 - g.bounds[1]) < 0.01 and math.abs(y0 - g.bounds[2]) < 0.01 and math.abs(x1 - g.bounds[3]) < 0.01 and math.abs(y1 - g.bounds[4]) < 0.01 t:check(ok, string.format("%s: %s control box %s %s %s %s vs %s %s %s %s", c.name, g.name, x0, y0, x1, y1, g.bounds[1], g.bounds[2], g.bounds[3], g.bounds[4])) end local a = math.abs(area(cmds)) local want = math.abs(g.area) local e = if want > 0 then math.abs(a - want) / want else a maxErr = math.max(maxErr, e) t:check(e < 0.002, string.format("%s: %s area %.1f vs %.1f", c.name, g.name, a, want)) end end for _, r in c.renders do local img = font:rasterize(font:glyphIndex(r.cp), r.px) :: any local p = psnr(img, r) worst = math.min(worst, p) t:check(p > 25, string.format("%s: U+%04X at %d px: PSNR %.1f dB vs FreeType", c.name, r.cp, r.px, p)) end t:note("%-22s %3d glyphs, area err %.5f", c.name, font.numGlyphs, maxErr) end t:eq(F.detect(c.data), "type1", c.name .. ": detect") table.insert(samples, string.sub(c.data, 1, 4000)) end t:note("worst PSNR vs FreeType %.1f dB", worst) local function code(s: string): string? local _, e = Type1.decode(Bytes.reader(buffer.fromstring(s))) return e and e.code end t:eq(code("%!PS-AdobeFont-1.0: X\n/FontName /X def\n"), Err.CORRUPT, "no eexec") t:eq(code("\128\1\255\255\0\0"), Err.TRUNCATED, "cut PFB segment") t:eq(code("%!PS-AdobeFont-1.0\ncurrentfile eexec\n00"), Err.TRUNCATED, "short eexec") t:fuzz("decode", function(b: buffer) local f = Type1.decode(Bytes.reader(b)) if f then for g = 0, math.min(f.numGlyphs - 1, 20) do f:outline(g) end end return f end, samples, { mutations = 20 }) t:done()