-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.hpp
More file actions
216 lines (188 loc) · 6.52 KB
/
Copy pathtypes.hpp
File metadata and controls
216 lines (188 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 Diego Lopes <diego95lopes@gmail.com>
#pragma once
#ifdef __APPLE__
#include <cairo.h>
#else
#include <cairo/cairo.h>
#endif
#include <algorithm>
#include <cmath>
#include <memory>
#include <numbers>
#include <string>
#include <vector>
constexpr double Pi = std::numbers::pi;
enum class ScaleMode { Stretch, Contain, Cover, FitWidth, FitHeight, None, Tile };
struct Point {
double x, y;
};
struct Size {
double width, height;
};
struct Rectangle {
double x, y, width, height;
};
struct Transform {
float rotation{0.0f}; // degrees, applied after shear
float shearX{0.0f};
float shearY{0.0f};
bool IsIdentity() const { return rotation == 0.0f && shearX == 0.0f && shearY == 0.0f; }
// Shear first, then rotation, both centred at (cx, cy) in ctx's current user space.
inline void Apply(cairo_t* ctx, double cx, double cy) const
{
if (IsIdentity()) return;
cairo_translate(ctx, cx, cy);
if (shearX != 0.0f || shearY != 0.0f) {
cairo_matrix_t mat;
cairo_matrix_init(&mat, 1.0, (double)shearY, (double)shearX, 1.0, 0.0, 0.0);
cairo_transform(ctx, &mat);
}
if (rotation != 0.0f)
cairo_rotate(ctx, rotation * Pi / 180.0);
cairo_translate(ctx, -cx, -cy);
}
};
struct Paint {
enum class Type { None, Solid, Linear, Radial, Image };
struct Stop {
double offset, r, g, b, a;
};
Type type{Type::None};
// Solid: r,g,b,a | Linear: x0,y0,x1,y1 | Radial: cx0,cy0,r0,cx1,cy1,r1
double params[6]{};
std::string imagePath{};
std::vector<Stop> stops;
ScaleMode imageScaleMode{ScaleMode::Stretch};
double tileScale{1.0};
Paint() = default;
inline static Paint Solid(double r, double g, double b, double a = 1.0)
{
Paint p;
p.type = Type::Solid;
p.params[0] = r;
p.params[1] = g;
p.params[2] = b;
p.params[3] = a;
return p;
}
// Normalized coords (0–1); multiplied by element bounds in Apply
inline static Paint Linear(double x0, double y0, double x1, double y1)
{
Paint p;
p.type = Type::Linear;
p.params[0] = x0;
p.params[1] = y0;
p.params[2] = x1;
p.params[3] = y1;
return p;
}
// cx/cy normalized by width/height; r normalized by width
inline static Paint Radial(double cx0, double cy0, double r0, double cx1, double cy1, double r1)
{
Paint p;
p.type = Type::Radial;
p.params[0] = cx0;
p.params[1] = cy0;
p.params[2] = r0;
p.params[3] = cx1;
p.params[4] = cy1;
p.params[5] = r1;
return p;
}
inline static Paint Image(const std::string& path)
{
Paint p{};
p.type = Type::Image;
p.imagePath = path;
p.m_patternImage = std::shared_ptr<cairo_surface_t>(
cairo_image_surface_create_from_png(path.c_str()),
cairo_surface_destroy);
return p;
}
inline void AddStop(double offset, double r, double g, double b, double a = 1.0)
{
stops.push_back({offset, r, g, b, a});
}
inline bool Valid() const
{
return type != Type::None;
}
inline void Apply(cairo_t* cr, double w, double h) const
{
if (type == Type::None)
return;
if (type == Type::Solid) {
cairo_set_source_rgba(cr, params[0], params[1], params[2], params[3]);
return;
}
if (type == Type::Image) {
if (!m_patternImage) return;
auto* surface = m_patternImage.get();
if (cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) return;
int sw = cairo_image_surface_get_width(surface);
int sh = cairo_image_surface_get_height(surface);
if (sw <= 0 || sh <= 0) return;
cairo_pattern_t* pat = cairo_pattern_create_for_surface(surface);
cairo_matrix_t m;
if (imageScaleMode == ScaleMode::Tile) {
double ts = (tileScale > 1e-9) ? tileScale : 1.0;
cairo_matrix_init_scale(&m, 1.0 / ts, 1.0 / ts);
cairo_pattern_set_extend(pat, CAIRO_EXTEND_REPEAT);
} else {
double ox = 0.0, oy = 0.0, s = 1.0;
switch (imageScaleMode) {
case ScaleMode::Contain:
s = std::min(w / sw, h / sh);
ox = (w - sw * s) / 2.0;
oy = (h - sh * s) / 2.0;
break;
case ScaleMode::Cover:
s = std::max(w / sw, h / sh);
ox = (w - sw * s) / 2.0;
oy = (h - sh * s) / 2.0;
break;
case ScaleMode::FitWidth:
s = w / sw;
oy = (h - sh * s) / 2.0;
break;
case ScaleMode::FitHeight:
s = h / sh;
ox = (w - sw * s) / 2.0;
break;
case ScaleMode::None:
s = 1.0;
ox = (w - sw) / 2.0;
oy = (h - sh) / 2.0;
break;
default: // Stretch
cairo_matrix_init(&m, (double)sw / w, 0, 0, (double)sh / h, 0, 0);
cairo_pattern_set_matrix(pat, &m);
cairo_set_source(cr, pat);
cairo_pattern_destroy(pat);
return;
}
// All non-Stretch, non-Tile modes: uniform scale s with centering offset
cairo_matrix_init(&m, 1.0 / s, 0, 0, 1.0 / s, -ox / s, -oy / s);
}
cairo_pattern_set_matrix(pat, &m);
cairo_set_source(cr, pat);
cairo_pattern_destroy(pat);
return;
}
cairo_pattern_t* pat;
if (type == Type::Linear) {
pat = cairo_pattern_create_linear(params[0] * w, params[1] * h, params[2] * w,
params[3] * h);
} else {
pat = cairo_pattern_create_radial(params[0] * w, params[1] * h, params[2] * w,
params[3] * w, params[4] * h, params[5] * w);
}
for (const auto& s : stops)
cairo_pattern_add_color_stop_rgba(pat, s.offset, s.r, s.g, s.b, s.a);
cairo_set_source(cr, pat);
cairo_pattern_destroy(pat);
}
protected:
std::shared_ptr<cairo_surface_t> m_patternImage{};
};