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

插件详情v4.1.0

icon_font_generator

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

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3.4万
喜欢数
147
Pub 分数
130

安装插件

flutter pub add icon_font_generator

插件介绍

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

一个 Flutter/Dart 包,可将 SVG 图标转换为 OTF 字体,并生成相应的 Dart 类,以便在 Flutter 应用中轻松集成。它提供命令行界面(CLI)和 API,支持自动化,帮助开发者在多个平台上简化图标管理。生成的字体兼容 Android、iOS、Web、macOS、Windows 和 Linux,确保图标渲染的一致性。非常适合需要高效管理自定义图标集的团队。

适合场景

  • 从 SVG 生成自定义图标字体
  • 自动化图标资产创建
  • 将矢量图标集成到Flutter应用中
  • 支持多平台图标渲染
  • 简化从设计到代码的工作流程

主要特点

  • SVG 到 OTF 字体转换
  • 图标Dart类生成
  • CLI 和 API 接口
  • 跨平台支持
  • 自定义图标集管理
  • 与 Flutter 的轻松集成

适合对象

  • Flutter 开发者
  • 设计团队
  • UI/UX 工程师
  • 需要自定义图标的应用
  • 频繁更新图标的项目

使用建议

  • 需要SVG文件作为输入
  • 需要为 CLI 或 API 使用进行设置
  • 可能需要额外的构建步骤
  • 字体文件大小考虑
  • 仅限图标使用场景

原始 README

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

icon_font_generator

pub package

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

Font generation

Install via dev dependency
$ flutter pub add --dev icon_font_generator

# And it's ready to go:
$ flutter pub run icon_font_generator:generate <input-svg-dir> <output-font-file> [options]
or Globally activate the package:
$ pub global activate icon_font_generator

# And it's ready to go:
$ icon_font_generator <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.
  • -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.
  • --[no-]format Format dart generated code.

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 icon_font_generator yaml configuration file. pubspec.yaml and icon_font.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:

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

Updated Flutter project's pubspec.yaml:

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

Config file

icon_font_generator'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
  format: true

  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.

Contributing

Any suggestions, issues, pull requests are welcomed.

License

MIT

Credits

This software is fork of unsupported package:

所属分类

依赖

依赖清单 11 项

源码仓库

使用此插件的项目