2 * Copyright (C) 2010, 2011 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include <wtf/Vector.h>
37 Region(const IntRect&);
39 IntRect bounds() const { return m_bounds; }
40 bool isEmpty() const { return m_bounds.isEmpty(); }
41 bool isRect() const { return m_shape.isRect(); }
43 Vector<IntRect> rects() const;
45 void unite(const Region&);
46 void intersect(const Region&);
47 void subtract(const Region&);
49 void translate(const IntSize&);
51 // Returns true if the query region is a subset of this region.
52 bool contains(const Region&) const;
54 bool contains(const IntPoint&) const;
56 // Returns true if the query region intersects any part of this region.
57 bool intersects(const Region&) const;
59 unsigned totalArea() const;
67 Span(int y, size_t segmentIndex)
68 : y(y), segmentIndex(segmentIndex)
79 Shape(const IntRect&);
81 IntRect bounds() const;
82 bool isEmpty() const { return m_spans.isEmpty(); }
83 bool isRect() const { return m_spans.size() <= 2 && m_segments.size() <= 2; }
85 typedef const Span* SpanIterator;
86 SpanIterator spans_begin() const;
87 SpanIterator spans_end() const;
89 typedef const int* SegmentIterator;
90 SegmentIterator segments_begin(SpanIterator) const;
91 SegmentIterator segments_end(SpanIterator) const;
93 static Shape unionShapes(const Shape& shape1, const Shape& shape2);
94 static Shape intersectShapes(const Shape& shape1, const Shape& shape2);
95 static Shape subtractShapes(const Shape& shape1, const Shape& shape2);
97 void translate(const IntSize&);
100 struct CompareContainsOperation;
101 struct CompareIntersectsOperation;
103 template<typename CompareOperation>
104 static bool compareShapes(const Shape& shape1, const Shape& shape2);
111 struct UnionOperation;
112 struct IntersectOperation;
113 struct SubtractOperation;
115 template<typename Operation>
116 static Shape shapeOperation(const Shape& shape1, const Shape& shape2);
118 void appendSegment(int x);
119 void appendSpan(int y);
120 void appendSpan(int y, SegmentIterator begin, SegmentIterator end);
121 void appendSpans(const Shape&, SpanIterator begin, SpanIterator end);
123 bool canCoalesce(SegmentIterator begin, SegmentIterator end);
125 Vector<int, 32> m_segments;
126 Vector<Span, 16> m_spans;
128 friend bool operator==(const Shape&, const Shape&);
134 friend bool operator==(const Region&, const Region&);
135 friend bool operator==(const Shape&, const Shape&);
136 friend bool operator==(const Span&, const Span&);
139 static inline Region intersect(const Region& a, const Region& b)
147 static inline Region subtract(const Region& a, const Region& b)
155 static inline Region translate(const Region& region, const IntSize& offset)
157 Region result(region);
158 result.translate(offset);
163 inline bool operator==(const Region& a, const Region& b)
165 return a.m_bounds == b.m_bounds && a.m_shape == b.m_shape;
168 inline bool operator==(const Region::Shape& a, const Region::Shape& b)
170 return a.m_spans == b.m_spans && a.m_segments == b.m_segments;
173 inline bool operator==(const Region::Span& a, const Region::Span& b)
175 return a.y == b.y && a.segmentIndex == b.segmentIndex;
178 } // namespace WebCore