1 /*
2  * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above
9  *    copyright notice, this list of conditions and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above
12  *    copyright notice, this list of conditions and the following
13  *    disclaimer in the documentation and/or other materials
14  *    provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_SHAPE_VALUE_H_
31 #define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_SHAPE_VALUE_H_
32 
33 #include "base/memory/scoped_refptr.h"
34 #include "third_party/blink/renderer/core/style/basic_shapes.h"
35 #include "third_party/blink/renderer/core/style/computed_style_constants.h"
36 #include "third_party/blink/renderer/core/style/data_equivalency.h"
37 #include "third_party/blink/renderer/core/style/style_image.h"
38 
39 namespace blink {
40 
41 class ShapeValue final : public GarbageCollected<ShapeValue> {
42  public:
43   enum ShapeValueType {
44     // The Auto value is defined by a null ShapeValue*
45     kShape,
46     kBox,
47     kImage
48   };
49 
ShapeValue(scoped_refptr<BasicShape> shape,CSSBoxType css_box)50   ShapeValue(scoped_refptr<BasicShape> shape, CSSBoxType css_box)
51       : type_(kShape), shape_(std::move(shape)), css_box_(css_box) {}
ShapeValue(ShapeValueType type)52   ShapeValue(ShapeValueType type)
53       : type_(type), css_box_(CSSBoxType::kMissing) {}
ShapeValue(StyleImage * image)54   ShapeValue(StyleImage* image)
55       : type_(kImage), image_(image), css_box_(CSSBoxType::kContent) {}
ShapeValue(CSSBoxType css_box)56   ShapeValue(CSSBoxType css_box) : type_(kBox), css_box_(css_box) {}
57 
GetType()58   ShapeValueType GetType() const { return type_; }
Shape()59   BasicShape* Shape() const { return shape_.get(); }
60 
GetImage()61   StyleImage* GetImage() const { return image_.Get(); }
SetImage(StyleImage * image)62   void SetImage(StyleImage* image) {
63     DCHECK_EQ(GetType(), kImage);
64     if (image_ != image)
65       image_ = image;
66   }
CssBox()67   CSSBoxType CssBox() const { return css_box_; }
68 
69   bool operator==(const ShapeValue& other) const;
70 
Trace(Visitor * visitor)71   virtual void Trace(Visitor* visitor) const { visitor->Trace(image_); }
72 
73  private:
74   ShapeValueType type_;
75   scoped_refptr<BasicShape> shape_;
76   Member<StyleImage> image_;
77   CSSBoxType css_box_;
78 };
79 
80 inline bool ShapeValue::operator==(const ShapeValue& other) const {
81   if (GetType() != other.GetType())
82     return false;
83 
84   switch (GetType()) {
85     case kShape:
86       return DataEquivalent(Shape(), other.Shape()) &&
87              CssBox() == other.CssBox();
88     case kBox:
89       return CssBox() == other.CssBox();
90     case kImage:
91       return DataEquivalent(GetImage(), other.GetImage());
92   }
93 
94   NOTREACHED();
95   return false;
96 }
97 
98 }  // namespace blink
99 
100 #endif
101