• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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"
12 #include "SkPath.h"
13 #include "SkRect.h"
14 #include "SkSize.h"
15 #include "SkSVGAttribute.h"
16 #include "SkSVGIDMapper.h"
17 #include "SkTLazy.h"
18 #include "SkTypes.h"
19 
20 class SkCanvas;
21 class SkSVGLength;
22 
23 class SkSVGLengthContext {
24 public:
25     SkSVGLengthContext(const SkSize& viewport, SkScalar dpi = 90)
fViewport(viewport)26         : fViewport(viewport), fDPI(dpi) {}
27 
28     enum class LengthType {
29         kHorizontal,
30         kVertical,
31         kOther,
32     };
33 
viewPort()34     const SkSize& viewPort() const { return fViewport; }
setViewPort(const SkSize & viewport)35     void setViewPort(const SkSize& viewport) { fViewport = viewport; }
36 
37     SkScalar resolve(const SkSVGLength&, LengthType) const;
38     SkRect   resolveRect(const SkSVGLength& x, const SkSVGLength& y,
39                          const SkSVGLength& w, const SkSVGLength& h) const;
40 
41 private:
42     SkSize   fViewport;
43     SkScalar fDPI;
44 };
45 
46 struct SkSVGPresentationContext {
47     SkSVGPresentationContext();
48     SkSVGPresentationContext(const SkSVGPresentationContext&)            = default;
49     SkSVGPresentationContext& operator=(const SkSVGPresentationContext&) = default;
50 
51     // Inherited presentation attributes, computed for the current node.
52     SkSVGPresentationAttributes fInherited;
53 
54     // Cached paints, reflecting the current presentation attributes.
55     SkPaint fFillPaint;
56     SkPaint fStrokePaint;
57 };
58 
59 class SkSVGRenderContext {
60 public:
61     SkSVGRenderContext(SkCanvas*, const SkSVGIDMapper&, const SkSVGLengthContext&,
62                        const SkSVGPresentationContext&);
63     SkSVGRenderContext(const SkSVGRenderContext&);
64     SkSVGRenderContext(const SkSVGRenderContext&, SkCanvas*);
65     ~SkSVGRenderContext();
66 
lengthContext()67     const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
writableLengthContext()68     SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }
69 
presentationContext()70     const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
71 
canvas()72     SkCanvas* canvas() const { return fCanvas; }
73     void saveOnce();
74 
75     enum ApplyFlags {
76         kLeaf = 1 << 0, // the target node doesn't have descendants
77     };
78     void applyPresentationAttributes(const SkSVGPresentationAttributes&, uint32_t flags);
79 
80     const SkSVGNode* findNodeById(const SkString&) const;
81 
82     const SkPaint* fillPaint() const;
83     const SkPaint* strokePaint() const;
84 
85     // The local computed clip path (not inherited).
clipPath()86     const SkPath* clipPath() const { return fClipPath.getMaybeNull(); }
87 
88 private:
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 
94     void applyOpacity(SkScalar opacity, uint32_t flags);
95     void applyClip(const SkSVGClip&);
96 
97     const SkSVGIDMapper&                          fIDMapper;
98     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;
104 
105     // clipPath, if present for the current context (not inherited).
106     SkTLazy<SkPath>                               fClipPath;
107 };
108 
109 #endif // SkSVGRenderContext_DEFINED
110