2016-07-27 01:46:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
#include "SkCanvas.h"
|
2017-10-13 18:07:44 +00:00
|
|
|
#include "SkDashPathEffect.h"
|
2016-12-01 18:35:11 +00:00
|
|
|
#include "SkPath.h"
|
2016-08-11 16:16:29 +00:00
|
|
|
#include "SkSVGAttribute.h"
|
2016-09-13 00:06:47 +00:00
|
|
|
#include "SkSVGNode.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
#include "SkSVGRenderContext.h"
|
2016-08-03 17:21:11 +00:00
|
|
|
#include "SkSVGTypes.h"
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-08-03 17:21:11 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::LengthType t) {
|
|
|
|
switch (t) {
|
|
|
|
case SkSVGLengthContext::LengthType::kHorizontal:
|
|
|
|
return viewport.width();
|
|
|
|
case SkSVGLengthContext::LengthType::kVertical:
|
|
|
|
return viewport.height();
|
|
|
|
case SkSVGLengthContext::LengthType::kOther:
|
|
|
|
return SkScalarSqrt(viewport.width() * viewport.height());
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(false); // Not reached.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-08-17 21:51:03 +00:00
|
|
|
// Multipliers for DPI-relative units.
|
|
|
|
constexpr SkScalar kINMultiplier = 1.00f;
|
|
|
|
constexpr SkScalar kPTMultiplier = kINMultiplier / 72.272f;
|
|
|
|
constexpr SkScalar kPCMultiplier = kPTMultiplier * 12;
|
|
|
|
constexpr SkScalar kMMMultiplier = kINMultiplier / 25.4f;
|
|
|
|
constexpr SkScalar kCMMultiplier = kMMMultiplier * 10;
|
|
|
|
|
2016-08-03 17:21:11 +00:00
|
|
|
} // anonymous ns
|
|
|
|
|
|
|
|
SkScalar SkSVGLengthContext::resolve(const SkSVGLength& l, LengthType t) const {
|
|
|
|
switch (l.unit()) {
|
|
|
|
case SkSVGLength::Unit::kNumber:
|
2016-08-11 16:16:29 +00:00
|
|
|
// Fall through.
|
|
|
|
case SkSVGLength::Unit::kPX:
|
2016-08-03 17:21:11 +00:00
|
|
|
return l.value();
|
|
|
|
case SkSVGLength::Unit::kPercentage:
|
|
|
|
return l.value() * length_size_for_type(fViewport, t) / 100;
|
2016-08-17 21:51:03 +00:00
|
|
|
case SkSVGLength::Unit::kCM:
|
|
|
|
return l.value() * fDPI * kCMMultiplier;
|
|
|
|
case SkSVGLength::Unit::kMM:
|
|
|
|
return l.value() * fDPI * kMMMultiplier;
|
|
|
|
case SkSVGLength::Unit::kIN:
|
|
|
|
return l.value() * fDPI * kINMultiplier;
|
|
|
|
case SkSVGLength::Unit::kPT:
|
|
|
|
return l.value() * fDPI * kPTMultiplier;
|
|
|
|
case SkSVGLength::Unit::kPC:
|
|
|
|
return l.value() * fDPI * kPCMultiplier;
|
2016-08-03 17:21:11 +00:00
|
|
|
default:
|
|
|
|
SkDebugf("unsupported unit type: <%d>\n", l.unit());
|
2016-08-11 16:16:29 +00:00
|
|
|
return 0;
|
2016-08-03 17:21:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
SkRect SkSVGLengthContext::resolveRect(const SkSVGLength& x, const SkSVGLength& y,
|
|
|
|
const SkSVGLength& w, const SkSVGLength& h) const {
|
|
|
|
return SkRect::MakeXYWH(
|
|
|
|
this->resolve(x, SkSVGLengthContext::LengthType::kHorizontal),
|
|
|
|
this->resolve(y, SkSVGLengthContext::LengthType::kVertical),
|
|
|
|
this->resolve(w, SkSVGLengthContext::LengthType::kHorizontal),
|
|
|
|
this->resolve(h, SkSVGLengthContext::LengthType::kVertical));
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
namespace {
|
2016-08-08 18:38:55 +00:00
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
SkPaint::Cap toSkCap(const SkSVGLineCap& cap) {
|
|
|
|
switch (cap.type()) {
|
|
|
|
case SkSVGLineCap::Type::kButt:
|
|
|
|
return SkPaint::kButt_Cap;
|
|
|
|
case SkSVGLineCap::Type::kRound:
|
|
|
|
return SkPaint::kRound_Cap;
|
|
|
|
case SkSVGLineCap::Type::kSquare:
|
|
|
|
return SkPaint::kSquare_Cap;
|
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
return SkPaint::kButt_Cap;
|
|
|
|
}
|
2016-08-08 18:38:55 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
SkPaint::Join toSkJoin(const SkSVGLineJoin& join) {
|
|
|
|
switch (join.type()) {
|
|
|
|
case SkSVGLineJoin::Type::kMiter:
|
|
|
|
return SkPaint::kMiter_Join;
|
|
|
|
case SkSVGLineJoin::Type::kRound:
|
|
|
|
return SkPaint::kRound_Join;
|
|
|
|
case SkSVGLineJoin::Type::kBevel:
|
|
|
|
return SkPaint::kBevel_Join;
|
|
|
|
default:
|
|
|
|
SkASSERT(false);
|
|
|
|
return SkPaint::kMiter_Join;
|
|
|
|
}
|
2016-08-08 18:38:55 +00:00
|
|
|
}
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-09-13 00:06:47 +00:00
|
|
|
void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, SkPaint* p) {
|
2016-08-11 16:16:29 +00:00
|
|
|
switch (svgPaint.type()) {
|
|
|
|
case SkSVGPaint::Type::kColor:
|
|
|
|
p->setColor(SkColorSetA(svgPaint.color(), p->getAlpha()));
|
|
|
|
break;
|
2016-09-13 00:06:47 +00:00
|
|
|
case SkSVGPaint::Type::kIRI: {
|
|
|
|
const auto* node = ctx.findNodeById(svgPaint.iri());
|
|
|
|
if (!node || !node->asPaint(ctx, p)) {
|
|
|
|
p->setColor(SK_ColorTRANSPARENT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2016-08-11 16:16:29 +00:00
|
|
|
case SkSVGPaint::Type::kCurrentColor:
|
|
|
|
SkDebugf("unimplemented 'currentColor' paint type");
|
|
|
|
// Fall through.
|
|
|
|
case SkSVGPaint::Type::kNone:
|
|
|
|
// Fall through.
|
|
|
|
case SkSVGPaint::Type::kInherit:
|
|
|
|
break;
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
2016-08-11 16:16:29 +00:00
|
|
|
}
|
2016-07-27 01:46:34 +00:00
|
|
|
|
2016-08-29 12:54:42 +00:00
|
|
|
inline uint8_t opacity_to_alpha(SkScalar o) {
|
|
|
|
return SkTo<uint8_t>(SkScalarRoundToInt(o * 255));
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
// Commit the selected attribute to the paint cache.
|
|
|
|
template <SkSVGAttribute>
|
|
|
|
void commitToPaint(const SkSVGPresentationAttributes&,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext&,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext*);
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kFill>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext& ctx,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
2016-09-13 00:06:47 +00:00
|
|
|
applySvgPaint(ctx, *attrs.fFill.get(), &pctx->fFillPaint);
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStroke>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext& ctx,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
2016-09-13 00:06:47 +00:00
|
|
|
applySvgPaint(ctx, *attrs.fStroke.get(), &pctx->fStrokePaint);
|
2016-08-11 16:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kFillOpacity>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext&,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
2016-08-29 12:54:42 +00:00
|
|
|
pctx->fFillPaint.setAlpha(opacity_to_alpha(*attrs.fFillOpacity.get()));
|
2016-08-11 16:16:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-13 18:07:44 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeDashArray>(const SkSVGPresentationAttributes& attrs,
|
|
|
|
const SkSVGRenderContext& ctx,
|
|
|
|
SkSVGPresentationContext* pctx) {
|
|
|
|
const auto& dashArray = attrs.fStrokeDashArray.get();
|
|
|
|
if (dashArray->type() != SkSVGDashArray::Type::kDashArray) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto count = dashArray->dashArray().count();
|
|
|
|
SkSTArray<128, SkScalar, true> intervals(count);
|
|
|
|
for (const auto& dash : dashArray->dashArray()) {
|
|
|
|
intervals.push_back(ctx.lengthContext().resolve(dash,
|
|
|
|
SkSVGLengthContext::LengthType::kOther));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count & 1) {
|
|
|
|
// If an odd number of values is provided, then the list of values
|
|
|
|
// is repeated to yield an even number of values.
|
|
|
|
intervals.push_back_n(count);
|
|
|
|
memcpy(intervals.begin() + count, intervals.begin(), count);
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT((intervals.count() & 1) == 0);
|
|
|
|
|
2017-10-13 22:18:32 +00:00
|
|
|
const SkScalar phase = ctx.lengthContext().resolve(*pctx->fInherited.fStrokeDashOffset.get(),
|
|
|
|
SkSVGLengthContext::LengthType::kOther);
|
2017-10-13 18:07:44 +00:00
|
|
|
pctx->fStrokePaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(),
|
|
|
|
intervals.count(),
|
|
|
|
phase));
|
|
|
|
}
|
|
|
|
|
2017-10-13 22:18:32 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeDashOffset>(const SkSVGPresentationAttributes&,
|
|
|
|
const SkSVGRenderContext&,
|
|
|
|
SkSVGPresentationContext*) {
|
|
|
|
// Applied via kStrokeDashArray.
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext&,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
|
|
|
const auto& cap = *attrs.fStrokeLineCap.get();
|
|
|
|
if (cap.type() != SkSVGLineCap::Type::kInherit) {
|
|
|
|
pctx->fStrokePaint.setStrokeCap(toSkCap(cap));
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeLineJoin>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext&,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
|
|
|
const auto& join = *attrs.fStrokeLineJoin.get();
|
|
|
|
if (join.type() != SkSVGLineJoin::Type::kInherit) {
|
|
|
|
pctx->fStrokePaint.setStrokeJoin(toSkJoin(join));
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-09 16:57:41 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(const SkSVGPresentationAttributes& attrs,
|
|
|
|
const SkSVGRenderContext&,
|
|
|
|
SkSVGPresentationContext* pctx) {
|
|
|
|
pctx->fStrokePaint.setStrokeMiter(*attrs.fStrokeMiterLimit.get());
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeOpacity>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext&,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
2016-08-29 12:54:42 +00:00
|
|
|
pctx->fStrokePaint.setAlpha(opacity_to_alpha(*attrs.fStrokeOpacity.get()));
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kStrokeWidth>(const SkSVGPresentationAttributes& attrs,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGRenderContext& ctx,
|
2016-08-11 16:16:29 +00:00
|
|
|
SkSVGPresentationContext* pctx) {
|
2016-09-13 00:06:47 +00:00
|
|
|
auto strokeWidth = ctx.lengthContext().resolve(*attrs.fStrokeWidth.get(),
|
|
|
|
SkSVGLengthContext::LengthType::kOther);
|
2016-08-11 16:16:29 +00:00
|
|
|
pctx->fStrokePaint.setStrokeWidth(strokeWidth);
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:35:11 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kFillRule>(const SkSVGPresentationAttributes&,
|
|
|
|
const SkSVGRenderContext&,
|
|
|
|
SkSVGPresentationContext*) {
|
|
|
|
// Not part of the SkPaint state; applied to the path at render time.
|
|
|
|
}
|
|
|
|
|
2017-10-10 15:22:08 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kClipRule>(const SkSVGPresentationAttributes&,
|
|
|
|
const SkSVGRenderContext&,
|
|
|
|
SkSVGPresentationContext*) {
|
|
|
|
// Not part of the SkPaint state; applied to the path at clip time.
|
|
|
|
}
|
|
|
|
|
2017-10-12 15:33:28 +00:00
|
|
|
template <>
|
|
|
|
void commitToPaint<SkSVGAttribute::kVisibility>(const SkSVGPresentationAttributes&,
|
|
|
|
const SkSVGRenderContext&,
|
|
|
|
SkSVGPresentationContext*) {
|
|
|
|
// Not part of the SkPaint state; queried to veto rendering.
|
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
} // anonymous ns
|
|
|
|
|
|
|
|
SkSVGPresentationContext::SkSVGPresentationContext()
|
|
|
|
: fInherited(SkSVGPresentationAttributes::MakeInitial()) {
|
|
|
|
|
|
|
|
fFillPaint.setStyle(SkPaint::kFill_Style);
|
|
|
|
fStrokePaint.setStyle(SkPaint::kStroke_Style);
|
|
|
|
|
|
|
|
// TODO: drive AA off presentation attrs also (shape-rendering?)
|
|
|
|
fFillPaint.setAntiAlias(true);
|
|
|
|
fStrokePaint.setAntiAlias(true);
|
|
|
|
|
|
|
|
// Commit initial values to the paint cache.
|
2016-09-13 00:06:47 +00:00
|
|
|
SkCanvas dummyCanvas(0, 0);
|
|
|
|
SkSVGRenderContext dummy(&dummyCanvas, SkSVGIDMapper(), SkSVGLengthContext(SkSize::Make(0, 0)),
|
|
|
|
*this);
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
commitToPaint<SkSVGAttribute::kFill>(fInherited, dummy, this);
|
|
|
|
commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, dummy, this);
|
|
|
|
commitToPaint<SkSVGAttribute::kStroke>(fInherited, dummy, this);
|
|
|
|
commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, dummy, this);
|
|
|
|
commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, dummy, this);
|
2017-10-09 16:57:41 +00:00
|
|
|
commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(fInherited, dummy, this);
|
2016-08-11 16:16:29 +00:00
|
|
|
commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, dummy, this);
|
|
|
|
commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, dummy, this);
|
2016-07-27 01:46:34 +00:00
|
|
|
}
|
2016-08-08 18:38:55 +00:00
|
|
|
|
|
|
|
SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas,
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGIDMapper& mapper,
|
2016-08-08 18:38:55 +00:00
|
|
|
const SkSVGLengthContext& lctx,
|
|
|
|
const SkSVGPresentationContext& pctx)
|
2016-09-13 00:06:47 +00:00
|
|
|
: fIDMapper(mapper)
|
|
|
|
, fLengthContext(lctx)
|
2016-08-08 18:38:55 +00:00
|
|
|
, fPresentationContext(pctx)
|
|
|
|
, fCanvas(canvas)
|
|
|
|
, fCanvasSaveCount(canvas->getSaveCount()) {}
|
|
|
|
|
|
|
|
SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other)
|
2016-08-11 16:16:29 +00:00
|
|
|
: SkSVGRenderContext(other.fCanvas,
|
2016-09-13 00:06:47 +00:00
|
|
|
other.fIDMapper,
|
2016-08-11 16:16:29 +00:00
|
|
|
*other.fLengthContext,
|
|
|
|
*other.fPresentationContext) {}
|
2016-08-08 18:38:55 +00:00
|
|
|
|
2017-10-11 18:34:33 +00:00
|
|
|
SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other, SkCanvas* canvas)
|
|
|
|
: SkSVGRenderContext(canvas,
|
|
|
|
other.fIDMapper,
|
|
|
|
*other.fLengthContext,
|
|
|
|
*other.fPresentationContext) {}
|
|
|
|
|
2016-08-08 18:38:55 +00:00
|
|
|
SkSVGRenderContext::~SkSVGRenderContext() {
|
|
|
|
fCanvas->restoreToCount(fCanvasSaveCount);
|
|
|
|
}
|
2016-08-11 16:16:29 +00:00
|
|
|
|
2016-09-13 00:06:47 +00:00
|
|
|
const SkSVGNode* SkSVGRenderContext::findNodeById(const SkString& id) const {
|
|
|
|
const auto* v = fIDMapper.find(id);
|
|
|
|
return v ? v->get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2016-09-20 22:45:57 +00:00
|
|
|
void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs,
|
|
|
|
uint32_t flags) {
|
2016-08-11 16:16:29 +00:00
|
|
|
|
|
|
|
#define ApplyLazyInheritedAttribute(ATTR) \
|
|
|
|
do { \
|
|
|
|
/* All attributes should be defined on the inherited context. */ \
|
|
|
|
SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \
|
|
|
|
const auto* value = attrs.f ## ATTR.getMaybeNull(); \
|
|
|
|
if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()) { \
|
|
|
|
/* Update the local attribute value */ \
|
|
|
|
fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \
|
|
|
|
/* Update the cached paints */ \
|
2016-09-13 00:06:47 +00:00
|
|
|
commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \
|
2016-08-11 16:16:29 +00:00
|
|
|
fPresentationContext.writable()); \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
ApplyLazyInheritedAttribute(Fill);
|
|
|
|
ApplyLazyInheritedAttribute(FillOpacity);
|
2016-12-01 18:35:11 +00:00
|
|
|
ApplyLazyInheritedAttribute(FillRule);
|
2017-10-10 15:22:08 +00:00
|
|
|
ApplyLazyInheritedAttribute(ClipRule);
|
2016-08-11 16:16:29 +00:00
|
|
|
ApplyLazyInheritedAttribute(Stroke);
|
2017-10-13 22:18:32 +00:00
|
|
|
ApplyLazyInheritedAttribute(StrokeDashOffset);
|
2017-10-13 18:07:44 +00:00
|
|
|
ApplyLazyInheritedAttribute(StrokeDashArray);
|
2016-08-11 16:16:29 +00:00
|
|
|
ApplyLazyInheritedAttribute(StrokeLineCap);
|
|
|
|
ApplyLazyInheritedAttribute(StrokeLineJoin);
|
2017-10-09 16:57:41 +00:00
|
|
|
ApplyLazyInheritedAttribute(StrokeMiterLimit);
|
2016-08-11 16:16:29 +00:00
|
|
|
ApplyLazyInheritedAttribute(StrokeOpacity);
|
|
|
|
ApplyLazyInheritedAttribute(StrokeWidth);
|
2017-10-12 15:33:28 +00:00
|
|
|
ApplyLazyInheritedAttribute(Visibility);
|
2016-08-11 16:16:29 +00:00
|
|
|
|
|
|
|
#undef ApplyLazyInheritedAttribute
|
2016-08-15 19:45:11 +00:00
|
|
|
|
|
|
|
// Uninherited attributes. Only apply to the current context.
|
|
|
|
|
2016-09-20 22:45:57 +00:00
|
|
|
if (auto* opacity = attrs.fOpacity.getMaybeNull()) {
|
|
|
|
this->applyOpacity(opacity->value(), flags);
|
|
|
|
}
|
2016-12-08 14:26:47 +00:00
|
|
|
|
|
|
|
if (auto* clip = attrs.fClipPath.getMaybeNull()) {
|
|
|
|
this->applyClip(*clip);
|
|
|
|
}
|
2016-09-20 22:45:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SkSVGRenderContext::applyOpacity(SkScalar opacity, uint32_t flags) {
|
|
|
|
if (opacity >= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool hasFill = SkToBool(this->fillPaint());
|
|
|
|
const bool hasStroke = SkToBool(this->strokePaint());
|
|
|
|
|
|
|
|
// We can apply the opacity as paint alpha iif it only affects one atomic draw.
|
|
|
|
// For now, this means a) the target node doesn't have any descendants, and
|
|
|
|
// b) it only has a stroke or a fill (but not both). Going forward, we may need
|
|
|
|
// to refine this heuristic (e.g. to accommodate markers).
|
|
|
|
if ((flags & kLeaf) && (hasFill ^ hasStroke)) {
|
|
|
|
auto* pctx = fPresentationContext.writable();
|
|
|
|
if (hasFill) {
|
|
|
|
pctx->fFillPaint.setAlpha(
|
|
|
|
SkScalarRoundToInt(opacity * pctx->fFillPaint.getAlpha()));
|
|
|
|
} else {
|
|
|
|
pctx->fStrokePaint.setAlpha(
|
|
|
|
SkScalarRoundToInt(opacity * pctx->fStrokePaint.getAlpha()));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Expensive, layer-based fall back.
|
2016-08-15 19:45:11 +00:00
|
|
|
SkPaint opacityPaint;
|
2016-09-20 22:45:57 +00:00
|
|
|
opacityPaint.setAlpha(opacity_to_alpha(opacity));
|
2016-08-15 19:45:11 +00:00
|
|
|
// Balanced in the destructor, via restoreToCount().
|
|
|
|
fCanvas->saveLayer(nullptr, &opacityPaint);
|
|
|
|
}
|
2016-08-11 16:16:29 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:11:16 +00:00
|
|
|
void SkSVGRenderContext::saveOnce() {
|
|
|
|
// The canvas only needs to be saved once, per local SkSVGRenderContext.
|
|
|
|
if (fCanvas->getSaveCount() == fCanvasSaveCount) {
|
|
|
|
fCanvas->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
SkASSERT(fCanvas->getSaveCount() > fCanvasSaveCount);
|
|
|
|
}
|
|
|
|
|
2016-12-08 14:26:47 +00:00
|
|
|
void SkSVGRenderContext::applyClip(const SkSVGClip& clip) {
|
|
|
|
if (clip.type() != SkSVGClip::Type::kIRI) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkSVGNode* clipNode = this->findNodeById(clip.iri());
|
|
|
|
if (!clipNode || clipNode->tag() != SkSVGTag::kClipPath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkPath clipPath = clipNode->asPath(*this);
|
|
|
|
|
2016-12-08 21:04:24 +00:00
|
|
|
// We use the computed clip path in two ways:
|
|
|
|
//
|
|
|
|
// - apply to the current canvas, for drawing
|
|
|
|
// - track in the presentation context, for asPath() composition
|
|
|
|
//
|
|
|
|
// TODO: the two uses are exclusive, avoid canvas churn when non needed.
|
|
|
|
|
2017-10-11 18:11:16 +00:00
|
|
|
this->saveOnce();
|
2016-12-08 14:26:47 +00:00
|
|
|
|
|
|
|
fCanvas->clipPath(clipPath, true);
|
2016-12-08 21:04:24 +00:00
|
|
|
fClipPath.set(clipPath);
|
2016-12-08 14:26:47 +00:00
|
|
|
}
|
|
|
|
|
2016-08-11 16:16:29 +00:00
|
|
|
const SkPaint* SkSVGRenderContext::fillPaint() const {
|
|
|
|
const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill.get()->type();
|
|
|
|
return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPaint : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
const SkPaint* SkSVGRenderContext::strokePaint() const {
|
|
|
|
const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke.get()->type();
|
|
|
|
return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStrokePaint : nullptr;
|
|
|
|
}
|