edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkTypes.h" |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 9 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 10 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 11 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #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.com | d1a874a | 2013-07-11 14:45:20 +0000 | [diff] [blame] | 27 | #include "SkPdfRenderer.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 28 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 29 | class PdfFileViewer : public Sample { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 30 | private: |
| 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 Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 35 | std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 36 | if (nullptr == renderer.get()) { |
| 37 | return nullptr; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 38 | } |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 39 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 40 | SkPicture* pic = new SkPicture; |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 41 | 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.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 45 | return pic; |
| 46 | } |
| 47 | |
| 48 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 49 | PdfFileViewer(const char name[] = nullptr) : fFilename(name) { |
| 50 | fPicture = nullptr; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | virtual ~PdfFileViewer() { |
| 54 | SkSafeUnref(fPicture); |
| 55 | } |
| 56 | |
| 57 | protected: |
Hal Canary | 8a02731 | 2019-07-03 10:55:44 -0400 | [diff] [blame] | 58 | 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.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 63 | } |
| 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 | |
| 81 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 82 | typedef Sample INHERITED; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 85 | Sample* CreateSamplePdfFileViewer(const char filename[]); |
| 86 | Sample* CreateSamplePdfFileViewer(const char filename[]) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 87 | return new PdfFileViewer(filename); |
| 88 | } |
| 89 | |
| 90 | ////////////////////////////////////////////////////////////////////////////// |
| 91 | |
| 92 | #if 0 |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 93 | static Sample* MyFactory() { return new PdfFileViewer; } |
| 94 | static SampleRegister reg(MyFactory); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 95 | #endif |
| 96 | |
| 97 | #endif // SAMPLE_PDF_FILE_VIEWER |