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 "SkBitmap.h"
12 #include "SkPicture.h"
13 #include "SkRect.h"
14 #include "SkRefCnt.h"
15 
16 class SkCanvas;
17 class SkWStream;
18 
19 /** SK_ScalarDefaultDPI is 72 DPI.
20 */
21 #define SK_ScalarDefaultRasterDPI           72.0f
22 
23 /**
24  *  High-level API for creating a document-based canvas. To use..
25  *
26  *  1. Create a document, specifying a stream to store the output.
27  *  2. For each "page" of content:
28  *      a. canvas = doc->beginPage(...)
29  *      b. draw_my_content(canvas);
30  *      c. doc->endPage();
31  *  3. Close the document with doc->close().
32  */
33 class SK_API SkDocument : public SkRefCnt {
34 public:
35     SK_DECLARE_INST_COUNT(SkDocument)
36 
37     /**
38      *  Create a PDF-backed document, writing the results into a SkWStream.
39      *
40      *  PDF pages are sized in point units. 1 pt == 1/72 inch == 127/360 mm.
41      *
42      *  @param SkWStream* A PDF document will be written to this
43      *         stream.  The document may write to the stream at
44      *         anytime during its lifetime, until either close() is
45      *         called or the document is deleted.
46      *  @param dpi The DPI (pixels-per-inch) at which features without
47      *         native PDF support will be rasterized (e.g. draw image
48      *         with perspective, draw text with perspective, ...)  A
49      *         larger DPI would create a PDF that reflects the
50      *         original intent with better fidelity, but it can make
51      *         for larger PDF files too, which would use more memory
52      *         while rendering, and it would be slower to be processed
53      *         or sent online or to printer.
54      *  @returns NULL if there is an error, otherwise a newly created
55      *           PDF-backed SkDocument.
56      */
57     static SkDocument* CreatePDF(SkWStream*,
58                                  SkScalar dpi = SK_ScalarDefaultRasterDPI);
59 
60     /**
61      *  Create a PDF-backed document, writing the results into a file.
62      */
63     static SkDocument* CreatePDF(const char outputFilePath[],
64                                  SkScalar dpi = SK_ScalarDefaultRasterDPI);
65 
66     /**
67      *  Create a XPS-backed document, writing the results into the stream.
68      *  Returns NULL if XPS is not supported.
69      */
70     static SkDocument* CreateXPS(SkWStream* stream,
71                                  SkScalar dpi = SK_ScalarDefaultRasterDPI);
72 
73     /**
74      *  Create a XPS-backed document, writing the results into a file.
75      *  Returns NULL if XPS is not supported.
76      */
77     static SkDocument* CreateXPS(const char path[],
78                                  SkScalar dpi = SK_ScalarDefaultRasterDPI);
79     /**
80      *  Begin a new page for the document, returning the canvas that will draw
81      *  into the page. The document owns this canvas, and it will go out of
82      *  scope when endPage() or close() is called, or the document is deleted.
83      */
84     SkCanvas* beginPage(SkScalar width, SkScalar height,
85                         const SkRect* content = NULL);
86 
87     /**
88      *  Call endPage() when the content for the current page has been drawn
89      *  (into the canvas returned by beginPage()). After this call the canvas
90      *  returned by beginPage() will be out-of-scope.
91      */
92     void endPage();
93 
94     /**
95      *  Call close() when all pages have been drawn. This will close the file
96      *  or stream holding the document's contents. After close() the document
97      *  can no longer add new pages. Deleting the document will automatically
98      *  call close() if need be.
99      *  Returns true on success or false on failure.
100      */
101     bool close();
102 
103     /**
104      *  Call abort() to stop producing the document immediately.
105      *  The stream output must be ignored, and should not be trusted.
106      */
107     void abort();
108 
109 protected:
110     SkDocument(SkWStream*, void (*)(SkWStream*, bool aborted));
111 
112     // note: subclasses must call close() in their destructor, as the base class
113     // cannot do this for them.
114     virtual ~SkDocument();
115 
116     virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height,
117                                   const SkRect& content) = 0;
118     virtual void onEndPage() = 0;
119     virtual bool onClose(SkWStream*) = 0;
120     virtual void onAbort() = 0;
121 
122     // Allows subclasses to write to the stream as pages are written.
getStream()123     SkWStream* getStream() { return fStream; }
124 
125     enum State {
126         kBetweenPages_State,
127         kInPage_State,
128         kClosed_State
129     };
getState()130     State getState() const { return fState; }
131 
132 private:
133     SkWStream* fStream;
134     void       (*fDoneProc)(SkWStream*, bool aborted);
135     State      fState;
136 
137     typedef SkRefCnt INHERITED;
138 };
139 
140 #endif
141