aboutsummaryrefslogtreecommitdiff
path: root/src/osx/graphics/quartz/Painter.cpp
blob: 6effe9f94135d3e2ac134922bf65599a86ef68d2 (plain)
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
#include "cru/osx/graphics/quartz/Painter.hpp"

#include "cru/osx/graphics/quartz/Brush.hpp"
#include "cru/osx/graphics/quartz/Convert.hpp"
#include "cru/osx/graphics/quartz/Geometry.hpp"
#include "cru/platform/Check.hpp"
#include "cru/platform/Exception.hpp"

namespace cru::platform::graphics::osx::quartz {
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();

  CGColorRef c =
      CGColorCreateGenericRGB(color.GetFloatRed(), color.GetFloatGreen(),
                              color.GetFloatBlue(), color.GetFloatAlpha());
  CGContextSetFillColorWithColor(cg_context_, c);
  CGColorRelease(c);

  CGContextFillRect(cg_context_, Convert(Rect{Point{}, 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) {
  // TODO: Implement this.
}

void QuartzCGContextPainter::PushLayer(const Rect& bounds) {
  // TODO: Implement this.
}

void QuartzCGContextPainter::PopLayer() {
  // TODO: Implement this.
}

void QuartzCGContextPainter::EndDraw() {
  CGContextRelease(cg_context_);
  CGContextFlush(cg_context_);
  cg_context_ = nullptr;
}

void QuartzCGContextPainter::Validate() {
  if (cg_context_ == nullptr)
    throw ReuseException(u"QuartzCGContextPainter has already be released.");
}
}  // namespace cru::platform::graphics::osx::quartz