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