edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #ifdef SAMPLE_PDF_FILE_VIEWER |
| 10 | |
| 11 | #include "SampleCode.h" |
| 12 | #include "SkDumpCanvas.h" |
| 13 | #include "SkView.h" |
| 14 | #include "SkCanvas.h" |
| 15 | #include "Sk64.h" |
| 16 | #include "SkGradientShader.h" |
| 17 | #include "SkGraphics.h" |
| 18 | #include "SkImageDecoder.h" |
| 19 | #include "SkOSFile.h" |
| 20 | #include "SkPath.h" |
| 21 | #include "SkPicture.h" |
| 22 | #include "SkRandom.h" |
| 23 | #include "SkRegion.h" |
| 24 | #include "SkShader.h" |
| 25 | #include "SkUtils.h" |
| 26 | #include "SkColorPriv.h" |
| 27 | #include "SkColorFilter.h" |
| 28 | #include "SkTime.h" |
| 29 | #include "SkTypeface.h" |
| 30 | #include "SkXfermode.h" |
| 31 | |
edisonn@google.com | d1a874a | 2013-07-11 14:45:20 +0000 | [diff] [blame] | 32 | #include "SkPdfRenderer.h" |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 33 | |
| 34 | class PdfFileViewer : public SampleView { |
| 35 | private: |
| 36 | SkString fFilename; |
| 37 | SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array |
| 38 | |
| 39 | static SkPicture* LoadPdf(const char path[]) { |
| 40 | SkPicture* pic = NULL; |
| 41 | |
edisonn@google.com | d1a874a | 2013-07-11 14:45:20 +0000 | [diff] [blame] | 42 | SkPdfRenderer renderer; |
| 43 | SkString skpath; |
| 44 | skpath.append(path); |
| 45 | renderer.load(skpath); |
| 46 | if (renderer.loaded()) { |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 47 | pic = SkNEW(SkPicture); |
edisonn@google.com | d1a874a | 2013-07-11 14:45:20 +0000 | [diff] [blame] | 48 | SkCanvas* canvas = pic->beginRecording((int)renderer.MediaBox(0).width(), (int)renderer.MediaBox(0).height()); |
edisonn@google.com | 444e25a | 2013-07-11 15:20:50 +0000 | [diff] [blame] | 49 | renderer.renderPage(0, canvas, renderer.MediaBox(0)); |
edisonn@google.com | f8b6b01 | 2013-07-11 14:28:04 +0000 | [diff] [blame] | 50 | pic->endRecording(); |
| 51 | } |
| 52 | return pic; |
| 53 | } |
| 54 | |
| 55 | public: |
| 56 | PdfFileViewer(const char name[] = NULL) : fFilename(name) { |
| 57 | fPicture = NULL; |
| 58 | } |
| 59 | |
| 60 | virtual ~PdfFileViewer() { |
| 61 | SkSafeUnref(fPicture); |
| 62 | } |
| 63 | |
| 64 | protected: |
| 65 | // overrides from SkEventSink |
| 66 | virtual bool onQuery(SkEvent* evt) { |
| 67 | if (SampleCode::TitleQ(*evt)) { |
| 68 | SkString name("P:"); |
| 69 | const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR); |
| 70 | name.append(basename ? basename+1: fFilename.c_str()); |
| 71 | SampleCode::TitleR(evt, name.c_str()); |
| 72 | return true; |
| 73 | } |
| 74 | return this->INHERITED::onQuery(evt); |
| 75 | } |
| 76 | |
| 77 | virtual bool onEvent(const SkEvent& evt) { |
| 78 | // TODO(edisonn): add here event handlers to disable clipping, or to show helpful info |
| 79 | // like pdf object from click, ... |
| 80 | // TODO(edisonn): first, next, prev, last page navigation + slideshow |
| 81 | return this->INHERITED::onEvent(evt); |
| 82 | } |
| 83 | |
| 84 | virtual void onDrawContent(SkCanvas* canvas) { |
| 85 | if (!fPicture) { |
| 86 | fPicture = LoadPdf(fFilename.c_str()); |
| 87 | } |
| 88 | if (fPicture) { |
| 89 | canvas->drawPicture(*fPicture); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | typedef SampleView INHERITED; |
| 95 | }; |
| 96 | |
| 97 | SampleView* CreateSamplePdfFileViewer(const char filename[]); |
| 98 | SampleView* CreateSamplePdfFileViewer(const char filename[]) { |
| 99 | return new PdfFileViewer(filename); |
| 100 | } |
| 101 | |
| 102 | ////////////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | #if 0 |
| 105 | static SkView* MyFactory() { return new PdfFileViewer; } |
| 106 | static SkViewRegister reg(MyFactory); |
| 107 | #endif |
| 108 | |
| 109 | #endif // SAMPLE_PDF_FILE_VIEWER |