xref: /aosp_15_r20/external/skia/include/core/SkDocument.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 "include/core/SkRefCnt.h"
12 #include "include/core/SkScalar.h"
13 #include "include/private/base/SkAPI.h"
14 
15 class SkCanvas;
16 class SkWStream;
17 struct SkRect;
18 
19 /** SK_ScalarDefaultDPI is 72 dots per inch. */
20 static constexpr SkScalar SK_ScalarDefaultRasterDPI = 72.0f;
21 
22 /**
23  *  High-level API for creating a document-based canvas. To use..
24  *
25  *  1. Create a document, specifying a stream to store the output.
26  *  2. For each "page" of content:
27  *      a. canvas = doc->beginPage(...)
28  *      b. draw_my_content(canvas);
29  *      c. doc->endPage();
30  *  3. Close the document with doc->close().
31  */
32 class SK_API SkDocument : public SkRefCnt {
33 public:
34 
35     /**
36      *  Begin a new page for the document, returning the canvas that will draw
37      *  into the page. The document owns this canvas, and it will go out of
38      *  scope when endPage() or close() is called, or the document is deleted.
39      *  This will call endPage() if there is a currently active page.
40      */
41     SkCanvas* beginPage(SkScalar width, SkScalar height, const SkRect* content = nullptr);
42 
43     /**
44      *  Call endPage() when the content for the current page has been drawn
45      *  (into the canvas returned by beginPage()). After this call the canvas
46      *  returned by beginPage() will be out-of-scope.
47      */
48     void endPage();
49 
50     /**
51      *  Call close() when all pages have been drawn. This will close the file
52      *  or stream holding the document's contents. After close() the document
53      *  can no longer add new pages. Deleting the document will automatically
54      *  call close() if need be.
55      */
56     void close();
57 
58     /**
59      *  Call abort() to stop producing the document immediately.
60      *  The stream output must be ignored, and should not be trusted.
61      */
62     void abort();
63 
64 protected:
65     SkDocument(SkWStream*);
66 
67     // note: subclasses must call close() in their destructor, as the base class
68     // cannot do this for them.
69     ~SkDocument() override;
70 
71     virtual SkCanvas* onBeginPage(SkScalar width, SkScalar height) = 0;
72     virtual void onEndPage() = 0;
73     virtual void onClose(SkWStream*) = 0;
74     virtual void onAbort() = 0;
75 
76     // Allows subclasses to write to the stream as pages are written.
getStream()77     SkWStream* getStream() { return fStream; }
78 
79     enum State {
80         kBetweenPages_State,
81         kInPage_State,
82         kClosed_State
83     };
getState()84     State getState() const { return fState; }
85 
86 private:
87     SkWStream* fStream;
88     State      fState;
89 
90     using INHERITED = SkRefCnt;
91 };
92 
93 #endif
94