Get font file as a File object or get its path - java

I have a Font object in Java for a font file. I need to convert that object to a File object or get the font file path.
Is there a way to do this?
What I'm doing here is calling a method from an external library that loads a font file to use it in writing:
loadTTF(PDDocument pdfFile, File fontfile);
So I wanted to let the user choose a font from the ones defined in his operating system using :
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font[] fonts = e.getAllFonts();
Then when the user chooses a font, I pass it to the loadTTF(...) method to load it.
Is there a bad practice here?

// use reflection on Font2D (<B>PhysicalFont.platName</B>) e.g.
Font f = new Font("Courier New", 0, 10);
Font2D f2d = FontManager.findFont2D(f.getFontName(), f.getStyle(),
FontManager.LOGICAL_FALLBACK).handle.font2D;
Field platName = PhysicalFont.class.getDeclaredField("platName");
platName.setAccessible(true);
String fontPath = (String)platName.get(f2d);
platName.setAccessible(false);
// that's it..
System.out.println(fontPath);

Ok ... This will return the font file path :
String fontFilePath = FontManager.getFontPath( true ) + "/" + FontManager.getFileNameForFontName( fontName );
I have tried this in Windows and Linux and in both it worked fine.

No.
A Font in Java is just a representation and definition of how characters can be displayed graphically. It has nothing to do with the filesystem, and technically need not even be ultimately defined in a file (see for example the createFont() method that takes an arbitrary input stream, which could come from anywhere e.g. a network socket). In any case, it would certainly be a ridiculous break in abstraction for you to be able to get the path of the underlying system file that defines the font.
I would suggest that you might be doing the wrong thing in your other method if you're relying on accepting a file. Or if this really is needed, then you're doing the wrong thing in this method by thinking that a Font object has a simple correlation to an underlying file. If you really need to get the file path of a particular font you'll need to approach it from a different angle that doesn't involve java.awt.Font.

Related

Java - Get icon based on file-name extension

I want to get the default icon for different file extension.
Important: I don't have a java.io.File, but a String that contains the filename + file extension. So something like this:
Icon ico = javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon(file);
won't work.
Anyone an idea?
You probably just want to use the String constructor on a file to create the file. For instance:
File file = new File(filenameString);
And then go from there.
There are probably some caveats to this, so you may want to double-check the docs.
What I ended up doing for all people having the same struggle:
lb = new JLabel(fileName);
File file = new File("C:\\Users\\Robin\\Desktop\\temp\\" + fileName);
file.createNewFile();
Icon ico = Javax.swing.filechooser.FileSystemView.getFileSystemView().getSystemIcon(file);
file.delete();
lb.setIcon(ico);
File is created - the icon gets stored in the ico variable - file is deleted - icon is set
Note: Created file is empty so it doesn't borther too much performancewise.

Embed non-embedded fonts in PDF with IText

So I have the following problem. I receive a PDF file which contains a set of fonts. These fonts are not embedded into the file. Here is a simple example:
I would like to embed these fonts inside the PDF, so they're self-contained and always available. But things don't seem that simple. I'm using IText to do my PDF processing.
I have read and tried the following questions/answers:
how-to-create-pdf-with-font-information-and-embed-actual-font-when-merging-them
embed-truetype-fonts-in-existing-pdf
embed-font-into-pdf-file-by-using-itext
how-to-check-that-all-used-fonts-are-embedded-in-pdf-with-java-itext
Chapter 16.1.4 Replacing a font of the book iText in Action - 2nd Edition
...
But what had gotten me closest was the following example: EmbedFontPostFacto.java (which comes from the book). I was able to embed the Arial font when providing the Arial.ttf file.
But with this, like with other examples, I need the source file of the font in order to embed it. In my case, I don't have the source file. But I might have them on the system however. So I'd like to query my available fonts on the system and see if it corresponds to the given font.
Something of the likes as
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
java.awt.Font[] fonts = e.getAllFonts();
for(java.awt.Font f : fonts){
System.out.println(f.getFontName());
}
But I cannot transform the given java.awt.Font into a RandomAccessFile or a byte[] to be used in order to embed the font file itself. Is there another way for embedding fonts into a PDF, without having the source file of the font itself?
For Windows C:\Windows\Fonts or such contain all font files, and in the explorer shows also font names. So a manual search is feasible.
In java, you have GraphicsEnvironment.getAvailableFontFamilyNames() and Font.getFamilyName() to check for a name from the PDF like "Arial MT."
However a getter for the file is missing from Font.
So list all files of the font directory, and load each file consecutively as Font.
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
Font font = Font.createFont(Font.TRUETYPE_FONT, ttfFile);
ge.registerFont(font); // If you want to load the font.
if (pdfFontName.startsWith(font.getFamilyName()) {
System.out.printf("%s - %s / %s%n", ttfFile.getName(), font.getFamilyName(),
font.getName());
}

Why do my custom fonts work from Eclipse, but not from an exported JAR?

I am trying to hack together a game in Java, and on the start screen I used a custom font I downloaded. When I run the program from Eclipse, the screen looks like this (just as it should):
However, when I run the program from the compiled jar, the screen looks like this:
Here is my code for loading the font:
title = new JLabel("philip k. dick"); // font requires all lowercase
try {
Font f = Font.createFont(Font.TRUETYPE_FONT, new File(Util.getFile("all used up.ttf")));
title.setFont(new Font(f.getName(), f.getStyle(), 150));
} catch (Exception e) {
e.printStackTrace();
Font oldFont = title.getFont();
title.setFont(new Font(oldFont.getName(), oldFont.getStyle(), 100));
}
The method Util.getFile just adds "resources/" to the beginning of the given String.
There are no errors given when I run the jar from the command line. I know that the program can access the font resource because when I rename the "resources" folder (to prevent access), the screen looks like this:
Additionally, I get this error:
java.io.IOException: Can't read resources/all used up.ttf
Of course, THIS is expected.
I would also like to note that I have other audio and image resources being loaded from the same location, and they work fine. This location is in the folder directly outside of the jar. Also, I am using a Mac, but I get the same problem on Windows.
The contents of your resources directly are typically add to your Jar.
This means that they can no longer be access using a File object, as they are actually now part of a Zip file.
You need to use something like getClass().getResource(...) to it up. This returns an instance of URL which points to the resource (if it can be found)
However, Font.createFont takes either a File or InputStream reference, in this case you should use getClass().getResourceAsInputStream(...), something like...
Font f = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsInputStream("resources/all used up.ttf"));
This makes a "relative" path request from the context of the class trying to load the resource. This is probably not going to work, so instead, you could use
getClass().getResourceAsInputStream("/resources/all used up.ttf")
Which creates an absolute path lookup from the context of the classpath
If you get stuck, try unpacking the Jar and see if the font resides within and where and make adjusts as required.
Updated...
Try using...
Font f = Font.createFont(Font.TRUETYPE_FONT, new File("resources/all used up.ttf"));
title.setFont(f.deriveFont(150f));
As I understand it, new Font(...) is trying to find the font from the available system fonts.
From the Java Docs for Font#createFont...
This base font can then be used with the deriveFont methods in this
class to derive new Font objects with varying sizes, styles,
transforms and font features. ...To make the Font available to Font
constructors the
returned Font must be registered in the GraphicsEnviroment by calling
registerFont(Font).

Japanese fonts in a Java GUI

Is it possible to write a GUI in Java that would display Japanese fonts correctly regardless of the language settings of the OS it's being run on?
I'd like to write a program which is able to do this but I'm not sure how to start going about this. Any advice would be a huge help!
The language settings aren't the problem here. Fonts are.
Java certainly can handle Japanese text, no matter what the system settings are, if the developer doesn't fall into the trap of depending on the platform default encoding by using things like FileReader, or new String(byte[]).
But to actually display the Japanese text, you need a font that has Japanese characters, and fonts are not part of Java. Depending on the OS, Japanese-capable fonts may be part of the default installation, or the user may be prompted to install them as needed, or they may have to be installed manually.
Java can easily display Japanese regardless of if the OS has the fonts installed or not, but this is only for Swing applications. Anything using the console window requires fonts installed in the OS.
Steps:
1) Download one of the truetype fonts from here : http://www.wazu.jp/gallery/Fonts_Japanese2.html
2) Use the following code to allow your swing clients to use your fonts:
InputStream fontStream = getClass().getResourceAsStream("/locationoffontonclasspath/myfontname.ttf");
Font japaneseEnabledFont = null;
boolean japaneseDisplayEnabled = false;
try {
japaneseEnabledFont = Font.createFont(Font.TRUETYPE_FONT, fontStream);
GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(japaneseEnabledFont);
japaneseDisplayEnabled = true;
} catch (Exception e) {
// handle exceptions here
} finally {
if (fontStream != null) {
try {fontStream.close();} catch (Exception e1) {}
}
}
if (japaneseDisplayEnabled) {
.....
}
Also, if you wish to use Japanese literals in your sourcecode you have to compile with -Dfile.encoding=utf-8. If using an IDE to compile then you can change the settings on the following screen (right click the project and select properties to get this window): screenshot
More information is available at this page
You can package a TrueType font with your program and load and use it in Swing components like so:
// Create a label in Japanese.
String message = "かつ、尊厳と権利とについて平等である。";
JLabel label = new JLabel(message);
// Load the TrueType font with Japanese characters and apply it.
File file = new File("msmincho.ttf");
Font font = Font.createFont(Font.TRUETYPE_FONT, file);
font = font.deriveFont(Font.PLAIN, 14f);
label.setFont(font);
Be careful to use the correct charset. If compiling Japanese characters in the code you should compile with javac -encoding utf-8 Foo.java. Also be sure to use the charset explicitly when using Readers.
Add -Dfile.encoding=utf-8 to your command line script that's launching the application.
You can copy your truetype Japanese fonts in $JAVA_HOME\jre\lib\fonts\fallback.
You'll have to create this folder. On linux a symbolic link will do the trick.
Source: http://www.asahi-net.or.jp/~ik2r-myr/kanji/german/linos_g.htm

Access file type icons Mac OSX

I am trying to find a way to access the Mac OSX system icons. Does anyone know their default location on a Mac? Or know of a way using Java to have them returned?
There is a method of using JFileChooser for Mac to retrieve an Icon for a file, but the file has to exist on the file system (in my app the file could be streaming from a server so short of creating a dummy file on the system with that extension it wont work).
I can access them on Windows in the following way using SWT (but this bombs on a Mac). The variable "fileType" below for example is ".txt", ".bmp", ".doc", etc:
Program p;
Image image;
//find the correct OS image for the file type and set
//the image to the tree item
p = Program.findProgram(fileType);
ImageData data = p.getImageData();
image = new Image(display, data);
UPDATE: There does not appear to be a clear way to import these. I ended up finding some generic Mac icons online and bundling them with my app to simply use getRecourceAsStream() when on a Mac until a better solution is found.
It is late, but maybe somebody else would search for the same problem (like me).
The FileSystemView trick works only for 16x16 images on all platforms. On Mac, you need to use the default Aqua look and feel to make it work.
For Windows, you can use ShellFolder.getShellFolder(file).getIcon(true) to get a 32x32 icon.
For Mac, you can use Quaqua that comes with some Objective-C jni libs that give you the desired/available icon size for any file (16px, 32, 64, 128, 256, 512) :
http://www.randelshofer.ch/quaqua/javadoc/ch/randelshofer/quaqua/osx/OSXFile.html#getIcon%28java.io.File,%20int%29
On OS X, a FileView works much better than a FileSystemView. I'm using the following to get icons for files:
final JFileChooser fc = new JFileChooser();
//return fc.getFileView().getIcon(f); // will throw a null pointer
Icon result = fc.getUI().getFileView(fc).getIcon(f);
I think the FileSystemView and its friends provide way for getting file icons.

Categories