1/*
2 * Copyright 2013 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#ifdef SAMPLE_PDF_FILE_VIEWER
9
10#include "SampleCode.h"
11#include "SkDumpCanvas.h"
12#include "SkView.h"
13#include "SkCanvas.h"
14#include "SkGradientShader.h"
15#include "SkGraphics.h"
16#include "SkImageDecoder.h"
17#include "SkOSFile.h"
18#include "SkPath.h"
19#include "SkPicture.h"
20#include "SkRandom.h"
21#include "SkRegion.h"
22#include "SkShader.h"
23#include "SkUtils.h"
24#include "SkColorPriv.h"
25#include "SkColorFilter.h"
26#include "SkTime.h"
27#include "SkTypeface.h"
28#include "SkXfermode.h"
29
30#include "SkPdfRenderer.h"
31
32class PdfFileViewer : public SampleView {
33private:
34    SkString    fFilename;
35    SkPicture*  fPicture;  // TODO(edisonn): multiple pages, one page / picture, make it an array
36
37    static SkPicture* LoadPdf(const char path[]) {
38        SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
39        if (NULL == renderer.get()) {
40            return NULL;
41        }
42
43        SkPicture* pic = SkNEW(SkPicture);
44        SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
45                                               (int) renderer->MediaBox(0).height());
46        renderer->renderPage(0, canvas, renderer->MediaBox(0));
47        pic->endRecording();
48        return pic;
49    }
50
51public:
52    PdfFileViewer(const char name[] = NULL) : fFilename(name) {
53        fPicture = NULL;
54    }
55
56    virtual ~PdfFileViewer() {
57        SkSafeUnref(fPicture);
58    }
59
60protected:
61    // overrides from SkEventSink
62    virtual bool onQuery(SkEvent* evt) {
63        if (SampleCode::TitleQ(*evt)) {
64            SkString name("P:");
65            const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
66            name.append(basename ? basename+1: fFilename.c_str());
67            SampleCode::TitleR(evt, name.c_str());
68            return true;
69        }
70        return this->INHERITED::onQuery(evt);
71    }
72
73    virtual bool onEvent(const SkEvent& evt) {
74        // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
75        // like pdf object from click, ...
76        // TODO(edisonn): first, next, prev, last page navigation + slideshow
77        return this->INHERITED::onEvent(evt);
78    }
79
80    virtual void onDrawContent(SkCanvas* canvas) {
81        if (!fPicture) {
82            fPicture = LoadPdf(fFilename.c_str());
83        }
84        if (fPicture) {
85            canvas->drawPicture(*fPicture);
86        }
87    }
88
89private:
90    typedef SampleView INHERITED;
91};
92
93SampleView* CreateSamplePdfFileViewer(const char filename[]);
94SampleView* CreateSamplePdfFileViewer(const char filename[]) {
95    return new PdfFileViewer(filename);
96}
97
98//////////////////////////////////////////////////////////////////////////////
99
100#if 0
101static SkView* MyFactory() { return new PdfFileViewer; }
102static SkViewRegister reg(MyFactory);
103#endif
104
105#endif  // SAMPLE_PDF_FILE_VIEWER
106