ecomfe / fontmin

Minify font seamlessly
http://ecomfe.github.io/fontmin
MIT License
5.81k stars 318 forks source link

能否增加对woff2的支持 #30

Open ghostgeek opened 8 years ago

ghostgeek commented 8 years ago

WOFF File Format 2.0 已经发布了一段时间,是否在项目里面考虑增加对这一标准的支持

junmer commented 8 years ago

@ghostgeek 是的 有这个考虑 但是目前并没有时间,也可以试试用 gulp-ttf2woff2 来搞定这个需求

var Fontmin = require('fontmin');
var ttf2woff2 = require('gulp-ttf2woff2');

var fontmin = new Fontmin()
    .src('fonts/*.ttf')
    .use(Fontmin.glyph({
        text: '对woff2的支持'
    }))
    .use(ttf2woff2({clone: true}))
    .dest('build/fonts');

fontmin.run(function (err, files) {
    if (err) {
        throw err;
    }

    console.log(files[0]);
    // => { contents: <Buffer 00 01 00 ...> }
});
ghostgeek commented 8 years ago

Delagen commented 8 years ago

based on plugin ttf2woff.js

var ttf2woff2=require("ttf2woff2");

var FontMinTtf2Woff2 = function(opts) {
    opts = _.extend({clone: true}, opts);

    function compileTtf(buffer, options, cb) {
        var output;
        var ttf2woffOpts = {};

        if (options.deflate) {
            ttf2woffOpts.deflate = deflate;
        }

        try {
            output = ttf2woff2(buffer);
        }
        catch (ex) {
            cb(ex);
        }

        output && cb(null, output);
    }

    return through.ctor({
                            objectMode: true
                        }, function(file, enc, cb) {

        // check null
        if (file.isNull()) {
            cb(null, file);
            return;
        }

        // check stream
        if (file.isStream()) {
            cb(new Error('Streaming is not supported'));
            return;
        }

        // check ttf
        if (!isTtf(file.contents)) {
            cb(null, file);
            return;
        }

        // clone
        if (opts.clone) {
            this.push(file.clone(false));
        }

        // replace ext
        file.path = path.join(path.dirname(file.path), path.basename(file.path, path.extname(file.path)) + ".woff2");

        compileTtf(file.contents, opts, function(err, buffer) {

            if (err) {
                cb(err);
                return;
            }

            file.contents = buffer;
            cb(null, file);
        });

    });
};
drazik commented 8 years ago

Hello,

Do you plan to embed ttf2woff2 in fontmin in a future release ?

Thanks for the workaround using gulp-ttf2woff2 as a plugin :+1:

Delagen commented 8 years ago

I preferred to write own implementation of svgs2ttf using fontforge basic scripting to produce svg font and ttf2* modules because svg font rendering issues does not fixed in such projects for years. Own implementation is smaller and more tunable. I understand people that prefer to use native project api instead of such wrappers now.

ianbrandt commented 8 years ago

As fonteditor-core is the basis of all the other fontmin plugins, I submitted a request for ttf2woff2 support there: https://github.com/kekee000/fonteditor-core/issues/3.

chemzqm commented 7 years ago

不知道作者是否接受 PR?

fasign commented 4 years ago

还在维护么