blob: 1842abe2fd0c1965c338b2bf3dbf10faeb77a0da [file] [log] [blame]
edisonn@google.comf8b6b012013-07-11 14:28:04 +00001/*
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
halcanary385fe4d2015-08-26 13:07:48 -07008#include "SkTypes.h"
9
edisonn@google.comf8b6b012013-07-11 14:28:04 +000010#ifdef SAMPLE_PDF_FILE_VIEWER
11
12#include "SampleCode.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000013#include "SkView.h"
14#include "SkCanvas.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000015#include "SkGradientShader.h"
16#include "SkGraphics.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000017#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"
edisonn@google.comd1a874a2013-07-11 14:45:20 +000028#include "SkPdfRenderer.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000029
30class PdfFileViewer : public SampleView {
31private:
32 SkString fFilename;
33 SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array
34
35 static SkPicture* LoadPdf(const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040036 std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070037 if (nullptr == renderer.get()) {
38 return nullptr;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000039 }
scroggo@google.com90922892013-11-14 19:09:27 +000040
halcanary385fe4d2015-08-26 13:07:48 -070041 SkPicture* pic = new SkPicture;
scroggo@google.com90922892013-11-14 19:09:27 +000042 SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
43 (int) renderer->MediaBox(0).height());
44 renderer->renderPage(0, canvas, renderer->MediaBox(0));
45 pic->endRecording();
edisonn@google.comf8b6b012013-07-11 14:28:04 +000046 return pic;
47 }
48
49public:
halcanary96fcdcc2015-08-27 07:41:13 -070050 PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
51 fPicture = nullptr;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000052 }
53
54 virtual ~PdfFileViewer() {
55 SkSafeUnref(fPicture);
56 }
57
58protected:
59 // overrides from SkEventSink
60 virtual bool onQuery(SkEvent* evt) {
61 if (SampleCode::TitleQ(*evt)) {
62 SkString name("P:");
63 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
64 name.append(basename ? basename+1: fFilename.c_str());
65 SampleCode::TitleR(evt, name.c_str());
66 return true;
67 }
68 return this->INHERITED::onQuery(evt);
69 }
70
71 virtual bool onEvent(const SkEvent& evt) {
72 // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
73 // like pdf object from click, ...
74 // TODO(edisonn): first, next, prev, last page navigation + slideshow
75 return this->INHERITED::onEvent(evt);
76 }
77
78 virtual void onDrawContent(SkCanvas* canvas) {
79 if (!fPicture) {
80 fPicture = LoadPdf(fFilename.c_str());
81 }
82 if (fPicture) {
83 canvas->drawPicture(*fPicture);
84 }
85 }
86
87private:
88 typedef SampleView INHERITED;
89};
90
91SampleView* CreateSamplePdfFileViewer(const char filename[]);
92SampleView* CreateSamplePdfFileViewer(const char filename[]) {
93 return new PdfFileViewer(filename);
94}
95
96//////////////////////////////////////////////////////////////////////////////
97
98#if 0
99static SkView* MyFactory() { return new PdfFileViewer; }
100static SkViewRegister reg(MyFactory);
101#endif
102
103#endif // SAMPLE_PDF_FILE_VIEWER