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 | |
| 32 | #include "SkPodofoParsedPDF.h" |
| 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 | |
| 42 | SkPodofoParsedPDF doc(path); |
| 43 | if (doc.pages()) { |
| 44 | pic = SkNEW(SkPicture); |
| 45 | SkCanvas* canvas = pic->beginRecording((int)doc.width(0), (int)doc.height(0)); |
| 46 | doc.drawPage(0, canvas); |
| 47 | pic->endRecording(); |
| 48 | } |
| 49 | return pic; |
| 50 | } |
| 51 | |
| 52 | public: |
| 53 | PdfFileViewer(const char name[] = NULL) : fFilename(name) { |
| 54 | fPicture = NULL; |
| 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 |