epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 The Android Open Source Project |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 10 | #include "SkPDFCatalog.h" |
| 11 | #include "SkPDFTypes.h" |
| 12 | #include "SkStream.h" |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 13 | #include "SkTypes.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 14 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 15 | SkPDFCatalog::SkPDFCatalog(SkPDFDocument::Flags flags) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 16 | : fFirstPageCount(0), |
| 17 | fNextObjNum(1), |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 18 | fNextFirstPageObjNum(0), |
| 19 | fDocumentFlags(flags) { |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 20 | } |
| 21 | |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 22 | SkPDFCatalog::~SkPDFCatalog() { |
| 23 | fSubstituteResourcesRemaining.safeUnrefAll(); |
| 24 | fSubstituteResourcesFirstPage.safeUnrefAll(); |
| 25 | } |
vandebo@chromium.org | f66025d | 2010-10-01 23:26:55 +0000 | [diff] [blame] | 26 | |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 27 | SkPDFObject* SkPDFCatalog::addObject(SkPDFObject* obj, bool onFirstPage) { |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 28 | if (findObjectIndex(obj) != -1) { // object already added |
| 29 | return obj; |
| 30 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 31 | SkASSERT(fNextFirstPageObjNum == 0); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 32 | if (onFirstPage) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 33 | fFirstPageCount++; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 34 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 35 | |
| 36 | struct Rec newEntry(obj, onFirstPage); |
| 37 | fCatalog.append(1, &newEntry); |
vandebo@chromium.org | f7c1576 | 2011-02-01 22:19:44 +0000 | [diff] [blame] | 38 | return obj; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 39 | } |
| 40 | |
vandebo@chromium.org | 30580f6 | 2012-07-12 20:22:04 +0000 | [diff] [blame] | 41 | size_t SkPDFCatalog::setFileOffset(SkPDFObject* obj, off_t offset) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 42 | int objIndex = assignObjNum(obj) - 1; |
| 43 | SkASSERT(fCatalog[objIndex].fObjNumAssigned); |
| 44 | SkASSERT(fCatalog[objIndex].fFileOffset == 0); |
| 45 | fCatalog[objIndex].fFileOffset = offset; |
| 46 | |
vandebo@chromium.org | d3a094c | 2011-07-25 22:22:25 +0000 | [diff] [blame] | 47 | return getSubstituteObject(obj)->getOutputSize(this, true); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 48 | } |
| 49 | |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 50 | void SkPDFCatalog::emitObjectNumber(SkWStream* stream, SkPDFObject* obj) { |
| 51 | stream->writeDecAsText(assignObjNum(obj)); |
| 52 | stream->writeText(" 0"); // Generation number is always 0. |
| 53 | } |
| 54 | |
| 55 | size_t SkPDFCatalog::getObjectNumberSize(SkPDFObject* obj) { |
| 56 | SkDynamicMemoryWStream buffer; |
| 57 | emitObjectNumber(&buffer, obj); |
| 58 | return buffer.getOffset(); |
| 59 | } |
| 60 | |
| 61 | int SkPDFCatalog::findObjectIndex(SkPDFObject* obj) const { |
| 62 | for (int i = 0; i < fCatalog.count(); i++) { |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 63 | if (fCatalog[i].fObject == obj) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 64 | return i; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 65 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 66 | } |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 67 | // If it's not in the main array, check if it's a substitute object. |
| 68 | for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 69 | if (fSubstituteMap[i].fSubstitute == obj) { |
| 70 | return findObjectIndex(fSubstituteMap[i].fOriginal); |
| 71 | } |
| 72 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | int SkPDFCatalog::assignObjNum(SkPDFObject* obj) { |
| 77 | int pos = findObjectIndex(obj); |
vandebo@chromium.org | da912d6 | 2011-03-08 18:31:02 +0000 | [diff] [blame] | 78 | // If this assert fails, it means you probably forgot to add an object |
| 79 | // to the resource list. |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 80 | SkASSERT(pos >= 0); |
| 81 | uint32_t currentIndex = pos; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 82 | if (fCatalog[currentIndex].fObjNumAssigned) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 83 | return currentIndex + 1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 84 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 85 | |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 86 | // First assignment. |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 87 | if (fNextFirstPageObjNum == 0) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 88 | fNextFirstPageObjNum = fCatalog.count() - fFirstPageCount + 1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 89 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 90 | |
| 91 | uint32_t objNum; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 92 | if (fCatalog[currentIndex].fOnFirstPage) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 93 | objNum = fNextFirstPageObjNum; |
| 94 | fNextFirstPageObjNum++; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 95 | } else { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 96 | objNum = fNextObjNum; |
| 97 | fNextObjNum++; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // When we assign an object an object number, we put it in that array |
| 101 | // offset (minus 1 because object number 0 is reserved). |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 102 | SkASSERT(!fCatalog[objNum - 1].fObjNumAssigned); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 103 | if (objNum - 1 != currentIndex) { |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 104 | SkTSwap(fCatalog[objNum - 1], fCatalog[currentIndex]); |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 105 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 106 | fCatalog[objNum - 1].fObjNumAssigned = true; |
| 107 | return objNum; |
| 108 | } |
| 109 | |
| 110 | int32_t SkPDFCatalog::emitXrefTable(SkWStream* stream, bool firstPage) { |
| 111 | int first = -1; |
| 112 | int last = fCatalog.count() - 1; |
ctguil@chromium.org | 769fa6a | 2011-08-20 00:36:18 +0000 | [diff] [blame] | 113 | // TODO(vandebo): Support linearized format. |
ctguil@chromium.org | a5c7234 | 2011-08-15 23:55:03 +0000 | [diff] [blame] | 114 | // int last = fCatalog.count() - fFirstPageCount - 1; |
| 115 | // if (firstPage) { |
| 116 | // first = fCatalog.count() - fFirstPageCount; |
| 117 | // last = fCatalog.count() - 1; |
| 118 | // } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 119 | |
| 120 | stream->writeText("xref\n"); |
| 121 | stream->writeDecAsText(first + 1); |
| 122 | stream->writeText(" "); |
| 123 | stream->writeDecAsText(last - first + 1); |
| 124 | stream->writeText("\n"); |
| 125 | |
| 126 | if (first == -1) { |
| 127 | stream->writeText("0000000000 65535 f \n"); |
| 128 | first++; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 129 | } |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 130 | for (int i = first; i <= last; i++) { |
reed@google.com | 46ee9c6 | 2013-03-11 18:55:04 +0000 | [diff] [blame] | 131 | // For 32 bits platforms, the maximum offset has to fit within off_t |
| 132 | // which is a 32 bits signed integer on these platforms. |
| 133 | SkDEBUGCODE(static const off_t kMaxOff = SK_MaxS32;) |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 134 | SkASSERT(fCatalog[i].fFileOffset > 0); |
sugoi@google.com | dfc867b | 2013-03-11 18:45:12 +0000 | [diff] [blame] | 135 | SkASSERT(fCatalog[i].fFileOffset < kMaxOff); |
vandebo@chromium.org | d877fdb | 2010-10-12 23:08:13 +0000 | [diff] [blame] | 136 | stream->writeBigDecAsText(fCatalog[i].fFileOffset, 10); |
| 137 | stream->writeText(" 00000 n \n"); |
| 138 | } |
| 139 | |
| 140 | return fCatalog.count() + 1; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 141 | } |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 142 | |
| 143 | void SkPDFCatalog::setSubstitute(SkPDFObject* original, |
| 144 | SkPDFObject* substitute) { |
| 145 | #if defined(SK_DEBUG) |
| 146 | // Sanity check: is the original already in substitute list? |
| 147 | for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 148 | if (original == fSubstituteMap[i].fSubstitute || |
| 149 | original == fSubstituteMap[i].fOriginal) { |
| 150 | SkASSERT(false); |
| 151 | return; |
| 152 | } |
| 153 | } |
| 154 | #endif |
| 155 | // Check if the original is on first page. |
| 156 | bool onFirstPage = false; |
| 157 | for (int i = 0; i < fCatalog.count(); ++i) { |
| 158 | if (fCatalog[i].fObject == original) { |
| 159 | onFirstPage = fCatalog[i].fOnFirstPage; |
| 160 | break; |
| 161 | } |
| 162 | #if defined(SK_DEBUG) |
| 163 | if (i == fCatalog.count() - 1) { |
| 164 | SkASSERT(false); // original not in catalog |
| 165 | return; |
| 166 | } |
| 167 | #endif |
| 168 | } |
| 169 | |
| 170 | SubstituteMapping newMapping(original, substitute); |
| 171 | fSubstituteMap.append(1, &newMapping); |
| 172 | |
| 173 | // Add resource objects of substitute object to catalog. |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 174 | SkTSet<SkPDFObject*>* targetSet = getSubstituteList(onFirstPage); |
| 175 | SkTSet<SkPDFObject*> newResourceObjects; |
| 176 | newMapping.fSubstitute->getResources(*targetSet, &newResourceObjects); |
| 177 | for (int i = 0; i < newResourceObjects.count(); ++i) { |
| 178 | addObject(newResourceObjects[i], onFirstPage); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 179 | } |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 180 | // mergeInto returns the number of duplicates. |
| 181 | // If there are duplicates, there is a bug and we mess ref counting. |
| 182 | SkDEBUGCODE(int duplicates =) targetSet->mergeInto(newResourceObjects); |
| 183 | SkASSERT(duplicates == 0); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | SkPDFObject* SkPDFCatalog::getSubstituteObject(SkPDFObject* object) { |
| 187 | for (int i = 0; i < fSubstituteMap.count(); ++i) { |
| 188 | if (object == fSubstituteMap[i].fOriginal) { |
| 189 | return fSubstituteMap[i].fSubstitute; |
| 190 | } |
| 191 | } |
| 192 | return object; |
| 193 | } |
| 194 | |
| 195 | off_t SkPDFCatalog::setSubstituteResourcesOffsets(off_t fileOffset, |
| 196 | bool firstPage) { |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 197 | SkTSet<SkPDFObject*>* targetSet = getSubstituteList(firstPage); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 198 | off_t offsetSum = fileOffset; |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 199 | for (int i = 0; i < targetSet->count(); ++i) { |
| 200 | offsetSum += setFileOffset((*targetSet)[i], offsetSum); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 201 | } |
vandebo@chromium.org | f5181a4 | 2011-07-20 00:33:53 +0000 | [diff] [blame] | 202 | return offsetSum - fileOffset; |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void SkPDFCatalog::emitSubstituteResources(SkWStream *stream, bool firstPage) { |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 206 | SkTSet<SkPDFObject*>* targetSet = getSubstituteList(firstPage); |
| 207 | for (int i = 0; i < targetSet->count(); ++i) { |
| 208 | (*targetSet)[i]->emit(stream, this, true); |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
edisonn@google.com | 6addb19 | 2013-04-02 15:33:08 +0000 | [diff] [blame] | 212 | SkTSet<SkPDFObject*>* SkPDFCatalog::getSubstituteList(bool firstPage) { |
vandebo@chromium.org | 2ef12d4 | 2011-07-06 23:31:24 +0000 | [diff] [blame] | 213 | return firstPage ? &fSubstituteResourcesFirstPage : |
| 214 | &fSubstituteResourcesRemaining; |
vandebo@chromium.org | f5181a4 | 2011-07-20 00:33:53 +0000 | [diff] [blame] | 215 | } |