reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCanvas.h" |
| 9 | #include "include/core/SkDocument.h" |
| 10 | #include "include/core/SkStream.h" |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 11 | |
Hal Canary | c5980d0 | 2018-01-08 15:02:36 -0500 | [diff] [blame] | 12 | SkDocument::SkDocument(SkWStream* stream) : fStream(stream), fState(kBetweenPages_State) {} |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 13 | |
| 14 | SkDocument::~SkDocument() { |
| 15 | this->close(); |
| 16 | } |
| 17 | |
Hal Canary | fa486cf | 2017-07-20 08:37:50 -0400 | [diff] [blame] | 18 | static SkCanvas* trim(SkCanvas* canvas, SkScalar width, SkScalar height, |
| 19 | const SkRect* content) { |
| 20 | if (content && canvas) { |
| 21 | SkRect inner = *content; |
| 22 | if (!inner.intersect({0, 0, width, height})) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 23 | return nullptr; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 24 | } |
Hal Canary | fa486cf | 2017-07-20 08:37:50 -0400 | [diff] [blame] | 25 | canvas->clipRect(inner); |
| 26 | canvas->translate(inner.x(), inner.y()); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 27 | } |
Hal Canary | fa486cf | 2017-07-20 08:37:50 -0400 | [diff] [blame] | 28 | return canvas; |
| 29 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 30 | |
Hal Canary | fa486cf | 2017-07-20 08:37:50 -0400 | [diff] [blame] | 31 | SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height, |
| 32 | const SkRect* content) { |
| 33 | if (width <= 0 || height <= 0 || kClosed_State == fState) { |
| 34 | return nullptr; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 35 | } |
Hal Canary | fa486cf | 2017-07-20 08:37:50 -0400 | [diff] [blame] | 36 | if (kInPage_State == fState) { |
| 37 | this->endPage(); |
| 38 | } |
| 39 | SkASSERT(kBetweenPages_State == fState); |
| 40 | fState = kInPage_State; |
| 41 | return trim(this->onBeginPage(width, height), width, height, content); |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void SkDocument::endPage() { |
| 45 | if (kInPage_State == fState) { |
| 46 | fState = kBetweenPages_State; |
| 47 | this->onEndPage(); |
| 48 | } |
| 49 | } |
| 50 | |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 51 | void SkDocument::close() { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 52 | for (;;) { |
| 53 | switch (fState) { |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 54 | case kBetweenPages_State: { |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 55 | fState = kClosed_State; |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 56 | this->onClose(fStream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | // we don't own the stream, but we mark it nullptr since we can |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 58 | // no longer write to it. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 59 | fStream = nullptr; |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 60 | return; |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 61 | } |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 62 | case kInPage_State: |
| 63 | this->endPage(); |
| 64 | break; |
| 65 | case kClosed_State: |
reed | d14df7c | 2016-09-22 14:12:46 -0700 | [diff] [blame] | 66 | return; |
reed@google.com | 99ac02b | 2013-06-07 20:30:16 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | } |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 70 | |
| 71 | void SkDocument::abort() { |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 72 | this->onAbort(); |
| 73 | |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 74 | fState = kClosed_State; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | // we don't own the stream, but we mark it nullptr since we can |
robertphillips@google.com | 701b405 | 2013-11-18 16:26:25 +0000 | [diff] [blame] | 76 | // no longer write to it. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | fStream = nullptr; |
commit-bot@chromium.org | b5a6651 | 2013-10-09 21:09:00 +0000 | [diff] [blame] | 78 | } |