| 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 "SkDeflate.h" | 11 #include "SkDeflate.h" |
| 12 #include "SkPDFStream.h" | 12 #include "SkPDFStream.h" |
| 13 #include "SkStream.h" | 13 #include "SkStream.h" |
| 14 #include "SkStreamPriv.h" | 14 #include "SkStreamPriv.h" |
| 15 | 15 |
| 16 SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) { | |
| 17 this->setData(stream); | |
| 18 } | |
| 19 | |
| 20 SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { | |
| 21 this->setData(data); | |
| 22 } | |
| 23 | |
| 24 SkPDFStream::~SkPDFStream() {} | 16 SkPDFStream::~SkPDFStream() {} |
| 25 | 17 |
| 26 void SkPDFStream::emitObject(SkWStream* stream, | 18 void SkPDFStream::emitObject(SkWStream* stream, |
| 27 const SkPDFObjNumMap& objNumMap, | 19 const SkPDFObjNumMap& objNumMap, |
| 28 const SkPDFSubstituteMap& substitutes) { | 20 const SkPDFSubstituteMap& substitutes) const { |
| 29 if (fState == kUnused_State) { | 21 SkASSERT(fCompressedData); |
| 30 fState = kNoCompression_State; | |
| 31 SkDynamicMemoryWStream compressedData; | |
| 32 SkDeflateWStream deflateWStream(&compressedData); | |
| 33 SkAssertResult(SkStreamCopy(&deflateWStream, fDataStream.get())); | |
| 34 deflateWStream.finalize(); | |
| 35 SkAssertResult(fDataStream->rewind()); | |
| 36 if (compressedData.getOffset() < this->dataSize()) { | |
| 37 SkAutoTDelete<SkStream> compressed( | |
| 38 compressedData.detachAsStream()); | |
| 39 this->setData(compressed.get()); | |
| 40 this->insertName("Filter", "FlateDecode"); | |
| 41 } | |
| 42 fState = kCompressed_State; | |
| 43 this->insertInt("Length", this->dataSize()); | |
| 44 } | |
| 45 this->INHERITED::emitObject(stream, objNumMap, substitutes); | 22 this->INHERITED::emitObject(stream, objNumMap, substitutes); |
| 23 // Note: emitObject isn't marked const, but could be in the future |
| 24 SkAutoTDelete<SkStreamRewindable> dup(fCompressedData->duplicate()); |
| 25 SkASSERT(dup); |
| 26 SkASSERT(dup->hasLength()); |
| 46 stream->writeText(" stream\n"); | 27 stream->writeText(" stream\n"); |
| 47 stream->writeStream(fDataStream.get(), fDataStream->getLength()); | 28 stream->writeStream(dup.get(), dup->getLength()); |
| 48 SkAssertResult(fDataStream->rewind()); | |
| 49 stream->writeText("\nendstream"); | 29 stream->writeText("\nendstream"); |
| 50 } | 30 } |
| 51 | 31 |
| 52 SkPDFStream::SkPDFStream() : fState(kUnused_State) {} | 32 void SkPDFStream::setData(SkStream* stream) { |
| 33 SkASSERT(!fCompressedData); // Only call this function once. |
| 34 SkASSERT(stream); |
| 35 // Code assumes that the stream starts at the beginning. |
| 53 | 36 |
| 54 void SkPDFStream::setData(SkData* data) { | 37 SkDynamicMemoryWStream compressedData; |
| 55 // FIXME: Don't swap if the data is the same. | 38 SkDeflateWStream deflateWStream(&compressedData); |
| 56 fDataStream.reset(SkNEW_ARGS(SkMemoryStream, (data))); | 39 SkStreamCopy(&deflateWStream, stream); |
| 40 deflateWStream.finalize(); |
| 41 size_t length = compressedData.bytesWritten(); |
| 42 |
| 43 if (stream->hasLength()) { |
| 44 SkAutoTDelete<SkStreamRewindable> dup(stream->duplicate()); |
| 45 if (dup && dup->hasLength() && |
| 46 dup->getLength() <= length + strlen("/Filter_/FlateDecode_")) { |
| 47 this->insertInt("Length", dup->getLength()); |
| 48 fCompressedData.reset(dup.detach()); |
| 49 return; |
| 50 } |
| 51 } |
| 52 fCompressedData.reset(compressedData.detachAsStream()); |
| 53 this->insertName("Filter", "FlateDecode"); |
| 54 this->insertInt("Length", length); |
| 57 } | 55 } |
| 58 | |
| 59 void SkPDFStream::setData(SkStream* stream) { | |
| 60 SkASSERT(stream); | |
| 61 // Code assumes that the stream starts at the beginning and is rewindable. | |
| 62 // SkStreamRewindableFromSkStream will try stream->duplicate(). | |
| 63 fDataStream.reset(SkStreamRewindableFromSkStream(stream)); | |
| 64 SkASSERT(fDataStream.get()); | |
| 65 } | |
| 66 | |
| 67 size_t SkPDFStream::dataSize() const { | |
| 68 SkASSERT(fDataStream->hasLength()); | |
| 69 return fDataStream->getLength(); | |
| 70 } | |
| OLD | NEW |