forked from badges/shields
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phantomjs-svg2png.js
53 lines (49 loc) · 1.5 KB
/
phantomjs-svg2png.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
52
53
var page = require('webpage').create();
var system = require('system');
var svg = system.args[1];
var tmpFile = system.args[2];
// Optional local font loading.
var fs = require('fs');
var fontPath = './Verdana.ttf';
if (fs.isFile(fontPath)) {
var fontData = fs.read(fontPath, 'b');
btoa(fontData, function(fontBase64) {
svg = svg.slice(0, svg.indexOf('</svg>')) + '<style><![CDATA['
+ '@font-face{font-family:"Verdana";src:url(data:font/ttf;base64,'
+ fontBase64 + ');}]]></style></svg>';
renderSvg(svg);
});
} else { renderSvg(svg); }
function renderSvg(svg) {
var svgUrl = 'data:image/svg+xml,' + window.encodeURI(svg);
page.viewportSize = getSvgDimensions(svg);
page.open(svgUrl, function(status) {
if (status !== 'success') {
console.error('Failed to load the following SVG data:');
console.error(svgUrl);
phantom.exit(1);
} else {
page.render(tmpFile);
phantom.exit();
}
});
}
function getSvgDimensions(svg) {
var frag = window.document.createElement('div');
frag.innerHTML = svg;
var svgRoot = frag.querySelector('svg');
return {
width: parseFloat(svgRoot.getAttribute('width') || 80),
height: parseFloat(svgRoot.getAttribute('height') || 18)
};
}
function btoa(data, cb) {
page.open('about:blank', function(status) {
if (status !== 'success') {
console.error('Failed to load blank page.');
phantom.exit(1);
} else {
cb(page.evaluate(function(data) { return window.btoa(data); }, data));
}
});
}