vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 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 | |
vandebo@chromium.org | 8af0b36 | 2011-01-26 21:59:00 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 22 | #include "SkPDFTypes.h" |
| 23 | #include "SkRefCnt.h" |
| 24 | #include "SkTDArray.h" |
| 25 | |
| 26 | /** \class SkPDFCatalog |
| 27 | |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 28 | The PDF catalog manages object numbers and file offsets. It is used |
| 29 | to create the PDF cross reference table. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 30 | */ |
ctguil@chromium.org | dfc5ffe | 2011-03-30 20:14:49 +0000 | [diff] [blame] | 31 | class SK_API SkPDFCatalog { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 32 | public: |
| 33 | /** Create a PDF catalog. |
| 34 | */ |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 35 | SkPDFCatalog(); |
| 36 | ~SkPDFCatalog(); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 37 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 38 | /** Add the passed object to the catalog. Refs obj. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 39 | * @param obj The object to add. |
| 40 | * @param onFirstPage Is the object on the first page. |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 41 | * @return The obj argument is returned. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 42 | */ |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 43 | SkPDFObject* addObject(SkPDFObject* obj, bool onFirstPage); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 44 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 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 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 53 | /** Output the object number for the passed object. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 54 | * @param obj The object of interest. |
| 55 | * @param stream The writable output stream to send the output to. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 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. |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 61 | * @param obj The object of interest |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 62 | */ |
| 63 | size_t getObjectNumberSize(SkPDFObject* obj); |
| 64 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 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 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 73 | private: |
| 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.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 90 | // Number of objects on the first page. |
| 91 | uint32_t fFirstPageCount; |
| 92 | // Next object number to assign (on page > 1). |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 93 | uint32_t fNextObjNum; |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 94 | // Next object number to assign on the first page. |
| 95 | uint32_t fNextFirstPageObjNum; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 96 | |
| 97 | int findObjectIndex(SkPDFObject* obj) const; |
| 98 | |
| 99 | int assignObjNum(SkPDFObject* obj); |
| 100 | }; |
| 101 | |
| 102 | #endif |