SkPDFStream.h revision 4fc48af0d7bec93a911d32330f251386a8adec98
1
2/*
3 * Copyright 2010 Google Inc.
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 SkPDFStream_DEFINED
11#define SkPDFStream_DEFINED
12
13#include "SkPDFTypes.h"
14#include "SkRefCnt.h"
15#include "SkStream.h"
16#include "SkTemplates.h"
17
18class SkPDFCatalog;
19
20/** \class SkPDFStream
21
22    A stream object in a PDF.  Note, all streams must be indirect objects (via
23    SkObjRef).
24*/
25class SkPDFStream : public SkPDFDict {
26    SK_DECLARE_INST_COUNT(SkPDFStream)
27public:
28    /** Create a PDF stream. A Length entry is automatically added to the
29     *  stream dictionary.
30     *  @param data   The data part of the stream.  Will be ref()ed.
31     */
32    explicit SkPDFStream(SkData* data);
33
34    /** Create a PDF stream. A Length entry is automatically added to the
35     *  stream dictionary.
36     *  @param stream The data part of the stream.  Will be duplicate()d.
37     */
38    explicit SkPDFStream(SkStream* stream);
39
40    virtual ~SkPDFStream();
41
42    // The SkPDFObject interface.  These two methods use a mutex to
43    // allow multiple threads to call at the same time.
44    virtual void emitObject(SkWStream* stream, SkPDFCatalog* catalog) SK_OVERRIDE;
45    virtual size_t getOutputSize(SkPDFCatalog* catalog, bool indirect);
46
47protected:
48    enum State {
49        kUnused_State,         //!< The stream hasn't been requested yet.
50        kNoCompression_State,  //!< The stream's been requested in an
51                               //   uncompressed form.
52        kCompressed_State,     //!< The stream's already been compressed.
53    };
54
55    /** Create a PDF stream with the same content and dictionary entries
56     *  as the passed one.
57     */
58    explicit SkPDFStream(const SkPDFStream& pdfStream);
59
60    /* Create a PDF stream with no data.  The setData method must be called to
61     * set the data.
62     */
63    SkPDFStream();
64
65    // Populate the stream dictionary.  This method returns false if
66    // fSubstitute should be used.
67    virtual bool populate(SkPDFCatalog* catalog);
68
69    void setSubstitute(SkPDFStream* stream) {
70        fSubstitute.reset(stream);
71    }
72
73    SkPDFStream* getSubstitute() const {
74        return fSubstitute.get();
75    }
76
77    void setData(SkData* data);
78    void setData(SkStream* stream);
79
80    size_t dataSize() const;
81
82    void setState(State state) {
83        fState = state;
84    }
85
86    State getState() const {
87        return fState;
88    }
89
90private:
91    // Indicates what form (or if) the stream has been requested.
92    State fState;
93
94    // Mutex guards fState, fDataStream, and fSubstitute in public interface.
95    SkMutex fMutex;
96
97    SkMemoryStream fMemoryStream;  // Used by fDataStream when
98                                   // fDataStream needs to be backed
99                                   // by SkData.
100    SkAutoTUnref<SkStreamRewindable> fDataStream;
101    SkAutoTUnref<SkPDFStream> fSubstitute;
102
103    typedef SkPDFDict INHERITED;
104};
105
106#endif
107