aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ector/cairo/ector_renderer_cairo_shape.c
blob: 0f9193ed5c1087fba2328d925f408cf835e81827 (plain) (blame)
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
#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;

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;

typedef struct _Ector_Renderer_Cairo_Shape_Data Ector_Renderer_Cairo_Shape_Data;
struct _Ector_Renderer_Cairo_Shape_Data
{
   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, Eo *obj, const Eo_Event_Description *desc EINA_UNUSED, void *event_info EINA_UNUSED)
{
   Ector_Renderer_Cairo_Shape_Data *pd = data;

   USE(obj, cairo_path_destroy, EINA_TRUE);

   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;

   eo_do_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS, ector_renderer_prepare());

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

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

        eo_do(obj, parent = eo_parent_get());
        if (!parent) return EINA_FALSE;
        pd->parent = eo_data_xref(parent, ECTOR_CAIRO_SURFACE_CLASS, obj);
        if (!pd->parent) return EINA_FALSE;
     }

   eo_do(obj, efl_gfx_shape_path_get(&cmds, &pts));
   if (!pd->path && cmds)
     {
        USE(obj, cairo_new_path, EINA_FALSE);

        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:
                   USE(obj, cairo_move_to, EINA_FALSE);

                   cairo_move_to(pd->parent->cairo, pts[0], pts[1]);

                   pts += 2;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_LINE_TO:
                   USE(obj, cairo_line_to, EINA_FALSE);

                   cairo_line_to(pd->parent->cairo, pts[0], pts[1]);

                   pts += 2;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_CUBIC_TO:
                   USE(obj, cairo_curve_to, EINA_FALSE);

                   // Be careful, we do have a different order than
                   // cairo, first is destination point, followed by
                   // the control point. The opposite of cairo.
                   cairo_curve_to(pd->parent->cairo,
                                  pts[2], pts[3], pts[4], pts[5], // control points
                                  pts[0], pts[1]); // destination point

                   pts += 6;
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_CLOSE:
                   USE(obj, cairo_close_path, EINA_FALSE);

                   cairo_close_path(pd->parent->cairo);
                   break;
                case EFL_GFX_PATH_COMMAND_TYPE_LAST:
                case EFL_GFX_PATH_COMMAND_TYPE_END:
                   break;
               }
          }

        USE(obj, cairo_copy_path, EINA_FALSE);

        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, Ector_Rop op, Eina_Array *clips, unsigned int mul_col)
{
   int r, g, b, a;
   unsigned i;

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

   USE(obj, cairo_save, EINA_FALSE);
   cairo_save(pd->parent->cairo);

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

   USE(obj, cairo_new_path, EINA_FALSE);
   USE(obj, cairo_append_path, EINA_FALSE);

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

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

   if (pd->shape->stroke.fill || pd->shape->stroke.color.a > 0)
     {
        USE(obj, cairo_fill_preserve, EINA_FALSE);
        USE(obj, cairo_set_source_rgba, EINA_FALSE);
        USE(obj, cairo_stroke, EINA_FALSE);
        USE(obj, cairo_set_line_width, EINA_FALSE);
        USE(obj, cairo_set_line_cap, EINA_FALSE);
        USE(obj, cairo_set_line_join, EINA_FALSE);

        cairo_fill_preserve(pd->parent->cairo);

        if (pd->shape->stroke.fill)
          eo_do(pd->shape->stroke.fill, ector_renderer_cairo_base_fill(mul_col));
       else
         {
            r = (((pd->shape->stroke.color.r * R_VAL(&mul_col)) + 0xff) >> 8);
            g = (((pd->shape->stroke.color.g * G_VAL(&mul_col)) + 0xff) >> 8);
            b = (((pd->shape->stroke.color.b * B_VAL(&mul_col)) + 0xff) >> 8);
            a = (((pd->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->shape->stroke.dash)
              {
                 double *dashinfo;

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

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

   USE(obj, cairo_restore, EINA_FALSE);
   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
   eo_do(obj, efl_gfx_shape_bounds_get(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_do_super_ret(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS, obj, eo_constructor());
   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_do(obj,
         eo_event_callback_add(EFL_GFX_PATH_CHANGED, _ector_renderer_cairo_shape_path_changed, pd));

   return obj;
}

void
_ector_renderer_cairo_shape_eo_base_destructor(Eo *obj, Ector_Renderer_Cairo_Shape_Data *pd)
{
   Eo *parent;
   //FIXME, As base class  destructor can't call destructor of mixin class.
   // call explicit API to free shape data.
   eo_do(obj, efl_gfx_shape_reset());

   eo_do(obj, parent = eo_parent_get());
   eo_data_xunref(parent, pd->parent, obj);

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

   eo_do_super(obj, ECTOR_RENDERER_CAIRO_SHAPE_CLASS, eo_destructor());

   USE(obj, cairo_path_destroy, );
   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;

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

   crc = eina_crc((void*) &pd->shape->stroke.marker, sizeof (pd->shape->stroke.marker), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->shape->stroke.scale, sizeof (pd->shape->stroke.scale) * 3, crc, EINA_FALSE); // scale, width, centered
   crc = eina_crc((void*) &pd->shape->stroke.color, sizeof (pd->shape->stroke.color), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->shape->stroke.cap, sizeof (pd->shape->stroke.cap), crc, EINA_FALSE);
   crc = eina_crc((void*) &pd->shape->stroke.join, sizeof (pd->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->shape->stroke.dash_length)
     {
        crc = eina_crc((void*) pd->shape->stroke.dash, sizeof (Efl_Gfx_Dash) * pd->shape->stroke.dash_length, crc, EINA_FALSE);
     }

   return crc;
}

#include "ector_renderer_cairo_shape.eo.c"