Bug 48055 - Request a SWT style for monospace and proportional fonts
Summary: Request a SWT style for monospace and proportional fonts
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.0   Edit
Hardware: All All
: P3 enhancement with 17 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform-SWT-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-12-04 05:43 EST by shmuel siegel CLA
Modified: 2020-11-14 02:53 EST (History)
20 users (show)

See Also:


Attachments
Cross platform, thread-safe, monospaced font utility class. (8.51 KB, text/plain)
2013-12-29 17:22 EST, Dennis Hendriks CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description shmuel siegel CLA 2003-12-04 05:43:27 EST
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().
Comment 1 Jon Dowland CLA 2003-12-20 16:51:28 EST
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
Comment 2 Jon Dowland CLA 2004-02-25 06:57:46 EST
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. 
Comment 3 Steve Northover CLA 2004-03-18 17:21:52 EST
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))
Comment 4 shmuel siegel CLA 2005-02-28 00:34:09 EST
has there been any activity for this enhancement request
Comment 5 Georgios Nikolaidis CLA 2006-07-05 09:46:19 EDT
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.
Comment 6 Francis Upton IV CLA 2008-06-10 00:53:07 EDT
Sorry about that, I hit the wrong key in adding myself as a cc to the bug.
Comment 7 Robert M. Fuhrer CLA 2010-04-02 11:56:03 EDT
Any update here?

A platform-independent "monospace font" would definitely be very useful to the IMP project.
Comment 8 Dennis Hendriks CLA 2013-12-29 17:22:39 EST
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 9 Dennis Hendriks CLA 2013-12-29 17:24:21 EST
Comment on attachment 238603 [details]
Cross platform, thread-safe, monospaced font utility class.

Setting mime-type to text/plain.
Comment 10 Piero Campalani CLA 2015-07-20 03:15:12 EDT
What about adding methods to retrieve default OS-dependent fonts, but for each family? 

I mean: serif, sans-serif, monospace, cursive and fantasy.
Comment 11 Dennis Hendriks CLA 2015-07-20 06:39:43 EDT
(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.
Comment 12 Silenio Quarti CLA 2015-07-20 10:28:53 EDT
I am no longer working on SWT. Moving to INBOX so that the team can see the comments.
Comment 13 Kirill Balod CLA 2019-03-11 09:27:58 EDT
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