API Usage topic

API Usage

The API mirrors the CLI pipeline: convert SVG strings to a font, write the font file, then generate a Flutter IconData class.

Job API

For directory-based workflows (same I/O as the CLI), use FontJob and runFontJob:

runFontJob(FontJob(
  inputSvgDir: 'assets/svg',
  outputFontFile: 'fonts/my_icons.otf',
  outputClassFile: 'lib/my_icons.dart',
  className: 'MyIcons',
));

Parse a multi-font yaml config:

final config = parseFontifyConfig(await File('fontify_plus.yaml').readAsString());
final jobs = config.resolve(fontFilter: 'icons');
runFontJobs(jobs);

runFontJob / runFontJobs require dart:io. On web, use the low-level pipeline below with in-memory SVG strings.

Low-level pipeline

final result = svgToOtf(
  svgMap: {'arrow_up': await File('arrow_up.svg').readAsString()},
  fontName: 'My Icons',
);

writeToFile('MyIcons.otf', result.font);

final source = generateFlutterClass(
  glyphList: result.glyphList,
  familyName: result.font.familyName,
  className: 'MyIcons',
  fontFileName: 'MyIcons.otf',
);

Write source to a .dart file with dart:io (or your preferred file API).

svgToOtf

Converts a map of SVG strings into an OpenType font.

Parameter Type Default Description
svgMap Map<String, String> (required) Glyph name to SVG source. Keys become glyph identifiers.
outlineStrokes bool? true Convert stroked paths into the filled region the stroke covers.
preview bool? true Store a minified copy of the input SVG on each glyph for dartdoc previews in the generated IconData class.
normalize bool? true Scale each glyph so its longest side fills the em square, then centre it.
useOpenType bool? true Emit CFF (OpenType) outlines. When false, TrueType outlines are generated with cubic-to-quadratic approximation.
fontName String? null PostScript / family name for the generated font.
strokeWidthRange StrokeWidthRange? null Build a variable font whose wght axis is the stroke width (min and max in SVG units). The maximum is the default instance unless defaultStrokeWidth names another width. Omit for a static font. See Variable stroke width.
defaultStrokeWidth double? null The width the axis opens at, instead of the range maximum. Requires strokeWidthRange and must lie strictly inside it; adds a third master.

Returns a SvgToOtfResult containing:

  • glyphListList<GenericGlyph> of parsed glyphs (pass to generateFlutterClass).
  • fontOpenTypeFont instance (pass to writeToFile).

generateFlutterClass

Emits Dart source for a class of IconData constants.

Parameter Type Default Description
glyphList List<GenericGlyph> (required) Glyphs from svgToOtf.
className String? null Generated class name (PascalCase recommended).
familyName String? null Font family name used in each IconData.
package String? null Package name when the font is provided through a dependency.
fontFileName String? null Font file name referenced in generated docs.
indent int? 2 Leading spaces for class members.
strokeWidthRange StrokeWidthRange? null When set, documents the variable wght axis in the class comment.
defaultStrokeWidth double? null When set alongside strokeWidthRange, names the width the axis opens at in that comment. Pass the same value given to svgToOtf. Ignored without a range.
preview bool? null false omits previews, true always emits them, null emits them unless the file would exceed 2 MiB (dropped with a warning).

Returns the class file contents as a String.

writeToFile / readFromFile

writeToFile(String path, OpenTypeFont font) serialises an OpenTypeFont to disk. Use a .otf extension for CFF-based fonts.

readFromFile(String path) reads an OpenType font back from disk. Both require dart:io and are unavailable on web targets.

Paint attributes

Paint attributes other than stroke geometry — fill colour, gradients, opacity — are ignored. A glyph is a single-colour region; colour comes from the surrounding Flutter text style at render time.

Classes

OpenTypeFont API Usage
An OpenType font. Contains either TrueType (glyf table) or OpenType (CFF2 table) outlines
SvgToOtfResult API Usage
Result of svg-to-otf conversion.

Functions

generateFlutterClass({required List<GenericGlyph> glyphList, String? className, String? familyName, String? fontFileName, String? package, int? indent, StrokeWidthRange? strokeWidthRange, double? defaultStrokeWidth, bool? preview}) String API Usage
Generates a Flutter-compatible class for a list of glyphs.
parseFontifyConfig(String yamlSource) FontifyConfig API Usage
Parses a YAML document containing a fontify_plus: section.
readFromFile(String path) OpenTypeFont API Usage
Reads OpenType font from a file.
runFontJob(FontJob job) FontJobResult API Usage
Runs one FontJob: reads SVGs, builds the font, writes outputs.
runFontJobs(Iterable<FontJob> jobs) List<FontJobResult> API Usage
Runs jobs sequentially; stops on the first failure.
svgToOtf({required Map<String, String> svgMap, bool? outlineStrokes, bool? preview, bool? normalize, bool? useOpenType, String? fontName, DateTime? created, DateTime? modified, StrokeWidthRange? strokeWidthRange, double? defaultStrokeWidth}) SvgToOtfResult API Usage Stroked Icons Glyph Sizing
Converts SVG icons to OTF font.
writeToFile(String path, OpenTypeFont font) → void API Usage
Writes OpenType font to a file.