Home | History | Annotate | Download | only in core
      1 /*
      2  * Copyright (C) 2006 The Android Open Source Project
      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 #ifndef SkDraw_DEFINED
     18 #define SkDraw_DEFINED
     19 
     20 #include "SkBitmap.h"
     21 #include "SkCanvas.h"
     22 #include "SkMask.h"
     23 #include "SkMatrix.h"
     24 #include "SkPaint.h"
     25 #include "SkRect.h"
     26 #include "SkAutoKern.h"
     27 
     28 class SkBounder;
     29 class SkClipStack;
     30 class SkDevice;
     31 class SkPath;
     32 class SkRegion;
     33 struct SkDrawProcs;
     34 
     35 class SkDraw {
     36 public:
     37     SkDraw();
     38     SkDraw(const SkDraw& src);
     39 
     40     void    drawPaint(const SkPaint&) const;
     41     void    drawPoints(SkCanvas::PointMode, size_t count, const SkPoint[],
     42                        const SkPaint&, bool forceUseDevice = false) const;
     43     void    drawRect(const SkRect&, const SkPaint&) const;
     44     /**
     45      *  To save on mallocs, we allow a flag that tells us that srcPath is
     46      *  mutable, so that we don't have to make copies of it as we transform it.
     47      *
     48      *  If prePathMatrix is not null, it should logically be applied before any
     49      *  stroking or other effects. If there are no effects on the paint that
     50      *  affect the geometry/rasterization, then the pre matrix can just be
     51      *  pre-concated with the current matrix.
     52      */
     53     void    drawPath(const SkPath& srcPath, const SkPaint&,
     54                      const SkMatrix* prePathMatrix, bool pathIsMutable) const;
     55     void    drawBitmap(const SkBitmap&, const SkMatrix&, const SkPaint&) const;
     56     void    drawSprite(const SkBitmap&, int x, int y, const SkPaint&) const;
     57     void    drawText(const char text[], size_t byteLength, SkScalar x,
     58                      SkScalar y, const SkPaint& paint) const;
     59     void    drawPosText(const char text[], size_t byteLength,
     60                         const SkScalar pos[], SkScalar constY,
     61                         int scalarsPerPosition, const SkPaint& paint) const;
     62     void    drawTextOnPath(const char text[], size_t byteLength,
     63                         const SkPath&, const SkMatrix*, const SkPaint&) const;
     64 #ifdef ANDROID
     65     void    drawPosTextOnPath(const char text[], size_t byteLength,
     66                               const SkPoint pos[], const SkPaint& paint,
     67                               const SkPath& path, const SkMatrix* matrix) const;
     68 #endif
     69     void    drawVertices(SkCanvas::VertexMode mode, int count,
     70                          const SkPoint vertices[], const SkPoint textures[],
     71                          const SkColor colors[], SkXfermode* xmode,
     72                          const uint16_t indices[], int ptCount,
     73                          const SkPaint& paint) const;
     74 
     75     void drawPath(const SkPath& src, const SkPaint& paint) const {
     76         this->drawPath(src, paint, NULL, false);
     77     }
     78 
     79     /** Helper function that creates a mask from a path and an optional maskfilter.
     80         Note however, that the resulting mask will not have been actually filtered,
     81         that must be done afterwards (by calling filterMask). The maskfilter is provided
     82         solely to assist in computing the mask's bounds (if the mode requests that).
     83     */
     84     static bool DrawToMask(const SkPath& devPath, const SkIRect* clipBounds,
     85                            SkMaskFilter* filter, const SkMatrix* filterMatrix,
     86                            SkMask* mask, SkMask::CreateMode mode);
     87 
     88     enum RectType {
     89         kHair_RectType,
     90         kFill_RectType,
     91         kStroke_RectType,
     92         kPath_RectType
     93     };
     94 
     95     /**
     96      *  Based on the paint's style, strokeWidth, and the matrix, classify how
     97      *  to draw the rect. If no special-case is available, returns
     98      *  kPath_RectType.
     99      *
    100      *  Iff RectType == kStroke_RectType, then strokeSize is set to the device
    101      *  width and height of the stroke.
    102      */
    103     static RectType ComputeRectType(const SkPaint&, const SkMatrix&,
    104                                     SkPoint* strokeSize);
    105 
    106 private:
    107     void    drawText_asPaths(const char text[], size_t byteLength,
    108                              SkScalar x, SkScalar y, const SkPaint&) const;
    109     void    drawDevMask(const SkMask& mask, const SkPaint&) const;
    110     void    drawBitmapAsMask(const SkBitmap&, const SkPaint&) const;
    111 
    112 public:
    113     const SkBitmap* fBitmap;        // required
    114     const SkMatrix* fMatrix;        // required
    115     const SkRegion* fClip;          // required
    116 
    117     const SkClipStack* fClipStack;  // optional
    118     SkDevice*       fDevice;        // optional
    119     SkBounder*      fBounder;       // optional
    120     SkDrawProcs*    fProcs;         // optional
    121 
    122     const SkMatrix* fMVMatrix;      // optional
    123     const SkMatrix* fExtMatrix;     // optional
    124 
    125 #ifdef SK_DEBUG
    126     void validate() const;
    127 #else
    128     void validate() const {}
    129 #endif
    130 };
    131 
    132 class SkGlyphCache;
    133 
    134 class SkTextToPathIter {
    135 public:
    136     SkTextToPathIter(const char text[], size_t length, const SkPaint&,
    137                      bool applyStrokeAndPathEffects, bool forceLinearTextOn);
    138     ~SkTextToPathIter();
    139 
    140     const SkPaint&  getPaint() const { return fPaint; }
    141     SkScalar        getPathScale() const { return fScale; }
    142 
    143     const SkPath*   next(SkScalar* xpos);   //!< returns nil when there are no more paths
    144 
    145 private:
    146     SkGlyphCache*   fCache;
    147     SkPaint         fPaint;
    148     SkScalar        fScale;
    149     SkFixed         fPrevAdvance;
    150     const char*     fText;
    151     const char*     fStop;
    152     SkMeasureCacheProc fGlyphCacheProc;
    153 
    154     const SkPath*   fPath;      // returned in next
    155     SkScalar        fXPos;      // accumulated xpos, returned in next
    156     SkAutoKern      fAutoKern;
    157 };
    158 
    159 #endif
    160 
    161 
    162