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

#include "PdfRenderer.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkPDFDevice.h"
#include "SkPDFDocument.h"

namespace sk_tools {

void PdfRenderer::init(SkPicture* pict) {
    SkASSERT(NULL == fPicture);
    SkASSERT(NULL == fCanvas.get());
    if (fPicture != NULL || NULL != fCanvas.get()) {
        return;
    }

    SkASSERT(pict != NULL);
    if (NULL == pict) {
        return;
    }

    fPicture = pict;
    fCanvas.reset(this->setupCanvas());
}

SkCanvas* PdfRenderer::setupCanvas() {
    return this->setupCanvas(fPicture->width(), fPicture->height());
}

SkCanvas* PdfRenderer::setupCanvas(int width, int height) {
    SkISize pageSize = SkISize::Make(width, height);
    fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I()));
    return SkNEW_ARGS(SkCanvas, (fPDFDevice));
}

void PdfRenderer::end() {
    fPicture = NULL;
    fCanvas.reset(NULL);
    if (fPDFDevice) {
        SkDELETE(fPDFDevice);
        fPDFDevice = NULL;
    }
}

void PdfRenderer::write(SkWStream* stream) const {
    SkPDFDocument doc;
    doc.appendPage(fPDFDevice);
    doc.emitPDF(stream);
}

void SimplePdfRenderer::render() {
    SkASSERT(fCanvas.get() != NULL);
    SkASSERT(fPicture != NULL);
    if (NULL == fCanvas.get() || NULL == fPicture) {
        return;
    }

    fCanvas->drawPicture(*fPicture);
    fCanvas->flush();
}

}