aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/doc/SkDocument_PDF.cpp
blob: b9b55f8dce688cefa8f1e555200d5a8a93f36f79 (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkDocument.h"
#include "SkPDFDevice.h"
#include "SkPDFDocument.h"

class SkDocument_PDF : public SkDocument {
public:
    SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*),
                   SkPicture::EncodeBitmap encoder)
            : SkDocument(stream, doneProc)
            , fEncoder(encoder) {
        fDoc = SkNEW(SkPDFDocument);
        fCanvas = NULL;
        fDevice = NULL;
    }

    virtual ~SkDocument_PDF() {
        // subclasses must call close() in their destructors
        this->close();
    }

protected:
    virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
                                  const SkRect& content) SK_OVERRIDE {
        SkASSERT(NULL == fCanvas);
        SkASSERT(NULL == fDevice);

        SkISize pageS, contentS;
        SkMatrix matrix;

        pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height));
        contentS.set(SkScalarRoundToInt(content.width()),
                     SkScalarRoundToInt(content.height()));
        matrix.setTranslate(content.fLeft, content.fTop);

        fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix));
        if (fEncoder) {
            fDevice->setDCTEncoder(fEncoder);
        }
        fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
        return fCanvas;
    }

    virtual void onEndPage() SK_OVERRIDE {
        SkASSERT(fCanvas);
        SkASSERT(fDevice);

        fCanvas->flush();
        fDoc->appendPage(fDevice);

        fCanvas->unref();
        fDevice->unref();

        fCanvas = NULL;
        fDevice = NULL;
    }

    virtual bool onClose(SkWStream* stream) SK_OVERRIDE {
        SkASSERT(NULL == fCanvas);
        SkASSERT(NULL == fDevice);

        bool success = fDoc->emitPDF(stream);
        SkDELETE(fDoc);
        fDoc = NULL;
        return success;
    }

    virtual void onAbort() SK_OVERRIDE {
        SkDELETE(fDoc);
        fDoc = NULL;
    }

private:
    SkPDFDocument*  fDoc;
    SkPDFDevice* fDevice;
    SkCanvas*       fCanvas;
    SkPicture::EncodeBitmap fEncoder;
};

///////////////////////////////////////////////////////////////////////////////

SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*),
                                  SkPicture::EncodeBitmap enc) {
    return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL;
}

static void delete_wstream(SkWStream* stream) {
    SkDELETE(stream);
}

SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc) {
    SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
    if (!stream->isValid()) {
        SkDELETE(stream);
        return NULL;
    }
    return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc));
}