1
2 /*
3 * 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.
7 */
8
9
10 #include "SkRectShape.h"
11 #include "SkCanvas.h"
12
SkPaintShape()13 SkPaintShape::SkPaintShape() {
14 fPaint.setAntiAlias(true);
15 }
16
SkRectShape()17 SkRectShape::SkRectShape() {
18 fBounds.setEmpty();
19 fRadii.set(0, 0);
20 }
21
setRect(const SkRect & bounds)22 void SkRectShape::setRect(const SkRect& bounds) {
23 fBounds = bounds;
24 fRadii.set(0, 0);
25 }
26
setOval(const SkRect & bounds)27 void SkRectShape::setOval(const SkRect& bounds) {
28 fBounds = bounds;
29 fRadii.set(-SK_Scalar1, -SK_Scalar1);
30 }
31
setCircle(SkScalar cx,SkScalar cy,SkScalar radius)32 void 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
setRRect(const SkRect & bounds,SkScalar rx,SkScalar ry)37 void 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
onDraw(SkCanvas * canvas)51 void 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
getFactory()63 SkFlattenable::Factory SkRectShape::getFactory() {
64 return CreateProc;
65 }
66
flatten(SkFlattenableWriteBuffer & buffer)67 void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) {
68 this->INHERITED::flatten(buffer);
69
70 buffer.writeRect(fBounds);
71 *(SkSize*)buffer.reserve(sizeof(SkSize)) = fRadii;
72 }
73
SkRectShape(SkFlattenableReadBuffer & buffer)74 SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
75 buffer.read(&fBounds, sizeof(fBounds));
76 buffer.read(&fRadii, sizeof(fRadii));
77 }
78
CreateProc(SkFlattenableReadBuffer & buffer)79 SkFlattenable* SkRectShape::CreateProc(SkFlattenableReadBuffer& buffer) {
80 return SkNEW_ARGS(SkRectShape, (buffer));
81 }
82
83 ///////////////////////////////////////////////////////////////////////////////
84
flatten(SkFlattenableWriteBuffer & buffer)85 void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) {
86 this->INHERITED::flatten(buffer);
87
88 fPaint.flatten(buffer);
89 }
90
SkPaintShape(SkFlattenableReadBuffer & buffer)91 SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
92 fPaint.unflatten(buffer);
93 }
94
95 SK_DEFINE_FLATTENABLE_REGISTRAR(SkRectShape)
96
97