Skip to content

Conversation

@ShahzaibIbrahim
Copy link
Contributor

@ShahzaibIbrahim ShahzaibIbrahim commented Jul 10, 2025

When a font is initialized with a Printer as its device, it should only be used with a GC that was also created from that same Printer. Using such a font with a GC tied to a different device, like a Display, leads to a device mismatch. Since the printer may have a much higher DPI (e.g., 600 DPI) compared to the display (typically 96 DPI), this mismatch causes the text to render extremely large.

To explain I am adding a snippet here:

public class FontDeviceMismatchExample {
	public static void main(String[] args) {
		Display display = new Display();
		// Create the shell
		Shell shell = new Shell(display);
		shell.setText("Font Device Mismatch Demo");
		shell.setSize(400, 200);
		shell.open();

		// Creating font using printer DPI = ~600
		Printer printer = new Printer();
		Font printerFont = new Font(printer, "Arial", 12, SWT.NORMAL);
               
		GC gc = new GC(shell); // creating gc with shell that uses display DPI = ~96

		gc.setFont(printerFont); // here device mismatch
		gc.drawText("This text may appear huge due to DPI mismatch", 20, 50);

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

		// Cleanup
		gc.dispose();
		printerFont.dispose();
		printer.dispose();
		display.dispose();
	}
}

Expected behavior

The correct behavior of this snippet should be Oversized text in a shell due to device mismatch. But that doesn't happen without proposed change. WHY? because while setting the font we used the display of the GC instead of Font which is incorrect.

@github-actions
Copy link
Contributor

github-actions bot commented Jul 10, 2025

Test Results

   545 files  ±0     545 suites  ±0   26m 59s ⏱️ -11s
 4 406 tests ±0   4 389 ✅ ±0   17 💤 ±0  0 ❌ ±0 
16 745 runs  ±0  16 607 ✅ ±0  138 💤 ±0  0 ❌ ±0 

Results for commit b8a2b4c. ± Comparison against base commit 28697b0.

♻️ This comment has been updated with latest results.

@arunjose696
Copy link
Contributor

I am slightly confused why we use data.nativeZoom in setFont() instead of GC.getZoom()

@ShahzaibIbrahim ShahzaibIbrahim force-pushed the master-207 branch 2 times, most recently from 7a145db to ebce070 Compare July 11, 2025 11:46
When a font is initialized with a Printer as its device, it should only
be used with a GC that was also created from that same Printer. Using
such a font with a GC tied to a different device, like a Display, leads
to a device mismatch. Since the printer may have a much higher DPI
(e.g., 600 DPI) compared to the display (typically 96 DPI), this
mismatch causes the text to render extremely large.
@fedejeanne fedejeanne merged commit cb16f62 into eclipse-platform:master Jul 14, 2025
17 checks passed
@fedejeanne fedejeanne deleted the master-207 branch July 14, 2025 12:05
1D17B65E referenced this pull request Jul 28, 2025
This commit adapts how to fetch fonts in GC in the win32 implementation. Now it always uses the internal SWTFontProvider to fetch system and non-system fonts in the correct scaling.

Contributes to #62 and #131
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revert changed behavior regarding Font/Device connections in GC

3 participants