Sunday, January 4, 2026

Converting Chapterizer from Cairo + Pango to CapyPDF

Chapterizer (not a great name, I know) is a tool I wrote to generate books. Originally used Cairo and Pango to generate PDF files. It works and was fairly easy to get started but has its own set of downsides:

  • Cairo always produces RGB PDFs, which are not accepted by printing houses
  • Cairo does not handle advanced PDF features like trim boxes
  • Pango aligns text at the top of each line, but for high quality text output you have to do baseline alignment
  • Pango is designed to "always print something", which is to say it does transparent font substitution for example when the chosen font does not have some glyph
I have also created CapyPDF to generate "proper" PDF. Over the holidays I finalized porting Chapterizer to use CapyPDF. The pipeline is now surprisingly simple. First you read in the source text, then it is shaped with Harfbuzz and then written to a PDF file with CapyPDF.

It was grunt work. Nothing about it was particularly difficult, just dealing with the same old issues like the fact that in PDF the page's origin is at bottom left, whereas in Cairo it is at the top left.

Anyhow, now that it is done we can actually test the performance of CapyPDF with a somewhat realistic setup. Currently creating a 40 page document takes 0.4 seconds which comes down to 0.01 seconds per page. Which is fast enough for me.

No comments:

Post a Comment