Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
tm.font = function(name, text, fontdir, callback) {
tm.fonts = tm.fonts || mapnik.fonts();
if (tm.fonts.indexOf(name) === -1 && fontdir === undefined) return callback(new Error('Invalid font ' + name));
// Attempt to retrieve from cache.
var xml = tm.templates.xrayfont({fontdir: fontdir, name:name, text:text||name});
var ckey = 'font-' + crypto.createHash('md5').update(xml).digest('hex').substr(0,8) + '.png';
var file = path.join(tm.config().cache,ckey);
fs.readFile(file, function(err, buffer) {
if (err && err.code !== 'ENOENT') return callback(err);
if (buffer) {
buffer.hit = true;
return callback(null, buffer);
}
var map = new mapnik.Map(500,60);
map.extent = [-180,-10,180,10];
validateAsync: function(attributes, options) {
// If client-side, pass-through.
if (typeof require === 'undefined') {
return options.success(this, null);
}
var carto = require('carto'),
mapnik = require('mapnik'),
that = this,
stylesheets = this.get('Stylesheet'),
env = {
returnErrors: true,
errors: [],
validation_data: {
fonts: mapnik.fonts()
},
deferred_externals: [],
only_validate: true,
effects: []
};
// Hard clone the model JSON before rendering as rendering will change
// properties (e.g. localize a datasource URL to the filesystem).
var data = JSON.parse(JSON.stringify(attributes));
new carto.Renderer(env)
.render(data, function(err, output) {
if (err) {
options.error(that, err);
} else {
options.success(that, null);
}
tm.fontfamilies = function() {
if (tm._fontfamilies) return tm._fontfamilies;
var fonts = require('mapnik').fonts();
fonts.sort();
// Overrides are custom exceptions -- regexes for known font family
// names that cannot otherwise be autodetected.
var overrides = {
'Call': /^Call (One|Two|Three|Four|Five|Six|Seven|Eight|Nine)/,
'Komika': /^Komika (Hand|Parch|Title)/
};
// Keywords are ordered by "display priority" -- e.g. fonts
// containing earlier words should be favored for being a preview
// of the family as a whole.
var keywords = [
'medium',
'normal',
'regular',
'book',
validateAsync: function(options) {
// If client-side, pass-through.
if (typeof require === 'undefined') {
return options.success(this, null);
}
var carto = require('carto'),
mapnik = require('mapnik'),
that = this,
stylesheets = this.get('Stylesheet'),
env = {
returnErrors: true,
errors: [],
validation_data: {
fonts: mapnik.fonts()
},
deferred_externals: [],
only_validate: true,
effects: []
};
// Hard clone the model JSON before rendering as rendering will change
// properties (e.g. localize a datasource URL to the filesystem).
var data = JSON.parse(JSON.stringify(this.toJSON()));
new carto.Renderer(env)
.render(data, function(err, output) {
if (err) {
options.error(that, err);
} else {
options.success(that, null);
}
function getFontFamilies() {
const fonts = mapnik.fonts();
fonts.sort();
let level1 = {};
for (let i = 0; i < fonts.length; i++) {
let parts = fonts[i].split(' ');
while (parts.length) {
let word = parts[parts.length - 1];
if (keywords.indexOf(word.toLowerCase()) === -1) break;
parts.pop();
}
let family = parts.join(' ');
level1[family] = level1[family] || [];
level1[family].push(fonts[i]);
}
let level2 = {};
for (let fam in level1) {
if (level1[fam].length > 1) continue;
function(err, data) {
if (err) return this(err);
if (that.language === 'carto') {
var renderer = new carto.Renderer({
data_dir: that.mapfile_dir,
optimization: false,
validation_data: { fonts: mapnik.fonts() }
});
renderer.render(data, this);
} else {
this(null, data);
}
},
function(err, compiled) {
function(err, data) {
if (err) throw err;
if (that.language === 'carto') {
var renderer = new carto.Renderer({
alias: true,
data_dir: that.data_dir,
local_data_dir: that.local_data_dir,
validation_data: { fonts: mapnik.fonts() }
});
renderer.render(data, this);
} else {
this(null, data);
}
},
callback
app.get('/:style(style):history()', function(req, res, next) {
res.set({'content-type':'text/html'});
try {
var page = tm.templates.style({
cwd: process.env.HOME,
fontsRef: require('mapnik').fonts(),
cartoRef: require('carto').tree.Reference.data,
sources: [req.style._backend._source.data],
style: req.style.data,
history: req.history
})
} catch(err) {
return next(new Error('style template: ' + err.message));
}
return res.send(page);
});