]> git.sur5r.net Git - glabels/blob - glabels2/src/cairo-label-path.c
2009-09-22 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / src / cairo-label-path.c
1 /*
2  *  cairo-label-path.c
3  *  Copyright (C) 2001-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 "cairo-label-path.h"
24
25 #include <math.h>
26
27 #include "debug.h"
28
29
30 /*===========================================*/
31 /* Private types                             */
32 /*===========================================*/
33
34
35 /*===========================================*/
36 /* Private globals                           */
37 /*===========================================*/
38
39
40 /*===========================================*/
41 /* Local function prototypes                 */
42 /*===========================================*/
43
44 static void gl_cairo_rect_label_path             (cairo_t                *cr,
45                                                   lglTemplate            *template,
46                                                   gboolean                rotate_flag,
47                                                   gboolean                waste_flag);
48 static void gl_cairo_round_label_path            (cairo_t                *cr,
49                                                   lglTemplate            *template,
50                                                   gboolean                rotate_flag,
51                                                   gboolean                waste_flag);
52 static void gl_cairo_cd_label_path               (cairo_t                *cr,
53                                                   lglTemplate            *template,
54                                                   gboolean                rotate_flag,
55                                                   gboolean                waste_flag);
56
57
58 /*--------------------------------------------------------------------------*/
59 /* Create label path                                                        */
60 /*--------------------------------------------------------------------------*/
61 void
62 gl_cairo_label_path (cairo_t           *cr,
63                      lglTemplate       *template,
64                      gboolean           rotate_flag,
65                      gboolean           waste_flag)
66 {
67         const lglTemplateFrame *frame;
68
69         gl_debug (DEBUG_PATH, "START");
70
71         frame = (lglTemplateFrame *)template->frames->data;
72
73         switch (frame->shape) {
74
75         case LGL_TEMPLATE_FRAME_SHAPE_RECT:
76                 gl_cairo_rect_label_path (cr, template, rotate_flag, waste_flag);
77                 break;
78
79         case LGL_TEMPLATE_FRAME_SHAPE_ROUND:
80                 gl_cairo_round_label_path (cr, template, rotate_flag, waste_flag);
81                 break;
82
83         case LGL_TEMPLATE_FRAME_SHAPE_CD:
84                 gl_cairo_cd_label_path (cr, template, rotate_flag, waste_flag);
85                 break;
86
87         default:
88                 g_message ("Unknown label style");
89                 break;
90         }
91
92         gl_debug (DEBUG_PATH, "END");
93 }
94
95
96 /*--------------------------------------------------------------------------*/
97 /* Create rectangular label path                                            */
98 /*--------------------------------------------------------------------------*/
99 static void
100 gl_cairo_rect_label_path (cairo_t           *cr,
101                           lglTemplate       *template,
102                           gboolean           rotate_flag,
103                           gboolean           waste_flag)
104 {
105         const lglTemplateFrame *frame;
106         gdouble                 w, h, r;
107         gdouble                 x_waste, y_waste;
108
109         gl_debug (DEBUG_PATH, "START");
110
111         frame = (lglTemplateFrame *)template->frames->data;
112         r = frame->rect.r;
113
114         x_waste = 0.0;
115         y_waste = 0.0;
116
117         if (rotate_flag)
118         {
119                 lgl_template_frame_get_size (frame, &h, &w);
120                 if (waste_flag)
121                 {
122                         x_waste = frame->rect.y_waste;
123                         y_waste = frame->rect.x_waste;
124                 }
125         }
126         else
127         {
128                 lgl_template_frame_get_size (frame, &w, &h);
129                 if (waste_flag)
130                 {
131                         x_waste = frame->rect.x_waste;
132                         y_waste = frame->rect.y_waste;
133                 }
134         }
135
136
137         if ( r == 0.0 )
138         {
139                 cairo_rectangle (cr, -x_waste, -y_waste, w+x_waste, h+y_waste);
140         }
141         else
142         {
143                 cairo_new_path (cr);
144                 cairo_arc_negative (cr, r-x_waste,   r-y_waste,   r, 3*G_PI/2, G_PI);
145                 cairo_arc_negative (cr, r-x_waste,   h-r+y_waste, r, G_PI,     G_PI/2);
146                 cairo_arc_negative (cr, w-r+x_waste, h-r+y_waste, r, G_PI/2,   0.);
147                 cairo_arc_negative (cr, w-r+x_waste, r-y_waste,   r, 2*G_PI,   3*G_PI/2);
148                 cairo_close_path (cr);
149         }
150
151         gl_debug (DEBUG_PATH, "END");
152 }
153
154
155 /*--------------------------------------------------------------------------*/
156 /* Create round label path                                                  */
157 /*--------------------------------------------------------------------------*/
158 static void
159 gl_cairo_round_label_path (cairo_t           *cr,
160                            lglTemplate       *template,
161                            gboolean           rotate_flag,
162                            gboolean           waste_flag)
163 {
164         const lglTemplateFrame *frame;
165         gdouble                 w, h;
166         gdouble                 waste;
167
168         gl_debug (DEBUG_PATH, "START");
169
170         frame = (lglTemplateFrame *)template->frames->data;
171         
172         if (rotate_flag)
173         {
174                 lgl_template_frame_get_size (frame, &h, &w);
175         }
176         else
177         {
178                 lgl_template_frame_get_size (frame, &w, &h);
179         }
180
181         if (waste_flag)
182         {
183                 waste = frame->round.waste;
184         }
185         else
186         {
187                 waste = 0.0;
188         }
189
190         cairo_arc (cr, w/2, h/2, w/2+waste, 0.0, 2*G_PI);
191         cairo_close_path (cr);
192
193         gl_debug (DEBUG_PATH, "END");
194 }
195
196
197 /*--------------------------------------------------------------------------*/
198 /* Create cd label path                                                     */
199 /*--------------------------------------------------------------------------*/
200 static void
201 gl_cairo_cd_label_path (cairo_t           *cr,
202                         lglTemplate       *template,
203                         gboolean           rotate_flag,
204                         gboolean           waste_flag)
205 {
206         const lglTemplateFrame *frame;
207         gdouble                 w, h;
208         gdouble                 xc, yc;
209         gdouble                 r1, r2;
210         gdouble                 theta1, theta2;
211         gdouble                 waste;
212
213         gl_debug (DEBUG_PATH, "START");
214
215         frame = (lglTemplateFrame *)template->frames->data;
216
217         if (rotate_flag)
218         {
219                 lgl_template_frame_get_size (frame, &h, &w);
220         }
221         else
222         {
223                 lgl_template_frame_get_size (frame, &w, &h);
224         }
225
226         xc = w/2.0;
227         yc = h/2.0;
228
229         r1 = frame->cd.r1;
230         r2 = frame->cd.r2;
231
232         if (waste_flag)
233         {
234                 waste = frame->cd.waste;
235         }
236         else
237         {
238                 waste = 0.0;
239         }
240
241         /*
242          * Outer path (may be clipped in the case of a business card type CD)
243          */
244         theta1 = acos (w / (2.0*r1));
245         theta2 = asin (h / (2.0*r1));
246
247         cairo_new_path (cr);
248         cairo_arc (cr, xc, yc, r1+waste, theta1, theta2);
249         cairo_arc (cr, xc, yc, r1+waste, G_PI-theta2, G_PI-theta1);
250         cairo_arc (cr, xc, yc, r1+waste, G_PI+theta1, G_PI+theta2);
251         cairo_arc (cr, xc, yc, r1+waste, 2*G_PI-theta2, 2*G_PI-theta1);
252         cairo_close_path (cr);
253
254
255         /*
256          * Inner path (hole)
257          */
258         cairo_new_sub_path (cr);
259         cairo_arc (cr, xc, yc, r2-waste, 0.0, 2*G_PI);
260         cairo_close_path (cr);
261
262         gl_debug (DEBUG_PATH, "END");
263 }
264
265
266
267 /*
268  * Local Variables:       -- emacs
269  * mode: C                -- emacs
270  * c-basic-offset: 8      -- emacs
271  * tab-width: 8           -- emacs
272  * indent-tabs-mode: nil  -- emacs
273  * End:                   -- emacs
274  */