Skip to content

Reintroduce scaling methods for Rectangle #2295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ public void testCorrectScaleUpUsingDifferentSetBoundsMethod() {

button.setBounds(new Rectangle(0, 47, 200, 47));
assertEquals("Control::setBounds(Rectangle) doesn't scale up correctly",
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());

button.setBounds(0, 47, 200, 47);
assertEquals("Control::setBounds(int, int, int, int) doesn't scale up correctly",
new Rectangle(0, 82, 350, 82), button.getBoundsInPixels());
new Rectangle(0, 82, 350, 83), button.getBoundsInPixels());
}

record FontComparison(int originalFontHeight, int currentFontHeight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ public static Point pixelToPoint(Drawable drawable, Point point, int zoom) {
}

public static Rectangle pixelToPoint(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pixelToPoint(rectOfFloat, zoom);
Rectangle scaledRect = new Rectangle.OfFloat (0,0,0,0);
Point scaledTopLeft = pixelToPoint(new Point (rect.x, rect.y), zoom);
Point scaledBottomRight = pixelToPoint(new Point (rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
return scaledRect;
}

private static Rectangle pixelToPoint(Rectangle.OfFloat rect, int zoom) {
return scaleBounds(rect, 100, zoom);
}

Expand All @@ -120,6 +134,21 @@ public static Rectangle pixelToPoint(Drawable drawable, Rectangle rect, int zoom
* Returns a new rectangle as per the scaleFactor.
*/
public static Rectangle scaleBounds (Rectangle rect, int targetZoom, int currentZoom) {
if (rect == null || targetZoom == currentZoom) return rect;
if (rect instanceof Rectangle.OfFloat rectOfFloat) return scaleBounds(rectOfFloat, targetZoom, currentZoom);
float scaleFactor = ((float)targetZoom) / (float)currentZoom;
Rectangle returnRect = new Rectangle.OfFloat (0,0,0,0);
returnRect.x = Math.round (rect.x * scaleFactor);
returnRect.y = Math.round (rect.y * scaleFactor);
returnRect.width = Math.round (rect.width * scaleFactor);
returnRect.height = Math.round (rect.height * scaleFactor);
return returnRect;
}

/**
* Returns a new rectangle as per the scaleFactor.
*/
private static Rectangle scaleBounds (Rectangle.OfFloat rect, int targetZoom, int currentZoom) {
if (rect == null || targetZoom == currentZoom) return rect;
Rectangle.OfFloat fRect = FloatAwareGeometryFactory.createFrom(rect);
float scaleFactor = DPIUtil.getScalingFactor(targetZoom, currentZoom);
Expand Down Expand Up @@ -185,6 +214,20 @@ public static Point pointToPixel(Drawable drawable, Point point, int zoom) {
}

public static Rectangle pointToPixel(Rectangle rect, int zoom) {
if (zoom == 100 || rect == null) return rect;
if (rect instanceof Rectangle.OfFloat rectOfFloat) return pointToPixel(rectOfFloat, zoom);
Rectangle scaledRect = new Rectangle.OfFloat(0,0,0,0);
Point scaledTopLeft = pointToPixel (new Point(rect.x, rect.y), zoom);
Point scaledBottomRight = pointToPixel (new Point(rect.x + rect.width, rect.y + rect.height), zoom);

scaledRect.x = scaledTopLeft.x;
scaledRect.y = scaledTopLeft.y;
scaledRect.width = scaledBottomRight.x - scaledTopLeft.x;
scaledRect.height = scaledBottomRight.y - scaledTopLeft.y;
return scaledRect;
}

private static Rectangle pointToPixel(Rectangle.OfFloat rect, int zoom) {
return scaleBounds(rect, zoom, 100);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void scaleUpPoint() {
@Test
public void scaleUpRectangle() {
Rectangle valueAt200 = new Rectangle(100, 150, 10, 14);
Rectangle valueAt150 = new Rectangle(75, 113, 8, 11);
Rectangle valueAt150 = new Rectangle(75, 113, 8, 10);
Rectangle valueAt100 = new Rectangle(50, 75, 5, 7);

Rectangle scaledValue = Win32DPIUtils.pointToPixel(valueAt100, 200);
Expand All @@ -225,7 +225,7 @@ public void scaleDownscaleUpRectangleInvertible() {
for (int zoom1 : zooms) {
for (int zoom2 : zooms) {
for (int i = 1; i <= 10000; i++) {
Rectangle rect = new Rectangle(0, 0, i, i);
Rectangle rect = new Rectangle.OfFloat(0, 0, i, i);
Rectangle scaleDown = Win32DPIUtils.pixelToPoint(rect, zoom1);
Rectangle scaleUp = Win32DPIUtils.pointToPixel(scaleDown, zoom2);
scaleDown = Win32DPIUtils.pixelToPoint(scaleUp, zoom2);
Expand Down
Loading