Converting Source ASCII Files to JPEGs

Posted by CommonsWare on Stack Overflow See other posts from Stack Overflow or by CommonsWare
Published on 2009-07-25T19:26:30Z Indexed on 2010/05/19 16:50 UTC
Read the original article Hit count: 197

Filed under:
|
|
|
|

I publish technical books, in print, PDF, and Kindle/MOBI, with EPUB on the way.

The Kindle does not support monospace fonts, which are kinda useful for source code listings. The only way to do monospace fonts is to convert the text (Java source, HTML, XML, etc.) into JPEG images. More specifically, due to pagination issues, a given input ASCII file needs to be split into slices of ~6 lines each, with each slice turned into a JPEG, so listings can span a screen. This is a royal pain.

My current mechanism to do that involves:

  1. Running expand to set a consistent 2-space tab size, which pipes to...
  2. a2ps, which pipes to...
  3. A small Perl snippet to add a "%%LanguageLevel: 3\n" line, which pipes to...
  4. ImageMagick's convert, to take the (E)PS and make a JPEG out it, with an appropriate background, cropped to 575x148+5+28, etc.

That used to work 100% of the time. It now works 95% of the time. The rest of the time, I get convert: geometry does not contain image errors, which I cannot seem to get rid of, in part because I don't understand what the problem is.

Before this process, I used to use a pretty-print engine (source-highlight) to get HTML out of the source code...but then the only thing I could find to convert the HTML into JPEGs was to automate screen-grabs from an embedded Gecko engine. Reliability stank, which is why I switched to my current mechanism.

So, if you were you, and you needed to turn source listings into JPEG images, in an automated fashion, how would you do it? Bonus points if it offers some sort of pretty-print process (e.g., bolded keywords)!

Or, if you know what typically causes convert: geometry does not contain image, that might help. My current process is ugly, but if I could get it back to 100% reliability, that'd be just fine for now.

Thanks in advance!

© Stack Overflow or respective owner

Related posts about imagemagick

Related posts about a2ps