blob: 55c7002fa8e4a4810f7f232b065e5e6559f59008 [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[]) {
scroggo@google.com90922892013-11-14 19:09:27 +000040 SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
41 if (NULL == renderer.get()) {
42 return NULL;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000043 }
scroggo@google.com90922892013-11-14 19:09:27 +000044
45 SkPicture* pic = SkNEW(SkPicture);
46 SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
47 (int) renderer->MediaBox(0).height());
48 renderer->renderPage(0, canvas, renderer->MediaBox(0));
49 pic->endRecording();
edisonn@google.comf8b6b012013-07-11 14:28:04 +000050 return pic;
51 }
52
53public:
54 PdfFileViewer(const char name[] = NULL) : fFilename(name) {
55 fPicture = NULL;
56 }
57
58 virtual ~PdfFileViewer() {
59 SkSafeUnref(fPicture);
60 }
61
62protected:
63 // overrides from SkEventSink
64 virtual bool onQuery(SkEvent* evt) {
65 if (SampleCode::TitleQ(*evt)) {
66 SkString name("P:");
67 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
68 name.append(basename ? basename+1: fFilename.c_str());
69 SampleCode::TitleR(evt, name.c_str());
70 return true;
71 }
72 return this->INHERITED::onQuery(evt);
73 }
74
75 virtual bool onEvent(const SkEvent& evt) {
76 // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
77 // like pdf object from click, ...
78 // TODO(edisonn): first, next, prev, last page navigation + slideshow
79 return this->INHERITED::onEvent(evt);
80 }
81
82 virtual void onDrawContent(SkCanvas* canvas) {
83 if (!fPicture) {
84 fPicture = LoadPdf(fFilename.c_str());
85 }
86 if (fPicture) {
87 canvas->drawPicture(*fPicture);
88 }
89 }
90
91private:
92 typedef SampleView INHERITED;
93};
94
95SampleView* CreateSamplePdfFileViewer(const char filename[]);
96SampleView* CreateSamplePdfFileViewer(const char filename[]) {
97 return new PdfFileViewer(filename);
98}
99
100//////////////////////////////////////////////////////////////////////////////
101
102#if 0
103static SkView* MyFactory() { return new PdfFileViewer; }
104static SkViewRegister reg(MyFactory);
105#endif
106
107#endif // SAMPLE_PDF_FILE_VIEWER