Debian Codegraph @

gsk/gskpathmeasure.c

raw symbols SCIP
1 /*
2 * Copyright © 2020 Benjamin Otte
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 *
17 * Authors: Benjamin Otte <[email protected]>
18 */
19
20 #include "config.h"cxx . . $ `<file>/build/config.h`/
21
22 #include "gskpathmeasure.h"cxx . . $ `<file>/gsk/gskpathmeasure.h`/
23
24 #include "gskpathbuilder.h"cxx . . $ `<file>/gsk/gskpathbuilder.h`/
25 #include "gskpathpointprivate.h"cxx . . $ `<file>/gsk/gskpathpointprivate.h`/
26 #include "gskcontourprivate.h"cxx . . $ `<file>/gsk/gskcontourprivate.h`/
27 #include "gskpathprivate.h"cxx . . $ `<file>/gsk/gskpathprivate.h`/
28
29 /**
30 * GskPathMeasure:
31 *
32 * Performs measurements on paths such as determining the length of the path.
33 *
34 * Many measuring operations require sampling the path length
35 * at intermediate points. Therefore, a `GskPathMeasure` has
36 * a tolerance that determines what precision is required
37 * for such approximations.
38 *
39 * A `GskPathMeasure` struct is a reference counted struct
40 * and should be treated as opaque.
41 *
42 * Since: 4.14
43 */
44
45 typedef struct _GskContourMeasurecxx . . $ _GskContourMeasure# GskContourMeasurecxx . . $ GskContourMeasure#;
46
47 struct _GskContourMeasurecxx . . $ _GskContourMeasure#
48 {
49 float lengthcxx . . $ _GskContourMeasure#length.;
50 gpointercxx . . $ gpointer# contour_datacxx . . $ _GskContourMeasure#contour_data.;
51 };
52
53 struct _GskPathMeasurecxx . . $ _GskPathMeasure#
54 {
55 /*< private >*/
56 guintcxx . . $ guint# ref_countcxx . . $ _GskPathMeasure#ref_count.;
57
58 GskPathcxx . . $ GskPath# *pathcxx . . $ _GskPathMeasure#path.;
59 float tolerancecxx . . $ _GskPathMeasure#tolerance.;
60
61 float lengthcxx . . $ _GskPathMeasure#length.;
62 gsizecxx . . $ gsize# n_contourscxx . . $ _GskPathMeasure#n_contours.;
63 GskContourMeasurecxx . . $ GskContourMeasure# measurescxx . . $ _GskPathMeasure#measures.[];
64 };
65
66 G_DEFINE_BOXED_TYPEcxx . . $ `<external>/9567554376676449156/gtype.h:2440:9`! (GskPathMeasure, gsk_path_measure,
67 gsk_path_measure_ref,
68 gsk_path_measure_unref)
69
70 /**
71 * gsk_path_measure_new:
72 * @path: the path to measure
73 *
74 * Creates a measure object for the given @path with the
75 * default tolerance.
76 *
77 * Returns: a new `GskPathMeasure` representing @path
78 *
79 * Since: 4.14
80 */
81 GskPathMeasurecxx . . $ GskPathMeasure# *
82 gsk_path_measure_newcxx . . $ gsk_path_measure_new(2e47e5a5a75cc263). (GskPathcxx . . $ GskPath# *path)
83 {
84 return gsk_path_measure_new_with_tolerancecxx . . $ gsk_path_measure_new_with_tolerance(13938316874bf0f8). (path, GSK_PATH_TOLERANCE_DEFAULTcxx . . $ `gsk/gskpathprivate.h:43:9`!);
85 }
86
87 /**
88 * gsk_path_measure_new_with_tolerance:
89 * @path: the path to measure
90 * @tolerance: the tolerance for measuring operations
91 *
92 * Creates a measure object for the given @path and @tolerance.
93 *
94 * Returns: a new `GskPathMeasure` representing @path
95 *
96 * Since: 4.14
97 */
98 GskPathMeasurecxx . . $ GskPathMeasure# *
99 gsk_path_measure_new_with_tolerancecxx . . $ gsk_path_measure_new_with_tolerance(13938316874bf0f8). (GskPathcxx . . $ GskPath# *path,
100 float tolerance)
101 {
102 GskPathMeasurecxx . . $ GskPathMeasure# *self;
103 gsizecxx . . $ gsize# i, n_contours;
104
105 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (path != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
106 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (tolerance > 0, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
107
108 n_contours = gsk_path_get_n_contourscxx . . $ gsk_path_get_n_contours(d2b684e3f07bcae9). (path);
109
110 self = g_malloc0cxx . . $ g_malloc0(bfcfa4d6b7f7ef64). (sizeof (GskPathMeasurecxx . . $ GskPathMeasure#) + n_contours * sizeof (GskContourMeasurecxx . . $ GskContourMeasure#));
111
112 self->ref_countcxx . . $ _GskPathMeasure#ref_count. = 1;
113 self->pathcxx . . $ _GskPathMeasure#path. = gsk_path_refcxx . . $ gsk_path_ref(bad2326ff2cc623a). (path);
114 self->tolerancecxx . . $ _GskPathMeasure#tolerance. = tolerance;
115 self->n_contourscxx . . $ _GskPathMeasure#n_contours. = n_contours;
116
117 for (i = 0; i < n_contours; i++)
118 {
119 self->measurescxx . . $ _GskPathMeasure#measures.[i].contour_datacxx . . $ _GskContourMeasure#contour_data. = gsk_contour_init_measurecxx . . $ gsk_contour_init_measure(33f326eeadef5a0b). (gsk_path_get_contourcxx . . $ gsk_path_get_contour(cde38ad1ee5b1f7f). (path, i),
120 self->tolerancecxx . . $ _GskPathMeasure#tolerance.,
121 &self->measurescxx . . $ _GskPathMeasure#measures.[i].lengthcxx . . $ _GskContourMeasure#length.);
122 self->lengthcxx . . $ _GskPathMeasure#length. += self->measurescxx . . $ _GskPathMeasure#measures.[i].lengthcxx . . $ _GskContourMeasure#length.;
123 }
124
125 return self;
126 }
127
128 /**
129 * gsk_path_measure_ref:
130 * @self: a path measure
131 *
132 * Increases the reference count of a `GskPathMeasure` by one.
133 *
134 * Returns: the passed in `GskPathMeasure`.
135 *
136 * Since: 4.14
137 */
138 GskPathMeasurecxx . . $ GskPathMeasure# *
139 gsk_path_measure_refcxx . . $ gsk_path_measure_ref(e88a6df33ddb4249). (GskPathMeasurecxx . . $ GskPathMeasure# *self)
140 {
141 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (self != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
142
143 self->ref_countcxx . . $ _GskPathMeasure#ref_count.++;
144
145 return self;
146 }
147
148 /**
149 * gsk_path_measure_unref:
150 * @self: a path measure
151 *
152 * Decreases the reference count of a `GskPathMeasure` by one.
153 *
154 * If the resulting reference count is zero, frees the object.
155 *
156 * Since: 4.14
157 */
158 void
159 gsk_path_measure_unrefcxx . . $ gsk_path_measure_unref(72f8802b581e39d7). (GskPathMeasurecxx . . $ GskPathMeasure# *self)
160 {
161 gsizecxx . . $ gsize# i;
162
163 g_return_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:652:9`! (self != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
164 g_return_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:652:9`! (self->ref_count > 0);
165
166 self->ref_countcxx . . $ _GskPathMeasure#ref_count.--;
167 if (self->ref_countcxx . . $ _GskPathMeasure#ref_count. > 0)
168 return;
169
170 for (i = 0; i < self->n_contourscxx . . $ _GskPathMeasure#n_contours.; i++)
171 {
172 gsk_contour_free_measurecxx . . $ gsk_contour_free_measure(c30e12d8e26f103b). (gsk_path_get_contourcxx . . $ gsk_path_get_contour(cde38ad1ee5b1f7f). (self->pathcxx . . $ _GskPathMeasure#path., i),
173 self->measurescxx . . $ _GskPathMeasure#measures.[i].contour_datacxx . . $ _GskContourMeasure#contour_data.);
174 }
175
176 gsk_path_unrefcxx . . $ gsk_path_unref(84da5b40fdd6197f). (self->pathcxx . . $ _GskPathMeasure#path.);
177 g_freecxx . . $ g_free(5d22bdacc48458e8). (self);
178 }
179
180 /**
181 * gsk_path_measure_get_path:
182 * @self: a path measure
183 *
184 * Returns the path that the measure was created for.
185 *
186 * Returns: (transfer none): the path of @self
187 *
188 * Since: 4.14
189 */
190 GskPathcxx . . $ GskPath# *
191 gsk_path_measure_get_pathcxx . . $ gsk_path_measure_get_path(7a07feefacb4cf8). (GskPathMeasurecxx . . $ GskPathMeasure# *self)
192 {
193 return self->pathcxx . . $ _GskPathMeasure#path.;
194 }
195
196 /**
197 * gsk_path_measure_get_tolerance:
198 * @self: a path measure
199 *
200 * Returns the tolerance that the measure was created with.
201 *
202 * Returns: the tolerance of @self
203 *
204 * Since: 4.14
205 */
206 float
207 gsk_path_measure_get_tolerancecxx . . $ gsk_path_measure_get_tolerance(542aedd04bfccbf1). (GskPathMeasurecxx . . $ GskPathMeasure# *self)
208 {
209 return self->tolerancecxx . . $ _GskPathMeasure#tolerance.;
210 }
211
212 /**
213 * gsk_path_measure_get_length:
214 * @self: a path measure
215 *
216 * Gets the length of the path being measured.
217 *
218 * The length is cached, so this function does not do any work.
219 *
220 * Returns: the length of the path measured by @self
221 *
222 * Since: 4.14
223 */
224 float
225 gsk_path_measure_get_lengthcxx . . $ gsk_path_measure_get_length(542aedd04bfccbf1). (GskPathMeasurecxx . . $ GskPathMeasure# *self)
226 {
227 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (self != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
228
229 return self->lengthcxx . . $ _GskPathMeasure#length.;
230 }
231
232 static float
233 gsk_path_measure_clamp_distancecxx . . $ gsk_path_measure_clamp_distance(fba5c8eae64f4e58). (GskPathMeasurecxx . . $ GskPathMeasure# *self,
234 float distance)
235 {
236 if (isnancxx . . $ `<external>/11263917310783833665/math.h:980:11`! (distance))
237 return 0;
238
239 return CLAMPcxx . . $ `<external>/12741050725406041920/gmacros.h:984:9`! (distance, 0, self->length);
240 }
241
242 /**
243 * gsk_path_measure_get_point:
244 * @self: a path measure
245 * @distance: the distance
246 * @result: (out caller-allocates): return location for the point
247 *
248 * Gets the point at the given distance into the path.
249 *
250 * An empty path has no points, so false is returned in that case.
251 *
252 * Returns: true if @result was set
253 *
254 * Since: 4.14
255 */
256 gbooleancxx . . $ gboolean#
257 gsk_path_measure_get_pointcxx . . $ gsk_path_measure_get_point(ea012416ff6952e5). (GskPathMeasurecxx . . $ GskPathMeasure# *self,
258 float distance,
259 GskPathPointcxx . . $ GskPathPoint# *result)
260 {
261 gsizecxx . . $ gsize# i;
262 const GskContourcxx . . $ GskContour# *contour;
263
264 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (self != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!);
265 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (result != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!);
266
267 if (self->n_contourscxx . . $ _GskPathMeasure#n_contours. == 0)
268 return FALSEcxx . . $ `<external>/12741050725406041920/gmacros.h:967:9`!;
269
270 distance = gsk_path_measure_clamp_distancecxx . . $ gsk_path_measure_clamp_distance(fba5c8eae64f4e58). (self, distance);
271
272 for (i = 0; i < self->n_contourscxx . . $ _GskPathMeasure#n_contours. - 1; i++)
273 {
274 if (distance < self->measurescxx . . $ _GskPathMeasure#measures.[i].lengthcxx . . $ _GskContourMeasure#length.)
275 break;
276
277 distance -= self->measurescxx . . $ _GskPathMeasure#measures.[i].lengthcxx . . $ _GskContourMeasure#length.;
278 }
279
280 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`! (0 <= i && i < self->n_contours);
281
282 distance = CLAMPcxx . . $ `<external>/12741050725406041920/gmacros.h:984:9`! (distance, 0, self->measures[i].length);
283
284 contour = gsk_path_get_contourcxx . . $ gsk_path_get_contour(cde38ad1ee5b1f7f). (self->pathcxx . . $ _GskPathMeasure#path., i);
285
286 gsk_contour_get_pointcxx . . $ gsk_contour_get_point(b4bcfeaf59aae4c). (contour, self->measurescxx . . $ _GskPathMeasure#measures.[i].contour_datacxx . . $ _GskContourMeasure#contour_data., distance, result);
287
288 g_assertcxx . . $ `<external>/15287032793271088244/gtestutils.h:275:9`! (0 <= result->t && result->t <= 1);
289
290 result->contourcxx . . $ _GskPathPoint#$anonymous_type_0#$anonymous_type_1#contour. = i;
291
292 return TRUEcxx . . $ `<external>/12741050725406041920/gmacros.h:971:9`!;
293 }
294
295 /**
296 * gsk_path_point_get_distance:
297 * @point: a point on the path
298 * @measure: a path measure for the path
299 *
300 * Returns the distance from the beginning of the path
301 * to the point.
302 *
303 * Returns: the distance of @point
304 *
305 * Since: 4.14
306 */
307 float
308 gsk_path_point_get_distancecxx . . $ gsk_path_point_get_distance(a5bfe9536088c507). (const GskPathPointcxx . . $ GskPathPoint# *point,
309 GskPathMeasurecxx . . $ GskPathMeasure# *measure)
310 {
311 float contour_offset = 0;
312
313 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (measure != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0);
314 g_return_val_if_failcxx . . $ `<external>/5169755362661750954/gmessages.h:665:9`! (gsk_path_point_valid (point, measure->path), 0);
315
316 for (gsizecxx . . $ gsize# i = 0; i < measure->n_contourscxx . . $ _GskPathMeasure#n_contours.; i++)
317 {
318 if (i == point->contourcxx . . $ _GskPathPoint#$anonymous_type_0#$anonymous_type_1#contour.)
319 return contour_offset + gsk_contour_get_distancecxx . . $ gsk_contour_get_distance(ab9d4a7a06a693b8). (gsk_path_get_contourcxx . . $ gsk_path_get_contour(cde38ad1ee5b1f7f). (measure->pathcxx . . $ _GskPathMeasure#path., i),
320 point,
321 measure->measurescxx . . $ _GskPathMeasure#measures.[i].contour_datacxx . . $ _GskContourMeasure#contour_data.);
322
323 contour_offset += measure->measurescxx . . $ _GskPathMeasure#measures.[i].lengthcxx . . $ _GskContourMeasure#length.;
324 }
325
326 g_return_val_if_reachedcxx . . $ `<external>/5169755362661750954/gmessages.h:689:9`! (0);
327 }