搜索插件、代码仓与 APP 分类

插件详情v0.2.0

fontify

将 SVG 图标转换为 OTF 字体并生成 Flutter 兼容类。提供 API 和 CLI 工具。

近 30 天下载
256
喜欢数
51
Pub 分数
40

安装插件

flutter pub add fontify

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Fontify 将 SVG 图标转换为 OTF 字体,并生成适用于 Flutter 的 Dart 类,以便轻松集成。它提供命令行界面(CLI)和 API,使开发者能够将其自定义图标作为字体打包到 Flutter 应用中。这通过减少对多个图像资源的需求并利用基于字体的渲染提升性能,简化了图标管理。该工具特别适用于需要自定义或品牌化图标集的项目。

适合场景

  • 从 SVG 创建自定义图标字体
  • 在Flutter应用中集成品牌图标
  • 使用字体图标减少应用包大小
  • 在 CI/CD 流水线中自动化图标字体生成

主要特点

  • SVG 到 OTF 字体转换
  • Flutter 类生成
  • CLI 和 API 支持
  • 支持自定义图标命名
  • 针对 Flutter 性能进行了优化

适合对象

  • 需要自定义图标的应用程序开发人员
  • 管理大型图标集的团队
  • 旨在最小化资产开销的项目

使用建议

  • 需要SVG文件作为输入
  • 仅限基于字体的图标渲染
  • 没有内置的图标编辑工具
  • 不适用于运行时动态图标更改

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Fontify

pub package

The Fontify 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.

Using CLI tool

Globally activate the package:

$ pub global activate fontify

And it's ready to go:

$ fontify <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:

  • -o or --output-class-file=<path> Output path for Flutter-compatible class that contains identifiers for the icons.
  • -i or --indent=<indent> Number of spaces in leading indentation for Flutter class file. (defaults to "2")
  • -c or --class-name=<name> Name for a generated class.
  • -p or --package=<name> Name of a package that provides a font. Used to provide a font through package dependency.

Font options:

  • -f or --font-name=<name> Name for a generated font.
  • --[no-]normalize Enables glyph normalization for the font. Disable this if every icon has the same size and positioning. (defaults to on)
  • --[no-]ignore-shapes Disables SVG shape-to-path conversion (circle, rect, etc.). (defaults to on)

Other options:

  • -z or --config-file=<path> Path to Fontify yaml configuration file. pubspec.yaml and fontify.yaml files are used by default.
  • -r or --recursive Recursively look for .svg files.
  • -v or --verbose Display every logging message.
  • -h or --help Shows usage information.

Usage example:

$ fontify assets/svg/ fonts/my_icons_font.otf --output-class-file=lib/my_icons.dart --indent=4 -r

Updated Flutter project's pubspec.yaml:

...

flutter:
  fonts:
    - family: Fontify Icons
      fonts:
        - asset: fonts/my_icons_font.otf

CLI tool config file

Fontify's configuration can also be placed in yaml file. Add fontify section to either pubspec.yaml or fontify.yaml file:

fontify:
  input_svg_dir: "assets/svg/"
  output_font_file: "fonts/my_icons_font.otf"
  
  output_class_file: "lib/my_icons.dart"
  class_name: "MyIcons"
  indent: 4
  package: my_font_package

  font_name: "My Icons"
  normalize: true
  ignore_shapes: true

  recursive: true
  verbose: false

input_svg_dir and output_font_file keys are required. It's possible to specify any other config file by using --config-file option.

Using API

svgToOtf and generateFlutterClass functions can be used for generating font and Flutter class.

The example of API usage is located in example folder.

Notes

  • 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.
  • When ignoreShapes is 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 normalize is 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.

Planned

  • Support svg-to-ttf conversion (cubic-to-quad curves approximation needs to be done).
  • Support ligatures.
  • Support font variations.

Contributing

Any suggestions, issues, pull requests are welcomed.

License

MIT

所属分类

依赖

依赖清单 9 项

源码仓库

使用此插件的项目

暂无相关数据。