Skip to content

Commit 785dba0

Browse files
committed
[Win32] Ensure GC.textExtent/stringExtent returns large enough results
The GC.textExtent()/stringExtent() operations return values in logical SWT points. On Windows, this involves a conversion from the actual pixel values according to the current zoom factor. This conversion includes a rounding to integer values which can lead to a value that is slightly smaller than the actual pixel value. The expectation of consumers is, however, that the result effectively represents the size of a bounding box, i.e., that the whole string fully fits into a rectangle of the returned size. To ensure that the results of GC.textExtent()/stringExtent() are always large enough so that the text fits into an accordingly sized rectangle on every zoom, this change adapts the performed rounding operations to always round up the values. This makes the behavior consistent to rounding behavior of the Control.computeSize() methods with a similar semantics. Contributes to eclipse-platform/eclipse.platform#2295
1 parent 3e1cc4f commit 785dba0

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5877,7 +5877,7 @@ void apply() {
58775877
*/
58785878
public Point stringExtent (String string) {
58795879
if (string == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
5880-
return Win32DPIUtils.pixelToPointAsSize(drawable, stringExtentInPixels(string), getZoom());
5880+
return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(drawable, stringExtentInPixels(string), getZoom());
58815881
}
58825882

58835883
Point stringExtentInPixels (String string) {
@@ -5922,7 +5922,7 @@ Point stringExtentInPixels (String string) {
59225922
* </ul>
59235923
*/
59245924
public Point textExtent (String string) {
5925-
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB), getZoom());
5925+
return textExtent(string, SWT.DRAW_DELIMITER | SWT.DRAW_TAB);
59265926
}
59275927

59285928
/**
@@ -5957,7 +5957,7 @@ public Point textExtent (String string) {
59575957
* </ul>
59585958
*/
59595959
public Point textExtent (String string, int flags) {
5960-
return Win32DPIUtils.pixelToPointAsSize(drawable, textExtentInPixels(string, flags), getZoom());
5960+
return Win32DPIUtils.pixelToPointAsSufficientlyLargeSize(textExtentInPixels(string, flags), getZoom());
59615961
}
59625962

59635963
Point textExtentInPixels(String string, int flags) {

bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/internal/Win32DPIUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public static Point pixelToPointAsSize(Drawable drawable, Point point, int zoom)
137137
return pixelToPointAsSize (point, zoom);
138138
}
139139

140+
public static Point pixelToPointAsSufficientlyLargeSize(Drawable drawable, Point point, int zoom) {
141+
if (drawable != null && !drawable.isAutoScalable()) return point;
142+
return pixelToPointAsSufficientlyLargeSize (point, zoom);
143+
}
144+
140145
public static Point pixelToPointAsLocation(Drawable drawable, Point point, int zoom) {
141146
if (drawable != null && !drawable.isAutoScalable()) return point;
142147
return pixelToPointAsLocation (point, zoom);

0 commit comments

Comments
 (0)