gfx/skia/trunk/src/doc/SkDocument.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/doc/SkDocument.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,95 @@
     1.4 +/*
     1.5 + * Copyright 2013 Google Inc.
     1.6 + *
     1.7 + * Use of this source code is governed by a BSD-style license that can be
     1.8 + * found in the LICENSE file.
     1.9 + */
    1.10 +
    1.11 +#include "SkDocument.h"
    1.12 +#include "SkStream.h"
    1.13 +
    1.14 +SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) {
    1.15 +    fStream = stream;   // we do not own this object.
    1.16 +    fDoneProc = doneProc;
    1.17 +    fState = kBetweenPages_State;
    1.18 +}
    1.19 +
    1.20 +SkDocument::~SkDocument() {
    1.21 +    this->close();
    1.22 +}
    1.23 +
    1.24 +SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
    1.25 +                                const SkRect* content) {
    1.26 +    if (width <= 0 || height <= 0) {
    1.27 +        return NULL;
    1.28 +    }
    1.29 +
    1.30 +    SkRect outer = SkRect::MakeWH(width, height);
    1.31 +    SkRect inner;
    1.32 +    if (content) {
    1.33 +        inner = *content;
    1.34 +        if (!inner.intersect(outer)) {
    1.35 +            return NULL;
    1.36 +        }
    1.37 +    } else {
    1.38 +        inner = outer;
    1.39 +    }
    1.40 +
    1.41 +    for (;;) {
    1.42 +        switch (fState) {
    1.43 +            case kBetweenPages_State:
    1.44 +                fState = kInPage_State;
    1.45 +                return this->onBeginPage(width, height, inner);
    1.46 +            case kInPage_State:
    1.47 +                this->endPage();
    1.48 +                break;
    1.49 +            case kClosed_State:
    1.50 +                return NULL;
    1.51 +        }
    1.52 +    }
    1.53 +    SkDEBUGFAIL("never get here");
    1.54 +    return NULL;
    1.55 +}
    1.56 +
    1.57 +void SkDocument::endPage() {
    1.58 +    if (kInPage_State == fState) {
    1.59 +        fState = kBetweenPages_State;
    1.60 +        this->onEndPage();
    1.61 +    }
    1.62 +}
    1.63 +
    1.64 +bool SkDocument::close() {
    1.65 +    for (;;) {
    1.66 +        switch (fState) {
    1.67 +            case kBetweenPages_State: {
    1.68 +                fState = kClosed_State;
    1.69 +                bool success = this->onClose(fStream);
    1.70 +
    1.71 +                if (fDoneProc) {
    1.72 +                    fDoneProc(fStream, false);
    1.73 +                }
    1.74 +                // we don't own the stream, but we mark it NULL since we can
    1.75 +                // no longer write to it.
    1.76 +                fStream = NULL;
    1.77 +                return success;
    1.78 +            }
    1.79 +            case kInPage_State:
    1.80 +                this->endPage();
    1.81 +                break;
    1.82 +            case kClosed_State:
    1.83 +                return false;
    1.84 +        }
    1.85 +    }
    1.86 +}
    1.87 +
    1.88 +void SkDocument::abort() {
    1.89 +    this->onAbort();
    1.90 +
    1.91 +    fState = kClosed_State;
    1.92 +    if (fDoneProc) {
    1.93 +        fDoneProc(fStream, true);
    1.94 +    }
    1.95 +    // we don't own the stream, but we mark it NULL since we can
    1.96 +    // no longer write to it.
    1.97 +    fStream = NULL;
    1.98 +}

mercurial