aboutsummaryrefslogtreecommitdiff
path: root/src/osx/graphics/quartz/Geometry.cpp
blob: 428b24ab77355cf7ddfb546bbbc863e9aace3be7 (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
#include "cru/osx/graphics/quartz/Geometry.hpp"

#include <memory>

namespace cru::platform::graphics::osx::quartz {
QuartzGeometry::QuartzGeometry(IGraphFactory* graphics_factory,
                               CGContextRef cg_context, CGPathRef cg_path)
    : OsxQuartzResource(graphics_factory),
      cg_context_(cg_context),
      cg_path_(cg_path) {}

QuartzGeometry::~QuartzGeometry() { CGPathRelease(cg_path_); }

bool QuartzGeometry::FillContains(const Point& point) {
  CGContextBeginPath(cg_context_);
  CGContextAddPath(cg_context_, cg_path_);
  return CGContextPathContainsPoint(cg_context_, CGPoint{point.x, point.y},
                                    kCGPathFill);
}

QuartzGeometryBuilder::QuartzGeometryBuilder(IGraphFactory* graphics_factory,
                                             CGContextRef cg_context)
    : OsxQuartzResource(graphics_factory), cg_context_(cg_context) {}

std::unique_ptr<IGeometry> QuartzGeometryBuilder::Build() {
  CGContextBeginPath(cg_context_);

  for (const auto& action : actions_) {
    Object* o = action.get();

    if (auto a = dynamic_cast<details::GeometryBeginFigureAction*>(o)) {
      CGContextMoveToPoint(cg_context_, a->point.x, a->point.y);
    } else if (auto a = dynamic_cast<details::GeometryCloseFigureAction*>(o)) {
      if (a->close) CGContextClosePath(cg_context_);
    } else if (auto a = dynamic_cast<details::GeometryLineToAction*>(o)) {
      CGContextAddLineToPoint(cg_context_, a->point.x, a->point.y);
    } else if (auto a =
                   dynamic_cast<details::GeometryQuadraticBezierToAction*>(o)) {
      CGContextAddQuadCurveToPoint(cg_context_, a->control_point.x,
                                   a->control_point.y, a->end_point.x,
                                   a->end_point.y);
    }
  }

  return std::make_unique<QuartzGeometry>(GetGraphFactory(), cg_context_,
                                          CGContextCopyPath(cg_context_));
}
}  // namespace cru::platform::graphics::osx::quartz