1 
2 /*
3  * Copyright 2010 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #ifndef SkPDFGraphicState_DEFINED
11 #define SkPDFGraphicState_DEFINED
12 
13 #include "SkPaint.h"
14 #include "SkPDFTypes.h"
15 #include "SkTemplates.h"
16 #include "SkChecksum.h"
17 
18 class SkPDFCanon;
19 class SkPDFFormXObject;
20 
21 /** \class SkPDFGraphicState
22     SkPaint objects roughly correspond to graphic state dictionaries that can
23     be installed. So that a given dictionary is only output to the pdf file
24     once, we want to canonicalize them.
25 */
26 class SkPDFGraphicState : public SkPDFObject {
27     SK_DECLARE_INST_COUNT(SkPDFGraphicState)
28 public:
29     enum SkPDFSMaskMode {
30         kAlpha_SMaskMode,
31         kLuminosity_SMaskMode
32     };
33 
34     // Override emitObject so that we can populate the dictionary on
35     // demand.
36     virtual void emitObject(SkWStream* stream,
37                             const SkPDFObjNumMap& objNumMap,
38                             const SkPDFSubstituteMap& substitutes);
39 
40     /** Get the graphic state for the passed SkPaint. The reference count of
41      *  the object is incremented and it is the caller's responsibility to
42      *  unreference it when done. This is needed to accommodate the weak
43      *  reference pattern used when the returned object is new and has no
44      *  other references.
45      *  @param paint  The SkPaint to emulate.
46      */
47     static SkPDFGraphicState* GetGraphicStateForPaint(SkPDFCanon* canon,
48                                                       const SkPaint& paint);
49 
50     /** Make a graphic state that only sets the passed soft mask. The
51      *  reference count of the object is incremented and it is the caller's
52      *  responsibility to unreference it when done.
53      *  @param sMask     The form xobject to use as a soft mask.
54      *  @param invert    Indicates if the alpha of the sMask should be inverted.
55      *  @param sMaskMode Whether to use alpha or luminosity for the sMask.
56      *
57      *  These are not de-duped.
58      */
59     static SkPDFDict* GetSMaskGraphicState(SkPDFFormXObject* sMask,
60                                            bool invert,
61                                            SkPDFSMaskMode sMaskMode);
62 
63     /** Get a graphic state that only unsets the soft mask. The reference
64      *  count of the object is incremented and it is the caller's responsibility
65      *  to unreference it when done. This is needed to accommodate the weak
66      *  reference pattern used when the returned object is new and has no
67      *  other references.
68      *
69      *  The returned object is a singleton.
70      */
71     static SkPDFDict* GetNoSMaskGraphicState();
72 
73     bool operator==(const SkPDFGraphicState& rhs) const {
74         return 0 == memcmp(&fStrokeWidth, &rhs.fStrokeWidth, 12);
75     }
hash()76     uint32_t hash() const { return SkChecksum::Murmur3(&fStrokeWidth, 12); }
77 
78 private:
79     const SkScalar fStrokeWidth;
80     const SkScalar fStrokeMiter;
81     const uint8_t fAlpha;
82     const uint8_t fStrokeCap;   // SkPaint::Cap
83     const uint8_t fStrokeJoin;  // SkPaint::Join
84     const uint8_t fMode;        // SkXfermode::Mode
85 
86     SkPDFGraphicState(const SkPaint&);
87 
88     typedef SkPDFDict INHERITED;
89 };
90 
91 #endif
92