Add code to load glyph data into Meteor

This commit is contained in:
Shaunak Kishore
2015-08-27 03:10:30 -04:00
parent 4c366184a3
commit 5741f20f79
10 changed files with 175 additions and 7097 deletions

View File

@ -1,24 +1,67 @@
var child_process = Npm.require('child_process');
var path = Npm.require('path');
function get_glyph_data(characters) {
var BATCH_SIZE = 64;
var GLYPH_RANGE = [0x4e00, 0x9fff];
var reloading = false;
function get_glyph_data(characters, callback) {
var json = '';
var font = path.join(process.env.PWD, 'derived', 'ukai.svg');
var main = path.join(process.env.PWD, 'scripts', 'main.py');
var child = child_process.spawn(main, ['-f', font].concat(characters));
child.stdout.on('data', function(data) {
child.stdout.on('data', Meteor.bindEnvironment(function(data) {
json += data;
});
child.stderr.on('data', function(data) {
console.error('' + data);
});
child.on('close', function(code) {
console.log('Subprocess exited with code: ' + code);
console.log('Got JSON data:');
return JSON.parse(json);
}));
child.on('close', Meteor.bindEnvironment(function(code) {
var glyphs = [];
try {
glyphs = JSON.parse(json);
} catch (e) {
console.error(e);
glyphs.length = 0;
}
callback(glyphs, code);
}));
}
function iterate(start, end, index) {
index = index || start;
if (index >= end) {
Progress.remove({});
reloading = false;
return;
}
Progress.upsert({}, {value: (index - start)/(end - start)});
var max = Math.min(index + BATCH_SIZE, end);
var characters = [];
for (var i = index; i < max; i++) {
characters.push(i.toString(16));
}
get_glyph_data(characters, function(glyphs) {
for (var i = 0; i < glyphs.length; i++) {
var glyph = glyphs[i];
Glyphs.upsert({name: glyph.name}, glyph);
}
Meteor.setTimeout(function() { iterate(start, end, max); }, 0);
});
}
Meteor.startup(function() {
get_glyph_data(['4dff', '4e00', '4e01']);
Meteor.methods({
reload: function() {
if (!reloading) {
iterate(GLYPH_RANGE[0], GLYPH_RANGE[1]);
reloading = true;
}
},
});
Meteor.publish('progress', function() {
return Progress.find();
});
Meteor.startup(function() {
Progress.remove({});
Glyphs._ensureIndex({name: 1}, {unique: true});
});