| 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 #include "SkPDFCatalog.h" | 10 #include "SkPDFCatalog.h" |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 | 26 |
| 27 SkPDFObject* SkPDFCatalog::addObject(SkPDFObject* obj, bool onFirstPage) { | 27 SkPDFObject* SkPDFCatalog::addObject(SkPDFObject* obj, bool onFirstPage) { |
| 28 if (findObjectIndex(obj) != -1) { // object already added | 28 if (findObjectIndex(obj) != -1) { // object already added |
| 29 return obj; | 29 return obj; |
| 30 } | 30 } |
| 31 SkASSERT(fNextFirstPageObjNum == 0); | 31 SkASSERT(fNextFirstPageObjNum == 0); |
| 32 if (onFirstPage) { | 32 if (onFirstPage) { |
| 33 fFirstPageCount++; | 33 fFirstPageCount++; |
| 34 } | 34 } |
| 35 | 35 |
| 36 struct Rec newEntry(obj, onFirstPage); | 36 Rec newEntry(obj, onFirstPage); |
| 37 fCatalog.append(1, &newEntry); | 37 fCatalog.append(1, &newEntry); |
| 38 return obj; | 38 return obj; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void SkPDFCatalog::setFileOffset(SkPDFObject* obj, off_t offset) { | 41 void SkPDFCatalog::setFileOffset(SkPDFObject* obj, off_t offset) { |
| 42 int objIndex = assignObjNum(obj) - 1; | 42 int objIndex = assignObjNum(obj) - 1; |
| 43 SkASSERT(fCatalog[objIndex].fObjNumAssigned); | 43 SkASSERT(fCatalog[objIndex].fObjNumAssigned); |
| 44 SkASSERT(fCatalog[objIndex].fFileOffset == 0); | 44 SkASSERT(fCatalog[objIndex].fFileOffset == 0); |
| 45 fCatalog[objIndex].fFileOffset = offset; | 45 fCatalog[objIndex].fFileOffset = offset; |
| 46 } | 46 } |
| 47 | 47 |
| 48 void SkPDFCatalog::emitObjectNumber(SkWStream* stream, SkPDFObject* obj) { | 48 int32_t SkPDFCatalog::getObjectNumber(SkPDFObject* obj) { |
| 49 stream->writeDecAsText(assignObjNum(obj)); | 49 return (int32_t)assignObjNum(obj); |
| 50 stream->writeText(" 0"); // Generation number is always 0. | |
| 51 } | 50 } |
| 52 | 51 |
| 53 size_t SkPDFCatalog::getObjectNumberSize(SkPDFObject* obj) { | 52 int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) { |
| 54 SkDynamicMemoryWStream buffer; | |
| 55 emitObjectNumber(&buffer, obj); | |
| 56 return buffer.getOffset(); | |
| 57 } | |
| 58 | |
| 59 int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) const { | |
| 60 for (int i = 0; i < fCatalog.count(); i++) { | 53 for (int i = 0; i < fCatalog.count(); i++) { |
| 61 if (fCatalog[i].fObject == obj) { | 54 if (fCatalog[i].fObject == obj) { |
| 62 return i; | 55 return i; |
| 63 } | 56 } |
| 64 } | 57 } |
| 65 // If it's not in the main array, check if it's a substitute object. | 58 // If it's not in the main array, check if it's a substitute object. |
| 66 for (int i = 0; i < fSubstituteMap.count(); ++i) { | 59 for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 67 if (fSubstituteMap[i].fSubstitute == obj) { | 60 if (fSubstituteMap[i].fSubstitute == obj) { |
| 68 return findObjectIndex(fSubstituteMap[i].fOriginal); | 61 return findObjectIndex(fSubstituteMap[i].fOriginal); |
| 69 } | 62 } |
| 70 } | 63 } |
| 71 return -1; | 64 Rec newEntry(obj, false); |
| 65 fCatalog.append(1, &newEntry); |
| 66 return fCatalog.count() - 1; |
| 72 } | 67 } |
| 73 | 68 |
| 74 int SkPDFCatalog::assignObjNum(SkPDFObject* obj) { | 69 int SkPDFCatalog::assignObjNum(SkPDFObject* obj) { |
| 75 int pos = findObjectIndex(obj); | 70 int pos = findObjectIndex(obj); |
| 76 // If this assert fails, it means you probably forgot to add an object | 71 // If this assert fails, it means you probably forgot to add an object |
| 77 // to the resource list. | 72 // to the resource list. |
| 78 SkASSERT(pos >= 0); | 73 SkASSERT(pos >= 0); |
| 79 uint32_t currentIndex = pos; | 74 uint32_t currentIndex = pos; |
| 80 if (fCatalog[currentIndex].fObjNumAssigned) { | 75 if (fCatalog[currentIndex].fObjNumAssigned) { |
| 81 return currentIndex + 1; | 76 return currentIndex + 1; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 #if defined(SK_DEBUG) | 138 #if defined(SK_DEBUG) |
| 144 // Sanity check: is the original already in substitute list? | 139 // Sanity check: is the original already in substitute list? |
| 145 for (int i = 0; i < fSubstituteMap.count(); ++i) { | 140 for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 146 if (original == fSubstituteMap[i].fSubstitute || | 141 if (original == fSubstituteMap[i].fSubstitute || |
| 147 original == fSubstituteMap[i].fOriginal) { | 142 original == fSubstituteMap[i].fOriginal) { |
| 148 SkASSERT(false); | 143 SkASSERT(false); |
| 149 return; | 144 return; |
| 150 } | 145 } |
| 151 } | 146 } |
| 152 #endif | 147 #endif |
| 153 // Check if the original is on first page. | |
| 154 bool onFirstPage = false; | |
| 155 for (int i = 0; i < fCatalog.count(); ++i) { | |
| 156 if (fCatalog[i].fObject == original) { | |
| 157 onFirstPage = fCatalog[i].fOnFirstPage; | |
| 158 break; | |
| 159 } | |
| 160 #if defined(SK_DEBUG) | |
| 161 if (i == fCatalog.count() - 1) { | |
| 162 SkASSERT(false); // original not in catalog | |
| 163 return; | |
| 164 } | |
| 165 #endif | |
| 166 } | |
| 167 | |
| 168 SubstituteMapping newMapping(original, substitute); | 148 SubstituteMapping newMapping(original, substitute); |
| 169 fSubstituteMap.append(1, &newMapping); | 149 fSubstituteMap.append(1, &newMapping); |
| 170 | |
| 171 // Add resource objects of substitute object to catalog. | |
| 172 SkTSet<SkPDFObject*>* targetSet = getSubstituteList(onFirstPage); | |
| 173 SkTSet<SkPDFObject*> newResourceObjects; | |
| 174 newMapping.fSubstitute->getResources(*targetSet, &newResourceObjects); | |
| 175 for (int i = 0; i < newResourceObjects.count(); ++i) { | |
| 176 addObject(newResourceObjects[i], onFirstPage); | |
| 177 } | |
| 178 // mergeInto returns the number of duplicates. | |
| 179 // If there are duplicates, there is a bug and we mess ref counting. | |
| 180 SkDEBUGCODE(int duplicates =) targetSet->mergeInto(newResourceObjects); | |
| 181 SkASSERT(duplicates == 0); | |
| 182 } | 150 } |
| 183 | 151 |
| 184 SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) { | 152 SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) { |
| 185 for (int i = 0; i < fSubstituteMap.count(); ++i) { | 153 for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 186 if (object == fSubstituteMap[i].fOriginal) { | 154 if (object == fSubstituteMap[i].fOriginal) { |
| 187 return fSubstituteMap[i].fSubstitute; | 155 return fSubstituteMap[i].fSubstitute; |
| 188 } | 156 } |
| 189 } | 157 } |
| 190 return object; | 158 return object; |
| 191 } | 159 } |
| 192 | 160 |
| 193 SkTSet<SkPDFObject*>* SkPDFCatalog::getSubstituteList(bool firstPage) { | |
| 194 return firstPage ? &fSubstituteResourcesFirstPage : | |
| 195 &fSubstituteResourcesRemaining; | |
| 196 } | |
| OLD | NEW |