1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_ASPECT_RATIO_H_
6 #define THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_ASPECT_RATIO_H_
7 
8 #include "third_party/blink/renderer/platform/geometry/int_size.h"
9 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
10 
11 namespace blink {
12 
13 enum class EAspectRatioType { kAuto, kAutoAndRatio, kRatio };
14 
15 class StyleAspectRatio {
16   DISALLOW_NEW();
17 
18  public:
19   // Style data for aspect-ratio: auto || <ratio>
StyleAspectRatio(EAspectRatioType type,FloatSize ratio)20   StyleAspectRatio(EAspectRatioType type, FloatSize ratio)
21       : type_(static_cast<unsigned>(type)), ratio_(ratio) {}
22 
23   // 0/x and x/0 are valid (and computed style needs to serialize them
24   // as such), but they are not useful for layout, so we map it to auto here.
GetType()25   EAspectRatioType GetType() const {
26     if (ratio_.Width() == 0 || ratio_.Height() == 0)
27       return EAspectRatioType::kAuto;
28     // Since we do calculations on LayoutUnits, also check that our width/height
29     // doesn't convert to zero.
30     if (ratio_.Width() < LayoutUnit::Epsilon() ||
31         ratio_.Height() < LayoutUnit::Epsilon()) {
32       return EAspectRatioType::kAuto;
33     }
34     return GetTypeForComputedStyle();
35   }
36 
GetTypeForComputedStyle()37   EAspectRatioType GetTypeForComputedStyle() const {
38     return static_cast<EAspectRatioType>(type_);
39   }
40 
IsAuto()41   bool IsAuto() const { return GetType() == EAspectRatioType::kAuto; }
42 
GetRatio()43   FloatSize GetRatio() const { return ratio_; }
44 
45   bool operator==(const StyleAspectRatio& o) const {
46     return type_ == o.type_ && ratio_ == o.ratio_;
47   }
48 
49   bool operator!=(const StyleAspectRatio& o) const { return !(*this == o); }
50 
51  private:
52   unsigned type_ : 2;  // EAspectRatioType
53   FloatSize ratio_;
54 };
55 
56 }  // namespace blink
57 
58 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_STYLE_STYLE_ASPECT_RATIO_H_
59