Home | History | Annotate | Download | only in effects
      1 /*
      2     Copyright 2011 Google Inc.
      3 
      4     Licensed under the Apache License, Version 2.0 (the "License");
      5     you may not use this file except in compliance with the License.
      6     You may obtain a copy of the License at
      7 
      8     http://www.apache.org/licenses/LICENSE-2.0
      9 
     10     Unless required by applicable law or agreed to in writing, software
     11     distributed under the License is distributed on an "AS IS" BASIS,
     12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13     See the License for the specific language governing permissions and
     14     limitations under the License.
     15  */
     16 
     17 #include "SkRectShape.h"
     18 #include "SkCanvas.h"
     19 
     20 SkPaintShape::SkPaintShape() {
     21     fPaint.setAntiAlias(true);
     22 }
     23 
     24 SkRectShape::SkRectShape() {
     25     fBounds.setEmpty();
     26     fRadii.set(0, 0);
     27 }
     28 
     29 void SkRectShape::setRect(const SkRect& bounds) {
     30     fBounds = bounds;
     31     fRadii.set(0, 0);
     32 }
     33 
     34 void SkRectShape::setOval(const SkRect& bounds) {
     35     fBounds = bounds;
     36     fRadii.set(-SK_Scalar1, -SK_Scalar1);
     37 }
     38 
     39 void SkRectShape::setCircle(SkScalar cx, SkScalar cy, SkScalar radius) {
     40     fBounds.set(cx - radius, cy - radius, cx + radius, cy + radius);
     41     fRadii.set(-SK_Scalar1, -SK_Scalar1);
     42 }
     43 
     44 void SkRectShape::setRRect(const SkRect& bounds, SkScalar rx, SkScalar ry) {
     45     if (rx < 0) {
     46         rx = 0;
     47     }
     48     if (ry < 0) {
     49         ry = 0;
     50     }
     51 
     52     fBounds = bounds;
     53     fRadii.set(rx, ry);
     54 }
     55 
     56 ///////////////////////////////////////////////////////////////////////////////
     57 
     58 void SkRectShape::onDraw(SkCanvas* canvas) {
     59     const SkPaint& paint = this->paint();
     60 
     61     if (fRadii.fWidth < 0) {
     62         canvas->drawOval(fBounds, paint);
     63     } else if (fRadii.isZero()) {
     64         canvas->drawRect(fBounds, paint);
     65     } else {
     66         canvas->drawRoundRect(fBounds, fRadii.fWidth, fRadii.fHeight, paint);
     67     }
     68 }
     69 
     70 SkFlattenable::Factory SkRectShape::getFactory() {
     71     return CreateProc;
     72 }
     73 
     74 void SkRectShape::flatten(SkFlattenableWriteBuffer& buffer) {
     75     this->INHERITED::flatten(buffer);
     76 
     77     buffer.writeRect(fBounds);
     78     *(SkSize*)buffer.reserve(sizeof(SkSize)) = fRadii;
     79 }
     80 
     81 SkRectShape::SkRectShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
     82     buffer.read(&fBounds, sizeof(fBounds));
     83     buffer.read(&fRadii, sizeof(fRadii));
     84 }
     85 
     86 SkFlattenable* SkRectShape::CreateProc(SkFlattenableReadBuffer& buffer) {
     87     return SkNEW_ARGS(SkRectShape, (buffer));
     88 }
     89 
     90 ///////////////////////////////////////////////////////////////////////////////
     91 
     92 void SkPaintShape::flatten(SkFlattenableWriteBuffer& buffer) {
     93     this->INHERITED::flatten(buffer);
     94 
     95     fPaint.flatten(buffer);
     96 }
     97 
     98 SkPaintShape::SkPaintShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) {
     99     fPaint.unflatten(buffer);
    100 }
    101 
    102 static SkFlattenable::Registrar gReg("SkRectShape", SkRectShape::CreateProc);
    103 
    104