Path#
Added in version 4.14.
- class Path(*args, **kwargs)#
Describes lines and curves that are more complex than simple rectangles.
Paths can used for rendering (filling or stroking) and for animations (e.g. as trajectories).
GskPath is an immutable, opaque, reference-counted struct.
After creation, you cannot change the types it represents. Instead,
new GskPath objects have to be created. The PathBuilder
structure is meant to help in this endeavor.
Conceptually, a path consists of zero or more contours (continuous, connected curves), each of which may or may not be closed. Contours are typically constructed from Bézier segments.
Methods#
- class Path
- equal(path2: Path) bool#
Returns whether two paths have identical structure.
Note that it is possible to construct paths that render identical even though they don’t have the same structure.
Added in version 4.22.
- Parameters:
path2 – another path
- foreach(flags: PathForeachFlags, func: Callable[[PathOperation, list[Point], float, Any], bool], user_data: Any = None) bool#
Calls
funcfor every operation of the path.Note that this may only approximate
self, because paths can contain optimizations for various specialized contours, and depending on theflags, the path may be decomposed into simpler curves than the ones that it contained originally.This function serves two purposes:
When the
flagsallow everything, it provides access to the raw, unmodified data of the path.When the
flagsdisallow certain operations, it provides an approximation of the path using just the allowed operations.
Added in version 4.14.
- Parameters:
flags – flags to pass to the foreach function
func – the function to call for operations
user_data – user data passed to
func
- foreach_intersection(path2: Path | None, func: Callable[[Path, PathPoint, Path, PathPoint, PathIntersection, Any], bool], user_data: Any = None) bool#
Finds intersections between two paths.
This function finds intersections between
path1andpath2, and callsfuncfor each of them, in increasing order forpath1.If
path2is not provided or equal topath1, the function finds non-trivial self-intersections ofpath1.When segments of the paths coincide, the callback is called once for the start of the segment, with
GSK_PATH_INTERSECTION_START, and once for the end of the segment, withGSK_PATH_INTERSECTION_END. Note that other intersections may occur between the start and end of such a segment.If
funcreturnsFALSE, the iteration is stopped.Added in version 4.20.
- Parameters:
path2 – the second path
func – the function to call for intersections
user_data – user data passed to
func
- get_bounds() tuple[bool, Rect]#
Computes the bounds of the given path.
The returned bounds may be larger than necessary, because this function aims to be fast, not accurate. The bounds are guaranteed to contain the path. For accurate bounds, use
get_tight_bounds.It is possible that the returned rectangle has 0 width and/or height. This can happen when the path only describes a point or an axis-aligned line.
If the path is empty, false is returned and
boundsare set tozero(). This is different from the case where the path is a single point at the origin, where theboundswill also be set to the zero rectangle but true will be returned.Added in version 4.14.
- get_closest_point(point: Point, threshold: float) tuple[bool, PathPoint, float]#
Computes the closest point on the path to the given point.
If there is no point closer than the given threshold, false is returned.
Added in version 4.14.
- Parameters:
point – the point
threshold – maximum allowed distance
- get_end_point() tuple[bool, PathPoint]#
Gets the end point of the path.
An empty path has no points, so false is returned in this case.
Added in version 4.14.
- get_next(point: PathPoint) tuple[bool, PathPoint]#
Moves
pointto the next vertex.An empty path has no points, so false is returned in this case.
Added in version 4.22.
- Parameters:
point – the current point
- get_previous(point: PathPoint) tuple[bool, PathPoint]#
Moves
pointto the previous vertex.An empty path has no points, so false is returned in this case.
Added in version 4.22.
- Parameters:
point – the current point
- get_start_point() tuple[bool, PathPoint]#
Gets the start point of the path.
An empty path has no points, so false is returned in this case.
Added in version 4.14.
- get_stroke_bounds(stroke: Stroke) tuple[bool, Rect]#
Computes the bounds for stroking the given path with the given parameters.
The returned bounds may be larger than necessary, because this function aims to be fast, not accurate. The bounds are guaranteed to contain the area affected by the stroke, including protrusions like miters.
Added in version 4.14.
- Parameters:
stroke – stroke parameters
- get_tight_bounds() tuple[bool, Rect]#
Computes the tight bounds of the given path.
This function works harder than
get_boundsto produce the smallest possible bounds.Added in version 4.22.
- in_fill(point: Point, fill_rule: FillRule) bool#
Returns whether a point is inside the fill area of a path.
Note that this function assumes that filling a contour implicitly closes it.
Added in version 4.14.
- Parameters:
point – the point to test
fill_rule – the fill rule to follow
- is_empty() bool#
Checks if the path is empty, i.e. contains no lines or curves.
Added in version 4.14.
- classmethod parse() Path | None#
Constructs a path from a serialized form.
The string is expected to be in (a superset of) SVG path syntax, as e.g. produced by
to_string.A high-level summary of the syntax:
M x yMove to(x, y)L x yAdd a line from the current point to(x, y)Q x1 y1 x2 y2Add a quadratic Bézier from the current point to(x2, y2), with control point(x1, y1)C x1 y1 x2 y2 x3 y3Add a cubic Bézier from the current point to(x3, y3), with control points(x1, y1)and(x2, y2)ZClose the contour by drawing a line back to the start pointH xAdd a horizontal line from the current point to the given x valueV yAdd a vertical line from the current point to the given y valueT x2 y2Add a quadratic Bézier, using the reflection of the previous segments’ control point as control pointS x2 y2 x3 y3Add a cubic Bézier, using the reflection of the previous segments’ second control point as first control pointA rx ry r l s x yAdd an elliptical arc from the current point to(x, y)with radii rx and ry. See the SVG documentation for how the other parameters influence the arc.O x1 y1 x2 y2 wAdd a rational quadratic Bézier from the current point to(x2, y2)with control point(x1, y1)and weightw.
All the commands have lowercase variants that interpret coordinates relative to the current point.
The
Ocommand is an extension that is not supported in SVG.Added in version 4.14.
- to_cairo(cr: Context) None#
Appends the path to a cairo context for drawing with Cairo.
This may cause some suboptimal conversions to be performed as Cairo does not support all features of
GskPath.This function does not clear the existing Cairo path. Call cairo_new_path() if you want this.
Added in version 4.14.
- Parameters:
cr – a cairo context