Skip to content

Commit 04870b3

Browse files
committed
[Win32] Return unrounded value in FontMetrics.getAverageCharacterWidth
The value for getAverageCharacterWidth was rounded, even though its method signature returns a double. The unrounded value provides a better estimation of the average character width for non-integer zoom factors. Fixes: #2461
1 parent 12597f1 commit 04870b3

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/internal/DPIUtil.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,12 @@ public static float pixelToPoint(float size, int zoom) {
197197
return (size / scaleFactor);
198198
}
199199

200+
public static double pixelToPoint(double size, int zoom) {
201+
if (zoom == 100 || size == SWT.DEFAULT) return size;
202+
double scaleFactor = getScalingFactor (zoom, 100);
203+
return size / scaleFactor;
204+
}
205+
200206

201207
/**
202208
* Auto-scale image with ImageData
@@ -280,6 +286,13 @@ public static float getScalingFactor(int zoom) {
280286
}
281287

282288

289+
public static double getScalingFactorD(int targetZoom, int currentZoom) {
290+
if (targetZoom <= 0) {
291+
targetZoom = deviceZoom;
292+
}
293+
return targetZoom / (double) currentZoom;
294+
}
295+
283296
public static float getScalingFactor(int targetZoom, int currentZoom) {
284297
if (targetZoom <= 0) {
285298
targetZoom = deviceZoom;

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/FontMetrics.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int getAscent() {
108108
* @since 3.107
109109
*/
110110
public double getAverageCharacterWidth() {
111-
return getAverageCharWidth();
111+
return DPIUtil.pixelToPoint((double) handle.tmAveCharWidth, getZoom());
112112
}
113113

114114
/**

0 commit comments

Comments
 (0)