Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SWT - OS agnostic way to get monospaced font

Tags:

java

fonts

swt

Is there a way in SWT to get a monospaced font simply, that works across various operating systems?

For example. this works on Linux, but not Windows:

  Font mono = new Font(parent.getDisplay(), "Mono", 10, SWT.NONE);  

or do I need to have a method that tries loading varying fonts (Consolas, Terminal, Monaco, Mono) until one isn't null? Alternatively I could specify it in a properties file on startup.

I tried getting the system font from Display, but that wasn't monospaced.

like image 635
JeeBee Avatar asked Oct 21 '08 11:10

JeeBee


People also ask

How do I make my font monospaced?

Basically, for a font to be considered monospace, every glyph has to be the same width, right down to the exact same number of units. This includes even glyphs that should normally be zero width, or a certain width (such as em spaces, em dashes, etc).

What word fonts are monospaced?

Summary. The only monospaced TrueType fonts shipped by Microsoft are Courier New, which shipped with Windows 3.1, and Lucida Sans Typewriter, which was included in the TrueType Font Pack.

What are examples of monospaced fonts?

The system fonts Courier, Menlo, and Consolas are examples of monospaced fonts , so named because every character is the same width. When the characters vary in width, the font is called proportional .


1 Answers

I spent a while bashing my head against this one until I realised that obviously eclipse must have access to a monospace font for use in its text fields, console etc. A little digging turned up:

Font terminalFont = JFaceResources.getFont(JFaceResources.TEXT_FONT); 

Which works if all you're interested in is getting hold of some monospace font.

Edit: Or based on @ctron's comment:

Font font = JFaceResources.getTextFont(); 

Edit: Caveat (based on @Lii's comment): this will get you the configured Text Font, which can be overriden by the user and may not be a monospace font. However, it will be consistent with, e.g., the font used in the editor and console which is probably what you want.

like image 154
Bartleby Avatar answered Sep 23 '22 00:09

Bartleby