GSK provides a path API that can be used to render more complex shapes than lines or rounded rectangles. It is comparable to cairos path API, with some notable differences.
In general, a path consists of one or more connected contours, each of which may have multiple operations, and may or may not be closed. Operations can be straight lines or curves of various kinds. At the points where the operations connect, the path can have sharp turns.
The central object of the GSK path API is the immutable
struct, which contains path data in compact form suitable for rendering.
GskPath is immutable, the auxiliary
can be used to construct a path piece by piece. The pieces are specified with
points, some of which will be on the path, while others are just control points
that are used to influence the shape of the resulting path.
GskPathBuilder API has three distinct groups of functions:
Functions for building contours from individual operations, like
GskPathBuildermaintains a current point, so these methods all take one less points than necessary for the operation (e.g.
gsk_path_builder_line_toonly takes a single point and draws a line from the current point to the new point).
When you are done with building a path, you can convert the accumulated path
data into a
GskPath struct with
A sometimes convenient alternative is to create a path from a serialized form,
gsk_path_parse(). This function interprets strings in SVG path syntax,
M 100 100 C 100 200 200 200 200 100 Z
There are two main ways to render with paths. The first is to fill the
interior of a path with a color or more complex content, such as a gradient.
GSK supports different ways of determining what part of the plane are interior
to the path, which can be selected with a
Alternatively, a path can be stroked, which means to emulate drawing with an idealized pen along the path. The result of stroking a path is another path (the stroke path), which is then filled.
The stroke operation can be influenced with the
that collects various stroke parameters, such as the line width, the style
of line joins and line caps, and a dash pattern.
When paths are rendered as part of an interactive interface, it is sometimes
necessary to determine whether the mouse points is over the path. GSK provides
gsk_path_in_fill() for this purpose.
An important property of paths is their length. Computing it efficiently
requires caching, therefore GSK provides a separate
to deal with path lengths. After constructing a
GskPathMeasure object for a path,
it can be used to determine the length of the path with
and locate points at a given distance into the path with
Paths have uses beyond rendering, for example as trajectories in animations.
In such uses, it is often important to access properties of paths, such as
their tangents at certain points. GSK provides an abstract representation
for points on a path in the form of the
You can query properties of a path at certain point once you have a
GskPathPoint representing that point.
To query properties of the path at a point, use
Some of the properties can have different values for the path going into the point and the path leaving the point, typically at points where the path takes sharp turns. Examples for this are tangents (which can have up to 4 different values) and curvatures (which can have two different values).
Lots of powerful functionality can be implemented for paths:
- Finding intersections
- Offsetting curves
- Turning stroke outlines into paths
- Molding curves (making them pass through a given point)
GSK does not provide API for all of these, but it does offer a way to get at
the underlying Bézier curves, so you can implement such functionality yourself.
You can use
gsk_path_foreach() to iterate over the operations of the
path, and get the points needed to reconstruct or modify the path piece by piece.
See e.g. the Primer on Bézier curves for inspiration of useful things to explore.