blob: 30c94d8317e1f3090115221a50eb51de49832eb7 [file] [log] [blame]
reed@google.com99ac02b2013-06-07 20:30:16 +00001/*
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 Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkCanvas.h"
9#include "include/core/SkDocument.h"
10#include "include/core/SkStream.h"
reed@google.com99ac02b2013-06-07 20:30:16 +000011
Hal Canaryc5980d02018-01-08 15:02:36 -050012SkDocument::SkDocument(SkWStream* stream) : fStream(stream), fState(kBetweenPages_State) {}
reed@google.com99ac02b2013-06-07 20:30:16 +000013
14SkDocument::~SkDocument() {
15 this->close();
16}
17
Hal Canaryfa486cf2017-07-20 08:37:50 -040018static 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})) {
halcanary96fcdcc2015-08-27 07:41:13 -070023 return nullptr;
reed@google.com99ac02b2013-06-07 20:30:16 +000024 }
Hal Canaryfa486cf2017-07-20 08:37:50 -040025 canvas->clipRect(inner);
26 canvas->translate(inner.x(), inner.y());
reed@google.com99ac02b2013-06-07 20:30:16 +000027 }
Hal Canaryfa486cf2017-07-20 08:37:50 -040028 return canvas;
29}
reed@google.com99ac02b2013-06-07 20:30:16 +000030
Hal Canaryfa486cf2017-07-20 08:37:50 -040031SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
32 const SkRect* content) {
33 if (width <= 0 || height <= 0 || kClosed_State == fState) {
34 return nullptr;
reed@google.com99ac02b2013-06-07 20:30:16 +000035 }
Hal Canaryfa486cf2017-07-20 08:37:50 -040036 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.com99ac02b2013-06-07 20:30:16 +000042}
43
44void SkDocument::endPage() {
45 if (kInPage_State == fState) {
46 fState = kBetweenPages_State;
47 this->onEndPage();
48 }
49}
50
reedd14df7c2016-09-22 14:12:46 -070051void SkDocument::close() {
reed@google.com99ac02b2013-06-07 20:30:16 +000052 for (;;) {
53 switch (fState) {
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000054 case kBetweenPages_State: {
reed@google.com99ac02b2013-06-07 20:30:16 +000055 fState = kClosed_State;
reedd14df7c2016-09-22 14:12:46 -070056 this->onClose(fStream);
halcanary96fcdcc2015-08-27 07:41:13 -070057 // we don't own the stream, but we mark it nullptr since we can
reed@google.com99ac02b2013-06-07 20:30:16 +000058 // no longer write to it.
halcanary96fcdcc2015-08-27 07:41:13 -070059 fStream = nullptr;
reedd14df7c2016-09-22 14:12:46 -070060 return;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000061 }
reed@google.com99ac02b2013-06-07 20:30:16 +000062 case kInPage_State:
63 this->endPage();
64 break;
65 case kClosed_State:
reedd14df7c2016-09-22 14:12:46 -070066 return;
reed@google.com99ac02b2013-06-07 20:30:16 +000067 }
68 }
69}
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000070
71void SkDocument::abort() {
robertphillips@google.com701b4052013-11-18 16:26:25 +000072 this->onAbort();
73
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000074 fState = kClosed_State;
halcanary96fcdcc2015-08-27 07:41:13 -070075 // we don't own the stream, but we mark it nullptr since we can
robertphillips@google.com701b4052013-11-18 16:26:25 +000076 // no longer write to it.
halcanary96fcdcc2015-08-27 07:41:13 -070077 fStream = nullptr;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000078}