mirror of
https://gitlab.gnome.org/GNOME/gtk.git
synced 2024-12-26 13:41:07 +00:00
1b5dfcba7e
This commit adds the basic infrastructure for paths. The public APIs consists of GskPath, GskPathPoint and GskPathBuilder. GskPath is a data structure for paths that consists of contours, which in turn might contain Bézier curves. The Bezier data structure is inspired by Skia, with separate arrays for points and operations. One advantage of this arrangement is that start and end points are shared between adjacent curves. A GskPathPoint represents a point on a path, which can be queried for various properties. GskPathBuilder is an auxiliary builder object for paths.
173 lines
5.0 KiB
C
173 lines
5.0 KiB
C
/*
|
|
* 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>
|
|
*/
|
|
|
|
|
|
#pragma once
|
|
|
|
#include "gskpath.h"
|
|
#include "gskpathbuilder.h"
|
|
|
|
G_BEGIN_DECLS
|
|
|
|
typedef gpointer gskpathop;
|
|
|
|
static inline
|
|
gskpathop gsk_pathop_encode (GskPathOperation op,
|
|
const graphene_point_t *pts);
|
|
static inline
|
|
const graphene_point_t *gsk_pathop_points (gskpathop pop);
|
|
static inline
|
|
GskPathOperation gsk_pathop_op (gskpathop pop);
|
|
|
|
static inline
|
|
gboolean gsk_pathop_foreach (gskpathop pop,
|
|
GskPathForeachFunc func,
|
|
gpointer user_data);
|
|
|
|
/* included inline so tests can use them */
|
|
static inline
|
|
void gsk_path_builder_pathop_to (GskPathBuilder *builder,
|
|
gskpathop op);
|
|
static inline
|
|
void gsk_path_builder_pathop_reverse_to (GskPathBuilder *builder,
|
|
gskpathop op);
|
|
|
|
/* IMPLEMENTATION */
|
|
|
|
#define GSK_PATHOP_OPERATION_MASK (0x7)
|
|
|
|
static inline gskpathop
|
|
gsk_pathop_encode (GskPathOperation op,
|
|
const graphene_point_t *pts)
|
|
{
|
|
/* g_assert (op & GSK_PATHOP_OPERATION_MASK == op); */
|
|
g_assert ((GPOINTER_TO_SIZE (pts) & GSK_PATHOP_OPERATION_MASK) == 0);
|
|
|
|
return GSIZE_TO_POINTER (GPOINTER_TO_SIZE (pts) | op);
|
|
}
|
|
|
|
static inline const graphene_point_t *
|
|
gsk_pathop_points (gskpathop pop)
|
|
{
|
|
return GSIZE_TO_POINTER (GPOINTER_TO_SIZE (pop) & ~GSK_PATHOP_OPERATION_MASK);
|
|
}
|
|
|
|
static inline
|
|
GskPathOperation gsk_pathop_op (gskpathop pop)
|
|
{
|
|
return GPOINTER_TO_SIZE (pop) & GSK_PATHOP_OPERATION_MASK;
|
|
}
|
|
|
|
static inline gboolean
|
|
gsk_pathop_foreach (gskpathop pop,
|
|
GskPathForeachFunc func,
|
|
gpointer user_data)
|
|
{
|
|
switch (gsk_pathop_op (pop))
|
|
{
|
|
case GSK_PATH_MOVE:
|
|
return func (gsk_pathop_op (pop), gsk_pathop_points (pop), 1, user_data);
|
|
|
|
case GSK_PATH_CLOSE:
|
|
case GSK_PATH_LINE:
|
|
return func (gsk_pathop_op (pop), gsk_pathop_points (pop), 2, user_data);
|
|
|
|
case GSK_PATH_QUAD:
|
|
return func (gsk_pathop_op (pop), gsk_pathop_points (pop), 3, user_data);
|
|
|
|
case GSK_PATH_CUBIC:
|
|
return func (gsk_pathop_op (pop), gsk_pathop_points (pop), 4, user_data);
|
|
|
|
default:
|
|
g_assert_not_reached ();
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
gsk_path_builder_pathop_to (GskPathBuilder *builder,
|
|
gskpathop op)
|
|
{
|
|
const graphene_point_t *pts = gsk_pathop_points (op);
|
|
|
|
switch (gsk_pathop_op (op))
|
|
{
|
|
case GSK_PATH_MOVE:
|
|
gsk_path_builder_move_to (builder, pts[0].x, pts[0].y);
|
|
break;
|
|
|
|
case GSK_PATH_CLOSE:
|
|
gsk_path_builder_close (builder);
|
|
break;
|
|
|
|
case GSK_PATH_LINE:
|
|
gsk_path_builder_line_to (builder, pts[1].x, pts[1].y);
|
|
break;
|
|
|
|
case GSK_PATH_QUAD:
|
|
gsk_path_builder_quad_to (builder, pts[1].x, pts[1].y, pts[2].x, pts[2].y);
|
|
break;
|
|
|
|
case GSK_PATH_CUBIC:
|
|
gsk_path_builder_cubic_to (builder, pts[1].x, pts[1].y, pts[2].x, pts[2].y, pts[3].x, pts[3].y);
|
|
break;
|
|
|
|
default:
|
|
g_assert_not_reached ();
|
|
break;
|
|
}
|
|
}
|
|
|
|
static inline void
|
|
gsk_path_builder_pathop_reverse_to (GskPathBuilder *builder,
|
|
gskpathop op)
|
|
{
|
|
const graphene_point_t *pts = gsk_pathop_points (op);
|
|
|
|
switch (gsk_pathop_op (op))
|
|
{
|
|
case GSK_PATH_MOVE:
|
|
gsk_path_builder_move_to (builder, pts[0].x, pts[0].y);
|
|
break;
|
|
|
|
case GSK_PATH_CLOSE:
|
|
gsk_path_builder_line_to (builder, pts[0].x, pts[0].y);
|
|
break;
|
|
|
|
case GSK_PATH_LINE:
|
|
gsk_path_builder_line_to (builder, pts[1].x, pts[1].y);
|
|
break;
|
|
|
|
case GSK_PATH_QUAD:
|
|
gsk_path_builder_quad_to (builder, pts[1].x, pts[1].y, pts[0].x, pts[0].y);
|
|
break;
|
|
|
|
case GSK_PATH_CUBIC:
|
|
gsk_path_builder_cubic_to (builder, pts[2].x, pts[2].y, pts[1].x, pts[1].y, pts[0].x, pts[0].y);
|
|
break;
|
|
|
|
default:
|
|
g_assert_not_reached ();
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
G_END_DECLS
|