blob: 68d1e1f7ffe4ca5b193764fff99137af49460d11 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.comf76bacf2009-05-13 14:00:33 +000010#include "SkRectShape.h"
11#include "SkCanvas.h"
12
13SkPaintShape::SkPaintShape() {
14 fPaint.setAntiAlias(true);
15}
16
17SkRectShape::SkRectShape() {
18 fBounds.setEmpty();
19 fRadii.set(0, 0);
20}
21
22void SkRectShape::setRect(const SkRect& bounds) {
23 fBounds = bounds;
24 fRadii.set(0, 0);
25}
26
27void SkRectShape::setOval(const SkRect& bounds) {
28 fBounds = bounds;
29 fRadii.set(-SK_Scalar1, -SK_Scalar1);
30}
31
32void SkRectShape::setCircle(SkScalar cx, SkScalar cy, SkScalar radius) {
33 fBounds.set(cx - radius, cy - radius, cx + radius, cy + radius);
34 fRadii.set(-SK_Scalar1, -SK_Scalar1);
35}
36
37void SkRectShape::setRRect(const SkRect& bounds, SkScalar rx, SkScalar ry) {
38 if (rx < 0) {
39 rx = 0;
40 }
41 if (ry < 0) {
42 ry = 0;
43 }
44
45 fBounds = bounds;
46 fRadii.set(rx, ry);
47}
48
49///////////////////////////////////////////////////////////////////////////////
50
51void SkRectShape::onDraw(SkCanvas* canvas) {
52 const SkPaint& paint = this->paint();
53
54 if (fRadii.fWidth < 0) {
55 canvas->drawOval(fBounds, paint);
56 } else if (fRadii.isZero()) {
57 canvas->drawRect(fBounds, paint);
58 } else {
59 canvas->drawRoundRect(fBounds, fRadii.fWidth, fRadii.fHeight, paint);
60 }
61}
62
djsollen@google.com54924242012-03-29 15:18:04 +000063void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000064 this->INHERITED::flatten(buffer);
65
66 buffer.writeRect(fBounds);
67 *(SkSize*)buffer.reserve(sizeof(SkSize)) = fRadii;
68}
69
70SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
71 buffer.read(&fBounds, sizeof(fBounds));
72 buffer.read(&fRadii, sizeof(fRadii));
73}
74
reed@android.comf76bacf2009-05-13 14:00:33 +000075///////////////////////////////////////////////////////////////////////////////
76
djsollen@google.com54924242012-03-29 15:18:04 +000077void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000078 this->INHERITED::flatten(buffer);
79
80 fPaint.flatten(buffer);
81}
82
83SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
84 fPaint.unflatten(buffer);
85}
86
caryclark@google.comd26147a2011-12-15 14:16:43 +000087SK_DEFINE_FLATTENABLE_REGISTRAR(SkRectShape)
reed@android.com0ad336f2009-06-29 16:02:20 +000088