199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com/*
299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Copyright 2013 Google Inc.
399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com *
499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * Use of this source code is governed by a BSD-style license that can be
599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com * found in the LICENSE file.
699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com */
799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#include "SkDocument.h"
999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com#include "SkStream.h"
1099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
11701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.comSkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) {
1299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    fStream = stream;   // we do not own this object.
1399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    fDoneProc = doneProc;
1499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    fState = kBetweenPages_State;
1599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
1699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
1799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comSkDocument::~SkDocument() {
1899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    this->close();
1999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
2099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
2199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comSkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
2299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                                const SkRect* content) {
2399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    if (width <= 0 || height <= 0) {
2496fcdcc219d2a0d3579719b84b28bede76efba64halcanary        return nullptr;
2599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
2699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
2799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    SkRect outer = SkRect::MakeWH(width, height);
2899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    SkRect inner;
2999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    if (content) {
3099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        inner = *content;
3199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        if (!inner.intersect(outer)) {
3296fcdcc219d2a0d3579719b84b28bede76efba64halcanary            return nullptr;
3399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        }
3499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    } else {
3599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        inner = outer;
3699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
3799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
3899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    for (;;) {
3999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        switch (fState) {
4099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com            case kBetweenPages_State:
4199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                fState = kInPage_State;
4299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                return this->onBeginPage(width, height, inner);
4399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com            case kInPage_State:
4499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                this->endPage();
4599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                break;
4699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com            case kClosed_State:
4796fcdcc219d2a0d3579719b84b28bede76efba64halcanary                return nullptr;
4899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        }
4999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
50330313a8a8343876ee596da39da06a5d69badd9cmtklein@google.com    SkDEBUGFAIL("never get here");
5196fcdcc219d2a0d3579719b84b28bede76efba64halcanary    return nullptr;
5299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
5399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
5499ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.comvoid SkDocument::endPage() {
5599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    if (kInPage_State == fState) {
5699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        fState = kBetweenPages_State;
5799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        this->onEndPage();
5899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
5999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
6099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
61d14df7c700bf77c8c42c791ca7a865c1bbc96679reedvoid SkDocument::close() {
6299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    for (;;) {
6399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        switch (fState) {
64b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org            case kBetweenPages_State: {
6599ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                fState = kClosed_State;
66d14df7c700bf77c8c42c791ca7a865c1bbc96679reed                this->onClose(fStream);
6799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com
6899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                if (fDoneProc) {
69701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com                    fDoneProc(fStream, false);
7099ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                }
7196fcdcc219d2a0d3579719b84b28bede76efba64halcanary                // we don't own the stream, but we mark it nullptr since we can
7299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                // no longer write to it.
7396fcdcc219d2a0d3579719b84b28bede76efba64halcanary                fStream = nullptr;
74d14df7c700bf77c8c42c791ca7a865c1bbc96679reed                return;
75b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org            }
7699ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com            case kInPage_State:
7799ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                this->endPage();
7899ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com                break;
7999ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com            case kClosed_State:
80d14df7c700bf77c8c42c791ca7a865c1bbc96679reed                return;
8199ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com        }
8299ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com    }
8399ac02bb701c1e30b20f2174aac25ffbe487c0afreed@google.com}
84b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org
85b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.orgvoid SkDocument::abort() {
86701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com    this->onAbort();
87701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com
88b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org    fState = kClosed_State;
89701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com    if (fDoneProc) {
90701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com        fDoneProc(fStream, true);
91701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com    }
9296fcdcc219d2a0d3579719b84b28bede76efba64halcanary    // we don't own the stream, but we mark it nullptr since we can
93701b40543d5d124dfa1e59b051cba9d2aaf61670robertphillips@google.com    // no longer write to it.
9496fcdcc219d2a0d3579719b84b28bede76efba64halcanary    fStream = nullptr;
95b5a6651f9f69570d964382134d64360915db9a29commit-bot@chromium.org}
96