1 | /* |
---|
2 | rsvg-bpath-util.c: Data structure and convenience functions for creating bezier paths. |
---|
3 | |
---|
4 | Copyright (C) 2000 Eazel, Inc. |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or |
---|
7 | modify it under the terms of the GNU General Public License as |
---|
8 | published by the Free Software Foundation; either version 2 of the |
---|
9 | License, or (at your option) any later version. |
---|
10 | |
---|
11 | This program is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
14 | General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public |
---|
17 | License along with this program; if not, write to the |
---|
18 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
19 | Boston, MA 02111-1307, USA. |
---|
20 | |
---|
21 | Author: Raph Levien <raph@artofcode.com> |
---|
22 | */ |
---|
23 | |
---|
24 | #include <glib.h> |
---|
25 | #include <math.h> |
---|
26 | #include <string.h> |
---|
27 | #include "rsvg-bpath-util.h" |
---|
28 | |
---|
29 | /* This is adapted from gnome-canvas-bpath-util in libgnomeprint |
---|
30 | (originally developed as part of Gill). */ |
---|
31 | |
---|
32 | RsvgBpathDef * |
---|
33 | rsvg_bpath_def_new (void) |
---|
34 | { |
---|
35 | RsvgBpathDef *bpd; |
---|
36 | |
---|
37 | bpd = g_new (RsvgBpathDef, 1); |
---|
38 | bpd->n_bpath = 0; |
---|
39 | bpd->n_bpath_max = 16; |
---|
40 | bpd->moveto_idx = -1; |
---|
41 | bpd->bpath = g_new (ArtBpath, bpd->n_bpath_max); |
---|
42 | bpd->ref_count = 1; |
---|
43 | |
---|
44 | return bpd; |
---|
45 | } |
---|
46 | |
---|
47 | RsvgBpathDef * |
---|
48 | rsvg_bpath_def_new_from (ArtBpath *path) |
---|
49 | { |
---|
50 | RsvgBpathDef *bpd; |
---|
51 | int i; |
---|
52 | |
---|
53 | g_return_val_if_fail (path != NULL, NULL); |
---|
54 | |
---|
55 | for (i = 0; path[i].code != ART_END; i++) |
---|
56 | ; |
---|
57 | if (i <= 0) |
---|
58 | return rsvg_bpath_def_new (); |
---|
59 | |
---|
60 | bpd = g_new (RsvgBpathDef, 1); |
---|
61 | |
---|
62 | bpd->n_bpath = i; |
---|
63 | bpd->n_bpath_max = i; |
---|
64 | bpd->moveto_idx = -1; |
---|
65 | bpd->ref_count = 1; |
---|
66 | bpd->bpath = g_new (ArtBpath, i); |
---|
67 | |
---|
68 | memcpy (bpd->bpath, path, i * sizeof (ArtBpath)); |
---|
69 | return bpd; |
---|
70 | } |
---|
71 | |
---|
72 | RsvgBpathDef * |
---|
73 | rsvg_bpath_def_ref (RsvgBpathDef *bpd) |
---|
74 | { |
---|
75 | g_return_val_if_fail (bpd != NULL, NULL); |
---|
76 | |
---|
77 | bpd->ref_count += 1; |
---|
78 | return bpd; |
---|
79 | } |
---|
80 | |
---|
81 | void |
---|
82 | rsvg_bpath_def_free (RsvgBpathDef *bpd) |
---|
83 | { |
---|
84 | g_return_if_fail (bpd != NULL); |
---|
85 | |
---|
86 | bpd->ref_count -= 1; |
---|
87 | if (bpd->ref_count == 0) |
---|
88 | { |
---|
89 | g_free (bpd->bpath); |
---|
90 | g_free (bpd); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | void |
---|
95 | rsvg_bpath_def_moveto (RsvgBpathDef *bpd, double x, double y) |
---|
96 | { |
---|
97 | ArtBpath *bpath; |
---|
98 | int n_bpath; |
---|
99 | |
---|
100 | g_return_if_fail (bpd != NULL); |
---|
101 | |
---|
102 | n_bpath = bpd->n_bpath++; |
---|
103 | |
---|
104 | if (n_bpath == bpd->n_bpath_max) |
---|
105 | bpd->bpath = g_realloc (bpd->bpath, |
---|
106 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
107 | bpath = bpd->bpath; |
---|
108 | bpath[n_bpath].code = ART_MOVETO_OPEN; |
---|
109 | bpath[n_bpath].x3 = x; |
---|
110 | bpath[n_bpath].y3 = y; |
---|
111 | bpd->moveto_idx = n_bpath; |
---|
112 | } |
---|
113 | |
---|
114 | void |
---|
115 | rsvg_bpath_def_lineto (RsvgBpathDef *bpd, double x, double y) |
---|
116 | { |
---|
117 | ArtBpath *bpath; |
---|
118 | int n_bpath; |
---|
119 | |
---|
120 | g_return_if_fail (bpd != NULL); |
---|
121 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
122 | |
---|
123 | n_bpath = bpd->n_bpath++; |
---|
124 | |
---|
125 | if (n_bpath == bpd->n_bpath_max) |
---|
126 | bpd->bpath = g_realloc (bpd->bpath, |
---|
127 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
128 | bpath = bpd->bpath; |
---|
129 | bpath[n_bpath].code = ART_LINETO; |
---|
130 | bpath[n_bpath].x3 = x; |
---|
131 | bpath[n_bpath].y3 = y; |
---|
132 | } |
---|
133 | |
---|
134 | void |
---|
135 | rsvg_bpath_def_curveto (RsvgBpathDef *bpd, double x1, double y1, double x2, double y2, double x3, double y3) |
---|
136 | { |
---|
137 | ArtBpath *bpath; |
---|
138 | int n_bpath; |
---|
139 | |
---|
140 | g_return_if_fail (bpd != NULL); |
---|
141 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
142 | |
---|
143 | n_bpath = bpd->n_bpath++; |
---|
144 | |
---|
145 | if (n_bpath == bpd->n_bpath_max) |
---|
146 | bpd->bpath = g_realloc (bpd->bpath, |
---|
147 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
148 | bpath = bpd->bpath; |
---|
149 | bpath[n_bpath].code = ART_CURVETO; |
---|
150 | bpath[n_bpath].x1 = x1; |
---|
151 | bpath[n_bpath].y1 = y1; |
---|
152 | bpath[n_bpath].x2 = x2; |
---|
153 | bpath[n_bpath].y2 = y2; |
---|
154 | bpath[n_bpath].x3 = x3; |
---|
155 | bpath[n_bpath].y3 = y3; |
---|
156 | } |
---|
157 | |
---|
158 | void |
---|
159 | rsvg_bpath_def_closepath (RsvgBpathDef *bpd) |
---|
160 | { |
---|
161 | ArtBpath *bpath; |
---|
162 | int n_bpath; |
---|
163 | |
---|
164 | g_return_if_fail (bpd != NULL); |
---|
165 | g_return_if_fail (bpd->moveto_idx >= 0); |
---|
166 | g_return_if_fail (bpd->n_bpath > 0); |
---|
167 | |
---|
168 | bpath = bpd->bpath; |
---|
169 | n_bpath = bpd->n_bpath; |
---|
170 | |
---|
171 | /* Add closing vector if we need it. */ |
---|
172 | if (bpath[n_bpath - 1].x3 != bpath[bpd->moveto_idx].x3 || |
---|
173 | bpath[n_bpath - 1].y3 != bpath[bpd->moveto_idx].y3) |
---|
174 | { |
---|
175 | rsvg_bpath_def_lineto (bpd, bpath[bpd->moveto_idx].x3, |
---|
176 | bpath[bpd->moveto_idx].y3); |
---|
177 | bpath = bpd->bpath; |
---|
178 | } |
---|
179 | bpath[bpd->moveto_idx].code = ART_MOVETO; |
---|
180 | bpd->moveto_idx = -1; |
---|
181 | } |
---|
182 | |
---|
183 | void |
---|
184 | rsvg_bpath_def_art_finish (RsvgBpathDef *bpd) |
---|
185 | { |
---|
186 | int n_bpath; |
---|
187 | |
---|
188 | g_return_if_fail (bpd != NULL); |
---|
189 | |
---|
190 | n_bpath = bpd->n_bpath++; |
---|
191 | |
---|
192 | if (n_bpath == bpd->n_bpath_max) |
---|
193 | bpd->bpath = g_realloc (bpd->bpath, |
---|
194 | (bpd->n_bpath_max <<= 1) * sizeof (ArtBpath)); |
---|
195 | bpd->bpath[n_bpath].code = ART_END; |
---|
196 | } |
---|