Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(610)

Unified Diff: src/pdf/SkPDFStream.cpp

Issue 1304493002: SkPDF: Simplify PDFStream / emitObject() const (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: formatting Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/pdf/SkPDFStream.h ('k') | src/pdf/SkPDFTypes.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pdf/SkPDFStream.cpp
diff --git a/src/pdf/SkPDFStream.cpp b/src/pdf/SkPDFStream.cpp
index d21205cd38db12c18c0b821f9574738bc6c850e7..69ce317bb9ccd50aafa8d1c5f149e8dd8decc795 100644
--- a/src/pdf/SkPDFStream.cpp
+++ b/src/pdf/SkPDFStream.cpp
@@ -13,58 +13,43 @@
#include "SkStream.h"
#include "SkStreamPriv.h"
-SkPDFStream::SkPDFStream(SkStream* stream) : fState(kUnused_State) {
- this->setData(stream);
-}
-
-SkPDFStream::SkPDFStream(SkData* data) : fState(kUnused_State) {
- this->setData(data);
-}
-
SkPDFStream::~SkPDFStream() {}
void SkPDFStream::emitObject(SkWStream* stream,
const SkPDFObjNumMap& objNumMap,
- const SkPDFSubstituteMap& substitutes) {
- if (fState == kUnused_State) {
- fState = kNoCompression_State;
- SkDynamicMemoryWStream compressedData;
- SkDeflateWStream deflateWStream(&compressedData);
- SkAssertResult(SkStreamCopy(&deflateWStream, fDataStream.get()));
- deflateWStream.finalize();
- SkAssertResult(fDataStream->rewind());
- if (compressedData.getOffset() < this->dataSize()) {
- SkAutoTDelete<SkStream> compressed(
- compressedData.detachAsStream());
- this->setData(compressed.get());
- this->insertName("Filter", "FlateDecode");
- }
- fState = kCompressed_State;
- this->insertInt("Length", this->dataSize());
- }
+ const SkPDFSubstituteMap& substitutes) const {
+ SkASSERT(fCompressedData);
this->INHERITED::emitObject(stream, objNumMap, substitutes);
+ // Note: emitObject isn't marked const, but could be in the future
+ SkAutoTDelete<SkStreamRewindable> dup(fCompressedData->duplicate());
+ SkASSERT(dup);
+ SkASSERT(dup->hasLength());
stream->writeText(" stream\n");
- stream->writeStream(fDataStream.get(), fDataStream->getLength());
- SkAssertResult(fDataStream->rewind());
+ stream->writeStream(dup.get(), dup->getLength());
stream->writeText("\nendstream");
}
-SkPDFStream::SkPDFStream() : fState(kUnused_State) {}
-
-void SkPDFStream::setData(SkData* data) {
- // FIXME: Don't swap if the data is the same.
- fDataStream.reset(SkNEW_ARGS(SkMemoryStream, (data)));
-}
-
void SkPDFStream::setData(SkStream* stream) {
+ SkASSERT(!fCompressedData); // Only call this function once.
SkASSERT(stream);
- // Code assumes that the stream starts at the beginning and is rewindable.
- // SkStreamRewindableFromSkStream will try stream->duplicate().
- fDataStream.reset(SkStreamRewindableFromSkStream(stream));
- SkASSERT(fDataStream.get());
-}
-
-size_t SkPDFStream::dataSize() const {
- SkASSERT(fDataStream->hasLength());
- return fDataStream->getLength();
+ // Code assumes that the stream starts at the beginning.
+
+ SkDynamicMemoryWStream compressedData;
+ SkDeflateWStream deflateWStream(&compressedData);
+ SkStreamCopy(&deflateWStream, stream);
+ deflateWStream.finalize();
+ size_t length = compressedData.bytesWritten();
+
+ if (stream->hasLength()) {
+ SkAutoTDelete<SkStreamRewindable> dup(stream->duplicate());
+ if (dup && dup->hasLength() &&
+ dup->getLength() <= length + strlen("/Filter_/FlateDecode_")) {
+ this->insertInt("Length", dup->getLength());
+ fCompressedData.reset(dup.detach());
+ return;
+ }
+ }
+ fCompressedData.reset(compressedData.detachAsStream());
+ this->insertName("Filter", "FlateDecode");
+ this->insertInt("Length", length);
}
« no previous file with comments | « src/pdf/SkPDFStream.h ('k') | src/pdf/SkPDFTypes.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698