Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 329641

Summary: Text drawn to an image GC looks different to text drawn to a screen GC
Product: [Eclipse Project] Platform Reporter: Mark McLaren <mark.k.mclaren>
Component: SWTAssignee: Scott Kovatch <skovatch>
Status: CLOSED DUPLICATE QA Contact: Silenio Quarti <Silenio_Quarti>
Severity: normal    
Priority: P3 CC: eclipse.felipe, skovatch
Version: 4.1   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard:
Attachments:
Description Flags
Screenshot showing different text
none
My screen shot none

Description Mark McLaren CLA 2010-11-07 17:26:18 EST
Build Identifier: 3.7m3

Mac Cocoa 32-bit: When double-buffering, the text appears lighter than when drawing direct to screen.  See example below

Reproducible: Always

Steps to Reproduce:
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class TestCanvas {

	public static void main(String[] args) {
		final Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());

		Canvas canvas = new Canvas(shell, SWT.NONE);

		FontData fd = new FontData("Lucida Grande", 16, SWT.NORMAL);

		final Font font = new Font(display, fd);

		final Image buffImg = new Image(display, 300, 100);

		canvas.addPaintListener(new PaintListener() {

			public void paintControl(PaintEvent e) {
				drawText(e.gc);

				GC buffGC = new GC(buffImg);
				drawText(buffGC);
				buffGC.dispose();
				e.gc.drawImage(buffImg, 0, 50);

			}

			private void drawText(GC gc) {
				gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
				gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
				gc.fillRectangle(0, 0, 300, 100);

				gc.setFont(font);
				gc.drawString("Bennie And The Jets", 20, 20);
			}
		});

		shell.setSize(400, 400);
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		font.dispose();
		display.dispose();
	}
}
Comment 1 Scott Kovatch CLA 2010-11-08 12:27:57 EST
Yes, this is 329569, just fixed late last week.

*** This bug has been marked as a duplicate of bug 329569 ***
Comment 2 Mark McLaren CLA 2010-11-10 12:25:02 EST
Hi Scott - 

Are you sure this is related to bug 329569?   When I run the snippet  from this bug on build N20101109-2100 using OSX 10.6.4 it still shows that the 'image GC' text is much lighter than the 'screen GC'...?

Thanks

Mark
Comment 3 Scott Kovatch CLA 2010-11-10 12:36:20 EST
(In reply to comment #2)
> Hi Scott - 
> 
> Are you sure this is related to bug 329569?   When I run the snippet  from this
> bug on build N20101109-2100 using OSX 10.6.4 it still shows that the 'image GC'
> text is much lighter than the 'screen GC'...?

That's odd -- I'm using 10.6.4 as well. I used xScope (magnifier/color inspector tool) to compare the blues and they're identical now, whereas before the fix the blue pixels had different values.
Comment 4 Mark McLaren CLA 2010-11-11 09:19:34 EST
Sorry for the misunderstanding - that is my fault.  What I meant is that the text font looks different depending on whether it was drawn to the image or screen (the background color is fine).  The text drawn to the image GC is so thin that it looks like a different font.  

This is causing me a problem because I have a custom widget which draws some parts directly to the screen, but double-buffers other parts.  This makes the widget look a little odd.

Thanks
Comment 5 Scott Kovatch CLA 2010-11-11 12:02:44 EST
OK, I understand now. I'll look at it again. The color difference came to light a day or so after your bug came in, which is probably why I assumed it was related.
Comment 6 Scott Kovatch CLA 2010-11-11 12:04:32 EST
Are you using 10.5 or 10.6? Lately that seems to be an important point.
Comment 7 Mark McLaren CLA 2010-11-12 03:29:22 EST
Created attachment 182964 [details]
Screenshot showing different text
Comment 8 Mark McLaren CLA 2010-11-12 03:31:37 EST
I'm running OSX 10.6.4 ('Snow Leopard').  I've attached screenshots showing what the output of the example program looks like to me.  Are you able to reproduce this problem?
Comment 9 Scott Kovatch CLA 2010-11-12 12:47:49 EST
(In reply to comment #8)
> I'm running OSX 10.6.4 ('Snow Leopard').  I've attached screenshots showing
> what the output of the example program looks like to me.  Are you able to
> reproduce this problem?

If I turn off the 'Use LCD font smoothing when available' option in the Appearance preference panel the top line does look thinner for me, but it's nowhere near as dramatic as your screen shot.  It is slightly different, though.
Comment 10 Scott Kovatch CLA 2010-11-12 12:48:15 EST
Created attachment 183018 [details]
My screen shot
Comment 11 Mark McLaren CLA 2010-11-12 18:35:53 EST
Wow - if I *untick* the 'Use LCD font smoothing when available' setting then the snippet works fine and there is no difference between the top and bottom text.

After a bit of digging it seems this issue has something to do with what type of monitor you are using:
http://www.macworld.com/article/145157/2009/12/smoothsnow.html

One insightful comment is on this page:
http://forums.macrumors.com/showthread.php?t=774761
"You're NOT supposed to combine sub-pixel anti-aliasing and regular anti-aliasing. It makes all the text look bold, especially light text on a dark background. That's because it's being anti-aliased twice.
If you want your text to render properly, turn OFF sub-pixel anti-aliasing. Sure, it sounds cool, but it's actually really terrible, and distorts your text."

I guess you can close this as being an OSX bug and not an SWT bug.
Comment 12 Scott Kovatch CLA 2010-11-16 13:06:09 EST
(In reply to comment #11)
> Wow - if I *untick* the 'Use LCD font smoothing when available' setting then
> the snippet works fine and there is no difference between the top and bottom
> text.
> 
> After a bit of digging it seems this issue has something to do with what type
> of monitor you are using:
> http://www.macworld.com/article/145157/2009/12/smoothsnow.html
> 
> I guess you can close this as being an OSX bug and not an SWT bug.

Well, maybe not so fast here... I think this is related to color premultiplication in NSBitmapImageRep. I made a change to how we created the bitmap that backs an Image when you call "new Image(display, height, width)", and that makes the two strings draw identically, with no difference in any of the pixel values. Unfortunately it won't work in 10.5, and also breaks too many other things.

In any event, I'm going to mark this as a duplicate of bug 272592, since that bug is tracking the same problem in a different way.

*** This bug has been marked as a duplicate of bug 272592 ***