blob: 06631061c2e9632914efa35ba4148e7890d49fe7 [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"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000012#include "SkFlattenableBuffers.h"
reed@android.comf76bacf2009-05-13 14:00:33 +000013
14SkPaintShape::SkPaintShape() {
15 fPaint.setAntiAlias(true);
16}
17
18SkRectShape::SkRectShape() {
19 fBounds.setEmpty();
20 fRadii.set(0, 0);
21}
22
23void SkRectShape::setRect(const SkRect& bounds) {
24 fBounds = bounds;
25 fRadii.set(0, 0);
26}
27
28void SkRectShape::setOval(const SkRect& bounds) {
29 fBounds = bounds;
30 fRadii.set(-SK_Scalar1, -SK_Scalar1);
31}
32
33void SkRectShape::setCircle(SkScalar cx, SkScalar cy, SkScalar radius) {
34 fBounds.set(cx - radius, cy - radius, cx + radius, cy + radius);
35 fRadii.set(-SK_Scalar1, -SK_Scalar1);
36}
37
38void SkRectShape::setRRect(const SkRect& bounds, SkScalar rx, SkScalar ry) {
39 if (rx < 0) {
40 rx = 0;
41 }
42 if (ry < 0) {
43 ry = 0;
44 }
45
46 fBounds = bounds;
47 fRadii.set(rx, ry);
48}
49
50///////////////////////////////////////////////////////////////////////////////
51
52void SkRectShape::onDraw(SkCanvas* canvas) {
53 const SkPaint& paint = this->paint();
54
55 if (fRadii.fWidth < 0) {
56 canvas->drawOval(fBounds, paint);
57 } else if (fRadii.isZero()) {
58 canvas->drawRect(fBounds, paint);
59 } else {
60 canvas->drawRoundRect(fBounds, fRadii.fWidth, fRadii.fHeight, paint);
61 }
62}
63
djsollen@google.com54924242012-03-29 15:18:04 +000064void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000065 this->INHERITED::flatten(buffer);
66
67 buffer.writeRect(fBounds);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000068 buffer.writeScalar(fRadii.fWidth);
69 buffer.writeScalar(fRadii.fHeight);
reed@android.comf76bacf2009-05-13 14:00:33 +000070}
71
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000072SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
73 buffer.readRect(&fBounds);
74 fRadii.fWidth = buffer.readScalar();
75 fRadii.fHeight = buffer.readScalar();
reed@android.comf76bacf2009-05-13 14:00:33 +000076}
77
reed@android.comf76bacf2009-05-13 14:00:33 +000078///////////////////////////////////////////////////////////////////////////////
79
djsollen@google.com54924242012-03-29 15:18:04 +000080void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000081 this->INHERITED::flatten(buffer);
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000082 buffer.writePaint(fPaint);
reed@android.comf76bacf2009-05-13 14:00:33 +000083}
84
85SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000086 buffer.readPaint(&fPaint);
reed@android.comf76bacf2009-05-13 14:00:33 +000087}
88
caryclark@google.comd26147a2011-12-15 14:16:43 +000089SK_DEFINE_FLATTENABLE_REGISTRAR(SkRectShape)
reed@android.com0ad336f2009-06-29 16:02:20 +000090