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
|
// 6 september 2015
#include "uipriv_unix.h"
#include "draw.h"
uiDrawContext *newContext(cairo_t *cr)
{
uiDrawContext *c;
c = uiNew(uiDrawContext);
c->cr = cr;
return c;
}
void freeContext(uiDrawContext *c)
{
uiFree(c);
}
static cairo_pattern_t *mkbrush(uiDrawBrush *b)
{
cairo_pattern_t *pat;
size_t i;
switch (b->Type) {
case uiDrawBrushTypeSolid:
pat = cairo_pattern_create_rgba(b->R, b->G, b->B, b->A);
break;
case uiDrawBrushTypeLinearGradient:
pat = cairo_pattern_create_linear(b->X0, b->Y0, b->X1, b->Y1);
break;
case uiDrawBrushTypeRadialGradient:
// make the start circle radius 0 to make it a point
pat = cairo_pattern_create_radial(
b->X0, b->Y0, 0,
b->X1, b->Y1, b->OuterRadius);
break;
// case uiDrawBrushTypeImage:
}
if (cairo_pattern_status(pat) != CAIRO_STATUS_SUCCESS)
implbug("error creating pattern in mkbrush(): %s",
cairo_status_to_string(cairo_pattern_status(pat)));
switch (b->Type) {
case uiDrawBrushTypeLinearGradient:
case uiDrawBrushTypeRadialGradient:
for (i = 0; i < b->NumStops; i++)
cairo_pattern_add_color_stop_rgba(pat,
b->Stops[i].Pos,
b->Stops[i].R,
b->Stops[i].G,
b->Stops[i].B,
b->Stops[i].A);
}
return pat;
}
void uiDrawStroke(uiDrawContext *c, uiDrawPath *path, uiDrawBrush *b, uiDrawStrokeParams *p)
{
cairo_pattern_t *pat;
runPath(path, c->cr);
pat = mkbrush(b);
cairo_set_source(c->cr, pat);
switch (p->Cap) {
case uiDrawLineCapFlat:
cairo_set_line_cap(c->cr, CAIRO_LINE_CAP_BUTT);
break;
case uiDrawLineCapRound:
cairo_set_line_cap(c->cr, CAIRO_LINE_CAP_ROUND);
break;
case uiDrawLineCapSquare:
cairo_set_line_cap(c->cr, CAIRO_LINE_CAP_SQUARE);
break;
}
switch (p->Join) {
case uiDrawLineJoinMiter:
cairo_set_line_join(c->cr, CAIRO_LINE_JOIN_MITER);
cairo_set_miter_limit(c->cr, p->MiterLimit);
break;
case uiDrawLineJoinRound:
cairo_set_line_join(c->cr, CAIRO_LINE_JOIN_ROUND);
break;
case uiDrawLineJoinBevel:
cairo_set_line_join(c->cr, CAIRO_LINE_JOIN_BEVEL);
break;
}
cairo_set_line_width(c->cr, p->Thickness);
cairo_set_dash(c->cr, p->Dashes, p->NumDashes, p->DashPhase);
cairo_stroke(c->cr);
cairo_pattern_destroy(pat);
}
void uiDrawFill(uiDrawContext *c, uiDrawPath *path, uiDrawBrush *b)
{
cairo_pattern_t *pat;
runPath(path, c->cr);
pat = mkbrush(b);
cairo_set_source(c->cr, pat);
switch (pathFillMode(path)) {
case uiDrawFillModeWinding:
cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_WINDING);
break;
case uiDrawFillModeAlternate:
cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_EVEN_ODD);
break;
}
cairo_fill(c->cr);
cairo_pattern_destroy(pat);
}
void uiDrawTransform(uiDrawContext *c, uiDrawMatrix *m)
{
cairo_matrix_t cm;
m2c(m, &cm);
cairo_transform(c->cr, &cm);
}
void uiDrawClip(uiDrawContext *c, uiDrawPath *path)
{
runPath(path, c->cr);
switch (pathFillMode(path)) {
case uiDrawFillModeWinding:
cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_WINDING);
break;
case uiDrawFillModeAlternate:
cairo_set_fill_rule(c->cr, CAIRO_FILL_RULE_EVEN_ODD);
break;
}
cairo_clip(c->cr);
}
void uiDrawSave(uiDrawContext *c)
{
cairo_save(c->cr);
}
void uiDrawRestore(uiDrawContext *c)
{
cairo_restore(c->cr);
}
// bitmap API
uiDrawBitmap* uiDrawNewBitmap(uiDrawContext* c, int width, int height)
{
uiDrawBitmap* bmp;
bmp = uiNew(uiDrawBitmap);
bmp->bmp = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height);
if (cairo_surface_status(bmp->bmp) != CAIRO_STATUS_SUCCESS)
implbug("error creating bitmap: %s",
cairo_status_to_string(cairo_surface_status(bmp->bmp)));
bmp->Width = width;
bmp->Height = height;
bmp->Stride = cairo_image_surface_get_stride(bmp->bmp);
return bmp;
}
void uiDrawBitmapUpdate(uiDrawBitmap* bmp, const void* data)
{
unsigned char* src = data;
unsigned char* dst = cairo_image_surface_get_data(bmp->bmp);
if (bmp->Stride == bmp->Width*4)
{
// stride 'good', can just directly copy all the shit
memcpy(dst, src, bmp->Stride*bmp->Height);
}
else
{
int y;
for (y = 0; y < bmp->Height; y++)
{
memcpy(dst, src, bmp->Width*4);
src += bmp->Width*4;
dst += bmp->Stride;
}
}
cairo_surface_mark_dirty(bmp->bmp);
}
void uiDrawBitmapDraw(uiDrawContext* c, uiDrawBitmap* bmp, uiRect* srcrect, uiRect* dstrect)
{
// TODO: rect
cairo_set_source_surface(c->cr, bmp->bmp, srcrect->X, srcrect->Y);
cairo_paint(c->cr);
}
void uiDrawFreeBitmap(uiDrawBitmap* bmp)
{
cairo_surface_destroy(bmp->bmp);
uiFree(bmp);
}
|