SkDocument.h 2.5 KB

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