Skip to content

StyledText does not handle \u2028 correctly #2202

Open
@tmssngr

Description

@tmssngr

Describe the bug
The unicode character \u2028 is not handled correctly. I may be rendered as line break, but it is not treated as line break.

To Reproduce

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

public class StyledTextBug {

	private static Font font;
	private static int index;

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

		final FontData[] fontData = display.getFontList(null, true);
		System.out.println(fontData.length);

		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

		StyledText styledText = new StyledText(shell, SWT.BORDER);
		styledText.setText("foo: \"bla\u2028bla.\"\nbar");
		styledText.setLineBackground(0, 1, new Color(255, 190, 190));
		styledText.setCaretOffset(styledText.getCharCount());
		styledText.addListener(SWT.KeyDown, new Listener() {
			@Override
			public void handleEvent(Event event) {
				if (event.keyCode == SWT.F1) {
					Font prevFont = font;
					String prevName = prevFont != null ? prevFont.getFontData()[0].getName() : "";
					FontData fontDatum;
					while (true) {
						fontDatum = fontData[index];
						index = (index + 1) % fontData.length;
						String name = fontDatum.getName();
						if (!name.equals(prevName)) {
							System.out.println(name);
							break;
						}
					}
					font = new Font(display, fontDatum);
					styledText.setFont(font);
					if (prevFont != null) {
						prevFont.dispose();
					}
				}
			}
		});

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

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

		if (font != null) {
			font.dispose();
		}

		display.dispose();
	}
}

Run this snippet on MacOS. It will show 3 lines. However, the first two have the line background set to pink and you can't navigate to the end of the file. Though the caret has been set after the last character, it blinks in the second line.

You can use the F1 key to switch the fonts.

Expected behavior
That \u2028 would be handled correctly - either treat it as a line separator or don't let it wrap the line. This causes other bugs, e.g. line height

Screenshots
Image

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS
  1. Additional OS info (e.g. OS version, Linux Desktop, etc)
    Tried on macOS 14, but I reckon this is a general problem.

On Windows I was not able to reproduce the problem. Depending on the font, the unicode character was not rendered at all or as a box with/without containing text.

Image

On Linux I can reproduce it as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions