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