HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Path.hpp
1// Copyright 2019 Pokitec
2// All rights reserved.
3
4#pragma once
5
6#include "TTauri/Foundation/BezierPoint.hpp"
7#include "TTauri/Foundation/attributes.hpp"
8#include "TTauri/Foundation/TTauriIconParser.hpp"
9#include "TTauri/Foundation/ResourceView.hpp"
10#include "TTauri/Foundation/exceptions.hpp"
11#include "TTauri/Foundation/vec.hpp"
12#include "TTauri/Foundation/aarect.hpp"
13#include "TTauri/Foundation/mat.hpp"
14#include "TTauri/Foundation/R16G16B16A16SFloat.hpp"
15#include "TTauri/Foundation/SDF8.hpp"
16#include <vector>
17
18namespace tt {
19
20struct BezierCurve;
21template<typename T> struct PixelMap;
22
29struct Path {
33
37
41
44 void clear() noexcept {
45 points.clear();
46 contourEndPoints.clear();
47 layerEndContours.clear();
48 }
49
52 [[nodiscard]] ssize_t numberOfContours() const noexcept;
53
56 [[nodiscard]] ssize_t numberOfLayers() const noexcept;
57
60 [[nodiscard]] bool allLayersHaveSameColor() const noexcept;
61
64 [[nodiscard]] aarect boundingBox() const noexcept;
65
70 void tryRemoveLayers() noexcept;
71
74 [[nodiscard]] std::vector<BezierPoint>::const_iterator beginContour(ssize_t contourNr) const noexcept;
75
76 /* Return and end-iterator beyond the end point of a contour.
77 */
78 [[nodiscard]] std::vector<BezierPoint>::const_iterator endContour(ssize_t contourNr) const noexcept;
79
80 /* Return the first contour index of a layer.
81 */
82 [[nodiscard]] ssize_t beginLayer(ssize_t layerNr) const noexcept;
83
84 /* Return beyond the last contour index of a layer.
85 */
86 [[nodiscard]] ssize_t endLayer(ssize_t layerNr) const noexcept;
87
88 [[nodiscard]] std::vector<BezierPoint> getBezierPointsOfContour(ssize_t contourNr) const noexcept;
89
90 [[nodiscard]] std::vector<BezierCurve> getBeziersOfContour(ssize_t contourNr) const noexcept;
91
92 [[nodiscard]] std::vector<BezierCurve> getBeziers() const noexcept;
93
94 [[nodiscard]] std::pair<Path,vec> getLayer(ssize_t layerNr) const noexcept;
95
96 [[nodiscard]] vec getColorOfLayer(ssize_t layerNr) const noexcept;
97
98 void setColorOfLayer(ssize_t layerNr, vec fillColor) noexcept;
99
102 [[nodiscard]] bool isContourOpen() const noexcept;
103
107 void closeContour() noexcept;
108
111 [[nodiscard]] bool hasLayers() const noexcept;
112
115 [[nodiscard]] bool isLayerOpen() const noexcept;
116
120 void closeLayer(vec fillColor) noexcept;
121
125 void optimizeLayers() noexcept;
126
130 [[nodiscard]] vec currentPosition() const noexcept;
131
135 void moveTo(vec position) noexcept;
136
140 void moveRelativeTo(vec direction) noexcept;
141
142 void lineTo(vec position) noexcept;
143
144 void lineRelativeTo(vec direction) noexcept;
145
146 void quadraticCurveTo(vec controlPosition, vec position) noexcept;
147
152 void quadraticCurveRelativeTo(vec controlDirection, vec direction) noexcept;
153
154 void cubicCurveTo(vec controlPosition1, vec controlPosition2, vec position) noexcept;
155
160 void cubicCurveRelativeTo(vec controlDirection1, vec controlDirection2, vec direction) noexcept;
161
173 void arcTo(float radius, vec position) noexcept;
174
180 void addRectangle(aarect r, vec corners={0.0f, 0.0f, 0.0f, 0.0f}) noexcept;
181
186 void addCircle(vec position, float radius) noexcept;
187
191 void addContour(std::vector<BezierCurve> const &contour) noexcept;
192
196 void addContour(std::vector<BezierPoint>::const_iterator const &begin, std::vector<BezierPoint>::const_iterator const &end) noexcept;
197
201 void addContour(std::vector<BezierPoint> const &contour) noexcept;
202
205 void addPath(Path const &path, vec fillColor) noexcept;
206
209 void addStroke(Path const &path, vec strokeColor, float strokeWidth, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) noexcept;
210
222 [[nodiscard]] Path toStroke(float strokeWidth=1.0f, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) const noexcept;
223
226 [[nodiscard]] Path centerScale(vec extent, float padding=0.0) const noexcept;
227
228 Path &operator+=(Path const &rhs) noexcept;
229
230 [[nodiscard]] friend Path operator+(Path lhs, Path const &rhs) noexcept {
231 return lhs += rhs;
232 }
233
234 template<typename M, std::enable_if_t<is_mat_v<M>, int> = 0>
235 Path &operator*=(M const &rhs) noexcept {
236 for (auto &&point: points) {
237 point *= rhs;
238 }
239 return *this;
240 }
241
242 template<typename M, std::enable_if_t<is_mat_v<M>, int> = 0>
243 friend Path operator*(M const &lhs, Path rhs) noexcept {
244 return rhs *= lhs;
245 }
246};
247
248
249
250
251
258void composit(PixelMap<R16G16B16A16SFloat>& dst, vec color, Path const &mask) noexcept;
259
265void composit(PixelMap<R16G16B16A16SFloat>& dst, Path const &mask) noexcept;
266
271void fill(PixelMap<SDF8> &dst, Path const &path) noexcept;
272
273}
274
275namespace tt {
276
277template<>
278inline std::unique_ptr<Path> parseResource(URL const &location)
279{
280 if (location.extension() == "tticon") {
281 ttlet view = location.loadView();
282
283 try {
284 return std::make_unique<Path>(parseTTauriIcon(*view));
285 } catch (error &e) {
286 e.set<url_tag>(location);
287 throw;
288 }
289
290 } else {
291 TTAURI_THROW(url_error("Unknown extension")
292 .set<url_tag>(location)
293 );
294 }
295}
296
297}
STL namespace.
Class which represents an axis-aligned rectangle.
Definition aarect.hpp:13
Definition BezierCurve.hpp:25
Definition BezierPoint.hpp:17
Definition Path.hpp:29
ssize_t numberOfLayers() const noexcept
bool isContourOpen() const noexcept
bool isLayerOpen() const noexcept
std::vector< BezierPoint >::const_iterator beginContour(ssize_t contourNr) const noexcept
void tryRemoveLayers() noexcept
void quadraticCurveRelativeTo(vec controlDirection, vec direction) noexcept
aarect boundingBox() const noexcept
Calculate bounding box.
void clear() noexcept
Clear the path.
Definition Path.hpp:44
void arcTo(float radius, vec position) noexcept
void addContour(std::vector< BezierPoint > const &contour) noexcept
void addStroke(Path const &path, vec strokeColor, float strokeWidth, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) noexcept
bool hasLayers() const noexcept
Path centerScale(vec extent, float padding=0.0) const noexcept
Center and scale a path inside the extent with padding.
std::vector< ssize_t > contourEndPoints
Definition Path.hpp:36
bool allLayersHaveSameColor() const noexcept
void addContour(std::vector< BezierPoint >::const_iterator const &begin, std::vector< BezierPoint >::const_iterator const &end) noexcept
Path toStroke(float strokeWidth=1.0f, LineJoinStyle lineJoinStyle=LineJoinStyle::Miter, float tolerance=0.05f) const noexcept
std::vector< std::pair< ssize_t, vec > > layerEndContours
Definition Path.hpp:40
void addRectangle(aarect r, vec corners={0.0f, 0.0f, 0.0f, 0.0f}) noexcept
ssize_t numberOfContours() const noexcept
void optimizeLayers() noexcept
Optimize layers.
void closeLayer(vec fillColor) noexcept
std::vector< BezierPoint > points
Definition Path.hpp:32
void moveTo(vec position) noexcept
vec currentPosition() const noexcept
void moveRelativeTo(vec direction) noexcept
void addPath(Path const &path, vec fillColor) noexcept
void addContour(std::vector< BezierCurve > const &contour) noexcept
void addCircle(vec position, float radius) noexcept
void cubicCurveRelativeTo(vec controlDirection1, vec controlDirection2, vec direction) noexcept
void closeContour() noexcept
A 4D vector.
Definition vec.hpp:37
T fill(T... args)