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