1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
4 * (GLABELS) Label and Business Card Creation program for GNOME
6 * mini-preview-pixbuf.c: mini preview pixbuf module
8 * Copyright (C) 2006 Jim Evins <evins@snaught.com>.
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.
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.
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
27 #include "mini-preview-pixbuf.h"
28 #include "cairo-label-path.h"
35 /*===========================================*/
36 /* Private macros and constants. */
37 /*===========================================*/
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
44 #define PAPER_OUTLINE_WIDTH_PIXELS 1.0
45 #define LABEL_OUTLINE_WIDTH_PIXELS 1.0
47 /*===========================================*/
49 /*===========================================*/
52 /*===========================================*/
54 /*===========================================*/
57 /*===========================================*/
58 /* Local function prototypes */
59 /*===========================================*/
61 static void draw_paper (cairo_t *cr,
62 lglTemplate *template,
65 static void draw_label_outlines (cairo_t *cr,
66 lglTemplate *template,
69 static void draw_label_outline (cairo_t *cr,
70 lglTemplate *template,
77 /****************************************************************************/
78 /* Create new pixbuf with mini preview of template */
79 /****************************************************************************/
81 gl_mini_preview_pixbuf_new (lglTemplate *template,
85 cairo_surface_t *surface;
90 gdouble offset_x, offset_y;
92 gl_debug (DEBUG_MINI_PREVIEW, "START");
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),
98 gdk_pixbuf_get_width (pixbuf),
99 gdk_pixbuf_get_height (pixbuf),
100 gdk_pixbuf_get_rowstride (pixbuf));
102 cr = cairo_create (surface);
103 cairo_surface_destroy (surface);
107 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
111 cairo_set_antialias (cr, CAIRO_ANTIALIAS_GRAY);
113 /* Set scale and offset */
116 if ( (w/template->page_width) > (h/template->page_height) ) {
117 scale = h / template->page_height;
119 scale = w / template->page_width;
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);
127 /* Draw paper and label outlines */
128 draw_paper (cr, template, scale);
129 draw_label_outlines (cr, template, scale);
134 gl_debug (DEBUG_MINI_PREVIEW, "END");
139 /*--------------------------------------------------------------------------*/
140 /* PRIVATE. Draw paper and paper outline. */
141 /*--------------------------------------------------------------------------*/
143 draw_paper (cairo_t *cr,
144 lglTemplate *template,
147 gl_debug (DEBUG_MINI_PREVIEW, "START");
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);
158 gl_debug (DEBUG_MINI_PREVIEW, "END");
161 /*--------------------------------------------------------------------------*/
162 /* PRIVATE. Draw label outlines. */
163 /*--------------------------------------------------------------------------*/
165 draw_label_outlines (cairo_t *cr,
166 lglTemplate *template,
169 const lglTemplateFrame *frame;
171 lglTemplateOrigin *origins;
173 gl_debug (DEBUG_MINI_PREVIEW, "START");
177 cairo_set_line_width (cr, LABEL_OUTLINE_WIDTH_PIXELS/scale);
179 frame = (lglTemplateFrame *)template->frames->data;
181 n_labels = lgl_template_frame_get_n_labels (frame);
182 origins = lgl_template_frame_get_origins (frame);
184 for ( i=0; i < n_labels; i++ ) {
186 draw_label_outline(cr, template, origins[i].x, origins[i].y);
194 gl_debug (DEBUG_MINI_PREVIEW, "END");
197 /*--------------------------------------------------------------------------*/
198 /* PRIVATE. Draw label outline. */
199 /*--------------------------------------------------------------------------*/
201 draw_label_outline (cairo_t *cr,
202 lglTemplate *template,
206 gl_debug (DEBUG_MINI_PREVIEW, "START");
210 cairo_translate (cr, x0, y0);
212 gl_cairo_label_path (cr, template, FALSE, FALSE);
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);
218 cairo_set_source_rgb (cr, LABEL_OUTLINE_RGB_ARGS);
223 gl_debug (DEBUG_MINI_PREVIEW, "END");