typeset 0.1.0
縦書き・横書きの日本語組版ライブラリ(JLReq 水準、ラスタ / PDF / SVG)
読み取り中…
検索中…
一致する文字列を見つけられません
geom.hpp
[詳解]
1#ifndef TYPESET_GEOM_HPP
2#define TYPESET_GEOM_HPP
3
4#include <algorithm>
5#include <cmath>
6#include <cstdint>
7#include <vector>
8
15namespace typeset {
16
17using Pt = float;
18
19constexpr Pt kInch = 72.0f;
20constexpr Pt kMm = 72.0f / 25.4f;
21constexpr Pt kCm = kMm * 10.0f;
22
23struct Point {
24 Pt x = 0.0f;
25 Pt y = 0.0f;
26};
27
28struct Size {
29 Pt w = 0.0f;
30 Pt h = 0.0f;
31};
32
33struct Rect {
34 Pt x = 0.0f;
35 Pt y = 0.0f;
36 Pt w = 0.0f;
37 Pt h = 0.0f;
38
39 Pt right() const { return x + w; }
40 Pt bottom() const { return y + h; }
41 bool empty() const { return w <= 0.0f || h <= 0.0f; }
42 bool contains(Point p) const {
43 return p.x >= x && p.x < right() && p.y >= y && p.y < bottom();
44 }
45 Rect intersect(const Rect& o) const;
46 Rect unite(const Rect& o) const;
47};
48
54struct Mat2 {
55 float xx = 1.0f, xy = 0.0f;
56 float yx = 0.0f, yy = 1.0f;
57
58 bool isIdentity() const {
59 return xx == 1.0f && xy == 0.0f && yx == 0.0f && yy == 1.0f;
60 }
61};
62
68struct Matrix {
69 float xx = 1.0f, xy = 0.0f, dx = 0.0f;
70 float yx = 0.0f, yy = 1.0f, dy = 0.0f;
71
72 static Matrix identity() { return Matrix{}; }
73 static Matrix translation(Pt tx, Pt ty) {
74 Matrix m; m.dx = tx; m.dy = ty; return m;
75 }
77 static Matrix rotation(float radians) {
78 const float c = std::cos(radians), s = std::sin(radians);
79 Matrix m;
80 m.xx = c; m.xy = -s;
81 m.yx = s; m.yy = c;
82 return m;
83 }
84 static Matrix scaling(float sx, float sy) {
85 Matrix m; m.xx = sx; m.yy = sy; return m;
86 }
87 static Matrix fromMat2(const Mat2& r, Pt tx = 0.0f, Pt ty = 0.0f) {
88 Matrix m;
89 m.xx = r.xx; m.xy = r.xy; m.yx = r.yx; m.yy = r.yy;
90 m.dx = tx; m.dy = ty;
91 return m;
92 }
93
94 bool isIdentity() const {
95 return xx == 1.0f && xy == 0.0f && dx == 0.0f &&
96 yx == 0.0f && yy == 1.0f && dy == 0.0f;
97 }
98 Mat2 linear() const { return Mat2{xx, xy, yx, yy}; }
99 float determinant() const { return xx * yy - xy * yx; }
100 Point apply(Point p) const {
101 return Point{xx * p.x + xy * p.y + dx, yx * p.x + yy * p.y + dy};
102 }
104 return Point{xx * v.x + xy * v.y, yx * v.x + yy * v.y};
105 }
106};
107
109Matrix multiply(const Matrix& a, const Matrix& b);
110Mat2 multiply(const Mat2& a, const Mat2& b);
112Matrix invert(const Matrix& m, bool* ok = nullptr);
113
117struct Color {
118 uint8_t r = 0, g = 0, b = 0, a = 255;
119
120 static Color rgb(uint8_t r, uint8_t g, uint8_t b) { return Color{r, g, b, 255}; }
121 static Color rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { return Color{r, g, b, a}; }
122 static Color argb(uint32_t v) {
123 return Color{static_cast<uint8_t>((v >> 16) & 0xFF), static_cast<uint8_t>((v >> 8) & 0xFF),
124 static_cast<uint8_t>(v & 0xFF), static_cast<uint8_t>((v >> 24) & 0xFF)};
125 }
126 uint32_t toArgb() const {
127 return (static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(r) << 16) |
128 (static_cast<uint32_t>(g) << 8) | static_cast<uint32_t>(b);
129 }
130 bool operator==(const Color& o) const { return r == o.r && g == o.g && b == o.b && a == o.a; }
131 bool operator!=(const Color& o) const { return !(*this == o); }
132};
133
138 float offset = 0.0f;
140 bool operator==(const GradientStop& o) const { return offset == o.offset && color == o.color; }
141};
142
143enum class PaintKind : uint8_t { Solid, Linear, Radial };
144
150enum class PaintUnits : uint8_t { BoundingBox, UserSpace };
151
158struct Paint {
160 Color color{0, 0, 0, 255};
163 Point end{1.0f, 0.0f};
164 float radius = 0.5f;
165 std::vector<GradientStop> stops;
166
167 Paint() = default;
168 Paint(Color c) : color(c) {} // 単色は暗黙変換
169
170 static Paint linear(Point from, Point to, std::vector<GradientStop> stops,
172 Paint p;
174 p.start = from;
175 p.end = to;
176 p.stops = std::move(stops);
177 p.units = units;
178 return p;
179 }
180 static Paint radial(Point center, float radius, std::vector<GradientStop> stops,
182 Paint p;
184 p.start = center;
185 p.radius = radius;
186 p.stops = std::move(stops);
187 p.units = units;
188 return p;
189 }
190
191 bool isGradient() const { return kind != PaintKind::Solid && stops.size() >= 2; }
192
194 Color at(float t) const {
195 if (stops.empty()) return color;
196 if (t <= stops.front().offset) return stops.front().color;
197 if (t >= stops.back().offset) return stops.back().color;
198 for (size_t i = 1; i < stops.size(); ++i) {
199 if (t > stops[i].offset) continue;
200 const GradientStop& a = stops[i - 1];
201 const GradientStop& b = stops[i];
202 const float span = b.offset - a.offset;
203 const float u = span > 0.0f ? (t - a.offset) / span : 0.0f;
204 auto mix = [&](uint8_t x, uint8_t y) {
205 return static_cast<uint8_t>(x + (static_cast<float>(y) - x) * u + 0.5f);
206 };
207 return Color{mix(a.color.r, b.color.r), mix(a.color.g, b.color.g),
208 mix(a.color.b, b.color.b), mix(a.color.a, b.color.a)};
209 }
210 return stops.back().color;
211 }
212
214 Color solid() const { return isGradient() ? at(0.5f) : color; }
216 uint8_t maxAlpha() const {
217 if (!isGradient()) return color.a;
218 uint8_t a = 0;
219 for (const GradientStop& s : stops) a = std::max(a, s.color.a);
220 return a;
221 }
222
223 bool operator==(const Paint& o) const {
224 return kind == o.kind && color == o.color && units == o.units &&
225 start.x == o.start.x && start.y == o.start.y && end.x == o.end.x && end.y == o.end.y &&
226 radius == o.radius && stops == o.stops;
227 }
228 bool operator!=(const Paint& o) const { return !(*this == o); }
229};
230
231enum class StrokeJoin : uint8_t { Miter, Round, Bevel };
232enum class StrokeCap : uint8_t { Butt, Round, Square };
233
237struct Stroke {
239 Pt width = 1.0f;
242 float miterLimit = 4.0f;
243
244 bool operator==(const Stroke& o) const {
245 return color == o.color && width == o.width && join == o.join && cap == o.cap &&
247 }
248 bool operator!=(const Stroke& o) const { return !(*this == o); }
249};
250
256struct Path {
257 enum class Cmd : uint8_t { Move, Line, Quad, Cubic, Close };
258
259 std::vector<Cmd> cmds;
260 std::vector<Point> pts;
261
262 bool empty() const { return cmds.empty(); }
263
264 void moveTo(Pt x, Pt y) { cmds.push_back(Cmd::Move); pts.push_back({x, y}); }
265 void lineTo(Pt x, Pt y) { cmds.push_back(Cmd::Line); pts.push_back({x, y}); }
266 void quadTo(Pt cx, Pt cy, Pt x, Pt y) {
267 cmds.push_back(Cmd::Quad); pts.push_back({cx, cy}); pts.push_back({x, y});
268 }
269 void cubicTo(Pt c1x, Pt c1y, Pt c2x, Pt c2y, Pt x, Pt y) {
270 cmds.push_back(Cmd::Cubic);
271 pts.push_back({c1x, c1y}); pts.push_back({c2x, c2y}); pts.push_back({x, y});
272 }
273 void close() { cmds.push_back(Cmd::Close); }
274
275 void addRect(const Rect& r);
277 void addEllipse(const Rect& r);
278 void addLine(Point a, Point b) { moveTo(a.x, a.y); lineTo(b.x, b.y); }
279
280 Path transformed(const Matrix& m) const;
283};
284
286std::vector<std::vector<Point>> flattenPath(const Path& path, float tolerance,
287 std::vector<bool>* closedFlags = nullptr);
288
289} // namespace typeset
290
291#endif // TYPESET_GEOM_HPP
geom — 単位と幾何
float Pt
Definition geom.hpp:17
PaintKind
Definition geom.hpp:143
StrokeJoin
Definition geom.hpp:231
StrokeCap
Definition geom.hpp:232
Matrix multiply(const Matrix &a, const Matrix &b)
a を後から適用する合成(結果 = a ∘ b。b で写してから a)
constexpr Pt kMm
Definition geom.hpp:20
std::vector< std::vector< Point > > flattenPath(const Path &path, float tolerance, std::vector< bool > *closedFlags=nullptr)
曲線を折れ線に平坦化した閉/開多角形列を得る(tolerance は pt)
Matrix invert(const Matrix &m, bool *ok=nullptr)
逆行列。特異なら単位行列を返し ok=false
constexpr Pt kInch
Definition geom.hpp:19
constexpr Pt kCm
Definition geom.hpp:21
PaintUnits
グラデーションの座標系
Definition geom.hpp:150
色(非前乗算 RGBA)
Definition geom.hpp:117
uint32_t toArgb() const
Definition geom.hpp:126
static Color rgb(uint8_t r, uint8_t g, uint8_t b)
Definition geom.hpp:120
bool operator==(const Color &o) const
Definition geom.hpp:130
uint8_t g
Definition geom.hpp:118
static Color rgba(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
Definition geom.hpp:121
static Color argb(uint32_t v)
Definition geom.hpp:122
bool operator!=(const Color &o) const
Definition geom.hpp:131
uint8_t a
Definition geom.hpp:118
uint8_t r
Definition geom.hpp:118
uint8_t b
Definition geom.hpp:118
グラデーションの停止点
Definition geom.hpp:137
bool operator==(const GradientStop &o) const
Definition geom.hpp:140
float offset
0〜1
Definition geom.hpp:138
2x2 行列(平行移動なし)。グリフ固有の変形に使う。 x' = xx*x + xy*y y' = yx*x + yy*y
Definition geom.hpp:54
float xx
Definition geom.hpp:55
float xy
Definition geom.hpp:55
float yy
Definition geom.hpp:56
float yx
Definition geom.hpp:56
bool isIdentity() const
Definition geom.hpp:58
2x3 アフィン行列(行優先) x' = xx*x + xy*y + dx y' = yx*x + yy*y + dy
Definition geom.hpp:68
static Matrix scaling(float sx, float sy)
Definition geom.hpp:84
static Matrix rotation(float radians)
回転(ラジアン。y-down なので画面上では時計回りが正)
Definition geom.hpp:77
float determinant() const
Definition geom.hpp:99
static Matrix translation(Pt tx, Pt ty)
Definition geom.hpp:73
Point apply(Point p) const
Definition geom.hpp:100
bool isIdentity() const
Definition geom.hpp:94
static Matrix identity()
Definition geom.hpp:72
Mat2 linear() const
Definition geom.hpp:98
Point applyVector(Point v) const
Definition geom.hpp:103
static Matrix fromMat2(const Mat2 &r, Pt tx=0.0f, Pt ty=0.0f)
Definition geom.hpp:87
塗り — 単色または線形/放射グラデーション
Definition geom.hpp:158
Color color
Solid のときの色
Definition geom.hpp:160
PaintKind kind
Definition geom.hpp:159
bool operator==(const Paint &o) const
Definition geom.hpp:223
Paint()=default
Paint(Color c)
Definition geom.hpp:168
Color solid() const
単色として扱うときの色(グラデーションは中間の色)
Definition geom.hpp:214
Point end
Linear: 終点
Definition geom.hpp:163
bool isGradient() const
Definition geom.hpp:191
bool operator!=(const Paint &o) const
Definition geom.hpp:228
std::vector< GradientStop > stops
offset の昇順
Definition geom.hpp:165
float radius
Radial: 半径
Definition geom.hpp:164
static Paint linear(Point from, Point to, std::vector< GradientStop > stops, PaintUnits units=PaintUnits::BoundingBox)
Definition geom.hpp:170
PaintUnits units
Definition geom.hpp:161
Point start
Linear: 始点/Radial: 中心
Definition geom.hpp:162
static Paint radial(Point center, float radius, std::vector< GradientStop > stops, PaintUnits units=PaintUnits::BoundingBox)
Definition geom.hpp:180
Color at(float t) const
t(0〜1)の色。停止点の間を線形補間する
Definition geom.hpp:194
uint8_t maxAlpha() const
不透明度の代表値(グラデーションは最大)
Definition geom.hpp:216
パス(moveTo / lineTo / quadTo / cubicTo / close)
Definition geom.hpp:256
void addRect(const Rect &r)
Rect controlBounds() const
制御点を含むバウンディングボックス
bool empty() const
Definition geom.hpp:262
void addEllipse(const Rect &r)
楕円(矩形に内接)を 4 本の 3 次ベジェで近似
void cubicTo(Pt c1x, Pt c1y, Pt c2x, Pt c2y, Pt x, Pt y)
Definition geom.hpp:269
std::vector< Point > pts
Definition geom.hpp:260
Path transformed(const Matrix &m) const
void lineTo(Pt x, Pt y)
Definition geom.hpp:265
std::vector< Cmd > cmds
Definition geom.hpp:259
void quadTo(Pt cx, Pt cy, Pt x, Pt y)
Definition geom.hpp:266
void addLine(Point a, Point b)
Definition geom.hpp:278
void close()
Definition geom.hpp:273
void moveTo(Pt x, Pt y)
Definition geom.hpp:264
Rect intersect(const Rect &o) const
bool empty() const
Definition geom.hpp:41
bool contains(Point p) const
Definition geom.hpp:42
Pt bottom() const
Definition geom.hpp:40
Pt right() const
Definition geom.hpp:39
Rect unite(const Rect &o) const
線の描き方
Definition geom.hpp:237
StrokeCap cap
Definition geom.hpp:241
StrokeJoin join
Definition geom.hpp:240
Paint color
線の塗り(単色または グラデーション。Color から暗黙に作れる)
Definition geom.hpp:238
bool operator==(const Stroke &o) const
Definition geom.hpp:244
bool operator!=(const Stroke &o) const
Definition geom.hpp:248
float miterLimit
Definition geom.hpp:242