An unsized font face.
@eryx/font Module
Font loading, inspection, shaping, and rasterization.
This library opens OpenType, TrueType, and font collection files from disk,
from memory, or from the host system font registry. A Typeface represents
an unsized face and exposes font metadata, glyph coverage, raw OpenType
tables, and variable-font axes. Calling Typeface:at() creates a sized
Font, which can shape UTF-8 text with HarfBuzz, measure text, inspect glyph
metrics and outlines, rasterize individual glyphs, or render text into a
pixel buffer.
Text shaping operates on one run at a time. Resolve bidi order, split text
into script/language/font runs, apply fallback fonts, and perform wrapping
before calling Font:shape() or the higher-level measurement and rendering
helpers.
Example:
local font = require("@eryx/font")
local face = font.findSystem("Inter", { weight = 600 }) or font.openFile("Inter-Regular.ttf")
local body = face:at(18)
local measure = body:measure("Hello, world!")
print(measure.width, measure.height)
local image = body:render("Hello, world!", {
width = 320,
align = "center",
mode = "gray",
})
body:close()
face:close()
Variable-font example:
local face = font.openFile("Recursive_VF.ttf")
local display = face:at(48, {
variations = {
wght = 750,
slnt = -8,
},
})
Summary
Classes
Functions
API Reference
Classes
Typeface
Properties
Family name reported by the font.
Style name reported by the font.
Source file path when the face was opened from disk or the system.
Whether this typeface has been closed.
Design units per em.
Number of glyphs in the face.
0-based face index for font collections.
OpenType weight class.
OpenType width class.
Whether the face is italic or oblique.
Selected OS/2 table metrics when available.
Variable-font axes exposed by this face.
Typeface:hasGlyph
Return whether the face contains a glyph for a Unicode codepoint.
Parameters
Unicode codepoint to test.
Returns
Whether the face has a glyph for the codepoint.
Typeface:glyphId
Return the glyph id for a Unicode codepoint.
Parameters
Unicode codepoint to look up.
Returns
Glyph id, or nil when the codepoint is missing.
Typeface:hasText
Return whether every codepoint in a UTF-8 string has a glyph.
Parameters
Text to check.
Returns
Whether every codepoint has a glyph.
Typeface:missingGlyphs
Return the codepoints in a UTF-8 string that are missing from this face.
Parameters
Text to check.
Returns
Missing glyph records in text order.
Typeface:names
Return decoded records from the font name table.
Returns
Name records.
Typeface:table
Return raw data for an OpenType table.
Parameters
4-byte OpenType table tag, such as "head", "name", or "OS/2".
Returns
Table bytes, or nil when the table is absent.
Typeface:codepoints
Return the Unicode cmap entries exposed by this face.
Returns
Character-to-glyph mapping entries.
Typeface:at
Create a sized font from this typeface. size is in CSS-style pixels, not points.
Parameters
Pixel size.
Sizing and rendering options.
Returns
Sized font.
Typeface:close
Release resources held by this typeface.
ShapedRun
A shaped text run.
Properties
Original text that was shaped.
Whether this shaped run has been closed.
Positioned glyphs in shaping order.
Final pen advance.
Ink bounds.
ShapedRun:close
Release resources held by this shaped run.
Font
A sized font created from a Typeface.
Properties
Source typeface.
Pixel size.
Whether this font has been closed.
Font-wide metrics at this size.
Font:shape
Shape a UTF-8 text run.
Parameters
Text to shape.
Shaping options.
Returns
Shaped run.
Font:measure
Measure shaped text.
Parameters
Text to measure.
Shaping options.
Returns
Text measurement.
Font:bounds
Return ink bounds for shaped text.
Parameters
Text to measure.
Shaping options.
Returns
Ink bounds.
Font:glyphMetrics
Return metrics for a glyph id.
Parameters
Glyph id to inspect.
Font options to apply for this lookup.
Returns
Glyph metrics.
Font:outlineGlyph
Return a vector outline for a glyph id.
Parameters
Glyph id to outline.
Font options to apply for this lookup.
Returns
Glyph outline, or nil when no outline is available.
Font:rasterizeGlyph
Rasterize a glyph id into a glyph bitmap.
Parameters
Glyph id to rasterize.
Rasterization options.
Returns
Glyph bitmap.
Font:renderGlyph
Render a glyph id into an image buffer.
Parameters
Glyph id to render.
Rendering options.
Returns
Rendered image buffer.
Font:render
Render UTF-8 text into an image buffer. Multiline rendering splits on "\n". Mixed-direction bidi text still needs run itemization before shaping.
Parameters
Text to render.
Rendering options.
Returns
Rendered image buffer.
Font:close
Release resources held by this sized font.
Functions
font.openFile
Open a font face from a file path.
Parameters
File path to an OpenType, TrueType, TTC, or OTC font.
Open options.
Returns
Opened typeface.
font.fromBuffer
Open a font face from an in-memory font buffer.
Parameters
Font bytes.
Open options.
Returns
Opened typeface.
font.findSystem
Find and open the best matching installed system font for a family name.
Parameters
Family name to search for.
System font match options.
Returns
Opened typeface, or nil when no matching font is found.
font.listSystem
List discovered system font faces.
Parameters
System font discovery options.
Returns
Discovered font faces.
font.listSystemFamilies
List discovered system font faces grouped by family.
Parameters
System font discovery options.
Returns
Discovered font families.
font.refreshSystem
Refresh the process-local system font cache.
Parameters
System font discovery options.
Types
OpenOptions
Options for opening a font face from a file or buffer.
0-based index for TTC/OTC font collections.
SystemFontOptions
Options for system font discovery and matching.
0-based index for TTC/OTC font collections.
Preferred style name, such as "Regular", "Bold", or "Italic".
Preferred OpenType weight class. Typical values are 100 through 900.
Preferred OpenType width class, where 5 is normal width.
Preferred italic or oblique state.
Extra directories to scan in addition to the platform defaults.
Rebuild the process-local system font cache before answering.
SystemFontInfo
Metadata for a discovered system font face.
Family name reported by the font.
Style name reported by the font.
0-based face index for font collections.
OpenType weight class. Typical values are 100 through 900.
OpenType width class, where 5 is normal width.
Whether the face is italic or oblique.
A single path for the common case, or all matching paths when duplicates were found.
All discovered paths for this family/style/face combination.
SystemFontFamilyInfo
A system font family grouped with all discovered faces in that family.
Family name.
Faces discovered for this family.
VariationAxis
OpenType variable-font axis metadata.
4-byte OpenType variation tag, such as "wght", "wdth", or "slnt".
Human-readable axis name when present in the font.
Minimum supported design coordinate.
Maximum supported design coordinate.
Default design coordinate.
FontNameRecord
A decoded entry from a font name table.
OpenType name identifier.
OpenType platform identifier.
OpenType encoding identifier.
OpenType language identifier.
Decoded text for this name record.
Raw name bytes as stored in the font.
CmapEntry
A character-to-glyph mapping entry.
Unicode codepoint.
Glyph id used by the font.
OS2Metrics
Selected metrics from a font OS/2 table.
OS/2 table version.
OpenType weight class.
OpenType width class.
OS/2 selection bitfield.
Typographic ascender in font units.
Typographic descender in font units.
Typographic line gap in font units.
Windows ascent in font units.
Windows descent in font units.
Recommended subscript glyph width in font units.
Recommended subscript glyph height in font units.
Recommended superscript glyph width in font units.
Recommended superscript glyph height in font units.
Recommended strikeout thickness in font units.
Recommended strikeout position in font units.
Lowercase x-height in font units when available.
Capital letter height in font units when available.
HintingMode
FreeType hinting mode used when sizing and rasterizing glyphs.
FontOptions
Options for creating or using a sized font.
Device DPI for backends that need physical point-size conversion.
Hinting mode.
Whether to use antialiasing when rasterizing.
Whether embedded bitmap strikes may be used.
Whether color glyph layers may be used.
OpenType variation coordinates keyed by 4-byte axis tags.
MissingGlyph
A missing codepoint reported by Typeface:missingGlyphs().
Unicode codepoint that has no glyph.
0-based byte offset into the original UTF-8 string.
FontMetrics
Metrics for a sized font in pixels.
Distance from baseline to ascender.
Distance from baseline to descender.
Recommended gap between lines.
Recommended line height.
Underline position relative to the baseline when available.
Underline thickness when available.
Strikethrough position relative to the baseline when available.
Strikethrough thickness when available.
TextDirection
Text direction for shaping.
ClusterLevel
HarfBuzz cluster granularity.
ShapeOptions
Options for shaping a single text run.
Text direction.
ISO 15924 script tag, such as "Latn", "Arab", "Deva", or "auto".
BCP 47 language tag, such as "en" or "ar".
OpenType feature settings keyed by feature tag.
Cluster granularity reported in shaped glyphs.
ShapedGlyph
A positioned glyph from a shaped text run.
Glyph id.
0-based byte offset into the original UTF-8 string.
Horizontal advance in pixels.
Vertical advance in pixels.
Horizontal offset in pixels.
Vertical offset in pixels.
GlyphMetrics
Metrics for a single glyph in a sized font.
Glyph bitmap width in pixels.
Glyph bitmap height in pixels.
Horizontal bearing X in pixels.
Horizontal bearing Y in pixels.
Horizontal advance in pixels.
Vertical bearing X in pixels when available.
Vertical bearing Y in pixels when available.
Vertical advance in pixels when available.
PathMoveToCommand
A path command that moves the current point.
Command discriminator.
Destination X coordinate.
Destination Y coordinate.
PathLineToCommand
A path command that draws a line.
Command discriminator.
Destination X coordinate.
Destination Y coordinate.
PathQuadToCommand
A path command that draws a quadratic Bezier curve.
Command discriminator.
Control point X coordinate.
Control point Y coordinate.
Destination X coordinate.
Destination Y coordinate.
PathCubicToCommand
A path command that draws a cubic Bezier curve.
Command discriminator.
First control point X coordinate.
First control point Y coordinate.
Second control point X coordinate.
Second control point Y coordinate.
Destination X coordinate.
Destination Y coordinate.
PathCommand
A vector outline drawing command for a glyph.
GlyphOutline
A glyph outline and its layout metrics.
Drawing commands in glyph coordinate order.
Outline bounds in pixels.
Horizontal advance in pixels.
Vertical advance in pixels.
RasterizeMode
Mode for rasterizing an individual glyph.
RasterizeOptions
Options for rasterizing an individual glyph.
Rasterization mode.
Horizontal subpixel offset.
Vertical subpixel offset.
TextAlign
Horizontal alignment for multiline rendering.
RenderOptions
Options for rendering text into a pixel buffer.
Text direction.
ISO 15924 script tag, such as "Latn", "Arab", or "Deva".
BCP 47 language tag, such as "en" or "ar".
OpenType feature settings keyed by feature tag.
Cluster granularity reported during shaping.
Device DPI for backends that need physical point-size conversion.
Hinting mode.
Whether to use antialiasing when rasterizing.
Whether embedded bitmap strikes may be used.
Whether color glyph layers may be used.
OpenType variation coordinates keyed by 4-byte axis tags.
Render mode.
Horizontal subpixel offset.
Vertical subpixel offset.
Maximum line width for multiline rendering.
Override line height in pixels.
Horizontal alignment for lines.
LcdOrder
Subpixel component order for LCD glyph bitmaps.
GlyphBitmapFormat
Pixel format for an individual glyph bitmap.
GlyphBitmap
A rasterized individual glyph.
Bitmap width in pixels.
Bitmap height in pixels.
Bytes per bitmap row.
Horizontal bitmap origin relative to the pen position.
Vertical bitmap origin relative to the baseline.
Horizontal advance in pixels.
Vertical advance in pixels.
Pixel format.
LCD component order when format is "lcd24".
Raw pixel data.
RenderedBuffer
A rendered text or glyph image buffer.
Buffer width in pixels.
Buffer height in pixels.
Bytes per image row.
Pixel format.
Baseline position inside the image.
Final pen advance after rendering.
Ink bounds inside the image.
Raw pixel data.
TextMeasure
Measurement result for shaped text.
Measured width in pixels.
Measured height in pixels.
Final pen advance.
Ink bounds.
Vector2
A 2D vector.
X coordinate.
Y coordinate.
Rect
A rectangle.
X coordinate of the top-left corner.
Y coordinate of the top-left corner.
Rectangle width.
Rectangle height.