summaryrefslogtreecommitdiff
path: root/gsk/gskpathmeasure.c
blob: d9afeffcb444601345255a722df22e000815be4c (plain)
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
 * Copyright © 2020 Benjamin Otte
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Benjamin Otte <otte@gnome.org>
 */

#include "config.h"

#include "gskpathmeasure.h"

#include "gskpathbuilder.h"
#include "gskpathprivate.h"

/**
 * SECTION:gskpathmeasure
 * @Title: PathMeasure
 * @Short_description: Measuring operations on paths
 * @See_also: #GskPath
 *
 * #GskPathMeasure is an object that allows measuring operations on #GskPaths.
 * These operations are useful when implementing animations.
 */

typedef struct _GskContourMeasure GskContourMeasure;

struct _GskContourMeasure
{
  float length;
  gpointer contour_data;
};

struct _GskPathMeasure
{
  /*< private >*/
  guint ref_count;

  GskPath *path;
  float tolerance;

  gsize first;
  gsize last;

  float length;
  gsize n_contours;
  GskContourMeasure measures[];
};

/**
 * GskPathMeasure:
 *
 * A #GskPathMeasure struct is a reference counted struct
 * and should be treated as opaque.
 */

G_DEFINE_BOXED_TYPE (GskPathMeasure, gsk_path_measure,
                     gsk_path_measure_ref,
                     gsk_path_measure_unref)

/**
 * gsk_path_measure_new:
 * @path: the path to measure
 *
 * Creates a measure object for the given @path.
 *
 * Returns: a new #GskPathMeasure representing @path
 **/
GskPathMeasure *
gsk_path_measure_new (GskPath *path)
{
  return gsk_path_measure_new_with_tolerance (path, GSK_PATH_TOLERANCE_DEFAULT);
}

/**
 * gsk_path_measure_new_with_tolerance:
 * @path: the path to measure
 * @tolerance: the tolerance for measuring operations
 *
 * Creates a measure object for the given @path and @tolerance.
 *
 * Returns: a new #GskPathMeasure representing @path
 **/
GskPathMeasure *
gsk_path_measure_new_with_tolerance (GskPath *path,
                                     float    tolerance)
{
  GskPathMeasure *self;
  gsize i, n_contours;

  g_return_val_if_fail (path != NULL, NULL);
  g_return_val_if_fail (tolerance > 0, NULL);

  n_contours = gsk_path_get_n_contours (path);

  self = g_malloc0 (sizeof (GskPathMeasure) + n_contours * sizeof (GskContourMeasure));

  self->ref_count = 1;
  self->path = gsk_path_ref (path);
  self->tolerance = tolerance;
  self->n_contours = n_contours;
  self->first = 0;
  self->last = n_contours;

  for (i = 0; i < n_contours; i++)
    {
      self->measures[i].contour_data = gsk_contour_init_measure (gsk_path_get_contour (path, i),
                                                                 self->tolerance,
                                                                 &self->measures[i].length);
      self->length += self->measures[i].length;
    }

  return self;
}

/**
 * gsk_path_measure_ref:
 * @self: a #GskPathMeasure
 *
 * Increases the reference count of a #GskPathMeasure by one.
 *
 * Returns: the passed in #GskPathMeasure.
 **/
GskPathMeasure *
gsk_path_measure_ref (GskPathMeasure *self)
{
  g_return_val_if_fail (self != NULL, NULL);

  self->ref_count++;

  return self;
}

/**
 * gsk_path_measure_unref:
 * @self: a #GskPathMeasure
 *
 * Decreases the reference count of a #GskPathMeasure by one.
 * If the resulting reference count is zero, frees the path_measure.
 **/
void
gsk_path_measure_unref (GskPathMeasure *self)
{
  gsize i;

  g_return_if_fail (self != NULL);
  g_return_if_fail (self->ref_count > 0);

  self->ref_count--;
  if (self->ref_count > 0)
    return;

  for (i = 0; i < self->n_contours; i++)
    {
      gsk_contour_free_measure (gsk_path_get_contour (self->path, i),
                                self->measures[i].contour_data);
    }

  gsk_path_unref (self->path);
  g_free (self);
}

/**
 * gsk_path_measure_get_path:
 * @self: a #GskPathMeasure
 *
 * Returns the path that the measure was created for.
 *
 * Returns: (transfer none): the path of @self
 */
GskPath *
gsk_path_measure_get_path (GskPathMeasure *self)
{
  g_return_val_if_fail (self != NULL, NULL);

  return self->path;
}

/**
 * gsk_path_measure_get_tolerance:
 * @self: a #GskPathMeasure
 *
 * Returns the tolerance that the measure was created with.
 *
 * Returns: the tolerance of @self
 */
float
gsk_path_measure_get_tolerance (GskPathMeasure *self)
{
  g_return_val_if_fail (self != NULL, 0.f);

  return self->tolerance;
}

/**
 * gsk_path_measure_get_n_contours:
 * @self: a #GskPathMeasure
 *
 * Returns the number of contours in the path being measured.
 *
 * The returned value is independent of whether @self if restricted
 * or not.
 *
 * Returns: The number of contours
 **/
gsize
gsk_path_measure_get_n_contours (GskPathMeasure *self)
{
  g_return_val_if_fail (self != NULL, 0);

  return self->n_contours;
}

/**
 * gsk_path_measure_restrict_to_contour:
 * @self: a #GskPathMeasure
 * @contour: contour to restrict to or (gsize) -1 for using the
 *     whole path
 *
 * Restricts all functions on the path to just the given @contour.
 *
 * If @contour >= gsk_path_measure_get_n_contours() - so in
 * particular when it is set to -1 - the whole path will be used.
 **/
void
gsk_path_measure_restrict_to_contour (GskPathMeasure *self,
                                      gsize           contour)
{
  if (contour >= self->n_contours)
    {
      /* use the whole path */
      self->first = 0;
      self->last = self->n_contours;
    }
  else
    {
      /* use just one contour */
      self->first = contour;
      self->last = contour + 1;
    }

  self->length = 0;
  for (gsize i = self->first; i < self->last; i++)
    {
      self->length += self->measures[i].length;
    }
}

/**
 * gsk_path_measure_get_length:
 * @self: a #GskPathMeasure
 *
 * Gets the length of the path being measured.
 *
 * The length is cached, so this function does not do any work.
 *
 * Returns: The length of the path measured by @self
 **/
float
gsk_path_measure_get_length (GskPathMeasure *self)
{
  g_return_val_if_fail (self != NULL, 0);

  return self->length;
}

static float
gsk_path_measure_clamp_distance (GskPathMeasure *self,
                                 float           distance)
{
  if (isnan (distance))
    return 0;

  return CLAMP (distance, 0, self->length);
}

/**
 * gsk_path_measure_get_point:
 * @self: a #GskPathMeasure
 * @distance: distance into the path
 * @pos: (out) (optional) (caller-allocates): The coordinates
 *    of the position at @distance
 * @tangent: (out) (optional) (caller-allocates): The tangent
 *    to the position at @distance
 *
 * Calculates the coordinates and tangent of the point @distance
 * units into the path. The value will be clamped to the length
 * of the path.
 *
 * If the point is a discontinuous edge in the path, the returned
 * point and tangent will describe the line starting at that point
 * going forward.
 *
 * If @self describes an empty path, the returned point will be 
 * set to `(0, 0)` and the tangent will be the x axis or `(1, 0)`.
 **/
void
gsk_path_measure_get_point (GskPathMeasure   *self,
                            float             distance,
                            graphene_point_t *pos,
                            graphene_vec2_t  *tangent)
{
  gsize i;

  g_return_if_fail (self != NULL);

  if (pos == NULL && tangent == NULL)
    return;

  distance = gsk_path_measure_clamp_distance (self, distance);

  for (i = self->first; i < self->last; i++)
    {
      if (distance < self->measures[i].length)
        break;

      distance -= self->measures[i].length;
    }

  /* weird corner cases */
  if (i == self->last)
    {
      /* the empty path goes here */
      if (self->first == self->last)
        {
          if (pos)
            graphene_point_init (pos, 0.f, 0.f);
          if (tangent)
            graphene_vec2_init (tangent, 1.f, 0.f);
          return;
        }
      /* rounding errors can make this happen */
      i = self->last - 1;
      distance = self->measures[i].length;
    }

  gsk_contour_get_point (gsk_path_get_contour (self->path, i),
                         self->measures[i].contour_data,
                         distance,
                         pos,
                         tangent);
}

/**
 * gsk_path_measure_get_closest_point:
 * @self: a #GskPathMeasure
 * @point: the point to fond the closest point to
 * @out_pos: (out) (optional) (caller-allocates): return location
 *    for the closest point
 *
 * Gets the point on the path that is closest to @point.
 *
 * If the path being measured is empty, return 0 and set
 * @out_pos to (0, 0).
 *
 * This is a simpler and slower version of
 * gsk_path_measure_get_closest_point_full(). Use that one if you
 * need more control.
 *
 * Returns: The offset into the path of the closest point
 **/
float
gsk_path_measure_get_closest_point (GskPathMeasure         *self,
                                    const graphene_point_t *point,
                                    graphene_point_t       *out_pos)
{
  float result;

  g_return_val_if_fail (self != NULL, 0.0f);

  if (gsk_path_measure_get_closest_point_full (self, 
                                               point,
                                               INFINITY,
                                               &result,
                                               out_pos,
                                               NULL,
                                               NULL))
    return result;

  if (out_pos)
    *out_pos = GRAPHENE_POINT_INIT (0, 0);

  return 0;

}

/**
 * gsk_path_measure_get_closest_point_full:
 * @self: a #GskPathMeasure
 * @point: the point to fond the closest point to
 * @threshold: The maximum allowed distance between the path and @point.
 *     Use INFINITY to look for any point.
 * @out_distance: (out) (optional) (caller-allocates): The 
 *     distance between the found closest point on the path and the given
 *     @point.
 * @out_pos: (out) (optional) (caller-allocates): return location
 *     for the closest point
 * @out_offset: (out) (optional) (caller-allocates): The offset into
 *     the path of the found point
 * @out_tangent: (out) (optional) (caller-allocates): return location for
 *     the tangent at the closest point
 *
 * Gets the point on the path that is closest to @point. If no point on
 * path is closer to @point than @threshold, return %FALSE.
 *
 * Returns: %TRUE if a point was found, %FALSE otherwise.
 **/
gboolean
gsk_path_measure_get_closest_point_full (GskPathMeasure         *self,
                                         const graphene_point_t *point,
                                         float                   threshold,
                                         float                  *out_distance,
                                         graphene_point_t       *out_pos,
                                         float                  *out_offset,
                                         graphene_vec2_t        *out_tangent)
{
  gboolean result;
  gsize i;
  float distance, length;

  g_return_val_if_fail (self != NULL, FALSE);
  g_return_val_if_fail (point != NULL, FALSE);

  result = FALSE;
  length = 0;

  for (i = self->first; i < self->last; i++)
    {
      if (gsk_contour_get_closest_point (gsk_path_get_contour (self->path, i),
                                         self->measures[i].contour_data,
                                         self->tolerance,
                                         point,
                                         threshold,
                                         &distance,
                                         out_pos,
                                         out_offset,
                                         out_tangent))
        {
          result = TRUE;
          if (out_offset)
            *out_offset += length;

          if (distance < self->tolerance)
            break;
          threshold = distance - self->tolerance;
        }

      length += self->measures[i].length;
    }

  if (result && out_distance)
    *out_distance = distance;

  return result;
}

/**
 * gsk_path_measure_in_fill:
 * @self: a #GskPathMeasure
 * @point: the point to test
 * @fill_rule: the fill rule to follow
 *
 * Returns whether the given point is inside the area that would be
 * affected if the path of @self was filled according to @fill_rule.
 *
 * Returns: %TRUE if @point is inside
 */
gboolean
gsk_path_measure_in_fill (GskPathMeasure   *self,
                          graphene_point_t *point,
                          GskFillRule       fill_rule)
{
  int winding = 0;
  gboolean on_edge = FALSE;
  int i;

  for (i = self->first; i < self->last; i++)
    {
      winding += gsk_contour_get_winding (gsk_path_get_contour (self->path, i),
                                          self->measures[i].contour_data,
                                          point,
                                          &on_edge);
      if (on_edge)
        return TRUE;
    }

  switch (fill_rule)
    {
    case GSK_FILL_RULE_EVEN_ODD:
      return winding & 1;
    case GSK_FILL_RULE_WINDING:
      return winding != 0;
    default:
      g_assert_not_reached ();
    }
}


/**
 * gsk_path_builder_add_segment:
 * @self: a #GskPathBuilder 
 * @measure: the #GskPathMeasure to take the segment to
 * @start: start distance into the path
 * @end: end distance into the path
 *
 * Adds to @self the segment of @measure inbetween @start and @end.
 *
 * The distances are given relative to the length of @measure's path,
 * from 0 for the beginning of the path to
 * gsk_path_measure_get_length() for the end of the path. The values
 * will be clamped to that range.
 *
 * If @start >= @end after clamping, no path will be added.
 **/
void
gsk_path_builder_add_segment (GskPathBuilder *self,
                              GskPathMeasure *measure,
                              float           start,
                              float           end)
{
  gsize i;

  g_return_if_fail (self != NULL);
  g_return_if_fail (measure != NULL);

  start = gsk_path_measure_clamp_distance (measure, start);
  end = gsk_path_measure_clamp_distance (measure, end);
  if (start >= end)
    return;

  for (i = measure->first; i < measure->last; i++)
    {
      if (measure->measures[i].length < start)
        {
          start -= measure->measures[i].length;
          end -= measure->measures[i].length;
        }
      else if (start > 0 || end < measure->measures[i].length)
        {
          float len = MIN (end, measure->measures[i].length);
          gsk_contour_add_segment (gsk_path_get_contour (measure->path, i),
                                   self,
                                   measure->measures[i].contour_data,
                                   start,
                                   len);
          end -= len;
          start = 0;
          if (end <= 0)
            break;
        }
      else
        {
          end -= measure->measures[i].length;
          gsk_path_builder_add_contour (self, gsk_contour_dup (gsk_path_get_contour (measure->path, i)));
        }
    }
}