1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MEDIAPROVIDER_PDF_JNI_PDFCLIENT_RECT_H_
18 #define MEDIAPROVIDER_PDF_JNI_PDFCLIENT_RECT_H_
19
20 // Simple points and rectangles.
21
22 namespace pdfClient {
23
24 // A point with integer precision
25 struct Point_i {
26 int x;
27 int y;
28 };
29
30 // A point with double precision
31 struct Point_d {
32 double x;
33 double y;
34 };
35
36 // A rectangle with integer precision
37 struct Rectangle_i {
38 // The x-coordinate of the left-top corner (lesser value).
39 int left;
40 // The y-coordinate of the left-top corner (lesser value).
41 int top;
42 // The x-coordinate of the right-bottom corner (greater value).
43 int right;
44 // The y-coordinate of the right-bottom corner (greater value).
45 int bottom;
46
WidthRectangle_i47 int Width() const { return right - left; }
48
HeightRectangle_i49 int Height() const { return bottom - top; }
50
CenterRectangle_i51 Point_i Center() const { return Point_i{(left + right) / 2, (top + bottom) / 2}; }
52 };
53
54 // A rectangle with double precision
55 struct Rectangle_d {
56 // The x-coordinate of the left-top corner (lesser value).
57 double left;
58 // The y-coordinate of the left-top corner (lesser value).
59 double top;
60 // The x-coordinate of the right-bottom corner (greater value).
61 double right;
62 // The y-coordinate of the right-bottom corner (greater value).
63 double bottom;
64
WidthRectangle_d65 double Width() const { return right - left; }
66
HeightRectangle_d67 double Height() const { return bottom - top; }
68
CenterRectangle_d69 Point_d Center() const { return Point_d{(left + right) / 2, (top + bottom) / 2}; }
70 };
71
72 // Check if two points are equal:
73 inline bool operator==(const Point_i& lhs, const Point_i& rhs) {
74 return lhs.x == rhs.x && lhs.y == rhs.y;
75 }
76
77 inline bool operator!=(const Point_i& lhs, const Point_i& rhs) {
78 return !(lhs == rhs);
79 }
80
81 // Check if two rectangles are equal:
82 inline bool operator==(const Rectangle_i& lhs, const Rectangle_i& rhs) {
83 return lhs.left == rhs.left && lhs.top == rhs.top && lhs.right == rhs.right &&
84 lhs.bottom == rhs.bottom;
85 }
86
87 inline bool operator!=(const Rectangle_i& lhs, const Rectangle_i& rhs) {
88 return !(lhs == rhs);
89 }
90
91 // Check if this rectangle has zero area.
IsEmpty(const Rectangle_i & rect)92 inline bool IsEmpty(const Rectangle_i& rect) {
93 return rect.Width() <= 0 || rect.Height() <= 0;
94 }
95
IsEmpty(const Rectangle_d & rect)96 inline bool IsEmpty(const Rectangle_d& rect) {
97 return rect.Width() <= 0 || rect.Height() <= 0;
98 }
99
100 // Initialize a point:
101 Point_i IntPoint(const int x, const int y);
102
103 // Initialize a point:
104 Point_d DoublePoint(const double x, const double y);
105
106 // Initialize a sorted Rectangle_i with corners at these two points.
107 Rectangle_i IntRect(const int x1, const int y1, const int x2, const int y2);
108
109 // Initialize a sorted Rectangle_i with corners at these two points.
110 Rectangle_i IntRect(const Point_i& p1, const Point_i& p2);
111
112 // Initialize a Rectangle_i based on its top left corner and size
113 Rectangle_i IntRectWithSize(const int left, const int top, const int width, const int height);
114
115 // Returns the smallest Rectangle_i that surrounds the given Rectangle_d.
116 Rectangle_i OuterIntRect(const Rectangle_d& input);
117
118 // Initialize a sorted Rectangle_d with corners at these two points.
119 Rectangle_d DoubleRect(const double x1, const double y1, const double x2, const double y2);
120
121 // Returns the intersection of two rectangles - but if they don't intersect
122 // at all, returns the rectangle (0, 0)-(0, 0).
123 Rectangle_i Intersect(const Rectangle_i& lhs, const Rectangle_i& rhs);
124 Rectangle_d Intersect(const Rectangle_d& lhs, const Rectangle_d& rhs);
125
126 // Returns the union of two Rectangles.
127 Rectangle_i Union(const Rectangle_i& lhs, const Rectangle_i& rhs);
128 Rectangle_d Union(const Rectangle_d& lhs, const Rectangle_d& rhs);
129
130 } // namespace pdfClient
131
132 #endif // MEDIAPROVIDER_PDF_JNI_PDFCLIENT_RECT_H_