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 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 10 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 11 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 12 | #include "Sample.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 13 | #include "SkCanvas.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 14 | #include "SkGradientShader.h" |
| 15 | #include "SkGraphics.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 16 | #include "SkOSFile.h" |
| 17 | #include "SkPath.h" |
| 18 | #include "SkPicture.h" |
| 19 | #include "SkRandom.h" |
| 20 | #include "SkRegion.h" |
| 21 | #include "SkShader.h" |
Hal Canary | ea60b95 | 2018-08-21 11:45:46 -0400 | [diff] [blame^] | 22 | #include "SkUTF.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 23 | #include "SkColorPriv.h" |
| 24 | #include "SkColorFilter.h" |
| 25 | #include "SkTime.h" |
| 26 | #include "SkTypeface.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: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 58 | virtual bool onQuery(Sample::Event* evt) { |
| 59 | if (Sample::TitleQ(*evt)) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 60 | SkString name("P:"); |
| 61 | const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
| 62 | name.append(basename ? basename+1: fFilename.c_str()); |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 63 | Sample::TitleR(evt, name.c_str()); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 64 | 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 | |
| 85 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 86 | typedef Sample INHERITED; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 89 | Sample* CreateSamplePdfFileViewer(const char filename[]); |
| 90 | Sample* CreateSamplePdfFileViewer(const char filename[]) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 91 | return new PdfFileViewer(filename); |
| 92 | } |
| 93 | |
| 94 | ////////////////////////////////////////////////////////////////////////////// |
| 95 | |
| 96 | #if 0 |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 97 | static Sample* MyFactory() { return new PdfFileViewer; } |
| 98 | static SampleRegister reg(MyFactory); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 99 | #endif |
| 100 | |
| 101 | #endif // SAMPLE_PDF_FILE_VIEWER |