aboutsummaryrefslogtreecommitdiffhomepage
path: root/samplecode/SamplePdfFileViewer.cpp
blob: a36b29f42f0e93165f0cd37cd31eb687ded253e5 (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
/*
 * 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 "SkTypes.h"

#ifdef SAMPLE_PDF_FILE_VIEWER

#include "SampleCode.h"
#include "SkDumpCanvas.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkOSFile.h"
#include "SkPath.h"
#include "SkPicture.h"
#include "SkRandom.h"
#include "SkRegion.h"
#include "SkShader.h"
#include "SkUtils.h"
#include "SkColorPriv.h"
#include "SkColorFilter.h"
#include "SkTime.h"
#include "SkTypeface.h"
#include "SkXfermode.h"

#include "SkPdfRenderer.h"

class PdfFileViewer : public SampleView {
private:
    SkString    fFilename;
    SkPicture*  fPicture;  // TODO(edisonn): multiple pages, one page / picture, make it an array

    static SkPicture* LoadPdf(const char path[]) {
        SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
        if (nullptr == renderer.get()) {
            return nullptr;
        }

        SkPicture* pic = new SkPicture;
        SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
                                               (int) renderer->MediaBox(0).height());
        renderer->renderPage(0, canvas, renderer->MediaBox(0));
        pic->endRecording();
        return pic;
    }

public:
    PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
        fPicture = nullptr;
    }

    virtual ~PdfFileViewer() {
        SkSafeUnref(fPicture);
    }

protected:
    // overrides from SkEventSink
    virtual bool onQuery(SkEvent* evt) {
        if (SampleCode::TitleQ(*evt)) {
            SkString name("P:");
            const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
            name.append(basename ? basename+1: fFilename.c_str());
            SampleCode::TitleR(evt, name.c_str());
            return true;
        }
        return this->INHERITED::onQuery(evt);
    }

    virtual bool onEvent(const SkEvent& evt) {
        // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
        // like pdf object from click, ...
        // TODO(edisonn): first, next, prev, last page navigation + slideshow
        return this->INHERITED::onEvent(evt);
    }

    virtual void onDrawContent(SkCanvas* canvas) {
        if (!fPicture) {
            fPicture = LoadPdf(fFilename.c_str());
        }
        if (fPicture) {
            canvas->drawPicture(*fPicture);
        }
    }

private:
    typedef SampleView INHERITED;
};

SampleView* CreateSamplePdfFileViewer(const char filename[]);
SampleView* CreateSamplePdfFileViewer(const char filename[]) {
    return new PdfFileViewer(filename);
}

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

#if 0
static SkView* MyFactory() { return new PdfFileViewer; }
static SkViewRegister reg(MyFactory);
#endif

#endif  // SAMPLE_PDF_FILE_VIEWER