Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

webfont

itgalaxy123.2kMIT11.2.26TypeScript support: included

Generator of fonts from svg icons, svg icons to svg font, svg font to ttf, ttf to eot, ttf to woff, ttf to woff2

cli, standalone, font, fonts, webfont, webfonts, svg, ttf, woff, woff2, otf, ttf2eot, ttf2woff, ttf2svg, svg2ttf, css, scss, builder, generator

readme

webfont

NPM version Travis Build Status Build status

Generator of fonts from SVG icons.

Features

  • Supported font formats: WOFF2, WOFF, EOT, TTF and SVG;
  • Support config files: use a JavaScript, JSON or YAML file to specify configuration information for an entire directory and all of its subdirectories;
  • Support all popular browsers, including IE8+;
  • Allows using custom templates (example css, scss, styl etc);
  • No extra dependencies as gulp, grunt or other big tools;
  • Tested on all platforms (linux, windows and osx);
  • CLI;
  • Webpack plugin.

Table Of Contents


Installation

npm install --save-dev webfont

Usage

import webfont from "webfont";

webfont({
  files: "src/svg-icons/**/*.svg",
  fontName: "my-font-name",
})
  .then((result) => {
    // Do something with result
    Function.prototype(result);
    // Or return it
    return result;
  })
  .catch((error) => {
    throw error;
  });

or

const webfont = require("webfont").default;

webfont({
  files: "src/svg-icons/**/*.svg",
  fontName: "my-font-name",
})
  .then((result) => {
    // Do something with result
    Function.prototype(result);
    // Or return it
    return result;
  })
  .catch((error) => {
    throw error;
  });

Options

files

  • Type: string | array
  • Description: A file glob, or array of file globs. Ultimately passed to fast-glob to figure out what files you want to get.
  • Note: node_modules and bower_components are always ignored.

configFile

  • Type: string
  • Description: Path to a specific configuration file (JSON, YAML, or CommonJS) or the name of a module in node_modules that points to one.
  • Note: If you do not provide configFile, webfont will search up the directory tree for configuration file in the following places, in this order:
    1. a webfont property in package.json
    2. a .webfontrc file (with or without filename extension: .json, .yaml, and .js are available)
    3. a webfont.config.js file exporting a JS object. The search will begin in the working directory and move up the directory tree until it finds a configuration file.

fontName

  • Type: string
  • Default: webfont
  • Description: The font family name you want.

formats

  • Type: array,
  • Default: ['svg', 'ttf', 'eot', 'woff', 'woff2'],
  • Possible values: svg, ttf, eot, woff, woff2,
  • Description: Font file types to generate.

template

  • Type: string
  • Default: null
  • Possible values: css, scss, styl (feel free to contribute more).
  • Note: If you want to use a custom template use this option pass in a path string like this:

    webfont({
      template: "./path/to/my-template.css",
    });

    Or

    webfont({
      template: path.resolve(__dirname, "./my-template.css"),
    });

    Or

    webfont({
      template: path.resolve(__dirname, "./my-template.styl"),
    });

templateClassName

  • Type: string
  • Default: null
  • Description: Default font class name.

templateFontPath

  • Type: string
  • Default: ./
  • Description: Path to generated fonts in the CSS file.

templateFontName

  • Type: string
  • Default: Gets is from fontName if not set, but you can specify any value.
  • Description: Template font family name you want.

glyphTransformFn

  • Type: function
  • Default: null
  • Description: If you want to transform glyph metadata (e.g. titles of CSS classes or unicode) before transferring it in your style template for your icons, you can use this option with glyphs metadata object.
  • Example:

    import webfont from "webfont";
    
    webfont({
      files: "src/svg-icons/**/*.svg",
      glyphTransformFn: (obj) => {
        obj.name += "_transform";
        something();
    
        return obj;
      },
    })
      .then((result) => {
        // Do something with result
        Function.prototype(result);
        // Or return it
        return result;
      })
      .catch((error) => {
        throw error;
      });

sort

  • Type: bool
  • Default: true
  • Description: Whether you want to sort the icons sorted by name.

svgicons2svgfont

svgicons2svgfont options

These can be appended to webfont options. These are passed directly to svgicons2svgfont.

svgicons2svgfont.fontName

svgicons2svgfont.fontId

  • Type: string
  • Default: The fontName value
  • Description: The font id you want.

svgicons2svgfont.fontStyle

  • Type: string
  • Default: ''
  • Description: The font style you want.

svgicons2svgfont.fontWeight

  • Type: string
  • Default: ''
  • Description: The font weight you want.

svgicons2svgfont.fixedWidth

  • Type: boolean
  • Default: false
  • Description: Creates a monospace font of the width of the largest input icon.

svgicons2svgfont.centerHorizontally

  • Type: boolean
  • Default: false
  • Description: Calculate the bounds of a glyph and center it horizontally.

svgicons2svgfont.normalize

  • Type: boolean
  • Default: false
  • Description: Normalize icons by scaling them to the height of the highest icon.

svgicons2svgfont.fontHeight

  • Type: number
  • Default: MAX(icons.height)
  • Description: The outputted font height (defaults to the height of the highest input icon).

svgicons2svgfont.round

  • Type: number
  • Default: 10e12 Setup SVG path rounding.

svgicons2svgfont.descent

  • Type: number
  • Default: 0
  • Description: The font descent. It is useful to fix the font baseline yourself.
  • Warning: The descent is a positive value!.

svgicons2svgfont.ascent

  • Type: number
  • Default: fontHeight - descent
  • Description: The font ascent. Use this options only if you know what you're doing. A suitable value for this is computed for you.

svgicons2svgfont.metadata

  • Type: string
  • Default: undefined
  • Description: The font metadata. You can set any character data in, but this is the recommended place for a copyright mention.

svgicons2svgfont.log

  • Type: function
  • Default: console.log
  • Description: Allows you to provide your own logging function. Set to function(){} to disable logging.

Command Line Interface

The interface for command-line usage is fairly simplistic at this stage, as seen in the following usage section.

CLI Installation

Add the cli script to your package.json file's scripts object:

{
  "scripts": {
    "webfont": "node node_modules/webfont/dist/cli.js"
  }
}

If you're using cross-env:

{
  "scripts": {
    "webfont": "cross-env node_modules/webfont/dist/cli.js"
  }
}

CLI Usage

    Usage: webfont [input] [options]

    Input: File(s) or glob(s).

        If an input argument is wrapped in quotation marks, it will be passed to "fast-glob"
        for cross-platform glob support.

    Options:

        --config

            Path to a specific configuration file (JSON, YAML, or CommonJS)
            or the name of a module in \`node_modules\` that points to one.
            If no \`--config\` argument is provided, webfont will search for
            configuration  files in the following places, in this order:
               - a \`webfont\` property in \`package.json\`
               - a \`.webfontrc\` file (with or without filename extension:
                   \`.json\`, \`.yaml\`, and \`.js\` are available)
               - a \`webfont.config.js\` file exporting a JS object
            The search will begin in the working directory and move up the
            directory tree until a configuration file is found.

        -f, --font-name

            The font family name you want, default: "webfont".

        -h, --help

            Output usage information.

        -v, --version

            Output the version number.

        -r, --formats

            Only this formats generate.

        -d, --dest

            Destination for generated fonts.

        -m, --dest-create
            Create destination directory if it does not exist.

        -t, --template

            Type of template (\`css\`, \`scss\`, \`styl\`) or path to custom template.
'
        -s, --dest-template

            Destination for generated template. If not passed used \`dest\` argument value.

        -c, --template-class-name

            Class name in css template.

        -p, --template-font-path

            Font path in css template.

        -n, --template-font-name

            Font name in css template.

        --no-sort

            Keeps the files in the same order of entry

        --verbose

            Tell me everything!.

    For "svgicons2svgfont":

        --font-id

            The font id you want, default as "--font-name".

        --font-style

            The font style you want.

        --font-weight

            The font weight you want.

        --fixed-width

            Creates a monospace font of the width of the largest input icon.

        --center-horizontally

            Calculate the bounds of a glyph and center it horizontally.

        --normalize

            Normalize icons by scaling them to the height of the highest icon.

        --font-height

            The outputted font height [MAX(icons.height)].

        --round

            Setup the SVG path rounding [10e12].

        --descent

            The font descent [0].

        --ascent

            The font ascent [height - descent].

        --start-unicode

            The start unicode codepoint for files without prefix [0xEA01].

        --prepend-unicode

            Prefix files with their automatically allocated unicode codepoint.

        --metadata

            Content of the metadata tag.

        --add-hash-in-font-url

            Generated font url will be : [webfont].[ext]?v=[hash]

CLI Exit Codes

The CLI can exit the process with the following exit codes:

  • 0: All ok.
  • 1: Something unknown went wrong.
  • Other: related to using packages.

Roadmap

  • The ability to generate from any type to any type;
  • More tests, include CLI test;
  • Improved docs;
  • Reduce package size (maybe implement ttf2woff2 with native js library);
  • Improve performance (maybe use cache for this).

Contribution

Feel free to push your code if you agree with publishing under the MIT license.

Changelog

Check our Changelog

License

Check our License

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

11.2.26 (2021-08-07)

11.2.25 (2021-08-03)

11.2.24 (2021-08-01)

11.2.23 (2021-07-31)

11.2.21 (2021-07-31)

11.2.20 (2021-07-31)

11.2.22 (2021-07-31)

11.2.20 (2021-07-31)

11.2.21 (2021-07-31)

11.2.20 (2021-07-31)

11.2.19 (2021-07-27)

11.2.18 (2021-07-27)

11.2.17 (2021-07-27)

Bug Fixes

  • deps: switch out from latest versions in dependencies (67efe05), closes #464

11.2.16 (2021-07-27)

11.2.15 (2021-07-26)

11.2.14 (2021-07-26)

11.2.13 (2021-07-24)

11.2.12 (2021-07-23)

11.2.11 (2021-07-23)

11.2.10 (2021-07-20)

11.2.8 (2021-07-06)

11.2.7 (2021-07-01)

11.2.6 (2021-06-29)

11.2.5 (2021-06-29)

11.2.4 (2021-06-23)

11.2.3 (2021-06-23)

11.2.2 (2021-06-22)

11.2.1 (2021-06-22)

11.2.0 (2021-06-22)

Features

  • add new transformation function (6a0c60c), closes #183
  • add standalone glyphs data getter (0cfe581)
  • types: add unicode key to GlyphMetadata (33fa7cb)
  • types: allow unicode key and Promise as GlyphTransformFn output (a3bde0c)
  • add standalone options getter (13b8596)

Bug Fixes

  • deps: fix security issue (3abc00a)
  • deps: fixed 1 package vulnerability (feda633)
  • deps: pin svgicons2svgfont to 10.0.1 (87789d3)

11.1.1 (2021-05-13)

Bug Fixes

  • deps: upgrade wawoff2 dependency (69a8dfb)

11.1.0 (2021-05-12)

Features

  • add support to base64 font strings on templates (2a56338), closes #328 #329

11.0.0 (2021-05-12)

⚠ BREAKING CHANGES

  • We removed support for Node.js 11 and Node.js 10 at this version. Please upgrade your environment to Node.js 12 or higher.

ci

  • remove Node.js 10 and 11 support (0f0dbf6)

10.1.0 (2021-05-12)

Features

  • cli: create output folder if it's not present. (#280) (d66a0d4), closes #307

Bug Fixes

  • deps: upgrade dependencies (d623b06)

10.0.1 (2021-05-12)

10.0.0 (2021-04-08)

⚠ BREAKING CHANGES

  • starting from version 10 stable, you should use webfont via named import, like this: import { webfont } from "webfont".

  • adopt named import/export system (31adbc7)

  • drop support for Node.js 8;

Features

  • migrate all codebase to TypeScript (0b71779)
  • add support to ligatures and JSON template (f622c44)
  • cli: add support to template-cache-string argument (ae8b050)
  • demo: add styl file example (3aa5180)
  • demo: update fonts with ligatures support (64b10d6)
  • json template and template cache string: add json built-in template, and templateCacheString which defaults to unix timestamp. (1e43bba)
  • ligatures: add ligature support (9106a13)
  • standalone: add support to JSON template and cacheString (ad64421)
  • templates: add cacheString support (f39d2c1)
  • templates: add template for JSON (3b704b3)
  • add stylus template (588a753)
  • allow adding a hash in generated font file name (@brunoroux) (1913c9a)
  • allow to add a hash in generated font file name (6ca0d9e)
  • respect formats option when using external config (29769b0)

Bug Fixes

  • cli: use fs instead of fs-extra for writing files (e7bf9f5)
  • demo: adopt font-display: block for a little better icon font loading (0f9dbc0)
  • deps: [Snyk] Fix for 1 vulnerabilities (6e08d33), closes #197
  • deps: [Snyk] Fix for 1 vulnerable dependencies (6e9e243), closes #188
  • deps: [Snyk] Security upgrade meow from 5.0.0 to 6.1.0 (b5f9021), closes #195
  • deps: fix insecure dependency (59a9604)
  • deps: fix/sort deps and scripts after npm audit fix (cf5cfc4), closes #209
  • deps: fix/sort deps and scripts after npm audit fix (4118fcf)
  • deps: pin/upgrade eslint dependencies, fix some errors (9206e72), closes #225
  • deps: sort/upgrade dependencies after npm audit fix (0f976a9), closes #220
  • deps: sort/upgrade dependencies after npm audit fix (a1ba509)
  • deps: upgrade eslint dependencies and plugins (1e5a5ca)
  • templates: adopt font-display: block for better icon font loading (4669891)
  • .snyk, package.json & package-lock.json to reduce vulnerabilities (2e387cc)
  • package.json & package-lock.json to reduce vulnerabilities (b02f52f)
  • package.json, package-lock.json & .snyk to reduce vulnerabilities (c805812)
  • package.json, package-lock.json & .snyk to reduce vulnerabilities (791e5f4)
  • package.json, package-lock.json & .snyk to reduce vulnerabilities (8f037d5)

9.0.0 - 2019-04-19

  • Changed: drops support for Node.js 6.
  • Feat: improve basic templates (see templates).
  • Feat: use wasm package for generate woff2

8.2.1 - 2018-12-28

  • Chore: update dependencies.

8.2.0 - 2018-11-13

  • Added: sort option (and --no-sort flag for CLI).
  • Chore: update dependencies.

8.1.4 - 2018-06-05

  • Chore: minimum require svgicons2svgfont version is now ^9.0.3 (this fix compatibility with nodejs@10).

8.1.3 - 2018-04-25

  • Chore: minimum require meow version is now ^5.0.0.
  • Chore: minimum require cosmiconfig version is now ^5.0.3.
  • Chore: minimum require fs-extra version is now ^6.0.1.
  • Chore: drop merge-deep in favor lodash.merge.

8.1.2 - 2018-04-23

  • Fixed: deterministic output (i.e. each glyph in font have same unicode in font).

8.1.1 - 2018-03-22

  • Fixed: always configure nunjucks for template path.

8.1.0 - 2018-03-22

  • Feat: export config path when he is using (in result.config.filePath).

8.0.0 - 2018-03-21

  • Added: demo directory (thanks for @shogo).
  • Changed: export glyphsData instead foundFiles (glyphsData contain all information about any glyph).
  • Changed: result.styles rename to result.template (for API).
  • Changed: dest by default process.cwd().
  • Changed: rename dest-styles CLI option to dest-template.
  • Changed: rename cssTemplateClassName to templateClassName.
  • Changed: rename cssTemplateFontPath to templateFontPath.
  • Changed: rename cssTemplateFontName to templateFontName.
  • Changed: glyphTransformFn should always return glyph metadata.
  • Changed: minimum required nodejs version is now 6.9.5 (see svgicons2svgfont.
  • Changed: use globby@8 (based on fast-glob, it is better perf).
  • Chore: minimum used svgicons2svgfont package is now ^9.0.2
  • Chore: minimum used cosmiconfig package is now ^4.0.0
  • Fixed: don't use globby for getting build-in tempalte (better perf).
  • Fixed: always add trailing slash to templateFontPath.

7.1.4 - 2017-05-24

  • Fixed: use copyright, ts and version with null value by default, it is avoid problems when your use long term caching.
  • Fixed: options for ttf font generation now correctly handles.

7.1.3 - 2017-04-13

  • Fixed: search config if not present in CLI arguments.

7.1.2 - 2017-04-12

  • Fixed: template option now respected from config.

7.1.1 - 2017-03-29

  • Fixed: potential crash with memory allocation when using fs for read files.

7.1.0 - 2017-01-24

  • Added: glyphTransformFn option for transform glyph metadata before transferred in style template.

7.0.2 - 2016-12-22

  • Fixed: exit code can be not number.

7.0.1 - 2016-12-20

  • Fixed: arguments for svgicons2svgfont (missing font prefix).
  • Chore: improved output of help in CLI.

7.0.0 - 2016-11-09

  • Added: template option instead css, cssFormat, srcCssTemplate.
  • Added: destStyles options instead destCssTemplate.
  • Added: styles property to result.
  • Fixed: throw error on empty svg files.
  • Removed: css option.
  • Removed: cssFormat option.
  • Removed: srcCssTemplate option.
  • Removed: css property from result.
  • Removed: destCssTemplate argument from cli.
  • Tests: improved tests (relative and absolute path to template).

6.0.4 - 2016-11-08

  • Fixed: regression bug with passed arguments to template.

6.0.3 - 2016-11-08

  • Fixed: validate xml of glyphs.
  • Chore: minimum required eslint-plugin-ava version is now ^2.2.0.
  • Chore: minimum required eslint-plugin-itgalaxy version is now ^26.0.0.
  • Chore: minimum required eslint-plugin-jsx-a11y version is now ^3.0.0.
  • Chore: minimum required eslint-plugin-react version is now ^6.6.0.
  • Chore: refactoring code.

6.0.2 - 2016-11-07

  • Fixed: use reject instead Promise.reject in glyphs error callback.
  • Fixed: use callback finish instead end for svgicons2svgfont stream.
  • Tests: improve tests on bad examples.

6.0.1 - 2016-11-07

  • Fixed: add error event to glyph stream.
  • Fixed: don't create new Error where this is not necessary.
  • Chore: minimum required nunjucks version is now ^3.0.0.
  • Chore: minimum required eslint version is now ^3.9.1.
  • Chore: minimum required eslint-plugin-ava version is now ^4.0.0.
  • Chore: minimum required eslint-plugin-itgalaxy version is now ^25.0.0.
  • Chore: minimum required eslint-plugin-node version is now ^3.0.0.
  • Chore: minimum required eslint-plugin-promise version is now ^3.3.0.
  • Chore: minimum required eslint-plugin-react version is now ^6.5.0.
  • Tests: improve tests on bad examples.

6.0.0 - 2016-10-26

  • Added: support nodejs 7.
  • Added: verbose argument for verbose output.
  • Remove: quite argument.
  • Chore: improve README.md.
  • Chore: improve description in package.json.

5.0.0 - 2016-10-24

  • Fixed: wrong CSS syntax when not all format are selected.
  • Chore(SEMVER-MAJOR): rename extension for all templates from nunjucks to njk.

4.0.1 - 2016-10-19

  • Fixed: CLI fontName and formats arguments bug.
  • Chore: minimum required ajv-cli version is now ^1.1.0.
  • Chore: minimum required remark-preset-lint-itgalaxy version is now ^2.0.0.
  • Chore: minimum required nunjucks from 2.0.0 to 2.5.0.
  • Chore: minimum required eslint-plugin-import version is now ^2.0.0.
  • Chore: minimum required eslint-plugin-promise version is now ^3.0.0.
  • Chore: minimum required eslint-plugin-lodash version is now ^2.1.0.
  • Chore: rename eslint-plugin-xo to eslint-plugin-unicorn.
  • Chore: minimum required eslint-plugin-unicorn version is now ^1.0.0.
  • Chore: minimum required eslint-plugin-itgalaxy version is now ^23.0.0.
  • Chore: minimum required cosmiconfig version is now ^2.0.0.

4.0.0

  • Changed: all style templates for font now have nunjucks extension.
  • Chore(package): remove extra files from package.json.
  • Chore(package): install all peerDependencies for eslint-plugin-itgalaxy.
  • Chore(package): update a minimal version ava from 0.15.0 to 0.16.0.
  • Chore(package): update a minimal version eslint-plugin-ava from 2.5.0 to 3.0.0.
  • Chore(package): update a minimal version npm-run-all from 2.3.0 to 3.0.0.
  • Chore(package): update a minimal version eslint-plugin-itgalaxy from 8.0.0 to 11.0.0.
  • Chore(package): update a minimal version nyc from 7.0.0 to 8.0.0.
  • Chore(package): remove nyc settings, now fine works without their.
  • Chore(package): use ^ instead ~ from babel-preset-stage-0.
  • Chore(package): use remark-preset-lint-itgalaxy instead remark-lint-config-itgalaxy.
  • Chore(package): use right version for eslint-plugin-* and eslint.
  • Chore: improved README.md.
  • Chore: fix glob pattern for lint:remark script command.

3.0.1

  • Fixed: --css-template-font-path now get also from cosmiconfig.

3.0.0

  • Added: support cosmiconfig.
  • Changed: change function arguments in standalone.
  • Chore: refactoring.
  • Chore: sorting alphabetically dependencies and devDependencies.
  • Chore: remove unused eslint-* plugins from devDependencies.
  • Chore: update minimal version eslint-plugin-itgalaxy to 8.0.0.
  • Chore: check is valid fonts in tests.
  • Chore: add more tests.
  • Chore: sharable config for remark-lint.
  • Chore: add nodejs v5 to .travis.yml.

2.0.3

  • Chore: improved description and keywords in package.json.

2.0.2

  • Fixed: svg2ttf now correctly generates ttf font.
  • Fixed: ttf2eot now correctly generates eot font.
  • Fixed: ttf2woff now correctly generates woff font.
  • Fixed: svg2ttf now correctly accepts option.
  • Chore: more readable name tests.
  • Chore: rename extension templates.

2.0.1

  • Chore: update globby to 6.0.0.
  • Chore: update minimal version babel-cli to 6.11.0.
  • Chore: update minimal version babel-core to 6.11.0.
  • Chore: update eslint-plugin-itgalaxy to 6.0.0.

2.0.0

  • Added: --src-css-template agument.
  • Added: --css-template-class-name argument.
  • Added: --css-template-font-path argument.
  • Added: --css-template-font-name argument.
  • Changed: remove --css-template-format argument, now format is taken from --dest-css-template.
  • Changed: remove --css argument, css now generated if you use --dest-css-template argument.
  • Changed: rename --css-template-dest argument to --dest-css-template.
  • Remove: --css-template argument.

1.0.1

  • Fixed: get fontId from fontName, if fontId is null or undefined.

1.0.0

  • Initial release.