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
14 namespace sksg {
15
Rect(const SkRect & rect)16 Rect::Rect(const SkRect& rect) : fRect(rect) {}
17
onClip(SkCanvas * canvas,bool antiAlias) const18 void Rect::onClip(SkCanvas* canvas, bool antiAlias) const {
19 canvas->clipRect(fRect, SkClipOp::kIntersect, antiAlias);
20 }
21
onDraw(SkCanvas * canvas,const SkPaint & paint) const22 void Rect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
23 canvas->drawRect(fRect, paint);
24 }
25
onContains(const SkPoint & p) const26 bool Rect::onContains(const SkPoint& p) const {
27 return fRect.contains(p.x(), p.y());
28 }
29
onRevalidate(InvalidationController *,const SkMatrix &)30 SkRect Rect::onRevalidate(InvalidationController*, const SkMatrix&) {
31 SkASSERT(this->hasInval());
32
33 return fRect;
34 }
35
onAsPath() const36 SkPath Rect::onAsPath() const {
37 return SkPath::Rect(fRect, this->getDirection(), this->getInitialPointIndex());
38 }
39
RRect(const SkRRect & rr)40 RRect::RRect(const SkRRect& rr) : fRRect(rr) {}
41
onClip(SkCanvas * canvas,bool antiAlias) const42 void RRect::onClip(SkCanvas* canvas, bool antiAlias) const {
43 canvas->clipRRect(fRRect, SkClipOp::kIntersect, antiAlias);
44 }
45
onDraw(SkCanvas * canvas,const SkPaint & paint) const46 void RRect::onDraw(SkCanvas* canvas, const SkPaint& paint) const {
47 canvas->drawRRect(fRRect, paint);
48 }
49
onContains(const SkPoint & p) const50 bool RRect::onContains(const SkPoint& p) const {
51 if (!fRRect.rect().contains(p.x(), p.y())) {
52 return false;
53 }
54
55 if (fRRect.isRect()) {
56 return true;
57 }
58
59 // TODO: no SkRRect::contains(x, y)
60 return fRRect.contains(SkRect::MakeLTRB(p.x() - SK_ScalarNearlyZero,
61 p.y() - SK_ScalarNearlyZero,
62 p.x() + SK_ScalarNearlyZero,
63 p.y() + SK_ScalarNearlyZero));
64 }
65
onRevalidate(InvalidationController *,const SkMatrix &)66 SkRect RRect::onRevalidate(InvalidationController*, const SkMatrix&) {
67 SkASSERT(this->hasInval());
68
69 return fRRect.getBounds();
70 }
71
onAsPath() const72 SkPath RRect::onAsPath() const {
73 return SkPath::RRect(fRRect, this->getDirection(), this->getInitialPointIndex());
74 }
75
76 } // namespace sksg
77