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';
|
'use strict';
|
||||||
|
|
||||||
var path = require('path'),
|
var async = require('async'),
|
||||||
|
path = require('path'),
|
||||||
fs = require('fs');
|
fs = require('fs');
|
||||||
|
|
||||||
var clone = require('clone'),
|
var clone = require('clone'),
|
||||||
|
@ -12,30 +13,47 @@ module.exports = function(fontPath, allowedFonts) {
|
||||||
|
|
||||||
var rootPath = path.join(process.cwd(), fontPath || '');
|
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',
|
app.get('/fonts/:fontstack/:range([\\d]+-[\\d]+).pbf',
|
||||||
function(req, res, next) {
|
function(req, res, next) {
|
||||||
var fontstack = decodeURI(req.params.fontstack);
|
var fontstack = decodeURI(req.params.fontstack);
|
||||||
var range = req.params.range;
|
var range = req.params.range;
|
||||||
|
|
||||||
var fonts = fontstack.split(',');
|
var fonts = fontstack.split(',');
|
||||||
if (fonts.length == 1) {
|
|
||||||
if (allowedFonts[fonts[0]]) {
|
var queue = [];
|
||||||
var filename = rootPath + '/' + fonts[0] + '/' + range + '.pbf';
|
fonts.forEach(function(font) {
|
||||||
return fs.readFile(filename, function(err, data) {
|
queue.push(function(callback) {
|
||||||
if (err) {
|
getFontPbf(font, range, callback);
|
||||||
console.log('Font load error:', filename);
|
});
|
||||||
return res.status(404).send('File not found');
|
});
|
||||||
} else {
|
|
||||||
res.header('Content-type', 'application/x-protobuf');
|
return async.parallel(queue, function(err, results) {
|
||||||
return res.send(data);
|
var concated = Buffer.concat(results);
|
||||||
}
|
if (err || concated.length == 0) {
|
||||||
});
|
return res.status(400).send('');
|
||||||
} else {
|
} 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;
|
return app;
|
||||||
|
|
Loading…
Reference in a new issue