Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / public / android / java / src / org / chromium / content / browser / RenderCoordinates.java
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.content.browser;
6
7 /**
8  * Cached copy of all positions and scales (CSS-to-DIP-to-physical pixels)
9  * reported from the renderer.
10  * Provides wrappers and a utility class to help with coordinate transforms on the client side.
11  * Provides the internally-visible set of update methods (called from ContentViewCore).
12  *
13  * Unless stated otherwise, all coordinates are in CSS (document) coordinate space.
14  */
15 public class RenderCoordinates {
16
17     // Scroll offset from the native in CSS.
18     private float mScrollXCss;
19     private float mScrollYCss;
20
21     // Content size from native in CSS.
22     private float mContentWidthCss;
23     private float mContentHeightCss;
24
25     // Last-frame render-reported viewport size in CSS.
26     private float mLastFrameViewportWidthCss;
27     private float mLastFrameViewportHeightCss;
28
29     // Cached page scale factor from native.
30     private float mPageScaleFactor = 1.0f;
31     private float mMinPageScaleFactor = 1.0f;
32     private float mMaxPageScaleFactor = 1.0f;
33
34     // Cached device density.
35     private float mDeviceScaleFactor;
36
37     private float mContentOffsetYPix;
38
39     // Internally-visible set of update methods (used by ContentViewCore).
40     void reset() {
41         mScrollXCss = mScrollYCss = 0;
42         mPageScaleFactor = 1.0f;
43     }
44
45     void updateContentSizeCss(float contentWidthCss, float contentHeightCss) {
46         mContentWidthCss = contentWidthCss;
47         mContentHeightCss = contentHeightCss;
48     }
49
50     void setDeviceScaleFactor(float deviceScaleFactor) {
51         mDeviceScaleFactor = deviceScaleFactor;
52     }
53
54     void updateFrameInfo(
55             float scrollXCss, float scrollYCss,
56             float contentWidthCss, float contentHeightCss,
57             float viewportWidthCss, float viewportHeightCss,
58             float pageScaleFactor, float minPageScaleFactor, float maxPageScaleFactor,
59             float contentOffsetYPix) {
60         mScrollXCss = scrollXCss;
61         mScrollYCss = scrollYCss;
62         mPageScaleFactor = pageScaleFactor;
63         mMinPageScaleFactor = minPageScaleFactor;
64         mMaxPageScaleFactor = maxPageScaleFactor;
65         mContentOffsetYPix = contentOffsetYPix;
66
67         updateContentSizeCss(contentWidthCss, contentHeightCss);
68         mLastFrameViewportWidthCss = viewportWidthCss;
69         mLastFrameViewportHeightCss = viewportHeightCss;
70     }
71
72     /**
73      * Handles conversion of a point from window-relative-local-dip or screen-pix
74      * to document-absolute-CSS space and vice versa.
75      */
76     public class NormalizedPoint {
77         private float mXAbsoluteCss, mYAbsoluteCss;
78
79         private NormalizedPoint() {
80         }
81
82         /**
83          * @return Absolute CSS (document) X coordinate of the point.
84          */
85         public float getXAbsoluteCss() {
86             return mXAbsoluteCss;
87         }
88
89         /**
90          * @return Absolute CSS (document) Y coordinate of the point.
91          */
92         public float getYAbsoluteCss() {
93             return mYAbsoluteCss;
94         }
95
96         /**
97          * @return Local device-scale-unadjusted X coordinate of the point.
98          */
99         public float getXLocalDip() {
100             return (mXAbsoluteCss - mScrollXCss) * mPageScaleFactor;
101         }
102
103         /**
104          * @return Local device-scale-unadjusted Y coordinate of the point.
105          */
106         public float getYLocalDip() {
107             return (mYAbsoluteCss - mScrollYCss) * mPageScaleFactor;
108         }
109
110         /**
111          * @return Physical (screen) X coordinate of the point.
112          */
113         public float getXPix() {
114             return getXLocalDip() * mDeviceScaleFactor;
115         }
116
117         /**
118          * @return Physical (screen) Y coordinate of the point.
119          */
120         public float getYPix() {
121             return getYLocalDip() * mDeviceScaleFactor + mContentOffsetYPix;
122         }
123
124         /**
125          * Sets the point to the given absolute CSS (document) coordinates.
126          */
127         public void setAbsoluteCss(float xCss, float yCss) {
128             mXAbsoluteCss = xCss;
129             mYAbsoluteCss = yCss;
130         }
131
132         /**
133          * Sets the point to the given local device-scale-unadjusted coordinates.
134          */
135         public void setLocalDip(float xDip, float yDip) {
136             setAbsoluteCss(
137                     xDip / mPageScaleFactor + mScrollXCss,
138                     yDip / mPageScaleFactor + mScrollYCss);
139         }
140
141         /**
142          * Sets the point to the given physical (screen) coordinates.
143          */
144         public void setScreen(float xPix, float yPix) {
145             setLocalDip(xPix / mDeviceScaleFactor, yPix / mDeviceScaleFactor);
146         }
147     }
148
149     /**
150      * @return A helper to convert a point between between absolute CSS and local DIP spaces.
151      */
152     public NormalizedPoint createNormalizedPoint() {
153         return new NormalizedPoint();
154     }
155
156     /**
157      * @return Horizontal scroll offset in CSS pixels.
158      */
159     public float getScrollX() {
160         return mScrollXCss;
161     }
162
163     /**
164      * @return Vertical scroll offset in CSS pixels.
165      */
166     public float getScrollY() {
167         return mScrollYCss;
168     }
169
170     /**
171      * @return Horizontal scroll offset in physical pixels.
172      */
173     public float getScrollXPix() {
174         return fromLocalCssToPix(mScrollXCss);
175     }
176
177     /**
178      * @return Vertical scroll offset in physical pixels.
179      */
180     public float getScrollYPix() {
181         return fromLocalCssToPix(mScrollYCss);
182     }
183
184     /**
185      * @return Horizontal scroll offset in physical pixels (approx, integer).
186      */
187     public int getScrollXPixInt() {
188         return (int) Math.floor(getScrollXPix());
189     }
190
191     /**
192      * @return Vertical scroll offset in physical pixels (approx, integer).
193      */
194     public int getScrollYPixInt() {
195         return (int) Math.floor(getScrollYPix());
196     }
197
198     /**
199      * @return Width of the content in CSS pixels.
200      */
201     public float getContentWidthCss() {
202         return mContentWidthCss;
203     }
204
205     /**
206      * @return Height of the content in CSS pixels.
207      */
208     public float getContentHeightCss() {
209         return mContentHeightCss;
210     }
211
212     /**
213      * @return Approximate width of the content in physical pixels.
214      */
215     public float getContentWidthPix() {
216         return fromLocalCssToPix(mContentWidthCss);
217     }
218
219     /**
220      * @return Approximate height of the content in physical pixels.
221      */
222     public float getContentHeightPix() {
223         return fromLocalCssToPix(mContentHeightCss);
224     }
225
226     /**
227      * @return Approximate width of the content in physical pixels (integer).
228      */
229     public int getContentWidthPixInt() {
230         return (int) Math.ceil(getContentWidthPix());
231     }
232
233     /**
234      * @return Approximate height of the content in physical pixels (integer).
235      */
236     public int getContentHeightPixInt() {
237         return (int) Math.ceil(getContentHeightPix());
238     }
239
240     /**
241      * @return Render-reported width of the viewport in CSS pixels.
242      */
243     public float getLastFrameViewportWidthCss() {
244         return mLastFrameViewportWidthCss;
245     }
246
247     /**
248      * @return Render-reported height of the viewport in CSS pixels.
249      */
250     public float getLastFrameViewportHeightCss() {
251         return mLastFrameViewportHeightCss;
252     }
253
254     /**
255      * @return Render-reported width of the viewport in physical pixels (approximate).
256      */
257     public float getLastFrameViewportWidthPix() {
258         return fromLocalCssToPix(mLastFrameViewportWidthCss);
259     }
260
261     /**
262      * @return Render-reported height of the viewport in physical pixels (approximate).
263      */
264     public float getLastFrameViewportHeightPix() {
265         return fromLocalCssToPix(mLastFrameViewportHeightCss);
266     }
267
268     /**
269      * @return Render-reported width of the viewport in physical pixels (approx, integer).
270      */
271     public int getLastFrameViewportWidthPixInt() {
272         return (int) Math.ceil(getLastFrameViewportWidthPix());
273     }
274
275     /**
276      * @return Render-reported height of the viewport in physical pixels (approx, integer).
277      */
278     public int getLastFrameViewportHeightPixInt() {
279         return (int) Math.ceil(getLastFrameViewportHeightPix());
280     }
281
282     /**
283      * @return The Physical on-screen Y offset amount below the top controls.
284      */
285     public float getContentOffsetYPix() {
286         return mContentOffsetYPix;
287     }
288
289     /**
290      * @return Current page scale factor (maps CSS pixels to DIP pixels).
291      */
292     public float getPageScaleFactor() {
293         return mPageScaleFactor;
294     }
295
296     /**
297      * @return Minimum page scale factor to be used with the content.
298      */
299     public float getMinPageScaleFactor() {
300         return mMinPageScaleFactor;
301     }
302
303     /**
304      * @return Maximum page scale factor to be used with the content.
305      */
306     public float getMaxPageScaleFactor() {
307         return mMaxPageScaleFactor;
308     }
309
310     /**
311      * @return Current device scale factor (maps DIP pixels to physical pixels).
312      */
313     public float getDeviceScaleFactor() {
314         return mDeviceScaleFactor;
315     }
316
317     /**
318      * @return Maximum possible horizontal scroll in physical pixels.
319      */
320     public float getMaxHorizontalScrollPix() {
321         return getContentWidthPix() - getLastFrameViewportWidthPix();
322     }
323
324     /**
325      * @return Maximum possible vertical scroll in physical pixels.
326      */
327     public float getMaxVerticalScrollPix() {
328         return getContentHeightPix() - getLastFrameViewportHeightPix();
329     }
330
331     /**
332      * @return Maximum possible horizontal scroll in physical pixels (approx, integer).
333      */
334     public int getMaxHorizontalScrollPixInt() {
335         return (int) Math.floor(getMaxHorizontalScrollPix());
336     }
337
338     /**
339      * @return Maximum possible vertical scroll in physical pixels (approx, integer).
340      */
341     public int getMaxVerticalScrollPixInt() {
342         return (int) Math.floor(getMaxVerticalScrollPix());
343     }
344
345     /**
346      * @return Physical on-screen coordinate converted to local DIP.
347      */
348     public float fromPixToDip(float pix) {
349         return pix / mDeviceScaleFactor;
350     }
351
352     /**
353      * @return Local DIP converted to physical coordinates.
354      */
355     public float fromDipToPix(float dip) {
356         return dip * mDeviceScaleFactor;
357     }
358
359     /**
360      * @return Physical coordinate converted to local CSS.
361      */
362     public float fromPixToLocalCss(float pix) {
363         return pix / (mDeviceScaleFactor * mPageScaleFactor);
364     }
365
366     /**
367      * @return Local CSS converted to physical coordinates.
368      */
369     public float fromLocalCssToPix(float css) {
370         return css * mPageScaleFactor * mDeviceScaleFactor;
371     }
372 }