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 SkSize pageSize = SkSize::Make(SkIntToScalar(width), SkIntToScalar(height)); |
37 SkISize pageSize = SkISize::Make(width, height); | 36 SkCanvas* canvas = fPdfDoc->beginPage(pageSize); |
38 fPDFDevice = SkNEW_ARGS(SkPDFDevice, (pageSize, pageSize, SkMatrix::I())); | 37 canvas->ref(); |
39 fPDFDevice->setDCTEncoder(fEncoder); | 38 |
40 return SkNEW_ARGS(SkCanvas, (fPDFDevice)); | 39 return canvas; |
41 } | 40 } |
42 | 41 |
43 void PdfRenderer::end() { | 42 void PdfRenderer::end() { |
44 fPicture = NULL; | 43 fPicture = NULL; |
45 fCanvas.reset(NULL); | 44 fCanvas.reset(NULL); |
46 if (fPDFDevice) { | 45 fPdfDoc.reset(NULL); |
47 SkDELETE(fPDFDevice); | |
48 fPDFDevice = NULL; | |
49 } | |
50 } | 46 } |
51 | 47 |
52 void PdfRenderer::write(SkWStream* stream) const { | 48 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); | 49 SkASSERT(fCanvas.get() != NULL); |
60 SkASSERT(fPicture != NULL); | 50 SkASSERT(fPicture != NULL); |
61 if (NULL == fCanvas.get() || NULL == fPicture) { | 51 if (NULL == fCanvas.get() || NULL == fPicture) { |
62 return; | 52 return false; |
63 } | 53 } |
64 | 54 |
65 fCanvas->drawPicture(*fPicture); | 55 fCanvas->drawPicture(*fPicture); |
66 fCanvas->flush(); | 56 fCanvas->flush(); |
57 | |
58 // TODO(edisonn): SkDocument should report failure somehow! | |
vandebo (ex-Chrome)
2013/09/27 15:50:17
Didn't you change close to return a bool, or does
edisonn
2013/10/04 19:40:37
Done.
| |
59 fPdfDoc->close(); | |
60 return true; | |
67 } | 61 } |
68 | 62 |
69 } | 63 } |
OLD | NEW |