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