blob: 664b3030a2d6bf7924c48e01af498371c7bd5d4a [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.org7d6c8f92012-03-22 20:45:15 +000013#include "SkAdvancedTypefaceMetrics.h"
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000014#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;
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000020class SkPDFDict;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000021class SkPDFPage;
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000022class SkPDFObject;
23class SkWStream;
edisonn@google.com6addb192013-04-02 15:33:08 +000024template <typename T> class SK_API SkTSet;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000025
26/** \class SkPDFDocument
27
28 A SkPDFDocument assembles pages together and generates the final PDF file.
29*/
30class SkPDFDocument {
31public:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000032 enum Flags {
edisonn@google.com8c020612013-03-12 19:53:16 +000033 kNoCompression_Flags = 0x01, //!< DEPRECATED.
34 kFavorSpeedOverSize_Flags = 0x01, //!< Don't compress the stream, but
35 // if it is already compressed return
36 // the compressed stream.
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000037 kNoLinks_Flags = 0x02, //!< do not honor link annotations.
vandebo@chromium.org421d6442011-07-20 17:39:01 +000038
vandebo@chromium.org238be8c2012-07-13 20:06:02 +000039 kDraftMode_Flags = 0x01,
vandebo@chromium.org421d6442011-07-20 17:39:01 +000040 };
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000041 /** Create a PDF document.
42 */
vandebo@chromium.org421d6442011-07-20 17:39:01 +000043 explicit SK_API SkPDFDocument(Flags flags = (Flags)0);
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000044 SK_API ~SkPDFDocument();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000045
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000046 /** Output the PDF to the passed stream. It is an error to call this (it
47 * will return false and not modify stream) if no pages have been added
48 * or there are pages missing (i.e. page 1 and 3 have been added, but not
49 * page 2).
50 *
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000051 * @param stream The writable output stream to send the PDF to.
52 */
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000053 SK_API bool emitPDF(SkWStream* stream);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000054
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000055 /** Sets the specific page to the passed PDF device. If the specified
56 * page is already set, this overrides it. Returns true if successful.
57 * Will fail if the document has already been emitted.
58 *
59 * @param pageNumber The position to add the passed device (1 based).
60 * @param pdfDevice The page to add to this document.
61 */
reed@google.com1feb3302011-07-20 18:43:19 +000062 SK_API bool setPage(int pageNumber, SkPDFDevice* pdfDevice);
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000063
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000064 /** Append the passed pdf device to the document as a new page. Returns
65 * true if successful. Will fail if the document has already been emitted.
66 *
67 * @param pdfDevice The page to add to this document.
68 */
reed@google.com1feb3302011-07-20 18:43:19 +000069 SK_API bool appendPage(SkPDFDevice* pdfDevice);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000070
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000071 /** Get the count of unique font types used in the document.
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000072 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000073 SK_API void getCountOfFontTypes(
74 int counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]) const;
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000075
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;
reed@google.comaf777272012-09-20 18:19:26 +000082 SkPDFDict* fDocCatalog;
edisonn@google.com6addb192013-04-02 15:33:08 +000083 SkTSet<SkPDFObject*>* fFirstPageResources;
84 SkTSet<SkPDFObject*>* fOtherPageResources;
vandebo@chromium.org98594282011-07-25 22:34:12 +000085 SkTDArray<SkPDFObject*> fSubstitutes;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000086
reed@google.comaf777272012-09-20 18:19:26 +000087 SkPDFDict* fTrailerDict;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000088
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