2012-10-10 15:20:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "PdfRenderer.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkDevice.h"
|
|
|
|
#include "SkPDFDevice.h"
|
|
|
|
#include "SkPDFDocument.h"
|
|
|
|
|
|
|
|
namespace sk_tools {
|
|
|
|
|
2013-10-14 13:42:12 +00:00
|
|
|
void PdfRenderer::init(SkPicture* pict, SkWStream* stream) {
|
2012-10-10 15:20:34 +00:00
|
|
|
SkASSERT(NULL == fPicture);
|
|
|
|
SkASSERT(NULL == fCanvas.get());
|
|
|
|
if (fPicture != NULL || NULL != fCanvas.get()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(pict != NULL);
|
|
|
|
if (NULL == pict) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fPicture = pict;
|
2013-10-14 13:42:12 +00:00
|
|
|
fCanvas.reset(this->setupCanvas(stream, pict->width(), pict->height()));
|
2012-10-10 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:42:12 +00:00
|
|
|
SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) {
|
|
|
|
fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL, fEncoder));
|
|
|
|
|
|
|
|
SkCanvas* canvas = fPdfDoc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
|
|
|
|
canvas->ref();
|
2012-10-10 15:20:34 +00:00
|
|
|
|
2013-10-14 13:42:12 +00:00
|
|
|
return canvas;
|
2012-10-10 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PdfRenderer::end() {
|
|
|
|
fPicture = NULL;
|
|
|
|
fCanvas.reset(NULL);
|
2013-10-14 13:42:12 +00:00
|
|
|
fPdfDoc.reset(NULL);
|
2012-10-10 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 13:42:12 +00:00
|
|
|
bool SimplePdfRenderer::render() {
|
2012-10-10 15:20:34 +00:00
|
|
|
SkASSERT(fCanvas.get() != NULL);
|
|
|
|
SkASSERT(fPicture != NULL);
|
|
|
|
if (NULL == fCanvas.get() || NULL == fPicture) {
|
2013-10-14 13:42:12 +00:00
|
|
|
return false;
|
2012-10-10 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fCanvas->drawPicture(*fPicture);
|
|
|
|
fCanvas->flush();
|
2013-10-14 13:42:12 +00:00
|
|
|
|
|
|
|
return fPdfDoc->close();
|
2012-10-10 15:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|