eclipse.platform.swt
eclipse.platform.swt copied to clipboard
Label: text aliasing breaks on lines following empty new lines
I was asked to show a bigger message with some paragraphs in a MessageDialog (that uses 'new Label(SWT.WRAP)') and surprisingly, every second line after a paragraph seem to be rendered with different anti-aliasing settings. Turned out, it is a bug in SWT / GTK. If a line is empty, next non-empty line of text will be rendered with anti-aliasing settings broken (switched off?).
package org.eclipse.swt.tests.manual;
/*
* Label example snippet: create a label with paragraphs separated by new lines.
*
* If the lines are empty, next line text is not properly rendered, breaking font aliasing
*/
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class LabelRenderingTextWithEmptyLines {
public static void main(String[] args) {
// Message with new lines containing some spaces
String goodMessage = "Hello\n \nworld of good text aliasing\n\n";
// just trim new lines to be empty
String badMessage = goodMessage.replaceAll(" ", "");
// and slightly change text to indicate where the problem is
badMessage = badMessage.replaceAll("good", "bad");
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setText("Crazy font rendering");
shell.setBackground(new Color(255, 255, 255));
Label badLabel = new Label(shell, SWT.WRAP);
badLabel.setText(badMessage);
Label goodLabel = new Label(shell, SWT.WRAP);
goodLabel.setText(goodMessage);
shell.setSize(300, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
display.dispose();
}
}