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
|
#include "cru/osx/graphics/quartz/Painter.hpp"
#include "cru/common/Logger.hpp"
#include "cru/osx/graphics/quartz/Brush.hpp"
#include "cru/osx/graphics/quartz/Convert.hpp"
#include "cru/osx/graphics/quartz/Geometry.hpp"
#include "cru/osx/graphics/quartz/TextLayout.hpp"
#include "cru/platform/Check.hpp"
#include "cru/platform/Exception.hpp"
#include "cru/platform/graphics/util/Painter.hpp"
namespace cru::platform::graphics::osx::quartz {
QuartzCGContextPainter::QuartzCGContextPainter(
IGraphicsFactory* graphics_factory, CGContextRef cg_context,
bool auto_release, const Size& size,
std::function<void(QuartzCGContextPainter*)> on_end_draw)
: OsxQuartzResource(graphics_factory),
cg_context_(cg_context),
auto_release_(auto_release),
size_(size),
on_end_draw_(std::move(on_end_draw)) {
Expects(cg_context);
log::TagDebug(log_tag,
u"Created with CGContext: {}, Auto Release: {}, Size: {}.",
cg_context, auto_release, size_);
}
QuartzCGContextPainter::~QuartzCGContextPainter() {
DoEndDraw();
if (auto_release_) {
CGContextRelease(cg_context_);
cg_context_ = nullptr;
}
}
Matrix QuartzCGContextPainter::GetTransform() {
return Convert(CGContextGetCTM(cg_context_));
}
void QuartzCGContextPainter::SetTransform(const Matrix& matrix) {
auto old = CGContextGetCTM(cg_context_);
old = CGAffineTransformInvert(old);
CGContextConcatCTM(cg_context_, old);
CGContextConcatCTM(cg_context_, Convert(matrix));
}
void QuartzCGContextPainter::Clear(const Color& color) {
Validate();
CGContextSetRGBFillColor(cg_context_, color.GetFloatRed(),
color.GetFloatGreen(), color.GetFloatBlue(),
color.GetFloatAlpha());
CGContextFillRect(cg_context_, Convert(Rect{Point{}, size_}));
log::TagDebug(log_tag, u"Clear with color {}, size {}.", color, size_);
}
void QuartzCGContextPainter::DrawLine(const Point& start, const Point& end,
IBrush* brush, float width) {
Validate();
CGContextBeginPath(cg_context_);
CGContextMoveToPoint(cg_context_, start.x, start.y);
CGContextAddLineToPoint(cg_context_, end.x, end.y);
QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
b->Select(cg_context_);
CGContextSetLineWidth(cg_context_, width);
CGContextStrokePath(cg_context_);
}
void QuartzCGContextPainter::StrokeRectangle(const Rect& rectangle,
IBrush* brush, float width) {
Validate();
QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
b->Select(cg_context_);
CGContextStrokeRectWithWidth(cg_context_, Convert(rectangle), width);
}
void QuartzCGContextPainter::FillRectangle(const Rect& rectangle,
IBrush* brush) {
Validate();
QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
b->Select(cg_context_);
CGContextFillRect(cg_context_, Convert(rectangle));
}
void QuartzCGContextPainter::StrokeGeometry(IGeometry* geometry, IBrush* brush,
float width) {
Validate();
QuartzGeometry* g = CheckPlatform<QuartzGeometry>(geometry, GetPlatformId());
QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
b->Select(cg_context_);
CGContextSetLineWidth(cg_context_, width);
CGContextBeginPath(cg_context_);
CGContextAddPath(cg_context_, g->GetCGPath());
CGContextStrokePath(cg_context_);
}
void QuartzCGContextPainter::FillGeometry(IGeometry* geometry, IBrush* brush) {
Validate();
QuartzGeometry* g = CheckPlatform<QuartzGeometry>(geometry, GetPlatformId());
QuartzBrush* b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
b->Select(cg_context_);
CGContextBeginPath(cg_context_);
CGContextAddPath(cg_context_, g->GetCGPath());
CGContextFillPath(cg_context_);
}
void QuartzCGContextPainter::DrawText(const Point& offset,
ITextLayout* text_layout, IBrush* brush) {
Validate();
auto tl = CheckPlatform<OsxCTTextLayout>(text_layout, GetPlatformId());
auto b = CheckPlatform<QuartzBrush>(brush, GetPlatformId());
util::WithTransform(this, Matrix::Translation(offset),
[this, tl, b](IPainter*) {
CTFrameDraw(tl->GetCTFrameRef(), cg_context_);
});
}
void QuartzCGContextPainter::PushLayer(const Rect& bounds) {
// TODO: Implement this.
}
void QuartzCGContextPainter::PopLayer() {
// TODO: Implement this.
}
void QuartzCGContextPainter::EndDraw() { DoEndDraw(); }
void QuartzCGContextPainter::DoEndDraw() {
if (cg_context_) {
on_end_draw_(this);
CGContextFlush(cg_context_);
CGContextSynchronize(cg_context_);
log::TagDebug(log_tag, u"End draw and flush.");
}
}
void QuartzCGContextPainter::Validate() {
if (cg_context_ == nullptr)
throw ReuseException(u"QuartzCGContextPainter has already be released.");
}
} // namespace cru::platform::graphics::osx::quartz
|