Skip to content

StyledText does not restore default line height on setText() if increased by previous content #3301

@tmssngr

Description

@tmssngr

Describe the bug
A StyledText increases automatically the line height if the default font can't display some characters and another, larger replacement font is used. That is more or less reasonable. However, when setting a new text, it should reset the line height to the default value.

To Reproduce

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class StyledTextLineHeightTest {

	public static void main(String[] args) {
		final Display display = new Display();

		final Font font = new Font(display, "Ubuntu Mono", 10, 0);

		final Shell shell = new Shell(display);
		shell.setLayout(new GridLayout(2, true));

		final StyledText text = new StyledText(shell, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		text.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
		text.setFont(font);

		createButton("Set US-Ascii Text", shell)
				.addListener(SWT.Selection, e -> {
					text.setText("""
					             line 1
					             line 2
					             line 3
					             line 4
					             """);
				});
		createButton("Set Non-US-ASCII Text", shell)
				.addListener(SWT.Selection, e -> {
					text.setText("""
					             नमस्ते। आप कैसे हैं?
					             こんにちは。お元気ですか?
					             안녕하세요. 잘 지내시나요?
					             Γεια σου. Τι κάνεις;
					             """);
				});

		shell.setSize(400, 300);
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}

		font.dispose();

		display.dispose();
	}

	private static Button createButton(String text, Shell shell) {
		final Button button = new Button(shell, SWT.PUSH);
		button.setText(text);
		button.setLayoutData(new GridData(SWT.FILL, SWT. FILL, false, false));
		return button;
	}
}
  • click the "Set US-Ascii Text" button -> the text is displayed with default line height
  • click the "Set Non-US-ASCII Text" button -> the line height increases (mostly noticeable on Linux)
  • click the "Set US-Ascii Text" button again -> the increased line height remains

Expected behavior
The line height for new texts should be as for a new StyledText control, not influenced by previous text content.

Screenshots
After clicking "Set US-Ascii Text" button:
Image

After clicking "Set Non-US-ASCII Text" button:
Image

After clicking "Set US-Ascii Text" button again:
Image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS

Workaround (or) Additional context
Create a new StyledText control before setting the next text (not always an easy option).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions