• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "SkCanvas.h"
9 #include "SkDocument.h"
10 #include "SkStream.h"
11 
SkDocument(SkWStream * stream,void (* doneProc)(SkWStream *,bool))12 SkDocument::SkDocument(SkWStream* stream, void (*doneProc)(SkWStream*, bool)) {
13     fStream = stream;   // we do not own this object.
14     fDoneProc = doneProc;
15     fState = kBetweenPages_State;
16 }
17 
~SkDocument()18 SkDocument::~SkDocument() {
19     this->close();
20 }
21 
beginPage(SkScalar width,SkScalar height,const SkRect * content)22 SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height,
23                                 const SkRect* content) {
24     if (width <= 0 || height <= 0) {
25         return nullptr;
26     }
27 
28     SkRect outer = SkRect::MakeWH(width, height);
29     SkRect inner;
30     if (content) {
31         inner = *content;
32         if (!inner.intersect(outer)) {
33             return nullptr;
34         }
35     } else {
36         inner = outer;
37     }
38 
39     for (;;) {
40         switch (fState) {
41             case kBetweenPages_State: {
42                 fState = kInPage_State;
43                 SkCanvas* canvas = this->onBeginPage(width, height);
44                 if (content) {
45                     canvas->clipRect(inner);
46                     canvas->translate(inner.x(), inner.y());
47                 }
48                 return canvas;
49             }
50             case kInPage_State:
51                 this->endPage();
52                 break;
53             case kClosed_State:
54                 return nullptr;
55         }
56     }
57     SkDEBUGFAIL("never get here");
58     return nullptr;
59 }
60 
endPage()61 void SkDocument::endPage() {
62     if (kInPage_State == fState) {
63         fState = kBetweenPages_State;
64         this->onEndPage();
65     }
66 }
67 
close()68 void SkDocument::close() {
69     for (;;) {
70         switch (fState) {
71             case kBetweenPages_State: {
72                 fState = kClosed_State;
73                 this->onClose(fStream);
74 
75                 if (fDoneProc) {
76                     fDoneProc(fStream, false);
77                 }
78                 // we don't own the stream, but we mark it nullptr since we can
79                 // no longer write to it.
80                 fStream = nullptr;
81                 return;
82             }
83             case kInPage_State:
84                 this->endPage();
85                 break;
86             case kClosed_State:
87                 return;
88         }
89     }
90 }
91 
abort()92 void SkDocument::abort() {
93     this->onAbort();
94 
95     fState = kClosed_State;
96     if (fDoneProc) {
97         fDoneProc(fStream, true);
98     }
99     // we don't own the stream, but we mark it nullptr since we can
100     // no longer write to it.
101     fStream = nullptr;
102 }
103