aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/pdf/SkPDFStream.cpp
blob: 193723ddc2c770b49170442e05ade0a13ca8812d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

/*
 * Copyright 2010 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */


#include "SkData.h"
#include "SkDeflate.h"
#include "SkPDFStream.h"
#include "SkStream.h"
#include "SkStreamPriv.h"

SkPDFStream::~SkPDFStream() {}

void SkPDFStream::drop() {
    fCompressedData.reset(nullptr);
    this->SkPDFDict::drop();
}

void SkPDFStream::emitObject(SkWStream* stream,
                             const SkPDFObjNumMap& objNumMap,
                             const SkPDFSubstituteMap& substitutes) const {
    SkASSERT(fCompressedData);
    this->INHERITED::emitObject(stream, objNumMap, substitutes);
    // duplicate (a cheap operation) preserves const on fCompressedData.
    SkAutoTDelete<SkStreamRewindable> dup(fCompressedData->duplicate());
    SkASSERT(dup);
    SkASSERT(dup->hasLength());
    stream->writeText(" stream\n");
    stream->writeStream(dup.get(), dup->getLength());
    stream->writeText("\nendstream");
}

void SkPDFStream::setData(SkStream* stream) {
    SkASSERT(!fCompressedData);  // Only call this function once.
    SkASSERT(stream);
    // 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.release());
            return;
        }
    }
    fCompressedData.reset(compressedData.detachAsStream());
    this->insertName("Filter", "FlateDecode");
    this->insertInt("Length", length);
}