Why isn't text anti-aliasing working, while using a SWT GC to draw text?
Asked Answered
M

2

6

Right now my goal is to have anti-aliased text on my Labels. If my research was correct, SWT Labels do not natively support anti-aliasing on text, so my current workaround attempt is to create an image, turn text anti-aliasing on, draw my text to that image, then give that image to the Label.

My current image drawing code is as follows:

Image image = new Image(Display.getDefault(), width, height);
GC gc = new GC(image);

gc.setAntialias(SWT.ON);
gc.setTextAntialias(SWT.ON);

gc.setBackground(background);
gc.fillRectangle(0, 0, width, height);

gc.setFont(font);
gc.setForeground(foreground);

int yPos = offset.y;
for (String rawLine : lines)
{
    String line = rawLine.trim();
    Point lineSize = gc.textExtent(line);
    int xPos = offset.x;
    switch (alignment)
    {
    case SWT.RIGHT:
        xPos += width - lineSize.x;
        break;
    case SWT.CENTER:
        xPos += width / 2 - lineSize.x / 2;
        break;
    case SWT.LEFT:
    default:
        xPos += 0;
    }
    gc.drawText(line, xPos, yPos, true);
    yPos += lineSize.y;
}

gc.dispose();
return image;

I have had inconsistent results on two different computers: At work, the text in the resulting images appears as choppy as ever- as if text anti-aliasing wasn't even on. But at home, connected to my work computer via remote desktop, I saw exactly the results I wanted.

Obviously I'd like things to work correctly on both computers, but I am currently stumped as to why they aren't. Each computer is running Windows 7, Eclipse v3.6.

What could be the problem that is causing this inconsistency? And if my workaround is just absurd and I am completely missing an easier way, what is that way? Thank you for any help!

Merriman answered 13/1, 2011 at 20:4 Comment(4)
Maybe the two machines are attempting to anti-alias two different fonts?Coldshoulder
They should both be using the "Tahoma" font.Merriman
My guess would be that it's because of differences between the graphics cards in the two machines. SWT uses the OS's native facilities, which include any hardware perks used by the OS. So I'd look into swapping the graphics card at work, if possible. Perhaps even look into the patch levels of each machine even though both use Windows 7.Hebner
SWT doesn't antialias? I've been using Eclipse on different platforms an all sane ones use antialiasing by default. Is Eclipse using antialiased text for you?Seaside
S
1

Check if Anti-Aliased text support is configured differently on both machines in the Control Panel. In XP, you'd be looking to make sure ClearType was enabled.

http://www.microsoft.com/typography/cleartype/tuner/step1.aspx

You might also look at this article. It could be playing a role.

http://www.ytechie.com/2008/12/cleartype-in-remote-desktop-with-xp.html

Safko answered 17/5, 2011 at 15:1 Comment(0)
I
0

If you can't get it working in SWT, perhaps you could try rendering to a Swing image and copy the resulting pixels.

Incogitable answered 17/5, 2011 at 8:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.