summaryrefslogtreecommitdiff
path: root/src/lib/ector/cairo/ector_renderer_cairo_shape.c
blob: 99e3016b87bbb4461ab16a833ba66b1252288a90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <math.h>
#include <float.h>

#include <Eina.h>
#include <Ector.h>
#include <cairo/Ector_Cairo.h>

#include "ector_private.h"
#include "ector_cairo_private.h"

typedef struct _cairo_path_t cairo_path_t;

typedef enum _cairo_line_cap {
  CAIRO_LINE_CAP_BUTT,
  CAIRO_LINE_CAP_ROUND,
  CAIRO_LINE_CAP_SQUARE
} cairo_line_cap_t;

typedef enum _cairo_line_join {
  CAIRO_LINE_JOIN_MITER,
  CAIRO_LINE_JOIN_ROUND,
  CAIRO_LINE_JOIN_BEVEL
} cairo_line_join_t;

typedef enum _cairo_fill_rule {
  CAIRO_FILL_RULE_WINDING,
  CAIRO_FILL_RULE_EVEN_ODD
} cairo_fill_rule_t;

static void (*cairo_move_to)(cairo_t *cr, double x, double y) = NULL;
static void (*cairo_line_to)(cairo_t *cr, double x, double y) = NULL;
static void (*cairo_curve_to)(cairo_t *cr,
                              double x1, double y1,
                              double x2, double y2,
                              double x3, double y3) = NULL;
static void (*cairo_close_path)(cairo_t *cr) = NULL;

static void (*cairo_fill)(cairo_t *cr) = NULL;
static void (*cairo_fill_preserve)(cairo_t *cr) = NULL;
static void (*cairo_stroke)(cairo_t *cr) = NULL;

static void (*cairo_set_source_rgba)(cairo_t *cr,
                                     double red, double green,
                                     double blue, double alpha) = NULL;


static cairo_path_t *(*cairo_copy_path)(cairo_t *cr) = NULL;
static void (*cairo_path_destroy)(cairo_path_t *path) = NULL;
static void (*cairo_new_path)(cairo_t *cr) = NULL;
static void (*cairo_append_path)(cairo_t *cr, const cairo_path_t *path) = NULL;

static void (*cairo_set_line_width)(cairo_t *cr, double width) = NULL;
static void (*cairo_set_line_cap)(cairo_t *cr, cairo_line_cap_t line_cap) = NULL;
static void (*cairo_set_line_join)(cairo_t *cr, cairo_line_join_t line_join) = NULL;

static void (*cairo_save)(cairo_t *cr) = NULL;
static void (*cairo_restore)(cairo_t *cr) = NULL;

static void (*cairo_set_dash) (cairo_t *cr, const double *dashes, int num_dashes, double offset) = NULL;
static void (*cairo_set_fill_rule) (cairo_t *cr, cairo_fill_rule_t fill_rule);

typedef struct _Ector_Renderer_Cairo_Shape_Data Ector_Renderer_Cairo_Shape_Data;
struct _Ector_Renderer_Cairo_Shape_Data
{
   Efl_Gfx_Shape_Public *public_shape;

   Ector_Cairo_Surface_Data *parent;
   Ector_Renderer_Generic_Shape_Data *shape;
   Ector_Renderer_Generic_Base_Data *base;

   cairo_path_t *path;
};

static Eina_Bool
_ector_renderer_cairo_shape_path_changed(void *data, const Eo_Event *event EINA_UNUSED)
{
   Ector_Renderer_Cairo_Shape_Data *pd = data;

   if (pd->path) cairo_path_destroy(pd->path);
   pd->path = NULL;

   return EINA_TRUE;
}

static Eina_Bool
_ector_renderer_cairo_shape_ector_renderer_generic_base_prepare(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd)
{
   const Efl_Gfx_Path_Command *cmds = NULL;
   const double *pts = NULL;

   ector_renderer_prepare(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS));

   if (pd->shape->fill)
     ector_renderer_prepare(pd->shape->fill);
   if (pd->shape->stroke.fill)
     ector_renderer_prepare(pd->shape->stroke.fill);
   if (pd->shape->stroke.marker)
     ector_renderer_prepare(pd->shape->stroke.marker);

   // shouldn't that be moved to the cairo base object
   if (!pd->parent)
     {
        Ector_Renderer_Generic_Base_Data *base;

        base = eo_data_scope_get(obj, ECTOR_RENDERER_GENERIC_BASE_CLASS);
        pd->parent = eo_data_xref(base->surface, ECTOR_CAIRO_SURFACE_CLASS, obj);
     }

   efl_gfx_shape_path_get(obj, &cmds, &pts);
   if (!pd->path && cmds)
     {
        cairo_new_path(pd->parent->cairo);

        for (; *cmds != EFL_GFX_PATH_COMMAND_TYPE_END; cmds++)
          {
             switch (*cmds)
               {
                case EFL_GFX_PATH_COMMAND_TYPE_MOVE_TO:
                   cairo_move_to(pd->parent->cairo, pts[0], pts[1]);

                   pts += 2;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_LINE_TO:
                   cairo_line_to(pd->parent->cairo, pts[0], pts[1]);

                   pts += 2;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO:

                   cairo_curve_to(pd->parent->cairo,
                                  pts[0], pts[1], pts[2], pts[3], // control points
                                  pts[4], pts[5]); // destination point

                   pts += 6;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_CLOSE:
                   cairo_close_path(pd->parent->cairo);
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_LAST:
                case EFL_GFX_PATH_COMMAND_TYPE_END:
                   break;
               }
          }

        pd->path = cairo_copy_path(pd->parent->cairo);
     }

   return EINA_TRUE;
}

static Eina_Bool
_ector_renderer_cairo_shape_ector_renderer_generic_base_draw(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd, Efl_Gfx_Render_Op op, Eina_Array *clips, unsigned int mul_col)
{
   int r, g, b, a;
   unsigned i;
   Efl_Gfx_Fill_Rule fill_rule;

   if (pd->path == NULL) return EINA_FALSE;

   cairo_save(pd->parent->cairo);

   ector_renderer_draw(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS), op, clips, mul_col);

   cairo_new_path(pd->parent->cairo);
   cairo_append_path(pd->parent->cairo, pd->path);

   fill_rule = efl_gfx_shape_fill_rule_get(obj);
   if (fill_rule == EFL_GFX_FILL_RULE_ODD_EVEN)
     cairo_set_fill_rule(pd->parent->cairo, CAIRO_FILL_RULE_EVEN_ODD);
  else
    cairo_set_fill_rule(pd->parent->cairo, CAIRO_FILL_RULE_WINDING);

   if (pd->shape->fill)
     ector_renderer_cairo_base_fill(pd->shape->fill, mul_col);

   if (pd->shape->stroke.fill || pd->public_shape->stroke.color.a > 0)
     {
        cairo_fill_preserve(pd->parent->cairo);

        if (pd->shape->stroke.fill)
          ector_renderer_cairo_base_fill(pd->shape->stroke.fill, mul_col);
       else
         {
            r = (((pd->public_shape->stroke.color.r * R_VAL(&mul_col)) + 0xff) >> 8);
            g = (((pd->public_shape->stroke.color.g * G_VAL(&mul_col)) + 0xff) >> 8);
            b = (((pd->public_shape->stroke.color.b * B_VAL(&mul_col)) + 0xff) >> 8);
            a = (((pd->public_shape->stroke.color.a * A_VAL(&mul_col)) + 0xff) >> 8);
            ector_color_argb_unpremul(a, &r, &g, &b);
            cairo_set_source_rgba(pd->parent->cairo, r/255.0, g/255.0, b/255.0, a/255.0);
            if (pd->public_shape->stroke.dash)
              {
                 double *dashinfo;

                 dashinfo = (double *) malloc(2 * pd->public_shape->stroke.dash_length * sizeof(double));
                 for (i = 0; i < pd->public_shape->stroke.dash_length; i++)
                   {
                      dashinfo[i*2] = pd->public_shape->stroke.dash[i].length;
                      dashinfo[i*2 + 1] = pd->public_shape->stroke.dash[i].gap;
                   }
                 cairo_set_dash(pd->parent->cairo, dashinfo, pd->public_shape->stroke.dash_length * 2, 0);
                 free(dashinfo);
              }
         }

       // Set dash, cap and join
       cairo_set_line_width(pd->parent->cairo, (pd->public_shape->stroke.width * pd->public_shape->stroke.scale * 2));
       cairo_set_line_cap(pd->parent->cairo, (cairo_line_cap_t) pd->public_shape->stroke.cap);
       cairo_set_line_join(pd->parent->cairo, (cairo_line_join_t) pd->public_shape->stroke.join);
       cairo_stroke(pd->parent->cairo);
     }
   else
     {
        cairo_fill(pd->parent->cairo);
     }

   cairo_restore(pd->parent->cairo);
   return EINA_TRUE;
}

static Eina_Bool
_ector_renderer_cairo_shape_ector_renderer_cairo_base_fill(Eo *obj EINA_UNUSED,
                                                           Ector_Renderer_Cairo_Shape_Data *pd EINA_UNUSED,
                                                           unsigned int mul_col EINA_UNUSED)
{
   // FIXME: let's find out how to fill a shape with a shape later.
   // I need to read SVG specification and see how to map that with cairo.
   ERR("fill with shape not implemented\n");
   return EINA_FALSE;
}

static void
_ector_renderer_cairo_shape_ector_renderer_generic_base_bounds_get(Eo *obj,
                                                                   Ector_Renderer_Cairo_Shape_Data *pd EINA_UNUSED,
                                                                   Eina_Rectangle *r)
{
   Ector_Renderer_Cairo_Base_Data *bd;

   // FIXME: It should be possible to actually ask cairo about that
   efl_gfx_shape_bounds_get(obj, r);

   bd = eo_data_scope_get(obj, ECTOR_RENDERER_CAIRO_BASE_CLASS);
   r->x += bd->generic->origin.x;
   r->y += bd->generic->origin.y;
}

Eo *
_ector_renderer_cairo_shape_eo_base_constructor(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd)
{
   obj = eo_constructor(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS));

   if (!obj) return NULL;

   pd->public_shape = eo_data_xref(obj, EFL_GFX_SHAPE_MIXIN, obj);
   pd->shape = eo_data_xref(obj, ECTOR_RENDERER_GENERIC_SHAPE_MIXIN, obj);
   pd->base = eo_data_xref(obj, ECTOR_RENDERER_GENERIC_BASE_CLASS, obj);

   eo_event_callback_add(obj, EFL_GFX_PATH_CHANGED, _ector_renderer_cairo_shape_path_changed, pd);

    return obj;
}

static Eo_Base *
_ector_renderer_cairo_shape_eo_base_finalize(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd)
{
   obj = eo_finalize(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS));
   if (!obj) return NULL;

   USE(pd->base, cairo_path_destroy, NULL);
   USE(pd->base, cairo_restore, NULL);
   USE(pd->base, cairo_fill, NULL);
   USE(pd->base, cairo_set_dash, NULL);
   USE(pd->base, cairo_fill_preserve, NULL);
   USE(pd->base, cairo_set_source_rgba, NULL);
   USE(pd->base, cairo_stroke, NULL);
   USE(pd->base, cairo_set_line_width, NULL);
   USE(pd->base, cairo_set_line_cap, NULL);
   USE(pd->base, cairo_set_line_join, NULL);
   USE(pd->base, cairo_new_path, NULL);
   USE(pd->base, cairo_append_path, NULL);
   USE(pd->base, cairo_save, NULL);
   USE(pd->base, cairo_copy_path, NULL);
   USE(pd->base, cairo_close_path, NULL);
   USE(pd->base, cairo_curve_to, NULL);
   USE(pd->base, cairo_line_to, NULL);
   USE(pd->base, cairo_move_to, NULL);
   USE(pd->base, cairo_set_fill_rule, NULL);

   return obj;
}

void
_ector_renderer_cairo_shape_eo_base_destructor(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd)
{
   Ector_Renderer_Generic_Base_Data *base;

   //FIXME, As base class  destructor can't call destructor of mixin class.
   // call explicit API to free shape data.
   efl_gfx_shape_reset(obj);

   base = eo_data_scope_get(obj, ECTOR_RENDERER_GENERIC_BASE_CLASS);
   eo_data_xunref(base->surface, pd->parent, obj);

   eo_data_xunref(obj, pd->shape, obj);
   eo_data_xunref(obj, pd->base, obj);

   eo_destructor(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS));

   if (pd->path) cairo_path_destroy(pd->path);
}

unsigned int
_ector_renderer_cairo_shape_ector_renderer_generic_base_crc_get(Eo *obj,
                                                                Ector_Renderer_Cairo_Shape_Data *pd)
{
   unsigned int crc;

   crc = ector_renderer_crc_get(eo_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS));

   crc = eina_crc((void*) &pd->shape->stroke.marker, sizeof (pd->shape->stroke.marker), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->public_shape->stroke.scale, sizeof (pd->public_shape->stroke.scale) * 3, crc, EINA_FALSE); // scale, width, centered
   crc = eina_crc((void*) &pd->public_shape->stroke.color, sizeof (pd->public_shape->stroke.color), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->public_shape->stroke.cap, sizeof (pd->public_shape->stroke.cap), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->public_shape->stroke.join, sizeof (pd->public_shape->stroke.join), crc, EINA_FALSE);

   if (pd->shape->fill) crc = _renderer_crc_get(pd->shape->fill, crc);
   if (pd->shape->stroke.fill) crc = _renderer_crc_get(pd->shape->stroke.fill, crc);
   if (pd->shape->stroke.marker) crc = _renderer_crc_get(pd->shape->stroke.marker, crc);
   if (pd->public_shape->stroke.dash_length)
     {
        crc = eina_crc((void*) pd->public_shape->stroke.dash, sizeof (Efl_Gfx_Dash) * pd->public_shape->stroke.dash_length, crc, EINA_FALSE);
     }

   return crc;
}

#include "ector_renderer_cairo_shape.eo.c"