LCOV - code coverage report
Current view: top level - layout/svg - SVGImageContext.h (source / functions) Hit Total Coverage
Test: output.info Lines: 31 39 79.5 %
Date: 2017-07-14 16:53:18 Functions: 12 16 75.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13