gsk: Add GskPathMeasure
An object to do measuring operations on paths - determining their length, cutting off subpaths, things like that.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
#include <gsk/gskenums.h>
|
||||
#include <gsk/gskglshader.h>
|
||||
#include <gsk/gskpath.h>
|
||||
#include <gsk/gskpathmeasure.h>
|
||||
#include <gsk/gskrenderer.h>
|
||||
#include <gsk/gskrendernode.h>
|
||||
#include <gsk/gskroundedrect.h>
|
||||
|
||||
117
gsk/gskpath.c
117
gsk/gskpath.c
@@ -19,7 +19,7 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gskpath.h"
|
||||
#include "gskpathprivate.h"
|
||||
|
||||
/**
|
||||
* SECTION:gskpath
|
||||
@@ -55,6 +55,10 @@ struct _GskContourClass
|
||||
cairo_t *cr);
|
||||
gboolean (* get_bounds) (const GskContour *contour,
|
||||
graphene_rect_t *bounds);
|
||||
gpointer (* init_measure) (const GskContour *contour,
|
||||
float *out_length);
|
||||
void (* free_measure) (const GskContour *contour,
|
||||
gpointer measure_data);
|
||||
};
|
||||
|
||||
struct _GskPath
|
||||
@@ -155,6 +159,23 @@ gsk_rect_contour_get_bounds (const GskContour *contour,
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
gsk_rect_contour_init_measure (const GskContour *contour,
|
||||
float *out_length)
|
||||
{
|
||||
const GskRectContour *self = (const GskRectContour *) contour;
|
||||
|
||||
*out_length = 2 * ABS (self->width) + 2 * ABS (self->height);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
gsk_rect_contour_free_measure (const GskContour *contour,
|
||||
gpointer data)
|
||||
{
|
||||
}
|
||||
|
||||
static const GskContourClass GSK_RECT_CONTOUR_CLASS =
|
||||
{
|
||||
sizeof (GskRectContour),
|
||||
@@ -162,7 +183,9 @@ static const GskContourClass GSK_RECT_CONTOUR_CLASS =
|
||||
gsk_contour_get_size_default,
|
||||
gsk_rect_contour_print,
|
||||
gsk_rect_contour_to_cairo,
|
||||
gsk_rect_contour_get_bounds
|
||||
gsk_rect_contour_get_bounds,
|
||||
gsk_rect_contour_init_measure,
|
||||
gsk_rect_contour_free_measure
|
||||
};
|
||||
|
||||
static void
|
||||
@@ -235,7 +258,7 @@ gsk_standard_contour_print (const GskContour *contour,
|
||||
|
||||
for (i = 0; i < self->n_ops; i ++)
|
||||
{
|
||||
graphene_point_t *pt = &self->points [self->ops[i].point];
|
||||
graphene_point_t *pt = &self->points[self->ops[i].point];
|
||||
|
||||
switch (self->ops[i].op)
|
||||
{
|
||||
@@ -280,7 +303,7 @@ gsk_standard_contour_to_cairo (const GskContour *contour,
|
||||
|
||||
for (i = 0; i < self->n_ops; i ++)
|
||||
{
|
||||
graphene_point_t *pt = &self->points [self->ops[i].point];
|
||||
graphene_point_t *pt = &self->points[self->ops[i].point];
|
||||
|
||||
switch (self->ops[i].op)
|
||||
{
|
||||
@@ -354,6 +377,52 @@ gsk_standard_contour_get_bounds (const GskContour *contour,
|
||||
return bounds->size.width > 0 && bounds->size.height > 0;
|
||||
}
|
||||
|
||||
static gpointer
|
||||
gsk_standard_contour_init_measure (const GskContour *contour,
|
||||
float *out_length)
|
||||
{
|
||||
const GskStandardContour *self = (const GskStandardContour *) contour;
|
||||
gsize i;
|
||||
float length;
|
||||
|
||||
length = 0;
|
||||
|
||||
for (i = 1; i < self->n_ops; i ++)
|
||||
{
|
||||
graphene_point_t *pt = &self->points[self->ops[i].point];
|
||||
|
||||
switch (self->ops[i].op)
|
||||
{
|
||||
case GSK_PATH_MOVE:
|
||||
break;
|
||||
|
||||
case GSK_PATH_CLOSE:
|
||||
case GSK_PATH_LINE:
|
||||
length += graphene_point_distance (&pt[0], &pt[1], NULL, NULL);
|
||||
break;
|
||||
|
||||
case GSK_PATH_CURVE:
|
||||
g_warning ("i'm not fat!");
|
||||
length += graphene_point_distance (&pt[0], &pt[3], NULL, NULL);
|
||||
break;
|
||||
|
||||
default:
|
||||
g_assert_not_reached();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
*out_length = length;
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void
|
||||
gsk_standard_contour_free_measure (const GskContour *contour,
|
||||
gpointer data)
|
||||
{
|
||||
}
|
||||
|
||||
static const GskContourClass GSK_STANDARD_CONTOUR_CLASS =
|
||||
{
|
||||
sizeof (GskStandardContour),
|
||||
@@ -361,7 +430,9 @@ static const GskContourClass GSK_STANDARD_CONTOUR_CLASS =
|
||||
gsk_standard_contour_get_size,
|
||||
gsk_standard_contour_print,
|
||||
gsk_standard_contour_to_cairo,
|
||||
gsk_standard_contour_get_bounds
|
||||
gsk_standard_contour_get_bounds,
|
||||
gsk_standard_contour_init_measure,
|
||||
gsk_standard_contour_free_measure
|
||||
};
|
||||
|
||||
/* You must ensure the contour has enough size allocated,
|
||||
@@ -385,6 +456,28 @@ gsk_standard_contour_init (GskContour *contour,
|
||||
memcpy (self->points, points, sizeof (graphene_point_t) * n_points);
|
||||
}
|
||||
|
||||
/* CONTOUR */
|
||||
|
||||
gpointer
|
||||
gsk_contour_init_measure (GskPath *path,
|
||||
gsize i,
|
||||
float *out_length)
|
||||
{
|
||||
GskContour *self = path->contours[i];
|
||||
|
||||
return self->klass->init_measure (self, out_length);
|
||||
}
|
||||
|
||||
void
|
||||
gsk_contour_free_measure (GskPath *path,
|
||||
gsize i,
|
||||
gpointer data)
|
||||
{
|
||||
GskContour *self = path->contours[i];
|
||||
|
||||
self->klass->free_measure (self, data);
|
||||
}
|
||||
|
||||
/* PATH */
|
||||
|
||||
static GskPath *
|
||||
@@ -578,6 +671,20 @@ gsk_path_to_cairo (GskPath *self,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* gsk_path_get_n_contours:
|
||||
* @path: a #GskPath
|
||||
*
|
||||
* Gets the nnumber of contours @path is composed out of.
|
||||
*
|
||||
* Returns: the number of contours in @path
|
||||
**/
|
||||
gsize
|
||||
gsk_path_get_n_contours (GskPath *path)
|
||||
{
|
||||
return path->n_contours;
|
||||
}
|
||||
|
||||
/**
|
||||
* gsk_path_is_empty:
|
||||
* @self: a #GskPath
|
||||
|
||||
163
gsk/gskpathmeasure.c
Normal file
163
gsk/gskpathmeasure.c
Normal file
@@ -0,0 +1,163 @@
|
||||
/*
|
||||
* 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 "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 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)
|
||||
{
|
||||
GskPathMeasure *self;
|
||||
gsize i, n_contours;
|
||||
|
||||
g_return_val_if_fail (path != NULL, 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->n_contours = n_contours;
|
||||
|
||||
for (i = 0; i < n_contours; i++)
|
||||
{
|
||||
self->measures[i].contour_data = gsk_contour_init_measure (path, i, &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 (self->path, i, self->measures[i].contour_data);
|
||||
}
|
||||
|
||||
gsk_path_unref (self->path);
|
||||
g_free (self);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
51
gsk/gskpathmeasure.h
Normal file
51
gsk/gskpathmeasure.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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>
|
||||
*/
|
||||
|
||||
#ifndef __GSK_PATH_MEASURE_H__
|
||||
#define __GSK_PATH_MEASURE_H__
|
||||
|
||||
#if !defined (__GSK_H_INSIDE__) && !defined (GTK_COMPILATION)
|
||||
#error "Only <gsk/gsk.h> can be included directly."
|
||||
#endif
|
||||
|
||||
|
||||
#include <gsk/gsktypes.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define GSK_TYPE_PATH_MEASURE (gsk_path_measure_get_type ())
|
||||
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
GType gsk_path_measure_get_type (void) G_GNUC_CONST;
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
GskPathMeasure * gsk_path_measure_new (GskPath *path);
|
||||
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
GskPathMeasure * gsk_path_measure_ref (GskPathMeasure *self);
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
void gsk_path_measure_unref (GskPathMeasure *self);
|
||||
|
||||
GDK_AVAILABLE_IN_ALL
|
||||
float gsk_path_measure_get_length (GskPathMeasure *self);
|
||||
|
||||
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GskPathMeasure, gsk_path_measure_unref)
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GSK_PATH_MEASURE_H__ */
|
||||
40
gsk/gskpathprivate.h
Normal file
40
gsk/gskpathprivate.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* 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>
|
||||
*/
|
||||
|
||||
|
||||
#ifndef __GSK_PATH_PRIVATE_H__
|
||||
#define __GSK_PATH_PRIVATE_H__
|
||||
|
||||
#include "gskpath.h"
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
gsize gsk_path_get_n_contours (GskPath *path);
|
||||
|
||||
gpointer gsk_contour_init_measure (GskPath *path,
|
||||
gsize i,
|
||||
float *out_length);
|
||||
void gsk_contour_free_measure (GskPath *path,
|
||||
gsize i,
|
||||
gpointer data);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* __GSK_PATH_PRIVATE_H__ */
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include <gsk/gskenums.h>
|
||||
|
||||
typedef struct _GskPath GskPath;
|
||||
typedef struct _GskPathMeasure GskPathMeasure;
|
||||
typedef struct _GskRenderer GskRenderer;
|
||||
typedef struct _GskStroke GskStroke;
|
||||
typedef struct _GskTransform GskTransform;
|
||||
|
||||
@@ -27,6 +27,7 @@ gsk_public_sources = files([
|
||||
'gskcairorenderer.c',
|
||||
'gskglshader.c',
|
||||
'gskpath.c',
|
||||
'gskpathmeasure.c',
|
||||
'gskrenderer.c',
|
||||
'gskrendernode.c',
|
||||
'gskrendernodeimpl.c',
|
||||
@@ -69,6 +70,7 @@ gsk_public_headers = files([
|
||||
'gskenums.h',
|
||||
'gskglshader.h',
|
||||
'gskpath.h',
|
||||
'gskpathmeasure.h',
|
||||
'gskrenderer.h',
|
||||
'gskrendernode.h',
|
||||
'gskroundedrect.h',
|
||||
|
||||
Reference in New Issue
Block a user