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 Google Inc. |
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 | |
reed@google.com | 8a85d0c | 2011-06-24 19:12:12 +0000 | [diff] [blame] | 10 | #include "SkData.h" |
halcanary | d9e5715 | 2015-08-12 11:24:40 -0700 | [diff] [blame] | 11 | #include "SkDeflate.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 12 | #include "SkPDFStream.h" |
| 13 | #include "SkStream.h" |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 14 | #include "SkStreamPriv.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 15 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 16 | SkPDFStream::~SkPDFStream() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 17 | |
halcanary | bae235e | 2016-03-21 10:05:23 -0700 | [diff] [blame] | 18 | void SkPDFStream::drop() { |
| 19 | fCompressedData.reset(nullptr); |
| 20 | this->SkPDFDict::drop(); |
| 21 | } |
| 22 | |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 23 | void SkPDFStream::emitObject(SkWStream* stream, |
| 24 | const SkPDFObjNumMap& objNumMap, |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 25 | const SkPDFSubstituteMap& substitutes) const { |
| 26 | SkASSERT(fCompressedData); |
halcanary | 37c46ca | 2015-03-31 12:30:20 -0700 | [diff] [blame] | 27 | this->INHERITED::emitObject(stream, objNumMap, substitutes); |
halcanary | 725c620 | 2015-08-20 08:09:37 -0700 | [diff] [blame] | 28 | // duplicate (a cheap operation) preserves const on fCompressedData. |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 29 | SkAutoTDelete<SkStreamRewindable> dup(fCompressedData->duplicate()); |
| 30 | SkASSERT(dup); |
| 31 | SkASSERT(dup->hasLength()); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 32 | stream->writeText(" stream\n"); |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 33 | stream->writeStream(dup.get(), dup->getLength()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 34 | stream->writeText("\nendstream"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 35 | } |
| 36 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 37 | void SkPDFStream::setData(SkStream* stream) { |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 38 | SkASSERT(!fCompressedData); // Only call this function once. |
halcanary | af9c85d | 2015-03-31 08:22:01 -0700 | [diff] [blame] | 39 | SkASSERT(stream); |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 40 | // Code assumes that the stream starts at the beginning. |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 41 | |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 42 | SkDynamicMemoryWStream compressedData; |
| 43 | SkDeflateWStream deflateWStream(&compressedData); |
| 44 | SkStreamCopy(&deflateWStream, stream); |
| 45 | deflateWStream.finalize(); |
| 46 | size_t length = compressedData.bytesWritten(); |
| 47 | |
| 48 | if (stream->hasLength()) { |
| 49 | SkAutoTDelete<SkStreamRewindable> dup(stream->duplicate()); |
| 50 | if (dup && dup->hasLength() && |
| 51 | dup->getLength() <= length + strlen("/Filter_/FlateDecode_")) { |
| 52 | this->insertInt("Length", dup->getLength()); |
mtklein | 18300a3 | 2016-03-16 13:53:35 -0700 | [diff] [blame] | 53 | fCompressedData.reset(dup.release()); |
halcanary | a060eba | 2015-08-19 12:26:46 -0700 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | } |
| 57 | fCompressedData.reset(compressedData.detachAsStream()); |
| 58 | this->insertName("Filter", "FlateDecode"); |
| 59 | this->insertInt("Length", length); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 60 | } |