blob: 3c5423c8e558ab7233b28d39577a2b256928d5df [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkTypes.h"
halcanary385fe4d2015-08-26 13:07:48 -07009
edisonn@google.comf8b6b012013-07-11 14:28:04 +000010#ifdef SAMPLE_PDF_FILE_VIEWER
11
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
13#include "include/core/SkColorFilter.h"
14#include "include/core/SkColorPriv.h"
15#include "include/core/SkGraphics.h"
16#include "include/core/SkPath.h"
17#include "include/core/SkPicture.h"
18#include "include/core/SkRegion.h"
19#include "include/core/SkShader.h"
20#include "include/core/SkTime.h"
21#include "include/core/SkTypeface.h"
22#include "include/effects/SkGradientShader.h"
23#include "include/utils/SkRandom.h"
24#include "samplecode/Sample.h"
25#include "src/core/SkOSFile.h"
26#include "src/utils/SkUTF.h"
edisonn@google.comd1a874a2013-07-11 14:45:20 +000027#include "SkPdfRenderer.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000028
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040029class PdfFileViewer : public Sample {
edisonn@google.comf8b6b012013-07-11 14:28:04 +000030private:
31 SkString fFilename;
32 SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array
33
34 static SkPicture* LoadPdf(const char path[]) {
Ben Wagner145dbcd2016-11-03 14:40:50 -040035 std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
halcanary96fcdcc2015-08-27 07:41:13 -070036 if (nullptr == renderer.get()) {
37 return nullptr;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000038 }
scroggo@google.com90922892013-11-14 19:09:27 +000039
halcanary385fe4d2015-08-26 13:07:48 -070040 SkPicture* pic = new SkPicture;
scroggo@google.com90922892013-11-14 19:09:27 +000041 SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
42 (int) renderer->MediaBox(0).height());
43 renderer->renderPage(0, canvas, renderer->MediaBox(0));
44 pic->endRecording();
edisonn@google.comf8b6b012013-07-11 14:28:04 +000045 return pic;
46 }
47
48public:
halcanary96fcdcc2015-08-27 07:41:13 -070049 PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
50 fPicture = nullptr;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000051 }
52
53 virtual ~PdfFileViewer() {
54 SkSafeUnref(fPicture);
55 }
56
57protected:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040058 virtual bool onQuery(Sample::Event* evt) {
59 if (Sample::TitleQ(*evt)) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +000060 SkString name("P:");
61 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
62 name.append(basename ? basename+1: fFilename.c_str());
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040063 Sample::TitleR(evt, name.c_str());
edisonn@google.comf8b6b012013-07-11 14:28:04 +000064 return true;
65 }
66 return this->INHERITED::onQuery(evt);
67 }
68
69 virtual bool onEvent(const SkEvent& evt) {
70 // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
71 // like pdf object from click, ...
72 // TODO(edisonn): first, next, prev, last page navigation + slideshow
73 return this->INHERITED::onEvent(evt);
74 }
75
76 virtual void onDrawContent(SkCanvas* canvas) {
77 if (!fPicture) {
78 fPicture = LoadPdf(fFilename.c_str());
79 }
80 if (fPicture) {
81 canvas->drawPicture(*fPicture);
82 }
83 }
84
85private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040086 typedef Sample INHERITED;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000087};
88
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040089Sample* CreateSamplePdfFileViewer(const char filename[]);
90Sample* CreateSamplePdfFileViewer(const char filename[]) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +000091 return new PdfFileViewer(filename);
92}
93
94//////////////////////////////////////////////////////////////////////////////
95
96#if 0
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040097static Sample* MyFactory() { return new PdfFileViewer; }
98static SampleRegister reg(MyFactory);
edisonn@google.comf8b6b012013-07-11 14:28:04 +000099#endif
100
101#endif // SAMPLE_PDF_FILE_VIEWER