Skip to content

[Win32] Monospace font doesn't place nice with Transform #2978

@ptziegler

Description

@ptziegler

Describe the bug

Unlike other fonts like e.g. Arial, the FontMetrics returned for the Monospace font are scaled by the Transform.

To Reproduce

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Test {
	protected static final Font DISPLAY_FONT = new Font(null, "Monospaced", 24, SWT.NORMAL); //$NON-NLS-1$
	public static void main(String[] args) {
		System.setProperty("swt.autoScale.updateOnRuntime", "true");
		
		Shell shell = new Shell();

		shell.addPaintListener(event -> {
			GC gc = event.gc;
			gc.setFont(DISPLAY_FONT);
			
			Transform t = new Transform(gc.getDevice());
			t.scale(3.0f, 3.0f);

			gc.setTransform(t);
			System.out.println(gc.getFontMetrics().getAverageCharacterWidth());
			
			gc.setTransform(null);
			System.out.println(gc.getFontMetrics().getAverageCharacterWidth());

			gc.setFont(DISPLAY_FONT);
			System.out.println(gc.getFontMetrics().getAverageCharacterWidth());

			gc.setTransform(t);
			System.out.println(gc.getFontMetrics().getAverageCharacterWidth());
			
			gc.dispose();
		});

		shell.open();
		
		Display d = shell.getDisplay();
		while(!shell.isDisposed()) {
			while(!d.readAndDispatch()) {
				d.sleep();
			}
		}
	}
}

In GEF, I even had a situation where the correct values are returned after calling these no-op operations. Which only works due to calling getFontMetrics() while gc.getTransform() is null:

gc.setTransform(null);
gc.getFontMetrics();
gc.setTransform(transform);

Output

  • Linux (with Monospaced)
    15, 15, 15, 15

  • Windows (125% zoom, with Monospaced)
    53, 53, 18, 18

  • Windows (125% zoom, with Arial)
    18, 18, 18, 18

Expected behavior

The average char width should behave the same across all operating systems.

Environment:

  1. Select the platform(s) on which the behavior is seen:
    • All OS
    • Windows
    • Linux
    • macOS

Version since

Probably since forever. I can see the same inconsistency with the 2023-12.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions