UNPKG

12.2 kBMarkdownView Raw
1# webfont
2
3[![NPM version](https://img.shields.io/npm/v/webfont.svg)](https://www.npmjs.org/package/webfont)
4[![Travis Build Status](https://img.shields.io/travis/itgalaxy/webfont/master.svg?label=build)](https://travis-ci.org/itgalaxy/webfont)
5[![Build status](https://ci.appveyor.com/api/projects/status/a8absovr2r44w1oc?svg=true)](https://ci.appveyor.com/project/evilebottnawi/webfont)
6
7Generator of fonts from SVG icons.
8
9## Features
10
11- Supported font formats: `WOFF2`, `WOFF`, `EOT`, `TTF` and `SVG`;
12- Support config files: use a `JavaScript`, `JSON` or `YAML` file to specify configuration information for an entire directory and all of its subdirectories;
13- Support all popular browsers, including IE8+;
14- Allows using custom templates (example `css`, `scss`, [`styl`](https://github.com/itgalaxy/webfont/pull/164/) etc);
15- No extra dependencies as `gulp`, `grunt` or other big tools;
16- Tested on all platforms (`linux`, `windows` and `osx`);
17- CLI;
18- [Webpack plugin](https://github.com/itgalaxy/webfont-webpack-plugin).
19
20## Table Of Contents
21
22- [Webfont](#webfont)
23 - [Installation](#installation)
24 - [Usage](#usage)
25 - [Options](#options)
26 - [svgicons2svgfont](#svgicons2svgfont)
27- [Command Line Interface (CLI)](#command-line-interface)
28 - [Installation](#cli-installation)
29 - [Usage](#cli-usage)
30 - [Exit Codes](#cli-exit-codes)
31- [Related](#related)
32- [Roadmap](#roadmap)
33- [Contribution](#contribution)
34- [Changelog](#changelog)
35- [License](#license)
36
37---
38
39## Installation
40
41```shell
42npm install --save-dev webfont
43```
44
45## Usage
46
47```js
48import webfont from "webfont";
49
50webfont({
51 files: "src/svg-icons/**/*.svg",
52 fontName: "my-font-name",
53})
54 .then((result) => {
55 // Do something with result
56 Function.prototype(result);
57 // Or return it
58 return result;
59 })
60 .catch((error) => {
61 throw error;
62 });
63```
64
65or
66
67```js
68const webfont = require("webfont").default;
69
70webfont({
71 files: "src/svg-icons/**/*.svg",
72 fontName: "my-font-name",
73})
74 .then((result) => {
75 // Do something with result
76 Function.prototype(result);
77 // Or return it
78 return result;
79 })
80 .catch((error) => {
81 throw error;
82 });
83```
84
85### Options
86
87#### `files`
88
89- Type: `string` | `array`
90- Description: A file glob, or array of file globs. Ultimately passed to [fast-glob](https://github.com/mrmlnc/fast-glob) to figure out what files you want to get.
91- Note: `node_modules` and `bower_components` are always ignored.
92
93#### `configFile`
94
95- Type: `string`
96- Description: Path to a specific configuration file `(JSON, YAML, or CommonJS)` or the name of a module in `node_modules` that points to one.
97- Note: If you do not provide `configFile`, webfont will search up the directory tree for configuration file in the following places, in this order:
98 1. a `webfont` property in `package.json`
99 2. a `.webfontrc` file (with or without filename extension: `.json`, `.yaml`, and `.js` are available)
100 3. a `webfont.config.js` file exporting a JS `object`.
101 The search will begin in the working directory and move up the directory tree until it finds a configuration file.
102
103#### `fontName`
104
105- Type: `string`
106- Default: `webfont`
107- Description: The font family name you want.
108
109#### `formats`
110
111- Type: `array`,
112- Default: `['svg', 'ttf', 'eot', 'woff', 'woff2']`,
113- Possible values: `svg, ttf, eot, woff, woff2`,
114- Description: Font file types to generate.
115
116#### `template`
117
118- Type: `string`
119- Default: `null`
120- Possible values: `css`, `scss`, [`styl`](https://github.com/itgalaxy/webfont/pull/164/) (feel free to contribute more).
121- Note: If you want to use a custom template use this option pass in a path `string` like this:
122
123 ```js
124 webfont({
125 template: "./path/to/my-template.css",
126 });
127 ```
128
129 Or
130
131 ```js
132 webfont({
133 template: path.resolve(__dirname, "./my-template.css"),
134 });
135 ```
136
137 Or
138
139 ```js
140 webfont({
141 template: path.resolve(__dirname, "./my-template.styl"),
142 });
143 ```
144
145#### `templateClassName`
146
147- Type: `string`
148- Default: `null`
149- Description: Default font class name.
150
151#### `templateFontPath`
152
153- Type: `string`
154- Default: `./`
155- Description: Path to generated fonts in the `CSS` file.
156
157#### `templateFontName`
158
159- Type: `string`
160- Default: Gets is from `fontName` if not set, but you can specify any value.
161- Description: Template font family name you want.
162
163#### `glyphTransformFn`
164
165- Type: `function`
166- Default: `null`
167- Description: If you want to transform glyph metadata `(e.g. titles of CSS classes)` before transferring it in your style template for your icons, you can use this option with glyphs metadata object.
168- Example:
169
170 ```js
171 import webfont from "webfont";
172
173 webfont({
174 files: "src/svg-icons/**/*.svg",
175 glyphTransformFn: (obj) => {
176 obj.name += "_transform";
177
178 return obj;
179 },
180 })
181 .then((result) => {
182 // Do something with result
183 Function.prototype(result);
184 // Or return it
185 return result;
186 })
187 .catch((error) => {
188 throw error;
189 });
190 ```
191
192#### `sort`
193
194- Type: `bool`
195- Default: `true`
196- Description: Whether you want to sort the icons sorted by name.
197
198---
199
200## svgicons2svgfont
201
202### svgicons2svgfont options
203
204These can be appended to [webfont options](#options). These are passed directly to [svgicons2svgfont](https://github.com/nfroidure/svgicons2svgfont).
205
206#### `svgicons2svgfont.fontName`
207
208- Type: `string`
209- Default: Taken from the [webfont fontName option](#fontname)
210- Description: The font family name you want.
211
212#### `svgicons2svgfont.fontId`
213
214- Type: `string`
215- Default: The `fontName` value
216- Description: The font id you want.
217
218#### `svgicons2svgfont.fontStyle`
219
220- Type: `string`
221- Default: `''`
222- Description: The font style you want.
223
224#### `svgicons2svgfont.fontWeight`
225
226- Type: `string`
227- Default: `''`
228- Description: The font weight you want.
229
230#### `svgicons2svgfont.fixedWidth`
231
232- Type: `boolean`
233- Default: `false`
234- Description: Creates a monospace font of the width of the largest input icon.
235
236#### `svgicons2svgfont.centerHorizontally`
237
238- Type: `boolean`
239- Default: `false`
240- Description: Calculate the bounds of a glyph and center it horizontally.
241
242#### `svgicons2svgfont.normalize`
243
244- Type: `boolean`
245- Default: `false`
246- Description: Normalize icons by scaling them to the height of the highest icon.
247
248#### `svgicons2svgfont.fontHeight`
249
250- Type: `number`
251- Default: `MAX(icons.height)`
252- Description: The outputted font height (defaults to the height of the highest input icon).
253
254#### `svgicons2svgfont.round`
255
256- Type: `number`
257- Default: `10e12` Setup SVG path rounding.
258
259#### `svgicons2svgfont.descent`
260
261- Type: `number`
262- Default: `0`
263- Description: The font descent. It is useful to fix the font baseline yourself.
264- Warning: The descent is a positive value!.
265
266#### `svgicons2svgfont.ascent`
267
268- Type: `number`
269- Default: `fontHeight - descent`
270- Description: The font ascent. Use this options only if you know what you're doing. A suitable value for this is computed for you.
271
272#### `svgicons2svgfont.metadata`
273
274- Type: `string`
275- Default: `undefined`
276- Description: The font [metadata](http://www.w3.org/TR/SVG/metadata.html).
277 You can set any character data in, but this is the recommended place for a copyright mention.
278
279#### `svgicons2svgfont.log`
280
281- Type: `function`
282- Default: `console.log`
283- Description: Allows you to provide your own logging function. Set to `function(){}` to disable logging.
284
285---
286
287## Command Line Interface
288
289The interface for command-line usage is fairly simplistic at this stage, as seen in the following usage section.
290
291### CLI Installation
292
293Add the `cli` script to your `package.json` file's `scripts` object:
294
295```json
296{
297 "scripts": {
298 "webfont": "node node_modules/webfont/dist/cli.js"
299 }
300}
301```
302
303If you're using cross-env:
304
305```json
306{
307 "scripts": {
308 "webfont": "cross-env node_modules/webfont/dist/cli.js"
309 }
310}
311```
312
313### CLI Usage
314
315```shell
316 Usage: webfont [input] [options]
317
318 Input: File(s) or glob(s).
319
320 If an input argument is wrapped in quotation marks, it will be passed to "fast-glob"
321 for cross-platform glob support.
322
323 Options:
324
325 --config
326
327 Path to a specific configuration file (JSON, YAML, or CommonJS)
328 or the name of a module in \`node_modules\` that points to one.
329 If no \`--config\` argument is provided, webfont will search for
330 configuration files in the following places, in this order:
331 - a \`webfont\` property in \`package.json\`
332 - a \`.webfontrc\` file (with or without filename extension:
333 \`.json\`, \`.yaml\`, and \`.js\` are available)
334 - a \`webfont.config.js\` file exporting a JS object
335 The search will begin in the working directory and move up the
336 directory tree until a configuration file is found.
337
338 -f, --font-name
339
340 The font family name you want, default: "webfont".
341
342 -h, --help
343
344 Output usage information.
345
346 -v, --version
347
348 Output the version number.
349
350 -r, --formats
351
352 Only this formats generate.
353
354 -d, --dest
355
356 Destination for generated fonts.
357
358 -m, --dest-create
359 Create destination directory if it does not exist.
360
361 -t, --template
362
363 Type of template (\`css\`, \`scss\`, \`styl\`) or path to custom template.
364'
365 -s, --dest-template
366
367 Destination for generated template. If not passed used \`dest\` argument value.
368
369 -c, --template-class-name
370
371 Class name in css template.
372
373 -p, --template-font-path
374
375 Font path in css template.
376
377 -n, --template-font-name
378
379 Font name in css template.
380
381 --no-sort
382
383 Keeps the files in the same order of entry
384
385 --verbose
386
387 Tell me everything!.
388
389 For "svgicons2svgfont":
390
391 --font-id
392
393 The font id you want, default as "--font-name".
394
395 --font-style
396
397 The font style you want.
398
399 --font-weight
400
401 The font weight you want.
402
403 --fixed-width
404
405 Creates a monospace font of the width of the largest input icon.
406
407 --center-horizontally
408
409 Calculate the bounds of a glyph and center it horizontally.
410
411 --normalize
412
413 Normalize icons by scaling them to the height of the highest icon.
414
415 --font-height
416
417 The outputted font height [MAX(icons.height)].
418
419 --round
420
421 Setup the SVG path rounding [10e12].
422
423 --descent
424
425 The font descent [0].
426
427 --ascent
428
429 The font ascent [height - descent].
430
431 --start-unicode
432
433 The start unicode codepoint for files without prefix [0xEA01].
434
435 --prepend-unicode
436
437 Prefix files with their automatically allocated unicode codepoint.
438
439 --metadata
440
441 Content of the metadata tag.
442
443 --add-hash-in-font-url
444
445 Generated font url will be : [webfont].[ext]?v=[hash]
446
447```
448
449### CLI Exit Codes
450
451The CLI can exit the process with the following exit codes:
452
453- 0: All ok.
454- 1: Something unknown went wrong.
455- Other: related to using packages.
456
457---
458
459## Related
460
461- [Webpack plugin](https://github.com/itgalaxy/webfont-webpack-plugin) - `webpack` plugin.
462- [svgicons2svgfont](https://github.com/nfroidure/svgicons2svgfont) - Simple tool to merge multiple icons to an SVG font.
463- [svg2ttf](https://github.com/fontello/svg2ttf) - Converts SVG fonts to TTF format.
464- [ttf2eot](https://github.com/fontello/ttf2eot) - Converts TTF fonts to EOT format.
465- [ttf2woff](https://github.com/fontello/ttf2woff) - Converts TTF fonts to WOFF format.
466- [wawoff2](https://github.com/fontello/wawoff2) - Converts TTF fonts to WOFF2 and versa vice.
467
468## Roadmap
469
470- The ability to generate from any type to any type;
471- More tests, include CLI test;
472- Improved docs;
473- Reduce package size (maybe implement `ttf2woff2` with native js library);
474- Improve performance (maybe use cache for this).
475
476## Contribution
477
478Feel free to push your code if you agree with publishing under the MIT license.
479
480## Changelog
481
482Check our [Changelog](CHANGELOG.md)
483
484## License
485
486Check our [License](LICENSE)