blob: 1a4a51fa43077008ff942fa84ab2ad34865ba50e [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;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000024
25/** \class SkPDFDocument
26
27 A SkPDFDocument assembles pages together and generates the final PDF file.
28*/
29class SkPDFDocument {
30public:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000031 enum Flags {
32 kNoCompression_Flag = 0x01, //!< mask disable stream compression.
33 kNoEmbedding_Flag = 0x02, //!< mask do not embed fonts.
34
35 kDraftMode_Flags = 0x03,
36 };
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000037 /** Create a PDF document.
38 */
vandebo@chromium.org421d6442011-07-20 17:39:01 +000039 explicit SK_API SkPDFDocument(Flags flags = (Flags)0);
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000040 SK_API ~SkPDFDocument();
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000041
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000042 /** Output the PDF to the passed stream. It is an error to call this (it
43 * will return false and not modify stream) if no pages have been added
44 * or there are pages missing (i.e. page 1 and 3 have been added, but not
45 * page 2).
46 *
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000047 * @param stream The writable output stream to send the PDF to.
48 */
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000049 SK_API bool emitPDF(SkWStream* stream);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000050
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000051 /** Sets the specific page to the passed PDF device. If the specified
52 * page is already set, this overrides it. Returns true if successful.
53 * Will fail if the document has already been emitted.
54 *
55 * @param pageNumber The position to add the passed device (1 based).
56 * @param pdfDevice The page to add to this document.
57 */
reed@google.com1feb3302011-07-20 18:43:19 +000058 SK_API bool setPage(int pageNumber, SkPDFDevice* pdfDevice);
vandebo@chromium.orgfb6a53a2011-07-18 23:13:19 +000059
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000060 /** Append the passed pdf device to the document as a new page. Returns
61 * true if successful. Will fail if the document has already been emitted.
62 *
63 * @param pdfDevice The page to add to this document.
64 */
reed@google.com1feb3302011-07-20 18:43:19 +000065 SK_API bool appendPage(SkPDFDevice* pdfDevice);
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000066
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000067 /** Get the count of unique font types used in the document.
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000068 */
vandebo@chromium.org7d6c8f92012-03-22 20:45:15 +000069 SK_API void getCountOfFontTypes(
70 int counts[SkAdvancedTypefaceMetrics::kNotEmbeddable_Font + 1]) const;
vandebo@chromium.orgd897bfb2011-05-31 18:18:21 +000071
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000072private:
vandebo@chromium.org421d6442011-07-20 17:39:01 +000073 SkTScopedPtr<SkPDFCatalog> fCatalog;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000074 int64_t fXRefFileOffset;
75
76 SkTDArray<SkPDFPage*> fPages;
77 SkTDArray<SkPDFDict*> fPageTree;
78 SkRefPtr<SkPDFDict> fDocCatalog;
79 SkTDArray<SkPDFObject*> fPageResources;
vandebo@chromium.org98594282011-07-25 22:34:12 +000080 SkTDArray<SkPDFObject*> fSubstitutes;
vandebo@chromium.orga5180862010-10-26 19:48:49 +000081 int fSecondPageFirstResourceIndex;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000082
83 SkRefPtr<SkPDFDict> fTrailerDict;
84
85 /** Output the PDF header to the passed stream.
86 * @param stream The writable output stream to send the header to.
87 */
88 void emitHeader(SkWStream* stream);
89
90 /** Get the size of the header.
91 */
92 size_t headerSize();
93
94 /** Output the PDF footer to the passed stream.
95 * @param stream The writable output stream to send the footer to.
96 * @param objCount The number of objects in the PDF.
97 */
98 void emitFooter(SkWStream* stream, int64_t objCount);
99};
100
101#endif