Convert a legacy SVG font into a binary TrueType (TTF) font.
The crate reads an SVG document that contains a <font> element with <glyph>
and <missing-glyph> children, normalizes each glyph outline, reduces cubic
Beziers to quadratics, and writes the eleven SFNT tables (GSUB, OS/2,
cmap, glyf, head, hhea, hmtx, loca, maxp, name, post) with
their offsets and checksums.
[dependencies]
svg2ttf = "0.2"use svg2ttf::{svg2ttf, Options};
let svg = std::fs::read_to_string("font.svg")?;
let ttf = svg2ttf(&svg, &Options::default())?;
std::fs::write("font.ttf", ttf)?;
# Ok::<(), Box<dyn std::error::Error>>(())The single entry point is svg2ttf(svg: &str, options: &Options) -> Result<Vec<u8>, Error>.
All fields are optional. Each text field overrides a value derived from the SVG font. Precedence is the option, then the SVG value, then a built-in default.
| Field | Effect |
|---|---|
id |
Font id. |
family_name |
Family name. Falls back to the SVG family, then the id. |
copyright |
Copyright string. Falls back to the SVG metadata. |
description |
Description. Defaults to a fixed line. |
url |
Vendor url. Defaults to a fixed url. |
subfamily_name |
Subfamily name (name id 2). Defaults to Regular. |
full_name |
Full name (name id 4) and PostScript name (id 6). |
version |
Version string. Must match Version x.y or x.y. |
timestamp |
Unix timestamp in seconds. Fixes the head created and modified dates. |
The head created and modified dates are the only part of the output that
depends on anything besides the input. Set timestamp to fix them, and the
same SVG and the same timestamp always produce the same bytes. Without
timestamp the dates use the current time.
svg2ttf returns an Error, an enum with a variant per failure cause. It fails
when the SVG has no <font> tag, the <font> has no <font-face>, a glyph
lacks a path, the version option is malformed, or a glyph coordinate falls
outside the signed 16-bit range. Match on the variant to branch on the cause.
The Display text stays stable for showing to a user.
Licensed under the MIT license.