Skip to content

[Analysis] Evaluate inconsistencies when Device of a Font and its GC differs #350

@akoch-yatta

Description

@akoch-yatta

Based on #349 there is multiple questions to be evaluated and answered:

  • If the passed font belongs to a different device than the device of the GC, can this lead to unexpected issues? If so, how to unify?
  • System fonts are always fetched via the device of the GC -> isn't that inconsistent? You can create a custom font with the same details as the system font has and compare behavior.

If these questions must be solved with code changes, I would create new issues for that.

This Snippet can be extended to analyse the behavior. Note that a new font is fetched internally in GC if the gc has a different zoom than the font.

import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.widgets.Display;

class PrintTest {

	public static void main(String[] args) {
		var printer = new Printer();
		var image = new Image(printer, 20, 20);

		FontData systemFontData = Display.getDefault().getSystemFont().getFontData()[0];
		var font = new Font(Display.getDefault(), systemFontData.getName(), systemFontData.getHeight(), 0);
		var fontPrinter = new Font(printer, systemFontData.getName(), systemFontData.getHeight(), 0);

		printMetric(systemFontData, "System Font");
		printMetric(font.getFontData()[0], "GC Font");
		printMetric(fontPrinter.getFontData()[0], "Printer Font");

		var gc = new GC(image);
		printMetric(gc.getFontMetrics(), "GC");

		gc.setFont(font);
		printMetric(gc.getFontMetrics(), "GC with Font");

		gc.setFont(fontPrinter);
		printMetric(gc.getFontMetrics(), "GC with Printer Font");
	}

	private static void printMetric(FontData fontData, String context) {
		System.out.println("### " + context);
		System.out.println("Name: " + fontData.getName());
		System.out.println("Height: " + fontData.height);
		System.out.println("Height in px: " + (-fontData.data.lfHeight));
	}

	private static void printMetric(FontMetrics textMetric, String context) {
		System.out.println("### " + context);
		System.out.println("Ascent: " + textMetric.getAscent());
		System.out.println("Descent: " + textMetric.getDescent());
		System.out.println("Height: " + textMetric.getHeight());
		System.out.println("Leading: " + textMetric.getLeading());
		System.out.println("Average Char Width: " + textMetric.getAverageCharWidth());
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    HiDPIA HiDPI-Related Issue or FeatureSWTIssue for SWT

    Type

    No type

    Projects

    Status

    🔖 Ready: Atomic

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions