[cairo] create an image from window for printing
Fabian Förg
fabian.foerg at gmx.de
Sun Jan 21 07:57:39 PST 2007
Owen Taylor wrote:
> Hmm, it just occurred to me that there was a small bit of nonsense
> in my examples:
>
> On Sat, 2007-01-20 at 17:50 -0500, Owen Taylor wrote:
>
>
>> So, say you have an surface of dimensions w0, h0, and you want to draw
>> it at x,y and size w,h
>>
>> cairo_save(cr);
>> cairo_move_to(cr, x, y);
>>
> ^^^^^^^
> translate
>
>
>> cairo_scale(cr, w/w0, h/h0);
>> cairo_set_source_surface(cr, surface, 0, 0);
>> cairo_paint(cr);
>>
>> You really should study that until you realize that there is zero magic
>> in it; it's just a straightforward consequence of how cairo works.
>>
>> So, say if you were drawing directly to a PDF surface, and you wanted to
>> paint it with the top left at 1cm, 1cm and with width of 10cm, then you
>> would do:
>>
>> #define POINTS_PER_CM (72. / 2.54);
>>
>> cairo_save(cr);
>> cairo_move_to(cr, POINTS_PER_CM, POINTS_PER_CM);
>>
> ^^^^^^^
> translate
>
>> double scale = (10 * POINTS_PER_CM) / w0;
>> cairo_scale(cr, scale, scale); /* Preserve aspect ratio */
>> cairo_set_source_surface(cr, surface, 0, 0);
>> cairo_paint(cr);
>>
>
> Sorry for any confusion,
> Owen
>
>
Thanks for your help.
I changed my code to the following:
(The window which should be printed is in the struct appl_t).
static void draw_page(GtkPrintOperation * operation,
GtkPrintContext * print_context, gint page_nr,
appl_t * appl)
{
GdkColor color;
gdouble print_width, print_height, scale;
gint width, height;
cairo_t *cr, *cr_win;
cairo_surface_t *surface;
/* set background color of appl->window to white */
color.red = 65535;
color.green = 65535;
color.blue = 65535;
gtk_widget_modify_bg(appl->window, GTK_STATE_NORMAL, &color);
/* get width and height of the appl->window */
gdk_drawable_get_size(appl->window->window, &width, &height);
/* create a surface of the appl->window */
cr_win = gdk_cairo_create(appl->window->window);
surface = cairo_get_target(cr_win);
cairo_surface_reference(surface);
cairo_destroy(cr_win);
/* get width and heigth of print_context */
print_width = gtk_print_context_get_width(print_context);
print_height = gtk_print_context_get_height(print_context);
/* transform the print_context into a cairo_context */
cr = gtk_print_context_get_cairo_context(print_context);
/* not needed ? */
/*cairo_save(cr);
cairo_translate(cr, 0., 0.);*/
scale = (gdouble) print_width / width;
/* scale, keep aspect ratio */
cairo_scale(cr, scale, scale);
cairo_set_source_surface(cr, surface, 0., 0.);
cairo_surface_destroy(surface);
cairo_paint(cr);
}
When I export it as PS, the PS-file looks as expected. However, when it
is exported as PDF, the margins and the unneeded space are filled with
"copies" of the captured window.
Is that a bug in the PDF export function?
More information about the cairo
mailing list