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, SkWStream* stream) { 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(stream, pict->width(), pict->height())); 30 } 31 32 SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) { 33 fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL, fEncoder)); 34 35 SkCanvas* canvas = fPdfDoc->beginPage(SkIntToScalar(width), SkIntToScalar(height)); 36 canvas->ref(); 37 38 return canvas; 39 } 40 41 void PdfRenderer::end() { 42 fPicture = NULL; 43 fCanvas.reset(NULL); 44 fPdfDoc.reset(NULL); 45 } 46 47 bool SimplePdfRenderer::render() { 48 SkASSERT(fCanvas.get() != NULL); 49 SkASSERT(fPicture != NULL); 50 if (NULL == fCanvas.get() || NULL == fPicture) { 51 return false; 52 } 53 54 fCanvas->drawPicture(*fPicture); 55 fCanvas->flush(); 56 57 return fPdfDoc->close(); 58 } 59 60 } 61