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"
34 /*===========================================*/
36 /*===========================================*/
39 /*===========================================*/
41 /*===========================================*/
44 /*===========================================*/
45 /* Local function prototypes */
46 /*===========================================*/
48 static void draw_paper (cairo_t *cr,
49 const glTemplate *template,
52 static void draw_label_outlines (cairo_t *cr,
53 const glTemplate *template,
56 static void draw_label_outline (cairo_t *cr,
57 const glTemplate *template,
61 static void draw_rect_label_outline (cairo_t *cr,
62 const glTemplate *template,
66 static void draw_round_label_outline (cairo_t *cr,
67 const glTemplate *template,
71 static void draw_cd_label_outline (cairo_t *cr,
72 const glTemplate *template,
79 /****************************************************************************/
80 /* Create new pixbuf with mini preview of template */
81 /****************************************************************************/
83 gl_mini_preview_pixbuf_new (glTemplate *template,
87 cairo_surface_t *surface;
92 gdouble offset_x, offset_y;
94 gl_debug (DEBUG_MINI_PREVIEW, "START");
96 /* Create pixbuf and cairo context. */
97 pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
98 surface = cairo_image_surface_create_for_data (gdk_pixbuf_get_pixels (pixbuf),
100 gdk_pixbuf_get_width (pixbuf),
101 gdk_pixbuf_get_height (pixbuf),
102 gdk_pixbuf_get_rowstride (pixbuf));
104 cr = cairo_create (surface);
105 cairo_surface_destroy (surface);
109 cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR);
113 cairo_set_antialias (cr, CAIRO_ANTIALIAS_DEFAULT);
115 /* Set scale and offset */
118 if ( (w/template->page_width) > (h/template->page_height) ) {
119 scale = h / template->page_height;
121 scale = w / template->page_width;
123 offset_x = (width/scale - template->page_width) / 2.0;
124 offset_y = (height/scale - template->page_height) / 2.0;
125 cairo_identity_matrix (cr);
126 cairo_scale (cr, scale, scale);
127 cairo_translate (cr, offset_x, offset_y);
129 /* Draw paper and label outlines */
130 draw_paper (cr, template, scale);
131 draw_label_outlines (cr, template, scale);
136 gl_debug (DEBUG_MINI_PREVIEW, "END");
141 /*--------------------------------------------------------------------------*/
142 /* PRIVATE. Draw paper and paper outline. */
143 /*--------------------------------------------------------------------------*/
145 draw_paper (cairo_t *cr,
146 const glTemplate *template,
149 gl_debug (DEBUG_MINI_PREVIEW, "START");
152 cairo_rectangle (cr, 0.0, 0.0, template->page_width, template->page_height);
153 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
154 cairo_fill_preserve (cr);
155 cairo_set_line_width (cr, 1/scale);
156 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
160 gl_debug (DEBUG_MINI_PREVIEW, "END");
163 /*--------------------------------------------------------------------------*/
164 /* PRIVATE. Draw label outlines. */
165 /*--------------------------------------------------------------------------*/
167 draw_label_outlines (cairo_t *cr,
168 const glTemplate *template,
171 const glTemplateLabelType *label_type;
173 glTemplateOrigin *origins;
175 gl_debug (DEBUG_MINI_PREVIEW, "START");
179 cairo_set_line_width (cr, 1.0/scale);
180 cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
182 label_type = gl_template_get_first_label_type (template);
184 n_labels = gl_template_get_n_labels (label_type);
185 origins = gl_template_get_origins (label_type);
187 for ( i=0; i < n_labels; i++ ) {
189 draw_label_outline(cr, template, origins[i].x, origins[i].y);
197 gl_debug (DEBUG_MINI_PREVIEW, "END");
200 /*--------------------------------------------------------------------------*/
201 /* PRIVATE. Draw label outline. */
202 /*--------------------------------------------------------------------------*/
204 draw_label_outline (cairo_t *cr,
205 const glTemplate *template,
209 const glTemplateLabelType *label_type;
211 gl_debug (DEBUG_MINI_PREVIEW, "START");
215 label_type = gl_template_get_first_label_type (template);
217 switch (label_type->shape) {
219 case GL_TEMPLATE_SHAPE_RECT:
220 draw_rect_label_outline (cr, template, x0, y0);
223 case GL_TEMPLATE_SHAPE_ROUND:
224 draw_round_label_outline (cr, template, x0, y0);
227 case GL_TEMPLATE_SHAPE_CD:
228 draw_cd_label_outline (cr, template, x0, y0);
232 g_message ("Unknown label style");
238 gl_debug (DEBUG_MINI_PREVIEW, "END");
241 /*--------------------------------------------------------------------------*/
242 /* PRIVATE. Draw rectangular label outline. */
243 /*--------------------------------------------------------------------------*/
245 draw_rect_label_outline (cairo_t *cr,
246 const glTemplate *template,
250 const glTemplateLabelType *label_type;
253 gl_debug (DEBUG_MINI_PREVIEW, "START");
257 label_type = gl_template_get_first_label_type (template);
258 gl_template_get_label_size (label_type, &w, &h);
260 cairo_rectangle (cr, x0, y0, w, h);
265 gl_debug (DEBUG_MINI_PREVIEW, "END");
268 /*--------------------------------------------------------------------------*/
269 /* PRIVATE. Draw round label outline. */
270 /*--------------------------------------------------------------------------*/
272 draw_round_label_outline (cairo_t *cr,
273 const glTemplate *template,
277 const glTemplateLabelType *label_type;
280 gl_debug (DEBUG_MINI_PREVIEW, "START");
284 label_type = gl_template_get_first_label_type (template);
285 gl_template_get_label_size (label_type, &w, &h);
287 cairo_arc (cr, x0+w/2, y0+h/2, w/2, 0.0, 2*M_PI);
292 gl_debug (DEBUG_MINI_PREVIEW, "END");
295 /*--------------------------------------------------------------------------*/
296 /* PRIVATE. Draw cd label outline. */
297 /*--------------------------------------------------------------------------*/
299 draw_cd_label_outline (cairo_t *cr,
300 const glTemplate *template,
304 const glTemplateLabelType *label_type;
309 gl_debug (DEBUG_MINI_PREVIEW, "START");
313 label_type = gl_template_get_first_label_type (template);
314 gl_template_get_label_size (label_type, &w, &h);
319 r1 = label_type->size.cd.r1;
320 r2 = label_type->size.cd.r2;
325 cairo_arc (cr, xc, yc, r1, 0.0, 2*M_PI);
330 /* Credit Card CD (One or both dimensions trucated) */
331 gdouble theta1, theta2;
333 theta1 = acos (w / (2.0*r1));
334 theta2 = asin (h / (2.0*r1));
337 cairo_arc (cr, xc, yc, r1, theta1, theta2);
338 cairo_arc (cr, xc, yc, r1, M_PI-theta2, M_PI-theta1);
339 cairo_arc (cr, xc, yc, r1, M_PI+theta1, M_PI+theta2);
340 cairo_arc (cr, xc, yc, r1, 2*M_PI-theta2, 2*M_PI-theta1);
341 cairo_close_path (cr);
346 cairo_arc (cr, xc, yc, r2, 0.0, 2*M_PI);
351 gl_debug (DEBUG_MINI_PREVIEW, "END");