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/. */
7 #ifndef LAYOUT_SVG_SVGIMAGECONTEXT_H_
8 #define LAYOUT_SVG_SVGIMAGECONTEXT_H_
10 #include "mozilla/Maybe.h"
11 #include "mozilla/SVGContextPaint.h"
12 #include "mozilla/SVGPreserveAspectRatio.h"
19 enum class ColorScheme
: uint8_t;
22 // SVG image-specific rendering context. For imgIContainer::Draw.
23 // Used to pass information such as
24 // - viewport and color-scheme information from CSS, and
25 // - overridden attributes from an SVG <image> element
26 // to the image's internal SVG document when it's drawn.
27 class SVGImageContext
{
29 SVGImageContext() = default;
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.
47 explicit SVGImageContext(
48 const Maybe
<CSSIntSize
>& aViewportSize
,
49 const Maybe
<SVGPreserveAspectRatio
>& aPreserveAspectRatio
= Nothing(),
50 const Maybe
<ColorScheme
>& aColorScheme
= Nothing())
51 : mViewportSize(aViewportSize
),
52 mPreserveAspectRatio(aPreserveAspectRatio
),
53 mColorScheme(aColorScheme
) {}
55 static void MaybeStoreContextPaint(SVGImageContext
& aContext
,
57 imgIContainer
* aImgContainer
);
59 static void MaybeStoreContextPaint(SVGImageContext
& aContext
,
60 const nsPresContext
&, const ComputedStyle
&,
63 const Maybe
<CSSIntSize
>& GetViewportSize() const { return mViewportSize
; }
65 void SetViewportSize(const Maybe
<CSSIntSize
>& aSize
) {
66 mViewportSize
= aSize
;
69 const Maybe
<ColorScheme
>& GetColorScheme() const { return mColorScheme
; }
71 void SetColorScheme(const Maybe
<ColorScheme
>& aScheme
) {
72 mColorScheme
= aScheme
;
75 const Maybe
<SVGPreserveAspectRatio
>& GetPreserveAspectRatio() const {
76 return mPreserveAspectRatio
;
79 void SetPreserveAspectRatio(const Maybe
<SVGPreserveAspectRatio
>& aPAR
) {
80 mPreserveAspectRatio
= aPAR
;
83 const SVGEmbeddingContextPaint
* GetContextPaint() const {
84 return mContextPaint
.get();
87 void ClearContextPaint() { mContextPaint
= nullptr; }
89 bool operator==(const SVGImageContext
& aOther
) const {
90 bool contextPaintIsEqual
=
91 // neither have context paint, or they have the same object:
92 (mContextPaint
== aOther
.mContextPaint
) ||
93 // or both have context paint that are different but equivalent objects:
94 (mContextPaint
&& aOther
.mContextPaint
&&
95 *mContextPaint
== *aOther
.mContextPaint
);
97 return contextPaintIsEqual
&& mViewportSize
== aOther
.mViewportSize
&&
98 mPreserveAspectRatio
== aOther
.mPreserveAspectRatio
&&
99 mColorScheme
== aOther
.mColorScheme
;
102 bool operator!=(const SVGImageContext
& aOther
) const {
103 return !(*this == aOther
);
106 PLDHashNumber
Hash() const {
107 PLDHashNumber hash
= 0;
109 hash
= HashGeneric(hash
, mContextPaint
->Hash());
111 return HashGeneric(hash
, mViewportSize
.map(HashSize
).valueOr(0),
112 mPreserveAspectRatio
.map(HashPAR
).valueOr(0),
113 mColorScheme
.map(HashColorScheme
).valueOr(0));
117 static PLDHashNumber
HashSize(const CSSIntSize
& aSize
) {
118 return HashGeneric(aSize
.width
, aSize
.height
);
120 static PLDHashNumber
HashPAR(const SVGPreserveAspectRatio
& aPAR
) {
123 static PLDHashNumber
HashColorScheme(ColorScheme aScheme
) {
124 return HashGeneric(uint8_t(aScheme
));
127 // NOTE: When adding new member-vars, remember to update Hash() & operator==.
128 RefPtr
<SVGEmbeddingContextPaint
> mContextPaint
;
129 Maybe
<CSSIntSize
> mViewportSize
;
130 Maybe
<SVGPreserveAspectRatio
> mPreserveAspectRatio
;
131 Maybe
<ColorScheme
> mColorScheme
;
134 } // namespace mozilla
136 #endif // LAYOUT_SVG_SVGIMAGECONTEXT_H_