1 /*
2  * Cogl
3  *
4  * A Low Level GPU Graphics and Utilities API
5  *
6  * Copyright (C) 2010,2013 Intel Corporation.
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation
10  * files (the "Software"), to deal in the Software without
11  * restriction, including without limitation the rights to use, copy,
12  * modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26  * SOFTWARE.
27  *
28  * Authors:
29  *  Robert Bragg <robert@linux.intel.com>
30  */
31 
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35 
36 #include "cogl-util.h"
37 #include "cogl-object.h"
38 #include "cogl-context-private.h"
39 
40 #include "cogl-path-types.h"
41 
42 #include "cogl2-path-functions.h"
43 
44 #undef cogl_path_set_fill_rule
45 #undef cogl_path_get_fill_rule
46 #undef cogl_path_fill
47 #undef cogl_path_fill_preserve
48 #undef cogl_path_stroke
49 #undef cogl_path_stroke_preserve
50 #undef cogl_path_move_to
51 #undef cogl_path_rel_move_to
52 #undef cogl_path_line_to
53 #undef cogl_path_rel_line_to
54 #undef cogl_path_close
55 #undef cogl_path_new
56 #undef cogl_path_line
57 #undef cogl_path_polyline
58 #undef cogl_path_polygon
59 #undef cogl_path_rectangle
60 #undef cogl_path_arc
61 #undef cogl_path_ellipse
62 #undef cogl_path_round_rectangle
63 #undef cogl_path_curve_to
64 #undef cogl_path_rel_curve_to
65 #undef cogl_clip_push_from_path
66 
67 #include "cogl1-path-functions.h"
68 
69 #include <string.h>
70 #include <math.h>
71 
72 static void
ensure_current_path(CoglContext * ctx)73 ensure_current_path (CoglContext *ctx)
74 {
75   if (ctx->current_path == NULL)
76     ctx->current_path = cogl2_path_new ();
77 }
78 
79 static CoglPath *
get_current_path(CoglContext * ctx)80 get_current_path (CoglContext *ctx)
81 {
82   ensure_current_path (ctx);
83   return ctx->current_path;
84 }
85 
86 void
cogl_path_set_fill_rule(CoglPathFillRule fill_rule)87 cogl_path_set_fill_rule (CoglPathFillRule fill_rule)
88 {
89   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
90 
91   cogl2_path_set_fill_rule (get_current_path (ctx), fill_rule);
92 }
93 
94 CoglPathFillRule
cogl_path_get_fill_rule(void)95 cogl_path_get_fill_rule (void)
96 {
97   _COGL_GET_CONTEXT (ctx, COGL_PATH_FILL_RULE_EVEN_ODD);
98 
99   return cogl2_path_get_fill_rule (get_current_path (ctx));
100 }
101 
102 void
cogl_path_fill(void)103 cogl_path_fill (void)
104 {
105   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
106 
107   cogl2_path_fill (get_current_path (ctx));
108 
109   if (ctx->current_path)
110     cogl_object_unref (ctx->current_path);
111   ctx->current_path = cogl2_path_new ();
112 }
113 
114 void
cogl_path_fill_preserve(void)115 cogl_path_fill_preserve (void)
116 {
117   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
118 
119   cogl2_path_fill (get_current_path (ctx));
120 }
121 
122 void
cogl_path_stroke(void)123 cogl_path_stroke (void)
124 {
125   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
126 
127   cogl2_path_stroke (get_current_path (ctx));
128 
129   if (ctx->current_path)
130     cogl_object_unref (ctx->current_path);
131   ctx->current_path = cogl2_path_new ();
132 }
133 
134 void
cogl_path_stroke_preserve(void)135 cogl_path_stroke_preserve (void)
136 {
137   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
138 
139   cogl2_path_stroke (get_current_path (ctx));
140 }
141 
142 void
cogl_path_move_to(float x,float y)143 cogl_path_move_to (float x,
144                    float y)
145 {
146   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
147 
148   cogl2_path_move_to (get_current_path (ctx), x, y);
149 }
150 
151 void
cogl_path_rel_move_to(float x,float y)152 cogl_path_rel_move_to (float x,
153                        float y)
154 {
155   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
156 
157   cogl2_path_rel_move_to (get_current_path (ctx), x, y);
158 }
159 
160 void
cogl_path_line_to(float x,float y)161 cogl_path_line_to (float x,
162                    float y)
163 {
164   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
165 
166   cogl2_path_line_to (get_current_path (ctx), x, y);
167 }
168 
169 void
cogl_path_rel_line_to(float x,float y)170 cogl_path_rel_line_to (float x,
171                        float y)
172 {
173   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
174 
175   cogl2_path_rel_line_to (get_current_path (ctx), x, y);
176 }
177 
178 void
cogl_path_close(void)179 cogl_path_close (void)
180 {
181   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
182 
183   cogl2_path_close (get_current_path (ctx));
184 }
185 
186 void
cogl_path_new(void)187 cogl_path_new (void)
188 {
189   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
190 
191   if (ctx->current_path)
192     cogl_object_unref (ctx->current_path);
193   ctx->current_path = cogl2_path_new ();
194 }
195 
196 void
cogl_path_line(float x_1,float y_1,float x_2,float y_2)197 cogl_path_line (float x_1,
198 	        float y_1,
199 	        float x_2,
200 	        float y_2)
201 {
202   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
203 
204   cogl2_path_line (get_current_path (ctx), x_1, y_1, x_2, y_2);
205 }
206 
207 void
cogl_path_polyline(const float * coords,int num_points)208 cogl_path_polyline (const float *coords,
209 	            int num_points)
210 {
211   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
212 
213   cogl2_path_polyline (get_current_path (ctx), coords, num_points);
214 }
215 
216 void
cogl_path_polygon(const float * coords,int num_points)217 cogl_path_polygon (const float *coords,
218 	           int num_points)
219 {
220   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
221 
222   cogl2_path_polygon (get_current_path (ctx), coords, num_points);
223 }
224 
225 void
cogl_path_rectangle(float x_1,float y_1,float x_2,float y_2)226 cogl_path_rectangle (float x_1,
227                      float y_1,
228                      float x_2,
229                      float y_2)
230 {
231   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
232 
233   cogl2_path_rectangle (get_current_path (ctx), x_1, y_1, x_2, y_2);
234 }
235 
236 void
cogl_path_arc(float center_x,float center_y,float radius_x,float radius_y,float angle_1,float angle_2)237 cogl_path_arc (float center_x,
238                float center_y,
239                float radius_x,
240                float radius_y,
241                float angle_1,
242                float angle_2)
243 {
244   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
245 
246   cogl2_path_arc (get_current_path (ctx),
247                   center_x,
248                   center_y,
249                   radius_x,
250                   radius_y,
251                   angle_1,
252                   angle_2);
253 }
254 
255 void
cogl_path_ellipse(float center_x,float center_y,float radius_x,float radius_y)256 cogl_path_ellipse (float center_x,
257                    float center_y,
258                    float radius_x,
259                    float radius_y)
260 {
261   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
262 
263   cogl2_path_ellipse (get_current_path (ctx),
264                       center_x,
265                       center_y,
266                       radius_x,
267                       radius_y);
268 }
269 
270 void
cogl_path_round_rectangle(float x_1,float y_1,float x_2,float y_2,float radius,float arc_step)271 cogl_path_round_rectangle (float x_1,
272                            float y_1,
273                            float x_2,
274                            float y_2,
275                            float radius,
276                            float arc_step)
277 {
278   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
279 
280   cogl2_path_round_rectangle (get_current_path (ctx),
281                               x_1, y_1, x_2, y_2, radius, arc_step);
282 }
283 
284 void
cogl_path_curve_to(float x_1,float y_1,float x_2,float y_2,float x_3,float y_3)285 cogl_path_curve_to (float x_1,
286                     float y_1,
287                     float x_2,
288                     float y_2,
289                     float x_3,
290                     float y_3)
291 {
292   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
293 
294   cogl2_path_curve_to (get_current_path (ctx),
295                        x_1, y_1, x_2, y_2, x_3, y_3);
296 }
297 
298 void
cogl_path_rel_curve_to(float x_1,float y_1,float x_2,float y_2,float x_3,float y_3)299 cogl_path_rel_curve_to (float x_1,
300                         float y_1,
301                         float x_2,
302                         float y_2,
303                         float x_3,
304                         float y_3)
305 {
306   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
307 
308   cogl2_path_rel_curve_to (get_current_path (ctx),
309                            x_1, y_1, x_2, y_2, x_3, y_3);
310 }
311 
312 CoglPath *
cogl_get_path(void)313 cogl_get_path (void)
314 {
315   _COGL_GET_CONTEXT (ctx, NULL);
316 
317   return get_current_path (ctx);
318 }
319 
320 void
cogl_set_path(CoglPath * path)321 cogl_set_path (CoglPath *path)
322 {
323   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
324 
325   _COGL_RETURN_IF_FAIL (cogl_is_path (path));
326 
327   /* Reference the new object first in case it is the same as the old
328      object */
329   cogl_object_ref (path);
330   if (ctx->current_path)
331     cogl_object_unref (ctx->current_path);
332   ctx->current_path = path;
333 }
334 
335 void
cogl_clip_push_from_path_preserve(void)336 cogl_clip_push_from_path_preserve (void)
337 {
338   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
339   cogl_framebuffer_push_path_clip (cogl_get_draw_framebuffer (),
340                                    get_current_path (ctx));
341 }
342 
343 void
cogl_clip_push_from_path(void)344 cogl_clip_push_from_path (void)
345 {
346   _COGL_GET_CONTEXT (ctx, NO_RETVAL);
347 
348   cogl_clip_push_from_path_preserve ();
349 
350   if (ctx->current_path)
351     cogl_object_unref (ctx->current_path);
352   ctx->current_path = cogl2_path_new ();
353 }
354