Home | History | Annotate | Download | only in model
      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 "SkCanvas.h"
      9 #include "SkSVGLine.h"
     10 #include "SkSVGRenderContext.h"
     11 #include "SkSVGValue.h"
     12 
     13 SkSVGLine::SkSVGLine() : INHERITED(SkSVGTag::kLine) {}
     14 
     15 void SkSVGLine::setX1(const SkSVGLength& x1) {
     16     fX1 = x1;
     17 }
     18 
     19 void SkSVGLine::setY1(const SkSVGLength& y1) {
     20     fY1 = y1;
     21 }
     22 
     23 void SkSVGLine::setX2(const SkSVGLength& x2) {
     24     fX2 = x2;
     25 }
     26 
     27 void SkSVGLine::setY2(const SkSVGLength& y2) {
     28     fY2 = y2;
     29 }
     30 
     31 void SkSVGLine::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
     32     switch (attr) {
     33     case SkSVGAttribute::kX1:
     34         if (const auto* x1 = v.as<SkSVGLengthValue>()) {
     35             this->setX1(*x1);
     36         }
     37         break;
     38     case SkSVGAttribute::kY1:
     39         if (const auto* y1 = v.as<SkSVGLengthValue>()) {
     40             this->setY1(*y1);
     41         }
     42         break;
     43     case SkSVGAttribute::kX2:
     44         if (const auto* x2 = v.as<SkSVGLengthValue>()) {
     45             this->setX2(*x2);
     46         }
     47         break;
     48     case SkSVGAttribute::kY2:
     49         if (const auto* y2 = v.as<SkSVGLengthValue>()) {
     50             this->setY2(*y2);
     51         }
     52         break;
     53     default:
     54         this->INHERITED::onSetAttribute(attr, v);
     55     }
     56 }
     57 
     58 std::tuple<SkPoint, SkPoint> SkSVGLine::resolve(const SkSVGLengthContext& lctx) const {
     59     return std::make_tuple(
     60         SkPoint::Make(lctx.resolve(fX1, SkSVGLengthContext::LengthType::kHorizontal),
     61                       lctx.resolve(fY1, SkSVGLengthContext::LengthType::kVertical)),
     62         SkPoint::Make(lctx.resolve(fX2, SkSVGLengthContext::LengthType::kHorizontal),
     63                       lctx.resolve(fY2, SkSVGLengthContext::LengthType::kVertical)));
     64 }
     65 
     66 void SkSVGLine::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
     67                        const SkPaint& paint, SkPath::FillType) const {
     68     SkPoint p0, p1;
     69     std::tie(p0, p1) = this->resolve(lctx);
     70 
     71     canvas->drawLine(p0, p1, paint);
     72 }
     73 
     74 SkPath SkSVGLine::onAsPath(const SkSVGRenderContext& ctx) const {
     75     SkPoint p0, p1;
     76     std::tie(p0, p1) = this->resolve(ctx.lengthContext());
     77 
     78     SkPath path;
     79     path.moveTo(p0);
     80     path.lineTo(p1);
     81     this->mapToParent(&path);
     82 
     83     return path;
     84 }
     85