blob: 8b472e1bd05f80bf2d3f98e88f02e46a907d9074 [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 */
reed@google.com1feb3302011-07-20 18:43:19 +000063 SK_API bool setPage(int pageNumber, SkPDFDevice* pdfDevice);
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000064
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 */
reed@google.com1feb3302011-07-20 18:43:19 +000070 SK_API bool appendPage(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.org98594282011-07-25 22:34:12 +000084 SkTDArray<SkPDFObject*> fSubstitutes;
vandebo@chromium.orga5180862010-10-26 19:48:49 +000085 int fSecondPageFirstResourceIndex;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000086
87 SkRefPtr<SkPDFDict> fTrailerDict;
88
89 /** Output the PDF header to the passed stream.
90 * @param stream The writable output stream to send the header to.
91 */
92 void emitHeader(SkWStream* stream);
93
94 /** Get the size of the header.
95 */
96 size_t headerSize();
97
98 /** Output the PDF footer to the passed stream.
99 * @param stream The writable output stream to send the footer to.
100 * @param objCount The number of objects in the PDF.
101 */
102 void emitFooter(SkWStream* stream, int64_t objCount);
103};
104
105#endif