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) const override;
35
36    /** Get the graphic state for the passed SkPaint. The reference count of
37     *  the object is incremented and it is the caller's responsibility to
38     *  unreference it when done. This is needed to accommodate the weak
39     *  reference pattern used when the returned object is new and has no
40     *  other references.
41     *  @param paint  The SkPaint to emulate.
42     */
43    static SkPDFGraphicState* GetGraphicStateForPaint(SkPDFCanon* canon,
44                                                      const SkPaint& paint);
45
46    /** Make a graphic state that only sets the passed soft mask.
47     *  @param sMask     The form xobject to use as a soft mask.
48     *  @param invert    Indicates if the alpha of the sMask should be inverted.
49     *  @param sMaskMode Whether to use alpha or luminosity for the sMask.
50     *
51     *  These are not de-duped.
52     */
53    static sk_sp<SkPDFDict> GetSMaskGraphicState(sk_sp<SkPDFObject> sMask,
54                                                 bool invert,
55                                                 SkPDFSMaskMode sMaskMode,
56                                                 SkPDFCanon* canon);
57
58    /** Make a graphic state that only unsets the soft mask. */
59    static sk_sp<SkPDFDict> MakeNoSmaskGraphicState();
60    static sk_sp<SkPDFStream> MakeInvertFunction();
61
62    bool operator==(const SkPDFGraphicState& rhs) const {
63        return 0 == memcmp(&fStrokeWidth, &rhs.fStrokeWidth, 12);
64    }
65    uint32_t hash() const { return SkOpts::hash(&fStrokeWidth, 12); }
66
67private:
68    const SkScalar fStrokeWidth;
69    const SkScalar fStrokeMiter;
70    const uint8_t fAlpha;
71    const uint8_t fStrokeCap;   // SkPaint::Cap
72    const uint8_t fStrokeJoin;  // SkPaint::Join
73    const uint8_t fMode;        // SkBlendMode
74
75    SkPDFGraphicState(const SkPaint&);
76
77    typedef SkPDFDict INHERITED;
78};
79
80#endif
81