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() { return mXAbsoluteCss; }
88 * @return Absolute CSS (document) Y coordinate of the point.
90 public float getYAbsoluteCss() { return mYAbsoluteCss; }
93 * @return Local device-scale-unadjusted X coordinate of the point.
95 public float getXLocalDip() { return (mXAbsoluteCss - mScrollXCss) * mPageScaleFactor; }
98 * @return Local device-scale-unadjusted Y coordinate of the point.
100 public float getYLocalDip() { return (mYAbsoluteCss - mScrollYCss) * mPageScaleFactor; }
103 * @return Physical (screen) X coordinate of the point.
105 public float getXPix() { return getXLocalDip() * mDeviceScaleFactor; }
108 * @return Physical (screen) Y coordinate of the point.
110 public float getYPix() { return getYLocalDip() * mDeviceScaleFactor + mContentOffsetYPix; }
113 * Sets the point to the given absolute CSS (document) coordinates.
115 public void setAbsoluteCss(float xCss, float yCss) {
116 mXAbsoluteCss = xCss;
117 mYAbsoluteCss = yCss;
121 * Sets the point to the given local device-scale-unadjusted coordinates.
123 public void setLocalDip(float xDip, float yDip) {
125 xDip / mPageScaleFactor + mScrollXCss,
126 yDip / mPageScaleFactor + mScrollYCss);
130 * Sets the point to the given physical (screen) coordinates.
132 public void setScreen(float xPix, float yPix) {
133 setLocalDip(xPix / mDeviceScaleFactor, yPix / mDeviceScaleFactor);
138 * @return A helper to convert a point between between absolute CSS and local DIP spaces.
140 public NormalizedPoint createNormalizedPoint() {
141 return new NormalizedPoint();
145 * @return Horizontal scroll offset in CSS pixels.
147 public float getScrollX() { return mScrollXCss; }
150 * @return Vertical scroll offset in CSS pixels.
152 public float getScrollY() { return mScrollYCss; }
155 * @return Horizontal scroll offset in physical pixels.
157 public float getScrollXPix() { return fromLocalCssToPix(mScrollXCss); }
160 * @return Vertical scroll offset in physical pixels.
162 public float getScrollYPix() { return fromLocalCssToPix(mScrollYCss); }
165 * @return Horizontal scroll offset in physical pixels (approx, integer).
167 public int getScrollXPixInt() { return (int) Math.floor(getScrollXPix()); }
170 * @return Vertical scroll offset in physical pixels (approx, integer).
172 public int getScrollYPixInt() { return (int) Math.floor(getScrollYPix()); }
175 * @return Width of the content in CSS pixels.
177 public float getContentWidthCss() { return mContentWidthCss; }
180 * @return Height of the content in CSS pixels.
182 public float getContentHeightCss() { return mContentHeightCss; }
185 * @return Approximate width of the content in physical pixels.
187 public float getContentWidthPix() { return fromLocalCssToPix(mContentWidthCss); }
190 * @return Approximate height of the content in physical pixels.
192 public float getContentHeightPix() { return fromLocalCssToPix(mContentHeightCss); }
195 * @return Approximate width of the content in physical pixels (integer).
197 public int getContentWidthPixInt() { return (int) Math.ceil(getContentWidthPix()); }
200 * @return Approximate height of the content in physical pixels (integer).
202 public int getContentHeightPixInt() { return (int) Math.ceil(getContentHeightPix()); }
205 * @return Render-reported width of the viewport in CSS pixels.
207 public float getLastFrameViewportWidthCss() { return mLastFrameViewportWidthCss; }
210 * @return Render-reported height of the viewport in CSS pixels.
212 public float getLastFrameViewportHeightCss() { return mLastFrameViewportHeightCss; }
215 * @return Render-reported width of the viewport in physical pixels (approximate).
217 public float getLastFrameViewportWidthPix() {
218 return fromLocalCssToPix(mLastFrameViewportWidthCss);
222 * @return Render-reported height of the viewport in physical pixels (approximate).
224 public float getLastFrameViewportHeightPix() {
225 return fromLocalCssToPix(mLastFrameViewportHeightCss);
229 * @return Render-reported width of the viewport in physical pixels (approx, integer).
231 public int getLastFrameViewportWidthPixInt() {
232 return (int) Math.ceil(getLastFrameViewportWidthPix());
236 * @return Render-reported height of the viewport in physical pixels (approx, integer).
238 public int getLastFrameViewportHeightPixInt() {
239 return (int) Math.ceil(getLastFrameViewportHeightPix());
243 * @return The Physical on-screen Y offset amount below the top controls.
245 public float getContentOffsetYPix() {
246 return mContentOffsetYPix;
250 * @return Current page scale factor (maps CSS pixels to DIP pixels).
252 public float getPageScaleFactor() { return mPageScaleFactor; }
255 * @return Minimum page scale factor to be used with the content.
257 public float getMinPageScaleFactor() { return mMinPageScaleFactor; }
260 * @return Maximum page scale factor to be used with the content.
262 public float getMaxPageScaleFactor() { return mMaxPageScaleFactor; }
265 * @return Current device scale factor (maps DIP pixels to physical pixels).
267 public float getDeviceScaleFactor() { return mDeviceScaleFactor; }
270 * @return Maximum possible horizontal scroll in physical pixels.
272 public float getMaxHorizontalScrollPix() {
273 return getContentWidthPix() - getLastFrameViewportWidthPix();
277 * @return Maximum possible vertical scroll in physical pixels.
279 public float getMaxVerticalScrollPix() {
280 return getContentHeightPix() - getLastFrameViewportHeightPix();
284 * @return Maximum possible horizontal scroll in physical pixels (approx, integer).
286 public int getMaxHorizontalScrollPixInt() {
287 return (int) Math.floor(getMaxHorizontalScrollPix());
291 * @return Maximum possible vertical scroll in physical pixels (approx, integer).
293 public int getMaxVerticalScrollPixInt() {
294 return (int) Math.floor(getMaxVerticalScrollPix());
298 * @return Physical on-screen coordinate converted to local DIP.
300 public float fromPixToDip(float pix) {
301 return pix / mDeviceScaleFactor;
305 * @return Local DIP converted to physical coordinates.
307 public float fromDipToPix(float dip) {
308 return dip * mDeviceScaleFactor;
312 * @return Physical coordinate converted to local CSS.
314 public float fromPixToLocalCss(float pix) {
315 return pix / (mDeviceScaleFactor * mPageScaleFactor);
319 * @return Local CSS converted to physical coordinates.
321 public float fromLocalCssToPix(float css) {
322 return css * mPageScaleFactor * mDeviceScaleFactor;