Coder Social home page Coder Social logo

fontmin's Introduction

fontmin

Minify font seamlessly

NPM version Build Status Downloads Dependencies Font support

Homepage

Install

$ npm install --save fontmin

Usage

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 ...> }
});

You can use gulp-rename to rename your files:

import Fontmin from 'fontmin';
const rename = require('gulp-rename');

const fontmin = new Fontmin()
    .src('fonts/big.ttf')
    .use(rename('small.ttf'));

API

new Fontmin()

Creates a new Fontmin instance.

.src(file)

Type: Array|Buffer|String

Set the files to be optimized. Takes a buffer, glob string or an array of glob strings as argument.

.dest(folder)

Type: String

Set the destination folder to where your files will be written. If you don't set any destination no files will be written.

.use(plugin)

Type: Function

Add a plugin to the middleware stack.

.run(cb)

Type: Function

Optimize your files with the given settings.

cb(err, files, stream)

The callback will return an array of vinyl files in files and a Readable/Writable stream in stream

Plugins

The following plugins are bundled with fontmin:

  • glyph — Compress ttf by glyph.
  • ttf2eot — Convert ttf to eot.
  • ttf2woff — Convert ttf to woff.
  • ttf2woff2 — Convert ttf to woff2.
  • ttf2svg — Convert ttf to svg.
  • css — Generate css from ttf, often used to make iconfont.
  • svg2ttf — Convert font format svg to ttf.
  • svgs2ttf — Concat svg files to a ttf, just like css sprite.
  • otf2ttf — Convert otf to ttf.

.glyph()

Compress ttf by glyph.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .use(Fontmin.glyph({
        text: '天地玄黄 宇宙洪荒',
        hinting: false         // keep ttf hint info (fpgm, prep, cvt). default = true
    }));

.ttf2eot()

Convert ttf to eot.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .use(Fontmin.ttf2eot());

.ttf2woff()

Convert ttf to woff.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .use(Fontmin.ttf2woff({
        deflate: true           // deflate woff. default = false
    }));

.ttf2woff2()

Convert ttf to woff2.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .use(Fontmin.ttf2woff2());

.ttf2svg()

Convert ttf to svg.

you can use imagemin-svgo to compress svg:

import Fontmin from 'fontmin';
const svgo = require('imagemin-svgo');

const fontmin = new Fontmin()
    .use(Fontmin.ttf2svg())
    .use(svgo());

.css()

Generate css from ttf, often used to make 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
    }));

Alternatively, a transform function can be passed as fontFamily option.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .use(Fontmin.css({
        // ...
        fontFamily: function(fontInfo, ttf) {
          return "Transformed Font Family Name"
        },
        // ...
    }));

.svg2ttf()

Convert font format svg to ttf.

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .src('font.svg')
    .use(Fontmin.svg2ttf());

.svgs2ttf()

Concat svg files to a ttf, just like css sprite.

awesome work with css plugin:

import Fontmin from 'fontmin';

const fontmin = new Fontmin()
    .src('svgs/*.svg')
    .use(Fontmin.svgs2ttf('font.ttf', {fontName: 'iconfont'}))
    .use(Fontmin.css({
        glyph: true
    }));

.otf2ttf()

Convert otf to 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

you can use curl to generate font for websites running on PHP, ASP, Rails and more:

$ text=`curl www.baidu.com` && fontmin -t "$text" font.ttf

or you can use html-to-text to make it smaller:

$ npm install -g html-to-text
$ text=`curl www.baidu.com | html-to-text` && fontmin -t "$text" font.ttf

what is more, you can use phantom-fetch-cli to generate font for SPA running JS template:

$ npm install -g phantom-fetch-cli
$ text=`phantom-fetch http://www.chinaw3c.org` && fontmin -t "$text" font.ttf

Related

Thanks

License

MIT © fontmin

fontmin's People

Contributors

abeet avatar akfish avatar andersk avatar firede avatar junmer avatar kekee000 avatar mkwiser avatar moustachos avatar njleonzhang avatar patchlog avatar smhg avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fontmin's Issues

fontmin 使字体清晰度下降

未使用fontmin的字体显示
Image of 感
使用后的字体显示
Image of 感
是不是fontmin默认采用了什么字体压缩之类的,能不能设置取消?
Thanks

fontmin 通过 through2 创建输出的时候,会指数级变慢

plugins/glyph.js

            file.contents = miniTtf.buffer;
            //file.ttfObject = miniTtf.object; // 加了这个之后,在大对象时会变得特别慢

可能是因为对象拷贝的原因吧,vinyl 克隆 File 的时候效率比较低,导致复制大 ttfObject 的时候超级慢,使用 file.clone(false)可以修复

Wrong css template

https://github.com/ecomfe/fontmin/blob/master/lib/font-face.tpl#L15
there should be befor not after
generated glyph is wrong and not working

$icon-font ./src/svg/*.svg ./src/css/fonts --font-family=demo --glyph=true --font-path=fonts/

result

@font-face {
    font-family: "fontname";
    src: url("./fonts/demo.eot"); /* IE9 */
    src: url("./fonts/demo.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */
    url("./fonts/demo.woff") format("woff"), /* chrome, firefox */
    url("./fonts/demo.ttf") format("truetype"), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
    url("./fonts/demo.svg#demo") format("svg"); /* iOS 4.1- */
    font-style: normal;
    font-weight: normal;
}


[class^="icon-"],
[class*=" icon-"]:after {
    font-family: "demo";
    speak: none;
    font-style: normal;
    font-weight: normal;
    font-variant: normal;
    text-transform: none;
    line-height: 1;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}


.icon-administration:before {
    content: "\e001";
}

So if I trying to use icon like so

<i class="super duper icon-administration"></i>

it will not be displayed. Here is some demo:
http://jsbin.com/runubezegi/edit?html,css,output
try to change :after to :before it will work as it should.
Any way please improve your css template

@font-face 中添加 local("Family Name") 优先使用本地字体

目前:

@font-face {
    font-family: "FZQingKeBenYueSongS-R-GB";
    src: url("FZQKBYSJW.eot"); /* IE9 */
    src: url("FZQKBYSJW.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */

    url("FZQKBYSJW.woff") format("woff"), /* chrome, firefox */
    url("FZQKBYSJW.ttf") format("truetype"), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */

    url("FZQKBYSJW.svg#FZQingKeBenYueSongS-R-GB") format("svg"); /* iOS 4.1- */
    font-style: normal;
    font-weight: normal;
}

带上本地字体:

@font-face {
    font-family: "FZQingKeBenYueSongS-R-GB";
    src: url("FZQKBYSJW.eot"); /* IE9 */
    src: local("FZQingKeBenYueSongS-R-GB"), url("FZQKBYSJW.eot?#iefix") format("embedded-opentype"), /* IE6-IE8 */

    url("FZQKBYSJW.woff") format("woff"), /* chrome, firefox */
    url("FZQKBYSJW.ttf") format("truetype"), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */

    url("FZQKBYSJW.svg#FZQingKeBenYueSongS-R-GB") format("svg"); /* iOS 4.1- */
    font-style: normal;
    font-weight: normal;
}

cli: "no writecb in Transform class"

master 分支的 cli 坏掉了,执行后提示:

no writecb in Transform class

字体生成失败。

建议启用 develop 分支,并增加 cli 相关的测试用例~

🙈 成功占领第一个 issue!

'严'和'霾'冲突

hi,

我使用fontmin压缩字体方正兰亭纤黑简体.ttf时出错,后来发现只要字同时出现就会出错。

fontmin cli hangs on Jenkins build

Hello,

This is probably not the right question to be asked here, but I wanted to try my luck because it's driving me crazy.

We have an npm build task using the cli fontmin src/fonts/* build/, which works fine as expected. However, if I try to run this cli on my Jenkins server, the cli hangs, not even creating the destination folder "build". Both Jenkins and my local dev use the same docker image. I tried to log on to the Jenkins server, ssh into the container and execute the cli manually, it worked. Just no luck when Jenkins tries it automatically.

Has anyone run into this issue before, does the cli need any special environment variable or anything?

Much appreciated!

华康翩翩体抽取失败

我在网上下载了华康翩翩体,安装到window本地后字体属性里有一栏 “字体嵌入性:打印和预览”, 而这个系统字体里都有一栏属性“设计用于:中文(简体中文)”。不太了解字体的这些细节,打印和预览的字体不能抽取码?

How to rename the CSS file name ?

I mean by now, the generated CSS file name is the same as the .ttf file. But I need to rename the CSS file before it generates.
How can I make it ?

CSS generator template issue

font-family setted to :after selector, but icon self to :before

so in case of HTML

<span class="icon-first"></span> it works

but in case of HTML

<span class="bla bla bla icon-first"></span> 

it set only content in before but not font-family

Deformed icon for some SVGs

Hey,
First of all great library.
I'm using your library in my own library, had some weird issues I didn't have in webfonts-generator library or any svg to font site (like icomoon).

This example html produced using their library with option height: 1000:
image

this is icomoon:
image

This is yours:
image

Code:
Their -

var webfontsGenerator = require('webfonts-generator')

webfontsGenerator({
  files: [
    '../library/src/icon/create.svg',
    '../library/src/icon/published.svg',
    '../library/src/icon/modified.svg'
  ],
  dest: 'build2/',
  fontHeight: 1000
}, function(error) {
  if (error) console.log('Fail!', error)
  else console.log('Done!')
})

Yours -

var Fontmin = require('fontmin');

var fontmin = new Fontmin()
    .src('../library/src/icon/*.svg')
    .use(Fontmin.svgs2ttf('font.ttf', {fontName: 'iconfont'}))
    .use(Fontmin.css({
        glyph: true,
        local: true
    }))
    .dest('./build');
fontmin.run();

Thanks in advance

EDIT:

Here are the svgs:
http://www.filedropper.com/modified_2
http://www.filedropper.com/published

Can't minify font-awesome ttf file

Hi,

I'm trying to use gulp-fontmin to minify font-awesome's fontawesome-webfont.ttf file, but I get the following error :

Offset is outside the bounds of the DataView

I'm using gulp-fontmin 0.7.1 that uses fontmin 0.7.3.

Do you know what's going wrong ?

Doesn't work!

In my case font.ttf size is 51 KB.
After using fontmin it creates for me new .ttf font but the size is the same.
What's going wrong?

函数行为异常

我的代码里


   var fontmin = new Fontmin()
        .use(Fontmin.glyph(
            {
                text: textSet,
                hinting: false
            }))
        .src(setting.srcFontPath+oriFontName+".ttf")
        .dest(setting.desFontPath)
        .use(rename(desFontName+".ttf"));
    fontmin.run(function(err, files)
    {
        if (err)
        {
            callback(false);
        }
        callback(true);
    });

在本机linux测试无问题,上服务器linux后发现返回时间极短(应该没有生成字体,事实也没在输出路径找到字体)但在回调函数里确实没有err。

[ReferenceError: glyph is not defined]

I'm just testing with the sample code:


var Fontmin = require('fontmin');

var srcPath = 'src/font/*.ttf'; // 字体源文件
var destPath = 'asset/font';    // 输出路径
var text = '我说你是人间的四月天;笑响点亮了四面风;轻灵在春的光艳中交舞着变。';

// 初始化
var fontmin = new Fontmin()
    .src(srcPath)               // 输入配置
    .use(Fontmin.glyph({        // 字型提取插件
        text: text              // 所需文字
    }))
    .use(Fontmin.ttf2eot())     // eot 转换插件
    .use(Fontmin.ttf2woff())    // woff 转换插件     
    .use(Fontmin.ttf2svg())     // svg 转换插件
    .use(Fontmin.css())         // css 生成插件
    .dest(destPath);            // 输出配置

// 执行
fontmin.run(function (err, files, stream) {

    if (err) {                  // 异常捕捉
        console.error(err);
    }

    console.log('done');        // 成功
});

The ERR msg is

 [ReferenceError: glyph is not defined]

and I only get a ttf and a css file. And both of them are not generated correctly.

No Documentation, No Usage, No requirements, No Guides

HI, I wonder how can i use your plugin, without any documentation, examples, guides, requirements.. Can you please put some lines how this plugin works and what do i need to make it work? how the CLI work? i tried my best to convert ttf/svg/otf/eot to woff no success at all in my case using fontmin.. what am i doing wrong.. and secondly when i use fontmin build command it just grab all files and put them to build folder.. i do not understand what build do.. as far i thought it help me create css files.. but it is not working. or may be i am doing something wrong.. Please drop me a line..

unable to subset certain characters

My steps for reproduction

  1. get http://www.apple.com/wss/fonts/PingHei/v1/PingHei-light.woff
  2. convert into ttf via https://andrewsun.com/projects/woffjs/woffer-woff-font-converter/
  3. use character-map to produce the glyph map string like character-map -f pinghei.ttf > map.txt
  4. try to subset the same ttf file, fontmin -t cat map.txt pinghei.ttf > subset.ttf
  5. character-map -f subset.ttf > map2.txt

Compare map.txt and map2.txt, some obscure glyph appear to be missing (most chinese fonts appear to be intact though).

If we only do fontmin pinghei.ttf > subset.ttf the character map doesn't change, so I am guessing some characters are not allowed by your -t settings.

Subset font by private use codepoints?

Trying to create a subset of the Bootstrap3 glyphicons so I get a font file with only the few glyphs I actually use.

Most of the icons use private use codepoints in UTF-8. I've been able to subset them by copying the character from http://www.fileformat.info/info/unicode/char/e013/browsertest.htm

But it isn't very readable. Any option I've missed that could make this more readable?

From my gulpfile.js:

var Fontmin = require('fontmin');
gulp.task('fontmin', function () {
    var characters = [
        '+', // plus    \002b
        '', // ok      \e013   http://www.fileformat.info/info/unicode/char/e013/browsertest.htm
        '', // remove  \e014
        '', // cog     \e019
        '', // trash   \e020
        '', // lock    \e033
        '', // stats   \e185
    ];
    var fontmin = new Fontmin()
        .src('node_modules/bootstrap-sass/assets/fonts/bootstrap/glyphicons-halflings-regular.ttf')
        .dest('public/build/fonts/bootstrap/subset')
        .use(Fontmin.glyph({
            text: characters.join(),
            hinting: false         // keep ttf hint info (fpgm, prep, cvt). default = true
        }))
        .use(Fontmin.ttf2eot())
        .use(Fontmin.ttf2woff())
        .use(Fontmin.ttf2svg());
    fontmin.run(function (err, files) {
        if (err) {
            throw err;
        }
        console.log(files[0]);
        // => { contents: <Buffer 00 01 00 ...> }
    });

});

Bug? Whitespace trimming

my command:

text="`cat subset.txt`" && node_modules/.bin/fontmin -t "$text" fonts/lato/lato__latofonts_com.ttf > fonts/lato/lato__fontmin.ttf

in subset.txt:

! "#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~©«»ЁАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяё‐‑‒–—―‘‚“„…‰™

whitespace character is in the set
but when I open the .ttf in FontForge, I see that the space character in the font is missing.
空白字符是集合
但是当我在FontForge打开.TTF,我看到,在字体的空格字符缺失。

Webpack 整合 fontmin 的 case

现有构架是 webpack + fontawesome,期望可以在 webpack 中直接加入 fontmin(fontmin-loader?)实现,最理想是现存样式中的 \Fxxx 无需修改。

官网无法正常浏览

Mac 10.9.5, Chrome 41

首页的魔方只能看到上半部分,打开后 Chrome 占的 CPU 随即狂飙到顶,然后一直卡死直至崩溃,自发布日至今都这样。

subset spacing

Everything works fine except that when I subset a font the spacing seems of compared to the original, we are talking small but noticeable. Spacing between the letters itself does not seem of. I removed the spacing characters from the subset string but I see that is done in glyph.js also

update:
with trim: true so it does not subset the space, and the spacing is of (subset = red, input = blue)
screen shot 2016-11-14 at 16 07 39
setting it to false fixes the spacing, but now there is strange character added to the font, not if you type only one word
screen shot 2016-11-14 at 16 06 53

fontmin don't support UNC path on Window

Hello,
Thank all of you for your provided the greatest font converter library. currently, when we use the fontmin library, it doesn't support UNC path on window system. Could you help to fix this issue? Thanks you in advance.

比如说我网站里面 要输入文字 具体的字没定 那么字蛛 可以动态生成吗?

比如说我网站里面 要输入文字 具体的字没定
字蛛说白了 就是在字库里按需加载字体 提取需要的合成.TTF

但是如果 我网站有动态变化的文字 字蛛是不是也会动态的变化呢 比如用户输入文字 等

第一次接触、字蛛 相关代码没有看 冒犯了别喷
如果愿意回答 可以发一封邮件 这个网站比较少逛 [email protected]
我也是很想了解能否做到这一点。谢谢!

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.