epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 10 | #include "SkRectShape.h" |
| 11 | #include "SkCanvas.h" |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 12 | #include "SkFlattenableBuffers.h" |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 13 | |
| 14 | SkPaintShape::SkPaintShape() { |
| 15 | fPaint.setAntiAlias(true); |
| 16 | } |
| 17 | |
| 18 | SkRectShape::SkRectShape() { |
| 19 | fBounds.setEmpty(); |
| 20 | fRadii.set(0, 0); |
| 21 | } |
| 22 | |
| 23 | void SkRectShape::setRect(const SkRect& bounds) { |
| 24 | fBounds = bounds; |
| 25 | fRadii.set(0, 0); |
| 26 | } |
| 27 | |
| 28 | void SkRectShape::setOval(const SkRect& bounds) { |
| 29 | fBounds = bounds; |
| 30 | fRadii.set(-SK_Scalar1, -SK_Scalar1); |
| 31 | } |
| 32 | |
| 33 | void 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 | |
| 38 | void 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 | |
| 52 | void 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.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 64 | void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 65 | this->INHERITED::flatten(buffer); |
| 66 | |
| 67 | buffer.writeRect(fBounds); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 68 | buffer.writeScalar(fRadii.fWidth); |
| 69 | buffer.writeScalar(fRadii.fHeight); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 70 | } |
| 71 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 72 | SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
| 73 | buffer.readRect(&fBounds); |
| 74 | fRadii.fWidth = buffer.readScalar(); |
| 75 | fRadii.fHeight = buffer.readScalar(); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 76 | } |
| 77 | |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 78 | /////////////////////////////////////////////////////////////////////////////// |
| 79 | |
djsollen@google.com | 5492424 | 2012-03-29 15:18:04 +0000 | [diff] [blame] | 80 | void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) const { |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 81 | this->INHERITED::flatten(buffer); |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 82 | buffer.writePaint(fPaint); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 86 | buffer.readPaint(&fPaint); |
reed@android.com | f76bacf | 2009-05-13 14:00:33 +0000 | [diff] [blame] | 87 | } |
| 88 | |
caryclark@google.com | d26147a | 2011-12-15 14:16:43 +0000 | [diff] [blame] | 89 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkRectShape) |
reed@android.com | 0ad336f | 2009-06-29 16:02:20 +0000 | [diff] [blame] | 90 | |