1/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "modules/sksg/include/SkSGRect.h"
9
10#include "include/core/SkCanvas.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkPath.h"
13
14namespace sksg {
15
16Rect::Rect(const SkRect& rect) : fRect(rect) {}
17
18void Rect::onClip(SkCanvas* canvas, bool antiAlias) const {
19 canvas->clipRect(fRect, SkClipOp::kIntersect, antiAlias);
20}
21
22void Rect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
23 canvas->drawRect(fRect, paint);
24}
25
26bool Rect::onContains(const SkPoint& p) const {
27 return fRect.contains(p.x(), p.y());
28}
29
30SkRect Rect::onRevalidate(InvalidationController*, const SkMatrix&) {
31 SkASSERT(this->hasInval());
32
33 return fRect;
34}
35
36SkPath Rect::onAsPath() const {
37 SkPath path;
38 path.addRect(fRect, this->getDirection(), this->getInitialPointIndex());
39 return path;
40}
41
42RRect::RRect(const SkRRect& rr) : fRRect(rr) {}
43
44void RRect::onClip(SkCanvas* canvas, bool antiAlias) const {
45 canvas->clipRRect(fRRect, SkClipOp::kIntersect, antiAlias);
46}
47
48void RRect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
49 canvas->drawRRect(fRRect, paint);
50}
51
52bool RRect::onContains(const SkPoint& p) const {
53 if (!fRRect.rect().contains(p.x(), p.y())) {
54 return false;
55 }
56
57 if (fRRect.isRect()) {
58 return true;
59 }
60
61 // TODO: no SkRRect::contains(x, y)
62 return fRRect.contains(SkRect::MakeLTRB(p.x() - SK_ScalarNearlyZero,
63 p.y() - SK_ScalarNearlyZero,
64 p.x() + SK_ScalarNearlyZero,
65 p.y() + SK_ScalarNearlyZero));
66}
67
68SkRect RRect::onRevalidate(InvalidationController*, const SkMatrix&) {
69 SkASSERT(this->hasInval());
70
71 return fRRect.getBounds();
72}
73
74SkPath RRect::onAsPath() const {
75 SkPath path;
76 path.addRRect(fRRect, this->getDirection(), this->getInitialPointIndex());
77 return path;
78}
79
80} // namespace sksg
81