1 /*
2 * Copyright 2016 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 "modules/svg/include/SkSVGPoly.h"
10 #include "modules/svg/include/SkSVGRenderContext.h"
11 #include "modules/svg/include/SkSVGValue.h"
12 #include "src/core/SkTLazy.h"
13
SkSVGPoly(SkSVGTag t)14 SkSVGPoly::SkSVGPoly(SkSVGTag t) : INHERITED(t) {}
15
parseAndSetAttribute(const char * n,const char * v)16 bool SkSVGPoly::parseAndSetAttribute(const char* n, const char* v) {
17 if (INHERITED::parseAndSetAttribute(n, v)) {
18 return true;
19 }
20
21 if (this->setPoints(SkSVGAttributeParser::parse<SkSVGPointsType>("points", n, v))) {
22 // TODO: we can likely just keep the points array and create the SkPath when needed.
23 fPath = SkPath::Polygon(
24 fPoints.begin(), fPoints.count(),
25 this->tag() == SkSVGTag::kPolygon); // only polygons are auto-closed
26 }
27
28 // No other attributes on this node
29 return false;
30 }
31
onDraw(SkCanvas * canvas,const SkSVGLengthContext &,const SkPaint & paint,SkPathFillType fillType) const32 void SkSVGPoly::onDraw(SkCanvas* canvas, const SkSVGLengthContext&, const SkPaint& paint,
33 SkPathFillType fillType) const {
34 // the passed fillType follows inheritance rules and needs to be applied at draw time.
35 fPath.setFillType(fillType);
36 canvas->drawPath(fPath, paint);
37 }
38
onAsPath(const SkSVGRenderContext & ctx) const39 SkPath SkSVGPoly::onAsPath(const SkSVGRenderContext& ctx) const {
40 SkPath path = fPath;
41
42 // clip-rule can be inherited and needs to be applied at clip time.
43 path.setFillType(ctx.presentationContext().fInherited.fClipRule->asFillType());
44
45 this->mapToParent(&path);
46 return path;
47 }
48
onObjectBoundingBox(const SkSVGRenderContext & ctx) const49 SkRect SkSVGPoly::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
50 return fPath.getBounds();
51 }
52