| |||||||||
Font handling in SWT
Font handling in SWTThe Eclipse SWT framework is a great framework. It is fast, tidy and free. Unfortunately the documentation seems to lack a bit. I spent some time fiddling with getting font loading to work properly for my latest project Data Storm. Here my secrets are unleashed! The code shows two things:
getFontList() for this, and use a specific name or null for a full font list.
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class FontList {
public static void main(String[] args) {
Display display = new Display();
Shell shell = new Shell(display);
displayAllLoadedFonts(shell);
loadFont(shell, "amigaforeverpro2.ttf", 8);
}
private static Font loadFont(Shell shell, String fontFileName, int fontSize) {
boolean isFontLoaded = shell.getDisplay().loadFont(fontFileName);
if( isFontLoaded ) {
displayAllLoadedFonts(shell);
// font name found by inspecting the font list..
return new Font(shell.getDisplay(), "Amiga Forever Pro2", fontSize, SWT.NORMAL);
}
return null;
}
private static void displayAllLoadedFonts(Shell shell) {
// display all scalable fonts in the system
FontData[] fd = shell.getDisplay().getFontList(null, true);
for( int i = 0; i < fd.length; i++ ) {
System.out.println(fd[i].getName());
}
// and the non-scalable ones
fd = shell.getDisplay().getFontList(null, false);
for( int i = 0; i < fd.length; i++ ) {
System.out.println(fd[i].getName());
}
}
}
The code is not particularly complicated, the hard part is just figuring out what to write! There! Served on a silver platter for you :-)
CommentsIf you have any comments to this article, please drop me a mail at firstclassthoughts at gmail dot com please indicate if I can't publish whole or parts of your comment on the site.If you like this site consider Help spread the wordShare this post on your favorite social bookmarking sites:
The most recent contributions 28/07/09 Magic in mathematics II Fun with the number cyclic numbers, and specifically with 142857 as it is the smallest of such numbers. 13/07/09 My top 8 time-saving Firefox shortcuts This article presents my favorite top 8 time-saving shortcuts in Firefox 3.0 and Firefox 3.5. Get to know these and you'll be saving a lot of time. They have been ordered by "the element of most surprise" 20/05/09 Board Game Jungle speed / Arriba Review of the cool game "Jungle Speed" aka. "Arriba". 16/05/09 Danish Twin words "Twin words" are words that not only have multiple meanings, they must be composed next to each other in meaningful sentences. This article explores the concept of twin words. Nothing of interest? Try browsing the entire article archive... | |||||||||