1 /* GTK - The GIMP Toolkit
2 * gtkprintcontext.c: Print Context
3 * Copyright (C) 2006, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
22 #include "gtkprintoperation-private.h"
25 typedef struct _GtkPrintContextClass GtkPrintContextClass;
27 #define GTK_IS_PRINT_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_CONTEXT))
28 #define GTK_PRINT_CONTEXT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))
29 #define GTK_PRINT_CONTEXT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))
31 #define MM_PER_INCH 25.4
32 #define POINTS_PER_INCH 72
34 struct _GtkPrintContext
36 GObject parent_instance;
38 GtkPrintOperation *op;
40 GtkPageSetup *page_setup;
41 PangoFontMap *fontmap;
43 gdouble pixels_per_unit_x;
44 gdouble pixels_per_unit_y;
47 struct _GtkPrintContextClass
49 GObjectClass parent_class;
52 G_DEFINE_TYPE (GtkPrintContext, gtk_print_context, G_TYPE_OBJECT)
55 gtk_print_context_finalize (GObject *object)
57 GtkPrintContext *context = GTK_PRINT_CONTEXT (object);
59 g_object_unref (context->fontmap);
60 if (context->page_setup)
61 g_object_unref (context->page_setup);
63 cairo_destroy (context->cr);
66 G_OBJECT_CLASS (gtk_print_context_parent_class)->finalize (object);
70 gtk_print_context_init (GtkPrintContext *context)
75 gtk_print_context_class_init (GtkPrintContextClass *class)
77 GObjectClass *gobject_class = (GObjectClass *)class;
79 gobject_class->finalize = gtk_print_context_finalize;
84 _gtk_print_context_new (GtkPrintOperation *op)
86 GtkPrintOperationPrivate *priv = op->priv;
87 GtkPrintContext *context;
89 context = g_object_new (GTK_TYPE_PRINT_CONTEXT, NULL);
92 context->cr = cairo_create (priv->surface);
98 /* Do nothing, this is the cairo default unit */
99 context->pixels_per_unit_x = 1.0;
100 context->pixels_per_unit_y = 1.0;
102 case GTK_UNIT_POINTS:
103 context->pixels_per_unit_x = priv->dpi_x / POINTS_PER_INCH;
104 context->pixels_per_unit_y = priv->dpi_y / POINTS_PER_INCH;
107 context->pixels_per_unit_x = priv->dpi_x;
108 context->pixels_per_unit_y = priv->dpi_y;
111 context->pixels_per_unit_x = priv->dpi_x / MM_PER_INCH;
112 context->pixels_per_unit_y = priv->dpi_y / MM_PER_INCH;
115 cairo_scale (context->cr,
116 context->pixels_per_unit_x,
117 context->pixels_per_unit_y);
119 context->fontmap = pango_cairo_font_map_new ();
120 /* We use the unit-scaled resolution, as we still want fonts given in points to work */
121 pango_cairo_font_map_set_resolution (PANGO_CAIRO_FONT_MAP (context->fontmap),
122 priv->dpi_y / context->pixels_per_unit_y);
128 _gtk_print_context_rotate_according_to_orientation (GtkPrintContext *context)
130 GtkPrintOperationPrivate *priv = context->op->priv;
131 cairo_t *cr = context->cr;
132 cairo_matrix_t matrix;
133 GtkPaperSize *paper_size;
134 gdouble width, height;
136 paper_size = gtk_page_setup_get_paper_size (context->page_setup);
138 width = gtk_paper_size_get_width (paper_size, GTK_UNIT_INCH);
139 width = width * priv->dpi_x / context->pixels_per_unit_x;
140 height = gtk_paper_size_get_height (paper_size, GTK_UNIT_INCH);
141 height = height * priv->dpi_y / context->pixels_per_unit_y;
143 switch (gtk_page_setup_get_orientation (context->page_setup))
146 case GTK_PAGE_ORIENTATION_PORTRAIT:
148 case GTK_PAGE_ORIENTATION_LANDSCAPE:
149 cairo_translate (cr, width, 0);
150 cairo_matrix_init (&matrix,
154 cairo_transform (cr, &matrix);
156 case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
157 cairo_translate (cr, width, height);
158 cairo_matrix_init (&matrix,
162 cairo_transform (cr, &matrix);
164 case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
165 cairo_translate (cr, 0, height);
166 cairo_matrix_init (&matrix,
170 cairo_transform (cr, &matrix);
176 _gtk_print_context_translate_into_margin (GtkPrintContext *context)
178 GtkPrintOperationPrivate *priv;
181 g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
183 priv = context->op->priv;
185 /* We do it this way to also handle GTK_UNIT_PIXELS */
187 left = gtk_page_setup_get_left_margin (context->page_setup, GTK_UNIT_INCH);
188 top = gtk_page_setup_get_top_margin (context->page_setup, GTK_UNIT_INCH);
190 cairo_translate (context->cr,
191 left * priv->dpi_x / context->pixels_per_unit_x,
192 top * priv->dpi_y / context->pixels_per_unit_y);
196 _gtk_print_context_set_page_setup (GtkPrintContext *context,
197 GtkPageSetup *page_setup)
199 g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
200 g_return_if_fail (page_setup == NULL ||
201 GTK_IS_PAGE_SETUP (page_setup));
203 g_object_ref (page_setup);
205 if (context->page_setup != NULL)
206 g_object_unref (context->page_setup);
208 context->page_setup = page_setup;
212 * gtk_print_context_get_cairo:
213 * @context: a #GtkPrintContext
215 * Obtains the cairo context that is associated with the
218 * Return value: the cairo context of @context
223 gtk_print_context_get_cairo (GtkPrintContext *context)
225 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
231 * gtk_print_context_get_page_setup:
232 * @context: a #GtkPrintContext
234 * Obtains the #GtkPageSetup that determines the page
235 * dimensions of the #GtkPrintContext.
237 * Return value: the page setup of @context
242 gtk_print_context_get_page_setup (GtkPrintContext *context)
244 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
246 return context->page_setup;
250 * gtk_print_context_get_width:
251 * @context: a #GtkPrintContext
253 * Obtains the width of the #GtkPrintContext, in pixels.
255 * Return value: the width of @context
260 gtk_print_context_get_width (GtkPrintContext *context)
262 GtkPrintOperationPrivate *priv;
265 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
267 priv = context->op->priv;
269 if (priv->use_full_page)
270 width = gtk_page_setup_get_paper_width (context->page_setup, GTK_UNIT_INCH);
272 width = gtk_page_setup_get_page_width (context->page_setup, GTK_UNIT_INCH);
274 /* Really dpi_x? What about landscape? what does dpi_x mean in that case? */
275 return width * priv->dpi_x / context->pixels_per_unit_x;
279 * gtk_print_context_get_height:
280 * @context: a #GtkPrintContext
282 * Obtains the width of the #GtkPrintContext, in pixels.
284 * Return value: the height of @context
289 gtk_print_context_get_height (GtkPrintContext *context)
291 GtkPrintOperationPrivate *priv;
294 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
296 priv = context->op->priv;
298 if (priv->use_full_page)
299 height = gtk_page_setup_get_paper_height (context->page_setup, GTK_UNIT_INCH);
301 height = gtk_page_setup_get_page_height (context->page_setup, GTK_UNIT_INCH);
303 /* Really dpi_x? What about landscape? what does dpi_x mean in that case? */
304 return height * priv->dpi_y / context->pixels_per_unit_y;
308 * gtk_print_context_get_dpi_x:
309 * @context: a #GtkPrintContext
311 * Obtains the horizontal resolution of the #GtkPrintContext,
314 * Return value: the horizontal resolution of @context
319 gtk_print_context_get_dpi_x (GtkPrintContext *context)
321 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
323 return context->op->priv->dpi_x;
327 * gtk_print_context_get_dpi_y:
328 * @context: a #GtkPrintContext
330 * Obtains the vertical resolution of the #GtkPrintContext,
333 * Return value: the vertical resolution of @context
338 gtk_print_context_get_dpi_y (GtkPrintContext *context)
340 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
342 return context->op->priv->dpi_y;
346 * gtk_print_context_get_fontmap:
347 * @context: a #GtkPrintContext
349 * Returns a #PangoFontMap that is suitable for use
350 * with the #GtkPrintContext.
352 * Return value: the font map of @context
357 gtk_print_context_get_fontmap (GtkPrintContext *context)
359 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
361 return context->fontmap;
365 * gtk_print_context_create_context:
366 * @context: a #GtkPrintContext
368 * Creates a new #PangoContext that can be used with the
371 * Return value: a new Pango context for @context
376 gtk_print_context_create_context (GtkPrintContext *context)
378 PangoContext *pango_context;
380 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
382 pango_context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (context->fontmap));
384 return pango_context;
388 * gtk_print_context_create_layout:
389 * @context: a #GtkPrintContext
391 * Creates a new #PangoLayout that is suitable for use
392 * with the #GtkPrintContext.
394 * Return value: a new Pango layout for @context
399 gtk_print_context_create_layout (GtkPrintContext *context)
401 PangoContext *pango_context;
404 g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
406 pango_context = gtk_print_context_create_context (context);
407 layout = pango_layout_new (pango_context);
409 pango_cairo_update_context (context->cr, pango_context);
410 g_object_unref (pango_context);
416 #define __GTK_PRINT_CONTEXT_C__
417 #include "gtkaliasdef.c"