| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright 2010 Google Inc. | |
| 3 * | |
| 4 * Use of this source code is governed by a BSD-style license that can be | |
| 5 * found in the LICENSE file. | |
| 6 */ | |
| 7 | |
| 8 | |
| 9 #include "SkData.h" | |
| 10 #include "SkDeflate.h" | |
| 11 #include "SkPDFStream.h" | |
| 12 #include "SkStreamPriv.h" | |
| 13 | |
| 14 SkPDFStream::~SkPDFStream() {} | |
| 15 | |
| 16 void SkPDFStream::drop() { | |
| 17 fCompressedData.reset(nullptr); | |
| 18 this->SkPDFDict::drop(); | |
| 19 } | |
| 20 | |
| 21 void SkPDFStream::emitObject(SkWStream* stream, | |
| 22 const SkPDFObjNumMap& objNumMap, | |
| 23 const SkPDFSubstituteMap& substitutes) const { | |
| 24 SkASSERT(fCompressedData); | |
| 25 this->INHERITED::emitObject(stream, objNumMap, substitutes); | |
| 26 // duplicate (a cheap operation) preserves const on fCompressedData. | |
| 27 std::unique_ptr<SkStreamAsset> dup(fCompressedData->duplicate()); | |
| 28 SkASSERT(dup); | |
| 29 SkASSERT(dup->hasLength()); | |
| 30 stream->writeText(" stream\n"); | |
| 31 stream->writeStream(dup.get(), dup->getLength()); | |
| 32 stream->writeText("\nendstream"); | |
| 33 } | |
| 34 | |
| 35 void SkPDFStream::setData(std::unique_ptr<SkStreamAsset> stream) { | |
| 36 SkASSERT(!fCompressedData); // Only call this function once. | |
| 37 SkASSERT(stream); | |
| 38 // Code assumes that the stream starts at the beginning. | |
| 39 | |
| 40 #ifdef SK_PDF_LESS_COMPRESSION | |
| 41 fCompressedData = std::move(stream); | |
| 42 SkASSERT(fCompressedData && fCompressedData->hasLength()); | |
| 43 this->insertInt("Length", fCompressedData->getLength()); | |
| 44 #else | |
| 45 | |
| 46 SkASSERT(stream->hasLength()); | |
| 47 SkDynamicMemoryWStream compressedData; | |
| 48 SkDeflateWStream deflateWStream(&compressedData); | |
| 49 SkStreamCopy(&deflateWStream, stream.get()); | |
| 50 deflateWStream.finalize(); | |
| 51 size_t compressedLength = compressedData.bytesWritten(); | |
| 52 size_t originalLength = stream->getLength(); | |
| 53 | |
| 54 if (originalLength <= compressedLength + strlen("/Filter_/FlateDecode_")) { | |
| 55 SkAssertResult(stream->rewind()); | |
| 56 fCompressedData = std::move(stream); | |
| 57 this->insertInt("Length", originalLength); | |
| 58 return; | |
| 59 } | |
| 60 fCompressedData.reset(compressedData.detachAsStream()); | |
| 61 this->insertName("Filter", "FlateDecode"); | |
| 62 this->insertInt("Length", compressedLength); | |
| 63 #endif | |
| 64 } | |
| OLD | NEW |