diff options
author | fmalita <fmalita@chromium.org> | 2016-09-12 17:06:47 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-09-12 17:06:47 -0700 |
commit | 28d5b72d86fdfae20dc47ba548748f119c7273e3 (patch) | |
tree | 579d10ae07a10a882ca68d00c2da6fa407c0c658 /experimental/svg/model/SkSVGAttributeParser.cpp | |
parent | 8c24f4fae3389b9937eb73128e76226cffebdd72 (diff) |
[SVGDom] Initial linear gradient support
Kind of a big change, to connect several new bits into something useful:
* ID tracking & lookup
* new asPaint() node virtual to support shader (and in the future filter) based paint servers
* <defs>, <linearGradient> and <stop> element support
* 'href', 'offset', 'stop-color', 'stop-opacity' attribute support
* IRI/FuncIRI and rgb(...) parsing
BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search?issue=2327233003
Review-Url: https://codereview.chromium.org/2327233003
Diffstat (limited to 'experimental/svg/model/SkSVGAttributeParser.cpp')
-rw-r--r-- | experimental/svg/model/SkSVGAttributeParser.cpp | 67 |
1 files changed, 65 insertions, 2 deletions
diff --git a/experimental/svg/model/SkSVGAttributeParser.cpp b/experimental/svg/model/SkSVGAttributeParser.cpp index f8bfa2033f..9d2d6b8136 100644 --- a/experimental/svg/model/SkSVGAttributeParser.cpp +++ b/experimental/svg/model/SkSVGAttributeParser.cpp @@ -113,6 +113,7 @@ bool SkSVGAttributeParser::parseLengthUnitToken(SkSVGLength::Unit* unit) { return false; } +// https://www.w3.org/TR/SVG/types.html#DataTypeColor bool SkSVGAttributeParser::parseNamedColorToken(SkColor* c) { if (const char* next = SkParse::FindNamedColor(fCurPos, strlen(fCurPos), c)) { fCurPos = next; @@ -148,7 +149,38 @@ bool SkSVGAttributeParser::parseHexColorToken(SkColor* c) { return true; } -// https://www.w3.org/TR/SVG/types.html#DataTypeColor +bool SkSVGAttributeParser::parseColorComponentToken(int32_t* c) { + fCurPos = SkParse::FindS32(fCurPos, c); + if (!fCurPos) { + return false; + } + + if (*fCurPos == '%') { + *c = SkScalarRoundToInt(*c * 255.0f / 100); + fCurPos++; + } + + return true; +} + +bool SkSVGAttributeParser::parseRGBColorToken(SkColor* c) { + return this->parseParenthesized("rgb", [this](SkColor* c) -> bool { + int32_t r, g, b; + if (this->parseColorComponentToken(&r) && + this->parseSepToken() && + this->parseColorComponentToken(&g) && + this->parseSepToken() && + this->parseColorComponentToken(&b)) { + + *c = SkColorSetRGB(static_cast<uint8_t>(r), + static_cast<uint8_t>(g), + static_cast<uint8_t>(b)); + return true; + } + return false; + }, c); +} + bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) { SkColor c; @@ -157,7 +189,9 @@ bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) { // TODO: rgb(...) bool parsedValue = false; - if (this->parseHexColorToken(&c) || this->parseNamedColorToken(&c)) { + if (this->parseHexColorToken(&c) + || this->parseNamedColorToken(&c) + || this->parseRGBColorToken(&c)) { *color = SkSVGColorType(c); parsedValue = true; @@ -168,6 +202,31 @@ bool SkSVGAttributeParser::parseColor(SkSVGColorType* color) { return parsedValue && this->parseEOSToken(); } +// https://www.w3.org/TR/SVG/linking.html#IRIReference +bool SkSVGAttributeParser::parseIRI(SkSVGStringType* iri) { + // consume preceding whitespace + this->parseWSToken(); + + // we only support local fragments + if (!this->parseExpectedStringToken("#")) { + return false; + } + const auto* start = fCurPos; + this->advanceWhile([](char c) -> bool { return !is_eos(c) && c != ')'; }); + if (start == fCurPos) { + return false; + } + *iri = SkString(start, fCurPos - start); + return true; +} + +// https://www.w3.org/TR/SVG/types.html#DataTypeFuncIRI +bool SkSVGAttributeParser::parseFuncIRI(SkSVGStringType* iri) { + return this->parseParenthesized("url", [this](SkSVGStringType* iri) -> bool { + return this->parseIRI(iri); + }, iri); +} + // https://www.w3.org/TR/SVG/types.html#DataTypeNumber bool SkSVGAttributeParser::parseNumber(SkSVGNumberType* number) { // consume WS @@ -363,6 +422,7 @@ bool SkSVGAttributeParser::parseTransform(SkSVGTransformType* t) { // https://www.w3.org/TR/SVG/painting.html#SpecifyingPaint bool SkSVGAttributeParser::parsePaint(SkSVGPaint* paint) { SkSVGColorType c; + SkSVGStringType iri; bool parsedValue = false; if (this->parseColor(&c)) { *paint = SkSVGPaint(c); @@ -376,6 +436,9 @@ bool SkSVGAttributeParser::parsePaint(SkSVGPaint* paint) { } else if (this->parseExpectedStringToken("inherit")) { *paint = SkSVGPaint(SkSVGPaint::Type::kInherit); parsedValue = true; + } else if (this->parseFuncIRI(&iri)) { + *paint = SkSVGPaint(iri.value()); + parsedValue = true; } return parsedValue && this->parseEOSToken(); } |