| 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 "src/svg/SkSVGDevice.h" |
| 9 |
|
| 10 |
#include "include/core/SkBitmap.h" |
| 11 |
#include "include/core/SkBlendMode.h" |
| 12 |
#include "include/core/SkClipOp.h" |
| 13 |
#include "include/core/SkColor.h" |
| 14 |
#include "include/core/SkColorFilter.h" |
| 15 |
#include "include/core/SkData.h" |
| 16 |
#include "include/core/SkDataTable.h" |
| 17 |
#include "include/core/SkFont.h" |
| 18 |
#include "include/core/SkFontStyle.h" |
| 19 |
#include "include/core/SkImage.h" |
| 20 |
#include "include/core/SkImageInfo.h" |
| 21 |
#include "include/core/SkMatrix.h" |
| 22 |
#include "include/core/SkPaint.h" |
| 23 |
#include "include/core/SkPath.h" |
| 24 |
#include "include/core/SkPathBuilder.h" |
| 25 |
#include "include/core/SkPathEffect.h" |
| 26 |
#include "include/core/SkPathTypes.h" |
| 27 |
#include "include/core/SkPathUtils.h" |
| 28 |
#include "include/core/SkPoint.h" |
| 29 |
#include "include/core/SkRRect.h" |
| 30 |
#include "include/core/SkRect.h" |
| 31 |
#include "include/core/SkScalar.h" |
| 32 |
#include "include/core/SkShader.h" |
| 33 |
#include "include/core/SkSize.h" |
| 34 |
#include "include/core/SkSpan.h" |
| 35 |
#include "include/core/SkStream.h" |
| 36 |
#include "include/core/SkString.h" |
| 37 |
#include "include/core/SkSurfaceProps.h" |
| 38 |
#include "include/core/SkTileMode.h" |
| 39 |
#include "include/core/SkTypeface.h" |
| 40 |
#include "include/private/base/SkDebug.h" |
| 41 |
#include "include/private/base/SkNoncopyable.h" |
| 42 |
#include "include/private/base/SkTPin.h" |
| 43 |
#include "include/private/base/SkTemplates.h" |
| 44 |
#include "include/private/base/SkTo.h" |
| 45 |
#include "include/svg/SkSVGCanvas.h" |
| 46 |
#include "src/base/SkBase64.h" |
| 47 |
#include "src/base/SkTLazy.h" |
| 48 |
#include "src/core/SkAnnotationKeys.h" |
| 49 |
#include "src/core/SkClipStack.h" |
| 50 |
#include "src/core/SkDevice.h" |
| 51 |
#include "src/core/SkFontPriv.h" |
| 52 |
#include "src/core/SkTHash.h" |
| 53 |
#include "src/image/SkImage_Base.h" |
| 54 |
#include "src/shaders/SkColorShader.h" |
| 55 |
#include "src/shaders/SkShaderBase.h" |
| 56 |
#include "src/text/GlyphRun.h" |
| 57 |
#include "src/xml/SkXMLWriter.h" |
| 58 |
|
| 59 |
#include <cstring> |
| 60 |
#include <memory> |
| 61 |
#include <string> |
| 62 |
#include <utility> |
| 63 |
|
| 64 |
using namespace skia_private; |
| 65 |
|
| 66 |
class SkBlender; |
| 67 |
class SkMesh; |
| 68 |
class SkVertices; |
| 69 |
struct SkSamplingOptions; |
| 70 |
|
| 71 |
namespace { |
| 72 |
|
| 73 |
static SkString svg_color(SkColor color) { |
| 74 |
// https://www.w3.org/TR/css-color-3/#html4 |
| 75 |
auto named_color = [](SkColor c) -> const char* { |
| 76 |
switch (c & 0xffffff) { |
| 77 |
case 0x000000: return "black"; |
| 78 |
case 0x000080: return "navy"; |
| 79 |
case 0x0000ff: return "blue"; |
| 80 |
case 0x008000: return "green"; |
| 81 |
case 0x008080: return "teal"; |
| 82 |
case 0x00ff00: return "lime"; |
| 83 |
case 0x00ffff: return "aqua"; |
| 84 |
case 0x800000: return "maroon"; |
| 85 |
case 0x800080: return "purple"; |
| 86 |
case 0x808000: return "olive"; |
| 87 |
case 0x808080: return "gray"; |
| 88 |
case 0xc0c0c0: return "silver"; |
| 89 |
case 0xff0000: return "red"; |
| 90 |
case 0xff00ff: return "fuchsia"; |
| 91 |
case 0xffff00: return "yellow"; |
| 92 |
case 0xffffff: return "white"; |
| 93 |
default: break; |
| 94 |
} |
| 95 |
|
| 96 |
return nullptr; |
| 97 |
}; |
| 98 |
|
| 99 |
if (const auto* nc = named_color(color)) { |
| 100 |
return SkString(nc); |
| 101 |
} |
| 102 |
|
| 103 |
uint8_t r = SkColorGetR(color); |
| 104 |
uint8_t g = SkColorGetG(color); |
| 105 |
uint8_t b = SkColorGetB(color); |
| 106 |
|
| 107 |
// Some users care about every byte here, so we'll use hex colors with single-digit channels |
| 108 |
// when possible. |
| 109 |
uint8_t rh = r >> 4; |
| 110 |
uint8_t rl = r & 0xf; |
| 111 |
uint8_t gh = g >> 4; |
| 112 |
uint8_t gl = g & 0xf; |
| 113 |
uint8_t bh = b >> 4; |
| 114 |
uint8_t bl = b & 0xf; |
| 115 |
if ((rh == rl) && (gh == gl) && (bh == bl)) { |
| 116 |
return SkStringPrintf("#%1X%1X%1X", rh, gh, bh); |
| 117 |
} |
| 118 |
|
| 119 |
return SkStringPrintf("#%02X%02X%02X", r, g, b); |
| 120 |
} |
| 121 |
|
| 122 |
static SkScalar svg_opacity(SkColor color) { |
| 123 |
return SkIntToScalar(SkColorGetA(color)) / SK_AlphaOPAQUE; |
| 124 |
} |
| 125 |
|
| 126 |
// Keep in sync with SkPaint::Cap |
| 127 |
static const char* cap_map[] = { |
| 128 |
nullptr, // kButt_Cap (default) |
| 129 |
"round", // kRound_Cap |
| 130 |
"square" // kSquare_Cap |
| 131 |
}; |
| 132 |
static_assert(std::size(cap_map) == SkPaint::kCapCount, "missing_cap_map_entry"); |
| 133 |
|
| 134 |
static const char* svg_cap(SkPaint::Cap cap) { |
| 135 |
SkASSERT(static_cast<size_t>(cap) < std::size(cap_map)); |
| 136 |
return cap_map[cap]; |
| 137 |
} |
| 138 |
|
| 139 |
// Keep in sync with SkPaint::Join |
| 140 |
static const char* join_map[] = { |
| 141 |
nullptr, // kMiter_Join (default) |
| 142 |
"round", // kRound_Join |
| 143 |
"bevel" // kBevel_Join |
| 144 |
}; |
| 145 |
static_assert(std::size(join_map) == SkPaint::kJoinCount, "missing_join_map_entry"); |
| 146 |
|
| 147 |
static const char* svg_join(SkPaint::Join join) { |
| 148 |
SkASSERT(join < std::size(join_map)); |
| 149 |
return join_map[join]; |
| 150 |
} |
| 151 |
|
| 152 |
static SkString svg_transform(const SkMatrix& t) { |
| 153 |
SkASSERT(!t.isIdentity()); |
| 154 |
|
| 155 |
SkString tstr; |
| 156 |
switch (t.getType()) { |
| 157 |
case SkMatrix::kPerspective_Mask: |
| 158 |
// TODO: handle perspective matrices? |
| 159 |
break; |
| 160 |
case SkMatrix::kTranslate_Mask: |
| 161 |
tstr.printf("translate(%g %g)", t.getTranslateX(), t.getTranslateY()); |
| 162 |
break; |
| 163 |
case SkMatrix::kScale_Mask: |
| 164 |
tstr.printf("scale(%g %g)", t.getScaleX(), t.getScaleY()); |
| 165 |
break; |
| 166 |
default: |
| 167 |
// http://www.w3.org/TR/SVG/coords.html#TransformMatrixDefined |
| 168 |
// | a c e | |
| 169 |
// | b d f | |
| 170 |
// | 0 0 1 | |
| 171 |
tstr.printf("matrix(%g %g %g %g %g %g)", |
| 172 |
t.getScaleX(), t.getSkewY(), |
| 173 |
t.getSkewX(), t.getScaleY(), |
| 174 |
t.getTranslateX(), t.getTranslateY()); |
| 175 |
break; |
| 176 |
} |
| 177 |
|
| 178 |
return tstr; |
| 179 |
} |
| 180 |
|
| 181 |
struct Resources { |
| 182 |
Resources(const SkPaint& paint) |
| 183 |
: fPaintServer(svg_color(paint.getColor())) {} |
| 184 |
|
| 185 |
SkString fPaintServer; |
| 186 |
SkString fColorFilter; |
| 187 |
}; |
| 188 |
|
| 189 |
// Determine if the paint requires us to reset the viewport. |
| 190 |
// Currently, we do this whenever the paint shader calls |
| 191 |
// for a repeating image. |
| 192 |
bool RequiresViewportReset(const SkPaint& paint) { |
| 193 |
SkShader* shader = paint.getShader(); |
| 194 |
if (!shader) |
| 195 |
return false; |
| 196 |
|
| 197 |
SkTileMode xy[2]; |
| 198 |
SkImage* image = shader->isAImage(nullptr, xy); |
| 199 |
|
| 200 |
if (!image) |
| 201 |
return false; |
| 202 |
|
| 203 |
for (int i = 0; i < 2; i++) { |
| 204 |
if (xy[i] == SkTileMode::kRepeat) |
| 205 |
return true; |
| 206 |
} |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
void AddPath(const sktext::GlyphRun& glyphRun, const SkPoint& offset, SkPathBuilder* builder) { |
| 211 |
struct Rec { |
| 212 |
SkPathBuilder* fBuilder; |
| 213 |
const SkPoint fOffset; |
| 214 |
const SkPoint* fPos; |
| 215 |
} rec = { builder, offset, glyphRun.positions().data() }; |
| 216 |
|
| 217 |
glyphRun.font().getPaths(glyphRun.glyphsIDs(), |
| 218 |
[](const SkPath* path, const SkMatrix& mx, void* ctx) { |
| 219 |
Rec* rec = reinterpret_cast<Rec*>(ctx); |
| 220 |
if (path) { |
| 221 |
SkMatrix total = mx; |
| 222 |
total.postTranslate(rec->fPos->fX + rec->fOffset.fX, |
| 223 |
rec->fPos->fY + rec->fOffset.fY); |
| 224 |
rec->fBuilder->addPath(*path, total); |
| 225 |
} else { |
| 226 |
// TODO: this is going to drop color emojis. |
| 227 |
} |
| 228 |
rec->fPos += 1; // move to the next glyph's position |
| 229 |
}, &rec); |
| 230 |
} |
| 231 |
|
| 232 |
} // namespace |
| 233 |
|
| 234 |
// For now all this does is serve unique serial IDs, but it will eventually evolve to track |
| 235 |
// and deduplicate resources. |
| 236 |
class SkSVGDevice::ResourceBucket : ::SkNoncopyable { |
| 237 |
public: |
| 238 |
ResourceBucket() |
| 239 |
: fGradientCount(0) |
| 240 |
, fPathCount(0) |
| 241 |
, fImageCount(0) |
| 242 |
, fPatternCount(0) |
| 243 |
, fColorFilterCount(0) {} |
| 244 |
|
| 245 |
SkString addGradient() { |
| 246 |
return SkStringPrintf("gradient_%u", fGradientCount++); |
| 247 |
} |
| 248 |
|
| 249 |
SkString addPath() { |
| 250 |
return SkStringPrintf("path_%u", fPathCount++); |
| 251 |
} |
| 252 |
|
| 253 |
SkString addImage() { |
| 254 |
return SkStringPrintf("img_%u", fImageCount++); |
| 255 |
} |
| 256 |
|
| 257 |
SkString addColorFilter() { return SkStringPrintf("cfilter_%u", fColorFilterCount++); } |
| 258 |
|
| 259 |
SkString addPattern() { |
| 260 |
return SkStringPrintf("pattern_%u", fPatternCount++); |
| 261 |
} |
| 262 |
|
| 263 |
private: |
| 264 |
uint32_t fGradientCount; |
| 265 |
uint32_t fPathCount; |
| 266 |
uint32_t fImageCount; |
| 267 |
uint32_t fPatternCount; |
| 268 |
uint32_t fColorFilterCount; |
| 269 |
}; |
| 270 |
|
| 271 |
struct SkSVGDevice::MxCp { |
| 272 |
const SkMatrix* fMatrix; |
| 273 |
const SkClipStack* fClipStack; |
| 274 |
|
| 275 |
MxCp(const SkMatrix* mx, const SkClipStack* cs) : fMatrix(mx), fClipStack(cs) {} |
| 276 |
MxCp(SkSVGDevice* device) : fMatrix(&device->localToDevice()), fClipStack(&device->cs()) {} |
| 277 |
}; |
| 278 |
|
| 279 |
class SkSVGDevice::AutoElement : ::SkNoncopyable { |
| 280 |
public: |
| 281 |
AutoElement(const char name[], const AutoElement& parent) |
| 282 |
: fWriter(parent.fWriter) |
| 283 |
, fPngEncoder(parent.fPngEncoder) |
| 284 |
, fResourceBucket(nullptr) { |
| 285 |
fWriter->startElement(name); |
| 286 |
} |
| 287 |
|
| 288 |
AutoElement(const char name[], SkSVGDevice* svgdev) |
| 289 |
: fWriter(svgdev->fWriter.get()) |
| 290 |
, fPngEncoder(svgdev->fOpts.pngEncoder) |
| 291 |
, fResourceBucket(nullptr) { |
| 292 |
fWriter->startElement(name); |
| 293 |
} |
| 294 |
|
| 295 |
AutoElement(const char name[], SkSVGDevice* svgdev, |
| 296 |
ResourceBucket* bucket, const MxCp& mc, const SkPaint& paint) |
| 297 |
: fWriter(svgdev->fWriter.get()) |
| 298 |
, fPngEncoder(svgdev->fOpts.pngEncoder) |
| 299 |
, fResourceBucket(bucket) { |
| 300 |
|
| 301 |
svgdev->syncClipStack(*mc.fClipStack); |
| 302 |
Resources res = this->addResources(mc, paint); |
| 303 |
|
| 304 |
fWriter->startElement(name); |
| 305 |
|
| 306 |
this->addPaint(paint, res); |
| 307 |
|
| 308 |
if (!mc.fMatrix->isIdentity()) { |
| 309 |
this->addAttribute("transform", svg_transform(*mc.fMatrix)); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
~AutoElement() { |
| 314 |
fWriter->endElement(); |
| 315 |
} |
| 316 |
|
| 317 |
void addAttribute(const char name[], const char val[]) { |
| 318 |
fWriter->addAttribute(name, val); |
| 319 |
} |
| 320 |
|
| 321 |
void addAttribute(const char name[], const SkString& val) { |
| 322 |
fWriter->addAttribute(name, val.c_str()); |
| 323 |
} |
| 324 |
|
| 325 |
void addAttribute(const char name[], int32_t val) { |
| 326 |
fWriter->addS32Attribute(name, val); |
| 327 |
} |
| 328 |
|
| 329 |
void addAttribute(const char name[], SkScalar val) { |
| 330 |
fWriter->addScalarAttribute(name, val); |
| 331 |
} |
| 332 |
|
| 333 |
void addText(const SkString& text) { |
| 334 |
fWriter->addText(text.c_str(), text.size()); |
| 335 |
} |
| 336 |
|
| 337 |
void addRectAttributes(const SkRect&); |
| 338 |
void addPathAttributes(const SkPath&, SkParsePath::PathEncoding); |
| 339 |
void addTextAttributes(const SkFont&); |
| 340 |
|
| 341 |
private: |
| 342 |
Resources addResources(const MxCp&, const SkPaint& paint); |
| 343 |
void addShaderResources(const SkPaint& paint, Resources* resources); |
| 344 |
void addGradientShaderResources(const SkShader* shader, const SkPaint& paint, |
| 345 |
Resources* resources); |
| 346 |
void addColorFilterResources(const SkColorFilter& cf, Resources* resources); |
| 347 |
void addImageShaderResources(const SkShader* shader, const SkPaint& paint, |
| 348 |
Resources* resources); |
| 349 |
|
| 350 |
void addPatternDef(const SkBitmap& bm); |
| 351 |
|
| 352 |
void addPaint(const SkPaint& paint, const Resources& resources); |
| 353 |
|
| 354 |
SkString addGradientDef(SkShaderBase::GradientType, |
| 355 |
const SkShaderBase::GradientInfo& info, |
| 356 |
const SkShader* shader, |
| 357 |
const SkMatrix& localMatrix); |
| 358 |
|
| 359 |
SkXMLWriter* fWriter = nullptr; |
| 360 |
SkSVGCanvas::EncodePngCallback fPngEncoder = nullptr; |
| 361 |
ResourceBucket* fResourceBucket = nullptr; |
| 362 |
}; |
| 363 |
|
| 364 |
void SkSVGDevice::AutoElement::addPaint(const SkPaint& paint, const Resources& resources) { |
| 365 |
// Path effects are applied to all vector graphics (rects, rrects, ovals, |
| 366 |
// paths etc). This should only happen when a path effect is attached to |
| 367 |
// non-vector graphics (text, image) or a new vector graphics primitive is |
| 368 |
//added that is not handled by base drawPath() routine. |
| 369 |
if (paint.getPathEffect() != nullptr) { |
| 370 |
SkDebugf("Unsupported path effect in addPaint."); |
| 371 |
} |
| 372 |
SkPaint::Style style = paint.getStyle(); |
| 373 |
if (style == SkPaint::kFill_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 374 |
static constexpr char kDefaultFill[] = "black"; |
| 375 |
if (!resources.fPaintServer.equals(kDefaultFill)) { |
| 376 |
this->addAttribute("fill", resources.fPaintServer); |
| 377 |
} |
| 378 |
if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 379 |
this->addAttribute("fill-opacity", svg_opacity(paint.getColor())); |
| 380 |
} |
| 381 |
} else { |
| 382 |
SkASSERT(style == SkPaint::kStroke_Style); |
| 383 |
this->addAttribute("fill", "none"); |
| 384 |
} |
| 385 |
|
| 386 |
if (!resources.fColorFilter.isEmpty()) { |
| 387 |
this->addAttribute("filter", resources.fColorFilter.c_str()); |
| 388 |
} |
| 389 |
|
| 390 |
if (style == SkPaint::kStroke_Style || style == SkPaint::kStrokeAndFill_Style) { |
| 391 |
this->addAttribute("stroke", resources.fPaintServer); |
| 392 |
|
| 393 |
SkScalar strokeWidth = paint.getStrokeWidth(); |
| 394 |
if (strokeWidth == 0) { |
| 395 |
// Hairline stroke |
| 396 |
strokeWidth = 1; |
| 397 |
this->addAttribute("vector-effect", "non-scaling-stroke"); |
| 398 |
} |
| 399 |
this->addAttribute("stroke-width", strokeWidth); |
| 400 |
|
| 401 |
if (const char* cap = svg_cap(paint.getStrokeCap())) { |
| 402 |
this->addAttribute("stroke-linecap", cap); |
| 403 |
} |
| 404 |
|
| 405 |
if (const char* join = svg_join(paint.getStrokeJoin())) { |
| 406 |
this->addAttribute("stroke-linejoin", join); |
| 407 |
} |
| 408 |
|
| 409 |
if (paint.getStrokeJoin() == SkPaint::kMiter_Join) { |
| 410 |
this->addAttribute("stroke-miterlimit", paint.getStrokeMiter()); |
| 411 |
} |
| 412 |
|
| 413 |
if (SK_AlphaOPAQUE != SkColorGetA(paint.getColor())) { |
| 414 |
this->addAttribute("stroke-opacity", svg_opacity(paint.getColor())); |
| 415 |
} |
| 416 |
} else { |
| 417 |
SkASSERT(style == SkPaint::kFill_Style); |
| 418 |
// SVG default stroke value is "none". |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
Resources SkSVGDevice::AutoElement::addResources(const MxCp& mc, const SkPaint& paint) { |
| 423 |
Resources resources(paint); |
| 424 |
|
| 425 |
if (paint.getShader()) { |
| 426 |
AutoElement defs("defs", *this); |
| 427 |
|
| 428 |
this->addShaderResources(paint, &resources); |
| 429 |
} |
| 430 |
|
| 431 |
if (const SkColorFilter* cf = paint.getColorFilter()) { |
| 432 |
// TODO: Implement skia color filters for blend modes other than SrcIn |
| 433 |
SkBlendMode mode; |
| 434 |
if (cf->asAColorMode(nullptr, &mode) && mode == SkBlendMode::kSrcIn) { |
| 435 |
this->addColorFilterResources(*cf, &resources); |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
return resources; |
| 440 |
} |
| 441 |
|
| 442 |
void SkSVGDevice::AutoElement::addGradientShaderResources(const SkShader* shader, |
| 443 |
const SkPaint& paint, |
| 444 |
Resources* resources) { |
| 445 |
SkASSERT(shader); |
| 446 |
|
| 447 |
SkShaderBase::GradientInfo grInfo; |
| 448 |
const auto gradient_type = as_SB(shader)->asGradient(&grInfo); |
| 449 |
|
| 450 |
if (gradient_type != SkShaderBase::GradientType::kLinear && |
| 451 |
gradient_type != SkShaderBase::GradientType::kRadial && |
| 452 |
gradient_type != SkShaderBase::GradientType::kConical) { |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
AutoSTArray<16, SkColor4f> grColors(grInfo.fColorCount); |
| 457 |
AutoSTArray<16, SkScalar> grOffsets(grInfo.fColorCount); |
| 458 |
grInfo.fColors = grColors.get(); |
| 459 |
grInfo.fColorOffsets = grOffsets.get(); |
| 460 |
|
| 461 |
// One more call to get the actual colors/offsets and local matrix. |
| 462 |
SkMatrix localMatrix; |
| 463 |
as_SB(shader)->asGradient(&grInfo, &localMatrix); |
| 464 |
SkASSERT(grInfo.fColorCount <= grColors.count()); |
| 465 |
SkASSERT(grInfo.fColorCount <= grOffsets.count()); |
| 466 |
|
| 467 |
SkASSERT(grColors.size() > 0); |
| 468 |
resources->fPaintServer = SkStringPrintf("url(#%s)", |
| 469 |
addGradientDef(gradient_type, grInfo, shader, localMatrix).c_str()); |
| 470 |
} |
| 471 |
|
| 472 |
void SkSVGDevice::AutoElement::addColorFilterResources(const SkColorFilter& cf, |
| 473 |
Resources* resources) { |
| 474 |
SkString colorfilterID = fResourceBucket->addColorFilter(); |
| 475 |
{ |
| 476 |
AutoElement filterElement("filter", *this); |
| 477 |
filterElement.addAttribute("id", colorfilterID); |
| 478 |
filterElement.addAttribute("x", "0%"); |
| 479 |
filterElement.addAttribute("y", "0%"); |
| 480 |
filterElement.addAttribute("width", "100%"); |
| 481 |
filterElement.addAttribute("height", "100%"); |
| 482 |
|
| 483 |
SkColor filterColor; |
| 484 |
SkBlendMode mode; |
| 485 |
bool asAColorMode = cf.asAColorMode(&filterColor, &mode); |
| 486 |
SkAssertResult(asAColorMode); |
| 487 |
SkASSERT(mode == SkBlendMode::kSrcIn); |
| 488 |
|
| 489 |
{ |
| 490 |
// first flood with filter color |
| 491 |
AutoElement floodElement("feFlood", *this); |
| 492 |
floodElement.addAttribute("flood-color", svg_color(filterColor)); |
| 493 |
floodElement.addAttribute("flood-opacity", svg_opacity(filterColor)); |
| 494 |
floodElement.addAttribute("result", "flood"); |
| 495 |
} |
| 496 |
|
| 497 |
{ |
| 498 |
// apply the transform to filter color |
| 499 |
AutoElement compositeElement("feComposite", *this); |
| 500 |
compositeElement.addAttribute("in", "flood"); |
| 501 |
compositeElement.addAttribute("operator", "in"); |
| 502 |
} |
| 503 |
} |
| 504 |
resources->fColorFilter.printf("url(#%s)", colorfilterID.c_str()); |
| 505 |
} |
| 506 |
|
| 507 |
static bool is_png(const void* bytes, size_t length) { |
| 508 |
static constexpr uint8_t pngSig[] = {0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}; |
| 509 |
return length >= sizeof(pngSig) && !memcmp(bytes, pngSig, sizeof(pngSig)); |
| 510 |
} |
| 511 |
|
| 512 |
static bool is_jpeg(const void* bytes, size_t length) { |
| 513 |
static constexpr uint8_t jpegSig[] = {0xFF, 0xD8, 0xFF}; |
| 514 |
return length >= sizeof(jpegSig) && !memcmp(bytes, jpegSig, sizeof(jpegSig)); |
| 515 |
} |
| 516 |
|
| 517 |
static sk_sp<SkData> EncodePng(const SkPixmap& pixmap, SkSVGCanvas::EncodePngCallback pngEncoder) { |
| 518 |
SkDynamicMemoryWStream stream; |
| 519 |
if (!pngEncoder(&stream, pixmap)) { |
| 520 |
return nullptr; |
| 521 |
} |
| 522 |
|
| 523 |
return stream.detachAsData(); |
| 524 |
} |
| 525 |
|
| 526 |
static sk_sp<SkData> EncodePng(SkImage* image, SkSVGCanvas::EncodePngCallback pngEncoder) { |
| 527 |
// GrDirectContext is nullptr because we shouldn't have any texture-based images passed in. |
| 528 |
sk_sp<SkImage> rasterImage = image->makeRasterImage(nullptr); |
| 529 |
if (!rasterImage) { |
| 530 |
return nullptr; |
| 531 |
} |
| 532 |
|
| 533 |
SkPixmap pixmap; |
| 534 |
if (!rasterImage->peekPixels(&pixmap)) { |
| 535 |
return nullptr; |
| 536 |
} |
| 537 |
|
| 538 |
return EncodePng(pixmap, pngEncoder); |
| 539 |
} |
| 540 |
|
| 541 |
// Returns data uri from bytes. |
| 542 |
// it will use any cached data if available, otherwise will |
| 543 |
// encode as png. |
| 544 |
sk_sp<SkData> AsDataUri(SkImage* image, SkSVGCanvas::EncodePngCallback pngEncoder) { |
| 545 |
static constexpr char jpgDataPrefix[] = "data:image/jpeg;base64,"; |
| 546 |
static constexpr char pngDataPrefix[] = "data:image/png;base64,"; |
| 547 |
|
| 548 |
SkASSERT(!image->isTextureBacked()); |
| 549 |
|
| 550 |
const char* selectedPrefix = pngDataPrefix; |
| 551 |
size_t selectedPrefixLength = sizeof(pngDataPrefix); |
| 552 |
|
| 553 |
sk_sp<const SkData> imageData = image->refEncodedData(); |
| 554 |
if (imageData) { // Already encoded as something |
| 555 |
if (is_jpeg(imageData->data(), imageData->size())) { |
| 556 |
selectedPrefix = jpgDataPrefix; |
| 557 |
selectedPrefixLength = sizeof(jpgDataPrefix); |
| 558 |
} else if (!is_png(imageData->data(), imageData->size())) { |
| 559 |
// re-encode the image as a PNG. |
| 560 |
imageData = EncodePng(image, pngEncoder); |
| 561 |
if (!imageData) { |
| 562 |
return nullptr; |
| 563 |
} |
| 564 |
} |
| 565 |
// else, it's already encoded as a PNG - we don't need to do anything. |
| 566 |
} else { |
| 567 |
// It was not encoded as something, so we need to encode it as a PNG. |
| 568 |
imageData = EncodePng(image, pngEncoder); |
| 569 |
if (!imageData) { |
| 570 |
return nullptr; |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
size_t b64Size = SkBase64::EncodedSize(imageData->size()); |
| 575 |
sk_sp<SkData> dataUri = SkData::MakeUninitialized(selectedPrefixLength + b64Size); |
| 576 |
char* dest = (char*)dataUri->writable_data(); |
| 577 |
memcpy(dest, selectedPrefix, selectedPrefixLength); |
| 578 |
SkBase64::Encode(imageData->data(), imageData->size(), dest + selectedPrefixLength - 1); |
| 579 |
dest[dataUri->size() - 1] = 0; |
| 580 |
return dataUri; |
| 581 |
} |
| 582 |
|
| 583 |
void SkSVGDevice::AutoElement::addImageShaderResources(const SkShader* shader, const SkPaint& paint, |
| 584 |
Resources* resources) { |
| 585 |
SkMatrix outMatrix; |
| 586 |
|
| 587 |
SkTileMode xy[2]; |
| 588 |
SkImage* image = shader->isAImage(&outMatrix, xy); |
| 589 |
SkASSERT(image); |
| 590 |
|
| 591 |
SkString patternDims[2]; // width, height |
| 592 |
|
| 593 |
sk_sp<SkData> dataUri = AsDataUri(image, fPngEncoder); |
| 594 |
if (!dataUri) { |
| 595 |
return; |
| 596 |
} |
| 597 |
SkIRect imageSize = image->bounds(); |
| 598 |
for (int i = 0; i < 2; i++) { |
| 599 |
int imageDimension = i == 0 ? imageSize.width() : imageSize.height(); |
| 600 |
switch (xy[i]) { |
| 601 |
case SkTileMode::kRepeat: |
| 602 |
patternDims[i].appendScalar(imageDimension); |
| 603 |
break; |
| 604 |
default: |
| 605 |
// TODO: other tile modes? |
| 606 |
patternDims[i] = "100%"; |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
SkString patternID = fResourceBucket->addPattern(); |
| 611 |
{ |
| 612 |
AutoElement pattern("pattern", *this); |
| 613 |
pattern.addAttribute("id", patternID); |
| 614 |
pattern.addAttribute("patternUnits", "userSpaceOnUse"); |
| 615 |
pattern.addAttribute("patternContentUnits", "userSpaceOnUse"); |
| 616 |
pattern.addAttribute("width", patternDims[0]); |
| 617 |
pattern.addAttribute("height", patternDims[1]); |
| 618 |
pattern.addAttribute("x", 0); |
| 619 |
pattern.addAttribute("y", 0); |
| 620 |
|
| 621 |
{ |
| 622 |
SkString imageID = fResourceBucket->addImage(); |
| 623 |
AutoElement imageTag("image", *this); |
| 624 |
imageTag.addAttribute("id", imageID); |
| 625 |
imageTag.addAttribute("x", 0); |
| 626 |
imageTag.addAttribute("y", 0); |
| 627 |
imageTag.addAttribute("width", image->width()); |
| 628 |
imageTag.addAttribute("height", image->height()); |
| 629 |
imageTag.addAttribute("xlink:href", static_cast<const char*>(dataUri->data())); |
| 630 |
} |
| 631 |
} |
| 632 |
resources->fPaintServer.printf("url(#%s)", patternID.c_str()); |
| 633 |
} |
| 634 |
|
| 635 |
void SkSVGDevice::AutoElement::addShaderResources(const SkPaint& paint, Resources* resources) { |
| 636 |
const SkShader* shader = paint.getShader(); |
| 637 |
SkASSERT(shader); |
| 638 |
|
| 639 |
if (as_SB(shader)->type() == SkShaderBase::ShaderType::kColor) { |
| 640 |
auto colorShader = static_cast<const SkColorShader*>(shader); |
| 641 |
resources->fPaintServer = svg_color(colorShader->color().toSkColor()); |
| 642 |
} else |
| 643 |
// Checking the shader type is too restrictive, as it rejects local matrix wrappers. |
| 644 |
// Wrappers do forward asGradient() though, so we can reach nested gradients this way. |
| 645 |
if (as_SB(shader)->asGradient() != SkShaderBase::GradientType::kNone) { |
| 646 |
this->addGradientShaderResources(shader, paint, resources); |
| 647 |
} else |
| 648 |
if (shader->isAImage()) { |
| 649 |
this->addImageShaderResources(shader, paint, resources); |
| 650 |
} |
| 651 |
// TODO: other shader types? |
| 652 |
} |
| 653 |
|
| 654 |
SkString SkSVGDevice::AutoElement::addGradientDef(SkShaderBase::GradientType type, |
| 655 |
const SkShaderBase::GradientInfo& info, |
| 656 |
const SkShader* shader, |
| 657 |
const SkMatrix& localMatrix) { |
| 658 |
SkASSERT(fResourceBucket); |
| 659 |
SkString id = fResourceBucket->addGradient(); |
| 660 |
|
| 661 |
SkASSERT(type == SkShaderBase::GradientType::kLinear || |
| 662 |
type == SkShaderBase::GradientType::kRadial || |
| 663 |
type == SkShaderBase::GradientType::kConical); |
| 664 |
|
| 665 |
const char* elem_name = type == SkShaderBase::GradientType::kLinear ? "linearGradient" |
| 666 |
: "radialGradient"; |
| 667 |
{ |
| 668 |
AutoElement gradient(elem_name, *this); |
| 669 |
|
| 670 |
gradient.addAttribute("id", id); |
| 671 |
gradient.addAttribute("gradientUnits", "userSpaceOnUse"); |
| 672 |
|
| 673 |
switch (type) { |
| 674 |
case SkShaderBase::GradientType::kLinear: |
| 675 |
gradient.addAttribute("x1", info.fPoint[0].x()); |
| 676 |
gradient.addAttribute("y1", info.fPoint[0].y()); |
| 677 |
gradient.addAttribute("x2", info.fPoint[1].x()); |
| 678 |
gradient.addAttribute("y2", info.fPoint[1].y()); |
| 679 |
break; |
| 680 |
case SkShaderBase::GradientType::kConical: |
| 681 |
gradient.addAttribute("fx", info.fPoint[1].x()); |
| 682 |
gradient.addAttribute("fy", info.fPoint[1].y()); |
| 683 |
gradient.addAttribute("fr", info.fRadius[1]); |
| 684 |
[[fallthrough]]; |
| 685 |
case SkShaderBase::GradientType::kRadial: |
| 686 |
gradient.addAttribute("cx", info.fPoint[0].x()); |
| 687 |
gradient.addAttribute("cy", info.fPoint[0].y()); |
| 688 |
gradient.addAttribute("r" , info.fRadius[0]); |
| 689 |
break; |
| 690 |
default: |
| 691 |
SkUNREACHABLE; |
| 692 |
} |
| 693 |
|
| 694 |
if (!localMatrix.isIdentity()) { |
| 695 |
this->addAttribute("gradientTransform", svg_transform(localMatrix)); |
| 696 |
} |
| 697 |
|
| 698 |
SkASSERT(info.fColorCount >= 2); |
| 699 |
for (int i = 0; i < info.fColorCount; ++i) { |
| 700 |
SkColor color = info.fColors[i].toSkColor(); |
| 701 |
SkString colorStr(svg_color(color)); |
| 702 |
|
| 703 |
{ |
| 704 |
AutoElement stop("stop", *this); |
| 705 |
stop.addAttribute("offset", info.fColorOffsets[i]); |
| 706 |
stop.addAttribute("stop-color", colorStr.c_str()); |
| 707 |
|
| 708 |
if (SK_AlphaOPAQUE != SkColorGetA(color)) { |
| 709 |
stop.addAttribute("stop-opacity", svg_opacity(color)); |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
return id; |
| 716 |
} |
| 717 |
|
| 718 |
void SkSVGDevice::AutoElement::addRectAttributes(const SkRect& rect) { |
| 719 |
// x, y default to 0 |
| 720 |
if (rect.x() != 0) { |
| 721 |
this->addAttribute("x", rect.x()); |
| 722 |
} |
| 723 |
if (rect.y() != 0) { |
| 724 |
this->addAttribute("y", rect.y()); |
| 725 |
} |
| 726 |
|
| 727 |
this->addAttribute("width", rect.width()); |
| 728 |
this->addAttribute("height", rect.height()); |
| 729 |
} |
| 730 |
|
| 731 |
void SkSVGDevice::AutoElement::addPathAttributes(const SkPath& path, |
| 732 |
SkParsePath::PathEncoding encoding) { |
| 733 |
this->addAttribute("d", SkParsePath::ToSVGString(path, encoding)); |
| 734 |
} |
| 735 |
|
| 736 |
void SkSVGDevice::AutoElement::addTextAttributes(const SkFont& font) { |
| 737 |
this->addAttribute("font-size", font.getSize()); |
| 738 |
|
| 739 |
SkString familyName; |
| 740 |
THashSet<SkString> familySet; |
| 741 |
sk_sp<SkTypeface> tface = font.refTypeface(); |
| 742 |
|
| 743 |
SkASSERT(tface); |
| 744 |
SkFontStyle style = tface->fontStyle(); |
| 745 |
if (style.slant() == SkFontStyle::kItalic_Slant) { |
| 746 |
this->addAttribute("font-style", "italic"); |
| 747 |
} else if (style.slant() == SkFontStyle::kOblique_Slant) { |
| 748 |
this->addAttribute("font-style", "oblique"); |
| 749 |
} |
| 750 |
int weightIndex = (SkTPin(style.weight(), 100, 900) - 50) / 100; |
| 751 |
if (weightIndex != 3) { |
| 752 |
static constexpr const char* weights[] = { |
| 753 |
"100", "200", "300", "normal", "400", "500", "600", "bold", "800", "900" |
| 754 |
}; |
| 755 |
this->addAttribute("font-weight", weights[weightIndex]); |
| 756 |
} |
| 757 |
int stretchIndex = style.width() - 1; |
| 758 |
if (stretchIndex != 4) { |
| 759 |
static constexpr const char* stretches[] = { |
| 760 |
"ultra-condensed", "extra-condensed", "condensed", "semi-condensed", |
| 761 |
"normal", |
| 762 |
"semi-expanded", "expanded", "extra-expanded", "ultra-expanded" |
| 763 |
}; |
| 764 |
this->addAttribute("font-stretch", stretches[stretchIndex]); |
| 765 |
} |
| 766 |
|
| 767 |
sk_sp<SkTypeface::LocalizedStrings> familyNameIter(tface->createFamilyNameIterator()); |
| 768 |
SkTypeface::LocalizedString familyString; |
| 769 |
if (familyNameIter) { |
| 770 |
while (familyNameIter->next(&familyString)) { |
| 771 |
if (familySet.contains(familyString.fString)) { |
| 772 |
continue; |
| 773 |
} |
| 774 |
familySet.add(familyString.fString); |
| 775 |
familyName.appendf((familyName.isEmpty() ? "%s" : ", %s"), |
| 776 |
familyString.fString.c_str()); |
| 777 |
} |
| 778 |
} |
| 779 |
if (!familyName.isEmpty()) { |
| 780 |
this->addAttribute("font-family", familyName); |
| 781 |
} |
| 782 |
} |
| 783 |
|
| 784 |
sk_sp<SkDevice> SkSVGDevice::Make(const SkISize& size, |
| 785 |
std::unique_ptr<SkXMLWriter> writer, |
| 786 |
SkSVGCanvas::Options opts) { |
| 787 |
return writer ? sk_sp<SkDevice>(new SkSVGDevice(size, std::move(writer), opts)) |
| 788 |
: nullptr; |
| 789 |
} |
| 790 |
|
| 791 |
SkSVGDevice::SkSVGDevice(const SkISize& size, |
| 792 |
std::unique_ptr<SkXMLWriter> writer, |
| 793 |
SkSVGCanvas::Options opts) |
| 794 |
: SkClipStackDevice( |
| 795 |
SkImageInfo::MakeUnknown(size.fWidth, size.fHeight), |
| 796 |
SkSurfaceProps()) |
| 797 |
, fWriter(std::move(writer)) |
| 798 |
, fResourceBucket(new ResourceBucket) |
| 799 |
, fOpts(opts) |
| 800 |
{ |
| 801 |
SkASSERT(fWriter); |
| 802 |
|
| 803 |
fWriter->writeHeader(); |
| 804 |
|
| 805 |
// The root <svg> tag gets closed by the destructor. |
| 806 |
fRootElement = std::make_unique<AutoElement>("svg", this); |
| 807 |
|
| 808 |
fRootElement->addAttribute("xmlns", "http://www.w3.org/2000/svg"); |
| 809 |
fRootElement->addAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink"); |
| 810 |
fRootElement->addAttribute("width", size.width()); |
| 811 |
fRootElement->addAttribute("height", size.height()); |
| 812 |
} |
| 813 |
|
| 814 |
SkSVGDevice::~SkSVGDevice() { |
| 815 |
// Pop order is important. |
| 816 |
while (!fClipStack.empty()) { |
| 817 |
fClipStack.pop_back(); |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
SkParsePath::PathEncoding SkSVGDevice::pathEncoding() const { |
| 822 |
return (fOpts.flags & SkSVGCanvas::kRelativePathEncoding_Flag) |
| 823 |
? SkParsePath::PathEncoding::Relative |
| 824 |
: SkParsePath::PathEncoding::Absolute; |
| 825 |
} |
| 826 |
|
| 827 |
void SkSVGDevice::syncClipStack(const SkClipStack& cs) { |
| 828 |
SkClipStack::B2TIter iter(cs); |
| 829 |
|
| 830 |
const SkClipStack::Element* elem; |
| 831 |
int rec_idx = 0; |
| 832 |
|
| 833 |
// First, find/preserve the common bottom. |
| 834 |
while ((elem = iter.next()) && (rec_idx < fClipStack.size())) { |
| 835 |
if (fClipStack[SkToInt(rec_idx)].fGenID != elem->getGenID()) { |
| 836 |
break; |
| 837 |
} |
| 838 |
rec_idx++; |
| 839 |
} |
| 840 |
|
| 841 |
// Discard out-of-date stack top. |
| 842 |
while (fClipStack.size() > rec_idx) { |
| 843 |
fClipStack.pop_back(); |
| 844 |
} |
| 845 |
|
| 846 |
auto define_clip = [this](const SkClipStack::Element* e) { |
| 847 |
const auto cid = SkStringPrintf("cl_%x", e->getGenID()); |
| 848 |
|
| 849 |
AutoElement clip_path("clipPath", this); |
| 850 |
clip_path.addAttribute("id", cid); |
| 851 |
|
| 852 |
// TODO: handle non-intersect clips. |
| 853 |
|
| 854 |
switch (e->getDeviceSpaceType()) { |
| 855 |
case SkClipStack::Element::DeviceSpaceType::kEmpty: { |
| 856 |
// TODO: can we skip this? |
| 857 |
AutoElement rect("rect", this); |
| 858 |
} break; |
| 859 |
case SkClipStack::Element::DeviceSpaceType::kRect: { |
| 860 |
AutoElement rect("rect", this); |
| 861 |
rect.addRectAttributes(e->getDeviceSpaceRect()); |
| 862 |
} break; |
| 863 |
case SkClipStack::Element::DeviceSpaceType::kRRect: { |
| 864 |
// TODO: complex rrect handling? |
| 865 |
const auto& rr = e->getDeviceSpaceRRect(); |
| 866 |
const auto radii = rr.getSimpleRadii(); |
| 867 |
|
| 868 |
AutoElement rrect("rect", this); |
| 869 |
rrect.addRectAttributes(rr.rect()); |
| 870 |
rrect.addAttribute("rx", radii.x()); |
| 871 |
rrect.addAttribute("ry", radii.y()); |
| 872 |
} break; |
| 873 |
case SkClipStack::Element::DeviceSpaceType::kPath: { |
| 874 |
const auto& p = e->getDeviceSpacePath(); |
| 875 |
AutoElement path("path", this); |
| 876 |
path.addPathAttributes(p, this->pathEncoding()); |
| 877 |
if (p.getFillType() == SkPathFillType::kEvenOdd) { |
| 878 |
path.addAttribute("clip-rule", "evenodd"); |
| 879 |
} |
| 880 |
} break; |
| 881 |
case SkClipStack::Element::DeviceSpaceType::kShader: |
| 882 |
// TODO: handle shader clipping, perhaps rasterize and apply as a mask image? |
| 883 |
break; |
| 884 |
} |
| 885 |
|
| 886 |
return cid; |
| 887 |
}; |
| 888 |
|
| 889 |
// Rebuild the top. |
| 890 |
while (elem) { |
| 891 |
const auto cid = define_clip(elem); |
| 892 |
|
| 893 |
auto clip_grp = std::make_unique<AutoElement>("g", this); |
| 894 |
clip_grp->addAttribute("clip-path", SkStringPrintf("url(#%s)", cid.c_str())); |
| 895 |
|
| 896 |
fClipStack.push_back({ std::move(clip_grp), elem->getGenID() }); |
| 897 |
|
| 898 |
elem = iter.next(); |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
void SkSVGDevice::drawPaint(const SkPaint& paint) { |
| 903 |
AutoElement rect("rect", this, fResourceBucket.get(), MxCp(this), paint); |
| 904 |
rect.addRectAttributes(SkRect::MakeWH(SkIntToScalar(this->width()), |
| 905 |
SkIntToScalar(this->height()))); |
| 906 |
} |
| 907 |
|
| 908 |
void SkSVGDevice::drawAnnotation(const SkRect& rect, const char key[], SkData* value) { |
| 909 |
if (!value) { |
| 910 |
return; |
| 911 |
} |
| 912 |
|
| 913 |
if (!strcmp(SkAnnotationKeys::URL_Key(), key) || |
| 914 |
!strcmp(SkAnnotationKeys::Link_Named_Dest_Key(), key)) { |
| 915 |
this->cs().save(); |
| 916 |
this->cs().clipRect(rect, this->localToDevice(), SkClipOp::kIntersect, true); |
| 917 |
SkRect transformedRect = this->cs().bounds(this->getGlobalBounds()); |
| 918 |
this->cs().restore(); |
| 919 |
if (transformedRect.isEmpty()) { |
| 920 |
return; |
| 921 |
} |
| 922 |
|
| 923 |
SkString url(static_cast<const char*>(value->data()), value->size() - 1); |
| 924 |
AutoElement a("a", this); |
| 925 |
a.addAttribute("xlink:href", url.c_str()); |
| 926 |
{ |
| 927 |
AutoElement r("rect", this); |
| 928 |
r.addAttribute("fill-opacity", "0.0"); |
| 929 |
r.addRectAttributes(transformedRect); |
| 930 |
} |
| 931 |
} |
| 932 |
} |
| 933 |
|
| 934 |
void SkSVGDevice::drawPoints(SkCanvas::PointMode mode, SkSpan<const SkPoint> pts, |
| 935 |
const SkPaint& paint) { |
| 936 |
SkPathBuilder path; |
| 937 |
|
| 938 |
const size_t count = pts.size(); |
| 939 |
switch (mode) { |
| 940 |
// todo |
| 941 |
case SkCanvas::kPoints_PointMode: |
| 942 |
for (const auto& pt : pts) { |
| 943 |
path.moveTo(pt); |
| 944 |
path.lineTo(pt); |
| 945 |
} |
| 946 |
break; |
| 947 |
case SkCanvas::kLines_PointMode: |
| 948 |
for (size_t i = 1; i < count; i += 2) { |
| 949 |
path.moveTo(pts[i-1]); |
| 950 |
path.lineTo(pts[i]); |
| 951 |
} |
| 952 |
break; |
| 953 |
case SkCanvas::kPolygon_PointMode: |
| 954 |
if (count > 1) { |
| 955 |
path.addPolygon(pts, false); |
| 956 |
} |
| 957 |
break; |
| 958 |
} |
| 959 |
|
| 960 |
this->drawPath(path.detach(), paint); |
| 961 |
} |
| 962 |
|
| 963 |
void SkSVGDevice::drawRect(const SkRect& r, const SkPaint& paint) { |
| 964 |
if (paint.getPathEffect()) { |
| 965 |
this->drawPath(SkPath::Rect(r), paint); |
| 966 |
return; |
| 967 |
} |
| 968 |
|
| 969 |
std::unique_ptr<AutoElement> svg; |
| 970 |
if (RequiresViewportReset(paint)) { |
| 971 |
svg = std::make_unique<AutoElement>("svg", this, fResourceBucket.get(), MxCp(this), paint); |
| 972 |
svg->addRectAttributes(r); |
| 973 |
} |
| 974 |
|
| 975 |
AutoElement rect("rect", this, fResourceBucket.get(), MxCp(this), paint); |
| 976 |
|
| 977 |
if (svg) { |
| 978 |
rect.addAttribute("x", 0); |
| 979 |
rect.addAttribute("y", 0); |
| 980 |
rect.addAttribute("width", "100%"); |
| 981 |
rect.addAttribute("height", "100%"); |
| 982 |
} else { |
| 983 |
rect.addRectAttributes(r); |
| 984 |
} |
| 985 |
} |
| 986 |
|
| 987 |
void SkSVGDevice::drawOval(const SkRect& oval, const SkPaint& paint) { |
| 988 |
if (paint.getPathEffect()) { |
| 989 |
this->drawPath(SkPath::Oval(oval), paint); |
| 990 |
return; |
| 991 |
} |
| 992 |
|
| 993 |
AutoElement ellipse("ellipse", this, fResourceBucket.get(), MxCp(this), paint); |
| 994 |
ellipse.addAttribute("cx", oval.centerX()); |
| 995 |
ellipse.addAttribute("cy", oval.centerY()); |
| 996 |
ellipse.addAttribute("rx", oval.width() / 2); |
| 997 |
ellipse.addAttribute("ry", oval.height() / 2); |
| 998 |
} |
| 999 |
|
| 1000 |
void SkSVGDevice::drawRRect(const SkRRect& rr, const SkPaint& paint) { |
| 1001 |
if (paint.getPathEffect()) { |
| 1002 |
this->drawPath(SkPath::RRect(rr), paint); |
| 1003 |
return; |
| 1004 |
} |
| 1005 |
|
| 1006 |
AutoElement elem("path", this, fResourceBucket.get(), MxCp(this), paint); |
| 1007 |
elem.addPathAttributes(SkPath::RRect(rr), this->pathEncoding()); |
| 1008 |
} |
| 1009 |
|
| 1010 |
void SkSVGDevice::drawPath(const SkPath& path, const SkPaint& paint) { |
| 1011 |
if (path.isInverseFillType()) { |
| 1012 |
SkDebugf("Inverse path fill type not yet implemented."); |
| 1013 |
return; |
| 1014 |
} |
| 1015 |
|
| 1016 |
SkPath pathStorage; |
| 1017 |
const SkPath* pathPtr = const_cast<SkPath*>(&path); |
| 1018 |
SkTCopyOnFirstWrite<SkPaint> path_paint(paint); |
| 1019 |
|
| 1020 |
// Apply path effect from paint to path. |
| 1021 |
if (path_paint->getPathEffect()) { |
| 1022 |
SkPathBuilder builder; |
| 1023 |
bool fill = skpathutils::FillPathWithPaint(path, *path_paint, &builder); |
| 1024 |
pathStorage = builder.detach(); |
| 1025 |
pathPtr = &pathStorage; |
| 1026 |
if (fill) { |
| 1027 |
// Path should be filled. |
| 1028 |
path_paint.writable()->setStyle(SkPaint::kFill_Style); |
| 1029 |
} else { |
| 1030 |
// Path should be drawn with a hairline (width == 0). |
| 1031 |
path_paint.writable()->setStyle(SkPaint::kStroke_Style); |
| 1032 |
path_paint.writable()->setStrokeWidth(0); |
| 1033 |
} |
| 1034 |
|
| 1035 |
path_paint.writable()->setPathEffect(nullptr); // path effect processed |
| 1036 |
} |
| 1037 |
|
| 1038 |
// Create path element. |
| 1039 |
AutoElement elem("path", this, fResourceBucket.get(), MxCp(this), *path_paint); |
| 1040 |
elem.addPathAttributes(*pathPtr, this->pathEncoding()); |
| 1041 |
|
| 1042 |
// TODO: inverse fill types? |
| 1043 |
if (pathPtr->getFillType() == SkPathFillType::kEvenOdd) { |
| 1044 |
elem.addAttribute("fill-rule", "evenodd"); |
| 1045 |
} |
| 1046 |
} |
| 1047 |
|
| 1048 |
void SkSVGDevice::drawBitmapCommon(const MxCp& mc, const SkBitmap& bm, const SkPaint& paint) { |
| 1049 |
sk_sp<SkData> pngData = EncodePng(bm.pixmap(), fOpts.pngEncoder); |
| 1050 |
if (!pngData) { |
| 1051 |
return; |
| 1052 |
} |
| 1053 |
|
| 1054 |
size_t b64Size = SkBase64::EncodedSize(pngData->size()); |
| 1055 |
AutoTMalloc<char> b64Data(b64Size); |
| 1056 |
SkBase64::Encode(pngData->data(), pngData->size(), b64Data.get()); |
| 1057 |
|
| 1058 |
SkString svgImageData("data:image/png;base64,"); |
| 1059 |
svgImageData.append(b64Data.get(), b64Size); |
| 1060 |
|
| 1061 |
SkString imageID = fResourceBucket->addImage(); |
| 1062 |
{ |
| 1063 |
AutoElement defs("defs", this); |
| 1064 |
{ |
| 1065 |
AutoElement image("image", this); |
| 1066 |
image.addAttribute("id", imageID); |
| 1067 |
image.addAttribute("width", bm.width()); |
| 1068 |
image.addAttribute("height", bm.height()); |
| 1069 |
image.addAttribute("xlink:href", svgImageData); |
| 1070 |
} |
| 1071 |
} |
| 1072 |
|
| 1073 |
{ |
| 1074 |
AutoElement imageUse("use", this, fResourceBucket.get(), mc, paint); |
| 1075 |
imageUse.addAttribute("xlink:href", SkStringPrintf("#%s", imageID.c_str())); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
|
| 1079 |
void SkSVGDevice::drawImageRect(const SkImage* image, const SkRect* src, const SkRect& dst, |
| 1080 |
const SkSamplingOptions& sampling, const SkPaint& paint, |
| 1081 |
SkCanvas::SrcRectConstraint constraint) { |
| 1082 |
SkBitmap bm; |
| 1083 |
// TODO: support gpu images |
| 1084 |
if (!as_IB(image)->getROPixels(nullptr, &bm)) { |
| 1085 |
return; |
| 1086 |
} |
| 1087 |
|
| 1088 |
SkClipStack* cs = &this->cs(); |
| 1089 |
SkClipStack::AutoRestore ar(cs, false); |
| 1090 |
if (src && *src != SkRect::Make(bm.bounds())) { |
| 1091 |
cs->save(); |
| 1092 |
cs->clipRect(dst, this->localToDevice(), SkClipOp::kIntersect, paint.isAntiAlias()); |
| 1093 |
} |
| 1094 |
|
| 1095 |
SkMatrix adjustedMatrix = this->localToDevice() |
| 1096 |
* SkMatrix::RectToRectOrIdentity(src ? *src : SkRect::Make(bm.bounds()), |
| 1097 |
dst); |
| 1098 |
|
| 1099 |
drawBitmapCommon(MxCp(&adjustedMatrix, cs), bm, paint); |
| 1100 |
} |
| 1101 |
|
| 1102 |
class SVGTextBuilder : SkNoncopyable { |
| 1103 |
public: |
| 1104 |
SVGTextBuilder(SkPoint origin, const sktext::GlyphRun& glyphRun) |
| 1105 |
: fOrigin(origin) { |
| 1106 |
auto runSize = glyphRun.runSize(); |
| 1107 |
AutoSTArray<64, SkUnichar> unichars(runSize); |
| 1108 |
SkFontPriv::GlyphsToUnichars(glyphRun.font(), glyphRun.glyphsIDs().data(), |
| 1109 |
runSize, unichars.get()); |
| 1110 |
auto positions = glyphRun.positions(); |
| 1111 |
for (size_t i = 0; i < runSize; ++i) { |
| 1112 |
this->appendUnichar(unichars[i], positions[i]); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
const SkString& text() const { return fText; } |
| 1117 |
const SkString& posX() const { return fPosXStr; } |
| 1118 |
const SkString& posY() const { return fHasConstY ? fConstYStr : fPosYStr; } |
| 1119 |
|
| 1120 |
private: |
| 1121 |
void appendUnichar(SkUnichar c, SkPoint position) { |
| 1122 |
bool discardPos = false; |
| 1123 |
bool isWhitespace = false; |
| 1124 |
|
| 1125 |
switch(c) { |
| 1126 |
case ' ': |
| 1127 |
case '\t': |
| 1128 |
// consolidate whitespace to match SVG's xml:space=default munging |
| 1129 |
// (http://www.w3.org/TR/SVG/text.html#WhiteSpace) |
| 1130 |
if (fLastCharWasWhitespace) { |
| 1131 |
discardPos = true; |
| 1132 |
} else { |
| 1133 |
fText.appendUnichar(c); |
| 1134 |
} |
| 1135 |
isWhitespace = true; |
| 1136 |
break; |
| 1137 |
case '\0': |
| 1138 |
// SkPaint::glyphsToUnichars() returns \0 for inconvertible glyphs, but these |
| 1139 |
// are not legal XML characters (http://www.w3.org/TR/REC-xml/#charsets) |
| 1140 |
discardPos = true; |
| 1141 |
isWhitespace = fLastCharWasWhitespace; // preserve whitespace consolidation |
| 1142 |
break; |
| 1143 |
case '&': |
| 1144 |
fText.append("&"); |
| 1145 |
break; |
| 1146 |
case '"': |
| 1147 |
fText.append("""); |
| 1148 |
break; |
| 1149 |
case '\'': |
| 1150 |
fText.append("'"); |
| 1151 |
break; |
| 1152 |
case '<': |
| 1153 |
fText.append("<"); |
| 1154 |
break; |
| 1155 |
case '>': |
| 1156 |
fText.append(">"); |
| 1157 |
break; |
| 1158 |
default: |
| 1159 |
fText.appendUnichar(c); |
| 1160 |
break; |
| 1161 |
} |
| 1162 |
|
| 1163 |
fLastCharWasWhitespace = isWhitespace; |
| 1164 |
|
| 1165 |
if (discardPos) { |
| 1166 |
return; |
| 1167 |
} |
| 1168 |
|
| 1169 |
position += fOrigin; |
| 1170 |
fPosXStr.appendf("%.8g, ", position.fX); |
| 1171 |
fPosYStr.appendf("%.8g, ", position.fY); |
| 1172 |
|
| 1173 |
if (fConstYStr.isEmpty()) { |
| 1174 |
fConstYStr = fPosYStr; |
| 1175 |
fConstY = position.fY; |
| 1176 |
} else { |
| 1177 |
fHasConstY &= SkScalarNearlyEqual(fConstY, position.fY); |
| 1178 |
} |
| 1179 |
} |
| 1180 |
|
| 1181 |
const SkPoint fOrigin; |
| 1182 |
|
| 1183 |
SkString fText, |
| 1184 |
fPosXStr, fPosYStr, |
| 1185 |
fConstYStr; |
| 1186 |
SkScalar fConstY; |
| 1187 |
bool fLastCharWasWhitespace = true, // start off in whitespace mode to strip leading space |
| 1188 |
fHasConstY = true; |
| 1189 |
}; |
| 1190 |
|
| 1191 |
void SkSVGDevice::onDrawGlyphRunList(SkCanvas* canvas, |
| 1192 |
const sktext::GlyphRunList& glyphRunList, |
| 1193 |
const SkPaint& paint) { |
| 1194 |
SkASSERT(!glyphRunList.hasRSXForm()); |
| 1195 |
const auto draw_as_path = |
| 1196 |
(fOpts.flags & SkSVGCanvas::kConvertTextToPaths_Flag) || paint.getPathEffect(); |
| 1197 |
|
| 1198 |
if (draw_as_path) { |
| 1199 |
// Emit a single <path> element. |
| 1200 |
SkPathBuilder builder; |
| 1201 |
for (auto& glyphRun : glyphRunList) { |
| 1202 |
AddPath(glyphRun, glyphRunList.origin(), &builder); |
| 1203 |
} |
| 1204 |
|
| 1205 |
this->drawPath(builder.detach(), paint); |
| 1206 |
|
| 1207 |
return; |
| 1208 |
} |
| 1209 |
|
| 1210 |
// Emit one <text> element for each run. |
| 1211 |
for (auto& glyphRun : glyphRunList) { |
| 1212 |
AutoElement elem("text", this, fResourceBucket.get(), MxCp(this), paint); |
| 1213 |
elem.addTextAttributes(glyphRun.font()); |
| 1214 |
|
| 1215 |
SVGTextBuilder builder(glyphRunList.origin(), glyphRun); |
| 1216 |
elem.addAttribute("x", builder.posX()); |
| 1217 |
elem.addAttribute("y", builder.posY()); |
| 1218 |
elem.addText(builder.text()); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
|
| 1222 |
void SkSVGDevice::drawVertices(const SkVertices*, sk_sp<SkBlender>, const SkPaint&, bool) { |
| 1223 |
// todo |
| 1224 |
} |
| 1225 |
|
| 1226 |
void SkSVGDevice::drawMesh(const SkMesh&, sk_sp<SkBlender>, const SkPaint&) { |
| 1227 |
// todo |
| 1228 |
} |