HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
graphic_path.hpp
1// Copyright Take Vos 2019-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "bezier_point.hpp"
8#include "bezier_curve.hpp"
9#include "exception.hpp"
10#include "numeric_array.hpp"
11#include "aarect.hpp"
12#include "geometry/transform.hpp"
13#include "color/sfloat_rgba16.hpp"
14#include "color/sdf_r8.hpp"
15#include <vector>
16
17namespace tt {
18
19struct bezier_curve;
20template<typename T>
21class pixel_map;
22
33
37
41
44 void clear() noexcept
45 {
46 points.clear();
47 contourEndPoints.clear();
48 layerEndContours.clear();
49 }
50
53 [[nodiscard]] ssize_t numberOfContours() const noexcept;
54
57 [[nodiscard]] ssize_t numberOfLayers() const noexcept;
58
61 [[nodiscard]] bool allLayersHaveSameColor() const noexcept;
62
65 [[nodiscard]] aarect boundingBox() const noexcept;
66
71 void tryRemoveLayers() noexcept;
72
75 [[nodiscard]] std::vector<bezier_point>::const_iterator beginContour(ssize_t contourNr) const noexcept;
76
77 /* Return and end-iterator beyond the end point of a contour.
78 */
79 [[nodiscard]] std::vector<bezier_point>::const_iterator endContour(ssize_t contourNr) const noexcept;
80
81 /* Return the first contour index of a layer.
82 */
83 [[nodiscard]] ssize_t beginLayer(ssize_t layerNr) const noexcept;
84
85 /* Return beyond the last contour index of a layer.
86 */
87 [[nodiscard]] ssize_t endLayer(ssize_t layerNr) const noexcept;
88
89 [[nodiscard]] std::vector<bezier_point> getbezier_pointsOfContour(ssize_t contourNr) const noexcept;
90
91 [[nodiscard]] std::vector<bezier_curve> getBeziersOfContour(ssize_t contourNr) const noexcept;
92
93 [[nodiscard]] std::vector<bezier_curve> getBeziers() const noexcept;
94
95 [[nodiscard]] std::pair<graphic_path, color> getLayer(ssize_t layerNr) const noexcept;
96
97 [[nodiscard]] color getColorOfLayer(ssize_t layerNr) const noexcept;
98
99 void setColorOfLayer(ssize_t layerNr, color fillColor) noexcept;
100
103 [[nodiscard]] bool isContourOpen() const noexcept;
104
108 void closeContour() noexcept;
109
112 [[nodiscard]] bool hasLayers() const noexcept;
113
116 [[nodiscard]] bool isLayerOpen() const noexcept;
117
121 void closeLayer(color fillColor) noexcept;
122
126 void optimizeLayers() noexcept;
127
131 [[nodiscard]] point2 currentPosition() const noexcept;
132
136 void moveTo(point2 position) noexcept;
137
141 void moveRelativeTo(vector2 direction) noexcept;
142
143 void lineTo(point2 position) noexcept;
144
145 void lineRelativeTo(vector2 direction) noexcept;
146
147 void quadraticCurveTo(point2 controlPosition, point2 position) noexcept;
148
153 void quadraticCurveRelativeTo(vector2 controlDirection, vector2 direction) noexcept;
154
155 void cubicCurveTo(point2 controlPosition1, point2 controlPosition2, point2 position) noexcept;
156
162 void cubicCurveRelativeTo(vector2 controlDirection1, vector2 controlDirection2, vector2 direction) noexcept;
163
175 void arcTo(float radius, point2 position) noexcept;
176
182 void addRectangle(aarect rectangle, f32x4 corners = {0.0f, 0.0f, 0.0f, 0.0f}) noexcept;
183
188 void addCircle(point2 position, float radius) noexcept;
189
193 void addContour(std::vector<bezier_curve> const &contour) noexcept;
194
199 std::vector<bezier_point>::const_iterator const &begin,
200 std::vector<bezier_point>::const_iterator const &end) noexcept;
201
205 void addContour(std::vector<bezier_point> const &contour) noexcept;
206
209 void addPath(graphic_path const &path, color fillColor) noexcept;
210
214 graphic_path const &path,
215 color strokeColor,
216 float strokeWidth,
217 LineJoinStyle lineJoinStyle = LineJoinStyle::Miter,
218 float tolerance = 0.05f) noexcept;
219
231 [[nodiscard]] graphic_path toStroke(
232 float strokeWidth = 1.0f,
233 LineJoinStyle lineJoinStyle = LineJoinStyle::Miter,
234 float tolerance = 0.05f) const noexcept;
235
238 [[nodiscard]] graphic_path centerScale(f32x4 extent, float padding = 0.0) const noexcept;
239
240 graphic_path &operator+=(graphic_path const &rhs) noexcept;
241
242 [[nodiscard]] friend graphic_path operator+(graphic_path lhs, graphic_path const &rhs) noexcept
243 {
244 return lhs += rhs;
245 }
246
247 friend graphic_path operator*(geo::transformer<2> auto const &lhs, graphic_path const &rhs) noexcept
248 {
249 auto rhs_ = rhs;
250 for (auto &&point : rhs_.points) {
251 point = lhs * point;
252 }
253 return rhs_;
254 }
255};
256
263void composit(pixel_map<sfloat_rgba16> &dst, f32x4 color, graphic_path const &mask) noexcept;
264
270void composit(pixel_map<sfloat_rgba16> &dst, graphic_path const &mask) noexcept;
271
276void fill(pixel_map<sdf_r8> &dst, graphic_path const &path) noexcept;
277
278} // namespace tt
STL namespace.
Definition bezier_curve.hpp:28
Definition bezier_point.hpp:18
This is a RGBA floating point color.
Definition color.hpp:39
A high-level geometric vector Part of the high-level vector, point, mat and color types.
Definition vector.hpp:20
A path is a vector graphics object.
Definition graphic_path.hpp:29
bool hasLayers() const noexcept
This path has layers.
void quadraticCurveRelativeTo(vector2 controlDirection, vector2 direction) noexcept
Draw curve from the current position to the new direction.
std::vector< std::pair< ssize_t, color > > layerEndContours
An color and index into.
Definition graphic_path.hpp:40
std::vector< bezier_point >::const_iterator beginContour(ssize_t contourNr) const noexcept
Return an iterator to the start point of a contour.
void optimizeLayers() noexcept
Optimize layers.
ssize_t numberOfLayers() const noexcept
Return the number of closed layers.
aarect boundingBox() const noexcept
Calculate bounding box.
void addContour(std::vector< bezier_point > const &contour) noexcept
Curve with the given bezier curve.
graphic_path toStroke(float strokeWidth=1.0f, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) const noexcept
Convert path to stroke-path.
void addContour(std::vector< bezier_curve > const &contour) noexcept
Contour with the given bezier curves.
void closeLayer(color fillColor) noexcept
Close current contour.
void moveTo(point2 position) noexcept
Start a new contour at position.
void arcTo(float radius, point2 position) noexcept
Draw an circular arc.
bool isContourOpen() const noexcept
Return true if there is an open contour.
point2 currentPosition() const noexcept
Get the currentPosition of the open contour.
std::vector< ssize_t > contourEndPoints
An index into.
Definition graphic_path.hpp:36
void addCircle(point2 position, float radius) noexcept
Draw a circle.
void addPath(graphic_path const &path, color fillColor) noexcept
Add path and close layer.
void addStroke(graphic_path const &path, color strokeColor, float strokeWidth, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) noexcept
Stroke a path and close layer.
graphic_path centerScale(f32x4 extent, float padding=0.0) const noexcept
Center and scale a path inside the extent with padding.
void addRectangle(aarect rectangle, f32x4 corners={0.0f, 0.0f, 0.0f, 0.0f}) noexcept
Draw a rectangle.
void closeContour() noexcept
Close current contour.
bool isLayerOpen() const noexcept
Return true if there is an open layer.
ssize_t numberOfContours() const noexcept
Return the number of closed contours.
void moveRelativeTo(vector2 direction) noexcept
Start a new contour relative to current position.
void tryRemoveLayers() noexcept
Try to move the layers in a path.
void clear() noexcept
Clear the path.
Definition graphic_path.hpp:44
void addContour(std::vector< bezier_point >::const_iterator const &begin, std::vector< bezier_point >::const_iterator const &end) noexcept
Curve with the given bezier curve.
std::vector< bezier_point > points
A set of all bezier points describing all bezier curves, contours and layers.
Definition graphic_path.hpp:32
bool allLayersHaveSameColor() const noexcept
Check if all layers have the same color.
void cubicCurveRelativeTo(vector2 controlDirection1, vector2 controlDirection2, vector2 direction) noexcept
Draw curve from the current position to the new direction.
Definition transform.hpp:86
T fill(T... args)