michael@0: /* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: package org.mozilla.gecko.gfx; michael@0: michael@0: import org.mozilla.gecko.util.FloatUtils; michael@0: michael@0: import org.json.JSONException; michael@0: import org.json.JSONObject; michael@0: michael@0: import android.graphics.Point; michael@0: import android.graphics.PointF; michael@0: import android.graphics.Rect; michael@0: import android.graphics.RectF; michael@0: michael@0: public final class RectUtils { michael@0: private RectUtils() {} michael@0: michael@0: public static Rect create(JSONObject json) { michael@0: try { michael@0: int x = json.getInt("x"); michael@0: int y = json.getInt("y"); michael@0: int width = json.getInt("width"); michael@0: int height = json.getInt("height"); michael@0: return new Rect(x, y, x + width, y + height); michael@0: } catch (JSONException e) { michael@0: throw new RuntimeException(e); michael@0: } michael@0: } michael@0: michael@0: public static String toJSON(RectF rect) { michael@0: StringBuilder sb = new StringBuilder(256); michael@0: sb.append("{ \"left\": ").append(rect.left) michael@0: .append(", \"top\": ").append(rect.top) michael@0: .append(", \"right\": ").append(rect.right) michael@0: .append(", \"bottom\": ").append(rect.bottom) michael@0: .append('}'); michael@0: return sb.toString(); michael@0: } michael@0: michael@0: public static RectF expand(RectF rect, float moreWidth, float moreHeight) { michael@0: float halfMoreWidth = moreWidth / 2; michael@0: float halfMoreHeight = moreHeight / 2; michael@0: return new RectF(rect.left - halfMoreWidth, michael@0: rect.top - halfMoreHeight, michael@0: rect.right + halfMoreWidth, michael@0: rect.bottom + halfMoreHeight); michael@0: } michael@0: michael@0: public static RectF contract(RectF rect, float lessWidth, float lessHeight) { michael@0: float halfLessWidth = lessWidth / 2.0f; michael@0: float halfLessHeight = lessHeight / 2.0f; michael@0: return new RectF(rect.left + halfLessWidth, michael@0: rect.top + halfLessHeight, michael@0: rect.right - halfLessWidth, michael@0: rect.bottom - halfLessHeight); michael@0: } michael@0: michael@0: public static RectF intersect(RectF one, RectF two) { michael@0: float left = Math.max(one.left, two.left); michael@0: float top = Math.max(one.top, two.top); michael@0: float right = Math.min(one.right, two.right); michael@0: float bottom = Math.min(one.bottom, two.bottom); michael@0: return new RectF(left, top, Math.max(right, left), Math.max(bottom, top)); michael@0: } michael@0: michael@0: public static RectF scale(RectF rect, float scale) { michael@0: float x = rect.left * scale; michael@0: float y = rect.top * scale; michael@0: return new RectF(x, y, michael@0: x + (rect.width() * scale), michael@0: y + (rect.height() * scale)); michael@0: } michael@0: michael@0: public static RectF scaleAndRound(RectF rect, float scale) { michael@0: float left = rect.left * scale; michael@0: float top = rect.top * scale; michael@0: return new RectF(Math.round(left), michael@0: Math.round(top), michael@0: Math.round(left + (rect.width() * scale)), michael@0: Math.round(top + (rect.height() * scale))); michael@0: } michael@0: michael@0: /** Returns the nearest integer rect of the given rect. */ michael@0: public static Rect round(RectF rect) { michael@0: Rect r = new Rect(); michael@0: round(rect, r); michael@0: return r; michael@0: } michael@0: michael@0: public static void round(RectF rect, Rect dest) { michael@0: dest.set(Math.round(rect.left), Math.round(rect.top), michael@0: Math.round(rect.right), Math.round(rect.bottom)); michael@0: } michael@0: michael@0: public static Rect roundIn(RectF rect) { michael@0: return new Rect((int)Math.ceil(rect.left), (int)Math.ceil(rect.top), michael@0: (int)Math.floor(rect.right), (int)Math.floor(rect.bottom)); michael@0: } michael@0: michael@0: public static IntSize getSize(Rect rect) { michael@0: return new IntSize(rect.width(), rect.height()); michael@0: } michael@0: michael@0: public static Point getOrigin(Rect rect) { michael@0: return new Point(rect.left, rect.top); michael@0: } michael@0: michael@0: public static PointF getOrigin(RectF rect) { michael@0: return new PointF(rect.left, rect.top); michael@0: } michael@0: michael@0: public static boolean fuzzyEquals(RectF a, RectF b) { michael@0: if (a == null && b == null) michael@0: return true; michael@0: else if ((a == null && b != null) || (a != null && b == null)) michael@0: return false; michael@0: else michael@0: return FloatUtils.fuzzyEquals(a.top, b.top) michael@0: && FloatUtils.fuzzyEquals(a.left, b.left) michael@0: && FloatUtils.fuzzyEquals(a.right, b.right) michael@0: && FloatUtils.fuzzyEquals(a.bottom, b.bottom); michael@0: } michael@0: }