From 18407dea657441cf91fbcd97425f1c691d94cc77 Mon Sep 17 00:00:00 2001 From: Joseph Canero Date: Thu, 5 Oct 2017 14:12:51 -0400 Subject: [PATCH] add new config setting to the style section to allow serving of sprites for a style even if the style json doesn't reference it locally. --- src/serve_style.js | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/serve_style.js b/src/serve_style.js index b7cf1f2..391f7ea 100644 --- a/src/serve_style.js +++ b/src/serve_style.js @@ -47,17 +47,29 @@ module.exports = function (options, repo, params, id, reportTiles, reportFont) { } }); - var spritePath; - - var httpTester = /^(http(s)?:)?\/\//; - if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) { - spritePath = path.join(options.paths.sprites, - styleJSON.sprite + var normalizeSpritePath = function (intermediatePath) { + return path.join(options.paths.sprites, + intermediatePath .replace('{style}', path.basename(styleFile, '.json')) .replace('{styleJsonFolder}', path.relative(options.paths.sprites, path.dirname(styleFile))) ); - styleJSON.sprite = 'local://styles/' + id + '/sprite'; } + + var spritePath; + + var httpTester = /^(http(s)?:)?\/\//; + + // if the current style JSON has a sprite path referencing some local sprites, + // replace placeholders, use that as our sprite path for this style, and + // then update the sprite path in the styleJSON to reference the sprite url. + if (styleJSON.sprite && !httpTester.test(styleJSON.sprite)) { + spritePath = normalizeSpritePath(styleJSON.sprite); + styleJSON.sprite = 'local://styles/' + id + '/sprite'; + // if there are still sprites for this style, serve them according to the config setting + } else if (item.serveSprites && typeof item.serveSprites === 'object') { + spritePath = normalizeSpritePath(item.serveSprites.file); + } + if (styleJSON.glyphs && !httpTester.test(styleJSON.glyphs)) { styleJSON.glyphs = 'local://fonts/{fontstack}/{range}.pbf'; }