Blame SOURCES/0001-Add-_gtk_printer_get_hard_margins_for_paper_size.patch

36c29e
From fff08fa319dff8542a3f3481a5273523bdfd5dd9 Mon Sep 17 00:00:00 2001
36c29e
From: Adrian Johnson <ajohnson@redneon.com>
36c29e
Date: Mon, 16 Apr 2018 13:53:44 +0200
36c29e
Subject: [PATCH] Add _gtk_printer_get_hard_margins_for_paper_size()
36c29e
36c29e
to retreive paper size specific hard margins and use this
36c29e
to set the hard margins in the print context.
36c29e
(modified by Marek Kasik <mkasik@redhat.com>)
36c29e
36c29e
https://bugzilla.gnome.org/show_bug.cgi?id=686109
36c29e
---
36c29e
 gtk/gtkprintbackend.c                         | 18 +++++++
36c29e
 gtk/gtkprintbackend.h                         |  8 +++-
36c29e
 gtk/gtkprinter-private.h                      |  6 +++
36c29e
 gtk/gtkprinter.c                              | 13 +++++
36c29e
 gtk/gtkprintoperation-unix.c                  |  4 +-
36c29e
 .../printbackends/cups/gtkprintbackendcups.c  | 48 +++++++++++++++++++
36c29e
 6 files changed, 95 insertions(+), 2 deletions(-)
36c29e
36c29e
diff --git a/gtk/gtkprintbackend.c b/gtk/gtkprintbackend.c
36c29e
index cccdbcaaf7..1a730ee3a2 100644
36c29e
--- a/gtk/gtkprintbackend.c
36c29e
+++ b/gtk/gtkprintbackend.c
36c29e
@@ -350,6 +350,12 @@ static gboolean             fallback_printer_get_hard_margins      (GtkPrinter
36c29e
                                                                     gdouble             *bottom,
36c29e
                                                                     gdouble             *left,
36c29e
                                                                     gdouble             *right);
36c29e
+static gboolean             fallback_printer_get_hard_margins_for_paper_size (GtkPrinter          *printer,
36c29e
+									      GtkPaperSize        *paper_size,
36c29e
+									      gdouble             *top,
36c29e
+									      gdouble             *bottom,
36c29e
+									      gdouble             *left,
36c29e
+									      gdouble             *right);
36c29e
 static GList *              fallback_printer_list_papers           (GtkPrinter          *printer);
36c29e
 static GtkPageSetup *       fallback_printer_get_default_page_size (GtkPrinter          *printer);
36c29e
 static GtkPrintCapabilities fallback_printer_get_capabilities      (GtkPrinter          *printer);
36c29e
@@ -376,6 +382,7 @@ gtk_print_backend_class_init (GtkPrintBackendClass *class)
36c29e
   class->printer_request_details = fallback_printer_request_details;
36c29e
   class->printer_mark_conflicts = fallback_printer_mark_conflicts;
36c29e
   class->printer_get_hard_margins = fallback_printer_get_hard_margins;
36c29e
+  class->printer_get_hard_margins_for_paper_size = fallback_printer_get_hard_margins_for_paper_size;
36c29e
   class->printer_list_papers = fallback_printer_list_papers;
36c29e
   class->printer_get_default_page_size = fallback_printer_get_default_page_size;
36c29e
   class->printer_get_capabilities = fallback_printer_get_capabilities;
36c29e
@@ -499,6 +506,17 @@ fallback_printer_get_hard_margins (GtkPrinter *printer,
36c29e
   return FALSE;
36c29e
 }
36c29e
 
36c29e
+static gboolean
36c29e
+fallback_printer_get_hard_margins_for_paper_size (GtkPrinter   *printer,
36c29e
+						  GtkPaperSize *paper_size,
36c29e
+						  gdouble      *top,
36c29e
+						  gdouble      *bottom,
36c29e
+						  gdouble      *left,
36c29e
+						  gdouble      *right)
36c29e
+{
36c29e
+  return FALSE;
36c29e
+}
36c29e
+
36c29e
 static GList *
36c29e
 fallback_printer_list_papers (GtkPrinter *printer)
36c29e
 {
36c29e
diff --git a/gtk/gtkprintbackend.h b/gtk/gtkprintbackend.h
36c29e
index f4524e8965..2fa236a8ac 100644
36c29e
--- a/gtk/gtkprintbackend.h
36c29e
+++ b/gtk/gtkprintbackend.h
36c29e
@@ -133,11 +133,17 @@ struct _GtkPrintBackendClass
36c29e
                                                               gchar              **auth_info,
36c29e
                                                               gboolean             store_auth_info);
36c29e
 
36c29e
+  gboolean              (*printer_get_hard_margins_for_paper_size) (GtkPrinter    *printer,
36c29e
+								    GtkPaperSize  *paper_size,
36c29e
+								    gdouble       *top,
36c29e
+								    gdouble       *bottom,
36c29e
+								    gdouble       *left,
36c29e
+								    gdouble       *right);
36c29e
+
36c29e
   /* Padding for future expansion */
36c29e
   void (*_gtk_reserved1) (void);
36c29e
   void (*_gtk_reserved2) (void);
36c29e
   void (*_gtk_reserved3) (void);
36c29e
-  void (*_gtk_reserved4) (void);
36c29e
 };
36c29e
 
36c29e
 GDK_AVAILABLE_IN_ALL
36c29e
diff --git a/gtk/gtkprinter-private.h b/gtk/gtkprinter-private.h
36c29e
index 51ec1a861f..a2c551156a 100644
36c29e
--- a/gtk/gtkprinter-private.h
36c29e
+++ b/gtk/gtkprinter-private.h
36c29e
@@ -44,6 +44,12 @@ cairo_surface_t *    _gtk_printer_create_cairo_surface      (GtkPrinter
36c29e
 							     gdouble              height,
36c29e
 							     GIOChannel          *cache_io);
36c29e
 GHashTable *         _gtk_printer_get_custom_widgets        (GtkPrinter          *printer);
36c29e
+gboolean             _gtk_printer_get_hard_margins_for_paper_size (GtkPrinter       *printer,
36c29e
+								   GtkPaperSize     *paper_size,
36c29e
+								   gdouble          *top,
36c29e
+								   gdouble          *bottom,
36c29e
+								   gdouble          *left,
36c29e
+								   gdouble          *right);
36c29e
 
36c29e
 /* GtkPrintJob private methods: */
36c29e
 GDK_AVAILABLE_IN_ALL
36c29e
diff --git a/gtk/gtkprinter.c b/gtk/gtkprinter.c
36c29e
index 5e145ddd69..579fba0859 100644
36c29e
--- a/gtk/gtkprinter.c
36c29e
+++ b/gtk/gtkprinter.c
36c29e
@@ -941,6 +941,19 @@ _gtk_printer_create_cairo_surface (GtkPrinter       *printer,
36c29e
 						      width, height, cache_io);
36c29e
 }
36c29e
 
36c29e
+gboolean
36c29e
+_gtk_printer_get_hard_margins_for_paper_size (GtkPrinter   *printer,
36c29e
+					      GtkPaperSize *paper_size,
36c29e
+					      gdouble      *top,
36c29e
+					      gdouble      *bottom,
36c29e
+					      gdouble      *left,
36c29e
+					      gdouble      *right)
36c29e
+{
36c29e
+  GtkPrintBackendClass *backend_class = GTK_PRINT_BACKEND_GET_CLASS (printer->priv->backend);
36c29e
+
36c29e
+  return backend_class->printer_get_hard_margins_for_paper_size (printer, paper_size, top, bottom, left, right);
36c29e
+}
36c29e
+
36c29e
 /**
36c29e
  * gtk_printer_list_papers:
36c29e
  * @printer: a #GtkPrinter
36c29e
diff --git a/gtk/gtkprintoperation-unix.c b/gtk/gtkprintoperation-unix.c
36c29e
index 9baf69438e..5dfdfe7e71 100644
36c29e
--- a/gtk/gtkprintoperation-unix.c
36c29e
+++ b/gtk/gtkprintoperation-unix.c
36c29e
@@ -32,6 +32,7 @@
36c29e
 #include "gtkprintoperation-private.h"
36c29e
 #include "gtkprintoperation-portal.h"
36c29e
 #include "gtkmessagedialog.h"
36c29e
+#include "gtkprinter-private.h"
36c29e
 
36c29e
 #include <cairo-pdf.h>
36c29e
 #include <cairo-ps.h>
36c29e
@@ -536,7 +537,8 @@ finish_print (PrintResponseData *rdata,
36c29e
 
36c29e
       if (gtk_print_settings_get_number_up (settings) < 2)
36c29e
         {
36c29e
-	  if (printer && gtk_printer_get_hard_margins (printer, &top, &bottom, &left, &right))
36c29e
+	  if (printer && (_gtk_printer_get_hard_margins_for_paper_size (printer, gtk_page_setup_get_paper_size (page_setup), &top, &bottom, &left, &right) ||
36c29e
+			  gtk_printer_get_hard_margins (printer, &top, &bottom, &left, &right)))
36c29e
 	    _gtk_print_context_set_hard_margins (priv->print_context, top, bottom, left, right);
36c29e
 	}
36c29e
       else
36c29e
diff --git a/modules/printbackends/cups/gtkprintbackendcups.c b/modules/printbackends/cups/gtkprintbackendcups.c
36c29e
index 7d061c5fc9..ccd08e50a0 100644
36c29e
--- a/modules/printbackends/cups/gtkprintbackendcups.c
36c29e
+++ b/modules/printbackends/cups/gtkprintbackendcups.c
36c29e
@@ -195,6 +195,12 @@ static gboolean             cups_printer_get_hard_margins          (GtkPrinter
36c29e
 								    gdouble                           *bottom,
36c29e
 								    gdouble                           *left,
36c29e
 								    gdouble                           *right);
36c29e
+static gboolean             cups_printer_get_hard_margins_for_paper_size (GtkPrinter                  *printer,
36c29e
+									  GtkPaperSize                *paper_size,
36c29e
+									  gdouble                     *top,
36c29e
+									  gdouble                     *bottom,
36c29e
+									  gdouble                     *left,
36c29e
+									  gdouble                     *right);
36c29e
 static GtkPrintCapabilities cups_printer_get_capabilities          (GtkPrinter                        *printer);
36c29e
 static void                 set_option_from_settings               (GtkPrinterOption                  *option,
36c29e
 								    GtkPrintSettings                  *setting);
36c29e
@@ -372,6 +378,7 @@ gtk_print_backend_cups_class_init (GtkPrintBackendCupsClass *class)
36c29e
   backend_class->printer_list_papers = cups_printer_list_papers;
36c29e
   backend_class->printer_get_default_page_size = cups_printer_get_default_page_size;
36c29e
   backend_class->printer_get_hard_margins = cups_printer_get_hard_margins;
36c29e
+  backend_class->printer_get_hard_margins_for_paper_size = cups_printer_get_hard_margins_for_paper_size;
36c29e
   backend_class->printer_get_capabilities = cups_printer_get_capabilities;
36c29e
   backend_class->set_password = gtk_print_backend_cups_set_password;
36c29e
 }
36c29e
@@ -6741,6 +6748,47 @@ cups_printer_get_hard_margins (GtkPrinter *printer,
36c29e
   return result;
36c29e
 }
36c29e
 
36c29e
+static gboolean
36c29e
+cups_printer_get_hard_margins_for_paper_size (GtkPrinter   *printer,
36c29e
+					      GtkPaperSize *paper_size,
36c29e
+					      gdouble      *top,
36c29e
+					      gdouble      *bottom,
36c29e
+					      gdouble      *left,
36c29e
+					      gdouble      *right)
36c29e
+{
36c29e
+  ppd_file_t *ppd_file;
36c29e
+  ppd_size_t *size;
36c29e
+  const gchar *paper_name;
36c29e
+  int i;
36c29e
+
36c29e
+  ppd_file = gtk_printer_cups_get_ppd (GTK_PRINTER_CUPS (printer));
36c29e
+  if (ppd_file == NULL)
36c29e
+    return FALSE;
36c29e
+
36c29e
+  paper_name = gtk_paper_size_get_ppd_name (paper_size);
36c29e
+
36c29e
+  for (i = 0; i < ppd_file->num_sizes; i++)
36c29e
+    {
36c29e
+      size = &ppd_file->sizes[i];
36c29e
+      if (g_strcmp0(size->name, paper_name) == 0)
36c29e
+        {
36c29e
+	   *top = size->length - size->top;
36c29e
+	   *bottom = size->bottom;
36c29e
+	   *left = size->left;
36c29e
+	   *right = size->width - size->right;
36c29e
+	   return TRUE;
36c29e
+	}
36c29e
+    }
36c29e
+
36c29e
+  /* Custom size */
36c29e
+  *left = ppd_file->custom_margins[0];
36c29e
+  *bottom = ppd_file->custom_margins[1];
36c29e
+  *right = ppd_file->custom_margins[2];
36c29e
+  *top = ppd_file->custom_margins[3];
36c29e
+
36c29e
+  return TRUE;
36c29e
+}
36c29e
+
36c29e
 static GtkPrintCapabilities
36c29e
 cups_printer_get_capabilities (GtkPrinter *printer)
36c29e
 {
36c29e
-- 
36c29e
2.17.1
36c29e