SkDocument.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include "include/core/SkCanvas.h"
  8. #include "include/core/SkDocument.h"
  9. #include "include/core/SkStream.h"
  10. SkDocument::SkDocument(SkWStream* stream) : fStream(stream), fState(kBetweenPages_State) {}
  11. SkDocument::~SkDocument() {
  12. this->close();
  13. }
  14. static SkCanvas* trim(SkCanvas* canvas, SkScalar width, SkScalar height,
  15. const SkRect* content) {
  16. if (content && canvas) {
  17. SkRect inner = *content;
  18. if (!inner.intersect({0, 0, width, height})) {
  19. return nullptr;
  20. }
  21. canvas->clipRect(inner);
  22. canvas->translate(inner.x(), inner.y());
  23. }
  24. return canvas;
  25. }
  26. SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
  27. const SkRect* content) {
  28. if (width <= 0 || height <= 0 || kClosed_State == fState) {
  29. return nullptr;
  30. }
  31. if (kInPage_State == fState) {
  32. this->endPage();
  33. }
  34. SkASSERT(kBetweenPages_State == fState);
  35. fState = kInPage_State;
  36. return trim(this->onBeginPage(width, height), width, height, content);
  37. }
  38. void SkDocument::endPage() {
  39. if (kInPage_State == fState) {
  40. fState = kBetweenPages_State;
  41. this->onEndPage();
  42. }
  43. }
  44. void SkDocument::close() {
  45. for (;;) {
  46. switch (fState) {
  47. case kBetweenPages_State: {
  48. fState = kClosed_State;
  49. this->onClose(fStream);
  50. // we don't own the stream, but we mark it nullptr since we can
  51. // no longer write to it.
  52. fStream = nullptr;
  53. return;
  54. }
  55. case kInPage_State:
  56. this->endPage();
  57. break;
  58. case kClosed_State:
  59. return;
  60. }
  61. }
  62. }
  63. void SkDocument::abort() {
  64. this->onAbort();
  65. fState = kClosed_State;
  66. // we don't own the stream, but we mark it nullptr since we can
  67. // no longer write to it.
  68. fStream = nullptr;
  69. }