fontmin
字体
2025/03/14 16:31:32
无缝缩小字体。
安装
npm install --save fontmin
fontmin v2.x 仅支持 ES 模块,运行在 Node v16+ 上, 如果您需要使用 CommonJS 版本:
请安装:
npm install --save fontmin@1
使用
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.src('fonts/*.ttf')
.dest('build/fonts');
fontmin.run(function (err, files) {
if (err) {
throw err;
}
console.log(files[0]);
// => { contents: <Buffer 00 01 00 ...> }
});
您可以使用 gulp-rename 重命名文件:
import Fontmin from 'fontmin';
const rename = require('gulp-rename');
const fontmin = new Fontmin()
.src('fonts/big.ttf')
.use(rename('small.ttf'));
API
new Fontmin()
创建新实例.
.src(file)
Type: Array|Buffer|String
设置需要优化的文件。接受缓冲区、glob 字符串或 glob 字符串数组 as 参数.
.dest(folder)
Type: String
将目标文件夹设置为将写入文件的位置。如果未设置 任何目标都不会写入任何文件。
.use(plugin)
Type: Function
将一个插件添加到中间件堆栈中.
.run(cb)
Type: Function
使用给定的设置优化您的文件。
cb(err, files, stream)
该回调将返回一个文件数组和一个可读/可写的 Readable/Writable stream
.
插件
以下插件与 fontmin 捆绑在一起:
- glyph — 逐个字形压缩 ttf.
- ttf2eot — 将 ttf 转换为 eot.
- ttf2woff — 将 ttf 转换为 woff.
- ttf2woff2 — 将 ttf 转换为 woff2.
- ttf2svg — 将 ttf 转换为 svg.
- css — 从 ttf 生成 css,通常用于制作 iconfont.
- svg2ttf — 将字体格式 svg 转换为 ttf.
- svgs2ttf — 将 svg 文件转换为 ttf,就像 css sprite 一样.
- otf2ttf — 将 otf 转换为 ttf.
.glyph()
逐个字形压缩 ttf
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.glyph({
text: '天地玄黄 宇宙洪荒',
hinting: false // keep ttf hint info (fpgm, prep, cvt). default = true
}));
.ttf2eot()
将 ttf 转换为 eot
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.ttf2eot());
.ttf2woff()
将 ttf 转换为 woff
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.ttf2woff({
deflate: true // deflate woff. default = false
}));
.ttf2woff2()
将 ttf 转换为 woff2
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.ttf2woff2());
.ttf2svg()
将 ttf 转换为 svg,可以使用 imagemin-svgo 来压缩 SVG
import Fontmin from 'fontmin';
const svgo = require('imagemin-svgo');
const fontmin = new Fontmin()
.use(Fontmin.ttf2svg())
.use(svgo());
.css()
从 ttf 生成 css
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.css({
fontPath: './', // location of font file
base64: true, // inject base64 data:application/x-font-ttf; (gzip font with css).
// default = false
glyph: true, // generate class for each glyph. default = false
iconPrefix: 'my-icon', // class prefix, only work when glyph is `true`. default to "icon"
fontFamily: 'myfont', // custom fontFamily, default to filename or get from analysed ttf file
asFileName: false, // rewrite fontFamily as filename force. default = false
local: true // boolean to add local font. default = false
}));
或者,可以将转换函数作为参数传递
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.use(Fontmin.css({
// ...
fontFamily: function(fontInfo, ttf) {
return "Transformed Font Family Name"
},
// ...
}));
.svg2ttf()
将字体格式 svg 转换为 ttf
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.src('font.svg')
.use(Fontmin.svg2ttf());
.svgs2ttf()
将 svg 文件连接到 ttf
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.src('svgs/*.svg')
.use(Fontmin.svgs2ttf('font.ttf', {fontName: 'iconfont'}))
.use(Fontmin.css({
glyph: true
}));
.otf2ttf()
将 otf 转换为 ttf
import Fontmin from 'fontmin';
const fontmin = new Fontmin()
.src('fonts/*.otf')
.use(Fontmin.otf2ttf());
CLI
安装
$ npm install -g fontmin
使用
$ fontmin --help
Usage
$ fontmin <file> [<output>]
$ fontmin <directory> [<output>]
$ fontmin <file> > <output>
$ cat <file> | fontmin > <output>
Example
$ fontmin fonts/* build
$ fontmin fonts build
$ fontmin foo.ttf > foo-optimized.ttf
$ cat foo.ttf | fontmin > foo-optimized.ttf
Options
-t, --text require glyphs by text
-b, --basic-text require glyphs with base chars
-d, --deflate-woff deflate woff
--font-family font-family for @font-face CSS
--css-glyph generate class for each glyf. default = false
-T, --show-time show time fontmin cost
curl
使用 curl
生成在线网站的字体:
$ text=`curl www.baidu.com` && fontmin -t "$text" font.ttf
html-to-text
使用 html-to-text:
$ npm install -g html-to-text
$ text=`curl www.baidu.com | html-to-text` && fontmin -t "$text" font.ttf
phantom-fetch-cli
$ npm install -g phantom-fetch-cli
$ text=`phantom-fetch http://www.chinaw3c.org` && fontmin -t "$text" font.ttf