blob: 890abde320a3feb4f6755c7d8696dfca3bfc3a83 [file] [log] [blame]
edisonn@google.com2a827e82012-10-10 15:20:34 +00001/*
2 * Copyright 2012 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 "PdfRenderer.h"
9#include "SkCanvas.h"
10#include "SkDevice.h"
11#include "SkPDFDevice.h"
12#include "SkPDFDocument.h"
13
14namespace sk_tools {
15
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000016void PdfRenderer::init(SkPicture* pict, SkWStream* stream) {
edisonn@google.com2a827e82012-10-10 15:20:34 +000017 SkASSERT(NULL == fPicture);
18 SkASSERT(NULL == fCanvas.get());
19 if (fPicture != NULL || NULL != fCanvas.get()) {
20 return;
21 }
22
23 SkASSERT(pict != NULL);
24 if (NULL == pict) {
25 return;
26 }
27
28 fPicture = pict;
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000029 fCanvas.reset(this->setupCanvas(stream, pict->width(), pict->height()));
edisonn@google.com2a827e82012-10-10 15:20:34 +000030}
31
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000032SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) {
33 fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL, fEncoder));
edisonn@google.com2a827e82012-10-10 15:20:34 +000034
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000035 SkCanvas* canvas = fPdfDoc->beginPage(SkIntToScalar(width), SkIntToScalar(height));
36 canvas->ref();
37
38 return canvas;
edisonn@google.com2a827e82012-10-10 15:20:34 +000039}
40
41void PdfRenderer::end() {
42 fPicture = NULL;
43 fCanvas.reset(NULL);
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000044 fPdfDoc.reset(NULL);
edisonn@google.com2a827e82012-10-10 15:20:34 +000045}
46
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000047bool SimplePdfRenderer::render() {
edisonn@google.com2a827e82012-10-10 15:20:34 +000048 SkASSERT(fCanvas.get() != NULL);
49 SkASSERT(fPicture != NULL);
50 if (NULL == fCanvas.get() || NULL == fPicture) {
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000051 return false;
edisonn@google.com2a827e82012-10-10 15:20:34 +000052 }
53
54 fCanvas->drawPicture(*fPicture);
55 fCanvas->flush();
commit-bot@chromium.org5e009892013-10-14 13:42:12 +000056
57 return fPdfDoc->close();
edisonn@google.com2a827e82012-10-10 15:20:34 +000058}
59
60}