The icon_font package provides an easy way to convert SVG icons to OpenType font and generate Flutter-compatible class that contains identifiers for the icons (just like CupertinoIcons or Icons classes).
The package is written fully in Dart and doesn't require any external dependency. Compatible with dart2js and dart2native.
Add following section to pubspec.yaml:
dev_dependencies:
icon_font:
git:
url: https://github.com/SkinnyMind/icon_font.gitUse following shell command to generate icon font:
$ dart run icon_font:generate <input-svg-dir> <output-font-file> [options]Required positional arguments:
<input-svg-dir>Path to the input directory that contains .svg files.<output-font-file>Path to the output font file. Should have .otf extension.
Flutter class options:
-oor--output-class-file=<path>Output path for Flutter-compatible class that contains identifiers for the icons.-cor--class-name=<name>Name for a generated class.-por--package=<name>Name of a package that provides a font. Used to provide a font through package dependency.-lor--listGenerate a list of icons (Can be accessed throughClassName.values).
Font options:
-for--font-name=<name>Name for a generated font.--[no-]normalizeEnables glyph normalization for the font. Disable this if every icon has the same size and positioning. (defaults to on)--[no-]ignore-shapesDisables SVG shape-to-path conversion (circle, rect, etc.). (defaults to on)
Other options:
-zor--config-file=<path>Path to icon_font yaml configuration file. pubspec.yaml and icon_font.yaml files are used by default.-ror--recursiveRecursively look for .svg files.-hor--helpShows usage information.
Usage example:
$ dart run icon_font:generate assets/svg/ fonts/my_icons_font.otf --output-class-file=lib/my_icons.dart -rUpdated Flutter project's pubspec.yaml:
flutter:
fonts:
- family: My Icons
fonts:
- asset: fonts/my_icons_font.otficon_font's configuration can also be placed in yaml file.
Add icon_font section to either pubspec.yaml or icon_font.yaml file:
icon_font:
input_svg_dir: "assets/svg/"
output_font_file: "fonts/my_icons_font.otf"
output_class_file: "lib/my_icons.dart"
class_name: "MyIcons"
package: my_font_package
list: true
font_name: "My Icons"
normalize: true
ignore_shapes: true
recursive: trueinput_svg_dir and output_font_file keys are required.
It's possible to specify any other config file by using --config-file option.
IconFont.svgToOtf(...) and IconFont.generateFlutterClass(...) functions can be used for generating font and Flutter class.
- Generated OpenType font is using CFF table.
- Generated font is using PostScript Table (post) of version 3.0, i.e., it doesn't contain glyph names.
- Supported SVG elements: path, g, circle, rect, polyline, polygon, line.
- SVG transforms are applied to paths according to specs.
- SVG
<g>element's children are expanded to the root with transformations applied. Anything else related to the group is ignored and group referencing is not supported. - Consider using Non-zero fill rule, or using tools like svg-fixer, pico-svg or similar.
- When
ignoreShapesis set to false, every SVG shape's (circle, rect, etc.) outline is converted to path. Note that any attributes like "fill" or "stroke" are ignored and only the outline is used, so the resulting glyph may look different from SVG icon. It's recommended to convert every element in SVG to path. - When
normalizeis set to false, it's recommended that SVG icons have the same height. Otherwise, final result might not look as expected. - When Flutter class is generated, static variables names derive from SVG file name converted to pascal case with non-allowed characters removed. Name is set to 'unnamed', if it's empty. Suffix '_{i+1}' is added, if name already exists.
Any suggestions, issues, pull requests are welcomed.
Hard fork of icon_font_generator
The original software is fork of unsupported package: