[SVG] Implement currentColor for fill and stroke

The 'color' presentation attribute when set determines the value of the
special color value 'currentColor'. See the color-prop-01-b test for an
example.

Not handled in this CL:
- The color type needs to be changed to be inheritable.
- currentColor should also be usable for stop-color for gradients. See
  <https://www.w3.org/TR/SVG11/color.html#ColorProperty> for a full list
  of attributes that can use currentColor.

Change-Id: Icf81b5313cda688d1b6e20809b9b339f517b9ada
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/282638
Commit-Queue: Tyler Denniston <tdenniston@google.com>
Reviewed-by: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Tyler Denniston 2020-04-09 14:14:10 -04:00 committed by Skia Commit-Bot
parent 4d932d170f
commit 6c8314efa1
6 changed files with 27 additions and 2 deletions

View File

@ -26,5 +26,7 @@ SkSVGPresentationAttributes SkSVGPresentationAttributes::MakeInitial() {
result.fVisibility.set(SkSVGVisibility(SkSVGVisibility::Type::kVisible));
result.fColor.set(SkSVGColorType(SK_ColorBLACK));
return result;
}

View File

@ -16,6 +16,7 @@ class SkSVGRenderContext;
enum class SkSVGAttribute {
kClipPath,
kClipRule,
kColor,
kCx, // <circle>, <ellipse>, <radialGradient>: center x position
kCy, // <circle>, <ellipse>, <radialGradient>: center y position
kD,
@ -84,6 +85,8 @@ struct SkSVGPresentationAttributes {
SkTLazy<SkSVGVisibility> fVisibility;
SkTLazy<SkSVGColorType> fColor;
// uninherited
SkTLazy<SkSVGNumberType> fOpacity;
SkTLazy<SkSVGClip> fClipPath;

View File

@ -316,6 +316,7 @@ struct AttrParseInfo {
SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
{ "clip-path" , { SkSVGAttribute::kClipPath , SetClipPathAttribute }},
{ "clip-rule" , { SkSVGAttribute::kClipRule , SetFillRuleAttribute }},
{ "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
{ "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
{ "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
{ "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},

View File

@ -79,6 +79,11 @@ void SkSVGNode::setClipRule(const SkSVGFillRule& clipRule) {
SetInheritedByDefault(fPresentationAttributes.fClipRule, clipRule);
}
void SkSVGNode::setColor(const SkSVGColorType& color) {
// TODO: Color should be inherited by default
fPresentationAttributes.fColor.set(color);
}
void SkSVGNode::setFill(const SkSVGPaint& svgPaint) {
SetInheritedByDefault(fPresentationAttributes.fFill, svgPaint);
}
@ -146,6 +151,11 @@ void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
this->setClipRule(*clipRule);
}
break;
case SkSVGAttribute::kColor:
if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
this->setColor(*color);
}
break;
case SkSVGAttribute::kFill:
if (const SkSVGPaintValue* paint = v.as<SkSVGPaintValue>()) {
this->setFill(*paint);

View File

@ -54,6 +54,7 @@ public:
void setClipPath(const SkSVGClip&);
void setClipRule(const SkSVGFillRule&);
void setColor(const SkSVGColorType&);
void setFill(const SkSVGPaint&);
void setFillOpacity(const SkSVGNumberType&);
void setFillRule(const SkSVGFillRule&);

View File

@ -116,8 +116,8 @@ void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, Sk
break;
}
case SkSVGPaint::Type::kCurrentColor:
SkDebugf("unimplemented 'currentColor' paint type");
// Fall through.
p->setColor(*ctx.presentationContext().fInherited.fColor);
break;
case SkSVGPaint::Type::kNone:
// Fall through.
case SkSVGPaint::Type::kInherit:
@ -267,6 +267,13 @@ void commitToPaint<SkSVGAttribute::kVisibility>(const SkSVGPresentationAttribute
// Not part of the SkPaint state; queried to veto rendering.
}
template <>
void commitToPaint<SkSVGAttribute::kColor>(const SkSVGPresentationAttributes&,
const SkSVGRenderContext&,
SkSVGPresentationContext*) {
// Not part of the SkPaint state; applied via 'currentColor' color value
}
} // anonymous ns
SkSVGPresentationContext::SkSVGPresentationContext()
@ -354,6 +361,7 @@ void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr
ApplyLazyInheritedAttribute(StrokeOpacity);
ApplyLazyInheritedAttribute(StrokeWidth);
ApplyLazyInheritedAttribute(Visibility);
ApplyLazyInheritedAttribute(Color);
#undef ApplyLazyInheritedAttribute