From d05606de4dadfeec942d48d0eac17a9967be95f8 Mon Sep 17 00:00:00 2001 From: Petr Sloup Date: Tue, 10 Jan 2017 16:17:51 +0100 Subject: [PATCH] Improved font serving --- docs/config.rst | 3 ++- docs/endpoints.rst | 4 ++++ src/serve_font.js | 15 ++++++++++++--- src/serve_style.js | 22 ++++++++++------------ src/server.js | 7 ++----- 5 files changed, 30 insertions(+), 21 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index bc22c49..699ee3e 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -25,7 +25,8 @@ Example:: "pngQuantization": false, "png": 90 }, - "maxSize": 2048 + "maxSize": 2048, + "serveAllFonts": false }, "styles": { "basic": { diff --git a/docs/endpoints.rst b/docs/endpoints.rst index 864e83e..6cf047c 100644 --- a/docs/endpoints.rst +++ b/docs/endpoints.rst @@ -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`` diff --git a/src/serve_font.js b/src/serve_font.js index 49ae7e1..4500466 100644 --- a/src/serve_font.js +++ b/src/serve_font.js @@ -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; }; diff --git a/src/serve_style.js b/src/serve_style.js index d56c139..cf0bc86 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -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; diff --git a/src/server.js b/src/server.js index 1ec2676..5b29361 100644 --- a/src/server.js +++ b/src/server.js @@ -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) {