xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGPath.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/SkSVGPath.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/utils/SkParsePath.h"
12 #include "modules/svg/include/SkSVGAttribute.h"
13 #include "modules/svg/include/SkSVGAttributeParser.h"
14 #include "modules/svg/include/SkSVGRenderContext.h"
15 #include "modules/svg/include/SkSVGTypes.h"
16 
17 class SkPaint;
18 enum class SkPathFillType;
19 
SkSVGPath()20 SkSVGPath::SkSVGPath() : INHERITED(SkSVGTag::kPath) { }
21 
parseAndSetAttribute(const char * n,const char * v)22 bool SkSVGPath::parseAndSetAttribute(const char* n, const char* v) {
23     return INHERITED::parseAndSetAttribute(n, v) ||
24            this->setPath(SkSVGAttributeParser::parse<SkPath>("d", n, v));
25 }
26 
27 template <>
parse(SkPath * path)28 bool SkSVGAttributeParser::parse<SkPath>(SkPath* path) {
29     return SkParsePath::FromSVGString(fCurPos, path);
30 }
31 
onDraw(SkCanvas * canvas,const SkSVGLengthContext &,const SkPaint & paint,SkPathFillType fillType) const32 void SkSVGPath::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     SkPath path = fPath;  // Note: point and verb data are CoW
36     path.setFillType(fillType);
37     canvas->drawPath(path, paint);
38 }
39 
onAsPath(const SkSVGRenderContext & ctx) const40 SkPath SkSVGPath::onAsPath(const SkSVGRenderContext& ctx) const {
41     SkPath path = fPath;
42     // clip-rule can be inherited and needs to be applied at clip time.
43     path.setFillType(ctx.presentationContext().fInherited.fClipRule->asFillType());
44     this->mapToParent(&path);
45     return path;
46 }
47 
onObjectBoundingBox(const SkSVGRenderContext & ctx) const48 SkRect SkSVGPath::onObjectBoundingBox(const SkSVGRenderContext& ctx) const {
49     return fPath.computeTightBounds();
50 }
51