blob: 9b6b54df4193ae7294c822c85d221a0b14c4adc2 [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:
Hal Canary8a027312019-07-03 10:55:44 -040058 virtual SkString name() {
59 SkString name("P:");
60 const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
61 name.append(basename ? basename+1: fFilename.c_str());
62 return name;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000063 }
64
65 virtual bool onEvent(const SkEvent& evt) {
66 // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
67 // like pdf object from click, ...
68 // TODO(edisonn): first, next, prev, last page navigation + slideshow
69 return this->INHERITED::onEvent(evt);
70 }
71
72 virtual void onDrawContent(SkCanvas* canvas) {
73 if (!fPicture) {
74 fPicture = LoadPdf(fFilename.c_str());
75 }
76 if (fPicture) {
77 canvas->drawPicture(*fPicture);
78 }
79 }
80
81private:
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040082 typedef Sample INHERITED;
edisonn@google.comf8b6b012013-07-11 14:28:04 +000083};
84
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040085Sample* CreateSamplePdfFileViewer(const char filename[]);
86Sample* CreateSamplePdfFileViewer(const char filename[]) {
edisonn@google.comf8b6b012013-07-11 14:28:04 +000087 return new PdfFileViewer(filename);
88}
89
90//////////////////////////////////////////////////////////////////////////////
91
92#if 0
Ben Wagnerb2c4ea62018-08-08 11:36:17 -040093static Sample* MyFactory() { return new PdfFileViewer; }
94static SampleRegister reg(MyFactory);
edisonn@google.comf8b6b012013-07-11 14:28:04 +000095#endif
96
97#endif // SAMPLE_PDF_FILE_VIEWER