blob: c1c6fb4c641e48d654c081c8ef1ada4a11a6b738 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2010 The Android Open Source Project
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000010#ifndef SkPDFDocument_DEFINED
11#define SkPDFDocument_DEFINED
12
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000013#include "SkPDFTypes.h"
14#include "SkRefCnt.h"
15#include "SkTDArray.h"
vandebo@chromium.org421d6442011-07-20 17:39:01 +000016#include "SkTScopedPtr.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000017
vandebo@chromium.org421d6442011-07-20 17:39:01 +000018class SkPDFCatalog;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000019class SkPDFDevice;
20class SkPDFPage;
21class SkWSteam;
22
23/** \class SkPDFDocument
24
25 A SkPDFDocument assembles pages together and generates the final PDF file.
26*/
27class SkPDFDocument {
28public:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000029 enum Flags {
30 kNoCompression_Flag = 0x01, //!< mask disable stream compression.
31 kNoEmbedding_Flag = 0x02, //!< mask do not embed fonts.
32
33 kDraftMode_Flags = 0x03,
34 };
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000035 /** Create a PDF document.
36 */
vandebo@chromium.org421d6442011-07-20 17:39:01 +000037 explicit SK_API SkPDFDocument(Flags flags = (Flags)0);
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000038 SK_API ~SkPDFDocument();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000039
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000040 /** Output the PDF to the passed stream. It is an error to call this (it
41 * will return false and not modify stream) if no pages have been added
42 * or there are pages missing (i.e. page 1 and 3 have been added, but not
43 * page 2).
44 *
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000045 * @param stream The writable output stream to send the PDF to.
46 */
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000047 SK_API bool emitPDF(SkWStream* stream);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000048
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000049 /** Sets the specific page to the passed PDF device. If the specified
50 * page is already set, this overrides it. Returns true if successful.
51 * Will fail if the document has already been emitted.
52 *
53 * @param pageNumber The position to add the passed device (1 based).
54 * @param pdfDevice The page to add to this document.
55 */
reed@google.com1feb3302011-07-20 18:43:19 +000056 SK_API bool setPage(int pageNumber, SkPDFDevice* pdfDevice);
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000057
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000058 /** Append the passed pdf device to the document as a new page. Returns
59 * true if successful. Will fail if the document has already been emitted.
60 *
61 * @param pdfDevice The page to add to this document.
62 */
reed@google.com1feb3302011-07-20 18:43:19 +000063 SK_API bool appendPage(SkPDFDevice* pdfDevice);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000064
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000065 /** Get the list of pages in this document.
66 */
67 SK_API const SkTDArray<SkPDFPage*>& getPages();
68
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000069private:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000070 SkTScopedPtr<SkPDFCatalog> fCatalog;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000071 int64_t fXRefFileOffset;
72
73 SkTDArray<SkPDFPage*> fPages;
74 SkTDArray<SkPDFDict*> fPageTree;
75 SkRefPtr<SkPDFDict> fDocCatalog;
76 SkTDArray<SkPDFObject*> fPageResources;
vandebo@chromium.org98594282011-07-25 22:34:12 +000077 SkTDArray<SkPDFObject*> fSubstitutes;
vandebo@chromium.orga5180862010-10-26 19:48:49 +000078 int fSecondPageFirstResourceIndex;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000079
80 SkRefPtr<SkPDFDict> fTrailerDict;
81
82 /** Output the PDF header to the passed stream.
83 * @param stream The writable output stream to send the header to.
84 */
85 void emitHeader(SkWStream* stream);
86
87 /** Get the size of the header.
88 */
89 size_t headerSize();
90
91 /** Output the PDF footer to the passed stream.
92 * @param stream The writable output stream to send the footer to.
93 * @param objCount The number of objects in the PDF.
94 */
95 void emitFooter(SkWStream* stream, int64_t objCount);
96};
97
98#endif