Skip to content

Commit 4c6f57a

Browse files
tdennistonSkia Commit-Bot
authored andcommitted
[svg] Use new code path for rest of presentation attrs
This is another necessary step in order to remove kInherit from all of the base SVG type enums. Change-Id: I2185e744f7b27369f7bad36591f896d3a9982b42 Reviewed-on: https://skia-review.googlesource.com/c/skia/+/335817 Commit-Queue: Tyler Denniston <[email protected]> Reviewed-by: Florin Malita <[email protected]>
1 parent 70fe17e commit 4c6f57a

File tree

8 files changed

+59
-148
lines changed

8 files changed

+59
-148
lines changed

modules/svg/include/SkSVGAttributeParser.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class SkSVGAttributeParser : public SkNoncopyable {
1616
public:
1717
SkSVGAttributeParser(const char[]);
1818

19-
bool parseColor(SkSVGColorType*);
20-
bool parseNumber(SkSVGNumberType*);
2119
bool parseInteger(SkSVGIntegerType*);
2220
bool parseViewBox(SkSVGViewBoxType*);
2321
bool parsePoints(SkSVGPointsType*);
@@ -26,7 +24,6 @@ class SkSVGAttributeParser : public SkNoncopyable {
2624

2725
// TODO: Migrate all parse*() functions to this style (and delete the old version)
2826
// so they can be used by parse<T>():
29-
bool parse(SkSVGNumberType* v) { return parseNumber(v); }
3027
bool parse(SkSVGIntegerType* v) { return parseInteger(v); }
3128

3229
template <typename T> using ParseResult = SkTLazy<T>;

modules/svg/include/SkSVGNode.h

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -94,32 +94,31 @@ class SkSVGNode : public SkRefCnt {
9494
// TODO: consolidate with existing setAttribute
9595
virtual bool parseAndSetAttribute(const char* name, const char* value);
9696

97-
void setColor(const SkSVGColorType&);
98-
void setFillOpacity(const SkSVGNumberType&);
99-
void setOpacity(const SkSVGNumberType&);
100-
void setStrokeDashOffset(const SkSVGLength&);
101-
void setStrokeOpacity(const SkSVGNumberType&);
102-
void setStrokeMiterLimit(const SkSVGNumberType&);
103-
void setStrokeWidth(const SkSVGLength&);
104-
10597
// inherited
106-
SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
107-
SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
108-
SVG_PRES_ATTR(Fill , SkSVGPaint , true)
109-
SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
110-
SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
111-
SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
112-
SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
113-
SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
114-
SVG_PRES_ATTR(StrokeDashArray, SkSVGDashArray , true)
115-
SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
116-
SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
117-
SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
118-
SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
98+
SVG_PRES_ATTR(ClipRule , SkSVGFillRule , true)
99+
SVG_PRES_ATTR(Color , SkSVGColorType , true)
100+
SVG_PRES_ATTR(FillRule , SkSVGFillRule , true)
101+
SVG_PRES_ATTR(Fill , SkSVGPaint , true)
102+
SVG_PRES_ATTR(FillOpacity , SkSVGNumberType, true)
103+
SVG_PRES_ATTR(FontFamily , SkSVGFontFamily, true)
104+
SVG_PRES_ATTR(FontSize , SkSVGFontSize , true)
105+
SVG_PRES_ATTR(FontStyle , SkSVGFontStyle , true)
106+
SVG_PRES_ATTR(FontWeight , SkSVGFontWeight, true)
107+
SVG_PRES_ATTR(Stroke , SkSVGPaint , true)
108+
SVG_PRES_ATTR(StrokeDashArray , SkSVGDashArray , true)
109+
SVG_PRES_ATTR(StrokeDashOffset, SkSVGLength , true)
110+
SVG_PRES_ATTR(StrokeLineCap , SkSVGLineCap , true)
111+
SVG_PRES_ATTR(StrokeLineJoin , SkSVGLineJoin , true)
112+
SVG_PRES_ATTR(StrokeMiterLimit, SkSVGNumberType, true)
113+
SVG_PRES_ATTR(StrokeOpacity , SkSVGNumberType, true)
114+
SVG_PRES_ATTR(StrokeWidth , SkSVGLength , true)
115+
SVG_PRES_ATTR(TextAnchor , SkSVGTextAnchor, true)
116+
SVG_PRES_ATTR(Visibility , SkSVGVisibility, true)
119117

120118
// not inherited
121-
SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
122-
SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
119+
SVG_PRES_ATTR(ClipPath , SkSVGClip , false)
120+
SVG_PRES_ATTR(Filter , SkSVGFilterType, false)
121+
SVG_PRES_ATTR(Opacity , SkSVGNumberType, false)
123122

124123
protected:
125124
SkSVGNode(SkSVGTag);
@@ -138,7 +137,7 @@ class SkSVGNode : public SkRefCnt {
138137

139138
virtual SkPath onAsPath(const SkSVGRenderContext&) const = 0;
140139

141-
virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&);
140+
virtual void onSetAttribute(SkSVGAttribute, const SkSVGValue&) {}
142141

143142
virtual bool hasChildren() const { return false; }
144143

modules/svg/src/SkSVGAttributeParser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ bool SkSVGAttributeParser::parseRGBColorToken(SkColor* c) {
224224
// https://www.w3.org/TR/SVG11/types.html#DataTypeColor
225225
// And https://www.w3.org/TR/CSS2/syndata.html#color-units for the alternative
226226
// forms supported by SVG (e.g. RGB percentages).
227-
bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) {
227+
template <>
228+
bool SkSVGAttributeParser::parse(SkSVGColorType* color) {
228229
SkColor c;
229230

230231
// consume preceding whitespace
@@ -286,7 +287,8 @@ bool SkSVGAttributeParser::parse(SkSVGStringType* result) {
286287
}
287288

288289
// https://www.w3.org/TR/SVG11/types.html#DataTypeNumber
289-
bool SkSVGAttributeParser::parseNumber(SkSVGNumberType* number) {
290+
template <>
291+
bool SkSVGAttributeParser::parse(SkSVGNumberType* number) {
290292
// consume WS
291293
this->parseWSToken();
292294

@@ -506,7 +508,7 @@ bool SkSVGAttributeParser::parse(SkSVGPaint* paint) {
506508
SkSVGColorType c;
507509
SkSVGStringType iri;
508510
bool parsedValue = false;
509-
if (this->parseColor(&c)) {
511+
if (this->parse(&c)) {
510512
*paint = SkSVGPaint(c);
511513
parsedValue = true;
512514
} else if (this->parseExpectedStringToken("none")) {
@@ -599,7 +601,7 @@ bool SkSVGAttributeParser::parse(SkSVGLineJoin* join) {
599601
bool SkSVGAttributeParser::parseStopColor(SkSVGStopColor* stopColor) {
600602
SkSVGColorType c;
601603
bool parsedValue = false;
602-
if (this->parseColor(&c)) {
604+
if (this->parse(&c)) {
603605
*stopColor = SkSVGStopColor(c);
604606
parsedValue = true;
605607
} else if (this->parseExpectedStringToken("currentColor")) {

modules/svg/src/SkSVGDOM.cpp

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@
4141

4242
namespace {
4343

44-
bool SetColorAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
45-
const char* stringValue) {
46-
SkSVGColorType color;
47-
SkSVGAttributeParser parser(stringValue);
48-
if (!parser.parseColor(&color)) {
49-
return false;
50-
}
51-
52-
node->setAttribute(attr, SkSVGColorValue(color));
53-
return true;
54-
}
55-
5644
bool SetIRIAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
5745
const char* stringValue) {
5846
auto parseResult = SkSVGAttributeParser::parse<SkSVGIRI>(stringValue);
@@ -107,13 +95,12 @@ bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
10795

10896
bool SetNumberAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr,
10997
const char* stringValue) {
110-
SkSVGNumberType number;
111-
SkSVGAttributeParser parser(stringValue);
112-
if (!parser.parseNumber(&number)) {
98+
auto parseResult = SkSVGAttributeParser::parse<SkSVGNumberType>(stringValue);
99+
if (!parseResult.isValid()) {
113100
return false;
114101
}
115102

116-
node->setAttribute(attr, SkSVGNumberValue(number));
103+
node->setAttribute(attr, SkSVGNumberValue(*parseResult));
117104
return true;
118105
}
119106

@@ -255,19 +242,16 @@ struct AttrParseInfo {
255242
};
256243

257244
SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
258-
{ "color" , { SkSVGAttribute::kColor , SetColorAttribute }},
259245
{ "cx" , { SkSVGAttribute::kCx , SetLengthAttribute }},
260246
{ "cy" , { SkSVGAttribute::kCy , SetLengthAttribute }},
261247
{ "d" , { SkSVGAttribute::kD , SetPathDataAttribute }},
262-
{ "fill-opacity" , { SkSVGAttribute::kFillOpacity , SetNumberAttribute }},
263248
{ "filterUnits" , { SkSVGAttribute::kFilterUnits ,
264249
SetObjectBoundingBoxUnitsAttribute }},
265250
// focal point x & y
266251
{ "fx" , { SkSVGAttribute::kFx , SetLengthAttribute }},
267252
{ "fy" , { SkSVGAttribute::kFy , SetLengthAttribute }},
268253
{ "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }},
269254
{ "offset" , { SkSVGAttribute::kOffset , SetLengthAttribute }},
270-
{ "opacity" , { SkSVGAttribute::kOpacity , SetNumberAttribute }},
271255
{ "patternTransform" , { SkSVGAttribute::kPatternTransform , SetTransformAttribute }},
272256
{ "points" , { SkSVGAttribute::kPoints , SetPointsAttribute }},
273257
{ "preserveAspectRatio", { SkSVGAttribute::kPreserveAspectRatio,
@@ -277,10 +261,6 @@ SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = {
277261
{ "ry" , { SkSVGAttribute::kRy , SetLengthAttribute }},
278262
{ "stop-color" , { SkSVGAttribute::kStopColor , SetStopColorAttribute }},
279263
{ "stop-opacity" , { SkSVGAttribute::kStopOpacity , SetNumberAttribute }},
280-
{ "stroke-dashoffset" , { SkSVGAttribute::kStrokeDashOffset , SetLengthAttribute }},
281-
{ "stroke-miterlimit" , { SkSVGAttribute::kStrokeMiterLimit , SetNumberAttribute }},
282-
{ "stroke-opacity" , { SkSVGAttribute::kStrokeOpacity , SetNumberAttribute }},
283-
{ "stroke-width" , { SkSVGAttribute::kStrokeWidth , SetLengthAttribute }},
284264
{ "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }},
285265
{ "text" , { SkSVGAttribute::kText , SetStringAttribute }},
286266
{ "transform" , { SkSVGAttribute::kTransform , SetTransformAttribute }},

modules/svg/src/SkSVGFeColorMatrix.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ sk_sp<SkImageFilter> SkSVGFeColorMatrix::onMakeImageFilter(const SkSVGRenderCont
9898

9999
template <> bool SkSVGAttributeParser::parse(SkSVGFeColorMatrixValues* values) {
100100
SkSVGNumberType value;
101-
if (!this->parseNumber(&value)) {
101+
if (!this->parse(&value)) {
102102
return false;
103103
}
104104

105105
values->push_back(value);
106106
while (true) {
107-
if (!this->parseNumber(&value) || values->count() >= 20) {
107+
if (!this->parse(&value) || values->count() >= 20) {
108108
break;
109109
}
110110
values->push_back(value);

modules/svg/src/SkSVGFeTurbulence.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ template <>
2828
bool SkSVGAttributeParser::parse<SkSVGFeTurbulenceBaseFrequency>(
2929
SkSVGFeTurbulenceBaseFrequency* freq) {
3030
SkSVGNumberType freqX;
31-
if (!this->parseNumber(&freqX)) {
31+
if (!this->parse(&freqX)) {
3232
return false;
3333
}
3434

3535
SkSVGNumberType freqY;
3636
this->parseCommaWspToken();
37-
if (this->parseNumber(&freqY)) {
37+
if (this->parse(&freqY)) {
3838
*freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqY);
3939
} else {
4040
*freq = SkSVGFeTurbulenceBaseFrequency(freqX, freqX);

modules/svg/src/SkSVGNode.cpp

Lines changed: 22 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -76,101 +76,34 @@ void SetInheritedByDefault(SkTLazy<T>& presentation_attribute, const T& value) {
7676
}
7777
}
7878

79-
void SkSVGNode::setColor(const SkSVGColorType& color) {
80-
// TODO: Color should be inherited by default
81-
fPresentationAttributes.fColor.set(color);
82-
}
83-
84-
void SkSVGNode::setFillOpacity(const SkSVGNumberType& opacity) {
85-
fPresentationAttributes.fFillOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
86-
}
87-
88-
void SkSVGNode::setOpacity(const SkSVGNumberType& opacity) {
89-
fPresentationAttributes.fOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
90-
}
91-
92-
void SkSVGNode::setStrokeDashOffset(const SkSVGLength& dashOffset) {
93-
fPresentationAttributes.fStrokeDashOffset.set(dashOffset);
94-
}
95-
96-
void SkSVGNode::setStrokeOpacity(const SkSVGNumberType& opacity) {
97-
fPresentationAttributes.fStrokeOpacity.set(SkSVGNumberType(SkTPin<SkScalar>(opacity, 0, 1)));
98-
}
99-
100-
void SkSVGNode::setStrokeMiterLimit(const SkSVGNumberType& ml) {
101-
fPresentationAttributes.fStrokeMiterLimit.set(ml);
102-
}
103-
104-
void SkSVGNode::setStrokeWidth(const SkSVGLength& strokeWidth) {
105-
fPresentationAttributes.fStrokeWidth.set(strokeWidth);
106-
}
107-
108-
void SkSVGNode::onSetAttribute(SkSVGAttribute attr, const SkSVGValue& v) {
109-
switch (attr) {
110-
case SkSVGAttribute::kColor:
111-
if (const SkSVGColorValue* color = v.as<SkSVGColorValue>()) {
112-
this->setColor(*color);
113-
}
114-
break;
115-
case SkSVGAttribute::kFillOpacity:
116-
if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
117-
this->setFillOpacity(*opacity);
118-
}
119-
break;
120-
case SkSVGAttribute::kOpacity:
121-
if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
122-
this->setOpacity(*opacity);
123-
}
124-
break;
125-
case SkSVGAttribute::kStrokeDashOffset:
126-
if (const SkSVGLengthValue* dashOffset= v.as<SkSVGLengthValue>()) {
127-
this->setStrokeDashOffset(*dashOffset);
128-
}
129-
break;
130-
case SkSVGAttribute::kStrokeOpacity:
131-
if (const SkSVGNumberValue* opacity = v.as<SkSVGNumberValue>()) {
132-
this->setStrokeOpacity(*opacity);
133-
}
134-
break;
135-
case SkSVGAttribute::kStrokeMiterLimit:
136-
if (const SkSVGNumberValue* miterLimit = v.as<SkSVGNumberValue>()) {
137-
this->setStrokeMiterLimit(*miterLimit);
138-
}
139-
break;
140-
case SkSVGAttribute::kStrokeWidth:
141-
if (const SkSVGLengthValue* strokeWidth = v.as<SkSVGLengthValue>()) {
142-
this->setStrokeWidth(*strokeWidth);
143-
}
144-
break;
145-
default:
146-
#if defined(SK_VERBOSE_SVG_PARSING)
147-
SkDebugf("attribute ID <%d> ignored for node <%d>\n", attr, fTag);
148-
#endif
149-
break;
150-
}
151-
}
152-
15379
bool SkSVGNode::parseAndSetAttribute(const char* n, const char* v) {
15480
#define PARSE_AND_SET(svgName, attrName) \
15581
this->set##attrName( \
15682
SkSVGAttributeParser::parseProperty<decltype(fPresentationAttributes.f##attrName)>( \
15783
svgName, n, v))
15884

159-
return PARSE_AND_SET( "clip-path" , ClipPath)
160-
|| PARSE_AND_SET("clip-rule" , ClipRule)
161-
|| PARSE_AND_SET("fill" , Fill)
162-
|| PARSE_AND_SET("fill-rule" , FillRule)
163-
|| PARSE_AND_SET("filter" , Filter)
164-
|| PARSE_AND_SET("font-family" , FontFamily)
165-
|| PARSE_AND_SET("font-size" , FontSize)
166-
|| PARSE_AND_SET("font-style" , FontStyle)
167-
|| PARSE_AND_SET("font-weight" , FontWeight)
168-
|| PARSE_AND_SET("stroke" , Stroke)
169-
|| PARSE_AND_SET("stroke-dasharray", StrokeDashArray)
170-
|| PARSE_AND_SET("stroke-linecap" , StrokeLineCap)
171-
|| PARSE_AND_SET("stroke-linejoin" , StrokeLineJoin)
172-
|| PARSE_AND_SET("text-anchor" , TextAnchor)
173-
|| PARSE_AND_SET("visibility" , Visibility);
85+
return PARSE_AND_SET( "clip-path" , ClipPath)
86+
|| PARSE_AND_SET("clip-rule" , ClipRule)
87+
|| PARSE_AND_SET("color" , Color)
88+
|| PARSE_AND_SET("fill" , Fill)
89+
|| PARSE_AND_SET("fill-opacity" , FillOpacity)
90+
|| PARSE_AND_SET("fill-rule" , FillRule)
91+
|| PARSE_AND_SET("filter" , Filter)
92+
|| PARSE_AND_SET("font-family" , FontFamily)
93+
|| PARSE_AND_SET("font-size" , FontSize)
94+
|| PARSE_AND_SET("font-style" , FontStyle)
95+
|| PARSE_AND_SET("font-weight" , FontWeight)
96+
|| PARSE_AND_SET("opacity" , Opacity)
97+
|| PARSE_AND_SET("stroke" , Stroke)
98+
|| PARSE_AND_SET("stroke-dasharray" , StrokeDashArray)
99+
|| PARSE_AND_SET("stroke-dashoffset", StrokeDashOffset)
100+
|| PARSE_AND_SET("stroke-linecap" , StrokeLineCap)
101+
|| PARSE_AND_SET("stroke-linejoin" , StrokeLineJoin)
102+
|| PARSE_AND_SET("stroke-miterlimit", StrokeMiterLimit)
103+
|| PARSE_AND_SET("stroke-opacity" , StrokeOpacity)
104+
|| PARSE_AND_SET("stroke-width" , StrokeWidth)
105+
|| PARSE_AND_SET("text-anchor" , TextAnchor)
106+
|| PARSE_AND_SET("visibility" , Visibility);
174107

175108
#undef PARSE_AND_SET
176109
}

modules/svg/src/SkSVGRenderContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, Sk
128128
}
129129

130130
inline uint8_t opacity_to_alpha(SkScalar o) {
131-
return SkTo<uint8_t>(SkScalarRoundToInt(o * 255));
131+
return SkTo<uint8_t>(SkScalarRoundToInt(SkTPin<SkScalar>(o, 0, 1) * 255));
132132
}
133133

134134
// Commit the selected attribute to the paint cache.

0 commit comments

Comments
 (0)