[SVGDom] Add 'stroke-dashoffset' support

https://www.w3.org/TR/SVG/painting.html#StrokeDashoffsetProperty
Change-Id: Ia25d0048a56ac3835cabcb4e1794d91667367d7c
Reviewed-on: https://skia-review.googlesource.com/59820
Reviewed-by: Robert Phillips <robertphillips@google.com>
Commit-Queue: Florin Malita <fmalita@chromium.org>
This commit is contained in:
Florin Malita 2017-10-13 18:18:32 -04:00 committed by Skia Commit-Bot
parent 7a0ebfc033
commit e1dadd74f8
6 changed files with 24 additions and 2 deletions

View File

@ -17,6 +17,7 @@ SkSVGPresentationAttributes SkSVGPresentationAttributes::MakeInitial() {
result.fStroke.set(SkSVGPaint(SkSVGPaint::Type::kNone));
result.fStrokeDashArray.set(SkSVGDashArray(SkSVGDashArray::Type::kNone));
result.fStrokeDashOffset.set(SkSVGLength(0));
result.fStrokeLineCap.set(SkSVGLineCap(SkSVGLineCap::Type::kButt));
result.fStrokeLineJoin.set(SkSVGLineJoin(SkSVGLineJoin::Type::kMiter));
result.fStrokeMiterLimit.set(SkSVGNumberType(4));

View File

@ -39,6 +39,7 @@ enum class SkSVGAttribute {
kStopOpacity,
kStroke,
kStrokeDashArray,
kStrokeDashOffset,
kStrokeOpacity,
kStrokeLineCap,
kStrokeLineJoin,
@ -70,6 +71,7 @@ struct SkSVGPresentationAttributes {
SkTLazy<SkSVGPaint> fStroke;
SkTLazy<SkSVGDashArray> fStrokeDashArray;
SkTLazy<SkSVGLength> fStrokeDashOffset;
SkTLazy<SkSVGLineCap> fStrokeLineCap;
SkTLazy<SkSVGLineJoin> fStrokeLineJoin;
SkTLazy<SkSVGNumberType> fStrokeMiterLimit;

View File

@ -329,6 +329,7 @@ SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
{ "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
{ "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }},
{ "stroke-dasharray" , { SkSVGAttribute::kStrokeDashArray , SetDashArrayAttribute }},
{ "stroke-dashoffset", { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
{ "stroke-linecap" , { SkSVGAttribute::kStrokeLineCap , SetLineCapAttribute }},
{ "stroke-linejoin" , { SkSVGAttribute::kStrokeLineJoin , SetLineJoinAttribute }},
{ "stroke-miterlimit", { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},

View File

@ -94,6 +94,10 @@ void SkSVGNode::setStrokeDashArray(const SkSVGDashArray& dashArray) {
fPresentationAttributes.fStrokeDashArray.set(dashArray);
}
void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) {
fPresentationAttributes.fStrokeDashOffset.set(dashOffset);
}
void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
fPresentationAttributes.fStrokeOpacity.set(
SkSVGNumberType(SkTPin<SkScalar>(opacity.value(), 0, 1)));
@ -149,6 +153,11 @@ void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
this->setStrokeDashArray(*dashArray);
}
break;
case SkSVGAttribute::kStrokeDashOffset:
if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) {
this->setStrokeDashOffset(*dashOffset);
}
break;
case SkSVGAttribute::kStrokeOpacity:
if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
this->setStrokeOpacity(*opacity);

View File

@ -59,6 +59,7 @@ public:
void setOpacity(const SkSVGNumberType&);
void setStroke(const SkSVGPaint&);
void setStrokeDashArray(const SkSVGDashArray&);
void setStrokeDashOffset(const SkSVGLength&);
void setStrokeOpacity(const SkSVGNumberType&);
void setStrokeWidth(const SkSVGLength&);
void setVisibility(const SkSVGVisibility&);

View File

@ -179,13 +179,20 @@ void commitToPaint<SkSVGAttribute::kStrokeDashArray>(const SkSVGPresentationAttr
SkASSERT((intervals.count() & 1) == 0);
// TODO: phase support
const SkScalar phase = 0;
const SkScalar phase = ctx.lengthContext().resolve(*pctx->fInherited.fStrokeDashOffset.get(),
SkSVGLengthContext::LengthType::kOther);
pctx->fStrokePaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(),
intervals.count(),
phase));
}
template <>
void commitToPaint<SkSVGAttribute::kStrokeDashOffset>(const SkSVGPresentationAttributes&,
const SkSVGRenderContext&,
SkSVGPresentationContext*) {
// Applied via kStrokeDashArray.
}
template <>
void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttributes& attrs,
const SkSVGRenderContext&,
@ -330,6 +337,7 @@ void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttr
ApplyLazyInheritedAttribute(FillRule);
ApplyLazyInheritedAttribute(ClipRule);
ApplyLazyInheritedAttribute(Stroke);
ApplyLazyInheritedAttribute(StrokeDashOffset);
ApplyLazyInheritedAttribute(StrokeDashArray);
ApplyLazyInheritedAttribute(StrokeLineCap);
ApplyLazyInheritedAttribute(StrokeLineJoin);