• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkPDFCatalog_DEFINED
18 #define SkPDFCatalog_DEFINED
19 
20 #include <sys/types.h>
21 
22 #include "SkPDFTypes.h"
23 #include "SkRefCnt.h"
24 #include "SkTDArray.h"
25 
26 /** \class SkPDFCatalog
27 
28     The PDF catalog manages object numbers and file offsets.  It is used
29     to create the PDF cross reference table.
30 */
31 class SK_API SkPDFCatalog {
32 public:
33     /** Create a PDF catalog.
34      */
35     SkPDFCatalog();
36     ~SkPDFCatalog();
37 
38     /** Add the passed object to the catalog.  Refs obj.
39      *  @param obj         The object to add.
40      *  @param onFirstPage Is the object on the first page.
41      *  @return The obj argument is returned.
42      */
43     SkPDFObject* addObject(SkPDFObject* obj, bool onFirstPage);
44 
45     /** Inform the catalog of the object's position in the final stream.
46      *  The object should already have been added to the catalog.  Returns
47      *  the object's size.
48      *  @param obj         The object to add.
49      *  @param offset      The byte offset in the output stream of this object.
50      */
51     size_t setFileOffset(SkPDFObject* obj, size_t offset);
52 
53     /** Output the object number for the passed object.
54      *  @param obj         The object of interest.
55      *  @param stream      The writable output stream to send the output to.
56      */
57     void emitObjectNumber(SkWStream* stream, SkPDFObject* obj);
58 
59     /** Return the number of bytes that would be emitted for the passed
60      *  object's object number.
61      *  @param obj         The object of interest
62      */
63     size_t getObjectNumberSize(SkPDFObject* obj);
64 
65     /** Output the cross reference table for objects in the catalog.
66      *  Returns the total number of objects.
67      *  @param stream      The writable output stream to send the output to.
68      *  @param firstPage   If true, include first page objects only, otherwise
69      *                     include all objects not on the first page.
70      */
71     int32_t emitXrefTable(SkWStream* stream, bool firstPage);
72 
73 private:
74     struct Rec {
RecRec75         Rec(SkPDFObject* object, bool onFirstPage)
76             : fObject(object),
77               fFileOffset(0),
78               fObjNumAssigned(false),
79               fOnFirstPage(onFirstPage) {
80         }
81         SkPDFObject* fObject;
82         off_t fFileOffset;
83         bool fObjNumAssigned;
84         bool fOnFirstPage;
85     };
86 
87     // TODO(vandebo) Make this a hash if it's a performance problem.
88     SkTDArray<struct Rec> fCatalog;
89 
90     // Number of objects on the first page.
91     uint32_t fFirstPageCount;
92     // Next object number to assign (on page > 1).
93     uint32_t fNextObjNum;
94     // Next object number to assign on the first page.
95     uint32_t fNextFirstPageObjNum;
96 
97     int findObjectIndex(SkPDFObject* obj) const;
98 
99     int assignObjNum(SkPDFObject* obj);
100 };
101 
102 #endif
103