OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 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 "SkSVGDevice.h" | 8 #include "SkSVGDevice.h" |
9 | 9 |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
(...skipping 13 matching lines...) Expand all Loading... |
24 return SkStringPrintf("rgb(%u,%u,%u)", | 24 return SkStringPrintf("rgb(%u,%u,%u)", |
25 SkColorGetR(color), | 25 SkColorGetR(color), |
26 SkColorGetG(color), | 26 SkColorGetG(color), |
27 SkColorGetB(color)); | 27 SkColorGetB(color)); |
28 } | 28 } |
29 | 29 |
30 static SkScalar svg_opacity(SkColor color) { | 30 static SkScalar svg_opacity(SkColor color) { |
31 return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; | 31 return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; |
32 } | 32 } |
33 | 33 |
| 34 // Keep in sync with SkPaint::Cap |
| 35 static const char* cap_map[] = { |
| 36 NULL, // kButt_Cap (default) |
| 37 "round", // kRound_Cap |
| 38 "square" // kSquare_Cap |
| 39 }; |
| 40 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(cap_map) == SkPaint::kCapCount, missing_cap_map
_entry); |
| 41 |
| 42 static const char* svg_cap(SkPaint::Cap cap) { |
| 43 SkASSERT(cap < SK_ARRAY_COUNT(cap_map)); |
| 44 return cap_map[cap]; |
| 45 } |
| 46 |
| 47 // Keep in sync with SkPaint::Join |
| 48 static const char* join_map[] = { |
| 49 NULL, // kMiter_Join (default) |
| 50 "round", // kRound_Join |
| 51 "bevel" // kBevel_Join |
| 52 }; |
| 53 SK_COMPILE_ASSERT(SK_ARRAY_COUNT(join_map) == SkPaint::kJoinCount, missing_join_
map_entry); |
| 54 |
| 55 static const char* svg_join(SkPaint::Join join) { |
| 56 SkASSERT(join < SK_ARRAY_COUNT(join_map)); |
| 57 return join_map[join]; |
| 58 } |
| 59 |
34 static void append_escaped_unichar(SkUnichar c, SkString* text) { | 60 static void append_escaped_unichar(SkUnichar c, SkString* text) { |
35 switch(c) { | 61 switch(c) { |
36 case '&': | 62 case '&': |
37 text->append("&"); | 63 text->append("&"); |
38 break; | 64 break; |
39 case '"': | 65 case '"': |
40 text->append("""); | 66 text->append("""); |
41 break; | 67 break; |
42 case '\'': | 68 case '\'': |
43 text->append("'"); | 69 text->append("'"); |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkSh
ader* shader); | 210 SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkSh
ader* shader); |
185 | 211 |
186 SkXMLWriter* fWriter; | 212 SkXMLWriter* fWriter; |
187 ResourceBucket* fResourceBucket; | 213 ResourceBucket* fResourceBucket; |
188 }; | 214 }; |
189 | 215 |
190 void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& r
esources) { | 216 void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& r
esources) { |
191 SkPaint::Style style = paint.getStyle(); | 217 SkPaint::Style style = paint.getStyle(); |
192 if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style)
{ | 218 if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style)
{ |
193 this->addAttribute("fill", resources.fPaintServer); | 219 this->addAttribute("fill", resources.fPaintServer); |
| 220 |
| 221 if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 222 this->addAttribute("fill-opacity", svg_opacity(paint.getColor())); |
| 223 } |
194 } else { | 224 } else { |
| 225 SkASSERT(style == SkPaint::kStroke_Style); |
195 this->addAttribute("fill", "none"); | 226 this->addAttribute("fill", "none"); |
196 } | 227 } |
197 | 228 |
198 if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Styl
e) { | 229 if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Styl
e) { |
199 this->addAttribute("stroke", resources.fPaintServer); | 230 this->addAttribute("stroke", resources.fPaintServer); |
200 this->addAttribute("stroke-width", paint.getStrokeWidth()); | 231 |
| 232 SkScalar strokeWidth = paint.getStrokeWidth(); |
| 233 if (strokeWidth == 0) { |
| 234 // Hairline stroke |
| 235 strokeWidth = 1; |
| 236 this->addAttribute("vector-effect", "non-scaling-stroke"); |
| 237 } |
| 238 this->addAttribute("stroke-width", strokeWidth); |
| 239 |
| 240 if (const char* cap = svg_cap(paint.getStrokeCap())) { |
| 241 this->addAttribute("stroke-linecap", cap); |
| 242 } |
| 243 |
| 244 if (const char* join = svg_join(paint.getStrokeJoin())) { |
| 245 this->addAttribute("stroke-linejoin", join); |
| 246 } |
| 247 |
| 248 if (paint.getStrokeJoin() == SkPaint::kMiter_Join) { |
| 249 this->addAttribute("stroke-miterlimit", paint.getStrokeMiter()); |
| 250 } |
| 251 |
| 252 if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 253 this->addAttribute("stroke-opacity", svg_opacity(paint.getColor())); |
| 254 } |
201 } else { | 255 } else { |
| 256 SkASSERT(style == SkPaint::kFill_Style); |
202 this->addAttribute("stroke", "none"); | 257 this->addAttribute("stroke", "none"); |
203 } | 258 } |
204 | 259 |
205 if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { | |
206 this->addAttribute("opacity", svg_opacity(paint.getColor())); | |
207 } | |
208 | |
209 if (!resources.fClip.isEmpty()) { | 260 if (!resources.fClip.isEmpty()) { |
210 this->addAttribute("clip-path", resources.fClip); | 261 this->addAttribute("clip-path", resources.fClip); |
211 } | 262 } |
212 } | 263 } |
213 | 264 |
214 void SkSVGDevice::AutoElement::addTransform(const SkMatrix& t, const char name[]
) { | 265 void SkSVGDevice::AutoElement::addTransform(const SkMatrix& t, const char name[]
) { |
215 if (t.isIdentity()) { | 266 if (t.isIdentity()) { |
216 return; | 267 return; |
217 } | 268 } |
218 | 269 |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
533 const SkPaint& paint) { | 584 const SkPaint& paint) { |
534 // todo | 585 // todo |
535 SkDebugf("unsupported operation: drawVertices()\n"); | 586 SkDebugf("unsupported operation: drawVertices()\n"); |
536 } | 587 } |
537 | 588 |
538 void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, | 589 void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
539 const SkPaint&) { | 590 const SkPaint&) { |
540 // todo | 591 // todo |
541 SkDebugf("unsupported operation: drawDevice()\n"); | 592 SkDebugf("unsupported operation: drawDevice()\n"); |
542 } | 593 } |
OLD | NEW |