Home | History | Annotate | Download | only in samplecode
      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 
      8 #include "SkTypes.h"
      9 
     10 #ifdef SAMPLE_PDF_FILE_VIEWER
     11 
     12 #include "SampleCode.h"
     13 #include "SkDumpCanvas.h"
     14 #include "SkView.h"
     15 #include "SkCanvas.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 "SkPdfRenderer.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         SkAutoTDelete<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
     41         if (nullptr == renderer.get()) {
     42             return nullptr;
     43         }
     44 
     45         SkPicture* pic = new 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();
     50         return pic;
     51     }
     52 
     53 public:
     54     PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
     55         fPicture = nullptr;
     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
    108