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