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