Skip to content

Commit cfb9510

Browse files
committed
[win32] fix endless loop with fixed text metrics
This commit fixes a regression that ignored bigger fixed font metrics in TextLayout in one scenario that led to an endloop recalculation loop. Fixes #1610
1 parent f8cbe80 commit cfb9510

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2136,10 +2136,10 @@ public FontMetrics getLineMetrics (int lineIndex) {
21362136
metricsAdapter.GetTextMetrics(srcHdc, lptm);
21372137
OS.DeleteDC(srcHdc);
21382138
device.internal_dispose_GC(hDC, null);
2139-
2140-
int ascentInPoints = this.ascent;
2141-
int descentInPoints = this.descent;
2142-
int leadingInPoints = DPIUtil.scaleDown(getDevice(), lptm.tmInternalLeading, availableFont.zoom);
2139+
final int zoom = getZoom();
2140+
int ascentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmAscent, zoom), this.ascent);
2141+
int descentInPoints = Math.max(DPIUtil.scaleDown(this.device, lptm.tmDescent, zoom), this.descent);
2142+
int leadingInPoints = DPIUtil.scaleDown(this.device, lptm.tmInternalLeading, availableFont.zoom);
21432143
if (text.length() != 0) {
21442144
for (StyleItem run : runs[lineIndex]) {
21452145
if (run.ascentInPoints > ascentInPoints) {
@@ -2149,10 +2149,10 @@ public FontMetrics getLineMetrics (int lineIndex) {
21492149
descentInPoints = Math.max(descentInPoints, run.descentInPoints);
21502150
}
21512151
}
2152-
lptm.tmAscent = DPIUtil.scaleUp(getDevice(), ascentInPoints, getZoom());
2153-
lptm.tmDescent = DPIUtil.scaleUp(getDevice(), descentInPoints, getZoom());
2154-
lptm.tmHeight = DPIUtil.scaleUp(getDevice(), ascentInPoints + descentInPoints, getZoom());
2155-
lptm.tmInternalLeading = DPIUtil.scaleUp(getDevice(), leadingInPoints, getZoom());
2152+
lptm.tmAscent = DPIUtil.scaleUp(this.device, ascentInPoints, zoom);
2153+
lptm.tmDescent = DPIUtil.scaleUp(this.device, descentInPoints, zoom);
2154+
lptm.tmHeight = DPIUtil.scaleUp(this.device, ascentInPoints + descentInPoints, zoom);
2155+
lptm.tmInternalLeading = DPIUtil.scaleUp(this.device, leadingInPoints, zoom);
21562156
lptm.tmAveCharWidth = 0;
21572157
return FontMetrics.win32_new(lptm, nativeZoom);
21582158
}

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import org.eclipse.swt.graphics.Color;
6464
import org.eclipse.swt.graphics.Font;
6565
import org.eclipse.swt.graphics.FontData;
66+
import org.eclipse.swt.graphics.FontMetrics;
67+
import org.eclipse.swt.graphics.GC;
6668
import org.eclipse.swt.graphics.GlyphMetrics;
6769
import org.eclipse.swt.graphics.Point;
6870
import org.eclipse.swt.graphics.RGB;
@@ -5934,4 +5936,20 @@ public void test_rangeSelectionKeepsCaret() {
59345936
assertEquals("Selection does not start from caret", initialOffset, text.getSelection().x);
59355937
assertNotEquals("Selection is not left-to-right", text.getSelection().x, text.getCaretOffset());
59365938
}
5939+
5940+
@Test
5941+
public void test_fixedLineHeightWithChangingToSmallerFont() {
5942+
shell.setVisible(true);
5943+
shell.setLayout(new GridLayout(1, false));
5944+
5945+
GC gc = new GC(shell.getDisplay());
5946+
FontMetrics metrics = gc.getFontMetrics();
5947+
text.setFixedLineMetrics(metrics);
5948+
5949+
FontData fontData = text.getFont().getFontData()[0];
5950+
text.setText("");
5951+
int smallFontHeight = metrics.getAscent() + metrics.getDescent() - 4;
5952+
Font font = new Font(text.getDisplay(), fontData.getName(), smallFontHeight, fontData.getStyle());
5953+
text.setFont(font);
5954+
}
59375955
}

0 commit comments

Comments
 (0)