2021-04-29 17:15:00 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_image_AutoRestoreSVGState_h
|
|
|
|
#define mozilla_image_AutoRestoreSVGState_h
|
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/AutoRestore.h"
|
|
|
|
#include "mozilla/SVGContextPaint.h"
|
|
|
|
#include "mozilla/dom/SVGSVGElement.h"
|
2022-04-17 23:38:01 +03:00
|
|
|
#include "nsPresContext.h"
|
2021-04-29 17:15:00 +03:00
|
|
|
#include "SVGDrawingParameters.h"
|
|
|
|
#include "SVGDocumentWrapper.h"
|
2022-04-17 23:38:01 +03:00
|
|
|
#include "mozilla/dom/DocumentInlines.h"
|
|
|
|
#include "mozilla/dom/SVGDocument.h"
|
|
|
|
#include "mozilla/dom/BrowsingContextBinding.h"
|
2021-04-29 17:15:00 +03:00
|
|
|
|
2022-04-17 23:38:01 +03:00
|
|
|
namespace mozilla::image {
|
2021-04-29 17:15:00 +03:00
|
|
|
|
|
|
|
class MOZ_STACK_CLASS AutoRestoreSVGState final {
|
|
|
|
public:
|
|
|
|
AutoRestoreSVGState(const SVGDrawingParameters& aParams,
|
2021-04-29 17:15:01 +03:00
|
|
|
SVGDocumentWrapper* aSVGDocumentWrapper,
|
2021-04-29 17:15:00 +03:00
|
|
|
bool aContextPaint)
|
2021-08-09 23:39:31 +03:00
|
|
|
: AutoRestoreSVGState(aParams.svgContext, aParams.animationTime,
|
|
|
|
aSVGDocumentWrapper, aContextPaint) {}
|
|
|
|
|
2022-07-15 11:27:50 +03:00
|
|
|
AutoRestoreSVGState(const SVGImageContext& aSVGContext, float aAnimationTime,
|
2021-08-09 23:39:31 +03:00
|
|
|
SVGDocumentWrapper* aSVGDocumentWrapper,
|
|
|
|
bool aContextPaint)
|
2022-04-17 23:38:01 +03:00
|
|
|
: mIsDrawing(aSVGDocumentWrapper->mIsDrawing),
|
2021-04-29 17:15:00 +03:00
|
|
|
// Apply any 'preserveAspectRatio' override (if specified) to the root
|
|
|
|
// element:
|
2022-04-17 23:38:01 +03:00
|
|
|
mPAR(aSVGContext, aSVGDocumentWrapper->GetRootSVGElem()),
|
2021-04-29 17:15:00 +03:00
|
|
|
// Set the animation time:
|
2021-08-09 23:39:31 +03:00
|
|
|
mTime(aSVGDocumentWrapper->GetRootSVGElem(), aAnimationTime) {
|
2021-04-29 17:15:01 +03:00
|
|
|
MOZ_ASSERT(!mIsDrawing.SavedValue());
|
2021-04-29 17:15:00 +03:00
|
|
|
MOZ_ASSERT(aSVGDocumentWrapper->GetDocument());
|
|
|
|
|
2022-04-17 23:38:01 +03:00
|
|
|
if (auto* pc = aSVGDocumentWrapper->GetDocument()->GetPresContext()) {
|
|
|
|
pc->SetColorSchemeOverride([&] {
|
2022-07-14 19:03:25 +03:00
|
|
|
if (auto scheme = aSVGContext.GetColorScheme()) {
|
|
|
|
return *scheme == ColorScheme::Light
|
2022-04-17 23:38:01 +03:00
|
|
|
? dom::PrefersColorSchemeOverride::Light
|
|
|
|
: dom::PrefersColorSchemeOverride::Dark;
|
|
|
|
}
|
|
|
|
return dom::PrefersColorSchemeOverride::None;
|
|
|
|
}());
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:15:01 +03:00
|
|
|
aSVGDocumentWrapper->mIsDrawing = true;
|
2021-04-29 17:15:00 +03:00
|
|
|
|
|
|
|
// Set context paint (if specified) on the document:
|
|
|
|
if (aContextPaint) {
|
2022-07-14 19:03:25 +03:00
|
|
|
MOZ_ASSERT(aSVGContext.GetContextPaint());
|
2023-01-17 22:13:41 +03:00
|
|
|
mContextPaint.emplace(aSVGContext.GetContextPaint(),
|
|
|
|
aSVGDocumentWrapper->GetDocument());
|
2021-04-29 17:15:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
AutoRestore<bool> mIsDrawing;
|
|
|
|
AutoPreserveAspectRatioOverride mPAR;
|
|
|
|
AutoSVGTimeSetRestore mTime;
|
|
|
|
Maybe<AutoSetRestoreSVGContextPaint> mContextPaint;
|
|
|
|
};
|
|
|
|
|
2022-04-17 23:38:01 +03:00
|
|
|
} // namespace mozilla::image
|
2021-04-29 17:15:00 +03:00
|
|
|
|
|
|
|
#endif // mozilla_image_AutoRestoreSVGState_h
|