1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/gfx/RectUtils.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,126 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko.gfx; 1.10 + 1.11 +import org.mozilla.gecko.util.FloatUtils; 1.12 + 1.13 +import org.json.JSONException; 1.14 +import org.json.JSONObject; 1.15 + 1.16 +import android.graphics.Point; 1.17 +import android.graphics.PointF; 1.18 +import android.graphics.Rect; 1.19 +import android.graphics.RectF; 1.20 + 1.21 +public final class RectUtils { 1.22 + private RectUtils() {} 1.23 + 1.24 + public static Rect create(JSONObject json) { 1.25 + try { 1.26 + int x = json.getInt("x"); 1.27 + int y = json.getInt("y"); 1.28 + int width = json.getInt("width"); 1.29 + int height = json.getInt("height"); 1.30 + return new Rect(x, y, x + width, y + height); 1.31 + } catch (JSONException e) { 1.32 + throw new RuntimeException(e); 1.33 + } 1.34 + } 1.35 + 1.36 + public static String toJSON(RectF rect) { 1.37 + StringBuilder sb = new StringBuilder(256); 1.38 + sb.append("{ \"left\": ").append(rect.left) 1.39 + .append(", \"top\": ").append(rect.top) 1.40 + .append(", \"right\": ").append(rect.right) 1.41 + .append(", \"bottom\": ").append(rect.bottom) 1.42 + .append('}'); 1.43 + return sb.toString(); 1.44 + } 1.45 + 1.46 + public static RectF expand(RectF rect, float moreWidth, float moreHeight) { 1.47 + float halfMoreWidth = moreWidth / 2; 1.48 + float halfMoreHeight = moreHeight / 2; 1.49 + return new RectF(rect.left - halfMoreWidth, 1.50 + rect.top - halfMoreHeight, 1.51 + rect.right + halfMoreWidth, 1.52 + rect.bottom + halfMoreHeight); 1.53 + } 1.54 + 1.55 + public static RectF contract(RectF rect, float lessWidth, float lessHeight) { 1.56 + float halfLessWidth = lessWidth / 2.0f; 1.57 + float halfLessHeight = lessHeight / 2.0f; 1.58 + return new RectF(rect.left + halfLessWidth, 1.59 + rect.top + halfLessHeight, 1.60 + rect.right - halfLessWidth, 1.61 + rect.bottom - halfLessHeight); 1.62 + } 1.63 + 1.64 + public static RectF intersect(RectF one, RectF two) { 1.65 + float left = Math.max(one.left, two.left); 1.66 + float top = Math.max(one.top, two.top); 1.67 + float right = Math.min(one.right, two.right); 1.68 + float bottom = Math.min(one.bottom, two.bottom); 1.69 + return new RectF(left, top, Math.max(right, left), Math.max(bottom, top)); 1.70 + } 1.71 + 1.72 + public static RectF scale(RectF rect, float scale) { 1.73 + float x = rect.left * scale; 1.74 + float y = rect.top * scale; 1.75 + return new RectF(x, y, 1.76 + x + (rect.width() * scale), 1.77 + y + (rect.height() * scale)); 1.78 + } 1.79 + 1.80 + public static RectF scaleAndRound(RectF rect, float scale) { 1.81 + float left = rect.left * scale; 1.82 + float top = rect.top * scale; 1.83 + return new RectF(Math.round(left), 1.84 + Math.round(top), 1.85 + Math.round(left + (rect.width() * scale)), 1.86 + Math.round(top + (rect.height() * scale))); 1.87 + } 1.88 + 1.89 + /** Returns the nearest integer rect of the given rect. */ 1.90 + public static Rect round(RectF rect) { 1.91 + Rect r = new Rect(); 1.92 + round(rect, r); 1.93 + return r; 1.94 + } 1.95 + 1.96 + public static void round(RectF rect, Rect dest) { 1.97 + dest.set(Math.round(rect.left), Math.round(rect.top), 1.98 + Math.round(rect.right), Math.round(rect.bottom)); 1.99 + } 1.100 + 1.101 + public static Rect roundIn(RectF rect) { 1.102 + return new Rect((int)Math.ceil(rect.left), (int)Math.ceil(rect.top), 1.103 + (int)Math.floor(rect.right), (int)Math.floor(rect.bottom)); 1.104 + } 1.105 + 1.106 + public static IntSize getSize(Rect rect) { 1.107 + return new IntSize(rect.width(), rect.height()); 1.108 + } 1.109 + 1.110 + public static Point getOrigin(Rect rect) { 1.111 + return new Point(rect.left, rect.top); 1.112 + } 1.113 + 1.114 + public static PointF getOrigin(RectF rect) { 1.115 + return new PointF(rect.left, rect.top); 1.116 + } 1.117 + 1.118 + public static boolean fuzzyEquals(RectF a, RectF b) { 1.119 + if (a == null && b == null) 1.120 + return true; 1.121 + else if ((a == null && b != null) || (a != null && b == null)) 1.122 + return false; 1.123 + else 1.124 + return FloatUtils.fuzzyEquals(a.top, b.top) 1.125 + && FloatUtils.fuzzyEquals(a.left, b.left) 1.126 + && FloatUtils.fuzzyEquals(a.right, b.right) 1.127 + && FloatUtils.fuzzyEquals(a.bottom, b.bottom); 1.128 + } 1.129 +}