OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #include "SkPDFShader.h" | 10 #include "SkPDFShader.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 between startOffset and endOffset from prevColor to curColor (for each color | 48 between startOffset and endOffset from prevColor to curColor (for each color |
49 component), leaving the result in component order on the stack. It assumes | 49 component), leaving the result in component order on the stack. It assumes |
50 there are always 3 components per color. | 50 there are always 3 components per color. |
51 @param range endOffset - startOffset | 51 @param range endOffset - startOffset |
52 @param curColor[components] The current color components. | 52 @param curColor[components] The current color components. |
53 @param prevColor[components] The previous color components. | 53 @param prevColor[components] The previous color components. |
54 @param result The result ps function. | 54 @param result The result ps function. |
55 */ | 55 */ |
56 static void interpolateColorCode(SkScalar range, SkScalar* curColor, | 56 static void interpolateColorCode(SkScalar range, SkScalar* curColor, |
57 SkScalar* prevColor, SkString* result) { | 57 SkScalar* prevColor, SkString* result) { |
| 58 SkASSERT(range != SkIntToScalar(0)); |
58 static const int kColorComponents = 3; | 59 static const int kColorComponents = 3; |
59 | 60 |
60 // Figure out how to scale each color component. | 61 // Figure out how to scale each color component. |
61 SkScalar multiplier[kColorComponents]; | 62 SkScalar multiplier[kColorComponents]; |
62 for (int i = 0; i < kColorComponents; i++) { | 63 for (int i = 0; i < kColorComponents; i++) { |
63 multiplier[i] = SkScalarDiv(curColor[i] - prevColor[i], range); | 64 multiplier[i] = SkScalarDiv(curColor[i] - prevColor[i], range); |
64 } | 65 } |
65 | 66 |
66 // Calculate when we no longer need to keep a copy of the input parameter t. | 67 // Calculate when we no longer need to keep a copy of the input parameter t. |
67 // If the last component to use t is i, then dupInput[0..i - 1] = true | 68 // If the last component to use t is i, then dupInput[0..i - 1] = true |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 // Clamp the initial color. | 146 // Clamp the initial color. |
146 result->append("dup 0 le {pop "); | 147 result->append("dup 0 le {pop "); |
147 result->appendScalar(colorData[0][0]); | 148 result->appendScalar(colorData[0][0]); |
148 result->append(" "); | 149 result->append(" "); |
149 result->appendScalar(colorData[0][1]); | 150 result->appendScalar(colorData[0][1]); |
150 result->append(" "); | 151 result->append(" "); |
151 result->appendScalar(colorData[0][2]); | 152 result->appendScalar(colorData[0][2]); |
152 result->append(" }\n"); | 153 result->append(" }\n"); |
153 | 154 |
154 // The gradient colors. | 155 // The gradient colors. |
| 156 int gradients = 0; |
155 for (int i = 1 ; i < info.fColorCount; i++) { | 157 for (int i = 1 ; i < info.fColorCount; i++) { |
| 158 if (info.fColorOffsets[i] == info.fColorOffsets[i - 1]) { |
| 159 continue; |
| 160 } |
| 161 gradients++; |
| 162 |
156 result->append("{dup "); | 163 result->append("{dup "); |
157 result->appendScalar(info.fColorOffsets[i]); | 164 result->appendScalar(info.fColorOffsets[i]); |
158 result->append(" le {"); | 165 result->append(" le {"); |
159 if (info.fColorOffsets[i - 1] != 0) { | 166 if (info.fColorOffsets[i - 1] != 0) { |
160 result->appendScalar(info.fColorOffsets[i - 1]); | 167 result->appendScalar(info.fColorOffsets[i - 1]); |
161 result->append(" sub\n"); | 168 result->append(" sub\n"); |
162 } | 169 } |
163 | 170 |
164 interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1], | 171 interpolateColorCode(info.fColorOffsets[i] - info.fColorOffsets[i - 1], |
165 colorData[i], colorData[i - 1], result); | 172 colorData[i], colorData[i - 1], result); |
166 result->append("}\n"); | 173 result->append("}\n"); |
167 } | 174 } |
168 | 175 |
169 // Clamp the final color. | 176 // Clamp the final color. |
170 result->append("{pop "); | 177 result->append("{pop "); |
171 result->appendScalar(colorData[info.fColorCount - 1][0]); | 178 result->appendScalar(colorData[info.fColorCount - 1][0]); |
172 result->append(" "); | 179 result->append(" "); |
173 result->appendScalar(colorData[info.fColorCount - 1][1]); | 180 result->appendScalar(colorData[info.fColorCount - 1][1]); |
174 result->append(" "); | 181 result->append(" "); |
175 result->appendScalar(colorData[info.fColorCount - 1][2]); | 182 result->appendScalar(colorData[info.fColorCount - 1][2]); |
176 | 183 |
177 for (int i = 0 ; i < info.fColorCount; i++) { | 184 for (int i = 0 ; i < gradients + 1; i++) { |
178 result->append("} ifelse\n"); | 185 result->append("} ifelse\n"); |
179 } | 186 } |
180 } | 187 } |
181 | 188 |
182 /* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */ | 189 /* Map a value of t on the stack into [0, 1) for Repeat or Mirror tile mode. */ |
183 static void tileModeCode(SkShader::TileMode mode, SkString* result) { | 190 static void tileModeCode(SkShader::TileMode mode, SkString* result) { |
184 if (mode == SkShader::kRepeat_TileMode) { | 191 if (mode == SkShader::kRepeat_TileMode) { |
185 result->append("dup truncate sub\n"); // Get the fractional part. | 192 result->append("dup truncate sub\n"); // Get the fractional part. |
186 result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1) | 193 result->append("dup 0 le {1 add} if\n"); // Map (-1,0) => (0,1) |
187 return; | 194 return; |
(...skipping 1022 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1210 return false; | 1217 return false; |
1211 } | 1218 } |
1212 | 1219 |
1213 void SkPDFShader::State::AllocateGradientInfoStorage() { | 1220 void SkPDFShader::State::AllocateGradientInfoStorage() { |
1214 fColorData.set(sk_malloc_throw( | 1221 fColorData.set(sk_malloc_throw( |
1215 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); | 1222 fInfo.fColorCount * (sizeof(SkColor) + sizeof(SkScalar)))); |
1216 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); | 1223 fInfo.fColors = reinterpret_cast<SkColor*>(fColorData.get()); |
1217 fInfo.fColorOffsets = | 1224 fInfo.fColorOffsets = |
1218 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); | 1225 reinterpret_cast<SkScalar*>(fInfo.fColors + fInfo.fColorCount); |
1219 } | 1226 } |
OLD | NEW |