Bug 319125 - GC#textExtent resets gc.FontMetrics
Summary: GC#textExtent resets gc.FontMetrics
Status: REOPENED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 4.0   Edit
Hardware: PC Windows XP
: P3 major with 2 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-07-07 09:50 EDT by Keith Willenson CLA
Modified: 2021-12-01 06:48 EST (History)
5 users (show)

See Also:


Attachments
Small Eclipse plugin / SWT app demonstrating the issue (9.26 KB, application/zip)
2011-06-29 10:02 EDT, Stephan Wahlbrink CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Keith Willenson CLA 2010-07-07 09:50:57 EDT
Build Identifier: Version: 3.6.0 Build id: I20100608-0911

I just upgraded to Eclipse 3.6.0 and my code started behaving strangely.

Diagnostics are show only when data is "12" so diagnostics are not excessive. I do not know how to get this to repeat in a small sample. The code is from SiteElement.drawLineText

 if (data.compareTo("12") == 0)
    {
      System.out.println("SiteElement.drawLineText b pixelHeight " +  gc.getFontMetrics().getHeight()); //$NON-NLS-1$
      System.out.printin("SiteElement.drawLineText b font " +  gc.getFont()); //$NON-NLS-1$
      System.out.println("SiteElement.drawLineText b metrics " +  gc.getFontMetrics()); //$NON-NLS-1$
    }

    org.eclipse.swt.graphics.Point dataSize = gc.textExtent(data);

    if (data.compareTo("12") == 0)
    {
     System.out.println("SiteElement.drawLineText b2 pixelHeight " +  gc.getFontMetrics().getHeight()); //$NON-NLS-1$
     System.out.println("SiteElement.drawLineText b2 font " +  gc.getFont()); //$NON-NLS-1$
      System.out.println("SiteElement.drawLineText b2 metrics " +  gc.getFontMetrics()); //$NON-NLS-1$
    }



First drawing pass (to screen), everything is fine

SiteElement.drawLabel gc GC {-100595411}
SiteElement.drawLabel font.fontData 1|Times New Roman|26.25|1|WINDOWS|1|-35|0|0|0|700|0|0|0|1|0|0|0|0|Times New Roman
SiteElement.drawLabel pixelHeight 40
SiteElement.drawText pixelHeight 40
SiteElement.drawLineText top pixelHeight 40
SiteElement.drawLineText b pixelHeight 40
SiteElement.drawLineText b font Font {1544167548}
SiteElement.drawLineText b metrics org.eclipse.swt.graphics.FontMetrics@2fa
SiteElement.drawLineText b2 pixelHeight 40
SiteElement.drawLineText b2 font Font {1544167548}
SiteElement.drawLineText b2 metrics SiteElement.drawLineText b2 metrics org.eclipse.swt.graphics.FontMetrics@2fa
SiteElement.drawLineText c pixelHeight 40
SiteElement.drawLineText gc GC {-100595411}



Second drawing pass (to image buffer), immediately after the first pass,

SiteElement.drawLabel gc GC {-1174337614}
SiteElement.drawLabel font.fontData 1|Times New Roman|26.25|1|WINDOWS|1|-35|0|0|0|700|0|0|0|1|0|0|0|0|Times New Roman
SiteElement.drawLabel pixelHeight 40
SiteElement.drawText pixelHeight 40
SiteElement.drawLineText top pixelHeight 40
SiteElement.drawLineText b pixelHeight 40
SiteElement.drawLineText b font Font {-536206755}
SiteElement.drawLineText b metrics org.eclipse.swt.graphics.FontMetrics@2fa
SiteElement.drawLineText b2 pixelHeight 16
SiteElement.drawLineText b2 font Font {-536206755}
SiteElement.drawLineText b2 metrics org.eclipse.swt.graphics.FontMetrics@289
SiteElement.drawLineText c pixelHeight 16
SiteElement.drawLineText gc GC {-1174337614}



You can see that in the second pass, after gc.getTextExtent(), gc does not change, gc.getFont() does not change, but gc.getFontMetrics () does change.

Reproducible: Always
Comment 1 Felipe Heidrich CLA 2010-08-09 16:10:46 EDT
Please, provide a small snippet that we can run here to reproduce the problem.
Thank you
Comment 2 Stephan Wahlbrink CLA 2011-06-29 10:02:14 EDT
Created attachment 198832 [details]
Small Eclipse plugin / SWT app demonstrating the issue

Sample output (Eclipse 3.7 win64):

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GC FONT Font {-1408622859} Arial 60
metrics: 35 41
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GC FONT Font {-1408622859} Arial 60
metrics: 7 8
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Comment 3 Felipe Heidrich CLA 2011-06-29 15:52:33 EDT
I reduced your testcase to this:
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.Display;

public class PR319125 {
	public static void main(String[] args) {
		Display display = new Display();
		Image image = new Image(display, 100, 100);
		GC gc = new GC(image);
		gc.setAdvanced(true);
		gc.fillRectangle(0, 0, 100, 100);
		Font fFont1 = new Font(display, new FontData("Arial", 60, SWT.NORMAL));
		gc.setFont(fFont1);
		
		FontMetrics fontMetrics = gc.getFontMetrics();
		System.out.println("metrics: " + fontMetrics.getAverageCharWidth() + " " + gc.getAdvanceWidth('x'));
		
		gc.stringExtent("m");
		fontMetrics = gc.getFontMetrics();
		System.out.println("metrics: " + fontMetrics.getAverageCharWidth() + " " + gc.getAdvanceWidth('x'));
		
		gc.dispose();
		image.dispose();
		display.dispose();
	}
}
Comment 4 Felipe Heidrich CLA 2011-06-29 15:59:45 EDT
The problem is that the HDC returned by Graphics_GetHDC() does not have the font set in it.
But when we switch the font in the HDC is also switches the font in the GC.handle.

The code in drawText() has some code that goes like this:

int /*long*/ hdc = Gdip.Graphics_GetHDC(gdipGraphics);
int /*long*/ hFont = data.hGDIFont;//this is NULL
if (hFont == 0 && data.font != null) hFont = data.font.handle;//Arial 60
int /*long*/ oldFont = 0;
if (hFont != 0) oldFont = OS.SelectObject(hdc, hFont);//oldFont=System 9
//do some work

if (hFont != 0) OS.SelectObject(hdc, oldFont);//sets System 9 in hdc and handle
Gdip.Graphics_ReleaseHDC(gdipGraphics, hdc);



Silenio, do you remember what is the relation between handle and hdc (returned by Graphics_GetHDC()) ? Apparently switching the font in handle does not affect hdc, but switching the font in hdc does affect handle.
Comment 5 Eclipse Genie CLA 2020-07-16 15:43:21 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 6 Stephan Wahlbrink CLA 2021-12-01 06:48:51 EST
The problem still occurs in SWT 3.118.0.