]> git.sur5r.net Git - glabels/blob - src/mini-preview-pixbuf.c
Imported Upstream version 2.2.8
[glabels] / src / mini-preview-pixbuf.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /*
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  mini-preview-pixbuf.c:  mini preview pixbuf module
7  *
8  *  Copyright (C) 2006  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include <config.h>
26
27 #include "mini-preview-pixbuf.h"
28 #include "cairo-label-path.h"
29
30 #include <cairo.h>
31 #include <math.h>
32
33 #include "debug.h"
34
35 /*===========================================*/
36 /* Private macros and constants.             */
37 /*===========================================*/
38
39 #define PAPER_RGB_ARGS          0.95,  0.95,  0.95
40 #define PAPER_OUTLINE_RGB_ARGS  0.0,   0.0,   0.0
41 #define LABEL_RGB_ARGS          1.0,   1.0,   1.0
42 #define LABEL_OUTLINE_RGB_ARGS  0.25,  0.25,  0.25
43
44 #define PAPER_OUTLINE_WIDTH_PIXELS  1.0
45 #define LABEL_OUTLINE_WIDTH_PIXELS  1.0
46
47 /*===========================================*/
48 /* Private types                             */
49 /*===========================================*/
50
51
52 /*===========================================*/
53 /* Private globals                           */
54 /*===========================================*/
55
56
57 /*===========================================*/
58 /* Local function prototypes                 */
59 /*===========================================*/
60
61 static void draw_paper                (cairo_t           *cr,
62                                        lglTemplate       *template,
63                                        gdouble            scale);
64
65 static void draw_label_outlines       (cairo_t           *cr,
66                                        lglTemplate       *template,
67                                        gdouble            scale);
68
69 static void draw_label_outline        (cairo_t           *cr,
70                                        lglTemplate       *template,
71                                        gdouble            x0,
72                                        gdouble            y0);
73
74
75
76 \f
77 /****************************************************************************/
78 /* Create new pixbuf with mini preview of template                          */
79 /****************************************************************************/
80 GdkPixbuf *
81 gl_mini_preview_pixbuf_new (lglTemplate *template,
82                             gint         width,
83                             gint         height)
84 {
85         cairo_surface_t   *surface;
86         cairo_t           *cr;
87         GdkPixbuf         *pixbuf;
88         gdouble            scale;
89         gdouble            w, h;
90         gdouble            offset_x, offset_y;
91
92         gl_debug (DEBUG_MINI_PREVIEW, "START");
93
94         /* Create pixbuf and cairo context. */
95         pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
96         surface = cairo_image_surface_create_for_data (gdk_pixbuf_get_pixels (pixbuf),
97                                                        CAIRO_FORMAT_RGB24,
98                                                        gdk_pixbuf_get_width (pixbuf),
99                                                        gdk_pixbuf_get_height (pixbuf),
100                                                        gdk_pixbuf_get_rowstride (pixbuf));
101
102         cr = cairo_create (surface);
103         cairo_surface_destroy (surface);
104
105         /* Clear pixbuf */
106         cairo_save (cr);
107         cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
108         cairo_paint (cr);
109         cairo_restore (cr);
110
111         cairo_set_antialias (cr, CAIRO_ANTIALIAS_GRAY);
112
113         /* Set scale and offset */
114         w = width - 1;
115         h = height - 1;
116         if ( (w/template->page_width) > (h/template->page_height) ) {
117                 scale = h / template->page_height;
118         } else {
119                 scale = w / template->page_width;
120         }
121         offset_x = (width/scale - template->page_width) / 2.0;
122         offset_y = (height/scale - template->page_height) / 2.0;
123         cairo_identity_matrix (cr);
124         cairo_scale (cr, scale, scale);
125         cairo_translate (cr, offset_x, offset_y);
126
127         /* Draw paper and label outlines */
128         draw_paper (cr, template, scale);
129         draw_label_outlines (cr, template, scale);
130
131         /* Cleanup */
132         cairo_destroy (cr);
133
134         gl_debug (DEBUG_MINI_PREVIEW, "END");
135
136         return pixbuf;
137 }
138
139 /*--------------------------------------------------------------------------*/
140 /* PRIVATE.  Draw paper and paper outline.                                  */
141 /*--------------------------------------------------------------------------*/
142 static void
143 draw_paper (cairo_t           *cr,
144             lglTemplate       *template,
145             gdouble            scale)
146 {
147         gl_debug (DEBUG_MINI_PREVIEW, "START");
148
149         cairo_save (cr);
150         cairo_rectangle (cr, 0.0, 0.0, template->page_width, template->page_height);
151         cairo_set_source_rgb (cr, PAPER_RGB_ARGS);
152         cairo_fill_preserve (cr);
153         cairo_set_line_width (cr, PAPER_OUTLINE_WIDTH_PIXELS/scale);
154         cairo_set_source_rgb (cr, PAPER_OUTLINE_RGB_ARGS);
155         cairo_stroke (cr);
156         cairo_restore (cr);
157
158         gl_debug (DEBUG_MINI_PREVIEW, "END");
159 }
160
161 /*--------------------------------------------------------------------------*/
162 /* PRIVATE.  Draw label outlines.                                           */
163 /*--------------------------------------------------------------------------*/
164 static void
165 draw_label_outlines (cairo_t           *cr,
166                      lglTemplate       *template,
167                      gdouble            scale)
168 {
169         const lglTemplateFrame *frame;
170         gint                    i, n_labels;
171         lglTemplateOrigin      *origins;
172
173         gl_debug (DEBUG_MINI_PREVIEW, "START");
174
175         cairo_save (cr);
176
177         cairo_set_line_width (cr, LABEL_OUTLINE_WIDTH_PIXELS/scale);
178
179         frame = (lglTemplateFrame *)template->frames->data;
180
181         n_labels = lgl_template_frame_get_n_labels (frame);
182         origins  = lgl_template_frame_get_origins (frame);
183
184         for ( i=0; i < n_labels; i++ ) {
185
186                 draw_label_outline(cr, template, origins[i].x, origins[i].y);
187
188         }
189
190         g_free (origins);
191
192         cairo_restore (cr);
193
194         gl_debug (DEBUG_MINI_PREVIEW, "END");
195 }
196
197 /*--------------------------------------------------------------------------*/
198 /* PRIVATE.  Draw label outline.                                            */
199 /*--------------------------------------------------------------------------*/
200 static void
201 draw_label_outline (cairo_t           *cr,
202                     lglTemplate       *template,
203                     gdouble            x0,
204                     gdouble            y0)
205 {
206         gl_debug (DEBUG_MINI_PREVIEW, "START");
207
208         cairo_save (cr);
209
210         cairo_translate (cr, x0, y0);
211
212         gl_cairo_label_path (cr, template, FALSE, FALSE);
213
214         cairo_set_source_rgb (cr, LABEL_RGB_ARGS);
215         cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
216         cairo_fill_preserve (cr);
217
218         cairo_set_source_rgb (cr, LABEL_OUTLINE_RGB_ARGS);
219         cairo_stroke (cr);
220
221         cairo_restore (cr);
222
223         gl_debug (DEBUG_MINI_PREVIEW, "END");
224 }
225