OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2010 Google Inc. | 3 * Copyright 2010 Google Inc. |
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 "SkData.h" | 10 #include "SkData.h" |
11 #include "SkFlate.h" | 11 #include "SkFlate.h" |
12 #include "SkPDFCatalog.h" | 12 #include "SkPDFCatalog.h" |
13 #include "SkPDFStream.h" | 13 #include "SkPDFStream.h" |
14 #include "SkStream.h" | 14 #include "SkStream.h" |
15 #include "SkStreamHelpers.h" // CopyStreamToData | |
16 | 15 |
17 static bool skip_compression(SkPDFCatalog* catalog) { | 16 static bool skip_compression(SkPDFCatalog* catalog) { |
18 return SkToBool(catalog->getDocumentFlags() & | 17 return SkToBool(catalog->getDocumentFlags() & |
19 SkPDFDocument::kFavorSpeedOverSize_Flags); | 18 SkPDFDocument::kFavorSpeedOverSize_Flags); |
20 } | 19 } |
21 | 20 |
22 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { | 21 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { |
23 this->setData(stream); | 22 setData(stream); |
24 } | 23 } |
25 | 24 |
26 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { | 25 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { |
27 this->setData(data); | 26 setData(data); |
28 } | 27 } |
29 | 28 |
30 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) | 29 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) |
31 : SkPDFDict(), | 30 : SkPDFDict(), |
32 fState(kUnused_State) { | 31 fState(kUnused_State) { |
33 this->setData(pdfStream.fData.get()); | 32 setData(pdfStream.fData.get()); |
34 bool removeLength = true; | 33 bool removeLength = true; |
35 // Don't uncompress an already compressed stream, but we could. | 34 // Don't uncompress an already compressed stream, but we could. |
36 if (pdfStream.fState == kCompressed_State) { | 35 if (pdfStream.fState == kCompressed_State) { |
37 fState = kCompressed_State; | 36 fState = kCompressed_State; |
38 removeLength = false; | 37 removeLength = false; |
39 } | 38 } |
40 SkPDFDict::Iter dict(pdfStream); | 39 SkPDFDict::Iter dict(pdfStream); |
41 SkPDFName* key; | 40 SkPDFName* key; |
42 SkPDFObject* value; | 41 SkPDFObject* value; |
43 SkPDFName lengthName("Length"); | 42 SkPDFName lengthName("Length"); |
44 for (key = dict.next(&value); key != NULL; key = dict.next(&value)) { | 43 for (key = dict.next(&value); key != NULL; key = dict.next(&value)) { |
45 if (removeLength && *key == lengthName) { | 44 if (removeLength && *key == lengthName) { |
46 continue; | 45 continue; |
47 } | 46 } |
48 this->insert(key, value); | 47 this->insert(key, value); |
49 } | 48 } |
50 } | 49 } |
51 | 50 |
52 SkPDFStream::~SkPDFStream() {} | 51 SkPDFStream::~SkPDFStream() {} |
53 | 52 |
54 void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, | 53 void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
55 bool indirect) { | 54 bool indirect) { |
56 if (indirect) { | 55 if (indirect) { |
57 return emitIndirectObject(stream, catalog); | 56 return emitIndirectObject(stream, catalog); |
58 } | 57 } |
59 SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit | |
60 if (!this->populate(catalog)) { | 58 if (!this->populate(catalog)) { |
61 return fSubstitute->emitObject(stream, catalog, indirect); | 59 return fSubstitute->emitObject(stream, catalog, indirect); |
62 } | 60 } |
63 | 61 |
64 this->INHERITED::emitObject(stream, catalog, false); | 62 this->INHERITED::emitObject(stream, catalog, false); |
65 stream->writeText(" stream\n"); | 63 stream->writeText(" stream\n"); |
66 if (fData.get()) { | 64 stream->writeStream(fData.get(), fData->getLength()); |
67 stream->write(fData->data(), fData->size()); | 65 fData->rewind(); |
68 } | |
69 stream->writeText("\nendstream"); | 66 stream->writeText("\nendstream"); |
70 } | 67 } |
71 | 68 |
72 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { | 69 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { |
73 if (indirect) { | 70 if (indirect) { |
74 return getIndirectOutputSize(catalog); | 71 return getIndirectOutputSize(catalog); |
75 } | 72 } |
76 SkAutoMutexAcquire lock(fMutex); // multiple threads could be calling emit | |
77 if (!this->populate(catalog)) { | 73 if (!this->populate(catalog)) { |
78 return fSubstitute->getOutputSize(catalog, indirect); | 74 return fSubstitute->getOutputSize(catalog, indirect); |
79 } | 75 } |
80 | 76 |
81 return this->INHERITED::getOutputSize(catalog, false) + | 77 return this->INHERITED::getOutputSize(catalog, false) + |
82 strlen(" stream\n\nendstream") + this->dataSize(); | 78 strlen(" stream\n\nendstream") + fData->getLength(); |
83 } | 79 } |
84 | 80 |
85 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} | 81 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} |
86 | 82 |
87 void SkPDFStream::setData(SkData* data) { | 83 void SkPDFStream::setData(SkData* data) { |
88 fData.reset(SkSafeRef(data)); | 84 SkMemoryStream* stream = new SkMemoryStream; |
| 85 stream->setData(data); |
| 86 fData.reset(stream); // Transfer ownership. |
89 } | 87 } |
90 | 88 |
91 void SkPDFStream::setData(SkStream* stream) { | 89 void SkPDFStream::setData(SkStream* stream) { |
92 // Code assumes that the stream starts at the beginning and is rewindable. | 90 // Code assumes that the stream starts at the beginning and is rewindable. |
93 if (stream) { | 91 if (stream) { |
94 SkASSERT(stream->getPosition() == 0); | 92 SkASSERT(stream->getPosition() == 0); |
95 fData.reset(CopyStreamToData(stream)); | |
96 SkASSERT(stream->rewind()); | 93 SkASSERT(stream->rewind()); |
97 } else { | |
98 fData.reset(NULL); | |
99 } | 94 } |
100 } | 95 fData.reset(stream); |
101 | 96 SkSafeRef(stream); |
102 size_t SkPDFStream::dataSize() const { | |
103 return fData.get() ? fData->size() : 0; | |
104 } | 97 } |
105 | 98 |
106 bool SkPDFStream::populate(SkPDFCatalog* catalog) { | 99 bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
107 if (fState == kUnused_State) { | 100 if (fState == kUnused_State) { |
108 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { | 101 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { |
109 SkDynamicMemoryWStream compressedData; | 102 SkDynamicMemoryWStream compressedData; |
110 | 103 |
111 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); | 104 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); |
112 if (compressedData.getOffset() < this->dataSize()) { | 105 if (compressedData.getOffset() < fData->getLength()) { |
113 fData.reset(compressedData.copyToData()); | 106 SkMemoryStream* stream = new SkMemoryStream; |
| 107 stream->setData(compressedData.copyToData())->unref(); |
| 108 fData.reset(stream); // Transfer ownership. |
114 insertName("Filter", "FlateDecode"); | 109 insertName("Filter", "FlateDecode"); |
115 } | 110 } |
116 fState = kCompressed_State; | 111 fState = kCompressed_State; |
117 } else { | 112 } else { |
118 fState = kNoCompression_State; | 113 fState = kNoCompression_State; |
119 } | 114 } |
120 insertInt("Length", this->dataSize()); | 115 insertInt("Length", fData->getLength()); |
121 } else if (fState == kNoCompression_State && !skip_compression(catalog) && | 116 } else if (fState == kNoCompression_State && !skip_compression(catalog) && |
122 SkFlate::HaveFlate()) { | 117 SkFlate::HaveFlate()) { |
123 if (!fSubstitute.get()) { | 118 if (!fSubstitute.get()) { |
124 fSubstitute.reset(new SkPDFStream(*this)); | 119 fSubstitute.reset(new SkPDFStream(*this)); |
125 catalog->setSubstitute(this, fSubstitute.get()); | 120 catalog->setSubstitute(this, fSubstitute.get()); |
126 } | 121 } |
127 return false; | 122 return false; |
128 } | 123 } |
129 return true; | 124 return true; |
130 } | 125 } |
OLD | NEW |