function Font (src, options) { number ++; var fontmin = new Fontmin().src(src); var dest = options.dest || {}; var chars = options.chars; var extname = path.extname(src); var basename = path.basename(src, extname); var temp = TEMP + number; dest.ttf = dest.ttf || src; if (options.chars) { fontmin.use(Fontmin.glyph({ text: chars })); } Object.keys(dest).forEach(function (key) { if (typeof Fontmin['ttf2' + key] === 'function') { fontmin.use(Fontmin['ttf2' + key]({clone: true})); } }); fontmin.dest(temp); return new Promise(function (resolve, reject) { fontmin.run(function (err, files) { if (err) { reject(err); } else { Object.keys(dest).forEach(function (key) { var filename = basename + '.' + key; // 特殊逻辑,支持非ttf后缀的turetype字体 // if (key === 'ttf') { // filename = basename + extname; // } var file = path.join(temp, filename); var out = dest[key]; file = path.resolve(file); rename(file, out); }); rmdir(temp); resolve(files); } }); }); };
/** * fontMinify 处理器 * * @param {FileObject} file 处理器文件对象 * @param {processContext} processContext 处理器上下文 * @param {Function} done 完成回调 */ function fontMinify(file, processContext, done) { var text = this.text || ''; var entryFiles = this.entryFiles; if (entryFiles) { var entryText = []; processContext .getFilesByPatterns(entryFiles) .forEach(function (entryFile) { entryText.push(entryFile.data); }); text += entryText.join(''); } var chineseOnly = this.chineseOnly; if (chineseOnly) { text = text.replace(/[^\u4e00-\u9fa5]/g, ''); } var srcPath = file.path; var outputDir = processContext.outputDir; var destPath = path.dirname(file.outputPath); destPath = path.resolve(outputDir, destPath); // 获取目标地址,传入 fontmin.dest file.outputPath = null; // 清除 edp 构建结果 var fontmin = new Fontmin() .src(srcPath) .use(Fontmin.glyph({ text: text })) .use(Fontmin.ttf2eot()) .use(Fontmin.ttf2woff()) .use(Fontmin.ttf2svg()) .dest(destPath); var me = this; fontmin.run(function (err, files, stream) { if (err) { me.log.error(err); } done(); }); }
htmlToText.fromFile(path.join(__dirname, '../../geojsonIndex.html'), {}, function (err, text) { new Fontmin() .src('./noto-thin.ttf') .use(Fontmin.glyph({ text: text })) .run(function (err, files) { if (err) { throw new Error(err); } console.log('files'); console.log(files); require('fs').writeFileSync('./noto-thin.min.ttf', files[0]._contents); }); })
return through.obj(function (file, enc, cb) { if (file.isNull()) { cb(null, file); return; } if (file.isStream()) { cb(new gutil.PluginError('gulp-fontmin', 'Streaming not supported')); return; } if (!isExt(file.path, 'svg')) { if (opts.verbose) { gutil.log('gulp-fontmin: Skipping unsupported ' + chalk.blue(file.relative)); } cb(null, file); return; } var fontmin = new Fontmin() .use(Fontmin.glyph(opts)) .use(Fontmin.ttf2eot()) .use(Fontmin.ttf2woff()) .use(Fontmin.ttf2svg()) .use(Fontmin.css(opts)); if (opts.use) { opts.use.forEach(fontmin.use.bind(fontmin)); } fontmin.run(function (err, files) { if (err) { cb(new gutil.PluginError('gulp-fontmin:', err, {fileName: file.path})); return; } cb(null, file); }); }, function (cb) {
}, function(flushCb){ var fontmin = null, _this = this; // remove duplication glyphListCacheStr = (function(str){ var o = {}, list=str.split(''), i; for (i=0; i<list.length; i++) { o[list[i]]=1; } return Object.keys(o).join(''); }(glyphListCacheStr + options.mustHaveGlyphs)); fontmin = new Fontmin() .src(options.fontPath) .use(Fontmin.glyph({ text: glyphListCacheStr })) .use(Fontmin.ttf2eot()) .use(Fontmin.ttf2woff()) .use(Fontmin.ttf2svg()); // .use(Fontmin.css()); fontmin.run(function (err, files) { if (err) { _this.emit('error', new gutil.PluginError(err)); return flushCb(); } for (var i=0; i<files.length; i++) { _this.push(files[i]); } flushCb(); }); });
min: function(succeed, error) { var webFont = this.webFont; var source = this.source; var temp = this.temp; var that = this; var originalSize = fs.statSync(source).size; var fontmin = new Fontmin().src(source); if (webFont.chars) { fontmin.use(Fontmin.glyph({ text: webFont.chars })); } var types = { 'embedded-opentype': 'ttf2eot', 'woff': 'ttf2woff', 'woff2': 'ttf2woff2', 'svg': 'ttf2svg' }; webFont.files.forEach(function(file) { var format = file.format; var fn = types[format]; if (format === 'truetype') { return; } if (typeof Fontmin[fn] === 'function') { fontmin.use(Fontmin[fn]({ clone: true })); } else { throw new TypeError('compressing the ' + format + ' format fonts is not supported, ' + 'please delete it in the CSS file: "' + file.url + '"'); } }); fontmin.dest(temp); fontmin.run(function(errors/*, buffer*/) { if (errors) { that.clear(); error(errors); } else { // 从临时目录把压缩后的字体剪切出来 webFont.files.forEach(function(file) { var basename = path.basename(file.url); var tempFile = path.join(temp, basename); var out = file.url; utils.rename(tempFile, out); if (fs.existsSync(file.url)) { file.size = fs.statSync(file.url).size; } else { file.size = null; } }); that.clear(); // 添加新字段:记录原始文件大小 webFont.originalSize = originalSize; succeed(webFont); } }); },