xref: /aosp_15_r20/external/skia/modules/svg/src/SkSVGEllipse.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/SkSVGEllipse.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkRect.h"
12 #include "modules/svg/include/SkSVGAttributeParser.h"
13 #include "modules/svg/include/SkSVGRenderContext.h"
14 #include "modules/svg/include/SkSVGTypes.h"
15 #include "modules/svg/src/SkSVGRectPriv.h"
16 
17 class SkPaint;
18 enum class SkPathFillType;
19 
SkSVGEllipse()20 SkSVGEllipse::SkSVGEllipse() : INHERITED(SkSVGTag::kEllipse) {}
21 
parseAndSetAttribute(const char * n,const char * v)22 bool SkSVGEllipse::parseAndSetAttribute(const char* n, const char* v) {
23     return INHERITED::parseAndSetAttribute(n, v) ||
24            this->setCx(SkSVGAttributeParser::parse<SkSVGLength>("cx", n, v)) ||
25            this->setCy(SkSVGAttributeParser::parse<SkSVGLength>("cy", n, v)) ||
26            this->setRx(SkSVGAttributeParser::parse<SkSVGLength>("rx", n, v)) ||
27            this->setRy(SkSVGAttributeParser::parse<SkSVGLength>("ry", n, v));
28 }
29 
resolve(const SkSVGLengthContext & lctx) const30 SkRect SkSVGEllipse::resolve(const SkSVGLengthContext& lctx) const {
31     const auto cx = lctx.resolve(fCx, SkSVGLengthContext::LengthType::kHorizontal);
32     const auto cy = lctx.resolve(fCy, SkSVGLengthContext::LengthType::kVertical);
33 
34     // https://www.w3.org/TR/SVG2/shapes.html#EllipseElement
35     //
36     // An auto value for either rx or ry is converted to a used value, following the rules given
37     // above for rectangles (but without any clamping based on width or height).
38     const auto [ rx, ry ] = ResolveOptionalRadii(fRx, fRy, lctx);
39 
40     // A computed value of zero for either dimension, or a computed value of auto for both
41     // dimensions, disables rendering of the element.
42     return (rx > 0 && ry > 0)
43         ? SkRect::MakeXYWH(cx - rx, cy - ry, rx * 2, ry * 2)
44         : SkRect::MakeEmpty();
45 }
46 
onDraw(SkCanvas * canvas,const SkSVGLengthContext & lctx,const SkPaint & paint,SkPathFillType) const47 void SkSVGEllipse::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
48                           const SkPaint& paint, SkPathFillType) const {
49     canvas->drawOval(this->resolve(lctx), paint);
50 }
51 
onAsPath(const SkSVGRenderContext & ctx) const52 SkPath SkSVGEllipse::onAsPath(const SkSVGRenderContext& ctx) const {
53     SkPath path = SkPath::Oval(this->resolve(ctx.lengthContext()));
54     this->mapToParent(&path);
55 
56     return path;
57 }
58