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 | 15 |
16 static bool skip_compression(SkPDFCatalog* catalog) { | 16 static bool skip_compression(SkPDFCatalog* catalog) { |
17 return SkToBool(catalog->getDocumentFlags() & | 17 return SkToBool(catalog->getDocumentFlags() & |
18 SkPDFDocument::kFavorSpeedOverSize_Flags); | 18 SkPDFDocument::kFavorSpeedOverSize_Flags); |
19 } | 19 } |
20 | 20 |
21 SkPDFStream::SkPDFStream(SkStream* stream) | 21 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { |
22 : fState(kUnused_State), | 22 setData(stream); |
23 fData(stream) { | |
24 SkSafeRef(stream); | |
25 } | 23 } |
26 | 24 |
27 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { | 25 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { |
28 setData(data); | 26 setData(data); |
29 } | 27 } |
30 | 28 |
31 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) | 29 SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) |
32 : SkPDFDict(), | 30 : SkPDFDict(), |
33 fState(kUnused_State), | 31 fState(kUnused_State) { |
34 fData(pdfStream.fData.get()) { | 32 setData(pdfStream.fData.get()); |
35 fData.get()->ref(); | |
36 bool removeLength = true; | 33 bool removeLength = true; |
37 // Don't uncompress an already compressed stream, but we could. | 34 // Don't uncompress an already compressed stream, but we could. |
38 if (pdfStream.fState == kCompressed_State) { | 35 if (pdfStream.fState == kCompressed_State) { |
39 fState = kCompressed_State; | 36 fState = kCompressed_State; |
40 removeLength = false; | 37 removeLength = false; |
41 } | 38 } |
42 SkPDFDict::Iter dict(pdfStream); | 39 SkPDFDict::Iter dict(pdfStream); |
43 SkPDFName* key; | 40 SkPDFName* key; |
44 SkPDFObject* value; | 41 SkPDFObject* value; |
45 SkPDFName lengthName("Length"); | 42 SkPDFName lengthName("Length"); |
(...skipping 11 matching lines...) Expand all Loading... |
57 bool indirect) { | 54 bool indirect) { |
58 if (indirect) { | 55 if (indirect) { |
59 return emitIndirectObject(stream, catalog); | 56 return emitIndirectObject(stream, catalog); |
60 } | 57 } |
61 if (!this->populate(catalog)) { | 58 if (!this->populate(catalog)) { |
62 return fSubstitute->emitObject(stream, catalog, indirect); | 59 return fSubstitute->emitObject(stream, catalog, indirect); |
63 } | 60 } |
64 | 61 |
65 this->INHERITED::emitObject(stream, catalog, false); | 62 this->INHERITED::emitObject(stream, catalog, false); |
66 stream->writeText(" stream\n"); | 63 stream->writeText(" stream\n"); |
67 stream->write(fData->getMemoryBase(), fData->getLength()); | 64 stream->writeStream(fData.get(), fData->getLength()); |
| 65 fData->rewind(); |
68 stream->writeText("\nendstream"); | 66 stream->writeText("\nendstream"); |
69 } | 67 } |
70 | 68 |
71 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { | 69 size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { |
72 if (indirect) { | 70 if (indirect) { |
73 return getIndirectOutputSize(catalog); | 71 return getIndirectOutputSize(catalog); |
74 } | 72 } |
75 if (!this->populate(catalog)) { | 73 if (!this->populate(catalog)) { |
76 return fSubstitute->getOutputSize(catalog, indirect); | 74 return fSubstitute->getOutputSize(catalog, indirect); |
77 } | 75 } |
78 | 76 |
79 return this->INHERITED::getOutputSize(catalog, false) + | 77 return this->INHERITED::getOutputSize(catalog, false) + |
80 strlen(" stream\n\nendstream") + fData->getLength(); | 78 strlen(" stream\n\nendstream") + fData->getLength(); |
81 } | 79 } |
82 | 80 |
83 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} | 81 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} |
84 | 82 |
85 void SkPDFStream::setData(SkData* data) { | 83 void SkPDFStream::setData(SkData* data) { |
86 SkMemoryStream* stream = new SkMemoryStream; | 84 SkMemoryStream* stream = new SkMemoryStream; |
87 stream->setData(data); | 85 stream->setData(data); |
88 fData.reset(stream); // Transfer ownership. | 86 fData.reset(stream); // Transfer ownership. |
89 } | 87 } |
90 | 88 |
91 void SkPDFStream::setData(SkStream* stream) { | 89 void SkPDFStream::setData(SkStream* stream) { |
| 90 // Code assumes that the stream starts at the beginning and is rewindable. |
| 91 if (stream) { |
| 92 SkASSERT(stream->getPosition() == 0); |
| 93 SkASSERT(stream->rewind()); |
| 94 } |
92 fData.reset(stream); | 95 fData.reset(stream); |
93 SkSafeRef(stream); | 96 SkSafeRef(stream); |
94 } | 97 } |
95 | 98 |
96 bool SkPDFStream::populate(SkPDFCatalog* catalog) { | 99 bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
97 if (fState == kUnused_State) { | 100 if (fState == kUnused_State) { |
98 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { | 101 if (!skip_compression(catalog) && SkFlate::HaveFlate()) { |
99 SkDynamicMemoryWStream compressedData; | 102 SkDynamicMemoryWStream compressedData; |
100 | 103 |
101 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); | 104 SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); |
(...skipping 11 matching lines...) Expand all Loading... |
113 } else if (fState == kNoCompression_State && !skip_compression(catalog) && | 116 } else if (fState == kNoCompression_State && !skip_compression(catalog) && |
114 SkFlate::HaveFlate()) { | 117 SkFlate::HaveFlate()) { |
115 if (!fSubstitute.get()) { | 118 if (!fSubstitute.get()) { |
116 fSubstitute.reset(new SkPDFStream(*this)); | 119 fSubstitute.reset(new SkPDFStream(*this)); |
117 catalog->setSubstitute(this, fSubstitute.get()); | 120 catalog->setSubstitute(this, fSubstitute.get()); |
118 } | 121 } |
119 return false; | 122 return false; |
120 } | 123 } |
121 return true; | 124 return true; |
122 } | 125 } |
OLD | NEW |