gtkprintcontext.c 13.3 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * gtkprintcontext.c: Print Context
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "gtkprintoperation-private.h"
#include "gtkalias.h"

typedef struct _GtkPrintContextClass GtkPrintContextClass;

#define GTK_IS_PRINT_CONTEXT_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_CONTEXT))
#define GTK_PRINT_CONTEXT_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))
#define GTK_PRINT_CONTEXT_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_CONTEXT, GtkPrintContextClass))

#define MM_PER_INCH 25.4
#define POINTS_PER_INCH 72

struct _GtkPrintContext
{
  GObject parent_instance;

  GtkPrintOperation *op;
  cairo_t *cr;
  GtkPageSetup *page_setup;

42 43 44
  gdouble surface_dpi_x;
  gdouble surface_dpi_y;
  
45 46
  gdouble pixels_per_unit_x;
  gdouble pixels_per_unit_y;
47 48 49 50 51 52 53

  gboolean has_hard_margins;
  gdouble hard_margin_top;
  gdouble hard_margin_bottom;
  gdouble hard_margin_left;
  gdouble hard_margin_right;

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
};

struct _GtkPrintContextClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (GtkPrintContext, gtk_print_context, G_TYPE_OBJECT)

static void
gtk_print_context_finalize (GObject *object)
{
  GtkPrintContext *context = GTK_PRINT_CONTEXT (object);

  if (context->page_setup)
    g_object_unref (context->page_setup);

71 72
  if (context->cr)
    cairo_destroy (context->cr);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  
  G_OBJECT_CLASS (gtk_print_context_parent_class)->finalize (object);
}

static void
gtk_print_context_init (GtkPrintContext *context)
{
}

static void
gtk_print_context_class_init (GtkPrintContextClass *class)
{
  GObjectClass *gobject_class = (GObjectClass *)class;

  gobject_class->finalize = gtk_print_context_finalize;
}


GtkPrintContext *
_gtk_print_context_new (GtkPrintOperation *op)
{
  GtkPrintContext *context;
95

96 97 98
  context = g_object_new (GTK_TYPE_PRINT_CONTEXT, NULL);

  context->op = op;
99
  context->cr = NULL;
100
  context->has_hard_margins = FALSE;
101 102 103
  
  return context;
}
104 105 106 107 108 109 110

static PangoFontMap *
_gtk_print_context_get_fontmap (GtkPrintContext *context)
{
  return pango_cairo_font_map_get_default ();
}

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
/**
 * gtk_print_context_set_cairo_context:
 * @context: a #GtkPrintContext
 * @cr: the cairo context
 * @dpi_x: the horizontal resolution to use with @cr
 * @dpi_y: the vertical resolution to use with @cr
 *
 * Sets a new cairo context on a print context. 
 * 
 * This function is intended to be used when implementing
 * an internal print preview, it is not needed for printing,
 * since GTK+ itself creates a suitable cairo context in that
 * case.
 *
 * Since: 2.10 
 */
127 128 129 130 131 132 133 134 135 136 137 138
void
gtk_print_context_set_cairo_context (GtkPrintContext *context,
				     cairo_t         *cr,
				     double           dpi_x,
				     double           dpi_y)
{
  if (context->cr)
    cairo_destroy (context->cr);

  context->cr = cairo_reference (cr);
  context->surface_dpi_x = dpi_x;
  context->surface_dpi_y = dpi_y;
139

140
  switch (context->op->priv->unit)
141 142 143 144 145 146 147 148
    {
    default:
    case GTK_UNIT_PIXEL:
      /* Do nothing, this is the cairo default unit */
      context->pixels_per_unit_x = 1.0;
      context->pixels_per_unit_y = 1.0;
      break;
    case GTK_UNIT_POINTS:
149 150
      context->pixels_per_unit_x = dpi_x / POINTS_PER_INCH;
      context->pixels_per_unit_y = dpi_y / POINTS_PER_INCH;
151 152
      break;
    case GTK_UNIT_INCH:
153 154
      context->pixels_per_unit_x = dpi_x;
      context->pixels_per_unit_y = dpi_y;
155 156
      break;
    case GTK_UNIT_MM:
157 158
      context->pixels_per_unit_x = dpi_x / MM_PER_INCH;
      context->pixels_per_unit_y = dpi_y / MM_PER_INCH;
159 160 161 162 163 164 165
      break;
    }
  cairo_scale (context->cr,
	       context->pixels_per_unit_x,
	       context->pixels_per_unit_y);
}

166

167 168 169 170 171 172 173 174 175 176 177
void
_gtk_print_context_rotate_according_to_orientation (GtkPrintContext *context)
{
  cairo_t *cr = context->cr;
  cairo_matrix_t matrix;
  GtkPaperSize *paper_size;
  gdouble width, height;

  paper_size = gtk_page_setup_get_paper_size (context->page_setup);

  width = gtk_paper_size_get_width (paper_size, GTK_UNIT_INCH);
178
  width = width * context->surface_dpi_x / context->pixels_per_unit_x;
179
  height = gtk_paper_size_get_height (paper_size, GTK_UNIT_INCH);
180
  height = height * context->surface_dpi_y / context->pixels_per_unit_y;
181 182 183 184 185 186 187
  
  switch (gtk_page_setup_get_orientation (context->page_setup))
    {
    default:
    case GTK_PAGE_ORIENTATION_PORTRAIT:
      break;
    case GTK_PAGE_ORIENTATION_LANDSCAPE:
188
      cairo_translate (cr, 0, height);
189
      cairo_matrix_init (&matrix,
190 191 192
			 0, -1,
			 1,  0,
			 0,  0);
193 194 195 196 197 198 199 200 201 202 203
      cairo_transform (cr, &matrix);
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
      cairo_translate (cr, width, height);
      cairo_matrix_init (&matrix,
			 -1,  0,
			  0, -1,
			  0,  0);
      cairo_transform (cr, &matrix);
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
204
      cairo_translate (cr, width, 0);
205
      cairo_matrix_init (&matrix,
206 207 208
			  0,  1,
			 -1,  0,
			  0,  0);
209 210 211 212 213 214 215 216
      cairo_transform (cr, &matrix);
      break;
    }
}

void
_gtk_print_context_translate_into_margin (GtkPrintContext *context)
{
217
  GtkPrintOperationPrivate *priv;
218 219 220 221
  gdouble left, top;

  g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));

222 223
  priv = context->op->priv;

224 225 226 227 228 229
  /* We do it this way to also handle GTK_UNIT_PIXELS */
  
  left = gtk_page_setup_get_left_margin (context->page_setup, GTK_UNIT_INCH);
  top = gtk_page_setup_get_top_margin (context->page_setup, GTK_UNIT_INCH);

  cairo_translate (context->cr,
230 231
		   left * context->surface_dpi_x / context->pixels_per_unit_x,
		   top * context->surface_dpi_y / context->pixels_per_unit_y);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
}

void
_gtk_print_context_set_page_setup (GtkPrintContext *context,
				   GtkPageSetup    *page_setup)
{
  g_return_if_fail (GTK_IS_PRINT_CONTEXT (context));
  g_return_if_fail (page_setup == NULL ||
		    GTK_IS_PAGE_SETUP (page_setup));
  
  g_object_ref (page_setup);

  if (context->page_setup != NULL)
    g_object_unref (context->page_setup);

  context->page_setup = page_setup;
}

/**
251
 * gtk_print_context_get_cairo_context:
252 253 254 255 256 257 258 259 260 261
 * @context: a #GtkPrintContext
 *
 * Obtains the cairo context that is associated with the
 * #GtkPrintContext.
 *
 * Return value: the cairo context of @context
 *
 * Since: 2.10
 */
cairo_t *
262
gtk_print_context_get_cairo_context (GtkPrintContext *context)
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
{
  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);

  return context->cr;
}

/**
 * gtk_print_context_get_page_setup:
 * @context: a #GtkPrintContext
 *
 * Obtains the #GtkPageSetup that determines the page
 * dimensions of the #GtkPrintContext.
 *
 * Return value: the page setup of @context
 *
 * Since: 2.10
 */
GtkPageSetup *
gtk_print_context_get_page_setup (GtkPrintContext *context)
{
  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);

  return context->page_setup;
}

/**
 * gtk_print_context_get_width:
 * @context: a #GtkPrintContext
 *
 * Obtains the width of the #GtkPrintContext, in pixels.
 *
 * Return value: the width of @context
 *
 * Since: 2.10 
 */
gdouble
gtk_print_context_get_width (GtkPrintContext *context)
{
301
  GtkPrintOperationPrivate *priv;
302 303 304 305
  gdouble width;

  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);

306 307 308
  priv = context->op->priv;

  if (priv->use_full_page)
309 310 311 312 313
    width = gtk_page_setup_get_paper_width (context->page_setup, GTK_UNIT_INCH);
  else
    width = gtk_page_setup_get_page_width (context->page_setup, GTK_UNIT_INCH);

  /* Really dpi_x? What about landscape? what does dpi_x mean in that case? */
314
  return width * context->surface_dpi_x / context->pixels_per_unit_x;
315 316 317 318 319 320
}

/**
 * gtk_print_context_get_height:
 * @context: a #GtkPrintContext
 * 
321
 * Obtains the height of the #GtkPrintContext, in pixels.
322 323 324 325 326 327 328 329
 *
 * Return value: the height of @context
 *
 * Since: 2.10
 */
gdouble
gtk_print_context_get_height (GtkPrintContext *context)
{
330
  GtkPrintOperationPrivate *priv;
331 332 333 334
  gdouble height;

  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);

335 336 337
  priv = context->op->priv;

  if (priv->use_full_page)
338 339 340 341
    height = gtk_page_setup_get_paper_height (context->page_setup, GTK_UNIT_INCH);
  else
    height = gtk_page_setup_get_page_height (context->page_setup, GTK_UNIT_INCH);

342 343
  /* Really dpi_y? What about landscape? what does dpi_y mean in that case? */
  return height * context->surface_dpi_y / context->pixels_per_unit_y;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
}

/**
 * gtk_print_context_get_dpi_x:
 * @context: a #GtkPrintContext
 * 
 * Obtains the horizontal resolution of the #GtkPrintContext,
 * in dots per inch.
 *
 * Return value: the horizontal resolution of @context
 *
 * Since: 2.10
 */
gdouble
gtk_print_context_get_dpi_x (GtkPrintContext *context)
{
  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);

362
  return context->surface_dpi_x;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
}

/**
 * gtk_print_context_get_dpi_y:
 * @context: a #GtkPrintContext
 * 
 * Obtains the vertical resolution of the #GtkPrintContext,
 * in dots per inch.
 *
 * Return value: the vertical resolution of @context
 *
 * Since: 2.10
 */
gdouble
gtk_print_context_get_dpi_y (GtkPrintContext *context)
{
  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), 0);

381
  return context->surface_dpi_y;
382 383
}

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
/**
 * gtk_print_context_get_hard_margins:
 * @context: a #GtkPrintContext
 * @top: top hardware printer margin
 * @bottom: bottom hardware printer margin
 * @left: left hardware printer margin
 * @right: right hardware printer margin
 *
 * Obtains the hardware printer margins of the #GtkPrintContext, in units.
 *
 * Return value: %TRUE if the hard margins were retrieved
 *
 * Since: 2.20
 */
gboolean
gtk_print_context_get_hard_margins (GtkPrintContext *context,
				    gdouble         *top,
				    gdouble         *bottom,
				    gdouble         *left,
				    gdouble         *right)
{
  if (context->has_hard_margins)
    {
      *top    = context->hard_margin_top / context->pixels_per_unit_y;
      *bottom = context->hard_margin_bottom / context->pixels_per_unit_y;
      *left   = context->hard_margin_left / context->pixels_per_unit_x;
      *right  = context->hard_margin_right / context->pixels_per_unit_x;
    }

  return context->has_hard_margins;
}

/**
 * gtk_print_context_set_hard_margins:
 * @context: a #GtkPrintContext
 * @top: top hardware printer margin
 * @bottom: bottom hardware printer margin
 * @left: left hardware printer margin
 * @right: right hardware printer margin
 *
 * set the hard margins in pixel coordinates
 */
void
_gtk_print_context_set_hard_margins (GtkPrintContext *context,
				     gdouble          top,
				     gdouble          bottom,
				     gdouble          left,
				     gdouble          right)
{
  context->hard_margin_top    = top;
  context->hard_margin_bottom = bottom;
  context->hard_margin_left   = left;
  context->hard_margin_right  = right;
  context->has_hard_margins   = TRUE;
}

440
/**
441
 * gtk_print_context_get_pango_fontmap:
442 443 444 445 446 447 448 449 450 451
 * @context: a #GtkPrintContext
 *
 * Returns a #PangoFontMap that is suitable for use 
 * with the #GtkPrintContext.
 *
 * Return value: the font map of @context
 *
 * Since: 2.10
 */
PangoFontMap *
452
gtk_print_context_get_pango_fontmap (GtkPrintContext *context)
453 454 455
{
  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);

456
  return _gtk_print_context_get_fontmap (context);
457 458 459
}

/**
460
 * gtk_print_context_create_pango_context:
461 462 463 464 465 466 467 468 469 470
 * @context: a #GtkPrintContext 
 *
 * Creates a new #PangoContext that can be used with the
 * #GtkPrintContext.
 *
 * Return value: a new Pango context for @context
 * 
 * Since: 2.10
 */
PangoContext *
471
gtk_print_context_create_pango_context (GtkPrintContext *context)
472 473
{
  PangoContext *pango_context;
474
  cairo_font_options_t *options;
475 476 477

  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);
  
478
  pango_context = pango_cairo_font_map_create_context (PANGO_CAIRO_FONT_MAP (_gtk_print_context_get_fontmap (context)));
479 480 481 482 483

  options = cairo_font_options_create ();
  cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
  pango_cairo_context_set_font_options (pango_context, options);
  cairo_font_options_destroy (options);
484
  
485 486 487 488 489
  /* We use the unit-scaled resolution, as we still want 
   * fonts given in points to work 
   */
  pango_cairo_context_set_resolution (pango_context,
				      context->surface_dpi_y / context->pixels_per_unit_y);
490 491 492 493
  return pango_context;
}

/**
494
 * gtk_print_context_create_pango_layout:
495 496 497 498 499 500 501 502 503 504
 * @context: a #GtkPrintContext
 *
 * Creates a new #PangoLayout that is suitable for use
 * with the #GtkPrintContext.
 * 
 * Return value: a new Pango layout for @context
 *
 * Since: 2.10
 */
PangoLayout *
505
gtk_print_context_create_pango_layout (GtkPrintContext *context)
506 507 508 509 510 511
{
  PangoContext *pango_context;
  PangoLayout *layout;

  g_return_val_if_fail (GTK_IS_PRINT_CONTEXT (context), NULL);

512
  pango_context = gtk_print_context_create_pango_context (context);
513 514 515 516 517 518 519 520 521 522 523
  layout = pango_layout_new (pango_context);

  pango_cairo_update_context (context->cr, pango_context);
  g_object_unref (pango_context);

  return layout;
}


#define __GTK_PRINT_CONTEXT_C__
#include "gtkaliasdef.c"