From 6fb0648c35aead138e65133c031d2b4fe5c8f0c7 Mon Sep 17 00:00:00 2001 From: fmalita Date: Mon, 15 Aug 2016 12:45:11 -0700 Subject: [SVGDom] Add opacity support Group opacity support. Unlike the other presentation attributes we support thus far, group opacity is not inherited. R=robertphillips@google.com,stephana@google.com GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2246943002 Review-Url: https://codereview.chromium.org/2246943002 --- experimental/svg/model/SkSVGAttribute.h | 3 +++ experimental/svg/model/SkSVGDOM.cpp | 1 + experimental/svg/model/SkSVGNode.cpp | 10 ++++++++++ experimental/svg/model/SkSVGNode.h | 1 + experimental/svg/model/SkSVGRenderContext.cpp | 9 +++++++++ 5 files changed, 24 insertions(+) (limited to 'experimental/svg') diff --git a/experimental/svg/model/SkSVGAttribute.h b/experimental/svg/model/SkSVGAttribute.h index d2f0cf2600..cc3e7a073c 100644 --- a/experimental/svg/model/SkSVGAttribute.h +++ b/experimental/svg/model/SkSVGAttribute.h @@ -18,6 +18,7 @@ enum class SkSVGAttribute { kFill, kFillOpacity, kHeight, + kOpacity, kPoints, kRx, kRy, @@ -48,6 +49,8 @@ struct SkSVGPresentationAttributes { SkTLazy fStrokeLineJoin; SkTLazy fStrokeOpacity; SkTLazy fStrokeWidth; + + SkTLazy fOpacity; }; #endif // SkSVGAttribute_DEFINED diff --git a/experimental/svg/model/SkSVGDOM.cpp b/experimental/svg/model/SkSVGDOM.cpp index c9d9e93f65..8e2667ad20 100644 --- a/experimental/svg/model/SkSVGDOM.cpp +++ b/experimental/svg/model/SkSVGDOM.cpp @@ -217,6 +217,7 @@ SortedDictionaryEntry gAttributeParseInfo[] = { { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }}, { "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }}, { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }}, + { "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }}, { "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }}, { "rx" , { SkSVGAttribute::kRx , SetLengthAttribute }}, { "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }}, diff --git a/experimental/svg/model/SkSVGNode.cpp b/experimental/svg/model/SkSVGNode.cpp index a039b59ff1..5a73ace548 100644 --- a/experimental/svg/model/SkSVGNode.cpp +++ b/experimental/svg/model/SkSVGNode.cpp @@ -42,6 +42,11 @@ void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) { SkSVGNumberType(SkTPin(opacity.value(), 0, 1))); } +void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) { + fPresentationAttributes.fOpacity.set( + SkSVGNumberType(SkTPin(opacity.value(), 0, 1))); +} + void SkSVGNode::setStroke(const SkSVGPaint& svgPaint) { fPresentationAttributes.fStroke.set(svgPaint); } @@ -67,6 +72,11 @@ void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) { this->setFillOpacity(*opacity); } break; + case SkSVGAttribute::kOpacity: + if (const SkSVGNumberValue* opacity = v.as()) { + this->setOpacity(*opacity); + } + break; case SkSVGAttribute::kStroke: if (const SkSVGPaintValue* paint = v.as()) { this->setStroke(*paint); diff --git a/experimental/svg/model/SkSVGNode.h b/experimental/svg/model/SkSVGNode.h index 8ffc6f9769..0f95d77a6f 100644 --- a/experimental/svg/model/SkSVGNode.h +++ b/experimental/svg/model/SkSVGNode.h @@ -39,6 +39,7 @@ public: void setFill(const SkSVGPaint&); void setFillOpacity(const SkSVGNumberType&); + void setOpacity(const SkSVGNumberType&); void setStroke(const SkSVGPaint&); void setStrokeOpacity(const SkSVGNumberType&); void setStrokeWidth(const SkSVGLength&); diff --git a/experimental/svg/model/SkSVGRenderContext.cpp b/experimental/svg/model/SkSVGRenderContext.cpp index ee92c47d54..8ad881429a 100644 --- a/experimental/svg/model/SkSVGRenderContext.cpp +++ b/experimental/svg/model/SkSVGRenderContext.cpp @@ -224,6 +224,15 @@ void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr ApplyLazyInheritedAttribute(StrokeWidth); #undef ApplyLazyInheritedAttribute + + // Uninherited attributes. Only apply to the current context. + + if (auto* opacity = attrs.fOpacity.getMaybeNull()) { + SkPaint opacityPaint; + opacityPaint.setAlpha(static_cast(opacity->value() * 255)); + // Balanced in the destructor, via restoreToCount(). + fCanvas->saveLayer(nullptr, &opacityPaint); + } } const SkPaint* SkSVGRenderContext::fillPaint() const { -- cgit v1.2.3