Skip to content

Commit 429ba4b

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 bea0991 commit 429ba4b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

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
@@ -116,6 +116,12 @@ public static float pixelToPoint(float size, int zoom) {
116116
return (size / scaleFactor);
117117
}
118118

119+
public static double pixelToPoint(double size, int zoom) {
120+
if (zoom == 100 || size == SWT.DEFAULT) return size;
121+
double scaleFactor = getScalingFactorD (zoom, 100);
122+
return (size / scaleFactor);
123+
}
124+
119125

120126
/**
121127
* Auto-scale image with ImageData
@@ -199,6 +205,13 @@ public static float getScalingFactor(int zoom) {
199205
}
200206

201207

208+
public static double getScalingFactorD(int targetZoom, int currentZoom) {
209+
if (targetZoom <= 0) {
210+
targetZoom = deviceZoom;
211+
}
212+
return targetZoom / (double) currentZoom;
213+
}
214+
202215
public static float getScalingFactor(int targetZoom, int currentZoom) {
203216
if (targetZoom <= 0) {
204217
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)