|
1 /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
|
2 * This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 package org.mozilla.gecko.gfx; |
|
7 |
|
8 import org.mozilla.gecko.util.FloatUtils; |
|
9 |
|
10 import org.json.JSONException; |
|
11 import org.json.JSONObject; |
|
12 |
|
13 import android.graphics.Point; |
|
14 import android.graphics.PointF; |
|
15 import android.graphics.Rect; |
|
16 import android.graphics.RectF; |
|
17 |
|
18 public final class RectUtils { |
|
19 private RectUtils() {} |
|
20 |
|
21 public static Rect create(JSONObject json) { |
|
22 try { |
|
23 int x = json.getInt("x"); |
|
24 int y = json.getInt("y"); |
|
25 int width = json.getInt("width"); |
|
26 int height = json.getInt("height"); |
|
27 return new Rect(x, y, x + width, y + height); |
|
28 } catch (JSONException e) { |
|
29 throw new RuntimeException(e); |
|
30 } |
|
31 } |
|
32 |
|
33 public static String toJSON(RectF rect) { |
|
34 StringBuilder sb = new StringBuilder(256); |
|
35 sb.append("{ \"left\": ").append(rect.left) |
|
36 .append(", \"top\": ").append(rect.top) |
|
37 .append(", \"right\": ").append(rect.right) |
|
38 .append(", \"bottom\": ").append(rect.bottom) |
|
39 .append('}'); |
|
40 return sb.toString(); |
|
41 } |
|
42 |
|
43 public static RectF expand(RectF rect, float moreWidth, float moreHeight) { |
|
44 float halfMoreWidth = moreWidth / 2; |
|
45 float halfMoreHeight = moreHeight / 2; |
|
46 return new RectF(rect.left - halfMoreWidth, |
|
47 rect.top - halfMoreHeight, |
|
48 rect.right + halfMoreWidth, |
|
49 rect.bottom + halfMoreHeight); |
|
50 } |
|
51 |
|
52 public static RectF contract(RectF rect, float lessWidth, float lessHeight) { |
|
53 float halfLessWidth = lessWidth / 2.0f; |
|
54 float halfLessHeight = lessHeight / 2.0f; |
|
55 return new RectF(rect.left + halfLessWidth, |
|
56 rect.top + halfLessHeight, |
|
57 rect.right - halfLessWidth, |
|
58 rect.bottom - halfLessHeight); |
|
59 } |
|
60 |
|
61 public static RectF intersect(RectF one, RectF two) { |
|
62 float left = Math.max(one.left, two.left); |
|
63 float top = Math.max(one.top, two.top); |
|
64 float right = Math.min(one.right, two.right); |
|
65 float bottom = Math.min(one.bottom, two.bottom); |
|
66 return new RectF(left, top, Math.max(right, left), Math.max(bottom, top)); |
|
67 } |
|
68 |
|
69 public static RectF scale(RectF rect, float scale) { |
|
70 float x = rect.left * scale; |
|
71 float y = rect.top * scale; |
|
72 return new RectF(x, y, |
|
73 x + (rect.width() * scale), |
|
74 y + (rect.height() * scale)); |
|
75 } |
|
76 |
|
77 public static RectF scaleAndRound(RectF rect, float scale) { |
|
78 float left = rect.left * scale; |
|
79 float top = rect.top * scale; |
|
80 return new RectF(Math.round(left), |
|
81 Math.round(top), |
|
82 Math.round(left + (rect.width() * scale)), |
|
83 Math.round(top + (rect.height() * scale))); |
|
84 } |
|
85 |
|
86 /** Returns the nearest integer rect of the given rect. */ |
|
87 public static Rect round(RectF rect) { |
|
88 Rect r = new Rect(); |
|
89 round(rect, r); |
|
90 return r; |
|
91 } |
|
92 |
|
93 public static void round(RectF rect, Rect dest) { |
|
94 dest.set(Math.round(rect.left), Math.round(rect.top), |
|
95 Math.round(rect.right), Math.round(rect.bottom)); |
|
96 } |
|
97 |
|
98 public static Rect roundIn(RectF rect) { |
|
99 return new Rect((int)Math.ceil(rect.left), (int)Math.ceil(rect.top), |
|
100 (int)Math.floor(rect.right), (int)Math.floor(rect.bottom)); |
|
101 } |
|
102 |
|
103 public static IntSize getSize(Rect rect) { |
|
104 return new IntSize(rect.width(), rect.height()); |
|
105 } |
|
106 |
|
107 public static Point getOrigin(Rect rect) { |
|
108 return new Point(rect.left, rect.top); |
|
109 } |
|
110 |
|
111 public static PointF getOrigin(RectF rect) { |
|
112 return new PointF(rect.left, rect.top); |
|
113 } |
|
114 |
|
115 public static boolean fuzzyEquals(RectF a, RectF b) { |
|
116 if (a == null && b == null) |
|
117 return true; |
|
118 else if ((a == null && b != null) || (a != null && b == null)) |
|
119 return false; |
|
120 else |
|
121 return FloatUtils.fuzzyEquals(a.top, b.top) |
|
122 && FloatUtils.fuzzyEquals(a.left, b.left) |
|
123 && FloatUtils.fuzzyEquals(a.right, b.right) |
|
124 && FloatUtils.fuzzyEquals(a.bottom, b.bottom); |
|
125 } |
|
126 } |