aboutsummaryrefslogtreecommitdiffhomepage
path: root/experimental/svg/model/SkSVGRenderContext.h
blob: 47886d7cb0c35f75e284d1432a1138a112891ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright 2016 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#ifndef SkSVGRenderContext_DEFINED
#define SkSVGRenderContext_DEFINED

#include "SkPaint.h"
#include "SkRect.h"
#include "SkSize.h"
#include "SkTLazy.h"
#include "SkTypes.h"

class SkCanvas;
class SkSVGLength;

class SkSVGLengthContext {
public:
    SkSVGLengthContext(const SkSize& viewport) : fViewport(viewport) {}

    enum class LengthType {
        kHorizontal,
        kVertical,
        kOther,
    };

    const SkSize& viewPort() const { return fViewport; }
    void setViewPort(const SkSize& viewport) { fViewport = viewport; }

    SkScalar resolve(const SkSVGLength&, LengthType) const;
    SkRect   resolveRect(const SkSVGLength& x, const SkSVGLength& y,
                         const SkSVGLength& w, const SkSVGLength& h) const;

private:
    SkSize fViewport;
};

class SkSVGPresentationContext {
public:
    SkSVGPresentationContext();
    SkSVGPresentationContext(const SkSVGPresentationContext&);
    SkSVGPresentationContext& operator=(const SkSVGPresentationContext&);

    const SkPaint* fillPaint() const { return fFill.getMaybeNull(); }
    const SkPaint* strokePaint() const { return fStroke.getMaybeNull(); }

    void setFillColor(SkColor);
    void setStrokeColor(SkColor);

private:
    void initFrom(const SkSVGPresentationContext&);

    SkPaint& ensureFill();
    SkPaint& ensureStroke();

    // TODO: convert to regular SkPaints and track explicit attribute values instead.
    SkTLazy<SkPaint> fFill;
    SkTLazy<SkPaint> fStroke;
};

class SkSVGRenderContext {
public:
    SkSVGRenderContext(SkCanvas*, const SkSVGLengthContext&, const SkSVGPresentationContext&);
    SkSVGRenderContext(const SkSVGRenderContext&);
    ~SkSVGRenderContext();

    const SkSVGLengthContext& lengthContext() const { return *fLengthContext; }
    SkSVGLengthContext* writableLengthContext() { return fLengthContext.writable(); }

    const SkSVGPresentationContext& presentationContext() const { return *fPresentationContext; }
    SkSVGPresentationContext* writablePresentationContext() {
        return fPresentationContext.writable();
    }

    SkCanvas* canvas() const { return fCanvas; }

private:
    // Stack-only
    void* operator new(size_t)                               = delete;
    void* operator new(size_t, void*)                        = delete;
    SkSVGRenderContext& operator=(const SkSVGRenderContext&) = delete;

    SkTCopyOnFirstWrite<SkSVGLengthContext>       fLengthContext;
    SkTCopyOnFirstWrite<SkSVGPresentationContext> fPresentationContext;
    SkCanvas*                                     fCanvas;
    // The save count on 'fCanvas' at construction time.
    // A restoreToCount() will be issued on destruction.
    int                                           fCanvasSaveCount;
};

#endif // SkSVGRenderContext_DEFINED