1/*
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 SkPDFPage_DEFINED
18#define SkPDFPage_DEFINED
19
20#include "SkPDFTypes.h"
21#include "SkPDFStream.h"
22#include "SkRefCnt.h"
23#include "SkTDArray.h"
24
25class SkPDFCatalog;
26class SkPDFDevice;
27class SkWStream;
28
29/** \class SkPDFPage
30
31    A SkPDFPage contains meta information about a page, is used in the page
32    tree and points to the content of the page.
33*/
34class SkPDFPage : public SkPDFDict {
35public:
36    /** Create a PDF page with the passed PDF device.  The device need not
37     *  have content on it yet.
38     *  @param content    The page content.
39     */
40    explicit SkPDFPage(const SkRefPtr<SkPDFDevice>& content);
41    ~SkPDFPage();
42
43    /** Before a page and its contents can be sized and emitted, it must
44     *  be finalized.  No changes to the PDFDevice will be honored after
45     *  finalizePage has been called.  This function adds the page content
46     *  to the passed catalog, so it must be called for each document
47     *  that the page is part of.
48     *  @param catalog         The catalog to add page content objects to.
49     *  @param firstPage       Indicate if this is the first page of a document.
50     *  @param resourceObjects The resource objects used on the page are added
51     *                         to this array.  This gives the caller a chance
52     *                         to deduplicate resources across pages.
53     */
54    void finalizePage(SkPDFCatalog* catalog, bool firstPage,
55                      SkTDArray<SkPDFObject*>* resourceObjects);
56
57    /** Determine the size of the page content and store to the catalog
58     *  the offsets of all nonresource-indirect objects that make up the page
59     *  content.  This must be called before emitPage(), but after finalizePage.
60     *  @param catalog    The catalog to add the object offsets to.
61     *  @param fileOffset The file offset where the page content will be
62     *                    emitted.
63     */
64    off_t getPageSize(SkPDFCatalog* catalog, off_t fileOffset);
65
66    /** Output the page content to the passed stream.
67     *  @param stream     The writable output stream to send the content to.
68     *  @param catalog    The active object catalog.
69     */
70    void emitPage(SkWStream* stream, SkPDFCatalog* catalog);
71
72    /** Generate a page tree for the passed vector of pages.  New objects are
73     *  added to the catalog.  The pageTree vector is populated with all of
74     *  the 'Pages' dictionaries as well as the 'Page' objects.  Page trees
75     *  have both parent and children links, creating reference cycles, so
76     *  it must be torn down explicitly.  The first page is not added to
77     *  the pageTree dictionary array so the caller can handle it specially.
78     *  @param pages      The ordered vector of page objects.
79     *  @param catalog    The catalog to add new objects into.
80     *  @param pageTree   An output vector with all of the internal and leaf
81     *                    nodes of the pageTree.
82     *  @param rootNode   An output parameter set to the root node.
83     */
84    static void generatePageTree(const SkTDArray<SkPDFPage*>& pages,
85                                 SkPDFCatalog* catalog,
86                                 SkTDArray<SkPDFDict*>* pageTree,
87                                 SkPDFDict** rootNode);
88
89    /** Get the fonts used on this page.
90     */
91    SK_API const SkTDArray<SkPDFFont*>& getFontResources() const;
92
93private:
94    // Multiple pages may reference the content.
95    SkRefPtr<SkPDFDevice> fDevice;
96
97    // Once the content is finalized, put it into a stream for output.
98    SkRefPtr<SkPDFStream> fContentStream;
99};
100
101#endif
102