forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
badge.js
51 lines (45 loc) · 1.61 KB
/
badge.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
var fs = require('fs');
var path = require('path');
var SVGO = require('svgo');
var dot = require('dot');
// Initialize what will be used for automatic text measurement.
var Canvas = require('canvas');
var canvasElement = new Canvas(0, 0); // Width and height are irrelevant.
var canvasContext = canvasElement.getContext('2d');
var CanvasFont = Canvas.Font;
try {
var opensans = new CanvasFont('Verdana',
path.join(__dirname, 'Verdana.ttf'));
canvasContext.addFont(opensans);
} catch(e) {}
canvasContext.font = '11px Verdana, "DejaVu Sans"';
// cache templates.
var templates = {};
var templateFiles = fs.readdirSync('templates');
templateFiles.forEach(function(filename) {
var templateData = fs.readFileSync(
path.join(__dirname, 'templates', filename)).toString();
var style = filename.slice(0, -('-template.svg'.length));
templates[style] = dot.template(templateData);
});
var colorscheme = require(path.join(__dirname, 'colorscheme.json'));
function optimize(string, callback) {
var svgo = new SVGO();
svgo.optimize(string, callback);
}
function makeImage(data, cb) {
var template = templates[data.template];
if (template == null) { template = templates['default']; }
if (data.colorscheme) {
data.colorA = colorscheme[data.colorscheme].colorA;
data.colorB = colorscheme[data.colorscheme].colorB;
}
data.widths = [
(canvasContext.measureText(data.text[0]).width|0) + 10,
(canvasContext.measureText(data.text[1]).width|0) + 10,
];
var result = template(data);
// Run the SVG through SVGO.
optimize(result, function(object) { cb(object.data); });
}
module.exports = makeImage;