Strange behavior in GtkPrintOperation

Hello,

I don’t have much experience with GTK, and I am having some trouble with GtkPrintOperation. I am trying to add printing support for Simon Tatham’s Portable Puzzle Collection in the GTK frontend. It is sort of working, but there are a few problems:

  • It prints twice as many pages as it should, with the last pages being blank.
  • When in landscape mode, the pages are printed in portrait mode, although the content is aligned for landscape mode. The extra blank pages are printed in landscape mode.
  • Reverse portrait mode is the same as portrait mode and reverse landscape mode is the same as landscape mode (probably the same cause as above).

I’ve scoured the Web for a solution, but to no avail.

I paginate everything in the begin-print handler, print_begin(). I call gtk_print_operation_set_n_pages() with the correct number of pages and I end each page with cairo_show_page(). All rendering is done with Cairo.

You can find a patch on GNOME Pastebin with my code that can be applied on top of the latest commit (currently 1c0c49d). (Is this the preferred way to attach files?)

I feel like there is something simple I’m missing but I don’t know what it is…

Also, if it matters, I’m using Debian GNU/Linux bullseye (testing).

Thanks in advance,
Asher

I’ve figured out the problem! It was that I was using cairo_show_page(), while GTK actually calls gtk_print_operation_draw_page_finish() for each page which itself calls cairo_show_page(). So that is why it was printing twice as many pages as it should. I guess gtk_print_operation_draw_page_finish() must also do some stuff depending on page orientation, which is why the page orientation was not working correctly for the pages that were not blank (the ones I used cairo_show_page() on).

So anyway, I tried using gtk_print_operation_set_defer_drawing() so that I could manually call gtk_print_operation_draw_page_finish() in place of cairo_show_page(), but that did not work (I guess that’s not what it’s for anyway). So I ended up changing the document printing code so that I could print one page at a time in the draw-page handler. And now everything works perfectly! I knew it was something (kind of) simple!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.