File: contoured_rect.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (301 lines) | stat: -rw-r--r-- 10,956 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_CONTOURED_RECT_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_CONTOURED_RECT_H_

#include <optional>

#include "third_party/blink/renderer/platform/geometry/float_rounded_rect.h"
#include "third_party/blink/renderer/platform/platform_export.h"
#include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "ui/gfx/geometry/insets_f.h"
#include "ui/gfx/geometry/outsets_f.h"
#include "ui/gfx/geometry/rect_f.h"
#include "ui/gfx/geometry/rrect_f.h"
#include "ui/gfx/geometry/size_f.h"
#include "ui/gfx/geometry/vector2d_f.h"

namespace gfx {
class QuadF;
}

namespace blink {

class Path;

// A ContouredRect is a rect with corners that can have arbitrary
// radius and curvature, as in not necessarily rounded.
// It is a superset of FloatRoundedRect, with the added information needed
// to render corner shapes correctly.
class PLATFORM_EXPORT ContouredRect {
  DISALLOW_NEW();

 public:
  // A corner curvature is the exponent of a superellipse quadrant.
  // e.g. 2 is round, 1 is bevel/angled, infinity is defunct/straight.
  class CornerCurvature {
   public:
    static constexpr float kRound = 2;
    static constexpr float kBevel = 1;
    static constexpr float kStraight = 1000;
    static constexpr float kNotch = 1 / kStraight;

    constexpr CornerCurvature() = default;
    constexpr CornerCurvature(float top_left,
                              float top_right,
                              float bottom_right,
                              float bottom_left)
        : top_left_(top_left),
          top_right_(top_right),
          bottom_right_(bottom_right),
          bottom_left_(bottom_left) {
      DCHECK_GE(top_left, 0);
      DCHECK_GE(top_right, 0);
      DCHECK_GE(bottom_right, 0);
      DCHECK_GE(bottom_left, 0);
    }

    constexpr bool IsRound() const {
      return (top_left_ == kRound) && IsUniform();
    }

    constexpr bool IsConvex() const {
      return top_left_ >= kBevel && top_right_ >= kBevel &&
             bottom_right_ >= kBevel && bottom_left_ >= kBevel;
    }

    constexpr bool IsUniform() const {
      return top_left_ == top_right_ && top_left_ == bottom_right_ &&
             top_left_ == bottom_left_;
    }

    constexpr float TopLeft() const { return top_left_; }
    constexpr float TopRight() const { return top_right_; }
    constexpr float BottomRight() const { return bottom_right_; }
    constexpr float BottomLeft() const { return bottom_left_; }

    constexpr bool operator==(const CornerCurvature&) const = default;

    String ToString() const;

   private:
    float top_left_ = kRound;
    float top_right_ = kRound;
    float bottom_right_ = kRound;
    float bottom_left_ = kRound;
  };

  // A Corner is a axis-aligned quad, with the points ordered (start, outer,
  // end, center), and a curvature. It is used as a convenient way to perform
  // corner-related computations without having to worry about the
  // transpositions of the different corners.
  class PLATFORM_EXPORT Corner {
   public:
    constexpr Corner(const gfx::RectF& rect, size_t rotation, float curvature)
        : curvature_(curvature) {
      vertices_ = {rect.origin(), rect.top_right(), rect.bottom_right(),
                   rect.bottom_left()};
      std::rotate(vertices_.begin(), vertices_.begin() + rotation,
                  vertices_.end());
    }

    constexpr Corner(const std::array<gfx::PointF, 4>& vertices,
                     float curvature)
        : vertices_(vertices), curvature_(ClampCurvature(curvature)) {}

    constexpr const gfx::PointF& Start() const { return vertices_.at(0); }
    constexpr const gfx::PointF& Outer() const { return vertices_.at(1); }
    constexpr const gfx::PointF& End() const { return vertices_.at(2); }
    constexpr const gfx::PointF& Center() const { return vertices_.at(3); }
    static constexpr float ClampCurvature(float curvature) {
      return std::clamp(curvature, CornerCurvature::kNotch,
                        CornerCurvature::kStraight);
    }
    constexpr float Curvature() const { return curvature_; }
    constexpr bool IsStraight() const {
      return Curvature() == CornerCurvature::kStraight;
    }
    constexpr bool IsBevel() const {
      return curvature_ == CornerCurvature::kBevel;
    }
    constexpr bool IsRound() const {
      return curvature_ == CornerCurvature::kRound;
    }
    constexpr bool IsNotch() const {
      return curvature_ == CornerCurvature::kNotch;
    }
    constexpr bool IsConcave() const { return curvature_ < 1; }
    constexpr bool IsZero() const { return Start() == End(); }
    constexpr bool operator==(const Corner&) const = default;

    // Invert the curvature
    constexpr Corner Inverse() const {
      return Corner({Start(), Center(), End(), Outer()}, 1 / Curvature());
    }

    // Change the direction (clockwise/counter-counterclockwise)
    constexpr Corner Reverse() const {
      return Corner({End(), Outer(), Start(), Center()}, Curvature());
    }

    constexpr gfx::RectF BoundingBox() const {
      return gfx::BoundingRect(Start(), End());
    }

    constexpr bool Intersects(const Corner& other) const {
      return BoundingBox().Intersects(other.BoundingBox());
    }

    constexpr gfx::Vector2dF v1() const { return Outer() - Start(); }
    constexpr gfx::Vector2dF v2() const { return End() - Outer(); }
    constexpr gfx::Vector2dF v3() const { return Center() - End(); }
    constexpr gfx::Vector2dF v4() const { return Start() - Center(); }
    constexpr float DiagonalLength() const {
      return (End() - Start()).Length();
    }
    static constexpr float HalfCornerForCurvature(float curvature) {
      return std::pow(0.5, 1 / ClampCurvature(curvature));
    }

    static float CurvatureForHalfCorner(float half_corner);

    constexpr gfx::PointF MapPoint(
        const gfx::Vector2dF& normalized_point) const {
      return Center() + gfx::ScaleVector2d(v1(), normalized_point.x()) +
             gfx::ScaleVector2d(v4(), normalized_point.y());
    }

    Corner AlignedToOrigin(const Corner& origin) const;
    String ToString() const;

   private:
    std::array<gfx::PointF, 4> vertices_;
    float curvature_;
  };

  constexpr ContouredRect() = default;
  constexpr explicit ContouredRect(const FloatRoundedRect& rect)
      : rect_(rect) {}
  constexpr ContouredRect(const FloatRoundedRect& rect,
                          const CornerCurvature& curvature)
      : rect_(rect), corner_curvature_(curvature) {}
  bool operator==(const ContouredRect&) const = default;
  constexpr const gfx::RectF& BoundingRect() const { return rect_.Rect(); }
  constexpr const CornerCurvature& GetCornerCurvature() const {
    return corner_curvature_;
  }

  constexpr bool HasRoundCurvature() const {
    return corner_curvature_.IsRound() || !IsRounded();
  }

  constexpr bool IsConvex() const {
    return !IsRounded() || corner_curvature_.IsConvex();
  }

  const FloatRoundedRect::Radii& GetRadii() const { return rect_.GetRadii(); }

  void SetRadii(const FloatRoundedRect::Radii& radii) { rect_.SetRadii(radii); }

  bool IsRounded() const { return rect_.IsRounded(); }

  const FloatRoundedRect& AsRoundedRect() const { return rect_; }

  const gfx::RectF& Rect() const { return rect_.Rect(); }

  constexpr bool IsEmpty() const { return rect_.IsEmpty(); }

  void SetCornerCurvature(const CornerCurvature& curvature) {
    corner_curvature_ = curvature;
  }

  void Move(const gfx::Vector2dF& offset) { rect_.Move(offset); }
  void Inset(const gfx::InsetsF& insets) { rect_.Inset(insets); }
  void Inset(float inset) { rect_.Inset(inset); }
  void OutsetForMarginOrShadow(float outset) {
    OutsetForMarginOrShadow(gfx::OutsetsF(outset));
  }

  void OutsetForShapeMargin(float outset) {
    rect_.OutsetForShapeMargin(outset);
  }

  bool XInterceptsAtY(float y,
                      float& min_x_intercept,
                      float& max_x_intercept) const;

  void Outset(const gfx::OutsetsF& outsets) { rect_.Outset(outsets); }
  void OutsetForMarginOrShadow(const gfx::OutsetsF&);

  void ConstrainRadii() { rect_.ConstrainRadii(); }

  bool IntersectsQuad(const gfx::QuadF&) const;

  // Whether the radii are constrained in the size of rect().
  bool IsRenderable() const { return rect_.IsRenderable(); }
  String ToString() const;
  Path GetPath() const;
  const FloatRoundedRect& GetOriginRect() const {
    return origin_rect_ ? *origin_rect_ : rect_;
  }
  void SetOriginRect(const FloatRoundedRect& rect) { origin_rect_ = rect; }

  constexpr bool IsInnerRect() const {
    return origin_rect_ && *origin_rect_ != rect_ &&
           origin_rect_->Rect().Contains(rect_.Rect());
  }

  constexpr Corner TopRightCorner() const {
    return IsInnerRect() ? TopRightCornerInternal().AlignedToOrigin(
                               ContouredRect(*origin_rect_, corner_curvature_)
                                   .TopRightCornerInternal())
                         : TopRightCornerInternal();
  }

  constexpr Corner BottomRightCorner() const {
    return IsInnerRect() ? BottomRightCornerInternal().AlignedToOrigin(
                               ContouredRect(*origin_rect_, corner_curvature_)
                                   .BottomRightCornerInternal())
                         : BottomRightCornerInternal();
  }

  constexpr Corner BottomLeftCorner() const {
    return IsInnerRect() ? BottomLeftCornerInternal().AlignedToOrigin(
                               ContouredRect(*origin_rect_, corner_curvature_)
                                   .BottomLeftCornerInternal())
                         : BottomLeftCornerInternal();
  }

  constexpr Corner TopLeftCorner() const {
    return IsInnerRect() ? TopLeftCornerInternal().AlignedToOrigin(
                               ContouredRect(*origin_rect_, corner_curvature_)
                                   .TopLeftCornerInternal())
                         : TopLeftCornerInternal();
  }

 private:
  constexpr Corner TopRightCornerInternal() const {
    return Corner(rect_.TopRightCorner(), 0, corner_curvature_.TopRight());
  }
  constexpr Corner BottomRightCornerInternal() const {
    return Corner(rect_.BottomRightCorner(), 1,
                  corner_curvature_.BottomRight());
  }
  constexpr Corner BottomLeftCornerInternal() const {
    return Corner(rect_.BottomLeftCorner(), 2, corner_curvature_.BottomLeft());
  }
  constexpr Corner TopLeftCornerInternal() const {
    return Corner(rect_.TopLeftCorner(), 3, corner_curvature_.TopLeft());
  }

  FloatRoundedRect rect_;
  CornerCurvature corner_curvature_;
  std::optional<FloatRoundedRect> origin_rect_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GEOMETRY_CONTOURED_RECT_H_