annotate src/paint.c @ 196:c234ee745ceb

Start moving to mb_obj_t
author Thinker K.F. Li <thinker@branda.to>
date Tue, 09 Dec 2008 17:34:10 +0800
parents 530bb7728546
children 29acbd8a0dd0
rev   line source
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
1 #include <stdio.h>
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <stdlib.h>
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include <cairo.h>
186
530bb7728546 Move header files to $(top_srcdir)/include/ and prefixed with 'mb_'.
Thinker K.F. Li <thinker@branda.to>
parents: 185
diff changeset
4 #include "mb_paint.h"
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
6 #define ASSERT(x)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
7
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
8 /*! \brief Solid color paint.
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
9 */
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 typedef struct _paint_color {
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 paint_t paint;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
12 co_comp_t r, g, b, a;
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 } paint_color_t;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14
147
995ee8fd5f1a Use local static variable to hold position array to reduce using malloc().
Thinker K.F. Li <thinker@branda.to>
parents: 146
diff changeset
15 int _paint_color_size = sizeof(paint_color_t);
146
e96a584487af Use elmpool to manage paint_color_t objects.
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
16
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 static void paint_color_prepare(paint_t *paint, cairo_t *cr) {
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 paint_color_t *color = (paint_color_t *)paint;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
21 cairo_set_source_rgba(cr, color->r, color->g, color->b, color->a);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
24 static void paint_color_free(redraw_man_t *rdman, paint_t *paint) {
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
25 shnode_list_free(rdman, paint->members);
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
26 elmpool_elm_free(rdman->paint_color_pool, paint);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
29 paint_t *rdman_paint_color_new(redraw_man_t *rdman,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
30 co_comp_t r, co_comp_t g,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
31 co_comp_t b, co_comp_t a) {
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 paint_color_t *color;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
146
e96a584487af Use elmpool to manage paint_color_t objects.
Thinker K.F. Li <thinker@branda.to>
parents: 58
diff changeset
34 color = (paint_color_t *)elmpool_elm_alloc(rdman->paint_color_pool);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35 if(color == NULL)
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 return NULL;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 color->r = r;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 color->g = g;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
39 color->b = b;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
40 color->a = a;
19
cf6d65398619 More animation demo
Thinker K.F. Li <thinker@branda.to>
parents: 18
diff changeset
41 paint_init(&color->paint, paint_color_prepare, paint_color_free);
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 return (paint_t *)color;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
43 }
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45 void paint_color_set(paint_t *paint,
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
46 co_comp_t r, co_comp_t g,
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
47 co_comp_t b, co_comp_t a) {
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 paint_color_t *color = (paint_color_t *)paint;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 color->r = r;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 color->g = g;
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 color->b = b;
21
83d24300a992 opacity (alpha) channel
Thinker K.F. Li <thinker@branda.to>
parents: 19
diff changeset
53 color->a = a;
18
0f3baa488a62 Support solid color paint for fill.
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 }
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
55
52
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
56 void paint_color_get(paint_t *paint,
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
57 co_comp_t *r, co_comp_t *g,
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
58 co_comp_t *b, co_comp_t *a) {
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
59 paint_color_t *color = (paint_color_t *)paint;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
60
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
61 *r = color->r;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
62 *g = color->g;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
63 *b = color->b;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
64 *a = color->a;
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
65 }
59a295651480 Add action mb_chgcolor_t to change color of paints.
Thinker K.F. Li <thinker@branda.to>
parents: 23
diff changeset
66
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
67 /*! \brief Linear gradient.
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
68 */
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
69 typedef struct _paint_linear {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
70 paint_t paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
71 co_aix x1, y1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
72 co_aix x2, y2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
73 int n_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
74 grad_stop_t *stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
75 int flags;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
76 cairo_pattern_t *ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
77 } paint_linear_t;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
78
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
79 #define LIF_DIRTY 0x1
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
80
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
81 static void paint_linear_prepare(paint_t *paint, cairo_t *cr) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
82 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
83 cairo_pattern_t *ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
84 grad_stop_t *stop;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
85 int i;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
86
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
87 ptn = linear->ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
88 if(linear->flags & LIF_DIRTY) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
89 if(ptn)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
90 cairo_pattern_destroy(ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
91 linear->flags &= ~LIF_DIRTY;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
92 ptn = cairo_pattern_create_linear(linear->x1, linear->y1,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
93 linear->x2, linear->y2);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
94 for(i = 0; i < linear->n_stops; i++) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
95 stop = &linear->stops[i];
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
96 cairo_pattern_add_color_stop_rgba(ptn, stop->offset,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
97 stop->r, stop->g, stop->b,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
98 stop->a);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
99 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
100 linear->ptn = ptn;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
101 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
102
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
103 cairo_set_source(cr, ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
104 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
105
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
106 static void paint_linear_free(redraw_man_t *rdman, paint_t *paint) {
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
107 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
108
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
109 if(linear->ptn)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
110 cairo_pattern_destroy(linear->ptn);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
111 free(paint);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
112 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
113
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
114 paint_t *rdman_paint_linear_new(redraw_man_t *rdman,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
115 co_aix x1, co_aix y1,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
116 co_aix x2, co_aix y2) {
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
117 paint_linear_t *linear;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
118
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
119 linear = (paint_linear_t *)malloc(sizeof(paint_linear_t));
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
120 if(linear == NULL)
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
121 return NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
122
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
123 paint_init(&linear->paint, paint_linear_prepare, paint_linear_free);
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
124
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
125 linear->x1 = x1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
126 linear->y1 = y1;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
127 linear->x2 = x2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
128 linear->y2 = y2;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
129 linear->n_stops = 0;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
130 linear->stops = NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
131 linear->flags = LIF_DIRTY;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
132 linear->ptn = NULL;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
133
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
134 return (paint_t *)linear;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
135 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
136
58
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
137 /*! \brief Setup color stop for a linear radient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
138 *
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
139 * stops should be managed by users of the function. It should be
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
140 * available before the paint being freed or changed to another
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
141 * array of stops.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
142 */
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
143 grad_stop_t *paint_linear_stops(paint_t *paint,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
144 int n_stops,
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
145 grad_stop_t *stops) {
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
146 paint_linear_t *linear = (paint_linear_t *)paint;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
147 grad_stop_t *old_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
148
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
149 old_stops = linear->stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
150 linear->n_stops = n_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
151 linear->stops = stops;
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
152 linear->flags |= LIF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
153
23
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
154 return old_stops;
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
155 }
56f592f56ff7 Fix bug and add linear gradient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 21
diff changeset
156
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
157 /*! \brief Radial gradient.
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
158 *
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
159 * NOTE: The only supported gradient unit is userSpaceOnUse.
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
160 */
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
161 typedef struct _paint_radial {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
162 paint_t paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
163 co_aix cx, cy;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
164 co_aix r;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
165 int n_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
166 grad_stop_t *stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
167 int flags;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
168 cairo_pattern_t *ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
169 } paint_radial_t;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
170
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
171 #define RDF_DIRTY 0x1
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
172
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
173 static void paint_radial_prepare(paint_t *paint, cairo_t *cr) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
174 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
175 cairo_pattern_t *ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
176 grad_stop_t *stop;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
177 int i;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
178
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
179 if(radial->flags & RDF_DIRTY) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
180 ptn = cairo_pattern_create_radial(radial->cx, radial->cy, 0,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
181 radial->cx, radial->cy,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
182 radial->r);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
183 ASSERT(ptn != NULL);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
184 stop = radial->stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
185 for(i = 0; i < radial->n_stops; i++, stop++) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
186 cairo_pattern_add_color_stop_rgba(ptn, stop->offset,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
187 stop->r, stop->g,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
188 stop->b, stop->a);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
189 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
190 cairo_pattern_destroy(radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
191 radial->ptn = ptn;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
192 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
193 cairo_set_source(cr, radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
194 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
195
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
196 static void paint_radial_free(redraw_man_t *rdman, paint_t *paint) {
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
197 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
198
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
199 if(radial->ptn)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
200 cairo_pattern_destroy(radial->ptn);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
201 free(paint);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
202 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
203
159
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
204 paint_t *rdman_paint_radial_new(redraw_man_t *rdman,
b90abd31a281 Postponse free of coords, shapes, and paints when the rdman is dirty.
Thinker K.F. Li <thinker@branda.to>
parents: 154
diff changeset
205 co_aix cx, co_aix cy, co_aix r) {
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
206 paint_radial_t *radial;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
207
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
208 radial = O_ALLOC(paint_radial_t);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
209 if(radial == NULL)
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
210 return NULL;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
211
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
212 paint_init(&radial->paint, paint_radial_prepare, paint_radial_free);
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
213 radial->cx = cx;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
214 radial->cy = cy;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
215 radial->r = r;
56
e444a8c01735 Change interface of paint_radial_new()
Thinker K.F. Li <thinker@branda.to>
parents: 55
diff changeset
216 radial->n_stops = 0;
e444a8c01735 Change interface of paint_radial_new()
Thinker K.F. Li <thinker@branda.to>
parents: 55
diff changeset
217 radial->stops = NULL;
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
218 radial->flags = RDF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
219 radial->ptn = NULL;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
220
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
221 return (paint_t *)radial;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
222 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
223
58
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
224 /*! \brief Setup color stop for a radial radient paint.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
225 *
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
226 * stops should be managed by users of the function. It should be
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
227 * available before the paint being freed or changed to another
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
228 * array of stops.
Thinker K.F. Li <thinker@branda.to>
parents: 56
diff changeset
229 */
55
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
230 grad_stop_t *paint_radial_stops(paint_t *paint,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
231 int n_stops,
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
232 grad_stop_t *stops) {
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
233 paint_radial_t *radial = (paint_radial_t *)paint;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
234 grad_stop_t *old_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
235
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
236 old_stops = radial->stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
237 radial->n_stops = n_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
238 radial->stops = stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
239 radial->flags |= RDF_DIRTY;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
240
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
241 return old_stops;
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
242 }
01ed2bc37eed Radial gradient paint
Thinker K.F. Li <thinker@branda.to>
parents: 52
diff changeset
243