views:

22

answers:

0

I'm writing a flashcard generating library. The user provides a text file, my program needs to output something that can be printed, directly or indirectly (e.g., TeX, PS, PDF, XSL-FO). My text formatting is very simple: plain, monospace, italic, or bold. I also need a way to center text in the region. Nothing else.

However, I need some not-known-in-advance amount of text to fit the 3" by 5" area provided by an index card. That means I need text wrapping and copy fitting. The goal is that I be able to say "here's a blob of text, fit it to this rectangle using a maximum font size of 18, scaling down as much as necessary".

I had no idea how hard copy fitting is! So far I've considered:

  • XSL-FO - provides wrapping but not fitting, although I've been told there are some patches to Apache FOP that can help
  • LaTeX - built-in wrapping, but fitting is very hard
  • Java2D - fitting is easy using a loop, but font rendering between platforms is different, and there's no free wrapping
  • PDF/PS directly - no clue

What would you suggest as the easiest way to fit text to a confined space?

UPDATE: I did not find a copyfitting silver bullet. However, after false starts with Apache FOP and Apache PdfBox I settled on iText's com.itextpdf.text.pdf.ColumnText which offers a simulation mode. From there you can write a loop, run a simulation, reducing the font size with each iteration until you find one that allows the text to fit the region. Text wrapping is automatic.