tiny-skia-cpp
A C++20 2D rendering library (port of tiny-skia)
Loading...
Searching...
No Matches
Geom.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <cstdint>
7#include <optional>
8
9#include "tiny_skia/Math.h"
10
11namespace tiny_skia {
12
13class ScreenIntRect;
14class IntRect;
15class Rect;
16
18class IntSize {
19 public:
20 constexpr IntSize() = default;
21 constexpr IntSize(LengthU32 width, LengthU32 height) : width_(width), height_(height) {}
22
24 static std::optional<IntSize> fromWH(LengthU32 width, LengthU32 height) {
25 if (width == 0u || height == 0u) {
26 return std::nullopt;
27 }
28 return IntSize{width, height};
29 }
30
31 constexpr LengthU32 width() const { return width_; }
32 constexpr LengthU32 height() const { return height_; }
33 [[nodiscard]] ScreenIntRect toScreenIntRect(std::uint32_t x, std::uint32_t y) const;
34 [[nodiscard]] std::optional<IntRect> toIntRect(std::int32_t x, std::int32_t y) const;
35 [[nodiscard]] Rect toRect() const;
36
37 constexpr bool operator==(const IntSize&) const = default;
38
39 private:
40 LengthU32 width_ = 0;
41 LengthU32 height_ = 0;
42};
43
46 public:
47 constexpr ScreenIntRect() = default;
48
50 static std::optional<ScreenIntRect> fromXYWH(std::uint32_t x, std::uint32_t y,
51 std::uint32_t width, std::uint32_t height);
52
53 static constexpr ScreenIntRect fromXYWHSafe(std::uint32_t x, std::uint32_t y, LengthU32 width,
54 LengthU32 height) {
55 return ScreenIntRect{x, y, width, height};
56 }
57
58 constexpr std::uint32_t x() const { return x_; }
59 constexpr std::uint32_t y() const { return y_; }
60 constexpr LengthU32 width() const { return width_; }
61 constexpr LengthU32 height() const { return height_; }
62 constexpr LengthU32 widthSafe() const { return width_; }
63
64 constexpr std::uint32_t left() const { return x_; }
65 constexpr std::uint32_t top() const { return y_; }
66
67 std::uint32_t right() const;
68 std::uint32_t bottom() const;
69 IntSize size() const;
70 [[nodiscard]] bool contains(const ScreenIntRect& other) const;
71 [[nodiscard]] IntRect toIntRect() const;
72 [[nodiscard]] Rect toRect() const;
73
74 private:
75 constexpr ScreenIntRect(std::uint32_t x, std::uint32_t y, LengthU32 width, LengthU32 height)
76 : x_{x}, y_{y}, width_{width}, height_{height} {}
77
78 std::uint32_t x_ = 0;
79 std::uint32_t y_ = 0;
80 LengthU32 width_;
81 LengthU32 height_;
82};
83
85class IntRect {
86 public:
88 static std::optional<IntRect> fromXYWH(std::int32_t x, std::int32_t y, std::uint32_t width,
89 std::uint32_t height);
90
91 constexpr std::int32_t x() const { return x_; }
92 constexpr std::int32_t y() const { return y_; }
93 constexpr LengthU32 width() const { return width_; }
94 constexpr LengthU32 height() const { return height_; }
95
96 [[nodiscard]] constexpr std::int32_t left() const { return x_; }
97 [[nodiscard]] constexpr std::int32_t top() const { return y_; }
98
99 [[nodiscard]] std::int32_t right() const;
100 [[nodiscard]] std::int32_t bottom() const;
101
103 [[nodiscard]] std::optional<IntRect> intersect(const IntRect& other) const;
104
105 [[nodiscard]] std::optional<ScreenIntRect> toScreenIntRect() const;
106
107 private:
108 constexpr IntRect(std::int32_t x, std::int32_t y, LengthU32 width, LengthU32 height)
109 : x_{x}, y_{y}, width_{width}, height_{height} {}
110
111 std::int32_t x_ = 0;
112 std::int32_t y_ = 0;
113 LengthU32 width_ = 0;
114 LengthU32 height_ = 0;
115};
116
119class Rect {
120 public:
122 static std::optional<Rect> fromLTRB(float left, float top, float right, float bottom);
123
125 static std::optional<Rect> fromXYWH(float x, float y, float w, float h) {
126 return fromLTRB(x, y, x + w, y + h);
127 }
128
129 constexpr float left() const { return left_; }
130 constexpr float top() const { return top_; }
131 constexpr float right() const { return right_; }
132 constexpr float bottom() const { return bottom_; }
133
134 constexpr float width() const { return right_ - left_; }
135 constexpr float height() const { return bottom_ - top_; }
136
137 constexpr bool operator==(const Rect&) const = default;
138
140 [[nodiscard]] std::optional<IntRect> roundOut() const;
142 [[nodiscard]] std::optional<IntRect> round() const;
143
144 private:
145 constexpr Rect(float left, float top, float right, float bottom)
146 : left_(left), top_(top), right_(right), bottom_(bottom) {}
147
148 float left_ = 0.0f;
149 float top_ = 0.0f;
150 float right_ = 0.0f;
151 float bottom_ = 0.0f;
152};
153
155std::optional<ScreenIntRect> intRectToScreen(const IntRect& rect);
156
157} // namespace tiny_skia
Signed integer rectangle (x, y, width, height).
Definition Geom.h:85
static std::optional< IntRect > fromXYWH(std::int32_t x, std::int32_t y, std::uint32_t width, std::uint32_t height)
Creates from components. Returns nullopt for zero dimensions or overflow.
std::optional< IntRect > intersect(const IntRect &other) const
Returns the intersection with another rect, or nullopt if disjoint.
Non-zero integer dimensions (width x height).
Definition Geom.h:18
static std::optional< IntSize > fromWH(LengthU32 width, LengthU32 height)
Creates from width/height. Returns nullopt if either is zero.
Definition Geom.h:24
Floating-point rectangle (left, top, right, bottom). All components must be finite,...
Definition Geom.h:119
static std::optional< Rect > fromXYWH(float x, float y, float w, float h)
Creates from origin and size. Returns nullopt if invalid.
Definition Geom.h:125
std::optional< IntRect > roundOut() const
Converts to IntRect by rounding outward (floor left/top, ceil right/bottom).
std::optional< IntRect > round() const
Converts to IntRect by rounding to nearest integer.
static std::optional< Rect > fromLTRB(float left, float top, float right, float bottom)
Creates from edges. Returns nullopt for non-finite, empty, or inverted rects.
Unsigned integer rectangle (x, y, width, height). Used for screen coordinates.
Definition Geom.h:45
static std::optional< ScreenIntRect > fromXYWH(std::uint32_t x, std::uint32_t y, std::uint32_t width, std::uint32_t height)
Creates from components. Returns nullopt for zero width/height or overflow.