Uninstall fonts installed using SWT's Display.loadFont API - java

I am working under eclipse plugin and trying to load fonts into the application as resources, so they will appear in project explorer tree.
In SWT we have method loadFont of Display class which loads font from specified file, so it will appear in list of available fonts of application. I am using this method.
But when I am trying to delete the font from project explorer I am getting following exception:
org.eclipse.core.internal.resources.ResourceException:
Problems encountered while deleting resources
I have following questions:
Is it possible to unload font from application in SWT ?
Is there any way to deal with it?
Is there another scenario to deal with custom fonts in eclipse?

Classes of the type FontData don't need to be disposed of, as they simply contain a small amount of information about the font. When you create an actual Font object then yes, it is your responsibility to call .dispose() and free up those resources.
If you are using JFace as well as SWT, then you may want to look at FontRegistry, which assists in keeping track of font resources.
Apologies for not linking any javadocs, the SWT docs aren't loading for me right now. I did find a small example of FontRegistry usage which explains the difference between FontData and Font.

Related

How to add Fonts to Azure Web Application Java JVM for server side use

How do I make my own custom fonts available to the JVM when deployed in Azure as a Web Application? (note - this is different than serving them back in an html page and configuring the proper MIME types. I'm applying fonts to an image generated using lucee and coldfusion.)
In the old world when we used a virtual machine, we would simply add the fonts to the system font folder and the JVM would have them available at run time. With Azure Web Applications, this folder is not accessible to me. We are running Java 8.
Things I thought should work, but don't seem to:
using the JAVA_FONTS environment variable to specify a path to my custom fonts folder (with ttf files in it)
Making Java calls to create and register the font within my web application. getLocalGraphicsEnvironment / CreateFont / RegisterFont (this registers the fonts, but the Garbage Collector thread chokes and crashes on the Disposer - crashing my JVM every few hours. Take the font loading out, and it runs smoooooth)
Now the fact they didn't work for me might be as simple as misunderstanding the exact syntax of the process.
How can I load my fonts in?
Base on what I found in this document.
I think the main reason for your Garbage Collector thread crash is that the CreateFont method creates a large amount of temporary files.
Solution:
You could refer to the snippet of the code below replace the inputStream method.
String pathString = FontLoader.class.getResource("/font/xxx.ttf").getFile();
Font dynamicFont = Font.createFont(Font.TRUETYPE_FONT, new File(pathString));
You could also refer to the SO thread:Font.createFont leaves files in temp directory.
Hope it helps you.
The solution is to add a key-value pair on the Application Settings blade that looked like this:
JAVA_OPTS
-Dsun.java2d.fontpath=D:\home\site\wwwroot\webapps\ROOT\App\include\fonts
That loaded the fonts properly. (My bug on Garbage Collection is still there, but the question of loading fonts has been solved)

How to open all files extensions in java? [duplicate]

I am displaying a list of files; i.e. xls, doc, pdf, odt etc., in my Java application (Eclipse RCP). When the user clicks on the file, I want to launch the appropriate (according to what the OS thinks) native application, just like it happens in Windows Explorer or the Finder.
And while I am here: It would be nice to also display the same icons that Finder or Explorer use for the different file types.
Is there a library or Eclipse plugin for this?
What you want is java.awt.Desktop:
Desktop.getDesktop().open( file );
I have found an API in Eclipse's SWT now that seems to do the trick:
org.eclipse.swt.program.Program "provides access to facilities for discovering operating system specific aspects of external program launching."
It has methods to find the program for a given file extension, get the program's icon, and even launch the program.
Sounds like you're after the Java Activation Framework ("JAF"). This API lets you determine what files are and what actions you can do on them. Or alternatively the Java Desktop Integration Component ("JDIC"). JDIC allows you to create and no doubt query file associations.
Both projects seem to be in a semi-abandoned state howeer (sigh). But that's par for the course for Sun these days. Only other thing I know of is some Windows specific third party library that's based on JNI called Winpack. It does a bunch of other things too.
You can get the associated icon using the FileSystemView class (Java 1.4+).

Common files between multiple GWT pages

I want to design multiple pages using google web toolkit, reading one of the previous answers GWT multi modules for separate HTML pages? suggests that they way to do this is to have multiple modules. Hence I made 2 modules in my GWT application. They compile and run fine but however I find that each has its own folder in the war directory after compilation. Some of the files like UI image components that GWT generates are shared between the 2 modules. I was wondering if there was a way to have it organized that only 1 copy of such files is produced?
From what you're said I can only assume you're using the Image widget and given the referenced post, that you might be using an older version of GWT.
I've worked with GWT 2.1 extensively and not knowing what you're trying to do it might be a worthwhile to check out these out:
Declarative Layout - to design multiple pages (Check out #UiTemplate)
ClientBundle - (See ImageResource)
Depending on the size of the images, you can use this approach to reduce the number of trips to the server by either using image sprites or in-lining it altogether. (Note: IE8 has an in-lining ("data:") has a size limitation of 32KB).
Why are you concerned with the generated UI Image components being duplicated? What problem are you trying to solve here?
For all image components which you supply, you can always put them under war/images and they will be shared.

How can I make arbitrary font files available to Java?

I'm using a third-party library which accesses fonts through the GraphicsEnvironment: getAllFonts() call. This includes fonts in font registries belonging to both the JRE and the operating system.
But on client machines that connect to our server I will likely not be able to install fonts into either of these locations. So how can I make other fonts available to the JRE so that this call will pick them up? Is there a way to expand its search path?
I can access a font from an arbitrary file with the Font.createFont() call. But then it still doesn't show up in GraphicsEnvironment.getAllFonts(). Is there a method I can pass that Font to that will add it to the list of fonts available? I'm reading that there are calls that access fonts from a system property, but I'm still not understanding, yet, and I think that just translates a font property I set up into a font name from the available list.
asalamon has provided the correct answer, but I'm stuck on an earlier version of Java that doesn't support it, at least until next year. So more answers are still very welcome! I'm probably going to try to use reflection to hack the library we are using and insert fonts directly into its cache.
Use GraphicsEnvironment.registerFont. (For JDK 1.6)
Try Font.createFont() and bundling the fonts you want to use.

Does Java work with PCF fonts?

I am trying to make IBM jre to use PCF fonts from default X11 installation on my linux box. In particular adobe-helvetica font. I have toyed to modify fontconfig.properties in jre/lib folder but no matter what I do Java seams to use some other fonts. I guess there is some algorithm how java VM tries to link java logical fonts to actual physical fonts in the system even in case when font specified in config could not be used. On Windows it is pretty straight forward, but on Linux I was unable to make it work with anything except TrueType fonts.
Anybody have experience with configuring fonts on IBM jre on Linux?
I've spent all morning learning about Java fonts. There's a lot of limitations, some of which are removed in Java 1.6. I don't have any answers, but here is some information I have that might be helpful.
Java distinguishes internally between system fonts and created fonts. (News flash to Sun: it'd be nice to make that clear through subclassing!) The system fonts seem to be fonts installed in the JRE possibly plus some of the fonts installed on your system. But apparently not all of the fonts installed on your system; I, too, have adobe-helvetica on my Linux system but it doesn't seem to be accessible from Java under any name I can think of.
Meanwhile, you can create fonts from a font file with the Font.createFont() method. You have to specify the font file type. The only constants I found to specify these types in the Font class are TRUETYPE_FONT and TYPE1_FONT. So it looks like if PCF fonts are available, it isn't made explicit through providing a constant to specify them. Maybe somebody else knows more. I did see some information online about using bitmap fonts in Java ME, so it must somehow be possible to use other types of fonts.
Once you have a created font, you can add it to the JVM's concept of available fonts with the GraphicsEnvironment.registerFont() call, but that is only available on Java 1.6 and later.
The PCFFont package seems to provide the ability to use PCF fonts from Java. Unfortunately I think it does so with a custom PCFFont class which does not extend the java.awt.Font class, so I would say its usefulness is probably limited. However, it might give some pointers to implementation details for writing a custom subclass of Font which can handle PCF fonts. Finding the source code isn't immediately obvious; code for that exact class is here.
I would say the author seems a little confused, because he talks about converting TrueType fonts to PCF so you can use them with his library, and Java already supports TrueType fonts natively. But he appears to be an engineer from Sun, so maybe he knew more than I think; also, this could just be very out of date (was there ever a time when Java didn't support TrueType?).

Categories

Resources