font formats: comments are specs only
This commit is contained in:
1 parent
0452f26eef
commit
d91dc6c597
14 files changed
+64
-167
No files matched your search
@@ -1,13 +1,7 @@
|
||||
--!strict
|
||||
-- font/bdf: Glyph Bitmap Distribution Format 2.1 (and 2.2's METRICSSET 0) bitmap fonts, read and written, as the
|
||||
-- standard Font with a bitmap strike (codecs/font/Bitmap): FONT / SIZE / FONTBOUNDINGBOX, the properties (all kept in
|
||||
-- meta.properties; FONT_ASCENT / FONT_DESCENT / PIXEL_SIZE / DEFAULT_CHAR / FAMILY_NAME / WEIGHT_NAME / SLANT used),
|
||||
-- glyphs with STARTCHAR names, ENCODING (-1 for unencoded, "-1 n" non-standard codes), DWIDTH / BBX (falling back to
|
||||
-- the font's) and hex BITMAP rows. Code points: ENCODING is the Unicode code point for CHARSET_REGISTRY ISO10646, for
|
||||
-- ISO8859-1 and for fonts without a registry; fonts in other charsets keep their glyphs unmapped (glyph names and
|
||||
-- meta.encodings keep the codes). Writing takes any Font (Bitmap.strike: a bitmap font as it is, an outline font
|
||||
-- rasterized at opts.px) and writes Unicode (ISO10646-1) BDF 2.1. Written from Adobe's BDF 2.1 specification.
|
||||
-- Not supported: vertical metrics (SWIDTH1 / DWIDTH1 / VVECTOR are read and ignored).
|
||||
-- font/bdf: Glyph Bitmap Distribution Format 2.1 (and 2.2's METRICSSET 0), read and written, from Adobe's BDF 2.1
|
||||
-- spec. ENCODING is Unicode for ISO10646, ISO8859-1 and registry-less fonts; other charsets stay unmapped. Writes
|
||||
-- Unicode (ISO10646-1) BDF 2.1 from any Font. Not supported: vertical metrics (SWIDTH1 / DWIDTH1 / VVECTOR ignored).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -27,9 +21,9 @@ local Bdf = {
|
||||
|
||||
export type DecodeOptions = { maxGlyphs: number? } -- default 1114112
|
||||
export type EncodeOptions = {
|
||||
px: number?, -- pixel size for outline fonts (default 16)
|
||||
chars: (string | { number })?, -- code points for outline fonts (default printable ASCII)
|
||||
name: string?, -- the FONT (XLFD) name (default built from the font's family / style)
|
||||
px: number?, -- default 16
|
||||
chars: (string | { number })?, -- default printable ASCII
|
||||
name: string?, -- XLFD; default built from the family / style
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -223,7 +217,6 @@ function Bdf.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local bold = string.find(string.lower(tostring(info.style or "")), "bold") ~= nil
|
||||
local italic = string.find(string.lower(tostring(info.style or "")), "italic") ~= nil
|
||||
local px = strike.size
|
||||
-- the font bounding box
|
||||
local x0, y0, x1, y1 = math.huge, math.huge, -math.huge, -math.huge
|
||||
for _, g in strike.glyphs do
|
||||
if g.width > 0 and g.height > 0 then
|
||||
@@ -244,7 +237,6 @@ function Bdf.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
string.format("SIZE %d 75 75", px),
|
||||
string.format("FONTBOUNDINGBOX %d %d %d %d", x1 - x0, y1 - y0, x0, y0),
|
||||
}
|
||||
-- the default glyph: the strike's default, else glyph 0
|
||||
local defaultIndex = (strike.default or 0) + 1
|
||||
local props: { { any } } = {
|
||||
{ "FAMILY_NAME", quote(family) },
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
--!strict
|
||||
-- font/bmfont/Parse: the three AngelCode BMFont descriptor encodings (text, XML, binary version 3) to and from one
|
||||
-- description table { info, common, pages, chars, kernings }. Written from the BMFont documentation
|
||||
-- (angelcode.com/products/bmfont/doc/file_format.html). The XML reader only scans elements and attributes (the
|
||||
-- descriptor has no nesting that matters, no text content).
|
||||
--
|
||||
-- Never throws on bad input: parse functions return nil, message.
|
||||
-- font/bmfont/Parse: BMFont text / XML / binary v3 descriptors (angelcode.com/products/bmfont/doc/file_format.html).
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
|
||||
local Parse = {}
|
||||
@@ -24,12 +19,12 @@ export type Char = {
|
||||
export type Desc = {
|
||||
info: { [string]: any }, -- face, size, bold, italic, charset, unicode, stretchH, smooth, aa, padding {4}, spacing {2}, outline
|
||||
common: { [string]: any }, -- lineHeight, base, scaleW, scaleH, pages, packed, alphaChnl, redChnl, greenChnl, blueChnl
|
||||
pages: { string }, -- file names, by page id (1-based: page id 0 is pages[1])
|
||||
pages: { string }, -- by page id: page id 0 is pages[1]
|
||||
chars: { Char },
|
||||
kernings: { { number } }, -- { first, second, amount }
|
||||
}
|
||||
|
||||
local LIMIT = 1000000 -- chars / kernings
|
||||
local LIMIT = 1000000
|
||||
|
||||
local INFO_NUM = { "size", "bold", "italic", "unicode", "stretchH", "smooth", "aa", "outline" }
|
||||
local COMMON_NUM = { "lineHeight", "base", "scaleW", "scaleH", "pages", "packed", "alphaChnl", "redChnl", "greenChnl", "blueChnl" }
|
||||
@@ -54,7 +49,6 @@ local function new(): Desc
|
||||
return { info = {}, common = {}, pages = {}, chars = {}, kernings = {} }
|
||||
end
|
||||
|
||||
-- one tag's attributes (already split into key -> string) into the description
|
||||
local function apply(d: Desc, tag: string, a: { [string]: string }): string?
|
||||
if tag == "info" then
|
||||
d.info.face = a.face or ""
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
--!strict
|
||||
-- font/bmfont: AngelCode BMFont descriptors (.fnt) in all three encodings: text, XML and binary (version 3), read and
|
||||
-- written. Decodes to the standard Font (a bitmap font: glyphIndex / advance / kern work, outline is nil, rasterize
|
||||
-- crops the glyph from its page as alpha8). Pages are image files named by the descriptor (meta.pageFiles); they are
|
||||
-- loaded lazily through font:loadPages(loader) or the loader the api attaches (opts.loadPage(file) -> Image, or the
|
||||
-- files next to the .fnt on hosts with a file system). Bmfont.bake(font, opts) renders any outline Font (ttf / otf /
|
||||
-- woff) into a packed bitmap font with its pages. Written from the BMFont documentation.
|
||||
-- Not supported: packed multi-channel glyphs (chnl other than one channel or all: read as alpha).
|
||||
-- font/bmfont: AngelCode BMFont descriptors (.fnt: text, XML, binary version 3), read and written, from the BMFont
|
||||
-- documentation. Pages are image files (meta.pageFiles), loaded lazily; Bmfont.bake renders an outline Font to pages.
|
||||
-- Not supported: packed multi-channel glyphs (read as alpha).
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -24,25 +20,24 @@ local Bmfont = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
pages: { Types.Image }?, -- the page images, if the caller has them already (page id 0 first)
|
||||
loadPage: ((file: string, page: number) -> any)?, -- loader for lazy pages: file -> Image (the api passes one)
|
||||
pages: { Types.Image }?, -- page id 0 first
|
||||
loadPage: ((file: string, page: number) -> any)?,
|
||||
}
|
||||
export type EncodeOptions = {
|
||||
variant: ("text" | "xml" | "binary")?, -- default: the variant it was read from, else "text"
|
||||
px: number?, -- fonts without a BMFont description are baked first (see bake)
|
||||
px: number?,
|
||||
chars: (string | { number })?,
|
||||
file: string?, -- baking: the page file name prefix (default: opts.name without its extension, else "font")
|
||||
name: string?, -- the destination's file name (set by the api)
|
||||
files: { [string]: any }?, -- the page images are written here, encoded by opts.encodeImage (the api passes
|
||||
-- both): files[page file name] = PNG bytes, for the baked pages or a BMFont's loaded pages
|
||||
file: string?, -- default: opts.name without its extension, else "font"
|
||||
name: string?,
|
||||
files: { [string]: any }?,
|
||||
encodeImage: ((img: any, format: string) -> (buffer?, any))?,
|
||||
}
|
||||
export type BakeOptions = {
|
||||
px: number?, -- pixels per em (default 32)
|
||||
chars: (string | { number })?, -- code points to include (default printable ASCII 32..126)
|
||||
pageSize: number?, -- page width = height (default: the smallest power of two that fits, up to 4096)
|
||||
padding: number?, -- empty pixels around each glyph (default 1)
|
||||
file: string?, -- page file name prefix (default "font"): pages are <file>_<n>.png
|
||||
px: number?, -- default 32
|
||||
chars: (string | { number })?, -- default printable ASCII 32..126
|
||||
pageSize: number?, -- default: the smallest power of two that fits, up to 4096
|
||||
padding: number?, -- default 1
|
||||
file: string?, -- default "font"; pages are <file>_<n>.png
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -61,7 +56,6 @@ function Bmfont.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the Font value
|
||||
local Face = {}
|
||||
Face.__index = Face
|
||||
|
||||
@@ -80,10 +74,9 @@ function Face.kern(self: any, a: number, b: number): number
|
||||
return self._kern[ca.id * 2097152 + cb.id] or 0
|
||||
end
|
||||
function Face.outline(_self: any, _g: number): { any }?
|
||||
return nil -- bitmap font
|
||||
return nil
|
||||
end
|
||||
|
||||
-- loadPages(loader?) -> true | nil, Err: loader(file, pageIndex) -> Image (rgba8) | nil
|
||||
function Face.loadPages(self: any, loader: any?): (boolean?, Err.Err?)
|
||||
local load = loader or self.meta.pageLoader
|
||||
if type(load) ~= "function" then
|
||||
@@ -103,8 +96,6 @@ end
|
||||
|
||||
local CHANNEL = { [1] = 2, [2] = 1, [4] = 0, [8] = 3 } -- chnl bit -> byte in rgba8
|
||||
|
||||
-- rasterize(g, px?) -> alpha8 Image cropped from the glyph's page (px is ignored: bitmap fonts have one size;
|
||||
-- meta.scale = px / size tells the caller how much to scale). nil when the pages are not loaded / available.
|
||||
function Face.rasterize(self: any, g: number, px: number?): Types.Image?
|
||||
local c = self._chars[g]
|
||||
if not c then
|
||||
@@ -141,7 +132,6 @@ function Face.rasterize(self: any, g: number, px: number?): Types.Image?
|
||||
return img
|
||||
end
|
||||
|
||||
-- the Font value of a description
|
||||
local function face(d: Parse.Desc, variant: string): any
|
||||
local f: any = setmetatable({
|
||||
kind = "font",
|
||||
@@ -244,7 +234,6 @@ function Bmfont.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
local d = type(font.meta) == "table" and font.meta.bmfont or nil
|
||||
local pages = font.pages
|
||||
if d == nil then
|
||||
-- page names: opts.file, else the destination's base name (the api passes opts.name), else "font"
|
||||
local file = opts and opts.file
|
||||
if file == nil and opts and type(opts.name) == "string" then
|
||||
file = string.match(opts.name, "([^/\\]-)%.[^./\\]*$") or string.match(opts.name, "([^/\\]+)$")
|
||||
@@ -262,7 +251,6 @@ function Bmfont.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if not validDesc(d) then
|
||||
return nil, Err.new(Err.ARG, "bad BMFont description in font.meta.bmfont")
|
||||
end
|
||||
-- the page images into opts.files, under the names the descriptor gives them
|
||||
local files = opts and opts.files
|
||||
local encodeImage = opts and opts.encodeImage
|
||||
if type(files) == "table" and type(encodeImage) == "function" and type(pages) == "table" then
|
||||
@@ -310,9 +298,6 @@ local function codepoints(chars: any): { number }
|
||||
return out
|
||||
end
|
||||
|
||||
-- bake(font, opts?) -> a BMFont Font (pages: rgba8 Images, white with the coverage as alpha) | nil, Err. Glyphs are
|
||||
-- rasterized with font:rasterize and packed in rows (tallest first); kerning pairs between the baked characters are
|
||||
-- kept (rounded to pixels).
|
||||
function Bmfont.bake(font: any, opts: BakeOptions?): (any, Err.Err?)
|
||||
if type(font) ~= "table" or type(font.rasterize) ~= "function" or type(font.glyphIndex) ~= "function" then
|
||||
return nil, Err.new(Err.ARG, "bake: expected an outline Font")
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
--!strict
|
||||
-- font/eot: Embedded OpenType (.eot; versions 0x00010000, 0x00020001 and 0x00020002): the EOT header (PANOSE, charset,
|
||||
-- weight, fsType, Unicode / code page ranges, family / style / version / full names, the root string, EUDC data) and
|
||||
-- the font data inside, XOR-0x50 obfuscated or not, read as the standard Font through codecs/font/TrueType (meta.eot
|
||||
-- holds the header fields, meta.fontData the TrueType bytes). Writes EOT 2.1 around TrueType bytes (a buffer / string,
|
||||
-- or a Font read from an EOT), filling the header from the font's OS/2, head and name tables. Written from the W3C
|
||||
-- EOT submission (2008). Not supported: MicroType Express compressed fonts (flag 0x4: Err.UNSUPPORTED), signatures.
|
||||
-- font/eot: Embedded OpenType (versions 0x00010000, 0x00020001, 0x00020002; W3C EOT submission, 2008): the header
|
||||
-- and the font data (XOR-0x50 obfuscated or not) through codecs/font/TrueType; writes EOT 2.1 around TrueType bytes.
|
||||
-- Not supported: MicroType Express compression (flag 0x4: Err.UNSUPPORTED), signatures.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -24,8 +21,8 @@ local Eot = {
|
||||
|
||||
export type DecodeOptions = {}
|
||||
export type EncodeOptions = {
|
||||
xor: boolean?, -- obfuscate the font data with XOR 0x50 (default false)
|
||||
rootString: string?, -- the URLs allowed to use the font (default "")
|
||||
xor: boolean?, -- XOR 0x50; default false
|
||||
rootString: string?, -- default ""
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
|
||||
@@ -1,10 +1,6 @@
|
||||
--!strict
|
||||
-- font/fon: Windows raster fonts: bare .fnt resources (versions 2.0 and 3.0: the header, the character table and the
|
||||
-- column-major glyph bitmaps) and .fon files holding them as RT_FONT resources of a 16-bit NE executable (or a PE
|
||||
-- one), as the standard Font with a bitmap strike (codecs/font/Bitmap). A .fon usually holds several sizes: the first
|
||||
-- (or the one closest to opts.size pixels) is the Font, every strike's Font is in meta.strikes. Code points: ANSI
|
||||
-- charset fonts map through Windows-1252, other charsets keep their byte codes below 0x80 only (meta.charset).
|
||||
-- Written from Microsoft's "Windows 3.0 font file format" (KB65123) and the NE / PE resource table layouts.
|
||||
-- font/fon: Windows raster fonts: .fnt resources (versions 2.0 / 3.0) and .fon NE / PE executables holding them as
|
||||
-- RT_FONT, from Microsoft's KB65123 and the NE / PE resource layouts; ANSI charset maps through Windows-1252.
|
||||
-- Not supported: vector FNT (Err.UNSUPPORTED), writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
@@ -24,7 +20,7 @@ local Fon = {
|
||||
caps = { decode = true, encode = false, bitmap = true, variants = { "fnt2", "fnt3", "ne", "pe" } },
|
||||
}
|
||||
|
||||
export type DecodeOptions = { size: number? } -- pick the strike closest to this pixel height (default: the first)
|
||||
export type DecodeOptions = { size: number? } -- default: the first strike
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
local function isFnt(h: string): boolean
|
||||
@@ -50,7 +46,6 @@ function Fon.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- one FNT resource -> Font
|
||||
local function fnt(r: Bytes.Reader): (any, Err.Err?)
|
||||
local version = r:u16le()
|
||||
local total = r:u32le()
|
||||
@@ -145,7 +140,6 @@ local function fnt(r: Bytes.Reader): (any, Err.Err?)
|
||||
}), nil
|
||||
end
|
||||
|
||||
-- the RT_FONT resources of an NE executable: { { offset, length } }
|
||||
local function neFonts(r: Bytes.Reader, ne: number): ({ { number } }?, string?)
|
||||
local rt = r:sub(ne + 0x24, 2):u16le()
|
||||
local t = r:sub(ne + rt)
|
||||
@@ -175,7 +169,7 @@ local function neFonts(r: Bytes.Reader, ne: number): ({ { number } }?, string?)
|
||||
return out, nil
|
||||
end
|
||||
|
||||
-- the RT_FONT (8) resources of a PE executable: { { offset, length } }
|
||||
-- RT_FONT: resource type 8
|
||||
local function peFonts(r: Bytes.Reader, pe: number): ({ { number } }?, string?)
|
||||
local h = r:sub(pe + 4, 20)
|
||||
h:skip(2)
|
||||
@@ -190,7 +184,6 @@ local function peFonts(r: Bytes.Reader, pe: number): ({ { number } }?, string?)
|
||||
if opt.err or rsrcRva == 0 then
|
||||
return {}, nil
|
||||
end
|
||||
-- the section holding the resources
|
||||
local secs = r:sub(pe + 24 + optSize, nsec * 40)
|
||||
local base, rva0 = -1, 0
|
||||
for _ = 1, nsec do
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
--!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.
|
||||
-- font/hershey: Hershey vector fonts, JHF layout (5-column id, 3-column vertex count, extents, vertices as character
|
||||
-- pairs offset by "R", " R" lifts the pen; records continue over 72-column lines). Units: unitsPerEm 32, y up.
|
||||
-- Not supported: writing, .hmp index files, filled rendering.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -26,8 +20,8 @@ local Hershey = {
|
||||
}
|
||||
|
||||
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)
|
||||
map: ("ascii" | "id")?, -- default "ascii"
|
||||
strokeWidth: number?, -- font units; default 2
|
||||
maxGlyphs: number?, -- default 65536
|
||||
}
|
||||
|
||||
@@ -93,7 +87,6 @@ function Face.outline(self: any, g: number): { any }?
|
||||
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
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
--!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.
|
||||
-- font/pcf: X11 Portable Compiled Format bitmap fonts, from the PCF description in libXfont / FreeType's pcf driver
|
||||
-- docs (no code copied); code points as font/bdf. Not supported: writing, INK_METRICS.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -38,7 +33,6 @@ 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?
|
||||
@@ -91,7 +85,6 @@ function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
if r.err then
|
||||
return nil, r.err
|
||||
end
|
||||
-- properties
|
||||
local props: { [string]: any } = {}
|
||||
local pt = open(r, tables[PROPERTIES])
|
||||
if pt then
|
||||
@@ -122,14 +115,12 @@ function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
return nil, Err.new(Err.TRUNCATED, "properties table")
|
||||
end
|
||||
end
|
||||
-- accelerators: ascent / descent
|
||||
local ascent: number?, descent: number? = 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")
|
||||
@@ -152,7 +143,6 @@ function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
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")
|
||||
@@ -202,7 +192,6 @@ function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
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])
|
||||
@@ -233,7 +222,6 @@ function Pcf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
end
|
||||
end
|
||||
-- glyph names
|
||||
local nt = open(r, tables[GLYPH_NAMES])
|
||||
if nt then
|
||||
local n = u32(nt)
|
||||
|
||||
@@ -1,14 +1,7 @@
|
||||
--!strict
|
||||
-- font/psf: Linux console fonts, PC Screen Font versions 1 (magic 36 04: 8 pixels wide, 256 or 512 glyphs, UCS-2
|
||||
-- Unicode table with FFFF / FFFE) and 2 (magic 72 B5 4A 86: any width, UTF-8 Unicode table with FF / FE), read and
|
||||
-- written, as the standard Font with a bitmap strike (codecs/font/Bitmap). Every code point of a glyph's table entry
|
||||
-- maps to it (strike.map; combining sequences after FE / FFFE are kept in meta.sequences); fonts without a table map
|
||||
-- glyph i to code point i (meta.unicode = false). Console cells have no baseline: the strike's ascent is the cell
|
||||
-- height (descent 0) and every glyph fills its cell. .psf.gz / .psfu.gz load through the api's gzip unwrap. Writing:
|
||||
-- PSF 2 by default (opts.version = 1 for PSF 1: 8-pixel cells, 256 or 512 glyphs) from any Font through
|
||||
-- Bitmap.strike, glyphs placed in a cell of the strike's widest advance and ascent + descent; a PSF read before keeps
|
||||
-- its Unicode table (meta.unicodeTable) and its table / no-table choice. Written from the kbd
|
||||
-- project's psf documentation (Andries Brouwer).
|
||||
-- font/psf: Linux console fonts (kbd project's psf docs, Andries Brouwer): PSF 1 (magic 36 04, UCS-2 table, FFFF /
|
||||
-- FFFE) and PSF 2 (magic 72 B5 4A 86, UTF-8 table, FF / FE), read and written (PSF 2 by default); the ascent is the
|
||||
-- cell height.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -28,10 +21,10 @@ local Psf = {
|
||||
|
||||
export type DecodeOptions = { maxGlyphs: number? } -- default 65536
|
||||
export type EncodeOptions = {
|
||||
version: number?, -- 1 or 2 (default 2)
|
||||
px: number?, -- pixel size for outline fonts (default 16)
|
||||
chars: (string | { number })?, -- code points for outline fonts (default printable ASCII)
|
||||
unicode: boolean?, -- write the Unicode table (default true)
|
||||
version: number?, -- 1 or 2; default 2
|
||||
px: number?, -- default 16
|
||||
chars: (string | { number })?, -- default printable ASCII
|
||||
unicode: boolean?, -- default true
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -103,7 +96,7 @@ function Psf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
local map: { [number]: number } = {}
|
||||
local sequences = {}
|
||||
local unicodeTable: { { any } } = {} -- per glyph: code points and { sequence } tables, in file order
|
||||
local unicodeTable: { { any } } = {}
|
||||
if hasTable then
|
||||
local t = r:sub(glyphAt + count * size)
|
||||
for i = 0, count - 1 do
|
||||
@@ -141,7 +134,6 @@ function Psf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
inSeq = true
|
||||
continue
|
||||
end
|
||||
-- UTF-8
|
||||
local n = if b < 0x80 then 0 elseif b >= 0xF0 then 3 elseif b >= 0xE0 then 2 elseif b >= 0xC0 then 1 else -1
|
||||
if n < 0 then
|
||||
return nil, Err.new(Err.CORRUPT, "bad UTF-8 in the Unicode table")
|
||||
@@ -208,7 +200,6 @@ function Psf.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
if n == 0 then
|
||||
return nil, Err.new(Err.ARG, "no glyphs")
|
||||
end
|
||||
-- the cell
|
||||
local width = 0
|
||||
for _, g in glyphs do
|
||||
width = math.max(width, g.advance, g.left + g.width)
|
||||
@@ -241,7 +232,6 @@ function Psf.encode(font: any, opts: EncodeOptions?): (buffer?, Err.Err?)
|
||||
end
|
||||
buffer.copy(cells, (i - 1) * size, Bitmap.pack(cell, width, height, stride))
|
||||
end
|
||||
-- the Unicode table: each glyph's code point and the extra mappings onto it
|
||||
local cps: { { number } } = {}
|
||||
for i = 1, count do
|
||||
cps[i] = {}
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
--!strict
|
||||
-- font/ttf: TrueType and OpenType fonts (.ttf, .otf with CFF outlines) and collections (.ttc / .otc), read into the
|
||||
-- standard Font (Types.Font) by codecs/font/TrueType: metrics, names, cmap, outlines (glyf or CFF), kerning ('kern' table,
|
||||
-- GPOS pair kerning) and rasterize(glyph, px) through codecs/image/Raster. Ported from stb_truetype 1.26 (public domain /
|
||||
-- MIT) via imgui-luau's imstb_truetype.lua; see codecs/font/TrueType. opts.index picks the font of a collection.
|
||||
-- Not supported: hinting, variable fonts, bitmap / colour glyphs, GSUB, writing.
|
||||
-- font/ttf: TrueType / OpenType (glyf or CFF) fonts and collections via codecs/font/TrueType, ported from stb_truetype
|
||||
-- 1.26 (public domain / MIT) via imgui-luau's imstb_truetype.lua. Not supported: hinting, variable fonts, bitmap /
|
||||
-- colour glyphs, GSUB, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -21,7 +19,7 @@ local Ttf = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
index: number?, -- the font of a collection (default 0)
|
||||
index: number?, -- default 0
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -44,7 +42,6 @@ function Ttf.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ extras
|
||||
-- count(r) -> the number of fonts in the file (1 unless a collection)
|
||||
function Ttf.count(r: Bytes.Reader): number
|
||||
return TrueType.count(r.buf, r.base)
|
||||
end
|
||||
|
||||
@@ -1,15 +1,8 @@
|
||||
--!strict
|
||||
-- font/type1/Charstring: the Type 1 charstring interpreter (Adobe Type 1 Font Format, chapter 6 and 8): decrypted
|
||||
-- charstrings to path commands (moveTo / lineTo / cubicTo / close, font units, y up) plus the advance and side
|
||||
-- bearing. hsbw / sbw, the r/h/v moveto, lineto and curveto operators, closepath, callsubr / return, div,
|
||||
-- callothersubr / pop with the standard OtherSubrs (0..2: flex, emitted as its two curves; 3: hint replacement;
|
||||
-- others pass their arguments back), setcurrentpoint, seac (the base and accent glyphs from StandardEncoding codes,
|
||||
-- through a callback), endchar; hints (hstem / vstem / hstem3 / vstem3 / dotsection) are ignored. New code from the
|
||||
-- specification. Never throws: malformed charstrings return nil, message (an operation budget stops subroutine loops).
|
||||
-- font/type1/Charstring: Type 1 charstring interpreter (Adobe Type 1 Font Format, chapters 6 and 8); hints ignored.
|
||||
local Charstring = {}
|
||||
|
||||
export type Result = { cmds: { any }, advance: number, sbx: number }
|
||||
-- fonts: subrs (decrypted subroutines), lenIV handled by the caller; seac(code) -> the base / accent Result
|
||||
export type Context = { subrs: { buffer }, seac: ((code: number) -> Result?)? }
|
||||
|
||||
local readu8 = buffer.readu8
|
||||
@@ -18,7 +11,7 @@ function Charstring.run(cs: buffer, ctx: Context): (Result?, string?)
|
||||
local cmds: { any } = {}
|
||||
local stack: { number } = {}
|
||||
local sp = 0
|
||||
local ps: { number } = {} -- the PostScript stack callothersubr / pop use
|
||||
local ps: { number } = {}
|
||||
local x, y = 0, 0
|
||||
local advance, sbx = 0, 0
|
||||
local open = false
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
--!strict
|
||||
-- font/type1: PostScript Type 1 fonts, PFB (segments 80 01 / 80 02 / 80 03) and PFA (hex eexec section), as the
|
||||
-- standard Font: the cleartext dictionary (FontName, FamilyName, FullName, Weight, ItalicAngle, FontMatrix, FontBBox,
|
||||
-- Encoding: StandardEncoding or the font's own array), eexec decryption (r = 55665), the Private dictionary (lenIV,
|
||||
-- Subrs, CharStrings with RD / -| binary strings, charstring decryption r = 4330) and the Type 1 charstring
|
||||
-- interpreter (font/type1/Charstring: flex, hint replacement, seac) to outlines; code points from the encoding's glyph
|
||||
-- names through codecs/font/GlyphNames (AGLFN, uniXXXX), plus every other glyph name the AGL knows; rasterize through
|
||||
-- codecs/image/Raster. Written from Adobe's "Adobe Type 1 Font Format" (1990). Not supported: hinting, Multiple
|
||||
-- Master and CID-keyed (Type 9 / 11) fonts, writing.
|
||||
-- font/type1: PostScript Type 1 fonts, PFB (segments 80 01 / 80 02 / 80 03) and PFA, from Adobe's "Adobe Type 1 Font
|
||||
-- Format" (1990): eexec (r = 55665) and charstring (r = 4330) decryption, code points via codecs/font/GlyphNames.
|
||||
-- Not supported: hinting, Multiple Master and CID-keyed (Type 9 / 11) fonts, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -41,7 +36,6 @@ function Type1.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- the cleartext and the (still encrypted) binary eexec section of a PFB or PFA
|
||||
local function sections(r: Bytes.Reader): (string?, buffer?, string?)
|
||||
local b = r.buf
|
||||
local n = r:size()
|
||||
@@ -204,7 +198,6 @@ function Type1.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
local priv = decrypt(enc, 0, buffer.len(enc), 55665, 4)
|
||||
local ptext = buffer.tostring(priv)
|
||||
local lenIV = tonumber(string.match(ptext, "/lenIV%s+(%-?%d+)") or "4") or 4
|
||||
-- Subrs
|
||||
local subrs: { buffer } = {}
|
||||
local function charstring(p: number, len: number): buffer?
|
||||
local raw = binaryAt(priv, p, len)
|
||||
@@ -241,7 +234,6 @@ function Type1.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
p = e + n
|
||||
end
|
||||
end
|
||||
-- CharStrings
|
||||
local names: { string } = {}
|
||||
local charstrings: { buffer } = {}
|
||||
local byName: { [string]: number } = {}
|
||||
@@ -252,7 +244,6 @@ function Type1.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
break
|
||||
end
|
||||
local e = e0 :: number
|
||||
-- stop at the end of the dictionary
|
||||
if string.find(string.sub(ptext, p, s), "%send%s") then
|
||||
break
|
||||
end
|
||||
@@ -281,7 +272,6 @@ function Type1.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
charstrings[1], charstrings[nd + 1] = charstrings[nd + 1], charstrings[1]
|
||||
byName[names[1]], byName[names[nd + 1]] = 0, nd
|
||||
end
|
||||
-- the encoding
|
||||
local encoding: { [number]: string } = {}
|
||||
if string.find(clear, "/Encoding%s+StandardEncoding") then
|
||||
for code, gname in GlyphNames.STANDARD do
|
||||
@@ -306,7 +296,6 @@ function Type1.decode(r: Bytes.Reader, opts: DecodeOptions?): (any, Err.Err?)
|
||||
cmap[cp] = k - 1
|
||||
end
|
||||
end
|
||||
-- font matrix / bbox
|
||||
local fm = string.match(clear, "/FontMatrix%s*[%[{]([^%]}]*)[%]}]")
|
||||
local scale = 0.001
|
||||
if fm then
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
--!strict
|
||||
-- font/woff: WOFF 1.0 (W3C Recommendation, 2012): an sfnt font whose tables are zlib-compressed one by one. Decoding
|
||||
-- rebuilds the sfnt and reads it through codecs/font/TrueType (TrueType or CFF outlines, see font/ttf); the extended
|
||||
-- metadata XML is kept in meta.woffMetadata. Written from the spec. Not supported: writing.
|
||||
-- font/woff: WOFF 1.0 (W3C Recommendation, 2012): zlib-compressed sfnt tables, rebuilt and read through
|
||||
-- codecs/font/TrueType; the metadata XML goes to meta.woffMetadata. Not supported: writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -20,7 +19,7 @@ local Woff = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a font whose sfnt is bigger (default 2^28)
|
||||
maxBytes: number?, -- default 2^28
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ sniff
|
||||
@@ -29,7 +28,6 @@ function Woff.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- sfnt(r, opts?) -> the rebuilt sfnt font file (a buffer) | nil, Err
|
||||
function Woff.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?, string?)
|
||||
local maxBytes = (opts and opts.maxBytes) or 2 ^ 28
|
||||
local base = r.base
|
||||
|
||||
@@ -1,10 +1,5 @@
|
||||
--!strict
|
||||
-- font/woff2/Rebuild: rebuilds the sfnt of a WOFF2 file (W3C WOFF2 Recommendation, 2024 edition): the table
|
||||
-- directory, the Brotli stream, the transformed glyf / loca tables (section 5: nContour, nPoints, flag, glyph,
|
||||
-- composite, bbox and instruction streams, triplet-encoded coordinates, the overlap bitmap) and the transformed hmtx
|
||||
-- (section 6: side bearings from the glyph bounds). Written from the spec. Not supported: collections (ttcf flavor).
|
||||
--
|
||||
-- Never throws: malformed data returns nil, message.
|
||||
-- font/woff2/Rebuild: the sfnt of a WOFF2 file (W3C WOFF2, 2024 edition: section 5 glyf / loca, section 6 hmtx).
|
||||
local Bytes = require("@rosetta/core/Bytes")
|
||||
local Brotli = require("@rosetta/codecs/compress/Brotli")
|
||||
|
||||
@@ -40,7 +35,6 @@ local function base128(r: Bytes.Reader): number?
|
||||
return nil
|
||||
end
|
||||
|
||||
-- a cursor over a stream inside the decompressed block
|
||||
type Stream = { buf: buffer, p: number, e: number, bad: boolean }
|
||||
local function stream(b: buffer, p: number, n: number): Stream
|
||||
return { buf = b, p = p, e = p + n, bad = false }
|
||||
@@ -77,7 +71,6 @@ local function withSign(flag: number, v: number): number
|
||||
return if band(flag, 1) ~= 0 then v else -v
|
||||
end
|
||||
|
||||
-- the glyf transform -> glyf bytes (a Writer), loca offsets, per-glyph xMin (for hmtx), numGlyphs, indexFormat
|
||||
local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, string?)
|
||||
local h = stream(b, p, n)
|
||||
su16(h) -- reserved
|
||||
@@ -132,7 +125,6 @@ local function glyf(b: buffer, p: number, n: number, maxBytes: number): (any?, s
|
||||
return nil, "empty glyph with a bounding box"
|
||||
end
|
||||
elseif nc > 0 then
|
||||
-- simple glyph
|
||||
local ends = table.create(nc, 0)
|
||||
local total = 0
|
||||
for c = 1, nc do
|
||||
@@ -363,7 +355,6 @@ local function hmtx(b: buffer, p: number, n: number, numGlyphs: number, numH: nu
|
||||
return out:finish(), nil
|
||||
end
|
||||
|
||||
-- sfnt(r, maxBytes) -> sfnt buffer | nil, message, code ("truncated" / "corrupt" / "unsupported" / "limit")
|
||||
function Rebuild.sfnt(r: Bytes.Reader, maxBytes: number): (buffer?, string?, string?)
|
||||
if not r:match("wOF2") then
|
||||
return nil, "bad signature", "corrupt"
|
||||
@@ -433,7 +424,6 @@ function Rebuild.sfnt(r: Bytes.Reader, maxBytes: number): (buffer?, string?, str
|
||||
if buffer.len(data) < streamTotal then
|
||||
return nil, "decompressed stream shorter than the tables", "corrupt"
|
||||
end
|
||||
-- the tables, glyf / loca / hmtx rebuilt
|
||||
local byTag: { [string]: any } = {}
|
||||
for _, e in dir do
|
||||
byTag[e.tag] = e
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
--!strict
|
||||
-- font/woff2: WOFF 2.0 (W3C Recommendation): sfnt tables in one Brotli stream, with the glyf / loca and hmtx
|
||||
-- transforms. Decoding rebuilds the sfnt (font/woff2/Rebuild) and reads it through codecs/font/TrueType (TrueType or CFF
|
||||
-- outlines, see font/ttf). Written from the spec; Brotli is codecs/compress/Brotli. Not supported: collections, writing.
|
||||
-- font/woff2: WOFF 2.0 (W3C Recommendation): Brotli-compressed sfnt tables with the glyf / loca and hmtx transforms,
|
||||
-- rebuilt by font/woff2/Rebuild. Not supported: collections, writing.
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ requires
|
||||
local Err = require("@rosetta/core/Err")
|
||||
@@ -20,7 +19,7 @@ local Woff2 = {
|
||||
}
|
||||
|
||||
export type DecodeOptions = {
|
||||
maxBytes: number?, -- refuse a font whose sfnt is bigger (default 2^28)
|
||||
maxBytes: number?, -- default 2^28
|
||||
}
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ constants
|
||||
@@ -30,7 +29,6 @@ function Woff2.sniff(r: Bytes.Reader): number
|
||||
end
|
||||
|
||||
-- ------------------------------------------------------------------------------------------------ decode
|
||||
-- sfnt(r, opts?) -> the rebuilt sfnt font file | nil, Err
|
||||
function Woff2.sfnt(r: Bytes.Reader, opts: DecodeOptions?): (buffer?, Err.Err?)
|
||||
local maxBytes = (opts and opts.maxBytes) or 2 ^ 28
|
||||
local at = r.base
|
||||
|
||||
Reference in new issue
Block a user