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

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: 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 "SkCanvasSimplifier.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, void (*doneProc)(SkWStream*)) 15 SkDocument_PDF(SkWStream* stream, void (*doneProc)(SkWStream*),
15 : SkDocument(stream, doneProc) { 16 EncodeToDCTStream encoder)
17 : SkDocument(stream, doneProc)
18 , fEncoder(encoder) {
16 fDoc = SkNEW(SkPDFDocument); 19 fDoc = SkNEW(SkPDFDocument);
17 fCanvas = NULL; 20 fCanvas = NULL;
18 fDevice = NULL; 21 fDevice = NULL;
19 } 22 }
20 23
21 virtual ~SkDocument_PDF() { 24 virtual ~SkDocument_PDF() {
22 // subclasses must call close() in their destructors 25 // subclasses must call close() in their destructors
23 this->close(); 26 this->close();
24 } 27 }
25 28
26 protected: 29 protected:
27 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 30 virtual SkCanvas* onBeginPage(const SkSize& trimSize,
28 const SkRect& content) SK_OVERRIDE { 31 const SkRect* mediaBox) SK_OVERRIDE {
29 SkASSERT(NULL == fCanvas); 32 SkASSERT(NULL == fCanvas);
30 SkASSERT(NULL == fDevice); 33 SkASSERT(NULL == fDevice);
31 34
32 SkISize pageS, contentS; 35 SkRect content = mediaBox ? *mediaBox : SkRect::MakeSize(trimSize);
33 SkMatrix matrix;
34 36
35 pageS.set(SkScalarRoundToInt(width), SkScalarRoundToInt(height)); 37 fDevice = SkNEW_ARGS(SkPDFDevice, (trimSize, content));
36 contentS.set(SkScalarRoundToInt(content.width()), 38 if (fEncoder) {
37 SkScalarRoundToInt(content.height())); 39 fDevice->setDCTEncoder(fEncoder);
38 matrix.setTranslate(content.fLeft, content.fTop); 40 }
39 41 fCanvas = SkNEW_ARGS(SkCanvasSimplifier, (fDevice));
40 fDevice = SkNEW_ARGS(SkPDFDevice, (pageS, contentS, matrix));
41 fCanvas = SkNEW_ARGS(SkCanvas, (fDevice));
42 return fCanvas; 42 return fCanvas;
43 } 43 }
44 44
45 virtual void onEndPage() SK_OVERRIDE { 45 virtual void onEndPage() SK_OVERRIDE {
46 SkASSERT(fCanvas); 46 SkASSERT(fCanvas);
47 SkASSERT(fDevice); 47 SkASSERT(fDevice);
48 48
49 fCanvas->flush(); 49 fCanvas->flush();
50 fDoc->appendPage(fDevice); 50 fDoc->appendPage(fDevice);
51 51
52 fCanvas->unref(); 52 fCanvas->unref();
53 fDevice->unref(); 53 fDevice->unref();
54 54
55 fCanvas = NULL; 55 fCanvas = NULL;
56 fDevice = NULL; 56 fDevice = NULL;
57 } 57 }
58 58
59 virtual void onClose(SkWStream* stream) SK_OVERRIDE { 59 virtual bool onClose(SkWStream* stream) SK_OVERRIDE {
60 SkASSERT(NULL == fCanvas); 60 SkASSERT(NULL == fCanvas);
61 SkASSERT(NULL == fDevice); 61 SkASSERT(NULL == fDevice);
62 62
63 fDoc->emitPDF(stream); 63 bool success = fDoc->emitPDF(stream);
64 SkDELETE(fDoc);
65 fDoc = NULL;
66 return success;
67 }
68
69 virtual void onAbort() SK_OVERRIDE {
64 SkDELETE(fDoc); 70 SkDELETE(fDoc);
65 fDoc = NULL; 71 fDoc = NULL;
66 } 72 }
67 73
68 private: 74 private:
69 SkPDFDocument* fDoc; 75 SkPDFDocument* fDoc;
70 SkPDFDevice* fDevice; 76 SkPDFDevice* fDevice;
71 SkCanvas* fCanvas; 77 SkCanvas* fCanvas;
78 EncodeToDCTStream fEncoder;
72 }; 79 };
73 80
74 /////////////////////////////////////////////////////////////////////////////// 81 ///////////////////////////////////////////////////////////////////////////////
75 82
76 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*)) { 83 SkDocument* SkDocument::CreatePDF(SkWStream* stream, void (*done)(SkWStream*),
77 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done)) : NULL; 84 EncodeToDCTStream enc) {
85 return stream ? SkNEW_ARGS(SkDocument_PDF, (stream, done, enc)) : NULL;
78 } 86 }
79 87
80 static void delete_wstream(SkWStream* stream) { 88 static void delete_wstream(SkWStream* stream) {
81 SkDELETE(stream); 89 SkDELETE(stream);
82 } 90 }
83 91
84 SkDocument* SkDocument::CreatePDF(const char path[]) { 92 SkDocument* SkDocument::CreatePDF(const char path[], EncodeToDCTStream enc) {
85 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path)); 93 SkFILEWStream* stream = SkNEW_ARGS(SkFILEWStream, (path));
86 if (!stream->isValid()) { 94 if (!stream->isValid()) {
87 SkDELETE(stream); 95 SkDELETE(stream);
88 return NULL; 96 return NULL;
89 } 97 }
90 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream)); 98 return SkNEW_ARGS(SkDocument_PDF, (stream, delete_wstream, enc));
91 } 99 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698