tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
PathBuilder.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstddef>
7#include <optional>
8#include <vector>
9
10#include "tiny_skia/Path.h"
11#include "tiny_skia/PathGeometry.h"
12#include "tiny_skia/Scalar.h"
13
14namespace tiny_skia {
15
21 public:
22 PathBuilder() = default;
23
25 PathBuilder(std::size_t verbsCapacity, std::size_t pointsCapacity) {
26 verbs_.reserve(verbsCapacity);
27 points_.reserve(pointsCapacity);
28 }
29
31 void reserve(std::size_t additionalVerbs, std::size_t additionalPoints) {
32 verbs_.reserve(verbs_.size() + additionalVerbs);
33 points_.reserve(points_.size() + additionalPoints);
34 }
35
36 [[nodiscard]] std::size_t size() const { return verbs_.size(); }
37 [[nodiscard]] bool empty() const { return verbs_.empty(); }
38
40 PathBuilder& moveTo(float x, float y);
42 PathBuilder& lineTo(float x, float y);
44 PathBuilder& quadTo(float x1, float y1, float x, float y);
48 PathBuilder& cubicTo(float x1, float y1, float x2, float y2, float x, float y);
52 PathBuilder& conicTo(float x1, float y1, float x, float y, float weight);
54 PathBuilder& conicPointsTo(Point pt1, Point pt2, float weight);
55
57 void setConicTolerance(float tolerance) { conicTolerance_ = tolerance; }
60
61 [[nodiscard]] std::optional<Point> lastPoint() const;
62 void setLastPoint(Point pt);
63
65 [[nodiscard]] bool isZeroLengthSincePoint(std::size_t startPtIndex) const;
66
68 [[nodiscard]] static std::optional<Path> fromCircle(float cx, float cy, float radius) {
70 b.pushCircle(cx, cy, radius);
71 return b.finish();
72 }
73
75 PathBuilder& pushRect(const Rect& rect);
77 PathBuilder& pushOval(const Rect& oval);
79 PathBuilder& pushCircle(float x, float y, float r);
81 PathBuilder& pushPath(const Path& other);
85 PathBuilder& reversePathTo(const PathBuilder& other);
86
88 void clear();
89
91 [[nodiscard]] std::optional<Path> finish();
92
94 [[nodiscard]] const std::vector<Point>& points() const { return points_; }
96 [[nodiscard]] const std::vector<PathVerb>& verbs() const { return verbs_; }
97
98 private:
99 void injectMoveToIfNeeded();
100
101 std::vector<PathVerb> verbs_;
102 std::vector<Point> points_;
103 std::size_t lastMoveToIndex_ = 0;
104 bool moveToRequired_ = true;
105 float conicTolerance_ = 0.25f;
106};
107
108} // namespace tiny_skia
Immutable vector path and related types.
Incrementally builds a Path from move/line/quad/cubic/close operations.
Definition PathBuilder.h:20
PathBuilder & pushPathBuilder(const PathBuilder &other)
Appends all segments from another builder.
static std::optional< Path > fromCircle(float cx, float cy, float radius)
Creates a Path from a circle. Returns nullopt for non-positive radius.
Definition PathBuilder.h:68
PathBuilder(std::size_t verbsCapacity, std::size_t pointsCapacity)
Pre-allocates capacity for verbs and points.
Definition PathBuilder.h:25
void clear()
Resets the builder, discarding all segments.
void setConicTolerance(float tolerance)
Sets the tolerance for conic-to-quadratic conversion (default 0.25).
Definition PathBuilder.h:57
PathBuilder & quadTo(float x1, float y1, float x, float y)
Adds a quadratic Bezier to (x, y) with control point (x1, y1).
PathBuilder & pushOval(const Rect &oval)
Appends a closed oval sub-path inscribed in the given rect.
std::optional< Path > finish()
Builds and returns the immutable Path. Returns nullopt if empty or invalid.
PathBuilder & pushPath(const Path &other)
Appends all segments from another path.
PathBuilder & pushRect(const Rect &rect)
Appends a closed rectangle sub-path.
PathBuilder & quadToPt(Point p1, Point p)
Adds a quadratic Bezier using Point arguments.
PathBuilder & conicTo(float x1, float y1, float x, float y, float weight)
Adds a conic (rational quadratic) segment, approximated as quadratics.
PathBuilder & pushCircle(float x, float y, float r)
Appends a closed circle sub-path.
PathBuilder & cubicTo(float x1, float y1, float x2, float y2, float x, float y)
Adds a cubic Bezier to (x, y) with control points (x1,y1) and (x2,y2).
PathBuilder & close()
Closes the current sub-path.
PathBuilder & conicPointsTo(Point pt1, Point pt2, float weight)
Adds a conic segment using Point arguments.
PathBuilder & lineTo(float x, float y)
Adds a line segment to (x, y).
void reserve(std::size_t additionalVerbs, std::size_t additionalPoints)
Reserves additional capacity.
Definition PathBuilder.h:31
PathBuilder & cubicToPt(Point p1, Point p2, Point p)
Adds a cubic Bezier using Point arguments.
PathBuilder & moveTo(float x, float y)
Begins a new sub-path at (x, y).
Immutable vector path — a sequence of lines, quadratics, and cubics.
Definition Path.h:56
Floating-point rectangle (left, top, right, bottom). All components must be finite,...
Definition Geom.h:119
2D point / vector with float components.
Definition Point.h:14