Mercurial > MadButterfly
annotate src/shape_rect.c @ 35:581a03196093
Support rectangle tag of SVG.
- Change rectangle in X_main to sh_rect_t.
author | Thinker K.F. Li <thinker@branda.to> |
---|---|
date | Wed, 06 Aug 2008 02:11:53 +0800 |
parents | 07c523c799f4 |
children | 51a20f240ce3 |
rev | line source |
---|---|
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
1 #include <stdio.h> |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
2 #include <stdlib.h> |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
3 #include <string.h> |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
4 #include "mb_types.h" |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
5 #include "shapes.h" |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
6 |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
7 typedef struct _sh_rect { |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
8 shape_t shape; |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
9 co_aix x, y; |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
10 co_aix w, h; |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
11 co_aix rx, ry; |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
12 co_aix poses[12][2]; |
33
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
13 } sh_rect_t; |
d82749f77108
Fix bug of demo and remove *_fill() and *_stroke().
Thinker K.F. Li <thinker@branda.to>
parents:
diff
changeset
|
14 |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
15 shape_t *sh_rect_new(co_aix x, co_aix y, co_aix w, co_aix h, |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
16 co_aix rx, co_aix ry) { |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
17 sh_rect_t *rect; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
18 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
19 rect = (sh_rect_t *)malloc(sizeof(sh_rect_t)); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
20 if(rect == NULL) |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
21 return NULL; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
22 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
23 memset(rect, 0, sizeof(sh_rect_t)); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
24 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
25 rect->shape.sh_type = SHT_RECT; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
26 rect->x = x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
27 rect->y = y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
28 rect->w = w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
29 rect->h = h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
30 rect->rx = rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
31 rect->ry = ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
32 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
33 return (shape_t *)rect; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
34 } |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
35 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
36 void sh_rect_free(shape_t *shape) { |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
37 free(shape); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
38 } |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
39 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
40 void sh_rect_transform(shape_t *shape) { |
34
07c523c799f4
Fix bug of relative path command
Thinker K.F. Li <thinker@branda.to>
parents:
33
diff
changeset
|
41 sh_rect_t *rect = (sh_rect_t *)shape; |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
42 co_aix x, y, w, h, rx, ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
43 co_aix (*poses)[2]; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
44 co_aix width; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
45 area_t *area; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
46 int i; |
34
07c523c799f4
Fix bug of relative path command
Thinker K.F. Li <thinker@branda.to>
parents:
33
diff
changeset
|
47 |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
48 x = rect->x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
49 y = rect->y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
50 w = rect->w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
51 h = rect->h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
52 rx = rect->rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
53 ry = rect->ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
54 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
55 poses = rect->poses; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
56 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
57 poses[0][0] = x + w - rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
58 poses[0][1] = y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
59 poses[1][0] = x + w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
60 poses[1][1] = y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
61 poses[2][0] = x + w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
62 poses[2][1] = y + ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
63 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
64 poses[3][0] = x + w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
65 poses[3][1] = y + h - ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
66 poses[4][0] = x + w; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
67 poses[4][1] = y + h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
68 poses[5][0] = x + w - rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
69 poses[5][1] = y + h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
70 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
71 poses[6][0] = x + rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
72 poses[6][1] = y + h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
73 poses[7][0] = x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
74 poses[7][1] = y + h; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
75 poses[8][0] = x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
76 poses[8][1] = y + h - ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
77 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
78 poses[9][0] = x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
79 poses[9][1] = y + ry; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
80 poses[10][0] = x; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
81 poses[10][1] = y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
82 poses[11][0] = x + rx; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
83 poses[11][1] = y; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
84 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
85 for(i = 0; i < 12; i++) |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
86 coord_trans_pos(shape->coord, &poses[i][0], &poses[i][1]); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
87 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
88 geo_from_positions(shape->geo, 12, poses); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
89 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
90 if(shape->stroke) { |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
91 area = shape->geo->cur_area; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
92 width = shape->stroke_width; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
93 area->x -= width / 2 + 1; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
94 area->y -= width / 2 + 1; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
95 area->w += width + 2; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
96 area->h += width + 2; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
97 } |
34
07c523c799f4
Fix bug of relative path command
Thinker K.F. Li <thinker@branda.to>
parents:
33
diff
changeset
|
98 } |
35
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
99 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
100 void sh_rect_draw(shape_t *shape, cairo_t *cr) { |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
101 sh_rect_t *rect = (sh_rect_t *)shape; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
102 int i; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
103 co_aix (*poses)[2]; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
104 |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
105 poses = rect->poses; |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
106 cairo_move_to(cr, poses[11][0], poses[11][1]); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
107 for(i = 0; i < 12; i += 3) { |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
108 cairo_line_to(cr, poses[i][0], poses[i][1]); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
109 cairo_curve_to(cr, |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
110 poses[i + 1][0], poses[i + 1][1], |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
111 poses[i + 1][0], poses[i + 1][1], |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
112 poses[i + 2][0], poses[i + 2][1]); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
113 } |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
114 cairo_close_path(cr); |
581a03196093
Support rectangle tag of SVG.
Thinker K.F. Li <thinker@branda.to>
parents:
34
diff
changeset
|
115 } |