From 8296f2639967e097690487b746e0b8d4c5a7c2ea Mon Sep 17 00:00:00 2001 From: Andreas Koch Date: Mon, 5 Aug 2024 11:35:16 +0200 Subject: [PATCH] [win32] Fix TextLayout::getBounds vertical indent This commit fixes too high lines when vertical indent was set for one TextLayout. There was a usage of vertical indent in pixels where vertical indent in points should have been used. Contributes to #62 and #127 --- .../swt/graphics/TextLayoutWin32Tests.java | 24 +++++++++++++++++-- .../swt/internal/Win32AutoscaleTestBase.java | 1 + .../org/eclipse/swt/graphics/TextLayout.java | 2 +- 3 files changed, 24 insertions(+), 3 deletions(-) diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java index 16d31a1d38c..4baa8920881 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/graphics/TextLayoutWin32Tests.java @@ -13,7 +13,8 @@ *******************************************************************************/ package org.eclipse.swt.graphics; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.eclipse.swt.internal.*; import org.junit.jupiter.api.*; @@ -38,7 +39,26 @@ public void testGetBoundPublicAPIshouldReturnTheSameValueRegardlessOfZoomLevel() layout.draw(scaledGc, 10, 10); Rectangle scaledBounds = layout.getBounds(); - assertEquals("The public API for getBounds should give the same result for any zoom level", scaledBounds, unscaledBounds); + assertEquals(unscaledBounds, scaledBounds, "The public API for getBounds should give the same result for any zoom level"); + } + + @Test + public void testCalculateGetBoundsWithVerticalIndent() { + TextLayout layout = new TextLayout(display); + layout.setVerticalIndent(16); + layout.setText(text); + Rectangle unscaledBounds = layout.getBounds(); + + int scalingFactor = 2; + int newZoom = DPIUtil.getNativeDeviceZoom() * scalingFactor; + changeDPIZoom(newZoom); + TextLayout scaledLayout = new TextLayout(display); + scaledLayout.setVerticalIndent(16); + scaledLayout.setText(text); + Rectangle scaledBounds = scaledLayout.getBounds(); + + assertNotEquals(layout.nativeZoom, scaledLayout.nativeZoom, "The native zoom for the TextLayouts must differ"); + assertEquals(unscaledBounds.height, scaledBounds.height, 1, "The public API for getBounds with vertical indent > 0 should give a similar result for any zoom level"); } } diff --git a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java index 88166702a60..2ce05ff4f62 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java +++ b/bundles/org.eclipse.swt/Eclipse SWT Tests/win32/org/eclipse/swt/internal/Win32AutoscaleTestBase.java @@ -41,6 +41,7 @@ public void tearDownTest() { } protected void changeDPIZoom (int nativeZoom) { + DPIUtil.setDeviceZoom(nativeZoom); float scalingFactor = 1f * DPIUtil.getZoomForAutoscaleProperty(nativeZoom) / DPIUtil.getZoomForAutoscaleProperty(shell.nativeZoom); DPIZoomChangeRegistry.applyChange(shell, nativeZoom, scalingFactor); } diff --git a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java index a5a0bc73614..e09684600af 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/graphics/TextLayout.java @@ -1792,7 +1792,7 @@ public Rectangle getBounds () { width = Math.max(width, DPIUtil.scaleDown(lineWidthInPixels[line], getZoom()) + getLineIndent(line)); } } - return new Rectangle (0, 0, width, lineY[lineY.length - 1] + getScaledVerticalIndent()); + return new Rectangle (0, 0, width, lineY[lineY.length - 1] + getVerticalIndent()); } /**