fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Google Inc. |
| 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 | #include "SkSVGDevice.h" |
| 9 | |
| 10 | #include "SkBitmap.h" |
| 11 | #include "SkDraw.h" |
| 12 | #include "SkPaint.h" |
| 13 | #include "SkParsePath.h" |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 14 | #include "SkPathOps.h" |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 15 | #include "SkShader.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 16 | #include "SkStream.h" |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 17 | #include "SkTypeface.h" |
| 18 | #include "SkUtils.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 19 | #include "SkXMLWriter.h" |
| 20 | |
| 21 | namespace { |
| 22 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 23 | static SkString svg_color(SkColor color) { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 24 | return SkStringPrintf("rgb(%u,%u,%u)", |
| 25 | SkColorGetR(color), |
| 26 | SkColorGetG(color), |
| 27 | SkColorGetB(color)); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static SkScalar svg_opacity(SkColor color) { |
| 31 | return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; |
| 32 | } |
| 33 | |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 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 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 60 | // Keep in sync with SkPaint::Align |
| 61 | static const char* text_align_map[] = { |
| 62 | NULL, // kLeft_Align (default) |
| 63 | "middle", // kCenter_Align |
| 64 | "end" // kRight_Align |
| 65 | }; |
| 66 | SK_COMPILE_ASSERT(SK_ARRAY_COUNT(text_align_map) == SkPaint::kAlignCount, |
| 67 | missing_text_align_map_entry); |
| 68 | static const char* svg_text_align(SkPaint::Align align) { |
| 69 | SkASSERT(align < SK_ARRAY_COUNT(text_align_map)); |
| 70 | return text_align_map[align]; |
| 71 | } |
| 72 | |
| 73 | static SkString svg_transform(const SkMatrix& t) { |
| 74 | SkASSERT(!t.isIdentity()); |
| 75 | |
| 76 | SkString tstr; |
| 77 | switch (t.getType()) { |
| 78 | case SkMatrix::kPerspective_Mask: |
| 79 | SkDebugf("Can't handle perspective matrices."); |
| 80 | break; |
| 81 | case SkMatrix::kTranslate_Mask: |
| 82 | tstr.printf("translate(%g %g)", t.getTranslateX(), t.getTranslateY()); |
| 83 | break; |
| 84 | case SkMatrix::kScale_Mask: |
| 85 | tstr.printf("scale(%g %g)", t.getScaleX(), t.getScaleY()); |
| 86 | break; |
| 87 | default: |
| 88 | // http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined |
| 89 | // | a c e | |
| 90 | // | b d f | |
| 91 | // | 0 0 1 | |
| 92 | tstr.printf("matrix(%g %g %g %g %g %g)", |
| 93 | t.getScaleX(), t.getSkewY(), |
| 94 | t.getSkewX(), t.getScaleY(), |
| 95 | t.getTranslateX(), t.getTranslateY()); |
| 96 | break; |
| 97 | } |
| 98 | |
| 99 | return tstr; |
| 100 | } |
| 101 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 102 | static void append_escaped_unichar(SkUnichar c, SkString* text) { |
| 103 | switch(c) { |
| 104 | case '&': |
| 105 | text->append("&"); |
| 106 | break; |
| 107 | case '"': |
| 108 | text->append("""); |
| 109 | break; |
| 110 | case '\'': |
| 111 | text->append("'"); |
| 112 | break; |
| 113 | case '<': |
| 114 | text->append("<"); |
| 115 | break; |
| 116 | case '>': |
| 117 | text->append(">"); |
| 118 | break; |
| 119 | default: |
| 120 | text->appendUnichar(c); |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static SkString svg_text(const void* text, size_t byteLen, const SkPaint& paint) { |
| 126 | SkString svgText; |
| 127 | int count = paint.countText(text, byteLen); |
| 128 | |
| 129 | switch(paint.getTextEncoding()) { |
| 130 | case SkPaint::kGlyphID_TextEncoding: { |
| 131 | SkASSERT(count * sizeof(uint16_t) == byteLen); |
| 132 | SkAutoSTArray<64, SkUnichar> unichars(count); |
| 133 | paint.glyphsToUnichars((const uint16_t*)text, count, unichars.get()); |
| 134 | for (int i = 0; i < count; ++i) { |
| 135 | append_escaped_unichar(unichars[i], &svgText); |
| 136 | } |
| 137 | } break; |
| 138 | case SkPaint::kUTF8_TextEncoding: { |
| 139 | const char* c8 = reinterpret_cast<const char*>(text); |
| 140 | for (int i = 0; i < count; ++i) { |
| 141 | append_escaped_unichar(SkUTF8_NextUnichar(&c8), &svgText); |
| 142 | } |
| 143 | SkASSERT(reinterpret_cast<const char*>(text) + byteLen == c8); |
| 144 | } break; |
| 145 | case SkPaint::kUTF16_TextEncoding: { |
| 146 | const uint16_t* c16 = reinterpret_cast<const uint16_t*>(text); |
| 147 | for (int i = 0; i < count; ++i) { |
| 148 | append_escaped_unichar(SkUTF16_NextUnichar(&c16), &svgText); |
| 149 | } |
| 150 | SkASSERT(SkIsAlign2(byteLen)); |
| 151 | SkASSERT(reinterpret_cast<const uint16_t*>(text) + (byteLen / 2) == c16); |
| 152 | } break; |
| 153 | case SkPaint::kUTF32_TextEncoding: { |
| 154 | SkASSERT(count * sizeof(uint32_t) == byteLen); |
| 155 | const uint32_t* c32 = reinterpret_cast<const uint32_t*>(text); |
| 156 | for (int i = 0; i < count; ++i) { |
| 157 | append_escaped_unichar(c32[i], &svgText); |
| 158 | } |
| 159 | } break; |
| 160 | default: |
| 161 | SkFAIL("unknown text encoding"); |
| 162 | } |
| 163 | |
| 164 | return svgText; |
| 165 | } |
| 166 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 167 | struct Resources { |
| 168 | Resources(const SkPaint& paint) |
| 169 | : fPaintServer(svg_color(paint.getColor())) {} |
| 170 | |
| 171 | SkString fPaintServer; |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 172 | SkString fClip; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } |
| 176 | |
| 177 | // For now all this does is serve unique serial IDs, but it will eventually evolve to track |
| 178 | // and deduplicate resources. |
| 179 | class SkSVGDevice::ResourceBucket : ::SkNoncopyable { |
| 180 | public: |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 181 | ResourceBucket() : fGradientCount(0), fClipCount(0), fPathCount(0) {} |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 182 | |
| 183 | SkString addLinearGradient() { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 184 | return SkStringPrintf("gradient_%d", fGradientCount++); |
| 185 | } |
| 186 | |
| 187 | SkString addClip() { |
| 188 | return SkStringPrintf("clip_%d", fClipCount++); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 189 | } |
| 190 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 191 | SkString addPath() { |
| 192 | return SkStringPrintf("path_%d", fPathCount++); |
| 193 | } |
| 194 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 195 | private: |
| 196 | uint32_t fGradientCount; |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 197 | uint32_t fClipCount; |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 198 | uint32_t fPathCount; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | class SkSVGDevice::AutoElement : ::SkNoncopyable { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 202 | public: |
| 203 | AutoElement(const char name[], SkXMLWriter* writer) |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 204 | : fWriter(writer) |
| 205 | , fResourceBucket(NULL) { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 206 | fWriter->startElement(name); |
| 207 | } |
| 208 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 209 | AutoElement(const char name[], SkXMLWriter* writer, ResourceBucket* bucket, |
| 210 | const SkDraw& draw, const SkPaint& paint) |
| 211 | : fWriter(writer) |
| 212 | , fResourceBucket(bucket) { |
| 213 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 214 | Resources res = this->addResources(draw, paint); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 215 | |
| 216 | fWriter->startElement(name); |
| 217 | |
| 218 | this->addPaint(paint, res); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 219 | |
| 220 | if (!draw.fMatrix->isIdentity()) { |
| 221 | this->addAttribute("transform", svg_transform(*draw.fMatrix)); |
| 222 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 223 | } |
| 224 | |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 225 | ~AutoElement() { |
| 226 | fWriter->endElement(); |
| 227 | } |
| 228 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 229 | void addAttribute(const char name[], const char val[]) { |
| 230 | fWriter->addAttribute(name, val); |
| 231 | } |
| 232 | |
| 233 | void addAttribute(const char name[], const SkString& val) { |
| 234 | fWriter->addAttribute(name, val.c_str()); |
| 235 | } |
| 236 | |
| 237 | void addAttribute(const char name[], int32_t val) { |
| 238 | fWriter->addS32Attribute(name, val); |
| 239 | } |
| 240 | |
| 241 | void addAttribute(const char name[], SkScalar val) { |
| 242 | fWriter->addScalarAttribute(name, val); |
| 243 | } |
| 244 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 245 | void addText(const SkString& text) { |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 246 | fWriter->addText(text.c_str(), text.size()); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 247 | } |
| 248 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 249 | void addRectAttributes(const SkRect&); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 250 | void addPathAttributes(const SkPath&); |
| 251 | void addTextAttributes(const SkPaint&); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 252 | |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 253 | private: |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 254 | Resources addResources(const SkDraw& draw, const SkPaint& paint); |
| 255 | void addClipResources(const SkDraw& draw, Resources* resources); |
| 256 | void addShaderResources(const SkPaint& paint, Resources* resources); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 257 | |
| 258 | void addPaint(const SkPaint& paint, const Resources& resources); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 259 | |
| 260 | SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkShader* shader); |
| 261 | |
| 262 | SkXMLWriter* fWriter; |
| 263 | ResourceBucket* fResourceBucket; |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 264 | }; |
| 265 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 266 | void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& resources) { |
| 267 | SkPaint::Style style = paint.getStyle(); |
| 268 | if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 269 | this->addAttribute("fill", resources.fPaintServer); |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 270 | |
| 271 | if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 272 | this->addAttribute("fill-opacity", svg_opacity(paint.getColor())); |
| 273 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 274 | } else { |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 275 | SkASSERT(style == SkPaint::kStroke_Style); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 276 | this->addAttribute("fill", "none"); |
| 277 | } |
| 278 | |
| 279 | if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 280 | this->addAttribute("stroke", resources.fPaintServer); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 281 | |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 282 | SkScalar strokeWidth = paint.getStrokeWidth(); |
| 283 | if (strokeWidth == 0) { |
| 284 | // Hairline stroke |
| 285 | strokeWidth = 1; |
| 286 | this->addAttribute("vector-effect", "non-scaling-stroke"); |
| 287 | } |
| 288 | this->addAttribute("stroke-width", strokeWidth); |
| 289 | |
| 290 | if (const char* cap = svg_cap(paint.getStrokeCap())) { |
| 291 | this->addAttribute("stroke-linecap", cap); |
| 292 | } |
| 293 | |
| 294 | if (const char* join = svg_join(paint.getStrokeJoin())) { |
| 295 | this->addAttribute("stroke-linejoin", join); |
| 296 | } |
| 297 | |
| 298 | if (paint.getStrokeJoin() == SkPaint::kMiter_Join) { |
| 299 | this->addAttribute("stroke-miterlimit", paint.getStrokeMiter()); |
| 300 | } |
| 301 | |
| 302 | if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 303 | this->addAttribute("stroke-opacity", svg_opacity(paint.getColor())); |
| 304 | } |
| 305 | } else { |
| 306 | SkASSERT(style == SkPaint::kFill_Style); |
| 307 | this->addAttribute("stroke", "none"); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 308 | } |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 309 | |
| 310 | if (!resources.fClip.isEmpty()) { |
| 311 | this->addAttribute("clip-path", resources.fClip); |
| 312 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 313 | } |
| 314 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 315 | Resources SkSVGDevice::AutoElement::addResources(const SkDraw& draw, const SkPaint& paint) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 316 | Resources resources(paint); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 317 | |
| 318 | // FIXME: this is a weak heuristic and we end up with LOTS of redundant clips. |
| 319 | bool hasClip = !draw.fClipStack->isWideOpen(); |
| 320 | bool hasShader = SkToBool(paint.getShader()); |
| 321 | |
| 322 | if (hasClip || hasShader) { |
| 323 | AutoElement defs("defs", fWriter); |
| 324 | |
| 325 | if (hasClip) { |
| 326 | this->addClipResources(draw, &resources); |
| 327 | } |
| 328 | |
| 329 | if (hasShader) { |
| 330 | this->addShaderResources(paint, &resources); |
| 331 | } |
| 332 | } |
| 333 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 334 | return resources; |
| 335 | } |
| 336 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 337 | void SkSVGDevice::AutoElement::addShaderResources(const SkPaint& paint, Resources* resources) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 338 | const SkShader* shader = paint.getShader(); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 339 | SkASSERT(SkToBool(shader)); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 340 | |
| 341 | SkShader::GradientInfo grInfo; |
| 342 | grInfo.fColorCount = 0; |
| 343 | if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) { |
| 344 | // TODO: non-linear gradient support |
| 345 | SkDebugf("unsupported shader type\n"); |
| 346 | return; |
| 347 | } |
| 348 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 349 | SkAutoSTArray<16, SkColor> grColors(grInfo.fColorCount); |
| 350 | SkAutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount); |
| 351 | grInfo.fColors = grColors.get(); |
| 352 | grInfo.fColorOffsets = grOffsets.get(); |
| 353 | |
| 354 | // One more call to get the actual colors/offsets. |
| 355 | shader->asAGradient(&grInfo); |
| 356 | SkASSERT(grInfo.fColorCount <= grColors.count()); |
| 357 | SkASSERT(grInfo.fColorCount <= grOffsets.count()); |
| 358 | |
| 359 | resources->fPaintServer.printf("url(#%s)", addLinearGradientDef(grInfo, shader).c_str()); |
| 360 | } |
| 361 | |
| 362 | void SkSVGDevice::AutoElement::addClipResources(const SkDraw& draw, Resources* resources) { |
| 363 | SkASSERT(!draw.fClipStack->isWideOpen()); |
| 364 | |
| 365 | SkPath clipPath; |
| 366 | (void) draw.fClipStack->asPath(&clipPath); |
| 367 | |
| 368 | SkString clipID = fResourceBucket->addClip(); |
| 369 | const char* clipRule = clipPath.getFillType() == SkPath::kEvenOdd_FillType ? |
| 370 | "evenodd" : "nonzero"; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 371 | { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 372 | // clipPath is in device space, but since we're only pushing transform attributes |
| 373 | // to the leaf nodes, so are all our elements => SVG userSpaceOnUse == device space. |
| 374 | AutoElement clipPathElement("clipPath", fWriter); |
| 375 | clipPathElement.addAttribute("id", clipID); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 376 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 377 | SkRect clipRect = SkRect::MakeEmpty(); |
| 378 | if (clipPath.isEmpty() || clipPath.isRect(&clipRect)) { |
| 379 | AutoElement rectElement("rect", fWriter); |
| 380 | rectElement.addRectAttributes(clipRect); |
| 381 | rectElement.addAttribute("clip-rule", clipRule); |
| 382 | } else { |
| 383 | AutoElement pathElement("path", fWriter); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 384 | pathElement.addPathAttributes(clipPath); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 385 | pathElement.addAttribute("clip-rule", clipRule); |
| 386 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 387 | } |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 388 | |
| 389 | resources->fClip.printf("url(#%s)", clipID.c_str()); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | SkString SkSVGDevice::AutoElement::addLinearGradientDef(const SkShader::GradientInfo& info, |
| 393 | const SkShader* shader) { |
| 394 | SkASSERT(fResourceBucket); |
| 395 | SkString id = fResourceBucket->addLinearGradient(); |
| 396 | |
| 397 | { |
| 398 | AutoElement gradient("linearGradient", fWriter); |
| 399 | |
| 400 | gradient.addAttribute("id", id); |
| 401 | gradient.addAttribute("gradientUnits", "userSpaceOnUse"); |
| 402 | gradient.addAttribute("x1", info.fPoint[0].x()); |
| 403 | gradient.addAttribute("y1", info.fPoint[0].y()); |
| 404 | gradient.addAttribute("x2", info.fPoint[1].x()); |
| 405 | gradient.addAttribute("y2", info.fPoint[1].y()); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 406 | |
| 407 | if (!shader->getLocalMatrix().isIdentity()) { |
| 408 | this->addAttribute("gradientTransform", svg_transform(shader->getLocalMatrix())); |
| 409 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 410 | |
| 411 | SkASSERT(info.fColorCount >= 2); |
| 412 | for (int i = 0; i < info.fColorCount; ++i) { |
| 413 | SkColor color = info.fColors[i]; |
| 414 | SkString colorStr(svg_color(color)); |
| 415 | |
| 416 | { |
| 417 | AutoElement stop("stop", fWriter); |
| 418 | stop.addAttribute("offset", info.fColorOffsets[i]); |
| 419 | stop.addAttribute("stop-color", colorStr.c_str()); |
| 420 | |
| 421 | if (SK_AlphaOPAQUE != SkColorGetA(color)) { |
| 422 | stop.addAttribute("stop-opacity", svg_opacity(color)); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return id; |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 429 | } |
| 430 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 431 | void SkSVGDevice::AutoElement::addRectAttributes(const SkRect& rect) { |
| 432 | // x, y default to 0 |
| 433 | if (rect.x() != 0) { |
| 434 | this->addAttribute("x", rect.x()); |
| 435 | } |
| 436 | if (rect.y() != 0) { |
| 437 | this->addAttribute("y", rect.y()); |
| 438 | } |
| 439 | |
| 440 | this->addAttribute("width", rect.width()); |
| 441 | this->addAttribute("height", rect.height()); |
| 442 | } |
| 443 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 444 | void SkSVGDevice::AutoElement::addPathAttributes(const SkPath& path) { |
| 445 | SkString pathData; |
| 446 | SkParsePath::ToSVGString(path, &pathData); |
| 447 | this->addAttribute("d", pathData); |
| 448 | } |
| 449 | |
| 450 | void SkSVGDevice::AutoElement::addTextAttributes(const SkPaint& paint) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 451 | this->addAttribute("font-size", paint.getTextSize()); |
| 452 | |
| 453 | SkTypeface::Style style = paint.getTypeface()->style(); |
| 454 | if (style & SkTypeface::kItalic) { |
| 455 | this->addAttribute("font-style", "italic"); |
| 456 | } |
| 457 | if (style & SkTypeface::kBold) { |
| 458 | this->addAttribute("font-weight", "bold"); |
| 459 | } |
| 460 | |
| 461 | SkAutoTUnref<const SkTypeface> tface(paint.getTypeface() ? |
| 462 | SkRef(paint.getTypeface()) : SkTypeface::RefDefault(style)); |
| 463 | SkString familyName; |
| 464 | tface->getFamilyName(&familyName); |
| 465 | if (!familyName.isEmpty()) { |
| 466 | this->addAttribute("font-family", familyName); |
| 467 | } |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 468 | |
| 469 | if (const char* textAlign = svg_text_align(paint.getTextAlign())) { |
| 470 | this->addAttribute("text-anchor", textAlign); |
| 471 | } |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 472 | } |
| 473 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 474 | SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkXMLWriter* writer) { |
| 475 | if (!writer) { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 476 | return NULL; |
| 477 | } |
| 478 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 479 | return SkNEW_ARGS(SkSVGDevice, (size, writer)); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 480 | } |
| 481 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 482 | SkSVGDevice::SkSVGDevice(const SkISize& size, SkXMLWriter* writer) |
| 483 | : fWriter(writer) |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 484 | , fResourceBucket(SkNEW(ResourceBucket)) { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame^] | 485 | SkASSERT(writer); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 486 | |
| 487 | fLegacyBitmap.setInfo(SkImageInfo::MakeUnknown(size.width(), size.height())); |
| 488 | |
| 489 | fWriter->writeHeader(); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 490 | |
| 491 | // The root <svg> tag gets closed by the destructor. |
| 492 | fRootElement.reset(SkNEW_ARGS(AutoElement, ("svg", fWriter))); |
| 493 | |
| 494 | fRootElement->addAttribute("xmlns", "http://www.w3.org/2000/svg"); |
| 495 | fRootElement->addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); |
| 496 | fRootElement->addAttribute("width", size.width()); |
| 497 | fRootElement->addAttribute("height", size.height()); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | SkSVGDevice::~SkSVGDevice() { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | SkImageInfo SkSVGDevice::imageInfo() const { |
| 504 | return fLegacyBitmap.info(); |
| 505 | } |
| 506 | |
| 507 | const SkBitmap& SkSVGDevice::onAccessBitmap() { |
| 508 | return fLegacyBitmap; |
| 509 | } |
| 510 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 511 | void SkSVGDevice::drawPaint(const SkDraw& draw, const SkPaint& paint) { |
| 512 | AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 513 | rect.addRectAttributes(SkRect::MakeWH(SkIntToScalar(this->width()), |
| 514 | SkIntToScalar(this->height()))); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void SkSVGDevice::drawPoints(const SkDraw&, SkCanvas::PointMode mode, size_t count, |
| 518 | const SkPoint[], const SkPaint& paint) { |
| 519 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 520 | SkDebugf("unsupported operation: drawPoints()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | void SkSVGDevice::drawRect(const SkDraw& draw, const SkRect& r, const SkPaint& paint) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 524 | AutoElement rect("rect", fWriter, fResourceBucket, draw, paint); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 525 | rect.addRectAttributes(r); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 526 | } |
| 527 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 528 | void SkSVGDevice::drawOval(const SkDraw& draw, const SkRect& oval, const SkPaint& paint) { |
| 529 | AutoElement ellipse("ellipse", fWriter, fResourceBucket, draw, paint); |
| 530 | ellipse.addAttribute("cx", oval.centerX()); |
| 531 | ellipse.addAttribute("cy", oval.centerY()); |
| 532 | ellipse.addAttribute("rx", oval.width() / 2); |
| 533 | ellipse.addAttribute("ry", oval.height() / 2); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | void SkSVGDevice::drawRRect(const SkDraw&, const SkRRect& rr, const SkPaint& paint) { |
| 537 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 538 | SkDebugf("unsupported operation: drawRRect()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | void SkSVGDevice::drawPath(const SkDraw& draw, const SkPath& path, const SkPaint& paint, |
| 542 | const SkMatrix* prePathMatrix, bool pathIsMutable) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 543 | AutoElement elem("path", fWriter, fResourceBucket, draw, paint); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 544 | elem.addPathAttributes(path); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | void SkSVGDevice::drawBitmap(const SkDraw&, const SkBitmap& bitmap, |
| 548 | const SkMatrix& matrix, const SkPaint& paint) { |
| 549 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 550 | SkDebugf("unsupported operation: drawBitmap()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | void SkSVGDevice::drawSprite(const SkDraw&, const SkBitmap& bitmap, |
| 554 | int x, int y, const SkPaint& paint) { |
| 555 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 556 | SkDebugf("unsupported operation: drawSprite()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | void SkSVGDevice::drawBitmapRect(const SkDraw&, const SkBitmap&, const SkRect* srcOrNull, |
| 560 | const SkRect& dst, const SkPaint& paint, |
| 561 | SkCanvas::DrawBitmapRectFlags flags) { |
| 562 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 563 | SkDebugf("unsupported operation: drawBitmapRect()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 564 | } |
| 565 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 566 | void SkSVGDevice::drawText(const SkDraw& draw, const void* text, size_t len, |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 567 | SkScalar x, SkScalar y, const SkPaint& paint) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 568 | AutoElement elem("text", fWriter, fResourceBucket, draw, paint); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 569 | elem.addTextAttributes(paint); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 570 | elem.addAttribute("x", x); |
| 571 | elem.addAttribute("y", y); |
| 572 | elem.addText(svg_text(text, len, paint)); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 573 | } |
| 574 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 575 | void SkSVGDevice::drawPosText(const SkDraw& draw, const void* text, size_t len, |
| 576 | const SkScalar pos[], int scalarsPerPos, const SkPoint& offset, |
| 577 | const SkPaint& paint) { |
| 578 | SkASSERT(scalarsPerPos == 1 || scalarsPerPos == 2); |
| 579 | |
| 580 | AutoElement elem("text", fWriter, fResourceBucket, draw, paint); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 581 | elem.addTextAttributes(paint); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 582 | |
| 583 | SkString xStr; |
| 584 | SkString yStr; |
| 585 | for (int i = 0; i < paint.countText(text, len); ++i) { |
| 586 | xStr.appendf("%.8g, ", offset.x() + pos[i * scalarsPerPos]); |
| 587 | |
| 588 | if (scalarsPerPos == 2) { |
| 589 | yStr.appendf("%.8g, ", offset.y() + pos[i * scalarsPerPos + 1]); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (scalarsPerPos != 2) { |
| 594 | yStr.appendScalar(offset.y()); |
| 595 | } |
| 596 | |
| 597 | elem.addAttribute("x", xStr); |
| 598 | elem.addAttribute("y", yStr); |
| 599 | elem.addText(svg_text(text, len, paint)); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | void SkSVGDevice::drawTextOnPath(const SkDraw&, const void* text, size_t len, const SkPath& path, |
| 603 | const SkMatrix* matrix, const SkPaint& paint) { |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 604 | SkString pathID = fResourceBucket->addPath(); |
| 605 | |
| 606 | { |
| 607 | AutoElement defs("defs", fWriter); |
| 608 | AutoElement pathElement("path", fWriter); |
| 609 | pathElement.addAttribute("id", pathID); |
| 610 | pathElement.addPathAttributes(path); |
| 611 | |
| 612 | } |
| 613 | |
| 614 | { |
| 615 | AutoElement textElement("text", fWriter); |
| 616 | textElement.addTextAttributes(paint); |
| 617 | |
| 618 | if (matrix && !matrix->isIdentity()) { |
| 619 | textElement.addAttribute("transform", svg_transform(*matrix)); |
| 620 | } |
| 621 | |
| 622 | { |
| 623 | AutoElement textPathElement("textPath", fWriter); |
| 624 | textPathElement.addAttribute("xlink:href", SkStringPrintf("#%s", pathID.c_str())); |
| 625 | |
| 626 | if (paint.getTextAlign() != SkPaint::kLeft_Align) { |
| 627 | SkASSERT(paint.getTextAlign() == SkPaint::kCenter_Align || |
| 628 | paint.getTextAlign() == SkPaint::kRight_Align); |
| 629 | textPathElement.addAttribute("startOffset", |
| 630 | paint.getTextAlign() == SkPaint::kCenter_Align ? "50%" : "100%"); |
| 631 | } |
| 632 | |
| 633 | textPathElement.addText(svg_text(text, len, paint)); |
| 634 | } |
| 635 | } |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 636 | } |
| 637 | |
| 638 | void SkSVGDevice::drawVertices(const SkDraw&, SkCanvas::VertexMode, int vertexCount, |
| 639 | const SkPoint verts[], const SkPoint texs[], |
| 640 | const SkColor colors[], SkXfermode* xmode, |
| 641 | const uint16_t indices[], int indexCount, |
| 642 | const SkPaint& paint) { |
| 643 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 644 | SkDebugf("unsupported operation: drawVertices()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | void SkSVGDevice::drawDevice(const SkDraw&, SkBaseDevice*, int x, int y, |
| 648 | const SkPaint&) { |
| 649 | // todo |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 650 | SkDebugf("unsupported operation: drawDevice()\n"); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 651 | } |