fontmin
v2 support ES Modules
无缝缩小字体
$ npm install --save fontminFontmin v2.x仅支持ES模块,并在节点V16+上运行。
如果您需要使用CONCORJS版本,请安装fontmin v1.x :
npm安装 - 节省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' ) ) ; 创建一个新的Fontmin实例。
类型: Array|Buffer|String
设置要优化的文件。将缓冲区,环字符串或一系列环球字符串作为参数。
类型: String
将目标文件夹设置为写入文件的位置。如果您不设置任何目的地,则不会写任何文件。
类型: Function
将plugin添加到中间件堆栈中。
类型: Function
用给定的设置优化文件。
回调将返回files中的乙烯基文件和stream中可读/可读的流
以下插件与fontmin捆绑在一起:
通过字形压缩TTF。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . glyph ( {
text : '天地玄黄 宇宙洪荒' ,
hinting : false // keep ttf hint info (fpgm, prep, cvt). default = true
} ) ) ;将TTF转换为EOT。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2eot ( ) ) ;将TTF转换为Woff。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2woff ( {
deflate : true // deflate woff. default = false
} ) ) ;将TTF转换为WOFF2。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2woff2 ( ) ) ;将TTF转换为SVG。
您可以使用Imagemin-Svgo压缩SVG:
import Fontmin from 'fontmin' ;
const svgo = require ( 'imagemin-svgo' ) ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2svg ( ) )
. use ( svgo ( ) ) ;从TTF生成CSS,通常用于制作IconFont。
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
} ) ) ;或者,转换函数可以作为fontFamily选项传递。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . css ( {
// ...
fontFamily : function ( fontInfo , ttf ) {
return "Transformed Font Family Name"
} ,
// ...
} ) ) ;将字体格式SVG转换为TTF。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. src ( 'font.svg' )
. use ( Fontmin . svg2ttf ( ) ) ;就像CSS Sprite一样,将SVG置于TTF。
使用CSS插件的很棒的工作:
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. src ( 'svgs/*.svg' )
. use ( Fontmin . svgs2ttf ( 'font.ttf' , { fontName : 'iconfont' } ) )
. use ( Fontmin . css ( {
glyph : true
} ) ) ;将OTF转换为TTF。
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. src ( 'fonts/*.otf' )
. use ( Fontmin . otf2ttf ( ) ) ; $ 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生成字体,以用于在PHP,ASP,Rails等上运行的网站:
$ text= ` curl www.baidu.com ` && fontmin -t " $text " font.ttf或者,您可以使用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来生成用于水疗JS模板的SPA字体:
$ npm install -g phantom-fetch-cli
$ text= ` phantom-fetch http://www.chinaw3c.org ` && fontmin -t " $text " font.ttf麻省理工学院©Fontmin