Minifant la police de manière transparente
$ npm install --save fontminFontmin V2.x ne prend en charge que les modules ES et exécutez sur le nœud v16 +.
Si vous devez utiliser la version CommonJS, veuillez installer fontmin v1.x :
Installation de NPM - Fontmin Save @ 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 ...> }
} ) ;Vous pouvez utiliser Gulp-Rename pour renommer vos fichiers:
import Fontmin from 'fontmin' ;
const rename = require ( 'gulp-rename' ) ;
const fontmin = new Fontmin ( )
. src ( 'fonts/big.ttf' )
. use ( rename ( 'small.ttf' ) ) ; Crée une nouvelle instance Fontmin .
Type: Array|Buffer|String
Définissez les fichiers pour être optimisés. Prend un tampon, une chaîne globale ou un tableau de chaînes de globe comme argument.
Type: String
Définissez le dossier de destination sur l'endroit où vos fichiers seront écrits. Si vous ne définissez aucune destination, aucun fichier ne sera écrit.
Type: Function
Ajoutez un plugin à la pile de middleware.
Type: Function
Optimisez vos fichiers avec les paramètres donnés.
Le rappel renvoie un tableau de fichiers vinyle dans files et un flux lisible / écrivable dans stream
Les plugins suivants sont regroupés avec Fontmin:
Comprimer TTF par Glyph.
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . glyph ( {
text : '天地玄黄 宇宙洪荒' ,
hinting : false // keep ttf hint info (fpgm, prep, cvt). default = true
} ) ) ;Convertir TTF en eot.
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2eot ( ) ) ;Convertissez TTF en woff.
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2woff ( {
deflate : true // deflate woff. default = false
} ) ) ;Convertissez TTF en WOFF2.
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2woff2 ( ) ) ;Convertissez TTF en SVG.
Vous pouvez utiliser ImageMemin-SVGO pour compresser SVG:
import Fontmin from 'fontmin' ;
const svgo = require ( 'imagemin-svgo' ) ;
const fontmin = new Fontmin ( )
. use ( Fontmin . ttf2svg ( ) )
. use ( svgo ( ) ) ;Générez CSS à partir de TTF, souvent utilisé pour faire 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
} ) ) ; Alternativement, une fonction de transformation peut être transmise comme option fontFamily .
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. use ( Fontmin . css ( {
// ...
fontFamily : function ( fontInfo , ttf ) {
return "Transformed Font Family Name"
} ,
// ...
} ) ) ;Convertir le format de police SVG en TTF.
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. src ( 'font.svg' )
. use ( Fontmin . svg2ttf ( ) ) ;Concat les fichiers SVG à un TTF, tout comme CSS Sprite.
Travail génial avec le plugin CSS:
import Fontmin from 'fontmin' ;
const fontmin = new Fontmin ( )
. src ( 'svgs/*.svg' )
. use ( Fontmin . svgs2ttf ( 'font.ttf' , { fontName : 'iconfont' } ) )
. use ( Fontmin . css ( {
glyph : true
} ) ) ;Convertir OTF en 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 Vous pouvez utiliser curl pour générer une police pour les sites Web fonctionnant sur PHP, ASP, Rails et plus:
$ text= ` curl www.baidu.com ` && fontmin -t " $text " font.ttfOu vous pouvez utiliser du Text HTML pour le rendre plus petit:
$ npm install -g html-to-text
$ text= ` curl www.baidu.com | html-to-text ` && fontmin -t " $text " font.ttf De plus, vous pouvez utiliser Phantom-Fetch-Cli pour générer une police pour le modèle JS en cours d'exécution SPA :
$ npm install -g phantom-fetch-cli
$ text= ` phantom-fetch http://www.chinaw3c.org ` && fontmin -t " $text " font.ttfMIT © Fontmin