blob: 759c5cf902695db29070147c86cf3bd712999429 [file] [log] [blame]
fmalita6ceef3d2016-07-26 18:46:34 -07001/*
2 * Copyright 2016 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
Florin Malitab3418102020-10-15 18:10:29 -04008#include "modules/svg/include/SkSVGRenderContext.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkPath.h"
12#include "include/effects/SkDashPathEffect.h"
13#include "include/private/SkTo.h"
Florin Malitab3418102020-10-15 18:10:29 -040014#include "modules/svg/include/SkSVGAttribute.h"
15#include "modules/svg/include/SkSVGNode.h"
16#include "modules/svg/include/SkSVGTypes.h"
fmalita6ceef3d2016-07-26 18:46:34 -070017
fmalitabffc2562016-08-03 10:21:11 -070018namespace {
19
20SkScalar length_size_for_type(const SkSize& viewport, SkSVGLengthContext::LengthType t) {
21 switch (t) {
22 case SkSVGLengthContext::LengthType::kHorizontal:
23 return viewport.width();
24 case SkSVGLengthContext::LengthType::kVertical:
25 return viewport.height();
26 case SkSVGLengthContext::LengthType::kOther:
27 return SkScalarSqrt(viewport.width() * viewport.height());
28 }
29
30 SkASSERT(false); // Not reached.
31 return 0;
32}
33
fmalita280e2822016-08-17 14:51:03 -070034// Multipliers for DPI-relative units.
35constexpr SkScalar kINMultiplier = 1.00f;
36constexpr SkScalar kPTMultiplier = kINMultiplier / 72.272f;
37constexpr SkScalar kPCMultiplier = kPTMultiplier * 12;
38constexpr SkScalar kMMMultiplier = kINMultiplier / 25.4f;
39constexpr SkScalar kCMMultiplier = kMMMultiplier * 10;
40
John Stilesa6841be2020-08-06 14:11:56 -040041} // namespace
fmalitabffc2562016-08-03 10:21:11 -070042
43SkScalar SkSVGLengthContext::resolve(const SkSVGLength& l, LengthType t) const {
44 switch (l.unit()) {
45 case SkSVGLength::Unit::kNumber:
fmalita2d961e02016-08-11 09:16:29 -070046 // Fall through.
47 case SkSVGLength::Unit::kPX:
fmalitabffc2562016-08-03 10:21:11 -070048 return l.value();
fmalitabffc2562016-08-03 10:21:11 -070049 case SkSVGLength::Unit::kPercentage:
50 return l.value() * length_size_for_type(fViewport, t) / 100;
fmalita280e2822016-08-17 14:51:03 -070051 case SkSVGLength::Unit::kCM:
52 return l.value() * fDPI * kCMMultiplier;
53 case SkSVGLength::Unit::kMM:
54 return l.value() * fDPI * kMMMultiplier;
55 case SkSVGLength::Unit::kIN:
56 return l.value() * fDPI * kINMultiplier;
57 case SkSVGLength::Unit::kPT:
58 return l.value() * fDPI * kPTMultiplier;
59 case SkSVGLength::Unit::kPC:
60 return l.value() * fDPI * kPCMultiplier;
fmalitabffc2562016-08-03 10:21:11 -070061 default:
62 SkDebugf("unsupported unit type: <%d>\n", l.unit());
fmalita2d961e02016-08-11 09:16:29 -070063 return 0;
fmalitabffc2562016-08-03 10:21:11 -070064 }
fmalitabffc2562016-08-03 10:21:11 -070065}
66
fmalita397a5172016-08-08 11:38:55 -070067SkRect SkSVGLengthContext::resolveRect(const SkSVGLength& x, const SkSVGLength& y,
68 const SkSVGLength& w, const SkSVGLength& h) const {
69 return SkRect::MakeXYWH(
70 this->resolve(x, SkSVGLengthContext::LengthType::kHorizontal),
71 this->resolve(y, SkSVGLengthContext::LengthType::kVertical),
72 this->resolve(w, SkSVGLengthContext::LengthType::kHorizontal),
73 this->resolve(h, SkSVGLengthContext::LengthType::kVertical));
74}
fmalita6ceef3d2016-07-26 18:46:34 -070075
fmalita2d961e02016-08-11 09:16:29 -070076namespace {
fmalita397a5172016-08-08 11:38:55 -070077
fmalita2d961e02016-08-11 09:16:29 -070078SkPaint::Cap toSkCap(const SkSVGLineCap& cap) {
79 switch (cap.type()) {
80 case SkSVGLineCap::Type::kButt:
81 return SkPaint::kButt_Cap;
82 case SkSVGLineCap::Type::kRound:
83 return SkPaint::kRound_Cap;
84 case SkSVGLineCap::Type::kSquare:
85 return SkPaint::kSquare_Cap;
86 default:
87 SkASSERT(false);
88 return SkPaint::kButt_Cap;
fmalita6ceef3d2016-07-26 18:46:34 -070089 }
fmalita6ceef3d2016-07-26 18:46:34 -070090}
91
fmalita2d961e02016-08-11 09:16:29 -070092SkPaint::Join toSkJoin(const SkSVGLineJoin& join) {
93 switch (join.type()) {
94 case SkSVGLineJoin::Type::kMiter:
95 return SkPaint::kMiter_Join;
96 case SkSVGLineJoin::Type::kRound:
97 return SkPaint::kRound_Join;
98 case SkSVGLineJoin::Type::kBevel:
99 return SkPaint::kBevel_Join;
100 default:
101 SkASSERT(false);
102 return SkPaint::kMiter_Join;
fmalita6ceef3d2016-07-26 18:46:34 -0700103 }
fmalita6ceef3d2016-07-26 18:46:34 -0700104}
105
fmalita28d5b722016-09-12 17:06:47 -0700106void applySvgPaint(const SkSVGRenderContext& ctx, const SkSVGPaint& svgPaint, SkPaint* p) {
fmalita2d961e02016-08-11 09:16:29 -0700107 switch (svgPaint.type()) {
108 case SkSVGPaint::Type::kColor:
109 p->setColor(SkColorSetA(svgPaint.color(), p->getAlpha()));
110 break;
fmalita28d5b722016-09-12 17:06:47 -0700111 case SkSVGPaint::Type::kIRI: {
Florin Malita27aeb122020-04-08 15:02:02 -0400112 const auto node = ctx.findNodeById(svgPaint.iri());
fmalita28d5b722016-09-12 17:06:47 -0700113 if (!node || !node->asPaint(ctx, p)) {
114 p->setColor(SK_ColorTRANSPARENT);
115 }
116 break;
117 }
fmalita2d961e02016-08-11 09:16:29 -0700118 case SkSVGPaint::Type::kCurrentColor:
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400119 p->setColor(*ctx.presentationContext().fInherited.fColor);
120 break;
fmalita2d961e02016-08-11 09:16:29 -0700121 case SkSVGPaint::Type::kNone:
122 // Fall through.
123 case SkSVGPaint::Type::kInherit:
124 break;
fmalita6ceef3d2016-07-26 18:46:34 -0700125 }
fmalita6ceef3d2016-07-26 18:46:34 -0700126}
127
fmalitaa26cab02016-08-29 05:54:42 -0700128inline uint8_t opacity_to_alpha(SkScalar o) {
129 return SkTo<uint8_t>(SkScalarRoundToInt(o * 255));
130}
131
fmalita2d961e02016-08-11 09:16:29 -0700132// Commit the selected attribute to the paint cache.
133template <SkSVGAttribute>
134void commitToPaint(const SkSVGPresentationAttributes&,
fmalita28d5b722016-09-12 17:06:47 -0700135 const SkSVGRenderContext&,
fmalita2d961e02016-08-11 09:16:29 -0700136 SkSVGPresentationContext*);
137
138template <>
139void commitToPaint<SkSVGAttribute::kFill>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700140 const SkSVGRenderContext& ctx,
fmalita2d961e02016-08-11 09:16:29 -0700141 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400142 const auto& fill = *attrs.fFill;
Florin Malita09294252020-04-09 08:54:29 -0400143 SkASSERT(fill.type() != SkSVGPaint::Type::kInherit);
144
145 applySvgPaint(ctx, fill, &pctx->fFillPaint);
fmalita6ceef3d2016-07-26 18:46:34 -0700146}
147
fmalita2d961e02016-08-11 09:16:29 -0700148template <>
149void commitToPaint<SkSVGAttribute::kStroke>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700150 const SkSVGRenderContext& ctx,
fmalita2d961e02016-08-11 09:16:29 -0700151 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400152 const auto& stroke = *attrs.fStroke;
Florin Malita09294252020-04-09 08:54:29 -0400153 SkASSERT(stroke.type() != SkSVGPaint::Type::kInherit);
154
155 applySvgPaint(ctx, stroke, &pctx->fStrokePaint);
fmalita2d961e02016-08-11 09:16:29 -0700156}
157
158template <>
159void commitToPaint<SkSVGAttribute::kFillOpacity>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700160 const SkSVGRenderContext&,
fmalita2d961e02016-08-11 09:16:29 -0700161 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400162 pctx->fFillPaint.setAlpha(opacity_to_alpha(*attrs.fFillOpacity));
fmalita2d961e02016-08-11 09:16:29 -0700163}
164
165template <>
Florin Malitaf543a602017-10-13 14:07:44 -0400166void commitToPaint<SkSVGAttribute::kStrokeDashArray>(const SkSVGPresentationAttributes& attrs,
167 const SkSVGRenderContext& ctx,
168 SkSVGPresentationContext* pctx) {
169 const auto& dashArray = attrs.fStrokeDashArray.get();
Florin Malita09294252020-04-09 08:54:29 -0400170 SkASSERT(dashArray->type() != SkSVGDashArray::Type::kInherit);
171
Florin Malitaf543a602017-10-13 14:07:44 -0400172 if (dashArray->type() != SkSVGDashArray::Type::kDashArray) {
173 return;
174 }
175
176 const auto count = dashArray->dashArray().count();
177 SkSTArray<128, SkScalar, true> intervals(count);
178 for (const auto& dash : dashArray->dashArray()) {
179 intervals.push_back(ctx.lengthContext().resolve(dash,
180 SkSVGLengthContext::LengthType::kOther));
181 }
182
183 if (count & 1) {
184 // If an odd number of values is provided, then the list of values
185 // is repeated to yield an even number of values.
186 intervals.push_back_n(count);
Florin Malita5076bd22020-04-09 07:41:52 -0400187 memcpy(intervals.begin() + count, intervals.begin(), count * sizeof(SkScalar));
Florin Malitaf543a602017-10-13 14:07:44 -0400188 }
189
190 SkASSERT((intervals.count() & 1) == 0);
191
John Stilesa008b0f2020-08-16 08:48:02 -0400192 const SkScalar phase = ctx.lengthContext().resolve(*pctx->fInherited.fStrokeDashOffset,
Florin Malitae1dadd72017-10-13 18:18:32 -0400193 SkSVGLengthContext::LengthType::kOther);
Florin Malitaf543a602017-10-13 14:07:44 -0400194 pctx->fStrokePaint.setPathEffect(SkDashPathEffect::Make(intervals.begin(),
195 intervals.count(),
196 phase));
197}
198
199template <>
Florin Malitae1dadd72017-10-13 18:18:32 -0400200void commitToPaint<SkSVGAttribute::kStrokeDashOffset>(const SkSVGPresentationAttributes&,
201 const SkSVGRenderContext&,
202 SkSVGPresentationContext*) {
203 // Applied via kStrokeDashArray.
204}
205
206template <>
fmalita2d961e02016-08-11 09:16:29 -0700207void commitToPaint<SkSVGAttribute::kStrokeLineCap>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700208 const SkSVGRenderContext&,
fmalita2d961e02016-08-11 09:16:29 -0700209 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400210 const auto& cap = *attrs.fStrokeLineCap;
Florin Malita09294252020-04-09 08:54:29 -0400211 SkASSERT(cap.type() != SkSVGLineCap::Type::kInherit);
212
213 pctx->fStrokePaint.setStrokeCap(toSkCap(cap));
fmalita2d961e02016-08-11 09:16:29 -0700214}
215
216template <>
217void commitToPaint<SkSVGAttribute::kStrokeLineJoin>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700218 const SkSVGRenderContext&,
fmalita2d961e02016-08-11 09:16:29 -0700219 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400220 const auto& join = *attrs.fStrokeLineJoin;
Florin Malita09294252020-04-09 08:54:29 -0400221 SkASSERT(join.type() != SkSVGLineJoin::Type::kInherit);
222
223 pctx->fStrokePaint.setStrokeJoin(toSkJoin(join));
fmalita2d961e02016-08-11 09:16:29 -0700224}
225
226template <>
Florin Malita4de426b2017-10-09 12:57:41 -0400227void commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(const SkSVGPresentationAttributes& attrs,
228 const SkSVGRenderContext&,
229 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400230 pctx->fStrokePaint.setStrokeMiter(*attrs.fStrokeMiterLimit);
Florin Malita4de426b2017-10-09 12:57:41 -0400231}
232
233template <>
fmalita2d961e02016-08-11 09:16:29 -0700234void commitToPaint<SkSVGAttribute::kStrokeOpacity>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700235 const SkSVGRenderContext&,
fmalita2d961e02016-08-11 09:16:29 -0700236 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400237 pctx->fStrokePaint.setAlpha(opacity_to_alpha(*attrs.fStrokeOpacity));
fmalita2d961e02016-08-11 09:16:29 -0700238}
239
240template <>
241void commitToPaint<SkSVGAttribute::kStrokeWidth>(const SkSVGPresentationAttributes& attrs,
fmalita28d5b722016-09-12 17:06:47 -0700242 const SkSVGRenderContext& ctx,
fmalita2d961e02016-08-11 09:16:29 -0700243 SkSVGPresentationContext* pctx) {
John Stilesa008b0f2020-08-16 08:48:02 -0400244 auto strokeWidth = ctx.lengthContext().resolve(*attrs.fStrokeWidth,
fmalita28d5b722016-09-12 17:06:47 -0700245 SkSVGLengthContext::LengthType::kOther);
fmalita2d961e02016-08-11 09:16:29 -0700246 pctx->fStrokePaint.setStrokeWidth(strokeWidth);
247}
248
Florin Malitae932d4b2016-12-01 13:35:11 -0500249template <>
250void commitToPaint<SkSVGAttribute::kFillRule>(const SkSVGPresentationAttributes&,
251 const SkSVGRenderContext&,
252 SkSVGPresentationContext*) {
253 // Not part of the SkPaint state; applied to the path at render time.
254}
255
Florin Malita57a0edf2017-10-10 11:22:08 -0400256template <>
257void commitToPaint<SkSVGAttribute::kClipRule>(const SkSVGPresentationAttributes&,
258 const SkSVGRenderContext&,
259 SkSVGPresentationContext*) {
260 // Not part of the SkPaint state; applied to the path at clip time.
261}
262
Florin Malitaffe6ae42017-10-12 11:33:28 -0400263template <>
264void commitToPaint<SkSVGAttribute::kVisibility>(const SkSVGPresentationAttributes&,
265 const SkSVGRenderContext&,
266 SkSVGPresentationContext*) {
267 // Not part of the SkPaint state; queried to veto rendering.
268}
269
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400270template <>
271void commitToPaint<SkSVGAttribute::kColor>(const SkSVGPresentationAttributes&,
272 const SkSVGRenderContext&,
273 SkSVGPresentationContext*) {
274 // Not part of the SkPaint state; applied via 'currentColor' color value
275}
276
Florin Malita39fe8c82020-10-20 10:43:03 -0400277template <>
278void commitToPaint<SkSVGAttribute::kFontFamily>(const SkSVGPresentationAttributes&,
279 const SkSVGRenderContext&,
280 SkSVGPresentationContext*) {
281 // Not part of the SkPaint state; applied at render time.
282}
283
284template <>
285void commitToPaint<SkSVGAttribute::kFontSize>(const SkSVGPresentationAttributes&,
286 const SkSVGRenderContext&,
287 SkSVGPresentationContext*) {
288 // Not part of the SkPaint state; applied at render time.
289}
290
291template <>
292void commitToPaint<SkSVGAttribute::kFontStyle>(const SkSVGPresentationAttributes&,
293 const SkSVGRenderContext&,
294 SkSVGPresentationContext*) {
295 // Not part of the SkPaint state; applied at render time.
296}
297
298template <>
299void commitToPaint<SkSVGAttribute::kFontWeight>(const SkSVGPresentationAttributes&,
300 const SkSVGRenderContext&,
301 SkSVGPresentationContext*) {
302 // Not part of the SkPaint state; applied at render time.
303}
304
John Stilesa6841be2020-08-06 14:11:56 -0400305} // namespace
fmalita2d961e02016-08-11 09:16:29 -0700306
307SkSVGPresentationContext::SkSVGPresentationContext()
308 : fInherited(SkSVGPresentationAttributes::MakeInitial()) {
309
310 fFillPaint.setStyle(SkPaint::kFill_Style);
311 fStrokePaint.setStyle(SkPaint::kStroke_Style);
312
313 // TODO: drive AA off presentation attrs also (shape-rendering?)
314 fFillPaint.setAntiAlias(true);
315 fStrokePaint.setAntiAlias(true);
316
317 // Commit initial values to the paint cache.
Leon Scroggins III577536a2020-07-31 13:47:50 -0400318 SkCanvas fakeCanvas(0, 0);
319 SkSVGRenderContext fake(&fakeCanvas, SkSVGIDMapper(), SkSVGLengthContext(SkSize::Make(0, 0)),
fmalita28d5b722016-09-12 17:06:47 -0700320 *this);
321
Leon Scroggins III577536a2020-07-31 13:47:50 -0400322 commitToPaint<SkSVGAttribute::kFill>(fInherited, fake, this);
323 commitToPaint<SkSVGAttribute::kFillOpacity>(fInherited, fake, this);
324 commitToPaint<SkSVGAttribute::kStroke>(fInherited, fake, this);
325 commitToPaint<SkSVGAttribute::kStrokeLineCap>(fInherited, fake, this);
326 commitToPaint<SkSVGAttribute::kStrokeLineJoin>(fInherited, fake, this);
327 commitToPaint<SkSVGAttribute::kStrokeMiterLimit>(fInherited, fake, this);
328 commitToPaint<SkSVGAttribute::kStrokeOpacity>(fInherited, fake, this);
329 commitToPaint<SkSVGAttribute::kStrokeWidth>(fInherited, fake, this);
fmalita6ceef3d2016-07-26 18:46:34 -0700330}
fmalita397a5172016-08-08 11:38:55 -0700331
332SkSVGRenderContext::SkSVGRenderContext(SkCanvas* canvas,
fmalita28d5b722016-09-12 17:06:47 -0700333 const SkSVGIDMapper& mapper,
fmalita397a5172016-08-08 11:38:55 -0700334 const SkSVGLengthContext& lctx,
335 const SkSVGPresentationContext& pctx)
fmalita28d5b722016-09-12 17:06:47 -0700336 : fIDMapper(mapper)
337 , fLengthContext(lctx)
fmalita397a5172016-08-08 11:38:55 -0700338 , fPresentationContext(pctx)
339 , fCanvas(canvas)
340 , fCanvasSaveCount(canvas->getSaveCount()) {}
341
342SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other)
fmalita2d961e02016-08-11 09:16:29 -0700343 : SkSVGRenderContext(other.fCanvas,
fmalita28d5b722016-09-12 17:06:47 -0700344 other.fIDMapper,
fmalita2d961e02016-08-11 09:16:29 -0700345 *other.fLengthContext,
346 *other.fPresentationContext) {}
fmalita397a5172016-08-08 11:38:55 -0700347
Florin Malita1aa1bb62017-10-11 14:34:33 -0400348SkSVGRenderContext::SkSVGRenderContext(const SkSVGRenderContext& other, SkCanvas* canvas)
349 : SkSVGRenderContext(canvas,
350 other.fIDMapper,
351 *other.fLengthContext,
352 *other.fPresentationContext) {}
353
fmalita397a5172016-08-08 11:38:55 -0700354SkSVGRenderContext::~SkSVGRenderContext() {
355 fCanvas->restoreToCount(fCanvasSaveCount);
356}
fmalita2d961e02016-08-11 09:16:29 -0700357
Florin Malita27aeb122020-04-08 15:02:02 -0400358SkSVGRenderContext::BorrowedNode SkSVGRenderContext::findNodeById(const SkString& id) const {
359 return BorrowedNode(fIDMapper.find(id));
fmalita28d5b722016-09-12 17:06:47 -0700360}
361
fmalitabef51c22016-09-20 15:45:57 -0700362void SkSVGRenderContext::applyPresentationAttributes(const SkSVGPresentationAttributes& attrs,
363 uint32_t flags) {
fmalita2d961e02016-08-11 09:16:29 -0700364
365#define ApplyLazyInheritedAttribute(ATTR) \
366 do { \
367 /* All attributes should be defined on the inherited context. */ \
368 SkASSERT(fPresentationContext->fInherited.f ## ATTR.isValid()); \
369 const auto* value = attrs.f ## ATTR.getMaybeNull(); \
370 if (value && *value != *fPresentationContext->fInherited.f ## ATTR.get()) { \
371 /* Update the local attribute value */ \
372 fPresentationContext.writable()->fInherited.f ## ATTR.set(*value); \
373 /* Update the cached paints */ \
fmalita28d5b722016-09-12 17:06:47 -0700374 commitToPaint<SkSVGAttribute::k ## ATTR>(attrs, *this, \
fmalita2d961e02016-08-11 09:16:29 -0700375 fPresentationContext.writable()); \
376 } \
377 } while (false)
378
379 ApplyLazyInheritedAttribute(Fill);
380 ApplyLazyInheritedAttribute(FillOpacity);
Florin Malitae932d4b2016-12-01 13:35:11 -0500381 ApplyLazyInheritedAttribute(FillRule);
Florin Malita39fe8c82020-10-20 10:43:03 -0400382 ApplyLazyInheritedAttribute(FontFamily);
383 ApplyLazyInheritedAttribute(FontSize);
384 ApplyLazyInheritedAttribute(FontStyle);
385 ApplyLazyInheritedAttribute(FontWeight);
Florin Malita57a0edf2017-10-10 11:22:08 -0400386 ApplyLazyInheritedAttribute(ClipRule);
fmalita2d961e02016-08-11 09:16:29 -0700387 ApplyLazyInheritedAttribute(Stroke);
Florin Malitae1dadd72017-10-13 18:18:32 -0400388 ApplyLazyInheritedAttribute(StrokeDashOffset);
Florin Malitaf543a602017-10-13 14:07:44 -0400389 ApplyLazyInheritedAttribute(StrokeDashArray);
fmalita2d961e02016-08-11 09:16:29 -0700390 ApplyLazyInheritedAttribute(StrokeLineCap);
391 ApplyLazyInheritedAttribute(StrokeLineJoin);
Florin Malita4de426b2017-10-09 12:57:41 -0400392 ApplyLazyInheritedAttribute(StrokeMiterLimit);
fmalita2d961e02016-08-11 09:16:29 -0700393 ApplyLazyInheritedAttribute(StrokeOpacity);
394 ApplyLazyInheritedAttribute(StrokeWidth);
Florin Malitaffe6ae42017-10-12 11:33:28 -0400395 ApplyLazyInheritedAttribute(Visibility);
Tyler Denniston6c8314e2020-04-09 14:14:10 -0400396 ApplyLazyInheritedAttribute(Color);
fmalita2d961e02016-08-11 09:16:29 -0700397
Tyler Dennistondf1b0142020-04-20 10:52:21 -0400398 // Local 'color' attribute: update paints for attributes that are set to 'currentColor'.
399 if (attrs.fColor.isValid()) {
400 updatePaintsWithCurrentColor(attrs);
401 }
402
fmalita2d961e02016-08-11 09:16:29 -0700403#undef ApplyLazyInheritedAttribute
fmalita6fb06482016-08-15 12:45:11 -0700404
405 // Uninherited attributes. Only apply to the current context.
406
fmalitabef51c22016-09-20 15:45:57 -0700407 if (auto* opacity = attrs.fOpacity.getMaybeNull()) {
Florin Malitaa3626692020-04-09 14:36:45 -0400408 this->applyOpacity(*opacity, flags);
fmalitabef51c22016-09-20 15:45:57 -0700409 }
Florin Malitace8840e2016-12-08 09:26:47 -0500410
411 if (auto* clip = attrs.fClipPath.getMaybeNull()) {
412 this->applyClip(*clip);
413 }
fmalitabef51c22016-09-20 15:45:57 -0700414}
415
416void SkSVGRenderContext::applyOpacity(SkScalar opacity, uint32_t flags) {
417 if (opacity >= 1) {
418 return;
419 }
420
421 const bool hasFill = SkToBool(this->fillPaint());
422 const bool hasStroke = SkToBool(this->strokePaint());
423
424 // We can apply the opacity as paint alpha iif it only affects one atomic draw.
425 // For now, this means a) the target node doesn't have any descendants, and
426 // b) it only has a stroke or a fill (but not both). Going forward, we may need
427 // to refine this heuristic (e.g. to accommodate markers).
428 if ((flags & kLeaf) && (hasFill ^ hasStroke)) {
429 auto* pctx = fPresentationContext.writable();
430 if (hasFill) {
431 pctx->fFillPaint.setAlpha(
432 SkScalarRoundToInt(opacity * pctx->fFillPaint.getAlpha()));
433 } else {
434 pctx->fStrokePaint.setAlpha(
435 SkScalarRoundToInt(opacity * pctx->fStrokePaint.getAlpha()));
436 }
437 } else {
438 // Expensive, layer-based fall back.
fmalita6fb06482016-08-15 12:45:11 -0700439 SkPaint opacityPaint;
fmalitabef51c22016-09-20 15:45:57 -0700440 opacityPaint.setAlpha(opacity_to_alpha(opacity));
fmalita6fb06482016-08-15 12:45:11 -0700441 // Balanced in the destructor, via restoreToCount().
442 fCanvas->saveLayer(nullptr, &opacityPaint);
443 }
fmalita2d961e02016-08-11 09:16:29 -0700444}
445
Florin Malitab36be142017-10-11 14:11:16 -0400446void SkSVGRenderContext::saveOnce() {
447 // The canvas only needs to be saved once, per local SkSVGRenderContext.
448 if (fCanvas->getSaveCount() == fCanvasSaveCount) {
449 fCanvas->save();
450 }
451
452 SkASSERT(fCanvas->getSaveCount() > fCanvasSaveCount);
453}
454
Florin Malitace8840e2016-12-08 09:26:47 -0500455void SkSVGRenderContext::applyClip(const SkSVGClip& clip) {
456 if (clip.type() != SkSVGClip::Type::kIRI) {
457 return;
458 }
459
Florin Malita27aeb122020-04-08 15:02:02 -0400460 const auto clipNode = this->findNodeById(clip.iri());
Florin Malitace8840e2016-12-08 09:26:47 -0500461 if (!clipNode || clipNode->tag() != SkSVGTag::kClipPath) {
462 return;
463 }
464
465 const SkPath clipPath = clipNode->asPath(*this);
466
Florin Malita7d529882016-12-08 16:04:24 -0500467 // We use the computed clip path in two ways:
468 //
469 // - apply to the current canvas, for drawing
470 // - track in the presentation context, for asPath() composition
471 //
472 // TODO: the two uses are exclusive, avoid canvas churn when non needed.
473
Florin Malitab36be142017-10-11 14:11:16 -0400474 this->saveOnce();
Florin Malitace8840e2016-12-08 09:26:47 -0500475
476 fCanvas->clipPath(clipPath, true);
Florin Malita7d529882016-12-08 16:04:24 -0500477 fClipPath.set(clipPath);
Florin Malitace8840e2016-12-08 09:26:47 -0500478}
479
Tyler Dennistondf1b0142020-04-20 10:52:21 -0400480void SkSVGRenderContext::updatePaintsWithCurrentColor(const SkSVGPresentationAttributes& attrs) {
481 // Of the attributes that can use currentColor:
482 // https://www.w3.org/TR/SVG11/color.html#ColorProperty
483 // Only fill and stroke require paint updates. The others are resolved at render time.
484
485 if (fPresentationContext->fInherited.fFill->type() == SkSVGPaint::Type::kCurrentColor) {
486 applySvgPaint(*this, *fPresentationContext->fInherited.fFill,
487 &fPresentationContext.writable()->fFillPaint);
488 }
489
490 if (fPresentationContext->fInherited.fStroke->type() == SkSVGPaint::Type::kCurrentColor) {
491 applySvgPaint(*this, *fPresentationContext->fInherited.fStroke,
492 &fPresentationContext.writable()->fStrokePaint);
493 }
494}
495
fmalita2d961e02016-08-11 09:16:29 -0700496const SkPaint* SkSVGRenderContext::fillPaint() const {
John Stilesa008b0f2020-08-16 08:48:02 -0400497 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fFill->type();
fmalita2d961e02016-08-11 09:16:29 -0700498 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fFillPaint : nullptr;
499}
500
501const SkPaint* SkSVGRenderContext::strokePaint() const {
John Stilesa008b0f2020-08-16 08:48:02 -0400502 const SkSVGPaint::Type paintType = fPresentationContext->fInherited.fStroke->type();
fmalita2d961e02016-08-11 09:16:29 -0700503 return paintType != SkSVGPaint::Type::kNone ? &fPresentationContext->fStrokePaint : nullptr;
504}