2013-07-11 14:28:04 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
2015-08-26 20:07:48 +00:00
|
|
|
|
2013-07-11 14:28:04 +00:00
|
|
|
#ifdef SAMPLE_PDF_FILE_VIEWER
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkColorFilter.h"
|
|
|
|
#include "include/core/SkColorPriv.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkPath.h"
|
|
|
|
#include "include/core/SkPicture.h"
|
|
|
|
#include "include/core/SkRegion.h"
|
|
|
|
#include "include/core/SkShader.h"
|
|
|
|
#include "include/core/SkTime.h"
|
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
|
|
|
#include "include/utils/SkRandom.h"
|
|
|
|
#include "samplecode/Sample.h"
|
|
|
|
#include "src/core/SkOSFile.h"
|
|
|
|
#include "src/utils/SkUTF.h"
|
2013-07-11 14:45:20 +00:00
|
|
|
#include "SkPdfRenderer.h"
|
2013-07-11 14:28:04 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class PdfFileViewer : public Sample {
|
2013-07-11 14:28:04 +00:00
|
|
|
private:
|
|
|
|
SkString fFilename;
|
|
|
|
SkPicture* fPicture; // TODO(edisonn): multiple pages, one page / picture, make it an array
|
|
|
|
|
|
|
|
static SkPicture* LoadPdf(const char path[]) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkPdfRenderer> renderer(SkPdfRenderer::CreateFromFile(path));
|
2015-08-27 14:41:13 +00:00
|
|
|
if (nullptr == renderer.get()) {
|
|
|
|
return nullptr;
|
2013-07-11 14:28:04 +00:00
|
|
|
}
|
2013-11-14 19:09:27 +00:00
|
|
|
|
2015-08-26 20:07:48 +00:00
|
|
|
SkPicture* pic = new SkPicture;
|
2013-11-14 19:09:27 +00:00
|
|
|
SkCanvas* canvas = pic->beginRecording((int) renderer->MediaBox(0).width(),
|
|
|
|
(int) renderer->MediaBox(0).height());
|
|
|
|
renderer->renderPage(0, canvas, renderer->MediaBox(0));
|
|
|
|
pic->endRecording();
|
2013-07-11 14:28:04 +00:00
|
|
|
return pic;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2015-08-27 14:41:13 +00:00
|
|
|
PdfFileViewer(const char name[] = nullptr) : fFilename(name) {
|
|
|
|
fPicture = nullptr;
|
2013-07-11 14:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual ~PdfFileViewer() {
|
|
|
|
SkSafeUnref(fPicture);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
virtual bool onQuery(Sample::Event* evt) {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
2013-07-11 14:28:04 +00:00
|
|
|
SkString name("P:");
|
|
|
|
const char* basename = strrchr(fFilename.c_str(), SkPATH_SEPARATOR);
|
|
|
|
name.append(basename ? basename+1: fFilename.c_str());
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample::TitleR(evt, name.c_str());
|
2013-07-11 14:28:04 +00:00
|
|
|
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:
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2013-07-11 14:28:04 +00:00
|
|
|
};
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
Sample* CreateSamplePdfFileViewer(const char filename[]);
|
|
|
|
Sample* CreateSamplePdfFileViewer(const char filename[]) {
|
2013-07-11 14:28:04 +00:00
|
|
|
return new PdfFileViewer(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if 0
|
2018-08-08 15:36:17 +00:00
|
|
|
static Sample* MyFactory() { return new PdfFileViewer; }
|
|
|
|
static SampleRegister reg(MyFactory);
|
2013-07-11 14:28:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // SAMPLE_PDF_FILE_VIEWER
|