blob: 97ab0e52036c1096658ce524d07aa5868e9d1ea4 [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
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040012#include "Sample.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000013#include "SkCanvas.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000014#include "SkGradientShader.h"
15#include "SkGraphics.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000016#include "SkOSFile.h"
17#include "SkPath.h"
18#include "SkPicture.h"
19#include "SkRandom.h"
20#include "SkRegion.h"
21#include "SkShader.h"
Hal Canaryea60b952018-08-21 11:45:46 -040022#include "SkUTF.h"
edisonn@google.comf8b6b012013-07-11 14:28:04 +000023#include "SkColorPriv.h"
24#include "SkColorFilter.h"
25#include "SkTime.h"
26#include "SkTypeface.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