1 2 /* 3 * Copyright 2010 The Android Open Source Project 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9 10 #ifndef SkPDFDocument_DEFINED 11 #define SkPDFDocument_DEFINED 12 13 #include "SkPDFTypes.h" 14 #include "SkRefCnt.h" 15 #include "SkTDArray.h" 16 #include "SkTScopedPtr.h" 17 18 class SkPDFCatalog; 19 class SkPDFDevice; 20 class SkPDFPage; 21 class SkWSteam; 22 23 /** \class SkPDFDocument 24 25 A SkPDFDocument assembles pages together and generates the final PDF file. 26 */ 27 class SkPDFDocument { 28 public: 29 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 }; 35 /** Create a PDF document. 36 */ 37 explicit SK_API SkPDFDocument(Flags flags = (Flags)0); 38 SK_API ~SkPDFDocument(); 39 40 /** 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 * 45 * @param stream The writable output stream to send the PDF to. 46 */ 47 SK_API bool emitPDF(SkWStream* stream); 48 49 /** 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 */ 56 SK_API bool setPage(int pageNumber, SkPDFDevice* pdfDevice); 57 58 /** 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 */ 63 SK_API bool appendPage(SkPDFDevice* pdfDevice); 64 65 /** Get the list of pages in this document. 66 */ 67 SK_API const SkTDArray<SkPDFPage*>& getPages(); 68 69 private: 70 SkTScopedPtr<SkPDFCatalog> fCatalog; 71 int64_t fXRefFileOffset; 72 73 SkTDArray<SkPDFPage*> fPages; 74 SkTDArray<SkPDFDict*> fPageTree; 75 SkRefPtr<SkPDFDict> fDocCatalog; 76 SkTDArray<SkPDFObject*> fPageResources; 77 SkTDArray<SkPDFObject*> fSubstitutes; 78 int fSecondPageFirstResourceIndex; 79 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 99