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 | |
| 16 | void PdfRenderer::init(SkPicture* pict) { |
| 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; |
| 29 | fCanvas.reset(this->setupCanvas()); |
| 30 | } |
| 31 | |
| 32 | SkCanvas* PdfRenderer::setupCanvas() { |
| 33 | return this->setupCanvas(fPicture->width(), fPicture->height()); |
| 34 | } |
| 35 | |
| 36 | SkCanvas* PdfRenderer::setupCanvas(int width, int height) { |
| 37 | SkISize pageSize = SkISize::Make(width, height); |
| 38 | fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I())); |
edisonn@google.com | d9dfa18 | 2013-04-24 13:01:01 +0000 | [diff] [blame] | 39 | fPDFDevice->setDCTEncoder(fEncoder); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 40 | return SkNEW_ARGS(SkCanvas, (fPDFDevice)); |
| 41 | } |
| 42 | |
| 43 | void PdfRenderer::end() { |
| 44 | fPicture = NULL; |
| 45 | fCanvas.reset(NULL); |
| 46 | if (fPDFDevice) { |
| 47 | SkDELETE(fPDFDevice); |
| 48 | fPDFDevice = NULL; |
| 49 | } |
| 50 | } |
| 51 | |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 52 | void PdfRenderer::write(SkWStream* stream) const { |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 53 | SkPDFDocument doc; |
| 54 | doc.appendPage(fPDFDevice); |
edisonn@google.com | 4fa566b | 2013-01-11 20:30:41 +0000 | [diff] [blame] | 55 | doc.emitPDF(stream); |
edisonn@google.com | 2a827e8 | 2012-10-10 15:20:34 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void SimplePdfRenderer::render() { |
| 59 | SkASSERT(fCanvas.get() != NULL); |
| 60 | SkASSERT(fPicture != NULL); |
| 61 | if (NULL == fCanvas.get() || NULL == fPicture) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | fCanvas->drawPicture(*fPicture); |
| 66 | fCanvas->flush(); |
| 67 | } |
| 68 | |
| 69 | } |