1 /*
2  * Copyright 2015 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkLatticeIter_DEFINED
9 #define SkLatticeIter_DEFINED
10 
11 #include "SkCanvas.h"
12 #include "SkScalar.h"
13 #include "SkTArray.h"
14 
15 struct SkIRect;
16 struct SkRect;
17 
18 /**
19  *  Disect a lattice request into an sequence of src-rect / dst-rect pairs
20  */
21 class SK_API SkLatticeIter {
22 public:
23 
24     static bool Valid(int imageWidth, int imageHeight, const SkCanvas::Lattice& lattice);
25 
26     SkLatticeIter(const SkCanvas::Lattice& lattice, const SkRect& dst);
27 
28     static bool Valid(int imageWidth, int imageHeight, const SkIRect& center);
29 
30     SkLatticeIter(int imageWidth, int imageHeight, const SkIRect& center, const SkRect& dst);
31 
32     /**
33      *  While it returns true, use src/dst to draw the image/bitmap. Optional parameters
34      *  isFixedColor and fixedColor specify if the rectangle is filled with a fixed color.
35      *  If (*isFixedColor) is true, then (*fixedColor) contains the rectangle color.
36      */
37     bool next(SkIRect* src, SkRect* dst, bool* isFixedColor = nullptr,
38               SkColor* fixedColor = nullptr);
39 
40     /** Version of above that converts the integer src rect to a scalar rect. */
41     bool next(SkRect* src, SkRect* dst, bool* isFixedColor = nullptr,
42               SkColor* fixedColor = nullptr) {
43         SkIRect isrcR;
44         if (this->next(&isrcR, dst, isFixedColor, fixedColor)) {
45             *src = SkRect::Make(isrcR);
46             return true;
47         }
48         return false;
49     }
50 
51     /**
52      *  Apply a matrix to the dst points.
53      */
54     void mapDstScaleTranslate(const SkMatrix& matrix);
55 
56     /**
57      *  Returns the number of rects that will actually be drawn.
58      */
numRectsToDraw()59     int numRectsToDraw() const {
60         return fNumRectsToDraw;
61     }
62 
63 private:
64     SkTArray<int> fSrcX;
65     SkTArray<int> fSrcY;
66     SkTArray<SkScalar> fDstX;
67     SkTArray<SkScalar> fDstY;
68     SkTArray<SkCanvas::Lattice::RectType> fRectTypes;
69     SkTArray<SkColor> fColors;
70 
71     int  fCurrX;
72     int  fCurrY;
73     int  fNumRectsInLattice;
74     int  fNumRectsToDraw;
75 };
76 
77 #endif
78