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 | |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 10 | #include "SkAnnotationKeys.h" |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 11 | #include "SkBase64.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 12 | #include "SkBitmap.h" |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 13 | #include "SkBlendMode.h" |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 14 | #include "SkChecksum.h" |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 15 | #include "SkClipOpPriv.h" |
bungeman | d3ebb48 | 2015-08-05 13:57:49 -0700 | [diff] [blame] | 16 | #include "SkClipStack.h" |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 17 | #include "SkColorFilter.h" |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 18 | #include "SkData.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 19 | #include "SkDraw.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 20 | #include "SkImage.h" |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 21 | #include "SkImageEncoder.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 22 | #include "SkJpegCodec.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 23 | #include "SkPaint.h" |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 24 | #include "SkPaintPriv.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 25 | #include "SkParsePath.h" |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 26 | #include "SkPngCodec.h" |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 27 | #include "SkShader.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 28 | #include "SkStream.h" |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 29 | #include "SkTHash.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 30 | #include "SkTo.h" |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 31 | #include "SkTypeface.h" |
| 32 | #include "SkUtils.h" |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 33 | #include "SkXMLWriter.h" |
| 34 | |
| 35 | namespace { |
| 36 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 37 | static SkString svg_color(SkColor color) { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 38 | return SkStringPrintf("rgb(%u,%u,%u)", |
| 39 | SkColorGetR(color), |
| 40 | SkColorGetG(color), |
| 41 | SkColorGetB(color)); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | static SkScalar svg_opacity(SkColor color) { |
| 45 | return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; |
| 46 | } |
| 47 | |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 48 | // Keep in sync with SkPaint::Cap |
| 49 | static const char* cap_map[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 50 | nullptr, // kButt_Cap (default) |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 51 | "round", // kRound_Cap |
| 52 | "square" // kSquare_Cap |
| 53 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 54 | static_assert(SK_ARRAY_COUNT(cap_map) == SkPaint::kCapCount, "missing_cap_map_entry"); |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 55 | |
| 56 | static const char* svg_cap(SkPaint::Cap cap) { |
| 57 | SkASSERT(cap < SK_ARRAY_COUNT(cap_map)); |
| 58 | return cap_map[cap]; |
| 59 | } |
| 60 | |
| 61 | // Keep in sync with SkPaint::Join |
| 62 | static const char* join_map[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 63 | nullptr, // kMiter_Join (default) |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 64 | "round", // kRound_Join |
| 65 | "bevel" // kBevel_Join |
| 66 | }; |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 67 | static_assert(SK_ARRAY_COUNT(join_map) == SkPaint::kJoinCount, "missing_join_map_entry"); |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 68 | |
| 69 | static const char* svg_join(SkPaint::Join join) { |
| 70 | SkASSERT(join < SK_ARRAY_COUNT(join_map)); |
| 71 | return join_map[join]; |
| 72 | } |
| 73 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 74 | static SkString svg_transform(const SkMatrix& t) { |
| 75 | SkASSERT(!t.isIdentity()); |
| 76 | |
| 77 | SkString tstr; |
| 78 | switch (t.getType()) { |
| 79 | case SkMatrix::kPerspective_Mask: |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 80 | // TODO: handle perspective matrices? |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 81 | break; |
| 82 | case SkMatrix::kTranslate_Mask: |
| 83 | tstr.printf("translate(%g %g)", t.getTranslateX(), t.getTranslateY()); |
| 84 | break; |
| 85 | case SkMatrix::kScale_Mask: |
| 86 | tstr.printf("scale(%g %g)", t.getScaleX(), t.getScaleY()); |
| 87 | break; |
| 88 | default: |
| 89 | // http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined |
| 90 | // | a c e | |
| 91 | // | b d f | |
| 92 | // | 0 0 1 | |
| 93 | tstr.printf("matrix(%g %g %g %g %g %g)", |
| 94 | t.getScaleX(), t.getSkewY(), |
| 95 | t.getSkewX(), t.getScaleY(), |
| 96 | t.getTranslateX(), t.getTranslateY()); |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | return tstr; |
| 101 | } |
| 102 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 103 | struct Resources { |
| 104 | Resources(const SkPaint& paint) |
| 105 | : fPaintServer(svg_color(paint.getColor())) {} |
| 106 | |
| 107 | SkString fPaintServer; |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 108 | SkString fClip; |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 109 | SkString fColorFilter; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 110 | }; |
| 111 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 112 | // Determine if the paint requires us to reset the viewport. |
| 113 | // Currently, we do this whenever the paint shader calls |
| 114 | // for a repeating image. |
| 115 | bool RequiresViewportReset(const SkPaint& paint) { |
| 116 | SkShader* shader = paint.getShader(); |
| 117 | if (!shader) |
| 118 | return false; |
| 119 | |
| 120 | SkShader::TileMode xy[2]; |
| 121 | SkImage* image = shader->isAImage(nullptr, xy); |
| 122 | |
| 123 | if (!image) |
| 124 | return false; |
| 125 | |
| 126 | for (int i = 0; i < 2; i++) { |
| 127 | if (xy[i] == SkShader::kRepeat_TileMode) |
| 128 | return true; |
| 129 | } |
| 130 | return false; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 133 | } // namespace |
| 134 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 135 | // For now all this does is serve unique serial IDs, but it will eventually evolve to track |
| 136 | // and deduplicate resources. |
| 137 | class SkSVGDevice::ResourceBucket : ::SkNoncopyable { |
| 138 | public: |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 139 | ResourceBucket() |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 140 | : fGradientCount(0) |
| 141 | , fClipCount(0) |
| 142 | , fPathCount(0) |
| 143 | , fImageCount(0) |
| 144 | , fPatternCount(0) |
| 145 | , fColorFilterCount(0) {} |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 146 | |
| 147 | SkString addLinearGradient() { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 148 | return SkStringPrintf("gradient_%d", fGradientCount++); |
| 149 | } |
| 150 | |
| 151 | SkString addClip() { |
| 152 | return SkStringPrintf("clip_%d", fClipCount++); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 153 | } |
| 154 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 155 | SkString addPath() { |
| 156 | return SkStringPrintf("path_%d", fPathCount++); |
| 157 | } |
| 158 | |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 159 | SkString addImage() { |
| 160 | return SkStringPrintf("img_%d", fImageCount++); |
| 161 | } |
| 162 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 163 | SkString addColorFilter() { return SkStringPrintf("cfilter_%d", fColorFilterCount++); } |
| 164 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 165 | SkString addPattern() { |
| 166 | return SkStringPrintf("pattern_%d", fPatternCount++); |
| 167 | } |
| 168 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 169 | private: |
| 170 | uint32_t fGradientCount; |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 171 | uint32_t fClipCount; |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 172 | uint32_t fPathCount; |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 173 | uint32_t fImageCount; |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 174 | uint32_t fPatternCount; |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 175 | uint32_t fColorFilterCount; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 178 | struct SkSVGDevice::MxCp { |
| 179 | const SkMatrix* fMatrix; |
| 180 | const SkClipStack* fClipStack; |
| 181 | |
Mike Reed | f880b68 | 2017-03-10 11:30:44 -0500 | [diff] [blame] | 182 | MxCp(const SkMatrix* mx, const SkClipStack* cs) : fMatrix(mx), fClipStack(cs) {} |
| 183 | MxCp(SkSVGDevice* device) : fMatrix(&device->ctm()), fClipStack(&device->cs()) {} |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 184 | }; |
| 185 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 186 | class SkSVGDevice::AutoElement : ::SkNoncopyable { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 187 | public: |
| 188 | AutoElement(const char name[], SkXMLWriter* writer) |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 189 | : fWriter(writer) |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 190 | , fResourceBucket(nullptr) { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 191 | fWriter->startElement(name); |
| 192 | } |
| 193 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 194 | AutoElement(const char name[], SkXMLWriter* writer, ResourceBucket* bucket, |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 195 | const MxCp& mc, const SkPaint& paint) |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 196 | : fWriter(writer) |
| 197 | , fResourceBucket(bucket) { |
| 198 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 199 | Resources res = this->addResources(mc, paint); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 200 | |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 201 | if (!res.fClip.isEmpty()) { |
| 202 | // The clip is in device space. Apply it via a <g> wrapper to avoid local transform |
| 203 | // interference. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 204 | fClipGroup.reset(new AutoElement("g", fWriter)); |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 205 | fClipGroup->addAttribute("clip-path",res.fClip); |
| 206 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 207 | |
| 208 | fWriter->startElement(name); |
| 209 | |
| 210 | this->addPaint(paint, res); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 211 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 212 | if (!mc.fMatrix->isIdentity()) { |
| 213 | this->addAttribute("transform", svg_transform(*mc.fMatrix)); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 214 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 215 | } |
| 216 | |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 217 | ~AutoElement() { |
| 218 | fWriter->endElement(); |
| 219 | } |
| 220 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 221 | void addAttribute(const char name[], const char val[]) { |
| 222 | fWriter->addAttribute(name, val); |
| 223 | } |
| 224 | |
| 225 | void addAttribute(const char name[], const SkString& val) { |
| 226 | fWriter->addAttribute(name, val.c_str()); |
| 227 | } |
| 228 | |
| 229 | void addAttribute(const char name[], int32_t val) { |
| 230 | fWriter->addS32Attribute(name, val); |
| 231 | } |
| 232 | |
| 233 | void addAttribute(const char name[], SkScalar val) { |
| 234 | fWriter->addScalarAttribute(name, val); |
| 235 | } |
| 236 | |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 237 | void addText(const SkString& text) { |
reed | e73da40 | 2015-02-04 18:29:27 -0800 | [diff] [blame] | 238 | fWriter->addText(text.c_str(), text.size()); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 239 | } |
| 240 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 241 | void addRectAttributes(const SkRect&); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 242 | void addPathAttributes(const SkPath&); |
| 243 | void addTextAttributes(const SkPaint&); |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 244 | |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 245 | private: |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 246 | Resources addResources(const MxCp&, const SkPaint& paint); |
| 247 | void addClipResources(const MxCp&, Resources* resources); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 248 | void addShaderResources(const SkPaint& paint, Resources* resources); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 249 | void addGradientShaderResources(const SkShader* shader, const SkPaint& paint, |
| 250 | Resources* resources); |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 251 | void addColorFilterResources(const SkColorFilter& cf, Resources* resources); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 252 | void addImageShaderResources(const SkShader* shader, const SkPaint& paint, |
| 253 | Resources* resources); |
| 254 | |
| 255 | void addPatternDef(const SkBitmap& bm); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 256 | |
| 257 | void addPaint(const SkPaint& paint, const Resources& resources); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 258 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 259 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 260 | SkString addLinearGradientDef(const SkShader::GradientInfo& info, const SkShader* shader); |
| 261 | |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 262 | SkXMLWriter* fWriter; |
| 263 | ResourceBucket* fResourceBucket; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 264 | std::unique_ptr<AutoElement> fClipGroup; |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 267 | void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& resources) { |
| 268 | SkPaint::Style style = paint.getStyle(); |
| 269 | if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 270 | this->addAttribute("fill", resources.fPaintServer); |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 271 | |
| 272 | if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 273 | this->addAttribute("fill-opacity", svg_opacity(paint.getColor())); |
| 274 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 275 | } else { |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 276 | SkASSERT(style == SkPaint::kStroke_Style); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 277 | this->addAttribute("fill", "none"); |
| 278 | } |
| 279 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 280 | if (!resources.fColorFilter.isEmpty()) { |
| 281 | this->addAttribute("filter", resources.fColorFilter.c_str()); |
| 282 | } |
| 283 | |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 284 | if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 285 | this->addAttribute("stroke", resources.fPaintServer); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 286 | |
fmalita | 12753cc | 2015-02-04 14:56:35 -0800 | [diff] [blame] | 287 | SkScalar strokeWidth = paint.getStrokeWidth(); |
| 288 | if (strokeWidth == 0) { |
| 289 | // Hairline stroke |
| 290 | strokeWidth = 1; |
| 291 | this->addAttribute("vector-effect", "non-scaling-stroke"); |
| 292 | } |
| 293 | this->addAttribute("stroke-width", strokeWidth); |
| 294 | |
| 295 | if (const char* cap = svg_cap(paint.getStrokeCap())) { |
| 296 | this->addAttribute("stroke-linecap", cap); |
| 297 | } |
| 298 | |
| 299 | if (const char* join = svg_join(paint.getStrokeJoin())) { |
| 300 | this->addAttribute("stroke-linejoin", join); |
| 301 | } |
| 302 | |
| 303 | if (paint.getStrokeJoin() == SkPaint::kMiter_Join) { |
| 304 | this->addAttribute("stroke-miterlimit", paint.getStrokeMiter()); |
| 305 | } |
| 306 | |
| 307 | if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 308 | this->addAttribute("stroke-opacity", svg_opacity(paint.getColor())); |
| 309 | } |
| 310 | } else { |
| 311 | SkASSERT(style == SkPaint::kFill_Style); |
| 312 | this->addAttribute("stroke", "none"); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 316 | Resources SkSVGDevice::AutoElement::addResources(const MxCp& mc, const SkPaint& paint) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 317 | Resources resources(paint); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 318 | |
| 319 | // FIXME: this is a weak heuristic and we end up with LOTS of redundant clips. |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 320 | bool hasClip = !mc.fClipStack->isWideOpen(); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 321 | bool hasShader = SkToBool(paint.getShader()); |
| 322 | |
| 323 | if (hasClip || hasShader) { |
| 324 | AutoElement defs("defs", fWriter); |
| 325 | |
| 326 | if (hasClip) { |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 327 | this->addClipResources(mc, &resources); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | if (hasShader) { |
| 331 | this->addShaderResources(paint, &resources); |
| 332 | } |
| 333 | } |
| 334 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 335 | if (const SkColorFilter* cf = paint.getColorFilter()) { |
| 336 | // TODO: Implement skia color filters for blend modes other than SrcIn |
| 337 | SkBlendMode mode; |
| 338 | if (cf->asColorMode(nullptr, &mode) && mode == SkBlendMode::kSrcIn) { |
| 339 | this->addColorFilterResources(*cf, &resources); |
| 340 | } |
| 341 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 342 | return resources; |
| 343 | } |
| 344 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 345 | void SkSVGDevice::AutoElement::addGradientShaderResources(const SkShader* shader, |
| 346 | const SkPaint& paint, |
| 347 | Resources* resources) { |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 348 | SkShader::GradientInfo grInfo; |
| 349 | grInfo.fColorCount = 0; |
| 350 | if (SkShader::kLinear_GradientType != shader->asAGradient(&grInfo)) { |
| 351 | // TODO: non-linear gradient support |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 352 | return; |
| 353 | } |
| 354 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 355 | SkAutoSTArray<16, SkColor> grColors(grInfo.fColorCount); |
| 356 | SkAutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount); |
| 357 | grInfo.fColors = grColors.get(); |
| 358 | grInfo.fColorOffsets = grOffsets.get(); |
| 359 | |
| 360 | // One more call to get the actual colors/offsets. |
| 361 | shader->asAGradient(&grInfo); |
| 362 | SkASSERT(grInfo.fColorCount <= grColors.count()); |
| 363 | SkASSERT(grInfo.fColorCount <= grOffsets.count()); |
| 364 | |
| 365 | resources->fPaintServer.printf("url(#%s)", addLinearGradientDef(grInfo, shader).c_str()); |
| 366 | } |
| 367 | |
Anas AlJabri | 84dd183 | 2018-07-31 15:53:05 -0700 | [diff] [blame] | 368 | void SkSVGDevice::AutoElement::addColorFilterResources(const SkColorFilter& cf, |
| 369 | Resources* resources) { |
| 370 | SkString colorfilterID = fResourceBucket->addColorFilter(); |
| 371 | { |
| 372 | AutoElement filterElement("filter", fWriter); |
| 373 | filterElement.addAttribute("id", colorfilterID); |
| 374 | filterElement.addAttribute("x", "0%"); |
| 375 | filterElement.addAttribute("y", "0%"); |
| 376 | filterElement.addAttribute("width", "100%"); |
| 377 | filterElement.addAttribute("height", "100%"); |
| 378 | |
| 379 | SkColor filterColor; |
| 380 | SkBlendMode mode; |
| 381 | bool asColorMode = cf.asColorMode(&filterColor, &mode); |
| 382 | SkAssertResult(asColorMode); |
| 383 | SkASSERT(mode == SkBlendMode::kSrcIn); |
| 384 | |
| 385 | { |
| 386 | // first flood with filter color |
| 387 | AutoElement floodElement("feFlood", fWriter); |
| 388 | floodElement.addAttribute("flood-color", svg_color(filterColor)); |
| 389 | floodElement.addAttribute("flood-opacity", svg_opacity(filterColor)); |
| 390 | floodElement.addAttribute("result", "flood"); |
| 391 | } |
| 392 | |
| 393 | { |
| 394 | // apply the transform to filter color |
| 395 | AutoElement compositeElement("feComposite", fWriter); |
| 396 | compositeElement.addAttribute("in", "flood"); |
| 397 | compositeElement.addAttribute("operator", "in"); |
| 398 | } |
| 399 | } |
| 400 | resources->fColorFilter.printf("url(#%s)", colorfilterID.c_str()); |
| 401 | } |
| 402 | |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 403 | // Returns data uri from bytes. |
| 404 | // it will use any cached data if available, otherwise will |
| 405 | // encode as png. |
| 406 | sk_sp<SkData> AsDataUri(SkImage* image) { |
| 407 | sk_sp<SkData> imageData = image->encodeToData(); |
| 408 | if (!imageData) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 409 | return nullptr; |
| 410 | } |
| 411 | |
| 412 | const char* src = (char*)imageData->data(); |
| 413 | const char* selectedPrefix = nullptr; |
| 414 | size_t selectedPrefixLength = 0; |
| 415 | |
| 416 | const static char pngDataPrefix[] = "data:image/png;base64,"; |
| 417 | const static char jpgDataPrefix[] = "data:image/jpeg;base64,"; |
| 418 | |
| 419 | if (SkJpegCodec::IsJpeg(src, imageData->size())) { |
| 420 | selectedPrefix = jpgDataPrefix; |
| 421 | selectedPrefixLength = sizeof(jpgDataPrefix); |
| 422 | } else { |
| 423 | if (!SkPngCodec::IsPng(src, imageData->size())) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 424 | imageData = image->encodeToData(SkEncodedImageFormat::kPNG, 100); |
| 425 | } |
| 426 | selectedPrefix = pngDataPrefix; |
| 427 | selectedPrefixLength = sizeof(pngDataPrefix); |
| 428 | } |
| 429 | |
| 430 | size_t b64Size = SkBase64::Encode(imageData->data(), imageData->size(), nullptr); |
| 431 | sk_sp<SkData> dataUri = SkData::MakeUninitialized(selectedPrefixLength + b64Size); |
| 432 | char* dest = (char*)dataUri->writable_data(); |
| 433 | memcpy(dest, selectedPrefix, selectedPrefixLength); |
| 434 | SkBase64::Encode(imageData->data(), imageData->size(), dest + selectedPrefixLength - 1); |
| 435 | dest[dataUri->size() - 1] = 0; |
| 436 | return dataUri; |
| 437 | } |
| 438 | |
| 439 | void SkSVGDevice::AutoElement::addImageShaderResources(const SkShader* shader, const SkPaint& paint, |
| 440 | Resources* resources) { |
| 441 | SkMatrix outMatrix; |
| 442 | |
| 443 | SkShader::TileMode xy[2]; |
| 444 | SkImage* image = shader->isAImage(&outMatrix, xy); |
| 445 | SkASSERT(image); |
| 446 | |
| 447 | SkString patternDims[2]; // width, height |
| 448 | |
| 449 | sk_sp<SkData> dataUri = AsDataUri(image); |
| 450 | if (!dataUri) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | SkIRect imageSize = image->bounds(); |
| 454 | for (int i = 0; i < 2; i++) { |
| 455 | int imageDimension = i == 0 ? imageSize.width() : imageSize.height(); |
| 456 | switch (xy[i]) { |
| 457 | case SkShader::kRepeat_TileMode: |
| 458 | patternDims[i].appendScalar(imageDimension); |
| 459 | break; |
| 460 | default: |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 461 | // TODO: other tile modes? |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 462 | patternDims[i] = "100%"; |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
| 466 | SkString patternID = fResourceBucket->addPattern(); |
| 467 | { |
| 468 | AutoElement pattern("pattern", fWriter); |
| 469 | pattern.addAttribute("id", patternID); |
| 470 | pattern.addAttribute("patternUnits", "userSpaceOnUse"); |
| 471 | pattern.addAttribute("patternContentUnits", "userSpaceOnUse"); |
| 472 | pattern.addAttribute("width", patternDims[0]); |
| 473 | pattern.addAttribute("height", patternDims[1]); |
| 474 | pattern.addAttribute("x", 0); |
| 475 | pattern.addAttribute("y", 0); |
| 476 | |
| 477 | { |
| 478 | SkString imageID = fResourceBucket->addImage(); |
| 479 | AutoElement imageTag("image", fWriter); |
| 480 | imageTag.addAttribute("id", imageID); |
| 481 | imageTag.addAttribute("x", 0); |
| 482 | imageTag.addAttribute("y", 0); |
| 483 | imageTag.addAttribute("width", image->width()); |
| 484 | imageTag.addAttribute("height", image->height()); |
| 485 | imageTag.addAttribute("xlink:href", static_cast<const char*>(dataUri->data())); |
| 486 | } |
| 487 | } |
| 488 | resources->fPaintServer.printf("url(#%s)", patternID.c_str()); |
| 489 | } |
| 490 | |
| 491 | void SkSVGDevice::AutoElement::addShaderResources(const SkPaint& paint, Resources* resources) { |
| 492 | const SkShader* shader = paint.getShader(); |
| 493 | SkASSERT(shader); |
| 494 | |
| 495 | if (shader->asAGradient(nullptr) != SkShader::kNone_GradientType) { |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 496 | this->addGradientShaderResources(shader, paint, resources); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 497 | } else if (shader->isAImage()) { |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 498 | this->addImageShaderResources(shader, paint, resources); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 499 | } |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 500 | // TODO: other shader types? |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 501 | } |
| 502 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 503 | void SkSVGDevice::AutoElement::addClipResources(const MxCp& mc, Resources* resources) { |
| 504 | SkASSERT(!mc.fClipStack->isWideOpen()); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 505 | |
| 506 | SkPath clipPath; |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 507 | (void) mc.fClipStack->asPath(&clipPath); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 508 | |
| 509 | SkString clipID = fResourceBucket->addClip(); |
| 510 | const char* clipRule = clipPath.getFillType() == SkPath::kEvenOdd_FillType ? |
| 511 | "evenodd" : "nonzero"; |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 512 | { |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 513 | // clipPath is in device space, but since we're only pushing transform attributes |
| 514 | // to the leaf nodes, so are all our elements => SVG userSpaceOnUse == device space. |
| 515 | AutoElement clipPathElement("clipPath", fWriter); |
| 516 | clipPathElement.addAttribute("id", clipID); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 517 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 518 | SkRect clipRect = SkRect::MakeEmpty(); |
| 519 | if (clipPath.isEmpty() || clipPath.isRect(&clipRect)) { |
| 520 | AutoElement rectElement("rect", fWriter); |
| 521 | rectElement.addRectAttributes(clipRect); |
| 522 | rectElement.addAttribute("clip-rule", clipRule); |
| 523 | } else { |
| 524 | AutoElement pathElement("path", fWriter); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 525 | pathElement.addPathAttributes(clipPath); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 526 | pathElement.addAttribute("clip-rule", clipRule); |
| 527 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 528 | } |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 529 | |
| 530 | resources->fClip.printf("url(#%s)", clipID.c_str()); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | SkString SkSVGDevice::AutoElement::addLinearGradientDef(const SkShader::GradientInfo& info, |
| 534 | const SkShader* shader) { |
| 535 | SkASSERT(fResourceBucket); |
| 536 | SkString id = fResourceBucket->addLinearGradient(); |
| 537 | |
| 538 | { |
| 539 | AutoElement gradient("linearGradient", fWriter); |
| 540 | |
| 541 | gradient.addAttribute("id", id); |
| 542 | gradient.addAttribute("gradientUnits", "userSpaceOnUse"); |
| 543 | gradient.addAttribute("x1", info.fPoint[0].x()); |
| 544 | gradient.addAttribute("y1", info.fPoint[0].y()); |
| 545 | gradient.addAttribute("x2", info.fPoint[1].x()); |
| 546 | gradient.addAttribute("y2", info.fPoint[1].y()); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 547 | |
| 548 | if (!shader->getLocalMatrix().isIdentity()) { |
| 549 | this->addAttribute("gradientTransform", svg_transform(shader->getLocalMatrix())); |
| 550 | } |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 551 | |
| 552 | SkASSERT(info.fColorCount >= 2); |
| 553 | for (int i = 0; i < info.fColorCount; ++i) { |
| 554 | SkColor color = info.fColors[i]; |
| 555 | SkString colorStr(svg_color(color)); |
| 556 | |
| 557 | { |
| 558 | AutoElement stop("stop", fWriter); |
| 559 | stop.addAttribute("offset", info.fColorOffsets[i]); |
| 560 | stop.addAttribute("stop-color", colorStr.c_str()); |
| 561 | |
| 562 | if (SK_AlphaOPAQUE != SkColorGetA(color)) { |
| 563 | stop.addAttribute("stop-opacity", svg_opacity(color)); |
| 564 | } |
| 565 | } |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | return id; |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 570 | } |
| 571 | |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 572 | void SkSVGDevice::AutoElement::addRectAttributes(const SkRect& rect) { |
| 573 | // x, y default to 0 |
| 574 | if (rect.x() != 0) { |
| 575 | this->addAttribute("x", rect.x()); |
| 576 | } |
| 577 | if (rect.y() != 0) { |
| 578 | this->addAttribute("y", rect.y()); |
| 579 | } |
| 580 | |
| 581 | this->addAttribute("width", rect.width()); |
| 582 | this->addAttribute("height", rect.height()); |
| 583 | } |
| 584 | |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 585 | void SkSVGDevice::AutoElement::addPathAttributes(const SkPath& path) { |
| 586 | SkString pathData; |
| 587 | SkParsePath::ToSVGString(path, &pathData); |
| 588 | this->addAttribute("d", pathData); |
| 589 | } |
| 590 | |
| 591 | void SkSVGDevice::AutoElement::addTextAttributes(const SkPaint& paint) { |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 592 | this->addAttribute("font-size", paint.getTextSize()); |
| 593 | |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 594 | SkString familyName; |
mtklein | 02f46cf | 2015-03-20 13:48:42 -0700 | [diff] [blame] | 595 | SkTHashSet<SkString> familySet; |
Herb Derby | eb3f674 | 2018-03-05 14:36:45 -0500 | [diff] [blame] | 596 | sk_sp<SkTypeface> tface = SkPaintPriv::RefTypefaceOrDefault(paint); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 597 | |
| 598 | SkASSERT(tface); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 599 | SkFontStyle style = tface->fontStyle(); |
| 600 | if (style.slant() == SkFontStyle::kItalic_Slant) { |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 601 | this->addAttribute("font-style", "italic"); |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 602 | } else if (style.slant() == SkFontStyle::kOblique_Slant) { |
| 603 | this->addAttribute("font-style", "oblique"); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 604 | } |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 605 | int weightIndex = (SkTPin(style.weight(), 100, 900) - 50) / 100; |
| 606 | if (weightIndex != 3) { |
| 607 | static constexpr const char* weights[] = { |
| 608 | "100", "200", "300", "normal", "400", "500", "600", "bold", "800", "900" |
| 609 | }; |
| 610 | this->addAttribute("font-weight", weights[weightIndex]); |
| 611 | } |
| 612 | int stretchIndex = style.width() - 1; |
| 613 | if (stretchIndex != 4) { |
| 614 | static constexpr const char* stretches[] = { |
| 615 | "ultra-condensed", "extra-condensed", "condensed", "semi-condensed", |
| 616 | "normal", |
| 617 | "semi-expanded", "expanded", "extra-expanded", "ultra-expanded" |
| 618 | }; |
| 619 | this->addAttribute("font-stretch", stretches[stretchIndex]); |
fmalita | 7a04869 | 2015-02-20 13:54:40 -0800 | [diff] [blame] | 620 | } |
| 621 | |
Hal Canary | 67b39de | 2016-11-07 11:47:44 -0500 | [diff] [blame] | 622 | sk_sp<SkTypeface::LocalizedStrings> familyNameIter(tface->createFamilyNameIterator()); |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 623 | SkTypeface::LocalizedString familyString; |
Mike Reed | f67c459 | 2017-02-17 17:06:11 -0500 | [diff] [blame] | 624 | if (familyNameIter) { |
| 625 | while (familyNameIter->next(&familyString)) { |
| 626 | if (familySet.contains(familyString.fString)) { |
| 627 | continue; |
| 628 | } |
| 629 | familySet.add(familyString.fString); |
| 630 | familyName.appendf((familyName.isEmpty() ? "%s" : ", %s"), familyString.fString.c_str()); |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 631 | } |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 632 | } |
fmalita | f89f60f | 2015-02-13 08:55:24 -0800 | [diff] [blame] | 633 | if (!familyName.isEmpty()) { |
| 634 | this->addAttribute("font-family", familyName); |
| 635 | } |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 636 | } |
| 637 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 638 | SkBaseDevice* SkSVGDevice::Create(const SkISize& size, SkXMLWriter* writer) { |
| 639 | if (!writer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 640 | return nullptr; |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 641 | } |
| 642 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 643 | return new SkSVGDevice(size, writer); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 644 | } |
| 645 | |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 646 | SkSVGDevice::SkSVGDevice(const SkISize& size, SkXMLWriter* writer) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 647 | : INHERITED(SkImageInfo::MakeUnknown(size.fWidth, size.fHeight), |
| 648 | SkSurfaceProps(0, kUnknown_SkPixelGeometry)) |
robertphillips | 9a53fd7 | 2015-06-22 09:46:59 -0700 | [diff] [blame] | 649 | , fWriter(writer) |
robertphillips | 1f3923e | 2016-07-21 07:17:54 -0700 | [diff] [blame] | 650 | , fResourceBucket(new ResourceBucket) |
reed | 589a39e | 2016-08-20 07:59:19 -0700 | [diff] [blame] | 651 | { |
fmalita | 2aafe6f | 2015-02-06 12:51:10 -0800 | [diff] [blame] | 652 | SkASSERT(writer); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 653 | |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 654 | fWriter->writeHeader(); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 655 | |
| 656 | // The root <svg> tag gets closed by the destructor. |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 657 | fRootElement.reset(new AutoElement("svg", fWriter)); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 658 | |
| 659 | fRootElement->addAttribute("xmlns", "http://www.w3.org/2000/svg"); |
| 660 | fRootElement->addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); |
| 661 | fRootElement->addAttribute("width", size.width()); |
| 662 | fRootElement->addAttribute("height", size.height()); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | SkSVGDevice::~SkSVGDevice() { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 666 | } |
| 667 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 668 | void SkSVGDevice::drawPaint(const SkPaint& paint) { |
| 669 | AutoElement rect("rect", fWriter, fResourceBucket.get(), MxCp(this), paint); |
fmalita | 1a481fe | 2015-02-04 07:39:34 -0800 | [diff] [blame] | 670 | rect.addRectAttributes(SkRect::MakeWH(SkIntToScalar(this->width()), |
| 671 | SkIntToScalar(this->height()))); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 672 | } |
| 673 | |
Bryce Thomas | fd5a508 | 2018-02-06 14:53:05 -0800 | [diff] [blame] | 674 | void SkSVGDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 675 | if (!value) { |
| 676 | return; |
| 677 | } |
| 678 | |
| 679 | if (!strcmp(SkAnnotationKeys::URL_Key(), key) || |
| 680 | !strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
| 681 | this->cs().save(); |
| 682 | this->cs().clipRect(rect, this->ctm(), kIntersect_SkClipOp, true); |
| 683 | SkRect transformedRect = this->cs().bounds(this->getGlobalBounds()); |
| 684 | this->cs().restore(); |
| 685 | if (transformedRect.isEmpty()) { |
| 686 | return; |
| 687 | } |
| 688 | |
| 689 | SkString url(static_cast<const char*>(value->data()), value->size() - 1); |
| 690 | AutoElement a("a", fWriter); |
| 691 | a.addAttribute("xlink:href", url.c_str()); |
| 692 | { |
| 693 | AutoElement r("rect", fWriter); |
| 694 | r.addAttribute("fill-opacity", "0.0"); |
| 695 | r.addRectAttributes(transformedRect); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 700 | void SkSVGDevice::drawPoints(SkCanvas::PointMode mode, size_t count, |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 701 | const SkPoint pts[], const SkPaint& paint) { |
| 702 | SkPath path; |
| 703 | |
| 704 | switch (mode) { |
| 705 | // todo |
| 706 | case SkCanvas::kPoints_PointMode: |
Mike Klein | 6b2b650 | 2018-05-09 10:48:52 -0400 | [diff] [blame] | 707 | // TODO? |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 708 | break; |
| 709 | case SkCanvas::kLines_PointMode: |
| 710 | count -= 1; |
| 711 | for (size_t i = 0; i < count; i += 2) { |
| 712 | path.rewind(); |
| 713 | path.moveTo(pts[i]); |
| 714 | path.lineTo(pts[i+1]); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 715 | AutoElement elem("path", fWriter, fResourceBucket.get(), MxCp(this), paint); |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 716 | elem.addPathAttributes(path); |
| 717 | } |
| 718 | break; |
| 719 | case SkCanvas::kPolygon_PointMode: |
| 720 | if (count > 1) { |
| 721 | path.addPoly(pts, SkToInt(count), false); |
| 722 | path.moveTo(pts[0]); |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 723 | AutoElement elem("path", fWriter, fResourceBucket.get(), MxCp(this), paint); |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 724 | elem.addPathAttributes(path); |
| 725 | } |
| 726 | break; |
| 727 | } |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 728 | } |
| 729 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 730 | void SkSVGDevice::drawRect(const SkRect& r, const SkPaint& paint) { |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 731 | std::unique_ptr<AutoElement> svg; |
| 732 | if (RequiresViewportReset(paint)) { |
| 733 | svg.reset(new AutoElement("svg", fWriter, fResourceBucket.get(), MxCp(this), paint)); |
| 734 | svg->addRectAttributes(r); |
| 735 | } |
| 736 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 737 | AutoElement rect("rect", fWriter, fResourceBucket.get(), MxCp(this), paint); |
Alexander Midlash | 77e3afc | 2018-03-06 17:21:28 -0800 | [diff] [blame] | 738 | |
| 739 | if (svg) { |
| 740 | rect.addAttribute("x", 0); |
| 741 | rect.addAttribute("y", 0); |
| 742 | rect.addAttribute("width", "100%"); |
| 743 | rect.addAttribute("height", "100%"); |
| 744 | } else { |
| 745 | rect.addRectAttributes(r); |
| 746 | } |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 747 | } |
| 748 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 749 | void SkSVGDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 750 | AutoElement ellipse("ellipse", fWriter, fResourceBucket.get(), MxCp(this), paint); |
fmalita | 532faa9 | 2015-02-03 05:44:40 -0800 | [diff] [blame] | 751 | ellipse.addAttribute("cx", oval.centerX()); |
| 752 | ellipse.addAttribute("cy", oval.centerY()); |
| 753 | ellipse.addAttribute("rx", oval.width() / 2); |
| 754 | ellipse.addAttribute("ry", oval.height() / 2); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 755 | } |
| 756 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 757 | void SkSVGDevice::drawRRect(const SkRRect& rr, const SkPaint& paint) { |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 758 | SkPath path; |
| 759 | path.addRRect(rr); |
| 760 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 761 | AutoElement elem("path", fWriter, fResourceBucket.get(), MxCp(this), paint); |
reed | 5f68c34 | 2015-05-15 10:11:11 -0700 | [diff] [blame] | 762 | elem.addPathAttributes(path); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 763 | } |
| 764 | |
Robert Phillips | 137ca52 | 2018-08-15 10:14:33 -0400 | [diff] [blame] | 765 | void SkSVGDevice::drawPath(const SkPath& path, const SkPaint& paint, bool pathIsMutable) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 766 | AutoElement elem("path", fWriter, fResourceBucket.get(), MxCp(this), paint); |
fmalita | a9d9de4 | 2015-02-04 17:54:46 -0800 | [diff] [blame] | 767 | elem.addPathAttributes(path); |
fmalita | 221722b | 2016-09-06 14:37:02 -0700 | [diff] [blame] | 768 | |
| 769 | // TODO: inverse fill types? |
| 770 | if (path.getFillType() == SkPath::kEvenOdd_FillType) { |
| 771 | elem.addAttribute("fill-rule", "evenodd"); |
| 772 | } |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 773 | } |
| 774 | |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 775 | static sk_sp<SkData> encode(const SkBitmap& src) { |
| 776 | SkDynamicMemoryWStream buf; |
| 777 | return SkEncodeImage(&buf, src, SkEncodedImageFormat::kPNG, 80) ? buf.detachAsData() : nullptr; |
| 778 | } |
| 779 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 780 | void SkSVGDevice::drawBitmapCommon(const MxCp& mc, const SkBitmap& bm, const SkPaint& paint) { |
Hal Canary | db68301 | 2016-11-23 08:55:18 -0700 | [diff] [blame] | 781 | sk_sp<SkData> pngData = encode(bm); |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 782 | if (!pngData) { |
| 783 | return; |
| 784 | } |
| 785 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 786 | size_t b64Size = SkBase64::Encode(pngData->data(), pngData->size(), nullptr); |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 787 | SkAutoTMalloc<char> b64Data(b64Size); |
| 788 | SkBase64::Encode(pngData->data(), pngData->size(), b64Data.get()); |
| 789 | |
| 790 | SkString svgImageData("data:image/png;base64,"); |
| 791 | svgImageData.append(b64Data.get(), b64Size); |
| 792 | |
| 793 | SkString imageID = fResourceBucket->addImage(); |
| 794 | { |
| 795 | AutoElement defs("defs", fWriter); |
| 796 | { |
| 797 | AutoElement image("image", fWriter); |
| 798 | image.addAttribute("id", imageID); |
| 799 | image.addAttribute("width", bm.width()); |
| 800 | image.addAttribute("height", bm.height()); |
| 801 | image.addAttribute("xlink:href", svgImageData); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | { |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 806 | AutoElement imageUse("use", fWriter, fResourceBucket.get(), mc, paint); |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 807 | imageUse.addAttribute("xlink:href", SkStringPrintf("#%s", imageID.c_str())); |
| 808 | } |
| 809 | } |
| 810 | |
Hal Canary | b964238 | 2017-06-27 09:58:56 -0400 | [diff] [blame] | 811 | void SkSVGDevice::drawBitmap(const SkBitmap& bitmap, SkScalar x, SkScalar y, |
| 812 | const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 813 | MxCp mc(this); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 814 | SkMatrix adjustedMatrix = *mc.fMatrix; |
Hal Canary | b964238 | 2017-06-27 09:58:56 -0400 | [diff] [blame] | 815 | adjustedMatrix.preTranslate(x, y); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 816 | mc.fMatrix = &adjustedMatrix; |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 817 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 818 | drawBitmapCommon(mc, bitmap, paint); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 819 | } |
| 820 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 821 | void SkSVGDevice::drawSprite(const SkBitmap& bitmap, |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 822 | int x, int y, const SkPaint& paint) { |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 823 | MxCp mc(this); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 824 | SkMatrix adjustedMatrix = *mc.fMatrix; |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 825 | adjustedMatrix.preTranslate(SkIntToScalar(x), SkIntToScalar(y)); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 826 | mc.fMatrix = &adjustedMatrix; |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 827 | |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 828 | drawBitmapCommon(mc, bitmap, paint); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 831 | void SkSVGDevice::drawBitmapRect(const SkBitmap& bm, const SkRect* srcOrNull, |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 832 | const SkRect& dst, const SkPaint& paint, |
reed | 562fe47 | 2015-07-28 07:35:14 -0700 | [diff] [blame] | 833 | SkCanvas::SrcRectConstraint) { |
Mike Reed | f880b68 | 2017-03-10 11:30:44 -0500 | [diff] [blame] | 834 | SkClipStack* cs = &this->cs(); |
| 835 | SkClipStack::AutoRestore ar(cs, false); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 836 | if (srcOrNull && *srcOrNull != SkRect::Make(bm.bounds())) { |
Mike Reed | f880b68 | 2017-03-10 11:30:44 -0500 | [diff] [blame] | 837 | cs->save(); |
| 838 | cs->clipRect(dst, this->ctm(), kIntersect_SkClipOp, paint.isAntiAlias()); |
Mike Reed | c5e641c | 2017-02-17 14:38:11 -0500 | [diff] [blame] | 839 | } |
| 840 | |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 841 | SkMatrix adjustedMatrix; |
| 842 | adjustedMatrix.setRectToRect(srcOrNull ? *srcOrNull : SkRect::Make(bm.bounds()), |
| 843 | dst, |
| 844 | SkMatrix::kFill_ScaleToFit); |
Mike Reed | f880b68 | 2017-03-10 11:30:44 -0500 | [diff] [blame] | 845 | adjustedMatrix.postConcat(this->ctm()); |
fmalita | 827da23 | 2015-02-27 07:44:47 -0800 | [diff] [blame] | 846 | |
Mike Reed | f880b68 | 2017-03-10 11:30:44 -0500 | [diff] [blame] | 847 | drawBitmapCommon(MxCp(&adjustedMatrix, cs), bm, paint); |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 848 | } |
| 849 | |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 850 | class SVGTextBuilder : SkNoncopyable { |
| 851 | public: |
| 852 | SVGTextBuilder(SkPoint origin, const SkGlyphRun& glyphRun) |
| 853 | : fOrigin(origin) |
| 854 | , fLastCharWasWhitespace(true) { // start off in whitespace mode to strip all leadingspace |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 855 | auto runSize = glyphRun.runSize(); |
| 856 | SkAutoSTArray<64, SkUnichar> unichars(runSize); |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame^] | 857 | glyphRun.font().glyphsToUnichars(glyphRun.glyphsIDs().data(), runSize, unichars.get()); |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 858 | auto positions = glyphRun.positions(); |
| 859 | for (size_t i = 0; i < runSize; ++i) { |
| 860 | this->appendUnichar(unichars[i], positions[i]); |
| 861 | } |
| 862 | } |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 863 | |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 864 | const SkString& text() const { return fText; } |
| 865 | const SkString& posX() const { return fPosX; } |
| 866 | const SkString& posY() const { return fPosY; } |
fmalita | fe3f260 | 2015-02-03 17:47:12 -0800 | [diff] [blame] | 867 | |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 868 | private: |
| 869 | void appendUnichar(SkUnichar c, SkPoint position) { |
| 870 | bool discardPos = false; |
| 871 | bool isWhitespace = false; |
| 872 | |
| 873 | switch(c) { |
| 874 | case ' ': |
| 875 | case '\t': |
| 876 | // consolidate whitespace to match SVG's xml:space=default munging |
| 877 | // (http://www.w3.org/TR/SVG/text.html#WhiteSpace) |
| 878 | if (fLastCharWasWhitespace) { |
| 879 | discardPos = true; |
| 880 | } else { |
| 881 | fText.appendUnichar(c); |
| 882 | } |
| 883 | isWhitespace = true; |
| 884 | break; |
| 885 | case '\0': |
| 886 | // SkPaint::glyphsToUnichars() returns \0 for inconvertible glyphs, but these |
| 887 | // are not legal XML characters (http://www.w3.org/TR/REC-xml/#charsets) |
| 888 | discardPos = true; |
| 889 | isWhitespace = fLastCharWasWhitespace; // preserve whitespace consolidation |
| 890 | break; |
| 891 | case '&': |
| 892 | fText.append("&"); |
| 893 | break; |
| 894 | case '"': |
| 895 | fText.append("""); |
| 896 | break; |
| 897 | case '\'': |
| 898 | fText.append("'"); |
| 899 | break; |
| 900 | case '<': |
| 901 | fText.append("<"); |
| 902 | break; |
| 903 | case '>': |
| 904 | fText.append(">"); |
| 905 | break; |
| 906 | default: |
| 907 | fText.appendUnichar(c); |
| 908 | break; |
| 909 | } |
| 910 | |
| 911 | this->advancePos(discardPos, position); |
| 912 | fLastCharWasWhitespace = isWhitespace; |
| 913 | } |
| 914 | |
| 915 | void advancePos(bool discard, SkPoint position) { |
| 916 | if (!discard) { |
| 917 | SkPoint finalPosition = fOrigin + position; |
| 918 | fPosX.appendf("%.8g, ", finalPosition.x()); |
| 919 | fPosY.appendf("%.8g, ", finalPosition.y()); |
| 920 | } |
| 921 | } |
| 922 | |
Robert Phillips | 9407864 | 2018-08-31 07:58:34 -0400 | [diff] [blame] | 923 | const SkPoint fOrigin; |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 924 | |
| 925 | SkString fText, fPosX, fPosY; |
| 926 | bool fLastCharWasWhitespace; |
| 927 | }; |
| 928 | |
| 929 | void SkSVGDevice::drawGlyphRunList(const SkGlyphRunList& glyphRunList) { |
| 930 | |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame^] | 931 | auto processGlyphRun = [this] |
| 932 | (SkPoint origin, const SkGlyphRun& glyphRun, const SkPaint& runPaint) { |
| 933 | SkPaint paint{runPaint}; |
| 934 | glyphRun.font().LEGACY_applyToPaint(&paint); |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 935 | AutoElement elem("text", fWriter, fResourceBucket.get(), MxCp(this), paint); |
| 936 | elem.addTextAttributes(paint); |
| 937 | |
| 938 | SVGTextBuilder builder(origin, glyphRun); |
| 939 | elem.addAttribute("x", builder.posX()); |
| 940 | elem.addAttribute("y", builder.posY()); |
| 941 | elem.addText(builder.text()); |
| 942 | }; |
| 943 | |
| 944 | for (auto& glyphRun : glyphRunList) { |
Herb Derby | 95e1760 | 2018-12-06 17:11:43 -0500 | [diff] [blame^] | 945 | processGlyphRun(glyphRunList.origin(), glyphRun, glyphRunList.paint()); |
Herb Derby | d708bd6 | 2018-08-29 15:59:34 -0400 | [diff] [blame] | 946 | } |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 947 | } |
| 948 | |
Ruiqi Mao | c97a339 | 2018-08-15 10:44:19 -0400 | [diff] [blame] | 949 | void SkSVGDevice::drawVertices(const SkVertices*, const SkVertices::Bone[], int, SkBlendMode, |
Ruiqi Mao | f510149 | 2018-06-29 14:32:21 -0400 | [diff] [blame] | 950 | const SkPaint&) { |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 951 | // todo |
| 952 | } |
| 953 | |
Mike Reed | a136136 | 2017-03-07 09:37:29 -0500 | [diff] [blame] | 954 | void SkSVGDevice::drawDevice(SkBaseDevice*, int x, int y, |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 955 | const SkPaint&) { |
| 956 | // todo |
fmalita | 93957f4 | 2015-01-30 09:03:29 -0800 | [diff] [blame] | 957 | } |