blob: 69bf9f278f9006a51eab7bbedbe79550bc8258a8 [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
8#ifndef SkSVGRenderContext_DEFINED
9#define SkSVGRenderContext_DEFINED
10
11#include "SkPaint.h"
Florin Malita7d529882016-12-08 16:04:24 -050012#include "SkPath.h"
fmalita397a5172016-08-08 11:38:55 -070013#include "SkRect.h"
14#include "SkSize.h"
fmalita2d961e02016-08-11 09:16:29 -070015#include "SkSVGAttribute.h"
fmalita28d5b722016-09-12 17:06:47 -070016#include "SkSVGIDMapper.h"
fmalita6ceef3d2016-07-26 18:46:34 -070017#include "SkTLazy.h"
fmalita397a5172016-08-08 11:38:55 -070018#include "SkTypes.h"
fmalita6ceef3d2016-07-26 18:46:34 -070019
fmalita397a5172016-08-08 11:38:55 -070020class SkCanvas;
fmalitabffc2562016-08-03 10:21:11 -070021class SkSVGLength;
22
23class SkSVGLengthContext {
24public:
fmalita280e2822016-08-17 14:51:03 -070025 SkSVGLengthContext(const SkSize& viewport, SkScalar dpi = 90)
26 : fViewport(viewport), fDPI(dpi) {}
fmalitabffc2562016-08-03 10:21:11 -070027
28 enum class LengthType {
29 kHorizontal,
30 kVertical,
31 kOther,
32 };
33
fmalita397a5172016-08-08 11:38:55 -070034 const SkSize& viewPort() const { return fViewport; }
fmalitabffc2562016-08-03 10:21:11 -070035 void setViewPort(const SkSize& viewport) { fViewport = viewport; }
36
37 SkScalar resolve(const SkSVGLength&, LengthType) const;
fmalita397a5172016-08-08 11:38:55 -070038 SkRect resolveRect(const SkSVGLength& x, const SkSVGLength& y,
39 const SkSVGLength& w, const SkSVGLength& h) const;
fmalitabffc2562016-08-03 10:21:11 -070040
41private:
fmalita280e2822016-08-17 14:51:03 -070042 SkSize fViewport;
43 SkScalar fDPI;
fmalitabffc2562016-08-03 10:21:11 -070044};
fmalita6ceef3d2016-07-26 18:46:34 -070045
fmalita2d961e02016-08-11 09:16:29 -070046struct SkSVGPresentationContext {
fmalita397a5172016-08-08 11:38:55 -070047 SkSVGPresentationContext();
fmalita2d961e02016-08-11 09:16:29 -070048 SkSVGPresentationContext(const SkSVGPresentationContext&) = default;
49 SkSVGPresentationContext& operator=(const SkSVGPresentationContext&) = default;
fmalitabffc2562016-08-03 10:21:11 -070050
fmalita2d961e02016-08-11 09:16:29 -070051 // Inherited presentation attributes, computed for the current node.
52 SkSVGPresentationAttributes fInherited;
fmalita6ceef3d2016-07-26 18:46:34 -070053
fmalita2d961e02016-08-11 09:16:29 -070054 // Cached paints, reflecting the current presentation attributes.
55 SkPaint fFillPaint;
56 SkPaint fStrokePaint;
fmalita397a5172016-08-08 11:38:55 -070057};
58
59class SkSVGRenderContext {
60public:
fmalita28d5b722016-09-12 17:06:47 -070061 SkSVGRenderContext(SkCanvas*, const SkSVGIDMapper&, const SkSVGLengthContext&,
62 const SkSVGPresentationContext&);
fmalita397a5172016-08-08 11:38:55 -070063 SkSVGRenderContext(const SkSVGRenderContext&);
Florin Malita1aa1bb62017-10-11 14:34:33 -040064 SkSVGRenderContext(const SkSVGRenderContext&, SkCanvas*);
fmalita397a5172016-08-08 11:38:55 -070065 ~SkSVGRenderContext();
66
67 const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
68 SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }
69
Florin Malitae932d4b2016-12-01 13:35:11 -050070 const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
71
fmalita397a5172016-08-08 11:38:55 -070072 SkCanvas* canvas() const { return fCanvas; }
Florin Malitab36be142017-10-11 14:11:16 -040073 void saveOnce();
fmalita397a5172016-08-08 11:38:55 -070074
fmalitabef51c22016-09-20 15:45:57 -070075 enum ApplyFlags {
76 kLeaf = 1 << 0, // the target node doesn't have descendants
77 };
78 void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
fmalita2d961e02016-08-11 09:16:29 -070079
fmalita28d5b722016-09-12 17:06:47 -070080 const SkSVGNode* findNodeById(const SkString&) const;
81
fmalita2d961e02016-08-11 09:16:29 -070082 const SkPaint* fillPaint() const;
83 const SkPaint* strokePaint() const;
84
Florin Malita7d529882016-12-08 16:04:24 -050085 // The local computed clip path (not inherited).
86 const SkPath* clipPath() const { return fClipPath.getMaybeNull(); }
87
fmalita397a5172016-08-08 11:38:55 -070088private:
89 // Stack-only
90 void* operator new(size_t) = delete;
91 void* operator new(size_t, void*) = delete;
92 SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;
93
fmalitabef51c22016-09-20 15:45:57 -070094 void applyOpacity(SkScalar opacity, uint32_t flags);
Florin Malitace8840e2016-12-08 09:26:47 -050095 void applyClip(const SkSVGClip&);
fmalitabef51c22016-09-20 15:45:57 -070096
fmalita28d5b722016-09-12 17:06:47 -070097 const SkSVGIDMapper& fIDMapper;
fmalita397a5172016-08-08 11:38:55 -070098 SkTCopyOnFirstWrite<SkSVGLengthContext> fLengthContext;
99 SkTCopyOnFirstWrite<SkSVGPresentationContext> fPresentationContext;
100 SkCanvas* fCanvas;
101 // The save count on 'fCanvas' at construction time.
102 // A restoreToCount() will be issued on destruction.
103 int fCanvasSaveCount;
Florin Malita7d529882016-12-08 16:04:24 -0500104
105 // clipPath, if present for the current context (not inherited).
106 SkTLazy<SkPath> fClipPath;
fmalita6ceef3d2016-07-26 18:46:34 -0700107};
108
109#endif // SkSVGRenderContext_DEFINED