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

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

Issue 24811002: Update the SkDocument interface to allow for 1) abort won't emit pdf, 2) close can report success/f… (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: fixes after code review Created 7 years, 2 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 | Annotate | Revision Log
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 "SkPDFDocument.h" 9 #include "SkPDFDocument.h"
10 #include "SkPDFDevice.h" 10 #include "SkPDFDevice.h"
11 11
12 class SkDocument_PDF : public SkDocument { 12 class SkDocument_PDF : public SkDocument {
13 public: 13 public:
14 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*)) 14 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*),
15 : SkDocument(stream, doneProc) { 15 SkPicture::EncodeBitmap encoder)
16 : SkDocument(stream, doneProc)
17 , fEncoder(encoder) {
16 fDoc = SkNEW(SkPDFDocument); 18 fDoc = SkNEW(SkPDFDocument);
17 fCanvas = NULL; 19 fCanvas = NULL;
18 fDevice = NULL; 20 fDevice = NULL;
19 } 21 }
20 22
21 virtual ~SkDocument_PDF() { 23 virtual ~SkDocument_PDF() {
22 // subclasses must call close() in their destructors 24 // subclasses must call close() in their destructors
23 this->close(); 25 this->close();
24 } 26 }
25 27
26 protected: 28 protected:
27 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 29 virtual SkCanvas* onBeginPage(const SkSize& trimSize,
28 const SkRect& content) SK_OVERRIDE { 30 const SkRect* mediaBox) SK_OVERRIDE {
29 SkASSERT(NULL == fCanvas); 31 SkASSERT(NULL == fCanvas);
30 SkASSERT(NULL == fDevice); 32 SkASSERT(NULL == fDevice);
31 33
32 SkISize pageS, contentS; 34 SkRect content = mediaBox ? *mediaBox : SkRect::MakeSize(trimSize);
33 SkMatrix matrix;
34 35
35 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); 36 fDevice = SkNEW_ARGS(SkPDFDeviceFlattener, (trimSize, content));
36 contentS.set(SkScalarRoundToInt(content.width()), 37 if (fEncoder) {
37 SkScalarRoundToInt(content.height())); 38 fDevice->setDCTEncoder(fEncoder);
38 matrix.setTranslate(content.fLeft, content.fTop); 39 }
39
40 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix));
41 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice)); 40 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
42 return fCanvas; 41 return fCanvas;
43 } 42 }
44 43
45 virtual void onEndPage() SK_OVERRIDE { 44 virtual void onEndPage() SK_OVERRIDE {
46 SkASSERT(fCanvas); 45 SkASSERT(fCanvas);
47 SkASSERT(fDevice); 46 SkASSERT(fDevice);
48 47
49 fCanvas->flush(); 48 fCanvas->flush();
50 fDoc->appendPage(fDevice); 49 fDoc->appendPage(fDevice);
51 50
52 fCanvas->unref(); 51 fCanvas->unref();
53 fDevice->unref(); 52 fDevice->unref();
54 53
55 fCanvas = NULL; 54 fCanvas = NULL;
56 fDevice = NULL; 55 fDevice = NULL;
57 } 56 }
58 57
59 virtual void onClose(SkWStream* stream) SK_OVERRIDE { 58 virtual bool onClose(SkWStream* stream) SK_OVERRIDE {
60 SkASSERT(NULL == fCanvas); 59 SkASSERT(NULL == fCanvas);
61 SkASSERT(NULL == fDevice); 60 SkASSERT(NULL == fDevice);
62 61
63 fDoc->emitPDF(stream); 62 bool success = fDoc->emitPDF(stream);
63 SkDELETE(fDoc);
64 fDoc = NULL;
65 return success;
66 }
67
68 virtual void onAbort() SK_OVERRIDE {
64 SkDELETE(fDoc); 69 SkDELETE(fDoc);
65 fDoc = NULL; 70 fDoc = NULL;
66 } 71 }
67 72
68 private: 73 private:
69 SkPDFDocument* fDoc; 74 SkPDFDocument* fDoc;
70 SkPDFDevice* fDevice; 75 SkPDFDeviceFlattener* fDevice;
71 SkCanvas* fCanvas; 76 SkCanvas* fCanvas;
77 SkPicture::EncodeBitmap fEncoder;
72 }; 78 };
73 79
74 /////////////////////////////////////////////////////////////////////////////// 80 ///////////////////////////////////////////////////////////////////////////////
75 81
76 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*)) { 82 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*),
77 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL; 83 SkPicture::EncodeBitmap enc) {
84 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL;
78 } 85 }
79 86
80 static void delete_wstream(SkWStream* stream) { 87 static void delete_wstream(SkWStream* stream) {
81 SkDELETE(stream); 88 SkDELETE(stream);
82 } 89 }
83 90
84 SkDocument* SkDocument::CreatePDF(const char path[]) { 91 SkDocument* SkDocument::CreatePDF(const char path[], SkPicture::EncodeBitmap enc ) {
85 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); 92 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
86 if (!stream->isValid()) { 93 if (!stream->isValid()) {
87 SkDELETE(stream); 94 SkDELETE(stream);
88 return NULL; 95 return NULL;
89 } 96 }
90 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream)); 97 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc));
91 } 98 }
OLDNEW
« include/pdf/SkPDFDevice.h ('K') | « src/doc/SkDocument.cpp ('k') | src/pdf/SkPDFDevice.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698