Skip to content

Commit

Permalink
Implemented progress bar
Browse files Browse the repository at this point in the history
  • Loading branch information
soimy committed Aug 14, 2017
1 parent 75bc1cf commit 22222bb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
31 changes: 15 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const mapLimit = require('map-limit');
const MaxRectsPacker = require('maxrects-packer');
const Canvas = require('canvas');
const path = require('path');
const ProgressBar = require('progress');
const ProgressBar = require('cli-progress');

const defaultCharset = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~".split('');

Expand Down Expand Up @@ -83,23 +83,20 @@ function generateBMFont (fontPath, opt, callback) {
const context = canvas.getContext('2d');
const packer = new MaxRectsPacker(textureWidth, textureHeight, texturePadding);
const chars = [];
let barGylphs;
if (progress) {
barGylphs = new ProgressBar('Generating Glyphs [:bar] :percent(:current/:total) :etas', {
complete: '=',
incomplete: ' ',
head: '>',
width: 40,
renderThrottle: 0,
clear: true,
total: charset.length
});

}


charset = charset.filter((e, i, self) => {
return i == self.indexOf(e);
}); // Remove duplicate

let bar;
if (progress){
bar = new ProgressBar.Bar({
format: "Genrating [{bar}] {percentage}%({value}/{total}) {duration}s",
clearOnComplete: true
}, ProgressBar.Presets.shades_classic);
bar.start(charset.length, 0);
} // Initializing progress bar

mapLimit(charset, 15, (char, cb) => {
generateImage({
binaryPath,
Expand All @@ -111,11 +108,12 @@ function generateBMFont (fontPath, opt, callback) {
roundDecimal
}, (err, res) => {
if (err) return cb(err);
if (progress) barGylphs.tick();
if (progress) bar.increment();
cb(null, res);
});
}, (err, results) => {
if (err) callback(err);
if (progress) bar.stop();

const os2 = font.tables.os2;
const baseline = os2.sTypoAscender * (fontSize / font.unitsPerEm) + (distanceRange >> 1);
Expand Down Expand Up @@ -207,6 +205,7 @@ function generateBMFont (fontPath, opt, callback) {
let fontFile = {};
fontFile.filename = outputType === "json" ? `${filename}.json` : `${filename}.fnt`;
fontFile.data = dataProc.stringify(fontData, outputType);
if (progress) console.log("\nGeneration complete!\n");
callback(null, textures, fontFile);
});
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@
"license": "MIT",
"dependencies": {
"canvas": "^1.4.0",
"cli-progress": "^1.4.1",
"js2xmlparser": "^3.0.0",
"map-limit": "0.0.1",
"maxrects-packer": "^1.0.2",
"opentype.js": "^0.6.4",
"progress": "^2.0.0"
"opentype.js": "^0.6.4"
},
"devDependencies": {
"handlebars": "^4.0.10",
Expand Down
4 changes: 2 additions & 2 deletions test/test-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const opt = {
textureHeight: 1024,
roundDecimal: 0,
// debug: true,
// progress: true
progress: true
};

fs.readFile(path.join(__dirname, 'charset.input.txt'), 'utf8', (error, data) => {
fs.readFile(path.join(__dirname, 'charset.gb2312.txt'), 'utf8', (error, data) => {
if (error) throw error;
if (data) opt.charset = data;
generateBMFont(path.join(__dirname, 'fonts/FZZDHS.TTF'), opt , (error, textures, font) => {
Expand Down

0 comments on commit 22222bb

Please sign in to comment.