michael@0: /* michael@0: * Copyright 2013 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkDocument.h" michael@0: #include "SkStream.h" michael@0: michael@0: SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) { michael@0: fStream = stream; // we do not own this object. michael@0: fDoneProc = doneProc; michael@0: fState = kBetweenPages_State; michael@0: } michael@0: michael@0: SkDocument::~SkDocument() { michael@0: this->close(); michael@0: } michael@0: michael@0: SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height, michael@0: const SkRect* content) { michael@0: if (width <= 0 || height <= 0) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkRect outer = SkRect::MakeWH(width, height); michael@0: SkRect inner; michael@0: if (content) { michael@0: inner = *content; michael@0: if (!inner.intersect(outer)) { michael@0: return NULL; michael@0: } michael@0: } else { michael@0: inner = outer; michael@0: } michael@0: michael@0: for (;;) { michael@0: switch (fState) { michael@0: case kBetweenPages_State: michael@0: fState = kInPage_State; michael@0: return this->onBeginPage(width, height, inner); michael@0: case kInPage_State: michael@0: this->endPage(); michael@0: break; michael@0: case kClosed_State: michael@0: return NULL; michael@0: } michael@0: } michael@0: SkDEBUGFAIL("never get here"); michael@0: return NULL; michael@0: } michael@0: michael@0: void SkDocument::endPage() { michael@0: if (kInPage_State == fState) { michael@0: fState = kBetweenPages_State; michael@0: this->onEndPage(); michael@0: } michael@0: } michael@0: michael@0: bool SkDocument::close() { michael@0: for (;;) { michael@0: switch (fState) { michael@0: case kBetweenPages_State: { michael@0: fState = kClosed_State; michael@0: bool success = this->onClose(fStream); michael@0: michael@0: if (fDoneProc) { michael@0: fDoneProc(fStream, false); michael@0: } michael@0: // we don't own the stream, but we mark it NULL since we can michael@0: // no longer write to it. michael@0: fStream = NULL; michael@0: return success; michael@0: } michael@0: case kInPage_State: michael@0: this->endPage(); michael@0: break; michael@0: case kClosed_State: michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkDocument::abort() { michael@0: this->onAbort(); michael@0: michael@0: fState = kClosed_State; michael@0: if (fDoneProc) { michael@0: fDoneProc(fStream, true); michael@0: } michael@0: // we don't own the stream, but we mark it NULL since we can michael@0: // no longer write to it. michael@0: fStream = NULL; michael@0: }