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