Skip to content

Commit

Permalink
Multiple zoom level support to path for win 32
Browse files Browse the repository at this point in the history
This commit contributes to the obtaining the path as per the zoom level
of the monitor. A method win32_getPAth is used in place wherever
path.handle was used earlier by the clients. The preferred zoom level
has to be passed by the clients as a parameter to the mthod in order to
get the handle for the scaled path.

contributes to #62 and #127
  • Loading branch information
amartya4256 committed Jun 18, 2024
1 parent 7ac43c0 commit d0092c0
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*******************************************************************************
* Copyright (c) 2024 Yatta Solutions
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Yatta Solutions - initial API and implementation
*******************************************************************************/
package org.eclipse.swt.graphics;

import static org.junit.Assert.assertTrue;

import java.util.*;

import org.eclipse.swt.internal.*;
import org.junit.*;

public class PathWin32Tests extends Win32AutoscaleTestBase {

@Test
public void pathMustBeScaledOnZoomLevelChange() {
int zoom = DPIUtil.getDeviceZoom();
Path path = new Path(display);
path.addArc(0, 0, 10, 10, 0, 90);
long scaledHandle = path.getHandle(zoom * 2);
PathData pathData = path.getPathData();
path.handle = scaledHandle;
PathData scaledPathData = path.getPathData();
assertTrue("PathData types don't change on zoom level change", Arrays.equals(pathData.types, scaledPathData.types));
for (int i = 0; i < pathData.points.length; i++) {
pathData.points[i] = pathData.points[i] * 2;
}
assertTrue("PathData types don't change on zoom level change", Arrays.equals(pathData.points, scaledPathData.points));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1746,12 +1746,13 @@ void drawOvalInPixels (int x, int y, int width, int height) {
public void drawPath (Path path) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (path.handle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
long pathHandle = path.getHandle(getZoom());
if (pathHandle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
initGdip();
checkGC(DRAW);
long gdipGraphics = data.gdipGraphics;
Gdip.Graphics_TranslateTransform(gdipGraphics, data.gdipXOffset, data.gdipYOffset, Gdip.MatrixOrderPrepend);
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, path.handle);
Gdip.Graphics_DrawPath(gdipGraphics, data.gdipPen, pathHandle);
Gdip.Graphics_TranslateTransform(gdipGraphics, -data.gdipXOffset, -data.gdipYOffset, Gdip.MatrixOrderPrepend);
}

Expand Down Expand Up @@ -2930,12 +2931,13 @@ void fillOvalInPixels (int x, int y, int width, int height) {
public void fillPath (Path path) {
if (handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (path == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
if (path.handle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
final long pathHandle = path.getHandle(getZoom());
if (pathHandle == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
initGdip();
checkGC(FILL);
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
Gdip.GraphicsPath_SetFillMode(path.handle, mode);
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, path.handle);
Gdip.GraphicsPath_SetFillMode(pathHandle, mode);
Gdip.Graphics_FillPath(data.gdipGraphics, data.gdipBrush, pathHandle);
}

/**
Expand Down Expand Up @@ -4323,8 +4325,9 @@ public void setClipping (Path path) {
if (path != null) {
initGdip();
int mode = OS.GetPolyFillMode(handle) == OS.WINDING ? Gdip.FillModeWinding : Gdip.FillModeAlternate;
Gdip.GraphicsPath_SetFillMode(path.handle, mode);
Gdip.Graphics_SetClipPath(data.gdipGraphics, path.handle);
final long pathHandle = path.getHandle(getZoom());
Gdip.GraphicsPath_SetFillMode(pathHandle, mode);
Gdip.Graphics_SetClipPath(data.gdipGraphics, pathHandle);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*******************************************************************************/
package org.eclipse.swt.graphics;

import java.util.*;

import org.eclipse.swt.*;
import org.eclipse.swt.internal.*;
import org.eclipse.swt.internal.gdip.*;
Expand Down Expand Up @@ -55,6 +57,10 @@ public class Path extends Resource {
*/
public long handle;

private int initialZoom;

private HashMap<Integer, Long> handleMap = new HashMap<>();

PointF currentPoint = new PointF(), startPoint = new PointF();

/**
Expand Down Expand Up @@ -85,8 +91,10 @@ public class Path extends Resource {
public Path (Device device) {
super(device);
this.device.checkGDIP();
initialZoom = DPIUtil.getDeviceZoom();
handle = Gdip.GraphicsPath_new(Gdip.FillModeAlternate);
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
handleMap.put(initialZoom, handle);
init();
}

Expand Down Expand Up @@ -132,6 +140,8 @@ public Path (Device device, Path path, float flatness) {
handle = Gdip.GraphicsPath_Clone(path.handle);
if (flatness != 0) Gdip.GraphicsPath_Flatten(handle, 0, flatness);
if (handle == 0) SWT.error(SWT.ERROR_NO_HANDLES);
initialZoom = path.initialZoom;
handleMap.put(initialZoom, handle);
init();
}

Expand Down Expand Up @@ -201,10 +211,10 @@ public Path (Device device, PathData data) {
public void addArc (float x, float y, float width, float height, float startAngle, float arcAngle) {
if (width == 0 || height == 0 || arcAngle == 0) return;
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
width = DPIUtil.autoScaleUp(drawable, width);
height = DPIUtil.autoScaleUp(drawable, height);
x = DPIUtil.autoScaleUp(drawable, x, initialZoom);
y = DPIUtil.autoScaleUp(drawable, y, initialZoom);
width = DPIUtil.autoScaleUp(drawable, width, initialZoom);
height = DPIUtil.autoScaleUp(drawable, height, initialZoom);
addArcInPixels(x, y, width, height, startAngle, arcAngle);
}

Expand Down Expand Up @@ -270,11 +280,6 @@ public void addPath(Path path) {
* </ul>
*/
public void addRectangle (float x, float y, float width, float height) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
width = DPIUtil.autoScaleUp(drawable, width);
height = DPIUtil.autoScaleUp(drawable, height);
addRectangleInPixels(x, y, width, height);
}

Expand Down Expand Up @@ -309,11 +314,10 @@ void addRectangleInPixels(float x, float y, float width, float height) {
*/
public void addString (String string, float x, float y, Font font) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
x = DPIUtil.autoScaleUp(drawable, x, initialZoom);
y = DPIUtil.autoScaleUp(drawable, y, initialZoom);
addStringInPixels(string, x, y, font);
}

void addStringInPixels(String string, float x, float y, Font font) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (font == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
Expand Down Expand Up @@ -382,11 +386,10 @@ public void close() {
*/
public boolean contains (float x, float y, GC gc, boolean outline) {
Drawable drawable = getDevice();
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
x = DPIUtil.autoScaleUp(drawable, x, initialZoom);
y = DPIUtil.autoScaleUp(drawable, y, initialZoom);
return containsInPixels(x, y, gc, outline);
}

boolean containsInPixels(float x, float y, GC gc, boolean outline) {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
if (gc == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
Expand Down Expand Up @@ -419,12 +422,12 @@ boolean containsInPixels(float x, float y, GC gc, boolean outline) {
*/
public void cubicTo (float cx1, float cy1, float cx2, float cy2, float x, float y) {
Drawable drawable = getDevice();
cx1 = DPIUtil.autoScaleUp(drawable, cx1);
cy1 = DPIUtil.autoScaleUp(drawable, cy1);
cx2 = DPIUtil.autoScaleUp(drawable, cx2);
cy2 = DPIUtil.autoScaleUp(drawable, cy2);
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
cx1 = DPIUtil.autoScaleUp(drawable, cx1, initialZoom);
cy1 = DPIUtil.autoScaleUp(drawable, cy1, initialZoom);
cx2 = DPIUtil.autoScaleUp(drawable, cx2, initialZoom);
cy2 = DPIUtil.autoScaleUp(drawable, cy2, initialZoom);
x = DPIUtil.autoScaleUp(drawable, x, initialZoom);
y = DPIUtil.autoScaleUp(drawable, y, initialZoom);
cubicToInPixels(cx1, cy1, cx2, cy2, x, y);
}

Expand All @@ -436,7 +439,7 @@ void cubicToInPixels(float cx1, float cy1, float cx2, float cy2, float x, float

@Override
void destroy() {
Gdip.GraphicsPath_delete(handle);
handleMap.values().forEach(handle -> Gdip.GraphicsPath_delete(handle));
handle = 0;
}

Expand All @@ -458,7 +461,7 @@ void destroy() {
public void getBounds (float[] bounds) {
if (bounds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
getBoundsInPixels(bounds);
float[] scaledbounds= DPIUtil.autoScaleDown(getDevice(), bounds);
float[] scaledbounds= DPIUtil.scaleDown(getDevice(), bounds, initialZoom);
System.arraycopy(scaledbounds, 0, bounds, 0, 4);
}

Expand Down Expand Up @@ -490,7 +493,7 @@ void getBoundsInPixels(float[] bounds) {
public void getCurrentPoint (float[] point) {
if (point == null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
getCurrentPointInPixels(point);
float[] scaledpoint= DPIUtil.autoScaleDown(getDevice(), point);
float[] scaledpoint= DPIUtil.scaleDown(getDevice(), point, initialZoom);
System.arraycopy(scaledpoint, 0, point, 0, 2);
}

Expand All @@ -515,7 +518,7 @@ void getCurrentPointInPixels(float[] point) {
public PathData getPathData() {
if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
PathData result = getPathDataInPixels();
result.points = DPIUtil.autoScaleDown(getDevice(), result.points);
result.points = DPIUtil.scaleDown(getDevice(), result.points, initialZoom);
return result;
}

Expand Down Expand Up @@ -577,7 +580,7 @@ PathData getPathDataInPixels() {
*/
public void lineTo (float x, float y) {
Drawable drawable = getDevice();
lineToInPixels(DPIUtil.autoScaleUp(drawable, x), DPIUtil.autoScaleUp(drawable, y));
lineToInPixels(DPIUtil.autoScaleUp(drawable, x, initialZoom), DPIUtil.autoScaleUp(drawable, y, initialZoom));
}

void lineToInPixels(float x, float y) {
Expand Down Expand Up @@ -642,7 +645,7 @@ public boolean isDisposed() {
*/
public void moveTo (float x, float y) {
Drawable drawable = getDevice();
moveToInPixels(DPIUtil.autoScaleUp(drawable, x), DPIUtil.autoScaleUp(drawable, y));
moveToInPixels(DPIUtil.autoScaleUp(drawable, x, initialZoom), DPIUtil.autoScaleUp(drawable, y, initialZoom));
}

void moveToInPixels(float x, float y) {
Expand All @@ -666,10 +669,10 @@ void moveToInPixels(float x, float y) {
*/
public void quadTo (float cx, float cy, float x, float y) {
Drawable drawable = getDevice();
cx = DPIUtil.autoScaleUp(drawable, cx);
cy = DPIUtil.autoScaleUp(drawable, cy);
x = DPIUtil.autoScaleUp(drawable, x);
y = DPIUtil.autoScaleUp(drawable, y);
cx = DPIUtil.autoScaleUp(drawable, cx, initialZoom);
cy = DPIUtil.autoScaleUp(drawable, cy, initialZoom);
x = DPIUtil.autoScaleUp(drawable, x, initialZoom);
y = DPIUtil.autoScaleUp(drawable, y, initialZoom);
quadToInPixels(cx, cy, x, y);
}

Expand All @@ -695,4 +698,15 @@ public String toString() {
return "Path {" + handle + "}";
}

long getHandle(int zoom) {
if (handleMap.get(zoom) == null) {
PathData pathData = getPathData();
for (int index = 0; index < pathData.points.length; index++) {
pathData.points[index] = DPIUtil.autoScaleUp(getDevice(), pathData.points[index], zoom);
}
handleMap.put(zoom, new Path(getDevice(), pathData).handle);
}
return handleMap.get(zoom);
}

}

0 comments on commit d0092c0

Please sign in to comment.