1 /*
2  * Copyright 2011 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 
9 #ifndef SkPDFShader_DEFINED
10 #define SkPDFShader_DEFINED
11 
12 #include "SkBitmapKey.h"
13 #include "SkPDFTypes.h"
14 #include "SkShader.h"
15 
16 class SkPDFCanon;
17 class SkPDFDocument;
18 class SkMatrix;
19 struct SkIRect;
20 
21 /** \class SkPDFShader
22 
23     In PDF parlance, this is a pattern, used in place of a color when the
24     pattern color space is selected.
25 */
26 
27 class SkPDFShader {
28 public:
29     /** Get the PDF shader for the passed SkShader. If the SkShader is
30      *  invalid in some way, returns nullptr. The reference count of
31      *  the object is incremented and it is the caller's responsibility to
32      *  unreference it when done.  This is needed to accommodate the weak
33      *  reference pattern used when the returned object is new and has no
34      *  other references.
35      *  @param shader      The SkShader to emulate.
36      *  @param matrix      The current transform. (PDF shaders are absolutely
37      *                     positioned, relative to where the page is drawn.)
38      *  @param surfceBBox  The bounding box of the drawing surface (with matrix
39      *                     already applied).
40      *  @param rasterScale Additional scale to be applied for early
41      *                     rasterization.
42      */
43     static sk_sp<SkPDFObject> GetPDFShader(SkPDFDocument* doc,
44                                            SkScalar dpi,
45                                            SkShader* shader,
46                                            const SkMatrix& matrix,
47                                            const SkIRect& surfaceBBox,
48                                            SkScalar rasterScale);
49 
50     static sk_sp<SkPDFArray> MakeRangeObject();
51 
52     class State {
53     public:
54         SkShader::GradientType fType;
55         SkShader::GradientInfo fInfo;
56         std::unique_ptr<SkColor[]> fColors;
57         std::unique_ptr<SkScalar[]> fStops;
58         SkMatrix fCanvasTransform;
59         SkMatrix fShaderTransform;
60         SkIRect fBBox;
61 
62         SkBitmapKey fBitmapKey;
63         SkShader::TileMode fImageTileModes[2];
64 
65         State(SkShader* shader, const SkMatrix& canvasTransform,
66               const SkIRect& bbox, SkScalar rasterScale,
67               SkBitmap* dstImage);
68 
69         bool operator==(const State& b) const;
70 
71         State MakeAlphaToLuminosityState() const;
72         State MakeOpaqueState() const;
73 
74         bool GradientHasAlpha() const;
75 
76         State(State&&) = default;
77         State& operator=(State&&) = default;
78 
79     private:
80         State(const State& other);
81         State& operator=(const State& rhs);
82         void allocateGradientInfoStorage();
83     };
84 };
85 
86 #endif
87