Font concatenation
This commit is contained in:
parent
946cb2ca5f
commit
06b88bbbe7
1 changed files with 35 additions and 17 deletions
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
var path = require('path'),
|
||||
var async = require('async'),
|
||||
path = require('path'),
|
||||
fs = require('fs');
|
||||
|
||||
var clone = require('clone'),
|
||||
|
@ -12,30 +13,47 @@ module.exports = function(fontPath, allowedFonts) {
|
|||
|
||||
var rootPath = path.join(process.cwd(), fontPath || '');
|
||||
|
||||
var getFontPbf = function(name, range, callback) {
|
||||
// if some of the files failed to load (does not exist or not allowed),
|
||||
// return empty buffer so the other fonts can still work
|
||||
if (allowedFonts[name]) {
|
||||
var filename = rootPath + '/' + name + '/' + range + '.pbf';
|
||||
return fs.readFile(filename, function(err, data) {
|
||||
if (err) {
|
||||
console.log('Font load error:', filename);
|
||||
return callback(null, new Buffer([]));
|
||||
} else {
|
||||
return callback(null, data);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return callback(null, new Buffer([]));
|
||||
}
|
||||
};
|
||||
|
||||
app.get('/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf',
|
||||
function(req, res, next) {
|
||||
var fontstack = decodeURI(req.params.fontstack);
|
||||
var range = req.params.range;
|
||||
|
||||
var fonts = fontstack.split(',');
|
||||
if (fonts.length == 1) {
|
||||
if (allowedFonts[fonts[0]]) {
|
||||
var filename = rootPath + '/' + fonts[0] + '/' + range + '.pbf';
|
||||
return fs.readFile(filename, function(err, data) {
|
||||
if (err) {
|
||||
console.log('Font load error:', filename);
|
||||
return res.status(404).send('File not found');
|
||||
} else {
|
||||
res.header('Content-type', 'application/x-protobuf');
|
||||
return res.send(data);
|
||||
}
|
||||
});
|
||||
|
||||
var queue = [];
|
||||
fonts.forEach(function(font) {
|
||||
queue.push(function(callback) {
|
||||
getFontPbf(font, range, callback);
|
||||
});
|
||||
});
|
||||
|
||||
return async.parallel(queue, function(err, results) {
|
||||
var concated = Buffer.concat(results);
|
||||
if (err || concated.length == 0) {
|
||||
return res.status(400).send('');
|
||||
} else {
|
||||
return res.status(403).send('Forbidden');
|
||||
res.header('Content-type', 'application/x-protobuf');
|
||||
return res.send(concated);
|
||||
}
|
||||
} else {
|
||||
return res.status(501).send('Not Yet Implemented');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return app;
|
||||
|
|
Loading…
Reference in a new issue