示例#1
0
static void
photos_print_setup_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
{
    PhotosPrintSetup *self = PHOTOS_PRINT_SETUP (object);
    PhotosPrintSetupPrivate *priv = self->priv;

    switch (prop_id)
    {
    case PROP_NODE:
    {
        GdkPixbuf *pixbuf;

        priv->node = GEGL_NODE (g_value_dup_object (value));
        pixbuf = photos_utils_create_pixbuf_from_node (priv->node);
        if (pixbuf != NULL)
        {
            g_object_set (priv->preview, "pixbuf", pixbuf, NULL);
            g_object_unref (pixbuf);
        }
    }
    break;
    case PROP_PAGE_SETUP:
        priv->page_setup = GTK_PAGE_SETUP (g_value_get_object (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}
static void
photos_print_operation_draw_page (GtkPrintOperation *operation, GtkPrintContext *context, gint page_nr)
{
  PhotosPrintOperation *self = PHOTOS_PRINT_OPERATION (operation);
  GeglRectangle bbox;
  GdkPixbuf *pixbuf = NULL;
  GtkPageSetup *page_setup;
  cairo_t *cr;
  gdouble dpi_x;
  gdouble dpi_y;
  gdouble page_height;
  gdouble page_width;
  gdouble scale_factor_n;
  gdouble x0;
  gdouble y0;

  scale_factor_n = self->scale_factor / 100.0;
  bbox = gegl_node_get_bounding_box (self->node);

  dpi_x = gtk_print_context_get_dpi_x (context);
  dpi_y = gtk_print_context_get_dpi_x (context);

  switch (self->unit)
    {
    case GTK_UNIT_INCH:
      x0 = self->left_margin * dpi_x;
      y0 = self->top_margin  * dpi_y;
      break;
    case GTK_UNIT_MM:
      x0 = self->left_margin * dpi_x / 25.4;
      y0 = self->top_margin  * dpi_y / 25.4;
      break;
    case GTK_UNIT_NONE:
    case GTK_UNIT_POINTS:
    default:
      g_assert_not_reached ();
    }

  cr = gtk_print_context_get_cairo_context (context);
  cairo_translate (cr, x0, y0);

  page_setup = gtk_print_context_get_page_setup (context);
  page_width =  gtk_page_setup_get_page_width (page_setup, GTK_UNIT_POINTS);
  page_height = gtk_page_setup_get_page_height (page_setup, GTK_UNIT_POINTS);

  /* This is both a workaround for a bug in cairo's PDF backend, and
   * a way to ensure we are not printing outside the page margins.
   */
  cairo_rectangle (cr,
                   0,
                   0,
                   MIN (bbox.width * scale_factor_n, page_width),
                   MIN (bbox.height * scale_factor_n, page_height));
  cairo_clip (cr);
  cairo_scale (cr, scale_factor_n, scale_factor_n);

  pixbuf = photos_utils_create_pixbuf_from_node (self->node);
  if (pixbuf == NULL)
    goto out;

  gdk_cairo_set_source_pixbuf (cr, pixbuf, 0.0, 0.0);
  cairo_paint (cr);

 out:
  g_clear_object (&pixbuf);
}