2 * mini-preview-pixbuf.c
3 * Copyright (C) 2006-2009 Jim Evins <evins@snaught.com>.
5 * This file is part of gLabels.
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.
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.
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/>.
23 #include "mini-preview-pixbuf.h"
28 #include "cairo-label-path.h"
32 /*===========================================*/
33 /* Private macros and constants. */
34 /*===========================================*/
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
41 #define PAPER_OUTLINE_WIDTH_PIXELS 1.0
42 #define LABEL_OUTLINE_WIDTH_PIXELS 1.0
44 /*===========================================*/
46 /*===========================================*/
49 /*===========================================*/
51 /*===========================================*/
54 /*===========================================*/
55 /* Local function prototypes */
56 /*===========================================*/
58 static void draw_paper (cairo_t *cr,
59 lglTemplate *template,
62 static void draw_label_outlines (cairo_t *cr,
63 lglTemplate *template,
66 static void draw_label_outline (cairo_t *cr,
67 lglTemplate *template,
74 /****************************************************************************/
75 /* Create new pixbuf with mini preview of template */
76 /****************************************************************************/
78 gl_mini_preview_pixbuf_new (lglTemplate *template,
82 cairo_surface_t *surface;
87 gdouble offset_x, offset_y;
89 gl_debug (DEBUG_MINI_PREVIEW, "START");
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),
95 gdk_pixbuf_get_width (pixbuf),
96 gdk_pixbuf_get_height (pixbuf),
97 gdk_pixbuf_get_rowstride (pixbuf));
99 cr = cairo_create (surface);
100 cairo_surface_destroy (surface);
104 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
108 cairo_set_antialias (cr, CAIRO_ANTIALIAS_GRAY);
110 /* Set scale and offset */
113 if ( (w/template->page_width) > (h/template->page_height) ) {
114 scale = h / template->page_height;
116 scale = w / template->page_width;
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);
124 /* Draw paper and label outlines */
125 draw_paper (cr, template, scale);
126 draw_label_outlines (cr, template, scale);
131 gl_debug (DEBUG_MINI_PREVIEW, "END");
136 /*--------------------------------------------------------------------------*/
137 /* PRIVATE. Draw paper and paper outline. */
138 /*--------------------------------------------------------------------------*/
140 draw_paper (cairo_t *cr,
141 lglTemplate *template,
144 gl_debug (DEBUG_MINI_PREVIEW, "START");
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);
155 gl_debug (DEBUG_MINI_PREVIEW, "END");
158 /*--------------------------------------------------------------------------*/
159 /* PRIVATE. Draw label outlines. */
160 /*--------------------------------------------------------------------------*/
162 draw_label_outlines (cairo_t *cr,
163 lglTemplate *template,
166 const lglTemplateFrame *frame;
168 lglTemplateOrigin *origins;
170 gl_debug (DEBUG_MINI_PREVIEW, "START");
174 cairo_set_line_width (cr, LABEL_OUTLINE_WIDTH_PIXELS/scale);
176 frame = (lglTemplateFrame *)template->frames->data;
178 n_labels = lgl_template_frame_get_n_labels (frame);
179 origins = lgl_template_frame_get_origins (frame);
181 for ( i=0; i < n_labels; i++ ) {
183 draw_label_outline(cr, template, origins[i].x, origins[i].y);
191 gl_debug (DEBUG_MINI_PREVIEW, "END");
194 /*--------------------------------------------------------------------------*/
195 /* PRIVATE. Draw label outline. */
196 /*--------------------------------------------------------------------------*/
198 draw_label_outline (cairo_t *cr,
199 lglTemplate *template,
203 gl_debug (DEBUG_MINI_PREVIEW, "START");
207 cairo_translate (cr, x0, y0);
209 gl_cairo_label_path (cr, template, FALSE, FALSE);
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);
215 cairo_set_source_rgb (cr, LABEL_OUTLINE_RGB_ARGS);
220 gl_debug (DEBUG_MINI_PREVIEW, "END");
226 * Local Variables: -- emacs
228 * c-basic-offset: 8 -- emacs
229 * tab-width: 8 -- emacs
230 * indent-tabs-mode: nil -- emacs