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" |
11 #include "SkDraw.h" | 11 #include "SkDraw.h" |
12 #include "SkPaint.h" | 12 #include "SkPaint.h" |
13 #include "SkParsePath.h" | 13 #include "SkParsePath.h" |
| 14 #include "SkPathOps.h" |
14 #include "SkShader.h" | 15 #include "SkShader.h" |
15 #include "SkStream.h" | 16 #include "SkStream.h" |
16 #include "SkTypeface.h" | 17 #include "SkTypeface.h" |
17 #include "SkUtils.h" | 18 #include "SkUtils.h" |
18 #include "SkXMLWriter.h" | 19 #include "SkXMLWriter.h" |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 static SkString svg_color(SkColor color) { | 23 static SkString svg_color(SkColor color) { |
23 SkString colorStr; | 24 return SkStringPrintf("rgb(%u,%u,%u)", |
24 colorStr.printf("rgb(%u,%u,%u)", | 25 SkColorGetR(color), |
25 SkColorGetR(color), | 26 SkColorGetG(color), |
26 SkColorGetG(color), | 27 SkColorGetB(color)); |
27 SkColorGetB(color)); | |
28 return colorStr; | |
29 } | 28 } |
30 | 29 |
31 static SkScalar svg_opacity(SkColor color) { | 30 static SkScalar svg_opacity(SkColor color) { |
32 return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; | 31 return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; |
33 } | 32 } |
34 | 33 |
35 static void append_escaped_unichar(SkUnichar c, SkString* text) { | 34 static void append_escaped_unichar(SkUnichar c, SkString* text) { |
36 switch(c) { | 35 switch(c) { |
37 case '&': | 36 case '&': |
38 text->append("&"); | 37 text->append("&"); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 } | 94 } |
96 | 95 |
97 return svgText; | 96 return svgText; |
98 } | 97 } |
99 | 98 |
100 struct Resources { | 99 struct Resources { |
101 Resources(const SkPaint& paint) | 100 Resources(const SkPaint& paint) |
102 : fPaintServer(svg_color(paint.getColor())) {} | 101 : fPaintServer(svg_color(paint.getColor())) {} |
103 | 102 |
104 SkString fPaintServer; | 103 SkString fPaintServer; |
| 104 SkString fClip; |
105 }; | 105 }; |
106 | 106 |
107 } | 107 } |
108 | 108 |
109 // For now all this does is serve unique serial IDs, but it will eventually evol
ve to track | 109 // For now all this does is serve unique serial IDs, but it will eventually evol
ve to track |
110 // and deduplicate resources. | 110 // and deduplicate resources. |
111 class SkSVGDevice::ResourceBucket : ::SkNoncopyable { | 111 class SkSVGDevice::ResourceBucket : ::SkNoncopyable { |
112 public: | 112 public: |
113 ResourceBucket() : fGradientCount(0) {} | 113 ResourceBucket() : fGradientCount(0), fClipCount(0) {} |
114 | 114 |
115 SkString addLinearGradient() { | 115 SkString addLinearGradient() { |
116 SkString id; | 116 return SkStringPrintf("gradient_%d", fGradientCount++); |
117 id.printf("gradient_%d", fGradientCount++); | 117 } |
118 return id; | 118 |
| 119 SkString addClip() { |
| 120 return SkStringPrintf("clip_%d", fClipCount++); |
119 } | 121 } |
120 | 122 |
121 private: | 123 private: |
122 uint32_t fGradientCount; | 124 uint32_t fGradientCount; |
| 125 uint32_t fClipCount; |
123 }; | 126 }; |
124 | 127 |
125 class SkSVGDevice::AutoElement : ::SkNoncopyable { | 128 class SkSVGDevice::AutoElement : ::SkNoncopyable { |
126 public: | 129 public: |
127 AutoElement(const char name[], SkXMLWriter* writer) | 130 AutoElement(const char name[], SkXMLWriter* writer) |
128 : fWriter(writer) | 131 : fWriter(writer) |
129 , fResourceBucket(NULL) { | 132 , fResourceBucket(NULL) { |
130 fWriter->startElement(name); | 133 fWriter->startElement(name); |
131 } | 134 } |
132 | 135 |
133 AutoElement(const char name[], SkXMLWriter* writer, ResourceBucket* bucket, | 136 AutoElement(const char name[], SkXMLWriter* writer, ResourceBucket* bucket, |
134 const SkDraw& draw, const SkPaint& paint) | 137 const SkDraw& draw, const SkPaint& paint) |
135 : fWriter(writer) | 138 : fWriter(writer) |
136 , fResourceBucket(bucket) { | 139 , fResourceBucket(bucket) { |
137 | 140 |
138 Resources res = this->addResources(paint); | 141 Resources res = this->addResources(draw, paint); |
139 | 142 |
140 fWriter->startElement(name); | 143 fWriter->startElement(name); |
141 | 144 |
142 this->addPaint(paint, res); | 145 this->addPaint(paint, res); |
143 this->addTransform(*draw.fMatrix); | 146 this->addTransform(*draw.fMatrix); |
144 } | 147 } |
145 | 148 |
146 ~AutoElement() { | 149 ~AutoElement() { |
147 fWriter->endElement(); | 150 fWriter->endElement(); |
148 } | 151 } |
(...skipping 11 matching lines...) Expand all Loading... |
160 } | 163 } |
161 | 164 |
162 void addAttribute(const char name[], SkScalar val) { | 165 void addAttribute(const char name[], SkScalar val) { |
163 fWriter->addScalarAttribute(name, val); | 166 fWriter->addScalarAttribute(name, val); |
164 } | 167 } |
165 | 168 |
166 void addText(const SkString& text) { | 169 void addText(const SkString& text) { |
167 fWriter->addText(text.c_str()); | 170 fWriter->addText(text.c_str()); |
168 } | 171 } |
169 | 172 |
| 173 void addRectAttributes(const SkRect&); |
170 void addFontAttributes(const SkPaint&); | 174 void addFontAttributes(const SkPaint&); |
171 | 175 |
172 private: | 176 private: |
173 Resources addResources(const SkPaint& paint); | 177 Resources addResources(const SkDraw& draw, const SkPaint& paint); |
174 void addResourceDefs(const SkPaint& paint, Resources* resources); | 178 void addClipResources(const SkDraw& draw, Resources* resources); |
| 179 void addShaderResources(const SkPaint& paint, Resources* resources); |
175 | 180 |
176 void addPaint(const SkPaint& paint, const Resources& resources); | 181 void addPaint(const SkPaint& paint, const Resources& resources); |
177 void addTransform(const SkMatrix& transform, const char name[] = "transform"
); | 182 void addTransform(const SkMatrix& transform, const char name[] = "transform"
); |
178 | 183 |
179 SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkSh
ader* shader); | 184 SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkSh
ader* shader); |
180 | 185 |
181 SkXMLWriter* fWriter; | 186 SkXMLWriter* fWriter; |
182 ResourceBucket* fResourceBucket; | 187 ResourceBucket* fResourceBucket; |
183 }; | 188 }; |
184 | 189 |
185 void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& r
esources) { | 190 void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& r
esources) { |
186 SkPaint::Style style = paint.getStyle(); | 191 SkPaint::Style style = paint.getStyle(); |
187 if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style)
{ | 192 if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style)
{ |
188 this->addAttribute("fill", resources.fPaintServer); | 193 this->addAttribute("fill", resources.fPaintServer); |
189 } else { | 194 } else { |
190 this->addAttribute("fill", "none"); | 195 this->addAttribute("fill", "none"); |
191 } | 196 } |
192 | 197 |
193 if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Styl
e) { | 198 if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Styl
e) { |
194 this->addAttribute("stroke", resources.fPaintServer); | 199 this->addAttribute("stroke", resources.fPaintServer); |
195 this->addAttribute("stroke-width", paint.getStrokeWidth()); | 200 this->addAttribute("stroke-width", paint.getStrokeWidth()); |
196 } else { | 201 } else { |
197 this->addAttribute("stroke", "none"); | 202 this->addAttribute("stroke", "none"); |
198 } | 203 } |
199 | 204 |
200 if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { | 205 if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
201 this->addAttribute("opacity", svg_opacity(paint.getColor())); | 206 this->addAttribute("opacity", svg_opacity(paint.getColor())); |
202 } | 207 } |
| 208 |
| 209 if (!resources.fClip.isEmpty()) { |
| 210 this->addAttribute("clip-path", resources.fClip); |
| 211 } |
203 } | 212 } |
204 | 213 |
205 void SkSVGDevice::AutoElement::addTransform(const SkMatrix& t, const char name[]
) { | 214 void SkSVGDevice::AutoElement::addTransform(const SkMatrix& t, const char name[]
) { |
206 if (t.isIdentity()) { | 215 if (t.isIdentity()) { |
207 return; | 216 return; |
208 } | 217 } |
209 | 218 |
210 SkString tstr; | 219 SkString tstr; |
211 switch (t.getType()) { | 220 switch (t.getType()) { |
212 case SkMatrix::kPerspective_Mask: | 221 case SkMatrix::kPerspective_Mask: |
(...skipping 13 matching lines...) Expand all Loading... |
226 tstr.printf("matrix(%g %g %g %g %g %g)", | 235 tstr.printf("matrix(%g %g %g %g %g %g)", |
227 SkScalarToFloat(t.getScaleX()), SkScalarToFloat(t.getSkewY(
)), | 236 SkScalarToFloat(t.getScaleX()), SkScalarToFloat(t.getSkewY(
)), |
228 SkScalarToFloat(t.getSkewX()), SkScalarToFloat(t.getScaleY(
)), | 237 SkScalarToFloat(t.getSkewX()), SkScalarToFloat(t.getScaleY(
)), |
229 SkScalarToFloat(t.getTranslateX()), SkScalarToFloat(t.getTr
anslateY())); | 238 SkScalarToFloat(t.getTranslateX()), SkScalarToFloat(t.getTr
anslateY())); |
230 break; | 239 break; |
231 } | 240 } |
232 | 241 |
233 fWriter->addAttribute(name, tstr.c_str()); | 242 fWriter->addAttribute(name, tstr.c_str()); |
234 } | 243 } |
235 | 244 |
236 Resources SkSVGDevice::AutoElement::addResources(const SkPaint& paint) { | 245 Resources SkSVGDevice::AutoElement::addResources(const SkDraw& draw, const SkPai
nt& paint) { |
237 Resources resources(paint); | 246 Resources resources(paint); |
238 this->addResourceDefs(paint, &resources); | 247 |
| 248 // FIXME: this is a weak heuristic and we end up with LOTS of redundant clip
s. |
| 249 bool hasClip = !draw.fClipStack->isWideOpen(); |
| 250 bool hasShader = SkToBool(paint.getShader()); |
| 251 |
| 252 if (hasClip || hasShader) { |
| 253 AutoElement defs("defs", fWriter); |
| 254 |
| 255 if (hasClip) { |
| 256 this->addClipResources(draw, &resources); |
| 257 } |
| 258 |
| 259 if (hasShader) { |
| 260 this->addShaderResources(paint, &resources); |
| 261 } |
| 262 } |
| 263 |
239 return resources; | 264 return resources; |
240 } | 265 } |
241 | 266 |
242 void SkSVGDevice::AutoElement::addResourceDefs(const SkPaint& paint, Resources*
resources) { | 267 void SkSVGDevice::AutoElement::addShaderResources(const SkPaint& paint, Resource
s* resources) { |
243 const SkShader* shader = paint.getShader(); | 268 const SkShader* shader = paint.getShader(); |
244 if (!SkToBool(shader)) { | 269 SkASSERT(SkToBool(shader)); |
245 // TODO: clip support | |
246 return; | |
247 } | |
248 | 270 |
249 SkShader::GradientInfo grInfo; | 271 SkShader::GradientInfo grInfo; |
250 grInfo.fColorCount = 0; | 272 grInfo.fColorCount = 0; |
251 if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) { | 273 if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) { |
252 // TODO: non-linear gradient support | 274 // TODO: non-linear gradient support |
253 SkDebugf("unsupported shader type\n"); | 275 SkDebugf("unsupported shader type\n"); |
254 return; | 276 return; |
255 } | 277 } |
256 | 278 |
| 279 SkAutoSTArray<16, SkColor> grColors(grInfo.fColorCount); |
| 280 SkAutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount); |
| 281 grInfo.fColors = grColors.get(); |
| 282 grInfo.fColorOffsets = grOffsets.get(); |
| 283 |
| 284 // One more call to get the actual colors/offsets. |
| 285 shader->asAGradient(&grInfo); |
| 286 SkASSERT(grInfo.fColorCount <= grColors.count()); |
| 287 SkASSERT(grInfo.fColorCount <= grOffsets.count()); |
| 288 |
| 289 resources->fPaintServer.printf("url(#%s)", addLinearGradientDef(grInfo, shad
er).c_str()); |
| 290 } |
| 291 |
| 292 void SkSVGDevice::AutoElement::addClipResources(const SkDraw& draw, Resources* r
esources) { |
| 293 SkASSERT(!draw.fClipStack->isWideOpen()); |
| 294 |
| 295 SkPath clipPath; |
| 296 (void) draw.fClipStack->asPath(&clipPath); |
| 297 |
| 298 SkString clipID = fResourceBucket->addClip(); |
| 299 const char* clipRule = clipPath.getFillType() == SkPath::kEvenOdd_FillType ? |
| 300 "evenodd" : "nonzero"; |
257 { | 301 { |
258 AutoElement defs("defs", fWriter); | 302 // clipPath is in device space, but since we're only pushing transform a
ttributes |
| 303 // to the leaf nodes, so are all our elements => SVG userSpaceOnUse == d
evice space. |
| 304 AutoElement clipPathElement("clipPath", fWriter); |
| 305 clipPathElement.addAttribute("id", clipID); |
259 | 306 |
260 SkAutoSTArray<16, SkColor> grColors(grInfo.fColorCount); | 307 SkRect clipRect = SkRect::MakeEmpty(); |
261 SkAutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount); | 308 if (clipPath.isEmpty() || clipPath.isRect(&clipRect)) { |
262 grInfo.fColors = grColors.get(); | 309 AutoElement rectElement("rect", fWriter); |
263 grInfo.fColorOffsets = grOffsets.get(); | 310 rectElement.addRectAttributes(clipRect); |
| 311 rectElement.addAttribute("clip-rule", clipRule); |
| 312 } else { |
| 313 AutoElement pathElement("path", fWriter); |
| 314 SkString pathStr; |
| 315 SkParsePath::ToSVGString(clipPath, &pathStr); |
| 316 pathElement.addAttribute("d", pathStr.c_str()); |
| 317 pathElement.addAttribute("clip-rule", clipRule); |
| 318 } |
| 319 } |
264 | 320 |
265 // One more call to get the actual colors/offsets. | 321 resources->fClip.printf("url(#%s)", clipID.c_str()); |
266 shader->asAGradient(&grInfo); | |
267 SkASSERT(grInfo.fColorCount <= grColors.count()); | |
268 SkASSERT(grInfo.fColorCount <= grOffsets.count()); | |
269 | |
270 resources->fPaintServer.printf("url(#%s)", addLinearGradientDef(grInfo,
shader).c_str()); | |
271 } | |
272 } | 322 } |
273 | 323 |
274 SkString SkSVGDevice::AutoElement::addLinearGradientDef(const SkShader::Gradient
Info& info, | 324 SkString SkSVGDevice::AutoElement::addLinearGradientDef(const SkShader::Gradient
Info& info, |
275 const SkShader* shader)
{ | 325 const SkShader* shader)
{ |
276 SkASSERT(fResourceBucket); | 326 SkASSERT(fResourceBucket); |
277 SkString id = fResourceBucket->addLinearGradient(); | 327 SkString id = fResourceBucket->addLinearGradient(); |
278 | 328 |
279 { | 329 { |
280 AutoElement gradient("linearGradient", fWriter); | 330 AutoElement gradient("linearGradient", fWriter); |
281 | 331 |
(...skipping 18 matching lines...) Expand all Loading... |
300 if (SK_AlphaOPAQUE != SkColorGetA(color)) { | 350 if (SK_AlphaOPAQUE != SkColorGetA(color)) { |
301 stop.addAttribute("stop-opacity", svg_opacity(color)); | 351 stop.addAttribute("stop-opacity", svg_opacity(color)); |
302 } | 352 } |
303 } | 353 } |
304 } | 354 } |
305 } | 355 } |
306 | 356 |
307 return id; | 357 return id; |
308 } | 358 } |
309 | 359 |
| 360 void SkSVGDevice::AutoElement::addRectAttributes(const SkRect& rect) { |
| 361 // x, y default to 0 |
| 362 if (rect.x() != 0) { |
| 363 this->addAttribute("x", rect.x()); |
| 364 } |
| 365 if (rect.y() != 0) { |
| 366 this->addAttribute("y", rect.y()); |
| 367 } |
| 368 |
| 369 this->addAttribute("width", rect.width()); |
| 370 this->addAttribute("height", rect.height()); |
| 371 } |
| 372 |
310 void SkSVGDevice::AutoElement::addFontAttributes(const SkPaint& paint) { | 373 void SkSVGDevice::AutoElement::addFontAttributes(const SkPaint& paint) { |
311 this->addAttribute("font-size", paint.getTextSize()); | 374 this->addAttribute("font-size", paint.getTextSize()); |
312 | 375 |
313 SkTypeface::Style style = paint.getTypeface()->style(); | 376 SkTypeface::Style style = paint.getTypeface()->style(); |
314 if (style & SkTypeface::kItalic) { | 377 if (style & SkTypeface::kItalic) { |
315 this->addAttribute("font-style", "italic"); | 378 this->addAttribute("font-style", "italic"); |
316 } | 379 } |
317 if (style & SkTypeface::kBold) { | 380 if (style & SkTypeface::kBold) { |
318 this->addAttribute("font-weight", "bold"); | 381 this->addAttribute("font-weight", "bold"); |
319 } | 382 } |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 SkImageInfo SkSVGDevice::imageInfo() const { | 421 SkImageInfo SkSVGDevice::imageInfo() const { |
359 return fLegacyBitmap.info(); | 422 return fLegacyBitmap.info(); |
360 } | 423 } |
361 | 424 |
362 const SkBitmap& SkSVGDevice::onAccessBitmap() { | 425 const SkBitmap& SkSVGDevice::onAccessBitmap() { |
363 return fLegacyBitmap; | 426 return fLegacyBitmap; |
364 } | 427 } |
365 | 428 |
366 void SkSVGDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { | 429 void SkSVGDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
367 AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); | 430 AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); |
368 rect.addAttribute("x", 0); | 431 rect.addRectAttributes(SkRect::MakeWH(SkIntToScalar(this->width()), |
369 rect.addAttribute("y", 0); | 432 SkIntToScalar(this->height()))); |
370 rect.addAttribute("width", this->width()); | |
371 rect.addAttribute("height", this->height()); | |
372 } | 433 } |
373 | 434 |
374 void SkSVGDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t cou
nt, | 435 void SkSVGDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t cou
nt, |
375 const SkPoint[], const SkPaint& paint) { | 436 const SkPoint[], const SkPaint& paint) { |
376 // todo | 437 // todo |
377 SkDebugf("unsupported operation: drawPoints()\n"); | 438 SkDebugf("unsupported operation: drawPoints()\n"); |
378 } | 439 } |
379 | 440 |
380 void SkSVGDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& p
aint) { | 441 void SkSVGDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& p
aint) { |
381 AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); | 442 AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); |
382 rect.addAttribute("x", r.fLeft); | 443 rect.addRectAttributes(r); |
383 rect.addAttribute("y", r.fTop); | |
384 rect.addAttribute("width", r.width()); | |
385 rect.addAttribute("height", r.height()); | |
386 } | 444 } |
387 | 445 |
388 void SkSVGDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint
& paint) { | 446 void SkSVGDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint
& paint) { |
389 AutoElement ellipse("ellipse", fWriter, fResourceBucket, draw, paint); | 447 AutoElement ellipse("ellipse", fWriter, fResourceBucket, draw, paint); |
390 ellipse.addAttribute("cx", oval.centerX()); | 448 ellipse.addAttribute("cx", oval.centerX()); |
391 ellipse.addAttribute("cy", oval.centerY()); | 449 ellipse.addAttribute("cy", oval.centerY()); |
392 ellipse.addAttribute("rx", oval.width() / 2); | 450 ellipse.addAttribute("rx", oval.width() / 2); |
393 ellipse.addAttribute("ry", oval.height() / 2); | 451 ellipse.addAttribute("ry", oval.height() / 2); |
394 } | 452 } |
395 | 453 |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 const SkPaint& paint) { | 533 const SkPaint& paint) { |
476 // todo | 534 // todo |
477 SkDebugf("unsupported operation: drawVertices()\n"); | 535 SkDebugf("unsupported operation: drawVertices()\n"); |
478 } | 536 } |
479 | 537 |
480 void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, | 538 void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
481 const SkPaint&) { | 539 const SkPaint&) { |
482 // todo | 540 // todo |
483 SkDebugf("unsupported operation: drawDevice()\n"); | 541 SkDebugf("unsupported operation: drawDevice()\n"); |
484 } | 542 } |
OLD | NEW |