2011-11-23 19:07:29 +00:00
|
|
|
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-11-23 19:07:29 +00:00
|
|
|
|
|
|
|
package org.mozilla.gecko.gfx;
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
import android.graphics.PointF;
|
|
|
|
import android.graphics.RectF;
|
2012-01-07 00:42:48 +00:00
|
|
|
import android.util.DisplayMetrics;
|
2012-07-15 20:20:43 +00:00
|
|
|
|
2011-12-07 18:44:36 +00:00
|
|
|
import org.mozilla.gecko.FloatUtils;
|
2012-01-07 00:42:48 +00:00
|
|
|
import org.mozilla.gecko.GeckoApp;
|
2012-07-15 20:20:43 +00:00
|
|
|
|
2011-11-23 19:07:29 +00:00
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ViewportMetrics manages state and contains some utility functions related to
|
|
|
|
* the page viewport for the Gecko layer client to use.
|
|
|
|
*/
|
2012-07-15 20:20:43 +00:00
|
|
|
public final class ViewportMetrics {
|
2011-12-10 03:58:10 +00:00
|
|
|
private static final String LOGTAG = "GeckoViewportMetrics";
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
private RectF mPageRect;
|
|
|
|
private RectF mCssPageRect;
|
2011-11-23 19:07:47 +00:00
|
|
|
private RectF mViewportRect;
|
|
|
|
private float mZoomFactor;
|
2011-11-23 19:07:29 +00:00
|
|
|
|
|
|
|
public ViewportMetrics() {
|
2012-05-24 01:53:39 +00:00
|
|
|
DisplayMetrics metrics = GeckoApp.mAppContext.getDisplayMetrics();
|
2012-01-07 00:42:48 +00:00
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
mPageRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
|
|
|
|
mCssPageRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
|
2012-01-07 00:42:48 +00:00
|
|
|
mViewportRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
|
2011-11-23 19:07:47 +00:00
|
|
|
mZoomFactor = 1.0f;
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public ViewportMetrics(ViewportMetrics viewport) {
|
2012-05-23 14:49:52 +00:00
|
|
|
mPageRect = new RectF(viewport.getPageRect());
|
|
|
|
mCssPageRect = new RectF(viewport.getCssPageRect());
|
2011-11-23 19:07:47 +00:00
|
|
|
mViewportRect = new RectF(viewport.getViewport());
|
|
|
|
mZoomFactor = viewport.getZoomFactor();
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2012-03-02 19:31:27 +00:00
|
|
|
public ViewportMetrics(ImmutableViewportMetrics viewport) {
|
2012-05-23 14:49:52 +00:00
|
|
|
mPageRect = new RectF(viewport.pageRectLeft,
|
|
|
|
viewport.pageRectTop,
|
|
|
|
viewport.pageRectRight,
|
|
|
|
viewport.pageRectBottom);
|
|
|
|
mCssPageRect = new RectF(viewport.cssPageRectLeft,
|
|
|
|
viewport.cssPageRectTop,
|
|
|
|
viewport.cssPageRectRight,
|
|
|
|
viewport.cssPageRectBottom);
|
2012-03-02 19:31:27 +00:00
|
|
|
mViewportRect = new RectF(viewport.viewportRectLeft,
|
2012-05-23 14:49:52 +00:00
|
|
|
viewport.viewportRectTop,
|
|
|
|
viewport.viewportRectRight,
|
|
|
|
viewport.viewportRectBottom);
|
2012-03-02 19:31:27 +00:00
|
|
|
mZoomFactor = viewport.zoomFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-23 19:07:29 +00:00
|
|
|
public ViewportMetrics(JSONObject json) throws JSONException {
|
2011-11-23 19:07:47 +00:00
|
|
|
float x = (float)json.getDouble("x");
|
|
|
|
float y = (float)json.getDouble("y");
|
|
|
|
float width = (float)json.getDouble("width");
|
|
|
|
float height = (float)json.getDouble("height");
|
2012-05-23 14:49:59 +00:00
|
|
|
float pageLeft = (float)json.getDouble("pageLeft");
|
|
|
|
float pageTop = (float)json.getDouble("pageTop");
|
|
|
|
float pageRight = (float)json.getDouble("pageRight");
|
|
|
|
float pageBottom = (float)json.getDouble("pageBottom");
|
|
|
|
float cssPageLeft = (float)json.getDouble("cssPageLeft");
|
|
|
|
float cssPageTop = (float)json.getDouble("cssPageTop");
|
|
|
|
float cssPageRight = (float)json.getDouble("cssPageRight");
|
|
|
|
float cssPageBottom = (float)json.getDouble("cssPageBottom");
|
2011-11-23 19:07:47 +00:00
|
|
|
float zoom = (float)json.getDouble("zoom");
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
mPageRect = new RectF(pageLeft, pageTop, pageRight, pageBottom);
|
|
|
|
mCssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
|
2011-11-23 19:07:47 +00:00
|
|
|
mViewportRect = new RectF(x, y, x + width, y + height);
|
|
|
|
mZoomFactor = zoom;
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public PointF getOrigin() {
|
|
|
|
return new PointF(mViewportRect.left, mViewportRect.top);
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public FloatSize getSize() {
|
|
|
|
return new FloatSize(mViewportRect.width(), mViewportRect.height());
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public RectF getViewport() {
|
2011-11-23 19:07:29 +00:00
|
|
|
return mViewportRect;
|
|
|
|
}
|
|
|
|
|
2012-04-14 17:18:10 +00:00
|
|
|
public RectF getCssViewport() {
|
|
|
|
return RectUtils.scale(mViewportRect, 1/mZoomFactor);
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:29 +00:00
|
|
|
/** Returns the viewport rectangle, clamped within the page-size. */
|
2011-11-23 19:07:47 +00:00
|
|
|
public RectF getClampedViewport() {
|
|
|
|
RectF clampedViewport = new RectF(mViewportRect);
|
2011-11-23 19:07:29 +00:00
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
// The viewport bounds ought to never exceed the page bounds.
|
|
|
|
if (clampedViewport.right > mPageRect.right)
|
|
|
|
clampedViewport.offset(mPageRect.right - clampedViewport.right, 0);
|
|
|
|
if (clampedViewport.left < mPageRect.left)
|
|
|
|
clampedViewport.offset(mPageRect.left - clampedViewport.left, 0);
|
2011-11-23 19:07:29 +00:00
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
if (clampedViewport.bottom > mPageRect.bottom)
|
|
|
|
clampedViewport.offset(0, mPageRect.bottom - clampedViewport.bottom);
|
|
|
|
if (clampedViewport.top < mPageRect.top)
|
|
|
|
clampedViewport.offset(0, mPageRect.top - clampedViewport.top);
|
2011-11-23 19:07:29 +00:00
|
|
|
|
|
|
|
return clampedViewport;
|
|
|
|
}
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
public RectF getPageRect() {
|
|
|
|
return mPageRect;
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
public RectF getCssPageRect() {
|
|
|
|
return mCssPageRect;
|
2012-04-12 20:00:56 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public float getZoomFactor() {
|
|
|
|
return mZoomFactor;
|
|
|
|
}
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
public void setPageRect(RectF pageRect, RectF cssPageRect) {
|
|
|
|
mPageRect = pageRect;
|
|
|
|
mCssPageRect = cssPageRect;
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public void setViewport(RectF viewport) {
|
2011-11-23 19:07:29 +00:00
|
|
|
mViewportRect = viewport;
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public void setOrigin(PointF origin) {
|
2011-11-23 19:07:29 +00:00
|
|
|
mViewportRect.set(origin.x, origin.y,
|
|
|
|
origin.x + mViewportRect.width(),
|
|
|
|
origin.y + mViewportRect.height());
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public void setSize(FloatSize size) {
|
2011-11-23 19:07:29 +00:00
|
|
|
mViewportRect.right = mViewportRect.left + size.width;
|
|
|
|
mViewportRect.bottom = mViewportRect.top + size.height;
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
public void setZoomFactor(float zoomFactor) {
|
|
|
|
mZoomFactor = zoomFactor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This will set the zoom factor and re-scale page-size and viewport offset
|
|
|
|
* accordingly. The given focus will remain at the same point on the screen
|
|
|
|
* after scaling.
|
|
|
|
*/
|
|
|
|
public void scaleTo(float newZoomFactor, PointF focus) {
|
2012-05-23 14:49:52 +00:00
|
|
|
// mCssPageRect is invariant, since we're setting the scale factor
|
|
|
|
// here. The page rect is based on the CSS page rect.
|
|
|
|
mPageRect = RectUtils.scale(mCssPageRect, newZoomFactor);
|
2011-11-23 19:07:47 +00:00
|
|
|
|
2012-04-12 20:00:56 +00:00
|
|
|
float scaleFactor = newZoomFactor / mZoomFactor;
|
2011-11-23 19:07:47 +00:00
|
|
|
PointF origin = getOrigin();
|
2012-04-12 20:00:56 +00:00
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
origin.offset(focus.x, focus.y);
|
|
|
|
origin = PointUtils.scale(origin, scaleFactor);
|
|
|
|
origin.offset(-focus.x, -focus.y);
|
2012-04-12 20:00:56 +00:00
|
|
|
|
2011-11-23 19:07:47 +00:00
|
|
|
setOrigin(origin);
|
|
|
|
|
|
|
|
mZoomFactor = newZoomFactor;
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-07 18:44:36 +00:00
|
|
|
/*
|
|
|
|
* Returns the viewport metrics that represent a linear transition between `from` and `to` at
|
|
|
|
* time `t`, which is on the scale [0, 1). This function interpolates the viewport rect, the
|
|
|
|
* page size, the offset, and the zoom factor.
|
|
|
|
*/
|
|
|
|
public ViewportMetrics interpolate(ViewportMetrics to, float t) {
|
|
|
|
ViewportMetrics result = new ViewportMetrics();
|
2012-05-23 14:49:52 +00:00
|
|
|
result.mPageRect = RectUtils.interpolate(mPageRect, to.mPageRect, t);
|
|
|
|
result.mCssPageRect = RectUtils.interpolate(mCssPageRect, to.mCssPageRect, t);
|
2011-12-07 18:44:36 +00:00
|
|
|
result.mZoomFactor = FloatUtils.interpolate(mZoomFactor, to.mZoomFactor, t);
|
|
|
|
result.mViewportRect = RectUtils.interpolate(mViewportRect, to.mViewportRect, t);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-12-16 22:01:02 +00:00
|
|
|
public boolean fuzzyEquals(ViewportMetrics other) {
|
2012-05-23 14:49:52 +00:00
|
|
|
return RectUtils.fuzzyEquals(mPageRect, other.mPageRect)
|
|
|
|
&& RectUtils.fuzzyEquals(mCssPageRect, other.mCssPageRect)
|
2011-12-16 22:01:02 +00:00
|
|
|
&& RectUtils.fuzzyEquals(mViewportRect, other.mViewportRect)
|
|
|
|
&& FloatUtils.fuzzyEquals(mZoomFactor, other.mZoomFactor);
|
|
|
|
}
|
|
|
|
|
2011-11-23 19:07:29 +00:00
|
|
|
public String toJSON() {
|
2012-01-21 05:14:03 +00:00
|
|
|
// Round off height and width. Since the height and width are the size of the screen, it
|
|
|
|
// makes no sense to send non-integer coordinates to Gecko.
|
|
|
|
int height = Math.round(mViewportRect.height());
|
|
|
|
int width = Math.round(mViewportRect.width());
|
|
|
|
|
2012-05-23 14:49:52 +00:00
|
|
|
StringBuffer sb = new StringBuffer(512);
|
2012-01-20 14:30:27 +00:00
|
|
|
sb.append("{ \"x\" : ").append(mViewportRect.left)
|
|
|
|
.append(", \"y\" : ").append(mViewportRect.top)
|
2012-01-21 05:14:03 +00:00
|
|
|
.append(", \"width\" : ").append(width)
|
|
|
|
.append(", \"height\" : ").append(height)
|
2012-05-23 14:49:52 +00:00
|
|
|
.append(", \"pageLeft\" : ").append(mPageRect.left)
|
|
|
|
.append(", \"pageTop\" : ").append(mPageRect.top)
|
|
|
|
.append(", \"pageRight\" : ").append(mPageRect.right)
|
|
|
|
.append(", \"pageBottom\" : ").append(mPageRect.bottom)
|
|
|
|
.append(", \"cssPageLeft\" : ").append(mCssPageRect.left)
|
|
|
|
.append(", \"cssPageTop\" : ").append(mCssPageRect.top)
|
|
|
|
.append(", \"cssPageRight\" : ").append(mCssPageRect.right)
|
|
|
|
.append(", \"cssPageBottom\" : ").append(mCssPageRect.bottom)
|
2012-01-20 14:30:27 +00:00
|
|
|
.append(", \"zoom\" : ").append(mZoomFactor)
|
|
|
|
.append(" }");
|
|
|
|
return sb.toString();
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|
2011-12-20 03:28:48 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2012-05-23 14:49:52 +00:00
|
|
|
StringBuffer buff = new StringBuffer(256);
|
2011-12-20 03:28:48 +00:00
|
|
|
buff.append("v=").append(mViewportRect.toString())
|
2012-05-23 14:49:52 +00:00
|
|
|
.append(" p=").append(mPageRect.toString())
|
|
|
|
.append(" c=").append(mCssPageRect.toString())
|
2012-02-20 21:51:37 +00:00
|
|
|
.append(" z=").append(mZoomFactor);
|
2011-12-20 03:28:48 +00:00
|
|
|
return buff.toString();
|
|
|
|
}
|
2011-11-23 19:07:29 +00:00
|
|
|
}
|