Community
Participate
Working Groups
Many times it is very import to have the length of a displayed text stream in a Label be independent of the actual text. That is what monospace fonts are about. But currently there is no way to set the style to monospace and let the system pick a font. The programmer needs to know the available fonts and their properties. Alternatively, if there was a font name like "monospace" which would be correctly translated to a local font I would be able to use that in Control.setFont().
I suggest adding methods to complement 'systemFont', shadowing the work done by the w3c to solve this problem for XML/CSS with font-families: e.g. methods getMonoFont(), getCursiveFont(), etc. see http://www.w3.org/TR/CSS1#font-family
I have since discovered that awt supports this concept, as 'logical fonts'. These are the meta-names Serif, SansSerif, Monospaced, Dialog, and DialogInput which are mapped to system-dependent values. If anyone knows how to extract a physical font name from a java.awt.Font and build a org.eclipse.swt.graphics.Font from this, I'd be very grateful. A useful intermediate step.
Perhaps the time has come to add Font aliases? At any rate, having a fixed width font that is available on all platforms is a useful concept (ie. Display.getSystemFixedFont() or Display.getSystemFont(SWT.FONT_FIXED))
has there been any activity for this enhancement request
Font aliases are a very interesting feature to have in future releases of SWT. The monospace issue shows why they are important in cross-platform development.
Sorry about that, I hit the wrong key in adding myself as a cc to the bug.
Any update here? A platform-independent "monospace font" would definitely be very useful to the IMP project.
Created attachment 238603 [details] Cross platform, thread-safe, monospaced font utility class. SWTFontUtils.java Utility class (thread safe) that implements 'getMonospacedFont' and 'getMonospacedFont(Display)', to get a monospaced font for the current/given display. Is platform independent. Has various hard coded fonts for operating system and windowing system combinations, as well as a default fallback. Hard coded values based on class 'org.eclipse.jface.resource.FontRegistry' and resources 'org.eclipse.jface.resource/jfacefonts*.properties' from 'org.eclipse.jface' plug-in (version 3.9.1). Some notes: - Caches the fonts, per display, for performance. - Is thread safe. - Hooks into the display dispose callback to dispose the font once the display is disposed, to avoid resource leaks. - Coded for Java 7, but could easily be adapted to work on older versions. - Uses org.apache.commons.lang3 (version 3.1), but it's not really essential. - Tested with org.eclipse.swt.win32.win32.x86_64_3.102.1.v20130827-2048.jar - Currently the hard coded fonts are hard coded in code. It might be better to use resource files as 'org.eclipse.jface' does, to allow users to override the properties, but I didn't need that. - If included in SWT, the functionality may be better added to the Display class instead of a utility class.
Comment on attachment 238603 [details] Cross platform, thread-safe, monospaced font utility class. Setting mime-type to text/plain.
What about adding methods to retrieve default OS-dependent fonts, but for each family? I mean: serif, sans-serif, monospace, cursive and fantasy.
(In reply to Piero Campalani from comment #10) > What about adding methods to retrieve default OS-dependent fonts, but for > each family? > > I mean: serif, sans-serif, monospace, cursive and fantasy. Can we do that? Is that sort of functionality available as a system call? If so, that would be the obvious solution.
I am no longer working on SWT. Moving to INBOX so that the team can see the comments.
Just for information for people who will decide to reuse "SWTFontUtils" snippet (https://bugs.eclipse.org/bugs/attachment.cgi?id=238603) Black Duck now recognize it as part of code was published in CCW project (https://github.com/ccw-ide/ccw/blob/master/ccw.core/src/java/ccw/util/SWTFontUtils.java) under EPL 1.0