aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/PdfViewer/pdfparser/native/SkPdfNativeObject.cpp
blob: 4b5f59e401036f865d35b2c141cdc7e0e688cfd0 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

#include "SkPdfNativeObject.h"

// TODO(edisonn): mac builder does not find the header ... but from headers is ok
//#include "SkPdfStreamCommonDictionary_autogen.h"
#include "SkPdfHeaders_autogen.h"

#include "SkFlate.h"
#include "SkStream.h"
#include "SkPdfNativeTokenizer.h"

#include "SkBitmap.h"
#include "SkPdfFont.h"

SkPdfNativeObject SkPdfNativeObject::kNull = SkPdfNativeObject::makeNull();

bool SkPdfNativeObject::applyFlateDecodeFilter() {
    if (!SkFlate::HaveFlate()) {
        // TODO(edisonn): warn, make callers handle it
        return false;
    }

    const unsigned char* old = fStr.fBuffer;
    bool deleteOld = isStreamOwned();

    SkMemoryStream skstream(fStr.fBuffer, fStr.fBytes >> 2, false);
    SkDynamicMemoryWStream uncompressedData;

    if (SkFlate::Inflate(&skstream, &uncompressedData)) {
        fStr.fBytes = (uncompressedData.bytesWritten() << 2) + kOwnedStreamBit + kUnfilteredStreamBit;
        fStr.fBuffer = (const unsigned char*)new unsigned char[uncompressedData.bytesWritten()];
        uncompressedData.copyTo((void*)fStr.fBuffer);

        if (deleteOld) {
            delete[] old;
        }

        return true;
    } else {
        // TODO(edisonn): warn, make callers handle it
        return false;
    }
}

bool SkPdfNativeObject::applyDCTDecodeFilter() {
    // this would fail, and it won't allow any more filters.
    // technically, it would be possible, but not a real world scenario
    // TODO(edisonn): or get the image here and store it for fast retrieval?
    return false;
}

bool SkPdfNativeObject::applyFilter(const char* name) {
    if (strcmp(name, "FlateDecode") == 0) {
        return applyFlateDecodeFilter();
    } else if (strcmp(name, "DCTDecode") == 0) {
        return applyDCTDecodeFilter();
    }
    // TODO(edisonn): allert, not supported, but should be implemented asap
    return false;
}

bool SkPdfNativeObject::filterStream() {
    if (!hasStream()) {
        return false;
    }

    if (isStreamFiltered()) {
        return true;
    }

    SkPdfStreamCommonDictionary* stream = (SkPdfStreamCommonDictionary*)this;

    if (!stream->has_Filter()) {
        fStr.fBytes = ((fStr.fBytes >> 1) << 1) + kFilteredStreamBit;
    } else if (stream->isFilterAName(NULL)) {
        std::string filterName = stream->getFilterAsName(NULL);
        applyFilter(filterName.c_str());
    } else if (stream->isFilterAArray(NULL)) {
        const SkPdfArray* filters = stream->getFilterAsArray(NULL);
        int cnt = filters->size();
        for (int i = cnt - 1; i >= 0; i--) {
            const SkPdfNativeObject* filterName = filters->objAtAIndex(i);
            if (filterName != NULL && filterName->isName()) {
                if (!applyFilter(filterName->nameValue())) {
                    break;
                }
            } else {
                // TODO(edisonn): report warning
            }
        }
    }

    return true;
}

void SkPdfNativeObject::releaseData() {
    if (fData) {
        switch (fDataType) {
            case kFont_Data:
                delete (SkPdfFont*)fData;
                break;
            case kBitmap_Data:
                delete (SkBitmap*)fData;
                break;
            default:
                SkASSERT(false);
                break;
        }
    }
    fData = NULL;
    fDataType = kEmpty_Data;
}