blob: 3cd38cbd7fad28e6f71c8f9b9bc5c60109e163a6 [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&);
64 ~SkSVGRenderContext();
65
66 const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
67 SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }
68
Florin Malitae932d4b2016-12-01 13:35:11 -050069 const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
70
fmalita397a5172016-08-08 11:38:55 -070071 SkCanvas* canvas() const { return fCanvas; }
72
fmalitabef51c22016-09-20 15:45:57 -070073 enum ApplyFlags {
74 kLeaf = 1 << 0, // the target node doesn't have descendants
75 };
76 void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
fmalita2d961e02016-08-11 09:16:29 -070077
fmalita28d5b722016-09-12 17:06:47 -070078 const SkSVGNode* findNodeById(const SkString&) const;
79
fmalita2d961e02016-08-11 09:16:29 -070080 const SkPaint* fillPaint() const;
81 const SkPaint* strokePaint() const;
82
Florin Malita7d529882016-12-08 16:04:24 -050083 // The local computed clip path (not inherited).
84 const SkPath* clipPath() const { return fClipPath.getMaybeNull(); }
85
fmalita397a5172016-08-08 11:38:55 -070086private:
87 // Stack-only
88 void* operator new(size_t) = delete;
89 void* operator new(size_t, void*) = delete;
90 SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;
91
fmalitabef51c22016-09-20 15:45:57 -070092 void applyOpacity(SkScalar opacity, uint32_t flags);
Florin Malitace8840e2016-12-08 09:26:47 -050093 void applyClip(const SkSVGClip&);
fmalitabef51c22016-09-20 15:45:57 -070094
fmalita28d5b722016-09-12 17:06:47 -070095 const SkSVGIDMapper& fIDMapper;
fmalita397a5172016-08-08 11:38:55 -070096 SkTCopyOnFirstWrite<SkSVGLengthContext> fLengthContext;
97 SkTCopyOnFirstWrite<SkSVGPresentationContext> fPresentationContext;
98 SkCanvas* fCanvas;
99 // The save count on 'fCanvas' at construction time.
100 // A restoreToCount() will be issued on destruction.
101 int fCanvasSaveCount;
Florin Malita7d529882016-12-08 16:04:24 -0500102
103 // clipPath, if present for the current context (not inherited).
104 SkTLazy<SkPath> fClipPath;
fmalita6ceef3d2016-07-26 18:46:34 -0700105};
106
107#endif // SkSVGRenderContext_DEFINED