1/*
2 * Copyright (C) 2015 Apple Inc. 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 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#pragma once
27
28#include "LayoutRect.h"
29
30namespace WTF {
31class TextStream;
32}
33
34namespace WebCore {
35
36class HitTestLocation;
37
38class ClipRect {
39public:
40 ClipRect() = default;
41
42 ClipRect(const LayoutRect& rect)
43 : m_rect(rect)
44 {
45 }
46
47 const LayoutRect& rect() const { return m_rect; }
48
49 void reset() { m_rect = LayoutRect::infiniteRect(); }
50
51 bool affectedByRadius() const { return m_affectedByRadius; }
52 void setAffectedByRadius(bool affectedByRadius) { m_affectedByRadius = affectedByRadius; }
53
54 bool operator==(const ClipRect& other) const { return rect() == other.rect() && affectedByRadius() == other.affectedByRadius(); }
55 bool operator!=(const ClipRect& other) const { return rect() != other.rect() || affectedByRadius() != other.affectedByRadius(); }
56 bool operator!=(const LayoutRect& otherRect) const { return rect() != otherRect; }
57
58 void intersect(const LayoutRect& other);
59 void intersect(const ClipRect& other);
60 void move(LayoutUnit x, LayoutUnit y) { m_rect.move(x, y); }
61 void move(const LayoutSize& size) { m_rect.move(size); }
62 void moveBy(const LayoutPoint& point) { m_rect.moveBy(point); }
63
64 bool intersects(const LayoutRect&) const;
65 bool intersects(const HitTestLocation&) const;
66
67 bool isEmpty() const { return m_rect.isEmpty(); }
68 bool isInfinite() const { return m_rect.isInfinite(); }
69
70 void inflateX(LayoutUnit dx) { m_rect.inflateX(dx); }
71 void inflateY(LayoutUnit dy) { m_rect.inflateY(dy); }
72 void inflate(LayoutUnit d) { inflateX(d); inflateY(d); }
73
74private:
75 LayoutRect m_rect;
76 bool m_affectedByRadius = false;
77};
78
79inline void ClipRect::intersect(const LayoutRect& other)
80{
81 if (other.isInfinite())
82 return;
83 if (isInfinite())
84 m_rect = other;
85 else
86 m_rect.intersect(other);
87}
88
89inline void ClipRect::intersect(const ClipRect& other)
90{
91 intersect(other.rect());
92 if (other.affectedByRadius())
93 m_affectedByRadius = true;
94}
95
96inline bool ClipRect::intersects(const LayoutRect& rect) const
97{
98 if (isInfinite() || rect.isInfinite())
99 return true;
100 return m_rect.intersects(rect);
101}
102
103inline ClipRect intersection(const ClipRect& a, const ClipRect& b)
104{
105 ClipRect c = a;
106 c.intersect(b);
107 return c;
108}
109
110WTF::TextStream& operator<<(WTF::TextStream&, const ClipRect&);
111
112} // namespace WebCore
113