svgToOtf function

SvgToOtfResult svgToOtf(
  1. {@required Map<String, String> svgMap,
  2. bool ignoreShapes,
  3. bool normalize,
  4. String fontName}
)

Converts SVG icons to OTF font.

  • svgMap contains name (key) to data (value) SVG mapping. Required.
  • If ignoreShapes is set to false, shapes (circle, rect, etc.) are converted into paths. Defaults to true. NOTE: Attributes like "fill" or "stroke" are ignored, which means only shape's outline will be used.
  • If normalize is set to true, glyphs are resized and centered to fit in coordinates grid (unitsPerEm). Defaults to true.
  • fontName is a name for a generated font.

Returns an instance of SvgToOtfResult class containing glyphs and a font.

Implementation

SvgToOtfResult svgToOtf({
  @required Map<String, String> svgMap,
  bool ignoreShapes,
  bool normalize,
  String fontName,
}) {
  svgMap ??= {};
  normalize ??= true;

  final svgList = [
    for (final e in svgMap.entries)
      Svg.parse(e.key, e.value, ignoreShapes: ignoreShapes)
  ];

  if (!normalize) {
    for (var i = 1; i < svgList.length; i++) {
      if (svgList[i - 1].viewBox.height != svgList[i].viewBox.height) {
        logger.logOnce(
            Level.warning,
            'Some SVG files contain different view box height, '
            'while normalization option is disabled. '
            'This is not recommended.');
        break;
      }
    }
  }

  final glyphList = svgList.map((e) => GenericGlyph.fromSvg(e)).toList();

  final font = OpenTypeFont.createFromGlyphs(
    glyphList: glyphList,
    fontName: fontName,
    normalize: normalize,
    useOpenType: true,
    usePostV2: false,
  );

  return SvgToOtfResult._(glyphList, font);
}