1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkCanvas.h"
#include "SkSVGRenderContext.h"
#include "SkSVGTypes.h"
namespace {
SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::LengthType t) {
switch (t) {
case SkSVGLengthContext::LengthType::kHorizontal:
return viewport.width();
case SkSVGLengthContext::LengthType::kVertical:
return viewport.height();
case SkSVGLengthContext::LengthType::kOther:
return SkScalarSqrt(viewport.width() * viewport.height());
}
SkASSERT(false); // Not reached.
return 0;
}
} // anonymous ns
SkScalar SkSVGLengthContext::resolve(const SkSVGLength& l, LengthType t) const {
switch (l.unit()) {
case SkSVGLength::Unit::kNumber:
return l.value();
break;
case SkSVGLength::Unit::kPercentage:
return l.value() * length_size_for_type(fViewport, t) / 100;
break;
default:
SkDebugf("unsupported unit type: <%d>\n", l.unit());
break;
}
return 0;
}
SkRect SkSVGLengthContext::resolveRect(const SkSVGLength& x, const SkSVGLength& y,
const SkSVGLength& w, const SkSVGLength& h) const {
return SkRect::MakeXYWH(
this->resolve(x, SkSVGLengthContext::LengthType::kHorizontal),
this->resolve(y, SkSVGLengthContext::LengthType::kVertical),
this->resolve(w, SkSVGLengthContext::LengthType::kHorizontal),
this->resolve(h, SkSVGLengthContext::LengthType::kVertical));
}
SkSVGPresentationContext::SkSVGPresentationContext() {}
SkSVGPresentationContext::SkSVGPresentationContext(const SkSVGPresentationContext& o) {
this->initFrom(o);
}
SkSVGPresentationContext& SkSVGPresentationContext::operator=(const SkSVGPresentationContext& o) {
this->initFrom(o);
return *this;
}
void SkSVGPresentationContext::initFrom(const SkSVGPresentationContext& other) {
if (other.fFill.isValid()) {
fFill.set(*other.fFill.get());
} else {
fFill.reset();
}
if (other.fStroke.isValid()) {
fStroke.set(*other.fStroke.get());
} else {
fStroke.reset();
}
}
SkPaint& SkSVGPresentationContext::ensureFill() {
if (!fFill.isValid()) {
fFill.init();
fFill.get()->setStyle(SkPaint::kFill_Style);
fFill.get()->setAntiAlias(true);
}
return *fFill.get();
}
SkPaint& SkSVGPresentationContext::ensureStroke() {
if (!fStroke.isValid()) {
fStroke.init();
fStroke.get()->setStyle(SkPaint::kStroke_Style);
fStroke.get()->setAntiAlias(true);
}
return *fStroke.get();
}
void SkSVGPresentationContext::setFillColor(SkColor color) {
this->ensureFill().setColor(color);
}
void SkSVGPresentationContext::setStrokeColor(SkColor color) {
this->ensureStroke().setColor(color);
}
SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas,
const SkSVGLengthContext& lctx,
const SkSVGPresentationContext& pctx)
: fLengthContext(lctx)
, fPresentationContext(pctx)
, fCanvas(canvas)
, fCanvasSaveCount(canvas->getSaveCount()) {}
SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other)
: SkSVGRenderContext(other.canvas(),
other.lengthContext(),
other.presentationContext()) {}
SkSVGRenderContext::~SkSVGRenderContext() {
fCanvas->restoreToCount(fCanvasSaveCount);
}
|