/src/mozilla-central/layout/svg/SVGImageContext.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef MOZILLA_SVGCONTEXT_H_ |
8 | | #define MOZILLA_SVGCONTEXT_H_ |
9 | | |
10 | | #include "mozilla/Maybe.h" |
11 | | #include "mozilla/SVGContextPaint.h" |
12 | | #include "SVGPreserveAspectRatio.h" |
13 | | #include "Units.h" |
14 | | |
15 | | class nsIFrame; |
16 | | |
17 | | namespace mozilla { |
18 | | |
19 | | class ComputedStyle; |
20 | | |
21 | | // SVG image-specific rendering context. For imgIContainer::Draw. |
22 | | // Used to pass information such as |
23 | | // - viewport information from CSS, and |
24 | | // - overridden attributes from an SVG <image> element |
25 | | // to the image's internal SVG document when it's drawn. |
26 | | class SVGImageContext |
27 | | { |
28 | | public: |
29 | 0 | SVGImageContext() {} |
30 | | |
31 | | /** |
32 | | * Currently it seems that the aViewportSize parameter ends up being used |
33 | | * for different things by different pieces of code, and probably in some |
34 | | * cases being used incorrectly (specifically in the case of pixel snapping |
35 | | * under the nsLayoutUtils::Draw*Image() methods). An unfortunate result of |
36 | | * the messy code is that aViewportSize is currently a Maybe<T> since it |
37 | | * is difficult to create a utility function that consumers can use up |
38 | | * front to get the "correct" viewport size (i.e. which for compatibility |
39 | | * with the current code (bugs and all) would mean the size including all |
40 | | * the snapping and resizing magic that happens in various places under the |
41 | | * nsLayoutUtils::Draw*Image() methods on the way to DrawImageInternal |
42 | | * creating |fallbackContext|). Using Maybe<T> allows code to pass Nothing() |
43 | | * in order to get the size that's created for |fallbackContext|. At some |
44 | | * point we need to clean this code up, make our abstractions clear, create |
45 | | * that utility and stop using Maybe for this parameter. |
46 | | */ |
47 | | explicit SVGImageContext(const Maybe<CSSIntSize>& aViewportSize, |
48 | | const Maybe<SVGPreserveAspectRatio>& aPreserveAspectRatio = Nothing()) |
49 | | : mViewportSize(aViewportSize) |
50 | | , mPreserveAspectRatio(aPreserveAspectRatio) |
51 | | { } |
52 | | |
53 | | static void MaybeStoreContextPaint(Maybe<SVGImageContext>& aContext, |
54 | | nsIFrame* aFromFrame, |
55 | | imgIContainer* aImgContainer); |
56 | | |
57 | | static void MaybeStoreContextPaint(Maybe<SVGImageContext>& aContext, |
58 | | ComputedStyle* aFromComputedStyle, |
59 | | imgIContainer* aImgContainer); |
60 | | |
61 | | const Maybe<CSSIntSize>& GetViewportSize() const { |
62 | | return mViewportSize; |
63 | | } |
64 | | |
65 | | void SetViewportSize(const Maybe<CSSIntSize>& aSize) { |
66 | | mViewportSize = aSize; |
67 | | } |
68 | | |
69 | | const Maybe<SVGPreserveAspectRatio>& GetPreserveAspectRatio() const { |
70 | | return mPreserveAspectRatio; |
71 | | } |
72 | | |
73 | | void SetPreserveAspectRatio(const Maybe<SVGPreserveAspectRatio>& aPAR) { |
74 | | mPreserveAspectRatio = aPAR; |
75 | | } |
76 | | |
77 | | const SVGEmbeddingContextPaint* GetContextPaint() const { |
78 | | return mContextPaint.get(); |
79 | | } |
80 | | |
81 | | void ClearContextPaint() { |
82 | | mContextPaint = nullptr; |
83 | | } |
84 | | |
85 | | bool operator==(const SVGImageContext& aOther) const { |
86 | | bool contextPaintIsEqual = |
87 | | // neither have context paint, or they have the same object: |
88 | | (mContextPaint == aOther.mContextPaint) || |
89 | | // or both have context paint that are different but equivalent objects: |
90 | | (mContextPaint && aOther.mContextPaint && |
91 | | *mContextPaint == *aOther.mContextPaint); |
92 | | |
93 | | return contextPaintIsEqual && |
94 | | mViewportSize == aOther.mViewportSize && |
95 | | mPreserveAspectRatio == aOther.mPreserveAspectRatio; |
96 | | } |
97 | | |
98 | | bool operator!=(const SVGImageContext& aOther) const { |
99 | | return !(*this == aOther); |
100 | | } |
101 | | |
102 | | PLDHashNumber Hash() const { |
103 | | PLDHashNumber hash = 0; |
104 | | if (mContextPaint) { |
105 | | hash = HashGeneric(hash, mContextPaint->Hash()); |
106 | | } |
107 | | return HashGeneric(hash, |
108 | | mViewportSize.map(HashSize).valueOr(0), |
109 | | mPreserveAspectRatio.map(HashPAR).valueOr(0)); |
110 | | } |
111 | | |
112 | | private: |
113 | | static PLDHashNumber HashSize(const CSSIntSize& aSize) { |
114 | | return HashGeneric(aSize.width, aSize.height); |
115 | | } |
116 | | static PLDHashNumber HashPAR(const SVGPreserveAspectRatio& aPAR) { |
117 | | return aPAR.Hash(); |
118 | | } |
119 | | |
120 | | // NOTE: When adding new member-vars, remember to update Hash() & operator==. |
121 | | RefPtr<SVGEmbeddingContextPaint> mContextPaint; |
122 | | Maybe<CSSIntSize> mViewportSize; |
123 | | Maybe<SVGPreserveAspectRatio> mPreserveAspectRatio; |
124 | | }; |
125 | | |
126 | | } // namespace mozilla |
127 | | |
128 | | #endif // MOZILLA_SVGCONTEXT_H_ |