xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGLine.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/SkSVGLine.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkPoint.h"
12 #include "modules/svg/include/SkSVGAttributeParser.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 
15 class SkPaint;
16 enum class SkPathFillType;
17 
SkSVGLine()18 SkSVGLine::SkSVGLine() : INHERITED(SkSVGTag::kLine) {}
19 
parseAndSetAttribute(const char * n,const char * v)20 bool SkSVGLine::parseAndSetAttribute(const char* n, const char* v) {
21     return INHERITED::parseAndSetAttribute(n, v) ||
22            this->setX1(SkSVGAttributeParser::parse<SkSVGLength>("x1", n, v)) ||
23            this->setY1(SkSVGAttributeParser::parse<SkSVGLength>("y1", n, v)) ||
24            this->setX2(SkSVGAttributeParser::parse<SkSVGLength>("x2", n, v)) ||
25            this->setY2(SkSVGAttributeParser::parse<SkSVGLength>("y2", n, v));
26 }
27 
resolve(const SkSVGLengthContext & lctx) const28 std::tuple<SkPoint, SkPoint> SkSVGLine::resolve(const SkSVGLengthContext& lctx) const {
29     return std::make_tuple(
30         SkPoint::Make(lctx.resolve(fX1, SkSVGLengthContext::LengthType::kHorizontal),
31                       lctx.resolve(fY1, SkSVGLengthContext::LengthType::kVertical)),
32         SkPoint::Make(lctx.resolve(fX2, SkSVGLengthContext::LengthType::kHorizontal),
33                       lctx.resolve(fY2, SkSVGLengthContext::LengthType::kVertical)));
34 }
35 
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPathFillType) const36 void SkSVGLine::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
37                        const SkPaint& paint, SkPathFillType) const {
38     SkPoint p0, p1;
39     std::tie(p0, p1) = this->resolve(lctx);
40 
41     canvas->drawLine(p0, p1, paint);
42 }
43 
onAsPath(const SkSVGRenderContext & ctx) const44 SkPath SkSVGLine::onAsPath(const SkSVGRenderContext& ctx) const {
45     SkPoint p0, p1;
46     std::tie(p0, p1) = this->resolve(ctx.lengthContext());
47 
48     SkPath path = SkPath::Line(p0, p1);
49     this->mapToParent(&path);
50 
51     return path;
52 }
53