| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "PdfRenderer.h" | 8 #include "PdfRenderer.h" |
| 9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
| 10 #include "SkCanvasSimplifier.h" |
| 10 #include "SkDevice.h" | 11 #include "SkDevice.h" |
| 11 #include "SkPDFDevice.h" | 12 #include "SkPDFDevice.h" |
| 12 #include "SkPDFDocument.h" | 13 #include "SkPDFDocument.h" |
| 13 | 14 |
| 14 namespace sk_tools { | 15 namespace sk_tools { |
| 15 | 16 |
| 16 void PdfRenderer::init(SkPicture* pict) { | 17 void PdfRenderer::init(SkPicture* pict, SkWStream* stream) { |
| 17 SkASSERT(NULL == fPicture); | 18 SkASSERT(NULL == fPicture); |
| 18 SkASSERT(NULL == fCanvas.get()); | 19 SkASSERT(NULL == fCanvas.get()); |
| 19 if (fPicture != NULL || NULL != fCanvas.get()) { | 20 if (fPicture != NULL || NULL != fCanvas.get()) { |
| 20 return; | 21 return; |
| 21 } | 22 } |
| 22 | 23 |
| 23 SkASSERT(pict != NULL); | 24 SkASSERT(pict != NULL); |
| 24 if (NULL == pict) { | 25 if (NULL == pict) { |
| 25 return; | 26 return; |
| 26 } | 27 } |
| 27 | 28 |
| 28 fPicture = pict; | 29 fPicture = pict; |
| 29 fCanvas.reset(this->setupCanvas()); | 30 fCanvas.reset(this->setupCanvas(stream, pict->width(), pict->height())); |
| 30 } | 31 } |
| 31 | 32 |
| 32 SkCanvas* PdfRenderer::setupCanvas() { | 33 SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) { |
| 33 return this->setupCanvas(fPicture->width(), fPicture->height()); | 34 fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL)); |
| 34 } | 35 fPdfDoc->setDCTEncoder(fEncoder); |
| 35 | 36 |
| 36 SkCanvas* PdfRenderer::setupCanvas(int width, int height) { | 37 SkSize pageSize = SkSize::Make(SkIntToScalar(width), SkIntToScalar(height)); |
| 37 SkISize pageSize = SkISize::Make(width, height); | 38 SkCanvas* canvas = fPdfDoc->beginPage(pageSize); |
| 38 fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I())); | 39 canvas->ref(); |
| 39 fPDFDevice->setDCTEncoder(fEncoder); | 40 |
| 40 return SkNEW_ARGS(SkCanvas, (fPDFDevice)); | 41 return canvas; |
| 41 } | 42 } |
| 42 | 43 |
| 43 void PdfRenderer::end() { | 44 void PdfRenderer::end() { |
| 44 fPicture = NULL; | 45 fPicture = NULL; |
| 45 fCanvas.reset(NULL); | 46 fCanvas.reset(NULL); |
| 46 if (fPDFDevice) { | 47 fPdfDoc.reset(NULL); |
| 47 SkDELETE(fPDFDevice); | |
| 48 fPDFDevice = NULL; | |
| 49 } | |
| 50 } | 48 } |
| 51 | 49 |
| 52 void PdfRenderer::write(SkWStream* stream) const { | 50 bool SimplePdfRenderer::render() { |
| 53 SkPDFDocument doc; | |
| 54 doc.appendPage(fPDFDevice); | |
| 55 doc.emitPDF(stream); | |
| 56 } | |
| 57 | |
| 58 void SimplePdfRenderer::render() { | |
| 59 SkASSERT(fCanvas.get() != NULL); | 51 SkASSERT(fCanvas.get() != NULL); |
| 60 SkASSERT(fPicture != NULL); | 52 SkASSERT(fPicture != NULL); |
| 61 if (NULL == fCanvas.get() || NULL == fPicture) { | 53 if (NULL == fCanvas.get() || NULL == fPicture) { |
| 62 return; | 54 return false; |
| 63 } | 55 } |
| 64 | 56 |
| 65 fCanvas->drawPicture(*fPicture); | 57 fCanvas->drawPicture(*fPicture); |
| 66 fCanvas->flush(); | 58 fCanvas->flush(); |
| 59 |
| 60 // TODO(edisonn): SkDocument should report failure somehow! |
| 61 fPdfDoc->close(); |
| 62 return true; |
| 67 } | 63 } |
| 68 | 64 |
| 69 } | 65 } |
| OLD | NEW |