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?).
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 building an Excel file with Apache POI. After I add all of the content I autosize the columns using sheet.autoSizeColumn(i).
Sometimes it doesn't quite give enough space. I have tried Verdana and Calibri-Regular (had to switch to Calibri-Regular from Calibri because the autosize was going completely crazy all of a sudden on our Windows 7 boxes but worked fine on our Linux boxes, which is a mystery to me). Is there something I can do to fix this? Or is there a way to add a little padding after autosizing?
Edit:
On my Windows dev box I tried setting the font to Verdana with font.setFontName("Verdana"). All of the content definitely changes to Verdana and the sizing on the columns is perfect. On the Linux production box I used the bit of Java code that Gagravarr referenced in the below comments to print out a list of the fonts that Java sees. I tried Monospaced but the spacing was identical to the above screenshot. I then tried Dingbats and the content was still readable (which it shouldn't have been) while the spacing was really off (see screenshot below).
Also openjdk and Oracle JDK do not use the same font engine (SUNâOracle kept its legacy engine for fear any change would break badly coded apps). It's quite possible openjdk will work better on linux for font stuff, since it reuses the same font libraries as the rest of the system, instead of reinventing a square wheel.
Fonts are a legal nightmare; Linux fonts licensing is usually liberal enough you can copy, modify and deploy them anywhere but the reverse is not true (Microsoft was very careful to create vendor lock-in for anyone foolish enough to build apps depending on Windows fonts)
I am not sure whether this will resolve your problem or not, but if you have asked about "is there a way to add a little padding after autosizing?".... yes you can do it by following way.
testSheet.autoSizeColumn(ColIndex);
testSheet.setColumnWidth(ColIndex ,testSheet.getColumnWidth(ColIndex)+PaddingWidth);
Using Oracle(Sun) JDK6 and trying to move to Oracle JDK7
I am using sun.awt.GraphicsEnvironment to find all system fonts in order to use them to change pdf font used in my pdf file. Here is the exact code I am using:
GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
// get all system fonts
final Font[] fonts = gEnv.getAllFonts();
After that I will need to get the exact font file path on the system, so I use:
FontManager.getFontPath(true) + "/" +
FontManager.getFileNameForFontName(font_name);
The problem now is that sun.font.FontManager is no longer a class and has been converted to an interface. I searched online and came up with some solutions that I am not satisfied with and I am looking for other ideas to help solve my problem.
The solutions that I found:
Deploy my project on Java 6 instead of Java 7 (Not recommend as I use some new features in Java 7).
I found the code of the FontManager class online, but using it will require including a lot of other classes/interfaces and the process seems dummy and time consuming. Also I am not if I am allowed to use that code as it's proprietary of Sun company.
What I need is: *A way to find the exact font file path on the system*. All ideas are welcomed.
You're not saying which JDK you use (Oracle, OpenJDK, ..). Possibly you have
FontManagerFactory.getInstance()
available. Or the inteface implementation Win32FontManager (if you're on Windows)?
This code works on Windows 10 and IBM Java 8
sun.font.SunFontManager.getInstance().getPlatformFontPath(true)
But on macOS 11 and AdoptOpenJDK 8/11 an empty string is returned.
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'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.