/src/skia/src/core/SkDocument.cpp
Line | Count | Source (jump to first uncovered line) |
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 "include/core/SkCanvas.h" |
9 | | #include "include/core/SkDocument.h" |
10 | | #include "include/core/SkStream.h" |
11 | | |
12 | 0 | SkDocument::SkDocument(SkWStream* stream) : fStream(stream), fState(kBetweenPages_State) {} |
13 | | |
14 | 0 | SkDocument::~SkDocument() { |
15 | 0 | this->close(); |
16 | 0 | } |
17 | | |
18 | | static SkCanvas* trim(SkCanvas* canvas, SkScalar width, SkScalar height, |
19 | 0 | const SkRect* content) { |
20 | 0 | if (content && canvas) { |
21 | 0 | SkRect inner = *content; |
22 | 0 | if (!inner.intersect({0, 0, width, height})) { |
23 | 0 | return nullptr; |
24 | 0 | } |
25 | 0 | canvas->clipRect(inner); |
26 | 0 | canvas->translate(inner.x(), inner.y()); |
27 | 0 | } |
28 | 0 | return canvas; |
29 | 0 | } |
30 | | |
31 | | SkCanvas* SkDocument::beginPage(SkScalar width, SkScalar height, |
32 | 0 | const SkRect* content) { |
33 | 0 | if (width <= 0 || height <= 0 || kClosed_State == fState) { |
34 | 0 | return nullptr; |
35 | 0 | } |
36 | 0 | if (kInPage_State == fState) { |
37 | 0 | this->endPage(); |
38 | 0 | } |
39 | 0 | SkASSERT(kBetweenPages_State == fState); |
40 | 0 | fState = kInPage_State; |
41 | 0 | return trim(this->onBeginPage(width, height), width, height, content); |
42 | 0 | } |
43 | | |
44 | 0 | void SkDocument::endPage() { |
45 | 0 | if (kInPage_State == fState) { |
46 | 0 | fState = kBetweenPages_State; |
47 | 0 | this->onEndPage(); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | 0 | void SkDocument::close() { |
52 | 0 | for (;;) { |
53 | 0 | switch (fState) { |
54 | 0 | case kBetweenPages_State: { |
55 | 0 | fState = kClosed_State; |
56 | 0 | this->onClose(fStream); |
57 | | // we don't own the stream, but we mark it nullptr since we can |
58 | | // no longer write to it. |
59 | 0 | fStream = nullptr; |
60 | 0 | return; |
61 | 0 | } |
62 | 0 | case kInPage_State: |
63 | 0 | this->endPage(); |
64 | 0 | break; |
65 | 0 | case kClosed_State: |
66 | 0 | return; |
67 | 0 | } |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | 0 | void SkDocument::abort() { |
72 | 0 | this->onAbort(); |
73 | |
|
74 | 0 | fState = kClosed_State; |
75 | | // we don't own the stream, but we mark it nullptr since we can |
76 | | // no longer write to it. |
77 | 0 | fStream = nullptr; |
78 | 0 | } |