Improved font serving

This commit is contained in:
Petr Sloup 2017-01-10 16:17:51 +01:00
parent b9930dd195
commit d05606de4d
5 changed files with 30 additions and 21 deletions

View file

@ -25,7 +25,8 @@ Example::
"pngQuantization": false, "pngQuantization": false,
"png": 90 "png": 90
}, },
"maxSize": 2048 "maxSize": 2048,
"serveAllFonts": false
}, },
"styles": { "styles": {
"basic": { "basic": {

View file

@ -60,3 +60,7 @@ Source data
TileJSON arrays TileJSON arrays
=============== ===============
Array of all TileJSONs is at ``/index.json`` (``/rendered.json``; ``/data.json``) Array of all TileJSONs is at ``/index.json`` (``/rendered.json``; ``/data.json``)
List of available fonts
=======================
Array of names of the available fonts is at ``/fontstacks.json``

View file

@ -19,7 +19,8 @@ module.exports = function(options, allowedFonts) {
files.forEach(function(file) { files.forEach(function(file) {
fs.stat(path.join(fontPath, file), function(err, stats) { fs.stat(path.join(fontPath, file), function(err, stats) {
if (!err) { if (!err) {
if (stats.isDirectory()) { if (stats.isDirectory() &&
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))) {
existingFonts[path.basename(file)] = true; existingFonts[path.basename(file)] = true;
} }
} }
@ -27,12 +28,13 @@ module.exports = function(options, allowedFonts) {
}); });
}); });
app.get('/: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;
return utils.getFontsPbf(allowedFonts, fontPath, fontstack, range, existingFonts, return utils.getFontsPbf(options.serveAllFonts ? null : allowedFonts,
fontPath, fontstack, range, existingFonts,
function(err, concated) { function(err, concated) {
if (err || concated.length === 0) { if (err || concated.length === 0) {
console.log(err); console.log(err);
@ -46,5 +48,12 @@ module.exports = function(options, allowedFonts) {
}); });
}); });
app.get('/fontstacks.json', function(req, res, next) {
res.header('Content-type', 'application/json');
return res.send(
Object.keys(options.serveAllFonts ? existingFonts : allowedFonts).sort()
);
});
return app; return app;
}; };

View file

@ -29,19 +29,17 @@ module.exports = function(options, repo, params, id, reportTiles, reportFont) {
} }
}); });
var findFontReferences = function(obj) { styleJSON.layers.forEach(function(obj) {
Object.keys(obj).forEach(function(key) { if (obj['type'] == 'symbol') {
var value = obj[key]; var fonts = (obj['layout'] || {})['text-font'];
if (key == 'text-font') { if (fonts && fonts.length) {
if (value && value.length > 0) { fonts.forEach(reportFont);
value.forEach(reportFont); } else {
reportFont('Open Sans Regular');
reportFont('Arial Unicode MS Regular');
} }
} else if (value && typeof value == 'object') {
findFontReferences(value);
} }
}); });
};
styleJSON.layers.forEach(findFontReferences);
var spritePath; var spritePath;

View file

@ -36,10 +36,7 @@ module.exports = function(opts, callback) {
styles: {}, styles: {},
rendered: {}, rendered: {},
data: {}, data: {},
fonts: { // default fonts, always expose these (if they exist) fonts: {}
'Open Sans Regular': true,
'Arial Unicode MS Regular': true
}
}; };
app.enable('trust proxy'); app.enable('trust proxy');
@ -143,7 +140,7 @@ module.exports = function(opts, callback) {
if (Object.keys(serving.styles).length > 0) { if (Object.keys(serving.styles).length > 0) {
// serve fonts only if serving some styles // serve fonts only if serving some styles
app.use('/fonts/', serve_font(options, serving.fonts)); app.use('/', serve_font(options, serving.fonts));
} }
Object.keys(data).forEach(function(id) { Object.keys(data).forEach(function(id) {