OLD | NEW |
---|---|
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 #ifndef SkDocument_DEFINED | 8 #ifndef SkDocument_DEFINED |
9 #define SkDocument_DEFINED | 9 #define SkDocument_DEFINED |
10 | 10 |
11 #include "SkRect.h" | 11 #include "SkRect.h" |
12 #include "SkRefCnt.h" | 12 #include "SkRefCnt.h" |
13 #include "SkBitmap.h" | |
14 #include "SkPicture.h" | |
15 #include "SkRect.h" | |
13 | 16 |
14 class SkCanvas; | 17 class SkCanvas; |
15 class SkWStream; | 18 class SkWStream; |
16 | 19 |
17 /** | 20 /** |
18 * High-level API for creating a document-based canvas. To use.. | 21 * High-level API for creating a document-based canvas. To use.. |
19 * | 22 * |
20 * 1. Create a document, specifying a stream to store the output. | 23 * 1. Create a document, specifying a stream to store the output. |
21 * 2. For each "page" of content: | 24 * 2. For each "page" of content: |
22 * a. canvas = doc->beginPage(...) | 25 * a. canvas = doc->beginPage(...) |
23 * b. draw_my_content(canvas); | 26 * b. draw_my_content(canvas); |
24 * c. doc->endPage(); | 27 * c. doc->endPage(); |
25 * 3. Close the document with doc->close(). | 28 * 3. Close the document with doc->close(). |
26 */ | 29 */ |
27 class SkDocument : public SkRefCnt { | 30 class SkDocument : public SkRefCnt { |
28 public: | 31 public: |
29 SK_DECLARE_INST_COUNT(SkDocument) | 32 SK_DECLARE_INST_COUNT(SkDocument) |
30 | 33 |
31 /** | 34 /** |
32 * Create a PDF-backed document, writing the results into a file. | 35 * Create a PDF-backed document, writing the results into a file. |
33 * If there is an error trying to create the doc, returns NULL. | 36 * If there is an error trying to create the doc, returns NULL. |
37 * encoder sets the DCTEncoder for images, to encode a bitmap | |
38 * as JPEG (DCT). | |
34 */ | 39 */ |
35 static SkDocument* CreatePDF(const char filename[]); | 40 static SkDocument* CreatePDF(const char filename[], |
41 SkPicture::EncodeBitmap encoder = NULL); | |
36 | 42 |
37 /** | 43 /** |
38 * Create a PDF-backed document, writing the results into a stream. | 44 * Create a PDF-backed document, writing the results into a stream. |
39 * If there is an error trying to create the doc, returns NULL. | 45 * If there is an error trying to create the doc, returns NULL. |
40 * | 46 * |
41 * The document may write to the stream at anytime during its lifetime, | 47 * 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() | 48 * 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, | 49 * 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. | 50 * 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. | 51 * The proc can delete the stream, or whatever it needs to do. |
52 * encoder sets the DCTEncoder for images, to encode a bitmap | |
53 * as JPEG (DCT). | |
46 */ | 54 */ |
47 static SkDocument* CreatePDF(SkWStream*, void (*Done)(SkWStream*) = NULL); | 55 static SkDocument* CreatePDF(SkWStream*, void (*Done)(SkWStream*) = NULL, |
56 SkPicture::EncodeBitmap encoder = NULL); | |
reed1
2013/10/07 13:24:54
I wonder, if we end up with more options in the fu
edisonn
2013/10/07 19:29:06
IMHO, since this is a function pointer, the caller
| |
48 | 57 |
49 /** | 58 /** |
50 * Begin a new page for the document, returning the canvas that will draw | 59 * 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 | 60 * 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. | 61 * scope when endPage() or close() is called, or the document is deleted. |
62 * @param trimSize The content size of the page in points. | |
63 * Everything outside of trimBox is considered non essential, | |
64 * for example margins are outside of trim box. | |
65 * @param mediaBox The Page size in points. If mediaBox is null, then we | |
66 * set the mediaBox to the trim rectangle. | |
53 */ | 67 */ |
54 SkCanvas* beginPage(SkScalar width, SkScalar height, | 68 SkCanvas* beginPage(const SkSize& trimSize, |
reed1
2013/10/07 13:24:54
I need a better (easier) picture of what these siz
edisonn
2013/10/07 19:29:06
A good reason would be to maintain consistency wit
| |
55 const SkRect* content = NULL); | 69 const SkRect* mediaBox = NULL); |
56 | 70 |
57 /** | 71 /** |
58 * Call endPage() when the content for the current page has been drawn | 72 * Call endPage() when the content for the current page has been drawn |
59 * (into the canvas returned by beginPage()). After this call the canvas | 73 * (into the canvas returned by beginPage()). After this call the canvas |
60 * returned by beginPage() will be out-of-scope. | 74 * returned by beginPage() will be out-of-scope. |
61 */ | 75 */ |
62 void endPage(); | 76 void endPage(); |
63 | 77 |
64 /** | 78 /** |
65 * Call close() when all pages have been drawn. This will close the file | 79 * 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 | 80 * or stream holding the document's contents. After close() the document |
67 * can no longer add new pages. Deleting the document will automatically | 81 * can no longer add new pages. Deleting the document will automatically |
68 * call close() if need be. | 82 * call close() if need be. |
83 * Returns true on success or false on failure. | |
69 */ | 84 */ |
70 void close(); | 85 bool close(); |
86 | |
87 /** | |
88 * Call abort() to stop producing the document immediately. | |
89 * The stream output must be ignored, and should not be trusted. | |
90 */ | |
91 void abort(); | |
71 | 92 |
72 protected: | 93 protected: |
73 SkDocument(SkWStream*, void (*)(SkWStream*)); | 94 SkDocument(SkWStream*, void (*)(SkWStream*)); |
74 // note: subclasses must call close() in their destructor, as the base class | 95 // note: subclasses must call close() in their destructor, as the base class |
75 // cannot do this for them. | 96 // cannot do this for them. |
76 virtual ~SkDocument(); | 97 virtual ~SkDocument(); |
77 | 98 |
78 virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height, | 99 virtual SkCanvas* onBeginPage(const SkSize& trimSize, |
79 const SkRect& content) = 0; | 100 const SkRect* mediaBox) = 0; |
80 virtual void onEndPage() = 0; | 101 virtual void onEndPage() = 0; |
81 virtual void onClose(SkWStream*) = 0; | 102 virtual bool onClose(SkWStream*) = 0; |
103 virtual void onAbort() = 0; | |
82 | 104 |
83 enum State { | 105 enum State { |
84 kBetweenPages_State, | 106 kBetweenPages_State, |
85 kInPage_State, | 107 kInPage_State, |
86 kClosed_State | 108 kClosed_State |
87 }; | 109 }; |
88 State getState() const { return fState; } | 110 State getState() const { return fState; } |
89 | 111 |
90 private: | 112 private: |
91 SkWStream* fStream; | 113 SkWStream* fStream; |
92 void (*fDoneProc)(SkWStream*); | 114 void (*fDoneProc)(SkWStream*); |
93 State fState; | 115 State fState; |
94 | 116 |
95 typedef SkRefCnt INHERITED; | 117 typedef SkRefCnt INHERITED; |
96 }; | 118 }; |
97 | 119 |
98 #endif | 120 #endif |
OLD | NEW |