annotate src/paint.c @ 147:995ee8fd5f1a

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