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.
5 package org.chromium.content.browser;
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).
13 * Unless stated otherwise, all coordinates are in CSS (document) coordinate space.
15 public class RenderCoordinates {
17 // Scroll offset from the native in CSS.
18 private float mScrollXCss;
19 private float mScrollYCss;
21 // Content size from native in CSS.
22 private float mContentWidthCss;
23 private float mContentHeightCss;
25 // Last-frame render-reported viewport size in CSS.
26 private float mLastFrameViewportWidthCss;
27 private float mLastFrameViewportHeightCss;
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;
34 // Cached device density.
35 private float mDeviceScaleFactor;
37 private float mContentOffsetYPix;
39 // Internally-visible set of update methods (used by ContentViewCore).
41 mScrollXCss = mScrollYCss = 0;
42 mPageScaleFactor = 1.0f;
45 void updateContentSizeCss(float contentWidthCss, float contentHeightCss) {
46 mContentWidthCss = contentWidthCss;
47 mContentHeightCss = contentHeightCss;
50 void setDeviceScaleFactor(float deviceScaleFactor) {
51 mDeviceScaleFactor = deviceScaleFactor;
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;
67 updateContentSizeCss(contentWidthCss, contentHeightCss);
68 mLastFrameViewportWidthCss = viewportWidthCss;
69 mLastFrameViewportHeightCss = viewportHeightCss;
73 * Handles conversion of a point from window-relative-local-dip or screen-pix
74 * to document-absolute-CSS space and vice versa.
76 public class NormalizedPoint {
77 private float mXAbsoluteCss, mYAbsoluteCss;
79 private NormalizedPoint() {
83 * @return Absolute CSS (document) X coordinate of the point.
85 public float getXAbsoluteCss() {
90 * @return Absolute CSS (document) Y coordinate of the point.
92 public float getYAbsoluteCss() {
97 * @return Local device-scale-unadjusted X coordinate of the point.
99 public float getXLocalDip() {
100 return (mXAbsoluteCss - mScrollXCss) * mPageScaleFactor;
104 * @return Local device-scale-unadjusted Y coordinate of the point.
106 public float getYLocalDip() {
107 return (mYAbsoluteCss - mScrollYCss) * mPageScaleFactor;
111 * @return Physical (screen) X coordinate of the point.
113 public float getXPix() {
114 return getXLocalDip() * mDeviceScaleFactor;
118 * @return Physical (screen) Y coordinate of the point.
120 public float getYPix() {
121 return getYLocalDip() * mDeviceScaleFactor + mContentOffsetYPix;
125 * Sets the point to the given absolute CSS (document) coordinates.
127 public void setAbsoluteCss(float xCss, float yCss) {
128 mXAbsoluteCss = xCss;
129 mYAbsoluteCss = yCss;
133 * Sets the point to the given local device-scale-unadjusted coordinates.
135 public void setLocalDip(float xDip, float yDip) {
137 xDip / mPageScaleFactor + mScrollXCss,
138 yDip / mPageScaleFactor + mScrollYCss);
142 * Sets the point to the given physical (screen) coordinates.
144 public void setScreen(float xPix, float yPix) {
145 setLocalDip(xPix / mDeviceScaleFactor, yPix / mDeviceScaleFactor);
150 * @return A helper to convert a point between between absolute CSS and local DIP spaces.
152 public NormalizedPoint createNormalizedPoint() {
153 return new NormalizedPoint();
157 * @return Horizontal scroll offset in CSS pixels.
159 public float getScrollX() {
164 * @return Vertical scroll offset in CSS pixels.
166 public float getScrollY() {
171 * @return Horizontal scroll offset in physical pixels.
173 public float getScrollXPix() {
174 return fromLocalCssToPix(mScrollXCss);
178 * @return Vertical scroll offset in physical pixels.
180 public float getScrollYPix() {
181 return fromLocalCssToPix(mScrollYCss);
185 * @return Horizontal scroll offset in physical pixels (approx, integer).
187 public int getScrollXPixInt() {
188 return (int) Math.floor(getScrollXPix());
192 * @return Vertical scroll offset in physical pixels (approx, integer).
194 public int getScrollYPixInt() {
195 return (int) Math.floor(getScrollYPix());
199 * @return Width of the content in CSS pixels.
201 public float getContentWidthCss() {
202 return mContentWidthCss;
206 * @return Height of the content in CSS pixels.
208 public float getContentHeightCss() {
209 return mContentHeightCss;
213 * @return Approximate width of the content in physical pixels.
215 public float getContentWidthPix() {
216 return fromLocalCssToPix(mContentWidthCss);
220 * @return Approximate height of the content in physical pixels.
222 public float getContentHeightPix() {
223 return fromLocalCssToPix(mContentHeightCss);
227 * @return Approximate width of the content in physical pixels (integer).
229 public int getContentWidthPixInt() {
230 return (int) Math.ceil(getContentWidthPix());
234 * @return Approximate height of the content in physical pixels (integer).
236 public int getContentHeightPixInt() {
237 return (int) Math.ceil(getContentHeightPix());
241 * @return Render-reported width of the viewport in CSS pixels.
243 public float getLastFrameViewportWidthCss() {
244 return mLastFrameViewportWidthCss;
248 * @return Render-reported height of the viewport in CSS pixels.
250 public float getLastFrameViewportHeightCss() {
251 return mLastFrameViewportHeightCss;
255 * @return Render-reported width of the viewport in physical pixels (approximate).
257 public float getLastFrameViewportWidthPix() {
258 return fromLocalCssToPix(mLastFrameViewportWidthCss);
262 * @return Render-reported height of the viewport in physical pixels (approximate).
264 public float getLastFrameViewportHeightPix() {
265 return fromLocalCssToPix(mLastFrameViewportHeightCss);
269 * @return Render-reported width of the viewport in physical pixels (approx, integer).
271 public int getLastFrameViewportWidthPixInt() {
272 return (int) Math.ceil(getLastFrameViewportWidthPix());
276 * @return Render-reported height of the viewport in physical pixels (approx, integer).
278 public int getLastFrameViewportHeightPixInt() {
279 return (int) Math.ceil(getLastFrameViewportHeightPix());
283 * @return The Physical on-screen Y offset amount below the top controls.
285 public float getContentOffsetYPix() {
286 return mContentOffsetYPix;
290 * @return Current page scale factor (maps CSS pixels to DIP pixels).
292 public float getPageScaleFactor() {
293 return mPageScaleFactor;
297 * @return Minimum page scale factor to be used with the content.
299 public float getMinPageScaleFactor() {
300 return mMinPageScaleFactor;
304 * @return Maximum page scale factor to be used with the content.
306 public float getMaxPageScaleFactor() {
307 return mMaxPageScaleFactor;
311 * @return Current device scale factor (maps DIP pixels to physical pixels).
313 public float getDeviceScaleFactor() {
314 return mDeviceScaleFactor;
318 * @return Maximum possible horizontal scroll in physical pixels.
320 public float getMaxHorizontalScrollPix() {
321 return getContentWidthPix() - getLastFrameViewportWidthPix();
325 * @return Maximum possible vertical scroll in physical pixels.
327 public float getMaxVerticalScrollPix() {
328 return getContentHeightPix() - getLastFrameViewportHeightPix();
332 * @return Maximum possible horizontal scroll in physical pixels (approx, integer).
334 public int getMaxHorizontalScrollPixInt() {
335 return (int) Math.floor(getMaxHorizontalScrollPix());
339 * @return Maximum possible vertical scroll in physical pixels (approx, integer).
341 public int getMaxVerticalScrollPixInt() {
342 return (int) Math.floor(getMaxVerticalScrollPix());
346 * @return Physical on-screen coordinate converted to local DIP.
348 public float fromPixToDip(float pix) {
349 return pix / mDeviceScaleFactor;
353 * @return Local DIP converted to physical coordinates.
355 public float fromDipToPix(float dip) {
356 return dip * mDeviceScaleFactor;
360 * @return Physical coordinate converted to local CSS.
362 public float fromPixToLocalCss(float pix) {
363 return pix / (mDeviceScaleFactor * mPageScaleFactor);
367 * @return Local CSS converted to physical coordinates.
369 public float fromLocalCssToPix(float css) {
370 return css * mPageScaleFactor * mDeviceScaleFactor;