1
/* GIMP - The GNU Image Manipulation Program
2
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
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 2 of the License, or
7
* (at your option) any later version.
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.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21
#include <libgimp/gimp.h>
22
#include <libgimp/gimpui.h>
25
#include "print-draw-page.h"
27
#include "libgimp/stdplugins-intl.h"
30
#define EPSILON 0.0001
33
static cairo_surface_t * create_surface (guchar *pixels,
39
static void draw_info_header (GtkPrintContext *context,
46
draw_page_cairo (GtkPrintContext *context,
49
gint tile_height = gimp_tile_height ();
50
gint32 image_id = data->image_id;
51
gint32 drawable_id = data->drawable_id;
52
GimpDrawable *drawable;
68
cairo_surface_t *surface;
70
/* export the image */
71
gimp_export_image (&image_id, &drawable_id, NULL,
72
GIMP_EXPORT_CAN_HANDLE_RGB |
73
GIMP_EXPORT_CAN_HANDLE_ALPHA |
74
GIMP_EXPORT_NEEDS_ALPHA);
76
drawable = gimp_drawable_get (drawable_id);
78
width = drawable->width;
79
height = drawable->height;
80
rowstride = drawable->bpp * width;
82
cr = gtk_print_context_get_cairo_context (context);
84
cr_width = gtk_print_context_get_width (context);
85
cr_height = gtk_print_context_get_height (context);
86
cr_dpi_x = gtk_print_context_get_dpi_x (context);
87
cr_dpi_y = gtk_print_context_get_dpi_y (context);
89
scale_x = cr_dpi_x / data->xres;
90
scale_y = cr_dpi_y / data->yres;
92
cairo_set_source_rgb (cr, 1, 1, 1);
96
/* print header if it is requested */
97
if (data->show_info_header)
99
draw_info_header (context, cr, data);
102
#define HEADER_HEIGHT (20 * 72.0 / 25.4)
103
cairo_translate (cr, 0, HEADER_HEIGHT);
104
cr_height -= HEADER_HEIGHT;
108
x0 = (cr_width - scale_x * width) / 2;
109
y0 = (cr_height - scale_y * height) / 2;
111
cairo_translate (cr, x0, y0);
112
cairo_scale (cr, scale_x, scale_y);
114
gimp_pixel_rgn_init (®ion, drawable, 0, 0, width, height, FALSE, FALSE);
116
pixels = g_new (guchar, MIN (height, tile_height) * rowstride);
118
for (y = 0; y < height; y += tile_height)
120
gint h = MIN (tile_height, height - y);
122
gimp_pixel_rgn_get_rect (®ion, pixels, 0, y, width, h);
124
surface = create_surface (pixels, width, h, rowstride);
126
cairo_set_source_surface (cr, surface, 0, y);
128
cairo_rectangle (cr, 0, y, width, h);
131
cairo_surface_destroy (surface);
133
gimp_progress_update ((gdouble) (y + h) / (gdouble) height);
138
gimp_drawable_detach (drawable);
140
if (image_id != data->image_id)
141
gimp_image_delete (image_id);
146
static cairo_surface_t *
147
create_surface (guchar *pixels,
152
guint32 *cairo_data = (guint32 *) pixels;
157
/* knock pixels into the shape requested by cairo:
159
* CAIRO_FORMAT_ARGB32:
160
* each pixel is a 32-bit quantity, with alpha in the upper 8 bits,
161
* then red, then green, then blue. The 32-bit quantities are
162
* stored native-endian. Pre-multiplied alpha is used.
166
len = width * height;
168
for (i = 0, p = pixels; i < len; i++)
177
gdouble alpha = a / 255.0;
184
cairo_data[i] = (a << 24) + (r << 16) + (g << 8) + b;
187
return cairo_image_surface_create_for_data (pixels, CAIRO_FORMAT_ARGB32,
188
width, height, rowstride);
193
draw_info_header (GtkPrintContext *context,
198
PangoFontDescription *desc;
201
gdouble fname_text_width;
204
gchar date_buffer[100];
206
const gchar *name_str;
207
GimpParasite *parasite;
208
const gchar *end_ptr;
214
cr_width = gtk_print_context_get_width (context);
215
cairo_rectangle (cr, 0, 0, cr_width, HEADER_HEIGHT);
216
cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
217
cairo_fill_preserve (cr);
219
cairo_set_source_rgb (cr, 0, 0, 0);
220
cairo_set_line_width (cr, 1);
223
layout = gtk_print_context_create_pango_layout (context);
225
desc = pango_font_description_from_string ("sans 14");
226
pango_layout_set_font_description (layout, desc);
227
pango_font_description_free (desc);
229
pango_layout_set_width (layout, -1);
230
pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
233
pango_layout_set_text (layout, gimp_image_get_name (data->image_id), -1);
235
pango_layout_get_size (layout, &layout_width, &layout_height);
236
text_height = (gdouble) layout_height / PANGO_SCALE;
238
cairo_move_to (cr, 0.02 * cr_width, (HEADER_HEIGHT - text_height) / 5);
239
pango_cairo_show_layout (cr, layout);
242
name_str = g_get_real_name ();
243
if (name_str && g_utf8_validate (name_str, -1, &end_ptr))
245
pango_layout_set_text (layout, name_str, -1);
247
pango_layout_get_size (layout, &layout_width, &layout_height);
248
text_height = (gdouble) layout_height / PANGO_SCALE;
249
text_width = (gdouble) layout_width / PANGO_SCALE;
251
cairo_move_to (cr, 0.5 * cr_width - 0.5 * text_width,
252
(HEADER_HEIGHT - text_height) / 5);
253
pango_cairo_show_layout (cr, layout);
257
date = g_date_new ();
258
g_date_set_time_t (date, time (NULL));
259
g_date_strftime (date_buffer, 100, "%x", date);
261
pango_layout_set_text (layout, date_buffer, -1);
263
pango_layout_get_size (layout, &layout_width, &layout_height);
264
text_height = (gdouble) layout_height / PANGO_SCALE;
265
text_width = (gdouble) layout_width / PANGO_SCALE;
268
0.98 * cr_width - text_width,
269
(HEADER_HEIGHT - text_height) / 5);
270
pango_cairo_show_layout (cr, layout);
272
/* file name if any */
273
filename = gimp_image_get_filename (data->image_id);
277
pango_layout_set_text (layout,
278
gimp_filename_to_utf8 (filename), -1);
281
pango_layout_get_size (layout, &layout_width, &layout_height);
282
text_height = (gdouble) layout_height / PANGO_SCALE;
283
fname_text_width = (gdouble) layout_width / PANGO_SCALE;
286
0.02 * cr_width, 4 * (HEADER_HEIGHT - text_height) / 5);
287
pango_cairo_show_layout (cr, layout);
291
fname_text_width = 0;
294
/* image comment if it is short */
295
parasite = gimp_image_parasite_find (data->image_id, "gimp-comment");
299
pango_layout_set_text (layout, gimp_parasite_data (parasite), -1);
301
pango_layout_get_size (layout, &layout_width, &layout_height);
302
text_height = (gdouble) layout_height / PANGO_SCALE;
303
text_width = (gdouble) layout_width / PANGO_SCALE;
305
if (fname_text_width + text_width < 0.8 * cr_width &&
306
text_height < 0.5 * HEADER_HEIGHT)
308
cairo_move_to (cr, 0.98 * cr_width - text_width,
309
4 * (HEADER_HEIGHT - text_height) / 5);
310
pango_cairo_show_layout (cr, layout);
313
gimp_parasite_free (parasite);
316
g_object_unref (layout);