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