1 /* GIMP - The GNU Image Manipulation Program
2  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16  */
17 
18 #include "config.h"
19 
20 #include <string.h>
21 
22 #include <libgimp/gimp.h>
23 #include <libgimp/gimpui.h>
24 
25 #include "print.h"
26 #include "print-draw-page.h"
27 
28 #include "libgimp/stdplugins-intl.h"
29 
30 
31 static cairo_surface_t * print_surface_from_drawable (gint32        drawable_ID,
32                                                       GError      **error);
33 
34 static void              print_draw_crop_marks       (GtkPrintContext *context,
35                                                       gdouble          x,
36                                                       gdouble          y,
37                                                       gdouble          w,
38                                                       gdouble          h);
39 
40 gboolean
print_draw_page(GtkPrintContext * context,PrintData * data,GError ** error)41 print_draw_page (GtkPrintContext *context,
42                  PrintData       *data,
43                  GError         **error)
44 {
45   cairo_t         *cr = gtk_print_context_get_cairo_context (context);
46   cairo_surface_t *surface;
47 
48   surface = print_surface_from_drawable (data->drawable_id, error);
49 
50   if (surface)
51     {
52       gint    width;
53       gint    height;
54       gdouble scale_x;
55       gdouble scale_y;
56 
57       /*  create a white rectangle covering the entire page, just
58        *  to be safe; see bug #777233.
59        */
60       cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 1.0);
61       cairo_paint (cr);
62 
63       width  = cairo_image_surface_get_width (surface);
64       height = cairo_image_surface_get_height (surface);
65 
66       scale_x = gtk_print_context_get_dpi_x (context) / data->xres;
67       scale_y = gtk_print_context_get_dpi_y (context) / data->yres;
68 
69       cairo_translate (cr, data->offset_x, data->offset_y);
70 
71       if (data->draw_crop_marks)
72         print_draw_crop_marks (context,
73                                0, 0, width * scale_x, height * scale_y);
74 
75       cairo_scale (cr, scale_x, scale_y);
76       cairo_rectangle (cr, 0, 0, width, height);
77       cairo_set_source_surface (cr, surface, 0, 0);
78       cairo_fill (cr);
79 
80       cairo_surface_destroy (surface);
81 
82       return TRUE;
83     }
84 
85   return FALSE;
86 }
87 
88 static cairo_surface_t *
print_surface_from_drawable(gint32 drawable_ID,GError ** error)89 print_surface_from_drawable (gint32   drawable_ID,
90                              GError **error)
91 {
92   GeglBuffer         *buffer   = gimp_drawable_get_buffer (drawable_ID);
93   const Babl         *format;
94   cairo_surface_t    *surface;
95   cairo_status_t      status;
96   const gint          width    = gimp_drawable_width  (drawable_ID);
97   const gint          height   = gimp_drawable_height (drawable_ID);
98   GeglBufferIterator *iter;
99   guchar             *pixels;
100   gint                stride;
101   guint               count    = 0;
102   guint               done     = 0;
103 
104   if (gimp_drawable_has_alpha (drawable_ID))
105     format = babl_format ("cairo-ARGB32");
106   else
107     format = babl_format ("cairo-RGB24");
108 
109   surface = cairo_image_surface_create (gimp_drawable_has_alpha (drawable_ID) ?
110                                         CAIRO_FORMAT_ARGB32 :
111                                         CAIRO_FORMAT_RGB24,
112                                         width, height);
113 
114   status = cairo_surface_status (surface);
115   if (status != CAIRO_STATUS_SUCCESS)
116     {
117       switch (status)
118         {
119         case CAIRO_STATUS_INVALID_SIZE:
120           g_set_error_literal (error,
121                                GIMP_PLUGIN_PRINT_ERROR,
122                                GIMP_PLUGIN_PRINT_ERROR_FAILED,
123                                _("Cannot handle the size (either width or height) of the image."));
124           break;
125         default:
126           g_set_error (error,
127                        GIMP_PLUGIN_PRINT_ERROR,
128                        GIMP_PLUGIN_PRINT_ERROR_FAILED,
129                        "Cairo error: %s",
130                        cairo_status_to_string (status));
131           break;
132         }
133 
134       return NULL;
135     }
136 
137   pixels = cairo_image_surface_get_data (surface);
138   stride = cairo_image_surface_get_stride (surface);
139 
140   iter = gegl_buffer_iterator_new (buffer,
141                                    GEGL_RECTANGLE (0, 0, width, height), 0,
142                                    format,
143                                    GEGL_ACCESS_READ, GEGL_ABYSS_NONE, 1);
144 
145   while (gegl_buffer_iterator_next (iter))
146     {
147       const guchar *src  = iter->items[0].data;
148       guchar       *dest = pixels + iter->items[0].roi.y * stride + iter->items[0].roi.x * 4;
149       gint          y;
150 
151       for (y = 0; y < iter->items[0].roi.height; y++)
152         {
153           memcpy (dest, src, iter->items[0].roi.width * 4);
154 
155           src  += iter->items[0].roi.width * 4;
156           dest += stride;
157         }
158 
159       done += iter->items[0].roi.height * iter->items[0].roi.width;
160 
161       if (count++ % 16 == 0)
162         gimp_progress_update ((gdouble) done / (width * height));
163     }
164 
165   g_object_unref (buffer);
166 
167   cairo_surface_mark_dirty (surface);
168 
169   gimp_progress_update (1.0);
170 
171   return surface;
172 }
173 
174 static void
print_draw_crop_marks(GtkPrintContext * context,gdouble x,gdouble y,gdouble w,gdouble h)175 print_draw_crop_marks (GtkPrintContext *context,
176                        gdouble          x,
177                        gdouble          y,
178                        gdouble          w,
179                        gdouble          h)
180 {
181   cairo_t *cr  = gtk_print_context_get_cairo_context (context);
182   gdouble  len = MIN (gtk_print_context_get_width (context),
183                       gtk_print_context_get_height (context)) / 20.0;
184 
185   /*  upper left  */
186 
187   cairo_move_to (cr, x - len,     y);
188   cairo_line_to (cr, x - len / 5, y);
189 
190   cairo_move_to (cr, x, y - len);
191   cairo_line_to (cr, x, y - len / 5);
192 
193   /*  upper right  */
194 
195   cairo_move_to (cr, x + w + len / 5, y);
196   cairo_line_to (cr, x + w + len,     y);
197 
198   cairo_move_to (cr, x + w, y - len);
199   cairo_line_to (cr, x + w, y - len / 5);
200 
201   /*  lower left  */
202 
203   cairo_move_to (cr, x - len,     y + h);
204   cairo_line_to (cr, x - len / 5, y + h);
205 
206   cairo_move_to (cr, x, y + h + len);
207   cairo_line_to (cr, x, y + h + len / 5);
208 
209   /*  lower right  */
210 
211   cairo_move_to (cr, x + w + len / 5, y + h);
212   cairo_line_to (cr, x + w + len,     y + h);
213 
214   cairo_move_to (cr, x + w, y + h + len);
215   cairo_line_to (cr, x + w, y + h + len / 5);
216 
217   cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
218   cairo_set_line_width (cr, 2);
219   cairo_stroke (cr);
220 }
221