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