1 /* 2 * Copyright 2013 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkDocument_DEFINED 9 #define SkDocument_DEFINED 10 11 #include "SkRect.h" 12 #include "SkRefCnt.h" 13 14 class SkCanvas; 15 class SkWStream; 16 17 /** 18 * High-level API for creating a document-based canvas. To use.. 19 * 20 * 1. Create a document, specifying a stream to store the output. 21 * 2. For each "page" of content: 22 * a. canvas = doc->beginPage(...) 23 * b. draw_my_content(canvas); 24 * c. doc->endPage(); 25 * 3. Close the document with doc->close(). 26 */ 27 class SkDocument : public SkRefCnt { 28 public: 29 SK_DECLARE_INST_COUNT(SkDocument) 30 31 /** 32 * Create a PDF-backed document, writing the results into a file. 33 * If there is an error trying to create the doc, returns NULL. 34 */ 35 static SkDocument* CreatePDF(const char filename[]); 36 37 /** 38 * Create a PDF-backed document, writing the results into a stream. 39 * If there is an error trying to create the doc, returns NULL. 40 * 41 * The document may write to the stream at anytime during its lifetime, 42 * until either close() is called or the document is deleted. Once close() 43 * has been called, and all of the data has been written to the stream, 44 * if there is a Done proc provided, it will be called with the stream. 45 * The proc can delete the stream, or whatever it needs to do. 46 */ 47 static SkDocument* CreatePDF(SkWStream*, void (*Done)(SkWStream*) = NULL); 48 49 /** 50 * Begin a new page for the document, returning the canvas that will draw 51 * into the page. The document owns this canvas, and it will go out of 52 * scope when endPage() or close() is called, or the document is deleted. 53 */ 54 SkCanvas* beginPage(SkScalar width, SkScalar height, 55 const SkRect* content = NULL); 56 57 /** 58 * Call endPage() when the content for the current page has been drawn 59 * (into the canvas returned by beginPage()). After this call the canvas 60 * returned by beginPage() will be out-of-scope. 61 */ 62 void endPage(); 63 64 /** 65 * Call close() when all pages have been drawn. This will close the file 66 * or stream holding the document's contents. After close() the document 67 * can no longer add new pages. Deleting the document will automatically 68 * call close() if need be. 69 */ 70 void close(); 71 72 protected: 73 SkDocument(SkWStream*, void (*)(SkWStream*)); 74 // note: subclasses must call close() in their destructor, as the base class 75 // cannot do this for them. 76 virtual ~SkDocument(); 77 78 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, 79 const SkRect& content) = 0; 80 virtual void onEndPage() = 0; 81 virtual void onClose(SkWStream*) = 0; 82 83 enum State { 84 kBetweenPages_State, 85 kInPage_State, 86 kClosed_State 87 }; 88 State getState() const { return fState; } 89 90 private: 91 SkWStream* fStream; 92 void (*fDoneProc)(SkWStream*); 93 State fState; 94 95 typedef SkRefCnt INHERITED; 96 }; 97 98 #endif 99