OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2006 The Android Open Source Project | |
3 * | |
4 * Use of this source code is governed by a BSD-style license that can be | |
5 * found in the LICENSE file. | |
6 */ | |
7 | |
8 | |
9 #include "SkSVGGradient.h" | |
10 #include "SkSVGParser.h" | |
11 #include "SkSVGStop.h" | |
12 | |
13 SkSVGGradient::SkSVGGradient() { | |
14 } | |
15 | |
16 SkSVGElement* SkSVGGradient::getGradient() { | |
17 return this; | |
18 } | |
19 | |
20 bool SkSVGGradient::isDef() { | |
21 return true; | |
22 } | |
23 | |
24 bool SkSVGGradient::isNotDef() { | |
25 return false; | |
26 } | |
27 | |
28 void SkSVGGradient::translate(SkSVGParser& parser, bool defState) { | |
29 INHERITED::translate(parser, defState); | |
30 // !!! no support for 'objectBoundingBox' yet | |
31 bool first = true; | |
32 bool addedFirst = false; | |
33 bool addedLast = false; | |
34 SkString offsets("["); | |
35 SkString* lastOffset = nullptr; | |
36 for (SkSVGElement** ptr = fChildren.begin(); ptr < fChildren.end(); ptr++) { | |
37 SkASSERT((*ptr)->getType() == SkSVGType_Stop); | |
38 SkSVGStop* stop = (SkSVGStop*) *ptr; | |
39 if (first && stop->f_offset.equals("0") == false) { | |
40 addedFirst = true; | |
41 offsets.append("0,"); | |
42 } | |
43 SkString* thisOffset = &stop->f_offset; | |
44 if (lastOffset && thisOffset->equals(*lastOffset)) { | |
45 if (thisOffset->equals("1")) { | |
46 offsets.remove(offsets.size() - 2, 2); | |
47 offsets.append(".999,"); | |
48 } else { | |
49 SkASSERT(0); // !!! need to write this case | |
50 } | |
51 } | |
52 offsets.append(*thisOffset); | |
53 if (ptr == fChildren.end() - 1) { // last | |
54 if (stop->f_offset.equals("1") == false) { | |
55 offsets.append(",1"); | |
56 addedLast = true; | |
57 } | |
58 } else | |
59 offsets.appendUnichar(','); | |
60 first = false; | |
61 lastOffset = thisOffset; | |
62 } | |
63 offsets.appendUnichar(']'); | |
64 parser._addAttribute("offsets", offsets); | |
65 if (addedFirst) | |
66 parser.translate(*fChildren.begin(), defState); | |
67 for (SkSVGElement** ptr = fChildren.begin(); ptr < fChildren.end(); ptr++) | |
68 parser.translate(*ptr, defState); | |
69 if (addedLast) | |
70 parser.translate(*(fChildren.end() - 1), defState); | |
71 } | |
72 | |
73 void SkSVGGradient::translateGradientUnits(SkString& units) { | |
74 // !!! no support for 'objectBoundingBox' yet | |
75 SkASSERT(strcmp(units.c_str(), "userSpaceOnUse") == 0); | |
76 } | |
77 | |
78 void SkSVGGradient::write(SkSVGParser& parser, SkString& baseColor) { | |
79 if (baseColor.c_str()[0] != '#') | |
80 return; | |
81 SkSVGPaint* saveHead = parser.fHead; | |
82 parser.fHead = &fPaintState; | |
83 parser.fSuppressPaint = true; | |
84 SkString originalID(f_id); | |
85 f_id.set("mask"); // write out gradient named given name + color (less initi
al #) | |
86 f_id.append(baseColor.c_str() + 1); | |
87 SkString originalColors; | |
88 for (SkSVGElement** ptr = fChildren.begin(); ptr < fChildren.end(); ptr++) { | |
89 SkSVGStop* colorElement = (SkSVGStop*) *ptr; | |
90 SkString& color = colorElement->fPaintState.f_stopColor; | |
91 originalColors.append(color); | |
92 originalColors.appendUnichar(','); | |
93 SkASSERT(color.c_str()[0] == '#'); | |
94 SkString replacement; | |
95 replacement.set("0x"); | |
96 replacement.append(color.c_str() + 1, 2); // add stop colors using given
color, turning existing stop color into alpha | |
97 SkASSERT(baseColor.c_str()[0] == '#'); | |
98 SkASSERT(baseColor.size() == 7); | |
99 replacement.append(baseColor.c_str() + 1); | |
100 color.set(replacement); | |
101 } | |
102 translate(parser, true); | |
103 const char* originalPtr = originalColors.c_str(); // restore original gradie
nt values | |
104 for (SkSVGElement** ptr = fChildren.begin(); ptr < fChildren.end(); ptr++) { | |
105 SkSVGStop* color = (SkSVGStop*) *ptr; | |
106 const char* originalEnd = strchr(originalPtr, ','); | |
107 color->fPaintState.f_stopColor.set(originalPtr, originalEnd - originalPt
r); | |
108 originalPtr = originalEnd + 1; | |
109 } | |
110 f_id.set(originalID); | |
111 parser.fSuppressPaint = false; | |
112 parser.fHead = saveHead; | |
113 } | |
OLD | NEW |