| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2016 Google Inc. | 2 * Copyright 2016 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCanvas.h" | 8 #include "SkCanvas.h" |
| 9 #include "SkDOM.h" | 9 #include "SkDOM.h" |
| 10 #include "SkParse.h" | 10 #include "SkParse.h" |
| 11 #include "SkParsePath.h" | 11 #include "SkParsePath.h" |
| 12 #include "SkString.h" | 12 #include "SkString.h" |
| 13 #include "SkSVGAttributeParser.h" |
| 13 #include "SkSVGDOM.h" | 14 #include "SkSVGDOM.h" |
| 14 #include "SkSVGG.h" | 15 #include "SkSVGG.h" |
| 15 #include "SkSVGNode.h" | 16 #include "SkSVGNode.h" |
| 16 #include "SkSVGPath.h" | 17 #include "SkSVGPath.h" |
| 18 #include "SkSVGRect.h" |
| 19 #include "SkSVGRenderContext.h" |
| 17 #include "SkSVGSVG.h" | 20 #include "SkSVGSVG.h" |
| 21 #include "SkSVGTypes.h" |
| 18 #include "SkSVGValue.h" | 22 #include "SkSVGValue.h" |
| 19 #include "SkTSearch.h" | 23 #include "SkTSearch.h" |
| 20 | 24 |
| 21 namespace { | 25 namespace { |
| 22 | 26 |
| 23 SkColor ParseColor(const char* str) { | |
| 24 // FIXME: real parser | |
| 25 if (*str++ != '#') { | |
| 26 return SK_ColorBLACK; | |
| 27 } | |
| 28 | |
| 29 uint32_t v; | |
| 30 const char* consumed = SkParse::FindHex(str, &v); | |
| 31 | |
| 32 switch(consumed - str) { | |
| 33 case 6: | |
| 34 // matched '#xxxxxx' | |
| 35 break; | |
| 36 case 3: | |
| 37 // matched '#xxx; | |
| 38 v = ((v << 12) & 0x00f00000) | | |
| 39 ((v << 8) & 0x000ff000) | | |
| 40 ((v << 4) & 0x00000ff0) | | |
| 41 ((v << 0) & 0x0000000f); | |
| 42 break; | |
| 43 default: | |
| 44 // failed | |
| 45 v = 0; | |
| 46 break; | |
| 47 } | |
| 48 | |
| 49 return v | 0xff000000; | |
| 50 } | |
| 51 | |
| 52 const char* ParseScalarPair(const char* str, SkScalar v[2]) { | 27 const char* ParseScalarPair(const char* str, SkScalar v[2]) { |
| 53 str = SkParse::FindScalar(str, v); | 28 str = SkParse::FindScalar(str, v); |
| 54 if (str) { | 29 if (str) { |
| 55 const char* second = SkParse::FindScalar(str, v + 1); | 30 const char* second = SkParse::FindScalar(str, v + 1); |
| 56 if (!second) { | 31 if (!second) { |
| 57 v[1] = v[0]; | 32 v[1] = v[0]; |
| 58 } else { | 33 } else { |
| 59 str = second; | 34 str = second; |
| 60 } | 35 } |
| 61 } | 36 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 91 if (str) { | 66 if (str) { |
| 92 m.setRotate(value); | 67 m.setRotate(value); |
| 93 } | 68 } |
| 94 } | 69 } |
| 95 | 70 |
| 96 return m; | 71 return m; |
| 97 } | 72 } |
| 98 | 73 |
| 99 bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, | 74 bool SetPaintAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, |
| 100 const char* stringValue) { | 75 const char* stringValue) { |
| 101 node->setAttribute(attr, SkSVGColorValue(ParseColor(stringValue))); | 76 SkSVGColor color; |
| 77 SkSVGAttributeParser parser(stringValue); |
| 78 if (!parser.parseColor(&color)) { |
| 79 return false; |
| 80 } |
| 81 |
| 82 node->setAttribute(attr, SkSVGColorValue(color)); |
| 102 return true; | 83 return true; |
| 103 } | 84 } |
| 104 | 85 |
| 105 bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, | 86 bool SetPathDataAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, |
| 106 const char* stringValue) { | 87 const char* stringValue) { |
| 107 SkPath path; | 88 SkPath path; |
| 108 if (!SkParsePath::FromSVGString(stringValue, &path)) { | 89 if (!SkParsePath::FromSVGString(stringValue, &path)) { |
| 109 return false; | 90 return false; |
| 110 } | 91 } |
| 111 | 92 |
| 112 node->setAttribute(attr, SkSVGPathValue(path)); | 93 node->setAttribute(attr, SkSVGPathValue(path)); |
| 113 return true; | 94 return true; |
| 114 } | 95 } |
| 115 | 96 |
| 116 bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, | 97 bool SetTransformAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, |
| 117 const char* stringValue) { | 98 const char* stringValue) { |
| 118 node->setAttribute(attr, SkSVGTransformValue(ParseTransform(stringValue))); | 99 node->setAttribute(attr, SkSVGTransformValue(ParseTransform(stringValue))); |
| 119 return true; | 100 return true; |
| 120 } | 101 } |
| 121 | 102 |
| 103 bool SetLengthAttribute(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, |
| 104 const char* stringValue) { |
| 105 SkSVGLength length; |
| 106 SkSVGAttributeParser parser(stringValue); |
| 107 if (!parser.parseLength(&length)) { |
| 108 return false; |
| 109 } |
| 110 |
| 111 node->setAttribute(attr, SkSVGLengthValue(length)); |
| 112 return true; |
| 113 } |
| 114 |
| 122 // Breaks a "foo: bar; baz: ..." string into key:value pairs. | 115 // Breaks a "foo: bar; baz: ..." string into key:value pairs. |
| 123 class StyleIterator { | 116 class StyleIterator { |
| 124 public: | 117 public: |
| 125 StyleIterator(const char* str) : fPos(str) { } | 118 StyleIterator(const char* str) : fPos(str) { } |
| 126 | 119 |
| 127 std::tuple<SkString, SkString> next() { | 120 std::tuple<SkString, SkString> next() { |
| 128 SkString name, value; | 121 SkString name, value; |
| 129 | 122 |
| 130 if (fPos) { | 123 if (fPos) { |
| 131 const char* sep = this->nextSeparator(); | 124 const char* sep = this->nextSeparator(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 const char* fKey; | 171 const char* fKey; |
| 179 const T fValue; | 172 const T fValue; |
| 180 }; | 173 }; |
| 181 | 174 |
| 182 struct AttrParseInfo { | 175 struct AttrParseInfo { |
| 183 SkSVGAttribute fAttr; | 176 SkSVGAttribute fAttr; |
| 184 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const cha
r* stringValue); | 177 bool (*fSetter)(const sk_sp<SkSVGNode>& node, SkSVGAttribute attr, const cha
r* stringValue); |
| 185 }; | 178 }; |
| 186 | 179 |
| 187 SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = { | 180 SortedDictionaryEntry<AttrParseInfo> gAttributeParseInfo[] = { |
| 188 { "d", { SkSVGAttribute::kD, SetPathDataAttribute }}, | 181 { "d" , { SkSVGAttribute::kD , SetPathDataAttribute }}, |
| 189 { "fill", { SkSVGAttribute::kFill, SetPaintAttribute }}, | 182 { "fill" , { SkSVGAttribute::kFill , SetPaintAttribute }}, |
| 190 { "stroke", { SkSVGAttribute::kStroke, SetPaintAttribute }}, | 183 { "height" , { SkSVGAttribute::kHeight , SetLengthAttribute }}, |
| 191 { "style", { SkSVGAttribute::kUnknown, SetStyleAttributes }}, | 184 { "stroke" , { SkSVGAttribute::kStroke , SetPaintAttribute }}, |
| 185 { "style" , { SkSVGAttribute::kUnknown , SetStyleAttributes }}, |
| 192 { "transform", { SkSVGAttribute::kTransform, SetTransformAttribute }}, | 186 { "transform", { SkSVGAttribute::kTransform, SetTransformAttribute }}, |
| 187 { "width" , { SkSVGAttribute::kWidth , SetLengthAttribute }}, |
| 188 { "x" , { SkSVGAttribute::kX , SetLengthAttribute }}, |
| 189 { "y" , { SkSVGAttribute::kY , SetLengthAttribute }}, |
| 193 }; | 190 }; |
| 194 | 191 |
| 195 SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = { | 192 SortedDictionaryEntry<sk_sp<SkSVGNode>(*)()> gTagFactories[] = { |
| 196 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }}, | 193 { "g" , []() -> sk_sp<SkSVGNode> { return SkSVGG::Make(); }}, |
| 197 { "path", []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }}, | 194 { "path", []() -> sk_sp<SkSVGNode> { return SkSVGPath::Make(); }}, |
| 195 { "rect", []() -> sk_sp<SkSVGNode> { return SkSVGRect::Make(); }}, |
| 198 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }}, | 196 { "svg" , []() -> sk_sp<SkSVGNode> { return SkSVGSVG::Make(); }}, |
| 199 }; | 197 }; |
| 200 | 198 |
| 201 struct ConstructionContext { | 199 struct ConstructionContext { |
| 202 ConstructionContext() : fParent(nullptr) { } | 200 ConstructionContext() : fParent(nullptr) { } |
| 203 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>
& newParent) | 201 ConstructionContext(const ConstructionContext& other, const sk_sp<SkSVGNode>
& newParent) |
| 204 : fParent(newParent.get()) { } | 202 : fParent(newParent.get()) { } |
| 205 | 203 |
| 206 const SkSVGNode* fParent; | 204 const SkSVGNode* fParent; |
| 207 }; | 205 }; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 SkDOM xmlDom; | 285 SkDOM xmlDom; |
| 288 if (!xmlDom.build(svgStream)) { | 286 if (!xmlDom.build(svgStream)) { |
| 289 return nullptr; | 287 return nullptr; |
| 290 } | 288 } |
| 291 | 289 |
| 292 return MakeFromDOM(xmlDom, containerSize); | 290 return MakeFromDOM(xmlDom, containerSize); |
| 293 } | 291 } |
| 294 | 292 |
| 295 void SkSVGDOM::render(SkCanvas* canvas) const { | 293 void SkSVGDOM::render(SkCanvas* canvas) const { |
| 296 if (fRoot) { | 294 if (fRoot) { |
| 297 fRoot->render(canvas); | 295 SkSVGRenderContext ctx(fContainerSize); |
| 296 fRoot->render(canvas, ctx); |
| 298 } | 297 } |
| 299 } | 298 } |
| 300 | 299 |
| 301 void SkSVGDOM::setContainerSize(const SkSize& containerSize) { | 300 void SkSVGDOM::setContainerSize(const SkSize& containerSize) { |
| 302 // TODO: inval | 301 // TODO: inval |
| 303 fContainerSize = containerSize; | 302 fContainerSize = containerSize; |
| 304 } | 303 } |
| OLD | NEW |