From 5121bf75f51e5321aefdc1a8a961c8f704c6b84d Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 27 Mar 2015 11:54:44 +0000 Subject: [PATCH] Add missing PNG generation script --- bin/asciicast2png | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 bin/asciicast2png diff --git a/bin/asciicast2png b/bin/asciicast2png new file mode 100755 index 0000000..4194e3d --- /dev/null +++ b/bin/asciicast2png @@ -0,0 +1,60 @@ +#!/usr/bin/env phantomjs + +var system = require('system'); + +if (system.args.length < 3) { + console.log("usage: " + system.args[0] + " "); + phantom.exit(1); +} + +var url = system.args[1]; +var filename = system.args[2]; +var page = require('webpage').create(); + +page.viewportSize = { width: 9999, height: 9999 }; +page.zoomFactor = 2; + +page.onError = function(msg, trace) { + console.log('Script error: ' + msg); + phantom.exit(1); +} + +page.onResourceError = function(resourceError) { + console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')'); + console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString); + phantom.exit(1); +}; + +page.open(url, function(status) { + if (status !== "success") { + console.log("Failed to load " + url); + phantom.exit(1); + } + + var size = page.evaluate(function() { + // Alternative to zoomFactor: + // + // /* scale the whole body */ + // document.body.style.webkitTransform = "scale(2)"; + // document.body.style.webkitTransformOrigin = "0% 0%"; + // /* fix the body width that overflows out of the viewport */ + // document.body.style.width = "50%"; + // + // It has small issue with letter spacing though. + + var term = $('.asciinema-player'); + return [term.width(), term.height()]; + }); + + if (!size[0]) { + console.log("Couldn't get dimensions of the player"); + phantom.exit(1); + return; + } + + page.clipRect = { left: 0, top: 0, width: size[0] * 2, height: size[1] * 2 }; + page.render(filename, { format: 'png' }); + phantom.exit(0); +}); + +// vim: ft=javascript