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,
"png": 90
},
"maxSize": 2048
"maxSize": 2048,
"serveAllFonts": false
},
"styles": {
"basic": {

View file

@ -60,3 +60,7 @@ Source data
TileJSON arrays
===============
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) {
fs.stat(path.join(fontPath, file), function(err, stats) {
if (!err) {
if (stats.isDirectory()) {
if (stats.isDirectory() &&
fs.existsSync(path.join(fontPath, file, '0-255.pbf'))) {
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) {
var fontstack = decodeURI(req.params.fontstack);
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) {
if (err || concated.length === 0) {
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;
};

View file

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

View file

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