~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/print/print-draw-page.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Holbach
  • Date: 2007-05-02 16:33:03 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070502163303-bvzhjzbpw8qglc4y
Tags: 2.3.16-1ubuntu1
* Resynchronized with Debian, remaining Ubuntu changes:
  - debian/rules: i18n magic.
* debian/control.in:
  - Maintainer: Ubuntu Core Developers <ubuntu-devel@lists.ubuntu.com>
* debian/patches/02_help-message.patch,
  debian/patches/03_gimp.desktop.in.in.patch,
  debian/patches/10_dont_show_wizard.patch: updated.
* debian/patches/04_composite-signedness.patch,
  debian/patches/05_add-letter-spacing.patch: dropped, used upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 2 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, write to the Free Software
 
16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
17
 */
 
18
 
 
19
#include "config.h"
 
20
 
 
21
#include <libgimp/gimp.h>
 
22
#include <libgimp/gimpui.h>
 
23
 
 
24
#include "print.h"
 
25
#include "print-draw-page.h"
 
26
 
 
27
#include "libgimp/stdplugins-intl.h"
 
28
 
 
29
 
 
30
#define EPSILON 0.0001
 
31
 
 
32
 
 
33
static cairo_surface_t * create_surface   (guchar          *pixels,
 
34
                                           gint             width,
 
35
                                           gint             height,
 
36
                                           gint             rowstride);
 
37
 
 
38
#if 0
 
39
static void              draw_info_header (GtkPrintContext *context,
 
40
                                           cairo_t         *cr,
 
41
                                           PrintData       *data);
 
42
#endif
 
43
 
 
44
 
 
45
gboolean
 
46
draw_page_cairo (GtkPrintContext *context,
 
47
                 PrintData       *data)
 
48
{
 
49
  gint              tile_height = gimp_tile_height ();
 
50
  gint32            image_id    = data->image_id;
 
51
  gint32            drawable_id = data->drawable_id;
 
52
  GimpDrawable     *drawable;
 
53
  GimpPixelRgn      region;
 
54
  cairo_t          *cr;
 
55
  gdouble           cr_width;
 
56
  gdouble           cr_height;
 
57
  gdouble           cr_dpi_x;
 
58
  gdouble           cr_dpi_y;
 
59
  gint              width;
 
60
  gint              height;
 
61
  gint              rowstride;
 
62
  gint              y;
 
63
  gdouble           scale_x;
 
64
  gdouble           scale_y;
 
65
  gdouble           x0 = 0;
 
66
  gdouble           y0 = 0;
 
67
  guchar           *pixels;
 
68
  cairo_surface_t  *surface;
 
69
 
 
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);
 
75
 
 
76
  drawable = gimp_drawable_get (drawable_id);
 
77
 
 
78
  width     = drawable->width;
 
79
  height    = drawable->height;
 
80
  rowstride = drawable->bpp * width;
 
81
 
 
82
  cr = gtk_print_context_get_cairo_context (context);
 
83
 
 
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);
 
88
 
 
89
  scale_x = cr_dpi_x / data->xres;
 
90
  scale_y = cr_dpi_y / data->yres;
 
91
 
 
92
  cairo_set_source_rgb (cr, 1, 1, 1);
 
93
  cairo_paint (cr);
 
94
 
 
95
#if 0
 
96
  /* print header if it is requested */
 
97
  if (data->show_info_header)
 
98
    {
 
99
      draw_info_header (context, cr, data);
 
100
 
 
101
/* In points */
 
102
#define HEADER_HEIGHT (20 * 72.0 / 25.4)
 
103
      cairo_translate (cr, 0, HEADER_HEIGHT);
 
104
      cr_height -= HEADER_HEIGHT;
 
105
    }
 
106
#endif
 
107
 
 
108
  x0 = (cr_width - scale_x * width) / 2;
 
109
  y0 = (cr_height - scale_y * height) / 2;
 
110
 
 
111
  cairo_translate (cr, x0, y0);
 
112
  cairo_scale (cr, scale_x, scale_y);
 
113
 
 
114
  gimp_pixel_rgn_init (&region, drawable, 0, 0, width, height, FALSE, FALSE);
 
115
 
 
116
  pixels = g_new (guchar, MIN (height, tile_height) * rowstride);
 
117
 
 
118
  for (y = 0; y < height; y += tile_height)
 
119
    {
 
120
      gint h = MIN (tile_height, height - y);
 
121
 
 
122
      gimp_pixel_rgn_get_rect (&region, pixels, 0, y, width, h);
 
123
 
 
124
      surface = create_surface (pixels, width, h, rowstride);
 
125
 
 
126
      cairo_set_source_surface (cr, surface, 0, y);
 
127
 
 
128
      cairo_rectangle (cr, 0, y, width, h);
 
129
      cairo_fill (cr);
 
130
 
 
131
      cairo_surface_destroy (surface);
 
132
 
 
133
      gimp_progress_update ((gdouble) (y + h) / (gdouble) height);
 
134
    }
 
135
 
 
136
  g_free (pixels);
 
137
 
 
138
  gimp_drawable_detach (drawable);
 
139
 
 
140
  if (image_id != data->image_id)
 
141
    gimp_image_delete (image_id);
 
142
 
 
143
  return TRUE;
 
144
}
 
145
 
 
146
static cairo_surface_t *
 
147
create_surface (guchar *pixels,
 
148
                gint    width,
 
149
                gint    height,
 
150
                gint    rowstride)
 
151
{
 
152
  guint32 *cairo_data = (guint32 *) pixels;
 
153
  guchar  *p;
 
154
  gint     len;
 
155
  gint     i;
 
156
 
 
157
  /* knock pixels into the shape requested by cairo:
 
158
   *
 
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.
 
163
   *
 
164
   */
 
165
 
 
166
  len = width * height;
 
167
 
 
168
  for (i = 0, p = pixels; i < len; i++)
 
169
    {
 
170
      guint32 r = *p++;
 
171
      guint32 g = *p++;
 
172
      guint32 b = *p++;
 
173
      guint32 a = *p++;
 
174
 
 
175
      if (a != 255)
 
176
        {
 
177
          gdouble alpha = a / 255.0;
 
178
 
 
179
          r *= alpha;
 
180
          g *= alpha;
 
181
          b *= alpha;
 
182
        }
 
183
 
 
184
      cairo_data[i] = (a << 24) + (r << 16) + (g << 8) + b;
 
185
    }
 
186
 
 
187
  return cairo_image_surface_create_for_data (pixels, CAIRO_FORMAT_ARGB32,
 
188
                                              width, height, rowstride);
 
189
}
 
190
 
 
191
#if 0
 
192
static void
 
193
draw_info_header (GtkPrintContext *context,
 
194
                  cairo_t         *cr,
 
195
                  PrintData       *data)
 
196
{
 
197
  PangoLayout          *layout;
 
198
  PangoFontDescription *desc;
 
199
  gdouble               text_height;
 
200
  gdouble               text_width;
 
201
  gdouble               fname_text_width;
 
202
  gint                  layout_height;
 
203
  gint                  layout_width;
 
204
  gchar                 date_buffer[100];
 
205
  GDate                *date;
 
206
  const gchar          *name_str;
 
207
  GimpParasite         *parasite;
 
208
  const gchar          *end_ptr;
 
209
  gchar                *filename;
 
210
  gdouble               cr_width;
 
211
 
 
212
  cairo_save (cr);
 
213
 
 
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);
 
218
 
 
219
  cairo_set_source_rgb (cr, 0, 0, 0);
 
220
  cairo_set_line_width (cr, 1);
 
221
  cairo_stroke (cr);
 
222
 
 
223
  layout = gtk_print_context_create_pango_layout (context);
 
224
 
 
225
  desc = pango_font_description_from_string ("sans 14");
 
226
  pango_layout_set_font_description (layout, desc);
 
227
  pango_font_description_free (desc);
 
228
 
 
229
  pango_layout_set_width (layout, -1);
 
230
  pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
 
231
 
 
232
  /* image name */
 
233
  pango_layout_set_text (layout, gimp_image_get_name (data->image_id), -1);
 
234
 
 
235
  pango_layout_get_size (layout, &layout_width, &layout_height);
 
236
  text_height = (gdouble) layout_height / PANGO_SCALE;
 
237
 
 
238
  cairo_move_to (cr, 0.02 * cr_width,  (HEADER_HEIGHT - text_height) / 5);
 
239
  pango_cairo_show_layout (cr, layout);
 
240
 
 
241
  /* user name */
 
242
  name_str = g_get_real_name ();
 
243
  if (name_str && g_utf8_validate (name_str, -1, &end_ptr))
 
244
    {
 
245
      pango_layout_set_text (layout, name_str, -1);
 
246
 
 
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;
 
250
 
 
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);
 
254
    }
 
255
 
 
256
  /* date */
 
257
  date = g_date_new ();
 
258
  g_date_set_time_t (date, time (NULL));
 
259
  g_date_strftime (date_buffer, 100, "%x", date);
 
260
  g_date_free (date);
 
261
  pango_layout_set_text (layout, date_buffer, -1);
 
262
 
 
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;
 
266
 
 
267
  cairo_move_to (cr,
 
268
                 0.98 * cr_width - text_width,
 
269
                 (HEADER_HEIGHT - text_height) / 5);
 
270
  pango_cairo_show_layout (cr, layout);
 
271
 
 
272
  /* file name if any */
 
273
  filename = gimp_image_get_filename (data->image_id);
 
274
 
 
275
  if (filename)
 
276
    {
 
277
      pango_layout_set_text (layout,
 
278
                             gimp_filename_to_utf8 (filename), -1);
 
279
      g_free (filename);
 
280
 
 
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;
 
284
 
 
285
      cairo_move_to (cr,
 
286
                     0.02 * cr_width,  4 * (HEADER_HEIGHT - text_height) / 5);
 
287
      pango_cairo_show_layout (cr, layout);
 
288
    }
 
289
  else
 
290
    {
 
291
      fname_text_width = 0;
 
292
    }
 
293
 
 
294
  /* image comment if it is short */
 
295
  parasite = gimp_image_parasite_find (data->image_id, "gimp-comment");
 
296
 
 
297
  if (parasite)
 
298
    {
 
299
      pango_layout_set_text (layout, gimp_parasite_data (parasite), -1);
 
300
 
 
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;
 
304
 
 
305
      if (fname_text_width + text_width < 0.8 * cr_width &&
 
306
          text_height < 0.5 * HEADER_HEIGHT)
 
307
        {
 
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);
 
311
        }
 
312
 
 
313
      gimp_parasite_free (parasite);
 
314
    }
 
315
 
 
316
  g_object_unref (layout);
 
317
 
 
318
  cairo_restore (cr);
 
319
}
 
320
#endif