[cairo] Printing/Creating PDf/Postscript

Jeff Hallock Jhallock at WBandA.com
Thu Jun 3 12:54:19 PDT 2010


I am attempting to create reports using (the perl bindings for) Gtk2/Cairo/Pango. I don't think it should be too hard to read my code if you aren't familiar with perl though.  Not knowing which was the best place to post this, I sent this to both the Gtk and the Cairo mailing lists.

 My reports should be able to write to a print operation, or a cairo surface. This seems simple enough - just pass  in the Cairo context you want to write to.  The problem I am running into is when draw text on a context from a  print operation - the text appears extremely tiny on the page (look at print_op.pdf).

When drawing to a Cairo Surface - the output is as expected (see surface.pdf/surface.ps).

surface_po_diff.pl is the script that created these files so you can run it yourself.

What is going on here? I know that the suggested way of printing text to a print operation is get a pango layout from the GtkPrintContext using gtk_print_context_create_pango_layout ()   ($print_context->create_pango_layout from perl) and then use that.

But I really don't want my reports to need to know about a GtkPrintContext - ideally they should be able to operate with just a CairoContext.

My gut says there is some sort of transformation going on here, but I'm not sure where are how to alter it. (I'm rather new to Cairo/Pango programming). I have tried playing with the set_matrix and set_font_matrix of the cairo context but didn't yield any results. I may not be using them correctly.

How can write this code, so the print_text function prints the same size on the print operation and a cairo surface that I create myself.


Thanks in advance for any input!


-          Jeffrey Ray



# prints to print driver - text is tiny
my $window = Gtk2::Window->new;
my $po = Gtk2::PrintOperation->new;
$po->set_n_pages( 1 );

$po->signal_connect('draw-page' => sub {
    my ($po, $print_context, $number) = @_;
    my $cr = $print_context->get_cairo_context;
    print_text($cr);
});
$po->run('print-dialog', $window);



# creates a pdf file, text is large
my $surface = Cairo::PdfSurface->create ( 'surface.pdf', 612, 792);
my $cr = Cairo::Context->create( $surface );
print_text($cr);
$cr->show_page;
$surface->finish;

# creates a ps file, text is large
$surface = Cairo::PdfSurface->create ( 'surface.ps', 612, 792);
$cr = Cairo::Context->create( $surface );
print_text($cr);
$cr->show_page;
$surface->finish;



sub print_text {
    my $cr = shift;

    my $layout = Pango::Cairo::create_layout( $cr );
    $layout->set_markup( 'Foo:Bar' );

    my $attrlist = Pango::AttrList->new;
    my $font = Pango::AttrFontDesc->new( Pango::FontDescription->from_string('calibri,arial 10') );
    $attrlist->insert($font);
    $layout->set_attributes( $attrlist );

    Pango::Cairo::update_layout( $cr, $layout );
    Pango::Cairo::show_layout( $cr, $layout );
}

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100603/dd8217a1/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: surface.pdf
Type: application/pdf
Size: 9439 bytes
Desc: surface.pdf
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100603/dd8217a1/attachment-0002.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: surface.ps
Type: application/postscript
Size: 9439 bytes
Desc: surface.ps
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100603/dd8217a1/attachment-0001.ps>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: surface_po_diff.pl
Type: application/octet-stream
Size: 1234 bytes
Desc: surface_po_diff.pl
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100603/dd8217a1/attachment-0001.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: print_op.pdf
Type: application/pdf
Size: 27553 bytes
Desc: print_op.pdf
URL: <http://lists.cairographics.org/archives/cairo/attachments/20100603/dd8217a1/attachment-0003.pdf>


More information about the cairo mailing list