blob: 1a5d8354e94c7bf0211f503a86850f8c0bac40f7 [file] [log] [blame]
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkPDFDocument_DEFINED
18#define SkPDFDocument_DEFINED
19
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000020#include "SkPDFTypes.h"
21#include "SkRefCnt.h"
22#include "SkTDArray.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000023#include "SkTScopedPtr.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000024
vandebo@chromium.org421d6442011-07-20 17:39:01 +000025class SkPDFCatalog;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000026class SkPDFDevice;
27class SkPDFPage;
28class SkWSteam;
29
30/** \class SkPDFDocument
31
32 A SkPDFDocument assembles pages together and generates the final PDF file.
33*/
34class SkPDFDocument {
35public:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000036 enum Flags {
37 kNoCompression_Flag = 0x01, //!< mask disable stream compression.
38 kNoEmbedding_Flag = 0x02, //!< mask do not embed fonts.
39
40 kDraftMode_Flags = 0x03,
41 };
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000042 /** Create a PDF document.
43 */
vandebo@chromium.org421d6442011-07-20 17:39:01 +000044 explicit SK_API SkPDFDocument(Flags flags = (Flags)0);
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000045 SK_API ~SkPDFDocument();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000046
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000047 /** Output the PDF to the passed stream. It is an error to call this (it
48 * will return false and not modify stream) if no pages have been added
49 * or there are pages missing (i.e. page 1 and 3 have been added, but not
50 * page 2).
51 *
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000052 * @param stream The writable output stream to send the PDF to.
53 */
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000054 SK_API bool emitPDF(SkWStream* stream);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000055
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000056 /** Sets the specific page to the passed PDF device. If the specified
57 * page is already set, this overrides it. Returns true if successful.
58 * Will fail if the document has already been emitted.
59 *
60 * @param pageNumber The position to add the passed device (1 based).
61 * @param pdfDevice The page to add to this document.
62 */
63 SK_API bool setPage(int pageNumber, const SkRefPtr<SkPDFDevice>& pdfDevice);
64
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000065 /** Append the passed pdf device to the document as a new page. Returns
66 * true if successful. Will fail if the document has already been emitted.
67 *
68 * @param pdfDevice The page to add to this document.
69 */
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000070 SK_API bool appendPage(const SkRefPtr<SkPDFDevice>& pdfDevice);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000071
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000072 /** Get the list of pages in this document.
73 */
74 SK_API const SkTDArray<SkPDFPage*>& getPages();
75
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000076private:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000077 SkTScopedPtr<SkPDFCatalog> fCatalog;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000078 int64_t fXRefFileOffset;
79
80 SkTDArray<SkPDFPage*> fPages;
81 SkTDArray<SkPDFDict*> fPageTree;
82 SkRefPtr<SkPDFDict> fDocCatalog;
83 SkTDArray<SkPDFObject*> fPageResources;
vandebo@chromium.orga5180862010-10-26 19:48:49 +000084 int fSecondPageFirstResourceIndex;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000085
86 SkRefPtr<SkPDFDict> fTrailerDict;
87
88 /** Output the PDF header to the passed stream.
89 * @param stream The writable output stream to send the header to.
90 */
91 void emitHeader(SkWStream* stream);
92
93 /** Get the size of the header.
94 */
95 size_t headerSize();
96
97 /** Output the PDF footer to the passed stream.
98 * @param stream The writable output stream to send the footer to.
99 * @param objCount The number of objects in the PDF.
100 */
101 void emitFooter(SkWStream* stream, int64_t objCount);
102};
103
104#endif