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 | |
| 12 | #include "SampleCode.h" |
| 13 | #include "SkDumpCanvas.h" |
| 14 | #include "SkView.h" |
| 15 | #include "SkCanvas.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 16 | #include "SkGradientShader.h" |
| 17 | #include "SkGraphics.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 18 | #include "SkOSFile.h" |
| 19 | #include "SkPath.h" |
| 20 | #include "SkPicture.h" |
| 21 | #include "SkRandom.h" |
| 22 | #include "SkRegion.h" |
| 23 | #include "SkShader.h" |
| 24 | #include "SkUtils.h" |
| 25 | #include "SkColorPriv.h" |
| 26 | #include "SkColorFilter.h" |
| 27 | #include "SkTime.h" |
| 28 | #include "SkTypeface.h" |
edisonn@google.com | d1a874a | 2013-07-11 14:45:20 +0000 | [diff] [blame] | 29 | #include "SkPdfRenderer.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 30 | |
| 31 | class PdfFileViewer : public SampleView { |
| 32 | private: |
| 33 | SkString fFilename; |
| 34 | SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array |
| 35 | |
| 36 | static SkPicture* LoadPdf(const char path[]) { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 37 | std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 38 | if (nullptr == renderer.get()) { |
| 39 | return nullptr; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 40 | } |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 41 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 42 | SkPicture* pic = new SkPicture; |
scroggo@google.com | 9092289 | 2013-11-14 19:09:27 +0000 | [diff] [blame] | 43 | SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(), |
| 44 | (int) renderer->MediaBox(0).height()); |
| 45 | renderer->renderPage(0, canvas, renderer->MediaBox(0)); |
| 46 | pic->endRecording(); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 47 | return pic; |
| 48 | } |
| 49 | |
| 50 | public: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 51 | PdfFileViewer(const char name[] = nullptr) : fFilename(name) { |
| 52 | fPicture = nullptr; |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | virtual ~PdfFileViewer() { |
| 56 | SkSafeUnref(fPicture); |
| 57 | } |
| 58 | |
| 59 | protected: |
| 60 | // overrides from SkEventSink |
| 61 | virtual bool onQuery(SkEvent* evt) { |
| 62 | if (SampleCode::TitleQ(*evt)) { |
| 63 | SkString name("P:"); |
| 64 | const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
| 65 | name.append(basename ? basename+1: fFilename.c_str()); |
| 66 | SampleCode::TitleR(evt, name.c_str()); |
| 67 | return true; |
| 68 | } |
| 69 | return this->INHERITED::onQuery(evt); |
| 70 | } |
| 71 | |
| 72 | virtual bool onEvent(const SkEvent& evt) { |
| 73 | // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info |
| 74 | // like pdf object from click, ... |
| 75 | // TODO(edisonn): first, next, prev, last page navigation + slideshow |
| 76 | return this->INHERITED::onEvent(evt); |
| 77 | } |
| 78 | |
| 79 | virtual void onDrawContent(SkCanvas* canvas) { |
| 80 | if (!fPicture) { |
| 81 | fPicture = LoadPdf(fFilename.c_str()); |
| 82 | } |
| 83 | if (fPicture) { |
| 84 | canvas->drawPicture(*fPicture); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | typedef SampleView INHERITED; |
| 90 | }; |
| 91 | |
| 92 | SampleView* CreateSamplePdfFileViewer(const char filename[]); |
| 93 | SampleView* CreateSamplePdfFileViewer(const char filename[]) { |
| 94 | return new PdfFileViewer(filename); |
| 95 | } |
| 96 | |
| 97 | ////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | #if 0 |
| 100 | static SkView* MyFactory() { return new PdfFileViewer; } |
| 101 | static SkViewRegister reg(MyFactory); |
| 102 | #endif |
| 103 | |
| 104 | #endif // SAMPLE_PDF_FILE_VIEWER |