tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AutoRestoreSVGState.h (2723B)


      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/. */
      5 
      6 #ifndef mozilla_image_AutoRestoreSVGState_h
      7 #define mozilla_image_AutoRestoreSVGState_h
      8 
      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"
     19 
     20 namespace mozilla::image {
     21 
     22 class MOZ_STACK_CLASS AutoRestoreSVGState final {
     23 public:
     24  AutoRestoreSVGState(const SVGDrawingParameters& aParams,
     25                      SVGDocumentWrapper* aSVGDocumentWrapper,
     26                      bool aContextPaint)
     27      : AutoRestoreSVGState(aParams.svgContext, aParams.animationTime,
     28                            aSVGDocumentWrapper, aContextPaint) {}
     29 
     30  AutoRestoreSVGState(const SVGImageContext& aSVGContext, float aAnimationTime,
     31                      SVGDocumentWrapper* aSVGDocumentWrapper,
     32                      bool aContextPaint)
     33      : mIsDrawing(aSVGDocumentWrapper->mIsDrawing),
     34        // Apply any 'preserveAspectRatio' override (if specified) to the root
     35        // element:
     36        mPAR(aSVGContext, aSVGDocumentWrapper->GetSVGRootElement()),
     37        // Set the animation time:
     38        mTime(aSVGDocumentWrapper->GetSVGRootElement(), aAnimationTime) {
     39    MOZ_ASSERT(!mIsDrawing.SavedValue());
     40    MOZ_ASSERT(aSVGDocumentWrapper->GetDocument());
     41 
     42    if (auto* pc = aSVGDocumentWrapper->GetDocument()->GetPresContext()) {
     43      pc->SetColorSchemeOverride([&] {
     44        if (auto scheme = aSVGContext.GetColorScheme()) {
     45          return *scheme == ColorScheme::Light
     46                     ? dom::PrefersColorSchemeOverride::Light
     47                     : dom::PrefersColorSchemeOverride::Dark;
     48        }
     49        return dom::PrefersColorSchemeOverride::None;
     50      }());
     51    }
     52 
     53    aSVGDocumentWrapper->mIsDrawing = true;
     54 
     55    // Set context paint (if specified) on the document:
     56    if (aContextPaint) {
     57      MOZ_ASSERT(aSVGContext.GetContextPaint());
     58      mContextPaint.emplace(aSVGContext.GetContextPaint(),
     59                            aSVGDocumentWrapper->GetDocument());
     60    }
     61  }
     62 
     63 private:
     64  AutoRestore<bool> mIsDrawing;
     65  AutoPreserveAspectRatioOverride mPAR;
     66  AutoSVGTimeSetRestore mTime;
     67  Maybe<AutoSetRestoreSVGContextPaint> mContextPaint;
     68 };
     69 
     70 }  // namespace mozilla::image
     71 
     72 #endif  // mozilla_image_AutoRestoreSVGState_h