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