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/. */
6 
7 #ifndef __NS_SVGPATTERNFRAME_H__
8 #define __NS_SVGPATTERNFRAME_H__
9 
10 #include "mozilla/Attributes.h"
11 #include "gfxMatrix.h"
12 #include "mozilla/gfx/2D.h"
13 #include "mozilla/RefPtr.h"
14 #include "nsAutoPtr.h"
15 #include "nsSVGPaintServerFrame.h"
16 
17 class nsIFrame;
18 class nsSVGLength2;
19 class nsSVGViewBox;
20 
21 namespace mozilla {
22 class SVGAnimatedPreserveAspectRatio;
23 class SVGGeometryFrame;
24 class nsSVGAnimatedTransformList;
25 }  // namespace mozilla
26 
27 /**
28  * Patterns can refer to other patterns. We create an nsSVGPaintingProperty
29  * with property type nsGkAtoms::href to track the referenced pattern.
30  */
31 class nsSVGPatternFrame final : public nsSVGPaintServerFrame {
32   typedef mozilla::gfx::SourceSurface SourceSurface;
33 
34  public:
35   NS_DECL_FRAMEARENA_HELPERS(nsSVGPatternFrame)
36 
37   friend nsIFrame* NS_NewSVGPatternFrame(nsIPresShell* aPresShell,
38                                          nsStyleContext* aContext);
39 
40   explicit nsSVGPatternFrame(nsStyleContext* aContext);
41 
42   // nsSVGPaintServerFrame methods:
43   virtual already_AddRefed<gfxPattern> GetPaintServerPattern(
44       nsIFrame* aSource, const DrawTarget* aDrawTarget,
45       const gfxMatrix& aContextMatrix,
46       nsStyleSVGPaint nsStyleSVG::*aFillOrStroke, float aOpacity,
47       imgDrawingParams& aImgParams, const gfxRect* aOverrideBounds) override;
48 
49  public:
50   typedef mozilla::SVGAnimatedPreserveAspectRatio
51       SVGAnimatedPreserveAspectRatio;
52 
53   // nsSVGContainerFrame methods:
54   virtual gfxMatrix GetCanvasTM() override;
55 
56   // nsIFrame interface:
57   virtual nsresult AttributeChanged(int32_t aNameSpaceID, nsAtom* aAttribute,
58                                     int32_t aModType) override;
59 
60 #ifdef DEBUG
61   virtual void Init(nsIContent* aContent, nsContainerFrame* aParent,
62                     nsIFrame* aPrevInFlow) override;
63 #endif
64 
65 #ifdef DEBUG_FRAME_DUMP
GetFrameName(nsAString & aResult)66   virtual nsresult GetFrameName(nsAString& aResult) const override {
67     return MakeFrameName(NS_LITERAL_STRING("SVGPattern"), aResult);
68   }
69 #endif  // DEBUG
70 
71  protected:
72   // Internal methods for handling referenced patterns
73   nsSVGPatternFrame* GetReferencedPattern();
74 
75   // Accessors to lookup pattern attributes
76   uint16_t GetEnumValue(uint32_t aIndex, nsIContent* aDefault);
GetEnumValue(uint32_t aIndex)77   uint16_t GetEnumValue(uint32_t aIndex) {
78     return GetEnumValue(aIndex, mContent);
79   }
80   mozilla::nsSVGAnimatedTransformList* GetPatternTransformList(
81       nsIContent* aDefault);
82   gfxMatrix GetPatternTransform();
83   const nsSVGViewBox& GetViewBox(nsIContent* aDefault);
GetViewBox()84   const nsSVGViewBox& GetViewBox() { return GetViewBox(mContent); }
85   const SVGAnimatedPreserveAspectRatio& GetPreserveAspectRatio(
86       nsIContent* aDefault);
GetPreserveAspectRatio()87   const SVGAnimatedPreserveAspectRatio& GetPreserveAspectRatio() {
88     return GetPreserveAspectRatio(mContent);
89   }
90   const nsSVGLength2* GetLengthValue(uint32_t aIndex, nsIContent* aDefault);
GetLengthValue(uint32_t aIndex)91   const nsSVGLength2* GetLengthValue(uint32_t aIndex) {
92     return GetLengthValue(aIndex, mContent);
93   }
94 
95   already_AddRefed<SourceSurface> PaintPattern(
96       const DrawTarget* aDrawTarget, Matrix* patternMatrix,
97       const Matrix& aContextMatrix, nsIFrame* aSource,
98       nsStyleSVGPaint nsStyleSVG::*aFillOrStroke, float aGraphicOpacity,
99       const gfxRect* aOverrideBounds, imgDrawingParams& aImgParams);
100 
101   /**
102    * A <pattern> element may reference another <pattern> element using
103    * xlink:href and, if it doesn't have any child content of its own, then it
104    * will "inherit" the children of the referenced pattern (which may itself be
105    * inheriting its children if it references another <pattern>).  This
106    * function returns this nsSVGPatternFrame or the first pattern along the
107    * reference chain (if there is one) to have children.
108    */
109   nsSVGPatternFrame* GetPatternWithChildren();
110 
111   gfxRect GetPatternRect(uint16_t aPatternUnits, const gfxRect& bbox,
112                          const Matrix& callerCTM, nsIFrame* aTarget);
113   gfxMatrix ConstructCTM(const nsSVGViewBox& aViewBox,
114                          uint16_t aPatternContentUnits, uint16_t aPatternUnits,
115                          const gfxRect& callerBBox, const Matrix& callerCTM,
116                          nsIFrame* aTarget);
117 
118  private:
119   // this is a *temporary* reference to the frame of the element currently
120   // referencing our pattern.  This must be temporary because different
121   // referencing frames will all reference this one frame
122   mozilla::SVGGeometryFrame* mSource;
123   nsAutoPtr<gfxMatrix> mCTM;
124 
125  protected:
126   // This flag is used to detect loops in xlink:href processing
127   bool mLoopFlag;
128   bool mNoHRefURI;
129 };
130 
131 #endif
132