blob: e02ffa1a477528b62fdea49937513cddcfef3ffa [file] [log] [blame]
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +00001/*
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
vandebo@chromium.org8af0b362011-01-26 21:59:00 +000020#include <sys/types.h>
21
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000022#include "SkPDFTypes.h"
23#include "SkRefCnt.h"
24#include "SkTDArray.h"
25
26/** \class SkPDFCatalog
27
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +000028 The PDF catalog manages object numbers and file offsets. It is used
29 to create the PDF cross reference table.
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000030*/
ctguil@chromium.orgdfc5ffe2011-03-30 20:14:49 +000031class SK_API SkPDFCatalog {
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000032public:
33 /** Create a PDF catalog.
34 */
vandebo@chromium.orgf66025d2010-10-01 23:26:55 +000035 SkPDFCatalog();
36 ~SkPDFCatalog();
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000037
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000038 /** Add the passed object to the catalog. Refs obj.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000039 * @param obj The object to add.
40 * @param onFirstPage Is the object on the first page.
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000041 * @return The obj argument is returned.
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000042 */
vandebo@chromium.orgf7c15762011-02-01 22:19:44 +000043 SkPDFObject* addObject(SkPDFObject* obj, bool onFirstPage);
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000044
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000045 /** 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
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000053 /** Output the object number for the passed object.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000054 * @param obj The object of interest.
55 * @param stream The writable output stream to send the output to.
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000056 */
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.
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000061 * @param obj The object of interest
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000062 */
63 size_t getObjectNumberSize(SkPDFObject* obj);
64
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000065 /** 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
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000073private:
74 struct Rec {
75 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
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000090 // Number of objects on the first page.
91 uint32_t fFirstPageCount;
92 // Next object number to assign (on page > 1).
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000093 uint32_t fNextObjNum;
vandebo@chromium.orgd877fdb2010-10-12 23:08:13 +000094 // Next object number to assign on the first page.
95 uint32_t fNextFirstPageObjNum;
vandebo@chromium.org8459d4e2010-09-24 22:25:30 +000096
97 int findObjectIndex(SkPDFObject* obj) const;
98
99 int assignObjNum(SkPDFObject* obj);
100};
101
102#endif