Avoid potential negative array index access to cached text.
[LibreOffice.git] / android / source / src / java / org / mozilla / gecko / gfx / ViewportMetrics.java
blobf8b5c2e055dc5d062c322ec40be111f0bbaa6387
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/. */
6 package org.mozilla.gecko.gfx;
8 import android.graphics.PointF;
9 import android.graphics.RectF;
10 import android.util.DisplayMetrics;
12 import org.json.JSONException;
13 import org.json.JSONObject;
15 /**
16 * ViewportMetrics manages state and contains some utility functions related to
17 * the page viewport for the Gecko layer client to use.
19 public class ViewportMetrics {
20 private static final String LOGTAG = "GeckoViewportMetrics";
22 private RectF mPageRect;
23 private RectF mCssPageRect;
24 private RectF mViewportRect;
25 private float mZoomFactor;
27 public ViewportMetrics(DisplayMetrics metrics) {
28 mPageRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
29 mCssPageRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
30 mViewportRect = new RectF(0, 0, metrics.widthPixels, metrics.heightPixels);
31 mZoomFactor = 1.0f;
34 public ViewportMetrics(ViewportMetrics viewport) {
35 mPageRect = new RectF(viewport.getPageRect());
36 mCssPageRect = new RectF(viewport.getCssPageRect());
37 mViewportRect = new RectF(viewport.getViewport());
38 mZoomFactor = viewport.getZoomFactor();
41 public ViewportMetrics(ImmutableViewportMetrics viewport) {
42 mPageRect = new RectF(viewport.pageRectLeft,
43 viewport.pageRectTop,
44 viewport.pageRectRight,
45 viewport.pageRectBottom);
46 mCssPageRect = new RectF(viewport.cssPageRectLeft,
47 viewport.cssPageRectTop,
48 viewport.cssPageRectRight,
49 viewport.cssPageRectBottom);
50 mViewportRect = new RectF(viewport.viewportRectLeft,
51 viewport.viewportRectTop,
52 viewport.viewportRectRight,
53 viewport.viewportRectBottom);
54 mZoomFactor = viewport.zoomFactor;
57 public ViewportMetrics(JSONObject json) throws JSONException {
58 float x = (float)json.getDouble("x");
59 float y = (float)json.getDouble("y");
60 float width = (float)json.getDouble("width");
61 float height = (float)json.getDouble("height");
62 float pageLeft = (float)json.getDouble("pageLeft");
63 float pageTop = (float)json.getDouble("pageTop");
64 float pageRight = (float)json.getDouble("pageRight");
65 float pageBottom = (float)json.getDouble("pageBottom");
66 float cssPageLeft = (float)json.getDouble("cssPageLeft");
67 float cssPageTop = (float)json.getDouble("cssPageTop");
68 float cssPageRight = (float)json.getDouble("cssPageRight");
69 float cssPageBottom = (float)json.getDouble("cssPageBottom");
70 float zoom = (float)json.getDouble("zoom");
72 mPageRect = new RectF(pageLeft, pageTop, pageRight, pageBottom);
73 mCssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
74 mViewportRect = new RectF(x, y, x + width, y + height);
75 mZoomFactor = zoom;
78 public ViewportMetrics(float x, float y, float width, float height,
79 float pageLeft, float pageTop, float pageRight, float pageBottom,
80 float cssPageLeft, float cssPageTop, float cssPageRight, float cssPageBottom,
81 float zoom) {
82 mPageRect = new RectF(pageLeft, pageTop, pageRight, pageBottom);
83 mCssPageRect = new RectF(cssPageLeft, cssPageTop, cssPageRight, cssPageBottom);
84 mViewportRect = new RectF(x, y, x + width, y + height);
85 mZoomFactor = zoom;
88 public PointF getOrigin() {
89 return new PointF(mViewportRect.left, mViewportRect.top);
92 public FloatSize getSize() {
93 return new FloatSize(mViewportRect.width(), mViewportRect.height());
96 public RectF getViewport() {
97 return mViewportRect;
100 public RectF getCssViewport() {
101 return RectUtils.scale(mViewportRect, 1/mZoomFactor);
104 public RectF getPageRect() {
105 return mPageRect;
108 public RectF getCssPageRect() {
109 return mCssPageRect;
112 public float getZoomFactor() {
113 return mZoomFactor;
116 public void setPageRect(RectF pageRect, RectF cssPageRect) {
117 mPageRect = pageRect;
118 mCssPageRect = cssPageRect;
121 public void setViewport(RectF viewport) {
122 mViewportRect = viewport;
125 public void setOrigin(PointF origin) {
126 mViewportRect.set(origin.x, origin.y,
127 origin.x + mViewportRect.width(),
128 origin.y + mViewportRect.height());
131 public void setSize(FloatSize size) {
132 mViewportRect.right = mViewportRect.left + size.width;
133 mViewportRect.bottom = mViewportRect.top + size.height;
136 public void setZoomFactor(float zoomFactor) {
137 mZoomFactor = zoomFactor;
140 public String toJSON() {
141 // Round off height and width. Since the height and width are the size of the screen, it
142 // makes no sense to send non-integer coordinates to Gecko.
143 int height = Math.round(mViewportRect.height());
144 int width = Math.round(mViewportRect.width());
146 StringBuffer sb = new StringBuffer(512);
147 sb.append("{ \"x\" : ").append(mViewportRect.left)
148 .append(", \"y\" : ").append(mViewportRect.top)
149 .append(", \"width\" : ").append(width)
150 .append(", \"height\" : ").append(height)
151 .append(", \"pageLeft\" : ").append(mPageRect.left)
152 .append(", \"pageTop\" : ").append(mPageRect.top)
153 .append(", \"pageRight\" : ").append(mPageRect.right)
154 .append(", \"pageBottom\" : ").append(mPageRect.bottom)
155 .append(", \"cssPageLeft\" : ").append(mCssPageRect.left)
156 .append(", \"cssPageTop\" : ").append(mCssPageRect.top)
157 .append(", \"cssPageRight\" : ").append(mCssPageRect.right)
158 .append(", \"cssPageBottom\" : ").append(mCssPageRect.bottom)
159 .append(", \"zoom\" : ").append(mZoomFactor)
160 .append(" }");
161 return sb.toString();
164 @Override
165 public String toString() {
166 StringBuffer buff = new StringBuffer(256);
167 buff.append("v=").append(mViewportRect.toString())
168 .append(" p=").append(mPageRect.toString())
169 .append(" c=").append(mCssPageRect.toString())
170 .append(" z=").append(mZoomFactor);
171 return buff.toString();