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" |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 11 | #include "SkFlate.h" |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 12 | #include "SkPDFCatalog.h" |
| 13 | #include "SkPDFStream.h" |
| 14 | #include "SkStream.h" |
| 15 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 16 | static bool skip_compression(SkPDFCatalog* catalog) { |
vandebo@chromium.org | 238be8c | 2012-07-13 20:06:02 +0000 | [diff] [blame] | 17 | return SkToBool(catalog->getDocumentFlags() & |
edisonn@google.com | 8c02061 | 2013-03-12 19:53:16 +0000 | [diff] [blame] | 18 | SkPDFDocument::kFavorSpeedOverSize_Flags); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 19 | } |
vandebo@chromium.org | 1cfa2c4 | 2011-01-31 19:35:43 +0000 | [diff] [blame] | 20 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 21 | SkPDFStream::SkPDFStream(SkStream* stream) |
| 22 | : fState(kUnused_State), |
| 23 | fData(stream) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 24 | SkSafeRef(stream); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) { |
| 28 | SkMemoryStream* stream = new SkMemoryStream; |
| 29 | stream->setData(data); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 30 | fData.reset(stream); // Transfer ownership. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | SkPDFStream::SkPDFStream(const SkPDFStream& pdfStream) |
| 34 | : SkPDFDict(), |
| 35 | fState(kUnused_State), |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 36 | fData(pdfStream.fData.get()) { |
| 37 | fData.get()->ref(); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 38 | bool removeLength = true; |
| 39 | // Don't uncompress an already compressed stream, but we could. |
| 40 | if (pdfStream.fState == kCompressed_State) { |
| 41 | fState = kCompressed_State; |
| 42 | removeLength = false; |
vandebo@chromium.org | a09ef97 | 2010-12-01 22:17:20 +0000 | [diff] [blame] | 43 | } |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 44 | SkPDFDict::Iter dict(pdfStream); |
| 45 | SkPDFName* key; |
| 46 | SkPDFObject* value; |
| 47 | SkPDFName lengthName("Length"); |
| 48 | for (key = dict.next(&value); key != NULL; key = dict.next(&value)) { |
| 49 | if (removeLength && *key == lengthName) { |
| 50 | continue; |
| 51 | } |
| 52 | this->insert(key, value); |
| 53 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 54 | } |
| 55 | |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 56 | SkPDFStream::~SkPDFStream() {} |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 57 | |
| 58 | void SkPDFStream::emitObject(SkWStream* stream, SkPDFCatalog* catalog, |
| 59 | bool indirect) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 60 | if (indirect) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 61 | return emitIndirectObject(stream, catalog); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 62 | } |
| 63 | if (!this->populate(catalog)) { |
| 64 | return fSubstitute->emitObject(stream, catalog, indirect); |
| 65 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 66 | |
vandebo@chromium.org | d90c141 | 2011-02-24 21:50:04 +0000 | [diff] [blame] | 67 | this->INHERITED::emitObject(stream, catalog, false); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 68 | stream->writeText(" stream\n"); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 69 | stream->write(fData->getMemoryBase(), fData->getLength()); |
vandebo@chromium.org | 9b49dc0 | 2010-10-20 22:23:29 +0000 | [diff] [blame] | 70 | stream->writeText("\nendstream"); |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | size_t SkPDFStream::getOutputSize(SkPDFCatalog* catalog, bool indirect) { |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 74 | if (indirect) { |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 75 | return getIndirectOutputSize(catalog); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 76 | } |
| 77 | if (!this->populate(catalog)) { |
| 78 | return fSubstitute->getOutputSize(catalog, indirect); |
| 79 | } |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 80 | |
vandebo@chromium.org | d90c141 | 2011-02-24 21:50:04 +0000 | [diff] [blame] | 81 | return this->INHERITED::getOutputSize(catalog, false) + |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 82 | strlen(" stream\n\nendstream") + fData->getLength(); |
| 83 | } |
| 84 | |
| 85 | SkPDFStream::SkPDFStream() : fState(kUnused_State) {} |
| 86 | |
| 87 | void SkPDFStream::setData(SkStream* stream) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 88 | fData.reset(stream); |
| 89 | SkSafeRef(stream); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | bool SkPDFStream::populate(SkPDFCatalog* catalog) { |
| 93 | if (fState == kUnused_State) { |
| 94 | if (!skip_compression(catalog) && SkFlate::HaveFlate()) { |
| 95 | SkDynamicMemoryWStream compressedData; |
| 96 | |
| 97 | SkAssertResult(SkFlate::Deflate(fData.get(), &compressedData)); |
| 98 | if (compressedData.getOffset() < fData->getLength()) { |
| 99 | SkMemoryStream* stream = new SkMemoryStream; |
vandebo@chromium.org | 610f716 | 2012-03-14 18:34:15 +0000 | [diff] [blame] | 100 | stream->setData(compressedData.copyToData())->unref(); |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 101 | fData.reset(stream); // Transfer ownership. |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 102 | insertName("Filter", "FlateDecode"); |
| 103 | } |
| 104 | fState = kCompressed_State; |
| 105 | } else { |
| 106 | fState = kNoCompression_State; |
| 107 | } |
| 108 | insertInt("Length", fData->getLength()); |
| 109 | } else if (fState == kNoCompression_State && !skip_compression(catalog) && |
| 110 | SkFlate::HaveFlate()) { |
| 111 | if (!fSubstitute.get()) { |
vandebo@chromium.org | d96d17b | 2013-01-04 19:31:24 +0000 | [diff] [blame] | 112 | fSubstitute.reset(new SkPDFStream(*this)); |
vandebo@chromium.org | 421d644 | 2011-07-20 17:39:01 +0000 | [diff] [blame] | 113 | catalog->setSubstitute(this, fSubstitute.get()); |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | return true; |
vandebo@chromium.org | 8459d4e | 2010-09-24 22:25:30 +0000 | [diff] [blame] | 118 | } |