Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: src/doc/SkDocument_PDF.cpp

Issue 941023005: PDF : New factory function for SkPDFDevice (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase on 07d5947 Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/pdf/SkPDFCanon.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2013 Google Inc. 2 * Copyright 2013 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 "SkDocument.h" 8 #include "SkDocument.h"
9 #include "SkPDFCanon.h"
9 #include "SkPDFDocument.h" 10 #include "SkPDFDocument.h"
10 #include "SkPDFDevice.h" 11 #include "SkPDFDevice.h"
11 12
12 class SkDocument_PDF : public SkDocument { 13 class SkDocument_PDF : public SkDocument {
13 public: 14 public:
14 SkDocument_PDF(SkWStream* stream, 15 SkDocument_PDF(SkWStream* stream,
15 void (*doneProc)(SkWStream*,bool), 16 void (*doneProc)(SkWStream*,bool),
16 SkScalar rasterDpi) 17 SkScalar rasterDpi)
17 : SkDocument(stream, doneProc) 18 : SkDocument(stream, doneProc)
18 , fDoc(SkNEW(SkPDFDocument)) 19 , fDoc(SkNEW(SkPDFDocument))
19 , fDevice(NULL)
20 , fCanvas(NULL)
21 , fRasterDpi(rasterDpi) {} 20 , fRasterDpi(rasterDpi) {}
22 21
23 virtual ~SkDocument_PDF() { 22 virtual ~SkDocument_PDF() {
24 // subclasses must call close() in their destructors 23 // subclasses must call close() in their destructors
25 this->close(); 24 this->close();
26 } 25 }
27 26
28 protected: 27 protected:
29 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 28 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
30 const SkRect& trimBox) SK_OVERRIDE { 29 const SkRect& trimBox) SK_OVERRIDE {
31 SkASSERT(NULL == fCanvas); 30 SkASSERT(!fCanvas.get());
32 SkASSERT(NULL == fDevice); 31 SkASSERT(!fDevice.get());
33 32
34 SkISize mediaBoxSize; 33 SkISize pageSize = SkISize::Make(
35 mediaBoxSize.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); 34 SkScalarRoundToInt(width), SkScalarRoundToInt(height));
36 35 fDevice.reset(SkPDFDevice::Create(pageSize, fRasterDpi, &fCanon));
37 fDevice = SkNEW_ARGS(SkPDFDevice, (mediaBoxSize, mediaBoxSize, SkMatrix: :I())); 36 fCanvas.reset(SkNEW_ARGS(SkCanvas, (fDevice)));
38 if (fRasterDpi != 0) {
39 fDevice->setRasterDpi(fRasterDpi);
40 }
41 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
42 fCanvas->clipRect(trimBox); 37 fCanvas->clipRect(trimBox);
43 fCanvas->translate(trimBox.x(), trimBox.y()); 38 fCanvas->translate(trimBox.x(), trimBox.y());
44 return fCanvas; 39 return fCanvas.get();
45 } 40 }
46 41
47 void onEndPage() SK_OVERRIDE { 42 void onEndPage() SK_OVERRIDE {
48 SkASSERT(fCanvas); 43 SkASSERT(fCanvas.get());
49 SkASSERT(fDevice); 44 SkASSERT(fDevice.get());
50 45
51 fCanvas->flush(); 46 fCanvas->flush();
52 fDoc->appendPage(fDevice); 47 fDoc->appendPage(fDevice.get());
53 48
54 fCanvas->unref(); 49 fCanvas.reset(NULL);
55 fDevice->unref(); 50 fDevice.reset(NULL);
56
57 fCanvas = NULL;
58 fDevice = NULL;
59 } 51 }
60 52
61 bool onClose(SkWStream* stream) SK_OVERRIDE { 53 bool onClose(SkWStream* stream) SK_OVERRIDE {
62 SkASSERT(NULL == fCanvas); 54 SkASSERT(!fCanvas.get());
63 SkASSERT(NULL == fDevice); 55 SkASSERT(!fDevice.get());
64 56
65 bool success = fDoc->emitPDF(stream); 57 bool success = fDoc->emitPDF(stream);
66 SkDELETE(fDoc); 58 fDoc.free();
67 fDoc = NULL; 59 SkDEBUGCODE(fCanon.assertEmpty());
68 return success; 60 return success;
69 } 61 }
70 62
71 void onAbort() SK_OVERRIDE { 63 void onAbort() SK_OVERRIDE {
72 SkDELETE(fDoc); 64 fDoc.free();
73 fDoc = NULL; 65 SkDEBUGCODE(fCanon.assertEmpty());
74 } 66 }
75 67
76 private: 68 private:
77 SkPDFDocument* fDoc; 69 SkPDFCanon fCanon;
78 SkPDFDevice* fDevice; 70 SkAutoTDelete<SkPDFDocument> fDoc;
79 SkCanvas* fCanvas; 71 SkAutoTUnref<SkPDFDevice> fDevice;
80 SkScalar fRasterDpi; 72 SkAutoTUnref<SkCanvas> fCanvas;
73 SkScalar fRasterDpi;
81 }; 74 };
82 75
83 /////////////////////////////////////////////////////////////////////////////// 76 ///////////////////////////////////////////////////////////////////////////////
84 77
85 SkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) { 78 SkDocument* SkDocument::CreatePDF(SkWStream* stream, SkScalar dpi) {
86 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, NULL, dpi)) : NULL; 79 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, NULL, dpi)) : NULL;
87 } 80 }
88 81
89 static void delete_wstream(SkWStream* stream, bool aborted) { 82 static void delete_wstream(SkWStream* stream, bool aborted) {
90 SkDELETE(stream); 83 SkDELETE(stream);
91 } 84 }
92 85
93 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) { 86 SkDocument* SkDocument::CreatePDF(const char path[], SkScalar dpi) {
94 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); 87 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
95 if (!stream->isValid()) { 88 if (!stream->isValid()) {
96 SkDELETE(stream); 89 SkDELETE(stream);
97 return NULL; 90 return NULL;
98 } 91 }
99 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, dpi)); 92 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, dpi));
100 } 93 }
OLDNEW
« no previous file with comments | « no previous file | src/pdf/SkPDFCanon.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698