1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_image_AutoRestoreSVGState_h
7 #define mozilla_image_AutoRestoreSVGState_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/AutoRestore.h"
11 #include "mozilla/SVGContextPaint.h"
12 #include "mozilla/dom/SVGSVGElement.h"
13 #include "nsPresContext.h"
14 #include "SVGDrawingParameters.h"
15 #include "SVGDocumentWrapper.h"
16 #include "mozilla/dom/DocumentInlines.h"
17 #include "mozilla/dom/SVGDocument.h"
18 #include "mozilla/dom/BrowsingContextBinding.h"
20 namespace mozilla::image
{
22 class MOZ_STACK_CLASS AutoRestoreSVGState final
{
24 AutoRestoreSVGState(const SVGDrawingParameters
& aParams
,
25 SVGDocumentWrapper
* aSVGDocumentWrapper
,
27 : AutoRestoreSVGState(aParams
.svgContext
, aParams
.animationTime
,
28 aSVGDocumentWrapper
, aContextPaint
) {}
30 AutoRestoreSVGState(const Maybe
<SVGImageContext
>& aSVGContext
,
32 SVGDocumentWrapper
* aSVGDocumentWrapper
,
34 : mIsDrawing(aSVGDocumentWrapper
->mIsDrawing
),
35 // Apply any 'preserveAspectRatio' override (if specified) to the root
37 mPAR(aSVGContext
, aSVGDocumentWrapper
->GetRootSVGElem()),
38 // Set the animation time:
39 mTime(aSVGDocumentWrapper
->GetRootSVGElem(), aAnimationTime
) {
40 MOZ_ASSERT(!mIsDrawing
.SavedValue());
41 MOZ_ASSERT(aSVGDocumentWrapper
->GetDocument());
43 if (auto* pc
= aSVGDocumentWrapper
->GetDocument()->GetPresContext()) {
44 pc
->SetColorSchemeOverride([&] {
45 if (aSVGContext
&& aSVGContext
->GetColorScheme()) {
46 auto scheme
= *aSVGContext
->GetColorScheme();
47 return scheme
== ColorScheme::Light
48 ? dom::PrefersColorSchemeOverride::Light
49 : dom::PrefersColorSchemeOverride::Dark
;
51 return dom::PrefersColorSchemeOverride::None
;
55 aSVGDocumentWrapper
->mIsDrawing
= true;
57 // Set context paint (if specified) on the document:
59 MOZ_ASSERT(aSVGContext
->GetContextPaint());
60 mContextPaint
.emplace(*aSVGContext
->GetContextPaint(),
61 *aSVGDocumentWrapper
->GetDocument());
66 AutoRestore
<bool> mIsDrawing
;
67 AutoPreserveAspectRatioOverride mPAR
;
68 AutoSVGTimeSetRestore mTime
;
69 Maybe
<AutoSetRestoreSVGContextPaint
> mContextPaint
;
72 } // namespace mozilla::image
74 #endif // mozilla_image_AutoRestoreSVGState_h