]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintcontext.c
7464933ed40b41cfe0a2b23a02961986a7cb9872
[~andy/gtk] / gtk / gtkprintcontext.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintcontext.c: Print Context
3  * Copyright (C) 2006, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "gtkprintoperation-private.h"
23 #include "gtkalias.h"
24
25 typedef struct _GtkPrintContextClass GtkPrintContextClass;
26
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))
30
31 #define MM_PER_INCH 25.4
32 #define POINTS_PER_INCH 72
33
34 struct _GtkPrintContext
35 {
36   GObject parent_instance;
37
38   GtkPrintOperation *op;
39   cairo_t *cr;
40   GtkPageSetup *page_setup;
41   PangoFontMap *fontmap;
42
43   gdouble pixels_per_unit_x;
44   gdouble pixels_per_unit_y;
45 };
46
47 struct _GtkPrintContextClass
48 {
49   GObjectClass parent_class;
50 };
51
52 G_DEFINE_TYPE (GtkPrintContext, gtk_print_context, G_TYPE_OBJECT)
53
54 static void
55 gtk_print_context_finalize (GObject *object)
56 {
57   GtkPrintContext *context = GTK_PRINT_CONTEXT (object);
58
59   g_object_unref (context->fontmap);
60   if (context->page_setup)
61     g_object_unref (context->page_setup);
62
63   cairo_destroy (context->cr);
64
65   
66   G_OBJECT_CLASS (gtk_print_context_parent_class)->finalize (object);
67 }
68
69 static void
70 gtk_print_context_init (GtkPrintContext *context)
71 {
72 }
73
74 static void
75 gtk_print_context_class_init (GtkPrintContextClass *class)
76 {
77   GObjectClass *gobject_class = (GObjectClass *)class;
78
79   gobject_class->finalize = gtk_print_context_finalize;
80 }
81
82
83 GtkPrintContext *
84 _gtk_print_context_new (GtkPrintOperation *op)
85 {
86   GtkPrintOperationPrivate *priv = op->priv;
87   GtkPrintContext *context;
88
89   context = g_object_new (GTK_TYPE_PRINT_CONTEXT, NULL);
90
91   context->op = op;
92   context->cr = cairo_create (priv->surface);
93
94   switch (priv->unit)
95     {
96     default:
97     case GTK_UNIT_PIXEL:
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;
101       break;
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;
105       break;
106     case GTK_UNIT_INCH:
107       context->pixels_per_unit_x = priv->dpi_x;
108       context->pixels_per_unit_y = priv->dpi_y;
109       break;
110     case GTK_UNIT_MM:
111       context->pixels_per_unit_x = priv->dpi_x / MM_PER_INCH;
112       context->pixels_per_unit_y = priv->dpi_y / MM_PER_INCH;
113       break;
114     }
115   cairo_scale (context->cr,
116                context->pixels_per_unit_x,
117                context->pixels_per_unit_y);
118     
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);
123   
124   return context;
125 }
126
127 void
128 _gtk_print_context_rotate_according_to_orientation (GtkPrintContext *context)
129 {
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;
135
136   paper_size = gtk_page_setup_get_paper_size (context->page_setup);
137
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;
142   
143   switch (gtk_page_setup_get_orientation (context->page_setup))
144     {
145     default:
146     case GTK_PAGE_ORIENTATION_PORTRAIT:
147       break;
148     case GTK_PAGE_ORIENTATION_LANDSCAPE:
149       cairo_translate (cr, width, 0);
150       cairo_matrix_init (&matrix,
151                           0,  1,
152                          -1,  0,
153                           0,  0);
154       cairo_transform (cr, &matrix);
155       break;
156     case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
157       cairo_translate (cr, width, height);
158       cairo_matrix_init (&matrix,
159                          -1,  0,
160                           0, -1,
161                           0,  0);
162       cairo_transform (cr, &matrix);
163       break;
164     case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
165       cairo_translate (cr, 0, height);
166       cairo_matrix_init (&matrix,
167                          0, -1,
168                          1,  0,
169                          0,  0);
170       cairo_transform (cr, &matrix);
171       break;
172     }
173 }
174
175 void
176 _gtk_print_context_translate_into_margin (GtkPrintContext *context)
177 {
178   GtkPrintOperationPrivate *priv;
179   gdouble left, top;
180
181   g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
182
183   priv = context->op->priv;
184
185   /* We do it this way to also handle GTK_UNIT_PIXELS */
186   
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);
189
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);
193 }
194
195 void
196 _gtk_print_context_set_page_setup (GtkPrintContext *context,
197                                    GtkPageSetup    *page_setup)
198 {
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));
202   
203   g_object_ref (page_setup);
204
205   if (context->page_setup != NULL)
206     g_object_unref (context->page_setup);
207
208   context->page_setup = page_setup;
209 }
210
211 /**
212  * gtk_print_context_get_cairo:
213  * @context: a #GtkPrintContext
214  *
215  * Obtains the cairo context that is associated with the
216  * #GtkPrintContext.
217  *
218  * Return value: the cairo context of @context
219  *
220  * Since: 2.10
221  */
222 cairo_t *
223 gtk_print_context_get_cairo (GtkPrintContext *context)
224 {
225   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
226
227   return context->cr;
228 }
229
230 /**
231  * gtk_print_context_get_page_setup:
232  * @context: a #GtkPrintContext
233  *
234  * Obtains the #GtkPageSetup that determines the page
235  * dimensions of the #GtkPrintContext.
236  *
237  * Return value: the page setup of @context
238  *
239  * Since: 2.10
240  */
241 GtkPageSetup *
242 gtk_print_context_get_page_setup (GtkPrintContext *context)
243 {
244   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
245
246   return context->page_setup;
247 }
248
249 /**
250  * gtk_print_context_get_width:
251  * @context: a #GtkPrintContext
252  *
253  * Obtains the width of the #GtkPrintContext, in pixels.
254  *
255  * Return value: the width of @context
256  *
257  * Since: 2.10 
258  */
259 gdouble
260 gtk_print_context_get_width (GtkPrintContext *context)
261 {
262   GtkPrintOperationPrivate *priv;
263   gdouble width;
264
265   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
266
267   priv = context->op->priv;
268
269   if (priv->use_full_page)
270     width = gtk_page_setup_get_paper_width (context->page_setup, GTK_UNIT_INCH);
271   else
272     width = gtk_page_setup_get_page_width (context->page_setup, GTK_UNIT_INCH);
273
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;
276 }
277
278 /**
279  * gtk_print_context_get_height:
280  * @context: a #GtkPrintContext
281  * 
282  * Obtains the width of the #GtkPrintContext, in pixels.
283  *
284  * Return value: the height of @context
285  *
286  * Since: 2.10
287  */
288 gdouble
289 gtk_print_context_get_height (GtkPrintContext *context)
290 {
291   GtkPrintOperationPrivate *priv;
292   gdouble height;
293
294   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
295
296   priv = context->op->priv;
297
298   if (priv->use_full_page)
299     height = gtk_page_setup_get_paper_height (context->page_setup, GTK_UNIT_INCH);
300   else
301     height = gtk_page_setup_get_page_height (context->page_setup, GTK_UNIT_INCH);
302
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;
305 }
306
307 /**
308  * gtk_print_context_get_dpi_x:
309  * @context: a #GtkPrintContext
310  * 
311  * Obtains the horizontal resolution of the #GtkPrintContext,
312  * in dots per inch.
313  *
314  * Return value: the horizontal resolution of @context
315  *
316  * Since: 2.10
317  */
318 gdouble
319 gtk_print_context_get_dpi_x (GtkPrintContext *context)
320 {
321   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
322
323   return context->op->priv->dpi_x;
324 }
325
326 /**
327  * gtk_print_context_get_dpi_y:
328  * @context: a #GtkPrintContext
329  * 
330  * Obtains the vertical resolution of the #GtkPrintContext,
331  * in dots per inch.
332  *
333  * Return value: the vertical resolution of @context
334  *
335  * Since: 2.10
336  */
337 gdouble
338 gtk_print_context_get_dpi_y (GtkPrintContext *context)
339 {
340   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);
341
342   return context->op->priv->dpi_y;
343 }
344
345 /**
346  * gtk_print_context_get_fontmap:
347  * @context: a #GtkPrintContext
348  *
349  * Returns a #PangoFontMap that is suitable for use 
350  * with the #GtkPrintContext.
351  *
352  * Return value: the font map of @context
353  *
354  * Since: 2.10
355  */
356 PangoFontMap *
357 gtk_print_context_get_fontmap (GtkPrintContext *context)
358 {
359   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
360
361   return context->fontmap;
362 }
363
364 /**
365  * gtk_print_context_create_context:
366  * @context: a #GtkPrintContext 
367  *
368  * Creates a new #PangoContext that can be used with the
369  * #GtkPrintContext.
370  *
371  * Return value: a new Pango context for @context
372  * 
373  * Since: 2.10
374  */
375 PangoContext *
376 gtk_print_context_create_context (GtkPrintContext *context)
377 {
378   PangoContext *pango_context;
379
380   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
381   
382   pango_context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (context->fontmap));
383   
384   return pango_context;
385 }
386
387 /**
388  * gtk_print_context_create_layout:
389  * @context: a #GtkPrintContext
390  *
391  * Creates a new #PangoLayout that is suitable for use
392  * with the #GtkPrintContext.
393  * 
394  * Return value: a new Pango layout for @context
395  *
396  * Since: 2.10
397  */
398 PangoLayout *
399 gtk_print_context_create_layout (GtkPrintContext *context)
400 {
401   PangoContext *pango_context;
402   PangoLayout *layout;
403
404   g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
405
406   pango_context = gtk_print_context_create_context (context);
407   layout = pango_layout_new (pango_context);
408
409   pango_cairo_update_context (context->cr, pango_context);
410   g_object_unref (pango_context);
411
412   return layout;
413 }
414
415
416 #define __GTK_PRINT_CONTEXT_C__
417 #include "gtkaliasdef.c"