Skip to content

Commit

Permalink
Local font loading into SVG in Phantom.js
Browse files Browse the repository at this point in the history
Ensures use of Verdana server-side when rendering images.
  • Loading branch information
espadrine committed Jan 5, 2014
1 parent 8e4c19f commit 6620d46
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
52 changes: 39 additions & 13 deletions phantomjs-svg2png.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,53 @@
var page = require('webpage').create();
var system = require('system');
var svg = system.args[1];
var svgUrl = 'data:image/svg+xml,' + window.encodeURI(svg);
var tmpFile = system.args[2];

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();
}
});
// 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') || 19)
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));
}
});
}
7 changes: 7 additions & 0 deletions svg-to-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ var phantom = require('phantomjs');
var childProcess = require('child_process');
var phantomScript = path.join(__dirname, 'phantomjs-svg2png.js');

// If available, use the font here.
var fontPath = './Verbana.ttf';
try {
// This happens at startup. Needn't be async.
var fontBase64 = fs.readFileSync(fontPath, 'base64');
} catch(e) {}

module.exports = function (svg, format, out, cb) {
var tmpFile = path.join(os.tmpdir(),
"svg2img-" + (Math.random()*2147483648|0) + "." + format);
Expand Down
2 changes: 1 addition & 1 deletion template.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6620d46

Please sign in to comment.