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