edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 14 | namespace sk_tools { |
| 15 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 16 | void PdfRenderer::init(SkPicture* pict, SkWStream* stream) { |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 17 | 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.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 29 | fCanvas.reset(this->setupCanvas(stream, pict->width(), pict->height())); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 30 | } |
| 31 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 32 | SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) { |
| 33 | fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL, fEncoder)); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 34 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 35 | SkCanvas* canvas = fPdfDoc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); |
| 36 | canvas->ref(); |
| 37 | |
| 38 | return canvas; |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void PdfRenderer::end() { |
| 42 | fPicture = NULL; |
| 43 | fCanvas.reset(NULL); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 44 | fPdfDoc.reset(NULL); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 45 | } |
| 46 | |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 47 | bool SimplePdfRenderer::render() { |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 48 | SkASSERT(fCanvas.get() != NULL); |
| 49 | SkASSERT(fPicture != NULL); |
| 50 | if (NULL == fCanvas.get() || NULL == fPicture) { |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 51 | return false; |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | fCanvas->drawPicture(*fPicture); |
| 55 | fCanvas->flush(); |
commit-bot@chromium.org | 5e00989 | 2013-10-14 13:42:12 +0000 | [diff] [blame] | 56 | |
| 57 | return fPdfDoc->close(); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | } |