blob: 5b2237d18a661312ba2d620e835bae26ed7fc3e6 [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
8#include "SkDocument.h"
9#include "SkStream.h"
10
robertphillips@google.com701b4052013-11-18 16:26:25 +000011SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) {
reed@google.com99ac02b2013-06-07 20:30:16 +000012 fStream = stream; // we do not own this object.
13 fDoneProc = doneProc;
14 fState = kBetweenPages_State;
15}
16
17SkDocument::~SkDocument() {
18 this->close();
19}
20
21SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
22 const SkRect* content) {
23 if (width <= 0 || height <= 0) {
24 return NULL;
25 }
26
27 SkRect outer = SkRect::MakeWH(width, height);
28 SkRect inner;
29 if (content) {
30 inner = *content;
31 if (!inner.intersect(outer)) {
32 return NULL;
33 }
34 } else {
35 inner = outer;
36 }
37
38 for (;;) {
39 switch (fState) {
40 case kBetweenPages_State:
41 fState = kInPage_State;
42 return this->onBeginPage(width, height, inner);
43 case kInPage_State:
44 this->endPage();
45 break;
46 case kClosed_State:
47 return NULL;
48 }
49 }
mtklein@google.com330313a2013-08-22 15:37:26 +000050 SkDEBUGFAIL("never get here");
reed@google.com99ac02b2013-06-07 20:30:16 +000051 return NULL;
52}
53
54void SkDocument::endPage() {
55 if (kInPage_State == fState) {
56 fState = kBetweenPages_State;
57 this->onEndPage();
58 }
59}
60
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000061bool SkDocument::close() {
reed@google.com99ac02b2013-06-07 20:30:16 +000062 for (;;) {
63 switch (fState) {
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000064 case kBetweenPages_State: {
reed@google.com99ac02b2013-06-07 20:30:16 +000065 fState = kClosed_State;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000066 bool success = this->onClose(fStream);
reed@google.com99ac02b2013-06-07 20:30:16 +000067
68 if (fDoneProc) {
robertphillips@google.com701b4052013-11-18 16:26:25 +000069 fDoneProc(fStream, false);
reed@google.com99ac02b2013-06-07 20:30:16 +000070 }
71 // we don't own the stream, but we mark it NULL since we can
72 // no longer write to it.
73 fStream = NULL;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000074 return success;
75 }
reed@google.com99ac02b2013-06-07 20:30:16 +000076 case kInPage_State:
77 this->endPage();
78 break;
79 case kClosed_State:
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000080 return false;
reed@google.com99ac02b2013-06-07 20:30:16 +000081 }
82 }
83}
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000084
85void SkDocument::abort() {
robertphillips@google.com701b4052013-11-18 16:26:25 +000086 this->onAbort();
87
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000088 fState = kClosed_State;
robertphillips@google.com701b4052013-11-18 16:26:25 +000089 if (fDoneProc) {
90 fDoneProc(fStream, true);
91 }
92 // we don't own the stream, but we mark it NULL since we can
93 // no longer write to it.
94 fStream = NULL;
commit-bot@chromium.orgb5a66512013-10-09 21:09:00 +000095}