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 "SkDevice.h" | 10 #include "SkDevice.h" |
11 #include "SkPDFDevice.h" | 11 #include "SkPDFDevice.h" |
12 #include "SkPDFDocument.h" | 12 #include "SkPDFDocument.h" |
13 | 13 |
14 namespace sk_tools { | 14 namespace sk_tools { |
15 | 15 |
16 void PdfRenderer::init(SkPicture* pict) { | 16 void PdfRenderer::init(SkPicture* pict, SkWStream* stream) { |
17 SkASSERT(NULL == fPicture); | 17 SkASSERT(NULL == fPicture); |
18 SkASSERT(NULL == fCanvas.get()); | 18 SkASSERT(NULL == fCanvas.get()); |
19 if (fPicture != NULL || NULL != fCanvas.get()) { | 19 if (fPicture != NULL || NULL != fCanvas.get()) { |
20 return; | 20 return; |
21 } | 21 } |
22 | 22 |
23 SkASSERT(pict != NULL); | 23 SkASSERT(pict != NULL); |
24 if (NULL == pict) { | 24 if (NULL == pict) { |
25 return; | 25 return; |
26 } | 26 } |
27 | 27 |
28 fPicture = pict; | 28 fPicture = pict; |
29 fCanvas.reset(this->setupCanvas()); | 29 fCanvas.reset(this->setupCanvas(stream, pict->width(), pict->height())); |
30 } | 30 } |
31 | 31 |
32 SkCanvas* PdfRenderer::setupCanvas() { | 32 SkCanvas* PdfRenderer::setupCanvas(SkWStream* stream, int width, int height) { |
33 return this->setupCanvas(fPicture->width(), fPicture->height()); | 33 fPdfDoc.reset(SkDocument::CreatePDF(stream, NULL, fEncoder)); |
34 } | |
35 | 34 |
36 SkCanvas* PdfRenderer::setupCanvas(int width, int height) { | 35 SkCanvas* canvas = fPdfDoc->beginPage(SkIntToScalar(width), SkIntToScalar(he
ight)); |
37 SkISize pageSize = SkISize::Make(width, height); | 36 canvas->ref(); |
38 fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I())); | 37 |
39 fPDFDevice->setDCTEncoder(fEncoder); | 38 return canvas; |
40 return SkNEW_ARGS(SkCanvas, (fPDFDevice)); | |
41 } | 39 } |
42 | 40 |
43 void PdfRenderer::end() { | 41 void PdfRenderer::end() { |
44 fPicture = NULL; | 42 fPicture = NULL; |
45 fCanvas.reset(NULL); | 43 fCanvas.reset(NULL); |
46 if (fPDFDevice) { | 44 fPdfDoc.reset(NULL); |
47 SkDELETE(fPDFDevice); | |
48 fPDFDevice = NULL; | |
49 } | |
50 } | 45 } |
51 | 46 |
52 void PdfRenderer::write(SkWStream* stream) const { | 47 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); | 48 SkASSERT(fCanvas.get() != NULL); |
60 SkASSERT(fPicture != NULL); | 49 SkASSERT(fPicture != NULL); |
61 if (NULL == fCanvas.get() || NULL == fPicture) { | 50 if (NULL == fCanvas.get() || NULL == fPicture) { |
62 return; | 51 return false; |
63 } | 52 } |
64 | 53 |
65 fCanvas->drawPicture(*fPicture); | 54 fCanvas->drawPicture(*fPicture); |
66 fCanvas->flush(); | 55 fCanvas->flush(); |
| 56 |
| 57 return fPdfDoc->close(); |
67 } | 58 } |
68 | 59 |
69 } | 60 } |
OLD | NEW |