[cairo] Paginating a png image.

Richard Shann richard.shann at virgin.net
Thu Nov 26 10:59:53 PST 2009

I am trying to print a .png image which is created by an external
application as several pages abutted vertically. (That is the width is
the width of A4 paper, the height is number_pages * the height of A4).

I am using the GtkPrintOperation, and can get the first page, but the
subsequent pages are (largely) blank.

In the callback for the begin-print I set up the cairo surface
and compute the number of pages from by scaling by 842/595 which is A4
aspect ratio.

  the_surface = cairo_image_surface_create_from_png (image.png);
  int src_width = cairo_image_surface_get_width(the_surface);
  int src_height = cairo_image_surface_get_height(the_surface);

  int width = 595;
  int height = 842;
  int page_height =  height*((double)src_width)/width;
  int num_pages = src_height/page_height;
  gtk_print_operation_set_n_pages (operation, num_pages);

Then with the draw-page callback I try to take each chunk of the image,
and this is where I guess I make some mistake:
    cairo_t * cr = gtk_print_context_get_cairo_context (context);
    cairo_set_source_surface (cr, the_surface, 0.0, 0.0);
    cairo_rectangle (cr, 0.0, (double)page_height*page_nr,
(double)width, (double)page_height);
    cairo_fill (cr);

That is, I move the y-coordinate of the cairo_rectangle on by one
page_height at each call expecting this to take a new page from the
image surface. However only the value page_nr=0 behaves as expected, the
second page has just a tiny strip from the bottom of the first page,
printed at the bottom, and the rest of the pages are blank.

Any help would be appreciated

Richard Shann

More information about the cairo mailing list