Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2010 The Android Open Source Project | 3 * Copyright 2010 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkPDFCatalog_DEFINED | 10 #ifndef SkPDFCatalog_DEFINED |
| 11 #define SkPDFCatalog_DEFINED | 11 #define SkPDFCatalog_DEFINED |
| 12 | 12 |
| 13 #include <sys/types.h> | 13 #include "SkChecksum.h" |
|
mtklein
2015/03/24 16:03:06
probably unneeded?
hal.canary
2015/03/24 20:08:05
moved to SkTHash where it belongs.
| |
| 14 | |
| 15 #include "SkPDFTypes.h" | 14 #include "SkPDFTypes.h" |
| 16 #include "SkTDArray.h" | 15 #include "SkTDArray.h" |
| 16 #include "SkTHash.h" | |
| 17 | 17 |
| 18 /** \class SkPDFCatalog | 18 /** \class SkPDFCatalog |
| 19 | 19 |
| 20 The PDF catalog manages object numbers and file offsets. It is used | 20 The PDF catalog manages object numbers and file offsets. It is used |
| 21 to create the PDF cross reference table. | 21 to create the PDF cross reference table. |
| 22 */ | 22 */ |
| 23 class SkPDFCatalog { | 23 class SkPDFCatalog { |
| 24 public: | 24 public: |
| 25 /** Create a PDF catalog. | 25 SkPDFCatalog() {} |
| 26 */ | |
| 27 SkPDFCatalog(); | |
| 28 ~SkPDFCatalog(); | 26 ~SkPDFCatalog(); |
| 29 | 27 |
| 30 /** Add the passed object to the catalog. Refs obj. | 28 /** Add the passed object to the catalog. |
| 31 * @param obj The object to add. | 29 * @param obj The object to add. |
| 32 * @param onFirstPage Is the object on the first page. | 30 * @return True iff the object was not already added to the catalog. |
| 33 * @return The obj argument is returned. | |
| 34 */ | 31 */ |
| 35 SkPDFObject* addObject(SkPDFObject* obj, bool onFirstPage); | 32 bool addObject(SkPDFObject* obj) { |
| 36 | 33 if (fObjectNumbers.find(obj)) { |
|
mtklein
2015/03/24 16:03:06
These functions working with hashes are all short,
hal.canary
2015/03/24 20:08:05
Done.
| |
| 37 /** Inform the catalog of the object's position in the final stream. | 34 return false; |
| 38 * The object should already have been added to the catalog. | 35 } |
| 39 * @param obj The object to add. | 36 fObjectNumbers.set(obj, fObjectNumbers.count() + 1); |
| 40 * @param offset The byte offset in the output stream of this object. | 37 return true; |
| 41 */ | 38 } |
| 42 void setFileOffset(SkPDFObject* obj, off_t offset); | |
| 43 | 39 |
| 44 /** Get the object number for the passed object. | 40 /** Get the object number for the passed object. |
| 45 * @param obj The object of interest. | 41 * @param obj The object of interest. |
| 46 */ | 42 */ |
| 47 int32_t getObjectNumber(SkPDFObject* obj); | 43 int32_t getObjectNumber(SkPDFObject* obj) { |
|
mtklein
2015/03/24 16:03:06
Think it's worth merging addObject and getObjectNu
hal.canary
2015/03/24 20:08:05
No. I will need to know if the object has been ad
| |
| 48 | 44 int32_t* objectNumberFound = fObjectNumbers.find(obj); |
| 49 /** Output the cross reference table for objects in the catalog. | 45 SkASSERT(objectNumberFound); |
| 50 * Returns the total number of objects. | 46 return *objectNumberFound; |
| 51 * @param stream The writable output stream to send the output to. | 47 } |
| 52 * @param firstPage If true, include first page objects only, otherwise | |
| 53 * include all objects not on the first page. | |
| 54 */ | |
| 55 int32_t emitXrefTable(SkWStream* stream, bool firstPage); | |
| 56 | 48 |
| 57 /** Set substitute object for the passed object. | 49 /** Set substitute object for the passed object. |
| 50 Refs substitute. | |
| 58 */ | 51 */ |
| 59 void setSubstitute(SkPDFObject* original, SkPDFObject* substitute); | 52 void setSubstitute(SkPDFObject* original, SkPDFObject* substitute) { |
| 53 SkASSERT(original != substitute); | |
| 54 SkASSERT(!fSubstituteMap.find(original)); | |
| 55 fSubstituteMap.set(original, SkRef(substitute)); | |
| 56 } | |
| 60 | 57 |
| 61 /** Find and return any substitute object set for the passed object. If | 58 /** Find and return any substitute object set for the passed object. If |
| 62 * there is none, return the passed object. | 59 * there is none, return the passed object. |
| 63 */ | 60 */ |
| 64 SkPDFObject* getSubstituteObject(SkPDFObject* object); | 61 SkPDFObject* getSubstituteObject(SkPDFObject* object) { |
| 62 SkPDFObject** found = fSubstituteMap.find(object); | |
| 63 return found ? *found : object; | |
| 64 } | |
| 65 | 65 |
| 66 private: | 66 private: |
| 67 struct Rec { | 67 SkTHashMap<SkPDFObject*, int32_t> fObjectNumbers; |
| 68 Rec(SkPDFObject* object, bool onFirstPage) | 68 SkTHashMap<SkPDFObject*, SkPDFObject*> fSubstituteMap; |
| 69 : fObject(object), | |
| 70 fFileOffset(0), | |
| 71 fObjNumAssigned(false), | |
| 72 fOnFirstPage(onFirstPage) { | |
| 73 } | |
| 74 SkPDFObject* fObject; | |
| 75 off_t fFileOffset; | |
| 76 bool fObjNumAssigned; | |
| 77 bool fOnFirstPage; | |
| 78 }; | |
| 79 | |
| 80 struct SubstituteMapping { | |
| 81 SubstituteMapping(SkPDFObject* original, SkPDFObject* substitute) | |
| 82 : fOriginal(original), fSubstitute(substitute) { | |
| 83 } | |
| 84 SkPDFObject* fOriginal; | |
| 85 SkPDFObject* fSubstitute; | |
| 86 }; | |
| 87 | |
| 88 // TODO(vandebo): Make this a hash if it's a performance problem. | |
| 89 SkTDArray<Rec> fCatalog; | |
| 90 | |
| 91 // TODO(arthurhsu): Make this a hash if it's a performance problem. | |
| 92 SkTDArray<SubstituteMapping> fSubstituteMap; | |
| 93 SkTSet<SkPDFObject*> fSubstituteResourcesFirstPage; | |
| 94 SkTSet<SkPDFObject*> fSubstituteResourcesRemaining; | |
| 95 | |
| 96 // Number of objects on the first page. | |
| 97 uint32_t fFirstPageCount; | |
| 98 // Next object number to assign (on page > 1). | |
| 99 uint32_t fNextObjNum; | |
| 100 // Next object number to assign on the first page. | |
| 101 uint32_t fNextFirstPageObjNum; | |
| 102 | |
| 103 int findObjectIndex(SkPDFObject* obj); | |
| 104 | |
| 105 int assignObjNum(SkPDFObject* obj); | |
| 106 }; | 69 }; |
| 107 | 70 |
| 108 #endif | 71 #endif |
| OLD | NEW |