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