skia2/samplecode/SamplePdfFileViewer.cpp
Ben Wagner b2c4ea6219 Move Views into Sample and Viewer.
What is left of the SkView system is used only by samples or viewer.
As a result, move it out of the Skia source tree and re-organize so it
is a bit easier to understand and use more shared code.

Move samplecode/ClockFaceView.cpp to samplecode/SampleTextEffects.cpp,
sice that's what's actually in it.

Move SkAnimTimer.h to tools/timer, since it's actually shared between gm
and samples.

Change-Id: I55dafd94c64e4f930ddbd19168e0f812af86c455
Reviewed-on: https://skia-review.googlesource.com/146161
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Ben Wagner <bungeman@google.com>
2018-08-09 15:25:32 +00:00

102 lines
2.8 KiB
C++

/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkTypes.h"
#ifdef SAMPLE_PDF_FILE_VIEWER
#include "Sample.h"
#include "SkCanvas.h"
#include "SkGradientShader.h"
#include "SkGraphics.h"
#include "SkOSFile.h"
#include "SkPath.h"
#include "SkPicture.h"
#include "SkRandom.h"
#include "SkRegion.h"
#include "SkShader.h"
#include "SkUtils.h"
#include "SkColorPriv.h"
#include "SkColorFilter.h"
#include "SkTime.h"
#include "SkTypeface.h"
#include "SkPdfRenderer.h"
class PdfFileViewer : public Sample {
private:
SkString fFilename;
SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array
static SkPicture* LoadPdf(const char path[]) {
std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
if (nullptr == renderer.get()) {
return nullptr;
}
SkPicture* pic = new SkPicture;
SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
(int) renderer->MediaBox(0).height());
renderer->renderPage(0, canvas, renderer->MediaBox(0));
pic->endRecording();
return pic;
}
public:
PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
fPicture = nullptr;
}
virtual ~PdfFileViewer() {
SkSafeUnref(fPicture);
}
protected:
virtual bool onQuery(Sample::Event* evt) {
if (Sample::TitleQ(*evt)) {
SkString name("P:");
const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
name.append(basename ? basename+1: fFilename.c_str());
Sample::TitleR(evt, name.c_str());
return true;
}
return this->INHERITED::onQuery(evt);
}
virtual bool onEvent(const SkEvent& evt) {
// TODO(edisonn): add here event handlers to disable clipping, or to show helpful info
// like pdf object from click, ...
// TODO(edisonn): first, next, prev, last page navigation + slideshow
return this->INHERITED::onEvent(evt);
}
virtual void onDrawContent(SkCanvas* canvas) {
if (!fPicture) {
fPicture = LoadPdf(fFilename.c_str());
}
if (fPicture) {
canvas->drawPicture(*fPicture);
}
}
private:
typedef Sample INHERITED;
};
Sample* CreateSamplePdfFileViewer(const char filename[]);
Sample* CreateSamplePdfFileViewer(const char filename[]) {
return new PdfFileViewer(filename);
}
//////////////////////////////////////////////////////////////////////////////
#if 0
static Sample* MyFactory() { return new PdfFileViewer; }
static SampleRegister reg(MyFactory);
#endif
#endif // SAMPLE_PDF_FILE_VIEWER