Code Monkey home page Code Monkey logo

subset-iconfont's Introduction

subset-iconfont

NPM version Node.js CI codecov

Subset multiple iconfont packages and generate CSS/SCSS.

Features

  • Subset from multiple iconfont npm packages, including FontAwesome Free fonts, Material Design Icons, Bootstrap Icons and Google Material Icons;
  • Supported output font formats: woff2, woff, eot, ttf;
  • Auto generate font LICENSE files for font used;
  • Auto generate FontAwesome styled CSS/SCSS, which means the subset icons can be used like FontAwesome icons, under FontAwesome free license;
  • Auto generate metadata of icons used;
  • Allow customizing (i.e., removing unwanted) FontAwesome CSS/SCSS components;
  • Tested on linux and windows.

Installation

npm install --save-dev subset-iconfont

Basic Usage

Subset from multiple iconfonts, and combine the css/scss files (Combination mode, see Standalone mode below), for example:

// npm install -D @fortawesome/fontawesome-free @mdi/font @mdi/svg

import { subsetIconfont, MdiProvider, FaFreeProvider } from 'subset-iconfont';

const mdi = new MdiProvider(['plus', 'circle', 'check']),
  fa = new FaFreeProvider(['clock', '500px']);

subsetIconfont([mdi, fa], './outputDir', { formats: ['ttf', 'woff2'] }).then(
  (result) => {
    console.log('Done!');
  }
);

Demo

Run

npm install --save-dev @fortawesome/fontawesome-free @mdi/font @mdi/svg bootstrap-icons material-icons @material-design-icons/svg
npm run demo

See results in output and output-standalone, and open the index.html files in browser to see the usage of the generated icons.

ProviderConstructor

The process runs on a list of ProviderInstance, or ProviderConstructor instances. ProviderConstructors are constructors we created for the purpose of subsetting different iconfont in a consistent way. Available ProviderConstructors include:

  • FaFreeProvider
  • Fa4Provider
  • MdiProvider
    • Source: Material Design Icons by Austin Andrews
    • License: MIT license
    • Required npm packages: @mdi/font and @mdi/svg
    • Note: many of the icons have a outline variant, which will be included in the result when subsetting. For example, plus-outline will also included if plus is to be subset. We do not use a outline style because the variants were assigned different unicode values.
  • BiProvider
    • Source: Bootstrap Icons by The Bootstrap Authors
    • License: MIT license
    • Required npm package: bootstrap-icons.
    • styles: outlined, filled, rounded, sharp and two-tone
  • MiProvider

Note: For provider with styles properties, the program will extract all available styles of that icon.

The syntax to create a ProviderInstance is:

ProviderConstructor(subset: subsetItem[], options?: ProviderOptions)

subset

  • Type: array of string
  • Descriptions: the icon names that are expected to be subset from the provider. Non-existing icons will be ignored and warn in the log.
  • Note: To extract all the icons available, use ['__all__'] as the subset.

options

  • Type: ProviderOptions

Allowed members:

formats

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

fontName

  • Type: string
  • Default: the value from the provider.
  • Description: The font family name you want to use.

fontFileName

  • Type: string
  • Default: The value from the provider.
  • Description: A string which can be used a file name, which will be used as:
    • The basename of the font file, for provider without multiple styles;
    • The basename of the css file which included the main style and definitions of icons;
    • The major part of the basename of the font-face css file, for providers without multiple styles;
  • Note: For providers with multiple styles, the font file name will be the concatenation of cssPrefix property of the provider, style used and fontWeight of that style, while the font-face file base name will be the concatenation of cssPrefix and style.

prefix

  • Type: string
  • Default: The value from the provider.
  • Description: The prefix in the css class when using the icon.

webfontDir

  • Type: string
  • Default: webfonts
  • Description: The name of the sub-directory where the font files are expected be written to.

generateMinCss

  • Type: boolean
  • Default: true
  • Description: Whether generate a minified css version for each css files generated.

generateCssMap

  • Type: boolean
  • Default: true
  • Description: Whether generate a .map file for each css files generated.

LoggerOptions

  • Type: winston.LoggerOptions
  • Default: {level: 'warn'}
  • Description: The options of winston logger. We implemented a Console transport, but that can be extended. See winston for more information.

writeOutFiles

  • Type: array
  • Default: ['webfonts', 'scss', 'css', 'licenses', 'web', 'metadata']
  • Description: The categories of files which will be physically written to disk:
    • webfonts: The font files generated
    • scss: The scss files
    • css: The css files
    • license: The LICENSE file of the font used
    • web: An index.html file which presenting all the subset icons in a web page
    • metadata: The metadata of all the subset icons

addHashInFontUrl

  • Type: boolean
  • Default: true
  • Description: Whether adding font file hash in font face CSS/SCSS file. This is useful to update client caching of font files.

cssChoices

  • Type: array
  • Default: ['sizing', 'fixed-width', 'list', 'bordered', 'animated', 'rotated', 'flipped', 'stacked', 'inverse', 'screen-reader']
  • Description: The FontAwesome CSS/SCSS components that will be generated. This is useful when you want to minimize the generated results. For example, you can set the value to [] if you don't need extra CSS features provided by FontAwesome.

API

Standalone mode

ProviderInstance.makeFonts(outputDir): Promise<MakeFontResult>

By Standalone, we mean subset from a ProviderInstance.

outputDir

  • Type: string
  • Description: A path string where the output files are expected to be written to.

For example:

// npm install -D @mdi/font @mdi/svg

import { MdiProvider } from 'subset-iconfont';

const mdi = new MdiProvider(['plus', 'circle', 'check'], {
  formats: ['ttf', 'woff2'],
});

mdi.makeFonts('./outputDir').then((result) => {
  console.log('Done!');
});

Combination mode

subsetIconfont(providerInstances: ProviderInstance[], outputDir: string, options?: SubsetOptions): Promise<MakeFontResult>

providerInstances

  • Type: array
  • Description: An array of ProviderInstance instances

outputDir

  • Type: string
  • Description: A path string where the output files are expected to be written to.

options

  • Type: SubsetOptions

The options of type SubsetOptions largely replicates the options (of type ProviderOptions) when creating a ProviderInstance , with an extra option outputPackages.

outputPackages
  • Type: boolean
  • Default: false
  • Description: Whether output the subset result of each iconfont. If true, there will be a packages directory which contains all the subset results of the combined packages.

If not configured, fontName will use "Subset Iconfont", and prefix will use "si", and fontFileName will use "subset-iconfont", as default value.

Notice: fontName, prefix under combination mode will override the options set in ProviderInstances so that we can use all subset icons in a consistent way irrespective of which icon comes from which provider (package).

Result

  • Type: MakeFontResult

Members include:

prefix

  • Type: string
  • Description: The css class prefix when using the generated CSS/SCSS.

fontName

  • Type: string
  • Description: see the description of fontName in ProviderOptions.

formats

  • Type: array of string
  • Description: the format of fonts generated.

fontFileName

  • Type: string
  • Description: see the description of fontFileName in ProviderOptions.

icons

  • Type: object
  • Description: The metadata of all icons from the subset process.

webfontDir

  • Type: string
  • Description: The name of the sub-directory where the font files are expected to exist in.

generateMinCss

Same as generateMinCss in ProviderOptions.

generateSourceMap

Same as generateSourceMap in ProviderOptions.

license

  • Type: string or null
  • Description: The content of the font license in standalone mode (for a single subsetProvider). The value will be null under combination mode.

logger

  • Type: winston Logger object
  • Description: The logger used in the process

blobObject

  • Type: array of objects
  • Description: Each of the object contains the generated category (in terms of webfonts, SCSS, CSS and font LICENSES) of the blob, and the information including the dir, name and data (string or Buffer).

writeOutFiles

See writeOutFiles in ProviderOptions.

fontWeightDefault

  • Type: string or number
  • Description: The default fontWeight used in the generated CSS.

Usage of Icons generate

See generated index.html and FontAwesome free documentation for details

Roadmap

  • Extend the use case beyond commonJS, for example, react.js, and that's why we've prepared the glyph data of each icon;
  • More tests;
  • Improved docs;
  • Better webpage (index.html) presentation, include search and select abilities.

Contribution

Feel free to contribute under the MIT license.

Changelog

Check our Changelog

License

Check our License

subset-iconfont's People

Contributors

dependabot[bot] avatar dzhuang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

subset-iconfont's Issues

Using Google Material Icons 'nice name' instead of code point.

Hello.

Just wondering if there's any way to use the nice name for google material icons, instead of the code point?

image

So, using "verified_user" instead of "e8e8" to output the icon.

Sorry, I'm not too familiar with the anatomy of a font file.

Many thanks!

Material Icons: Some icon names fail to resolve to SVG, e.g. 'info_outline'

Some 'filled' style icons actually have an outline variant.

Example:
info_outline

See:
https://fonts.google.com/icons?icon.style=Filled&icon.set=Material+Icons&icon.query=info_outline

delete_outline, help_outline, etc. are other examples

These don't seem to be getting correctly parsed. I think it's likely because there may need to be some logic to detect an '_outline' suffix in this case and redirect to the 'outline' ones for the resource?

In this case, the expectation would be that this icon appears in the 'filled' woff2.

node:fs:600
  handleErrorFromBinding(ctx);
  ^

Error: ENOENT: no such file or directory, open 'node_modules/@material-design-icons/svg/filled/info_outline.svg'
    at Object.openSync (node:fs:600:3)
    at readFileSync (node:fs:468:35)
    at /Users/a/fonts/subset-iconfont-1.0.2/dist/providers/material_icons.js:78:51
    at Array.forEach (<anonymous>)
    at MiProvider.normalizeIconMeta (/Users/a/fonts/subset-iconfont-1.0.2/dist/providers/material_icons.js:77:31)
    at MiProvider.validateIconMeta (/Users/a/fonts/subset-iconfont-1.0.2/dist/providers/base.js:98:31)
    at get subsetMeta [as subsetMeta] (/Users/a/fonts/subset-iconfont-1.0.2/dist/providers/base.js:147:43)
    at MiProvider.makeFonts (/Users/a/fonts/subset-iconfont-1.0.2/dist/providers/base.js:264:56)
    at Object.<anonymous> (/Users/a/fonts/subset-iconfont-1.0.2/dist/demo-standalone.js:161:4)
    at Module._compile (node:internal/modules/cjs/loader:1254:14) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: 'node_modules/@material-design-icons/svg/filled/info_outline.svg'
}

Feature request: option to provide server path to fonts

Thanks a lot for this project!

I am integrating into vite/React project and want to put my CSS in one place and fonts in another, so fonts will be located in /fonts/xxxxx.woff2 during build. The CSS that is generated currently references font files using ../webfonts/xxxxx.woff2. It would be good to have an option to specify the relative (or server-relative) path, for example:

const mdi = new MiProvider(['comment'], {
    formats: ['ttf', 'woff2'],
    generateMinCss: false,
    writeOutFiles: ['webfonts', 'css'],
    cssChoices: [],
    webfontServerPath: "/fonts" // default '../webfonts'
    prefix
});

Some icons are not added with MiProvider

For example 'add-circle' outline variant is not added:

import { MiProvider} from 'subset-iconfont';

const mi = new MiProvider([
  'pause-circle', 
  'play-circle', 
  'settings', 
  'light-mode', 
  'dark-mode', 
  'expand-less', 
  'add-box',
  'add-circle',
  'remove-circle',
  'close',
  'check-box-outline-blank',
  'check-box',
  'star'
  ], {
  formats: ['ttf', 'woff', 'woff2'],
});

mi.makeFonts('./font-output').then((result) => {
  console.log('Done!');
});

image

and there is icon from google site https://fonts.google.com/icons?icon.query=add_circle

Material Icons seems to be missing many common icons, e.g. 'arrow_back'

Hello,

Thank you for this functionality.

I'm simply trying to extract a subset of Material Icons to a woff2 file. Things generally seem to be working fine, and some icons export properly.

I'm using 'npm run demo' with custom modified icon subset in demo-standalone.js modified directly in /dist folder after running build.

Many icons appear to be missing, here are examples:
[MiProvider] warn: "arrow_back" is ignored, reason: "arrow_back" does not exists in metadata available.
[MiProvider] warn: "rotate_90_degrees_ccw" is ignored, reason: "rotate_90_degrees_ccw" does not exists in metadata available.
[MiProvider] warn: "skip_previous" is ignored, reason: "skip_previous" does not exists in metadata available.
[MiProvider] warn: "skip_next" is ignored, reason: "skip_next" does not exists in metadata available.
[MiProvider] warn: "folder_zip" is ignored, reason: "folder_zip" does not exists in metadata available.
[MiProvider] warn: "file_download" is ignored, reason: "file_download" does not exists in metadata available.
[MiProvider] warn: "more_vert" is ignored, reason: "more_vert" does not exists in metadata available.

But, all of these show up correctly in @marella's demo page,
https://marella.me/material-icons/demo/

e.g., typing in 'arrow_back' into the search there clearly shows the icon.

I've gone through the code, and my understanding is that we link dynamically to the latest material-icons that was installed form NPM (and the SVGs, too).

The only thing I can think of is, the demo is using very old versions of the material icon libraries. Checking in node_modules, I see:

128352 Feb 18 18:15 material-icons.woff2

so these seem to be written fairly recently, but I'm wondering if they're old versions in case material-icons has not been updated. But it seems to be.

In any case, I must be doing something wrong. If not, then we probably need to make sure we're using the latest icon libraries.

Can you offer any suggestions for me?

Thank you

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.