[SVGDom] Add rx/ry support for <rect>

R=stephana@google.com,robertphillips@google.com
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2234863002

Review-Url: https://codereview.chromium.org/2234863002
This commit is contained in:
fmalita 2016-08-10 17:11:29 -07:00 committed by Commit bot
parent d71fe83ac2
commit 286a8657da
5 changed files with 39 additions and 1 deletions

View File

@ -17,6 +17,8 @@ enum class SkSVGAttribute {
kD,
kFill,
kHeight,
kRx,
kRy,
kStroke,
kTransform,
kViewBox,

View File

@ -163,6 +163,8 @@ SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
{ "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
{ "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }},
{ "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
{ "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }},
{ "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
{ "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
{ "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
{ "transform", { SkSVGAttribute::kTransform, SetTransformAttribute }},

View File

@ -29,6 +29,14 @@ void SkSVGRect::setHeight(const SkSVGLength& h) {
fHeight = h;
}
void SkSVGRect::setRx(const SkSVGLength& rx) {
fRx = rx;
}
void SkSVGRect::setRy(const SkSVGLength& ry) {
fRy = ry;
}
void SkSVGRect::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
switch (attr) {
case SkSVGAttribute::kX:
@ -51,6 +59,16 @@ void SkSVGRect::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
this->setHeight(*h);
}
break;
case SkSVGAttribute::kRx:
if (const auto* rx = v.as<SkSVGLengthValue>()) {
this->setRx(*rx);
}
break;
case SkSVGAttribute::kRy:
if (const auto* ry = v.as<SkSVGLengthValue>()) {
this->setRy(*ry);
}
break;
default:
this->INHERITED::onSetAttribute(attr, v);
}
@ -58,5 +76,13 @@ void SkSVGRect::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
void SkSVGRect::onDraw(SkCanvas* canvas, const SkSVGLengthContext& lctx,
const SkPaint& paint) const {
canvas->drawRect(lctx.resolveRect(fX, fY, fWidth, fHeight), paint);
const SkRect rect = lctx.resolveRect(fX, fY, fWidth, fHeight);
const SkScalar rx = lctx.resolve(fRx, SkSVGLengthContext::LengthType::kHorizontal);
const SkScalar ry = lctx.resolve(fRy, SkSVGLengthContext::LengthType::kVertical);
if (rx || ry) {
canvas->drawRRect(SkRRect::MakeRectXY(rect, rx, ry), paint);
} else {
canvas->drawRect(rect, paint);
}
}

View File

@ -20,6 +20,8 @@ public:
void setY(const SkSVGLength&);
void setWidth(const SkSVGLength&);
void setHeight(const SkSVGLength&);
void setRx(const SkSVGLength&);
void setRy(const SkSVGLength&);
protected:
void onSetAttribute(SkSVGAttribute, const SkSVGValue&) override;
@ -34,6 +36,11 @@ private:
SkSVGLength fWidth = SkSVGLength(0);
SkSVGLength fHeight = SkSVGLength(0);
// The x radius for rounded rects.
SkSVGLength fRx = SkSVGLength(0);
// The y radius for rounded rects.
SkSVGLength fRy = SkSVGLength(0);
typedef SkSVGShape INHERITED;
};

View File

@ -11,6 +11,7 @@
SkSVGShape::SkSVGShape(SkSVGTag t) : INHERITED(t) {}
void SkSVGShape::onRender(const SkSVGRenderContext& ctx) const {
// TODO: this approach forces duplicate geometry resolution in onDraw(); refactor to avoid.
if (const SkPaint* fillPaint = ctx.presentationContext().fillPaint()) {
this->onDraw(ctx.canvas(), ctx.lengthContext(), *fillPaint);
}