Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(259)

Side by Side Diff: src/pdf/SkPDFCatalog.cpp

Issue 870333002: Simplify reference management in SkPDF (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: tests Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 stream->writeDecAsText(assignObjNum(obj)); 49 stream->writeDecAsText(assignObjNum(obj));
50 stream->writeText(" 0"); // Generation number is always 0. 50 stream->writeText(" 0"); // Generation number is always 0.
51 } 51 }
52 52
53 size_t SkPDFCatalog::getObjectNumberSize(SkPDFObject* obj) { 53 size_t SkPDFCatalog::getObjectNumberSize(SkPDFObject* obj) {
54 SkDynamicMemoryWStream buffer; 54 SkDynamicMemoryWStream buffer;
55 emitObjectNumber(&buffer, obj); 55 emitObjectNumber(&buffer, obj);
56 return buffer.getOffset(); 56 return buffer.getOffset();
57 } 57 }
58 58
59 int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) const { 59 int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) {
mtklein 2015/01/26 23:38:59 Seems like this wants a new name, lookupOrAdd?
hal.canary 2015/02/09 23:35:01 I'm going to heavily refactor this class in a late
60 for (int i = 0; i < fCatalog.count(); i++) { 60 for (int i = 0; i < fCatalog.count(); i++) {
61 if (fCatalog[i].fObject == obj) { 61 if (fCatalog[i].fObject == obj) {
62 return i; 62 return i;
63 } 63 }
64 } 64 }
65 // If it's not in the main array, check if it's a substitute object. 65 // If it's not in the main array, check if it's a substitute object.
66 for (int i = 0; i < fSubstituteMap.count(); ++i) { 66 for (int i = 0; i < fSubstituteMap.count(); ++i) {
67 if (fSubstituteMap[i].fSubstitute == obj) { 67 if (fSubstituteMap[i].fSubstitute == obj) {
68 return findObjectIndex(fSubstituteMap[i].fOriginal); 68 return findObjectIndex(fSubstituteMap[i].fOriginal);
69 } 69 }
70 } 70 }
71 return -1; 71 struct Rec newEntry(obj, false);
72 fCatalog.append(1, &newEntry);
73 return fCatalog.count() - 1;
72 } 74 }
73 75
74 int SkPDFCatalog::assignObjNum(SkPDFObject* obj) { 76 int SkPDFCatalog::assignObjNum(SkPDFObject* obj) {
75 int pos = findObjectIndex(obj); 77 int pos = findObjectIndex(obj);
76 // If this assert fails, it means you probably forgot to add an object 78 // If this assert fails, it means you probably forgot to add an object
77 // to the resource list. 79 // to the resource list.
78 SkASSERT(pos >= 0); 80 SkASSERT(pos >= 0);
79 uint32_t currentIndex = pos; 81 uint32_t currentIndex = pos;
80 if (fCatalog[currentIndex].fObjNumAssigned) { 82 if (fCatalog[currentIndex].fObjNumAssigned) {
81 return currentIndex + 1; 83 return currentIndex + 1;
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 #if defined(SK_DEBUG) 145 #if defined(SK_DEBUG)
144 // Sanity check: is the original already in substitute list? 146 // Sanity check: is the original already in substitute list?
145 for (int i = 0; i < fSubstituteMap.count(); ++i) { 147 for (int i = 0; i < fSubstituteMap.count(); ++i) {
146 if (original == fSubstituteMap[i].fSubstitute || 148 if (original == fSubstituteMap[i].fSubstitute ||
147 original == fSubstituteMap[i].fOriginal) { 149 original == fSubstituteMap[i].fOriginal) {
148 SkASSERT(false); 150 SkASSERT(false);
149 return; 151 return;
150 } 152 }
151 } 153 }
152 #endif 154 #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); 155 SubstituteMapping newMapping(original, substitute);
169 fSubstituteMap.append(1, &newMapping); 156 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 } 157 }
183 158
184 SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) { 159 SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) {
185 for (int i = 0; i < fSubstituteMap.count(); ++i) { 160 for (int i = 0; i < fSubstituteMap.count(); ++i) {
186 if (object == fSubstituteMap[i].fOriginal) { 161 if (object == fSubstituteMap[i].fOriginal) {
187 return fSubstituteMap[i].fSubstitute; 162 return fSubstituteMap[i].fSubstitute;
188 } 163 }
189 } 164 }
190 return object; 165 return object;
191 } 166 }
192 167
193 SkTSet<SkPDFObject*>* SkPDFCatalog::getSubstituteList(bool firstPage) {
194 return firstPage ? &fSubstituteResourcesFirstPage :
195 &fSubstituteResourcesRemaining;
196 }
OLDNEW
« no previous file with comments | « src/pdf/SkPDFCatalog.h ('k') | src/pdf/SkPDFDocument.cpp » ('j') | src/pdf/SkPDFGraphicState.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698