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.
Related
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)
I am printing a receipt using Zebra printer ( Android SDK).
I have checked the documentation of the SDk but could not find any utility class that can help in setting of font size/name/height.
Currently i am hard-coding the parameters and creating a .lbl file but i think that is not a good approach.
So, is their any utility class that would generate the script for .lbl file depending on set of parameters i provided to it.
The Zebra SDK does not provide extensive label design support. It is designed to help with discovery, communication, configuration, and printing to Zebra printers. To my knowledge there is no real element of the SDK that will help you change font sizes dynamically.
Your approach is correct: defined a .LBL template file, and populate the data at print-time. Often, an end-user wil want to use a nearly identical label (in terms of font size, graphics, positioning) and will simply change the printed data. Dynamically changing those elements (font size, for example) doesn't seem to be a common need. That is just my impression.
I am using iText to generate PDF files, which may include embedded fonts. iText includes a DefaultFontMapper class which allows us to specify a directory from which to load fonts that may then be embedded in the generated PDF files as needed. The program itself is run across a range of environments (Windows, Linux, Solaris...) and manually specifying directories has resulted in errors in the past (Due to directories being omitted).
My question is, "Is there a way in java to get all the directories from which fonts are loaded?"
I think it is only possible to list fontNames that are currently used (can be tricky due to GraphicsEnvironment being used on headless environment)
GraphicsEnvironment.getLocalGraphicsEnvironment()
.getAvailableFontFamilyNames();
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.
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?).