few days ago i was preparing C++ test for a training i'll be doing for coworkers. of course (almost) all questions had source code attached with questions like “is this code correct?”, “does it leak memory?”, etc… unfortunately for me, system that we use for preparing such a trainings does not provide syntax highlighting, which make reading problematic. the accepted solution is to insert such a questions as images, instead of text. so i could either do tens of print-screens… or write a script. :)
script does few steps:
enscript --color -f Courier12 -B -1 --highlight=cpp -h -o tmp.ps input.cpp gs -sDEVICE=jpeg -r400 -sPAPERSIZE=a4 -dBATCH -dNOPAUSE -sOutputFile=tmp.png tmp.ps convert tmp.png -trim -border 10 -scale "50%" out.png
(working script scr2img on github)
first line produces colored postscript from source. its nice, but i've needed png, so next line creates one with ghostscript (note 400dpi!). last one removes extra white spaces, adds a border… and scales image down to 50% (aka: 200dpi). looks weird, but this is the only way to get reasonable quality of an output - sorry folks. :/
example source:
#include <iostream> using std::cout; using std::endl; int main(void) { cout << "hello world" << endl; cout << "bye bye world" << endl; }
imperfect, but good enough for me. :D note that script itself can be optimized as well – most of the times temporary file is not really needed.