How to get ttf font data from system fonts in java - java

I have some ttf fonts installed on system.
I get that list using
GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames()
This is not only ttf fonts but all fonts I guess.
Eventually if I use:
Font.decode(fontName)
I can get awt.Font instance.
As far as I know Font is not connected to actual PhysicalFont, so
how can I retrieve either ttf font file, or byte data from that ttf file for a font from that list or from awt.Font ? I am trying to retrieve a physical font data or anything similar. That data should be somewhere right?
The reason I need it is to eventually use with libGDX
FreeTypeFontGenerator in order to generate BitmapFont
This has to work on windows, osx and linux.

It isn't possible. The best you can do is using reflection, but it will only work with an Oracle JRE and accesses a private API so may break with any new Oracle release.
You could probably write a native lib to enumerate the fonts and their files.

As #NateS pointed out, it appears what I want to achieve is not exactly possible.
So I will just share the solution I used in my case for now:
Which is this class:
FontManager.java
This allows to pre-cache existing ttf files in known system locations based on your system, and then make a Map for fontName->fontFile type of connection. this then goes to preferences, and is loaded on next run.
Known issues are
awt.Font has a known bug that is not able to read some ttf font family names on osx systems (mainly some arabic and chinese fonts)
Did not test on Linux, will probably fail.
First run might be slow if you have many fonts.
It would be ideal to write a native lib, but time is of an essence...

Related

Aspose Android via java

I am using aspose java jar for web application and aspose Android via java for mobile application. By default aspose increase font size when i create pdf from mobile . Can someone tell me what exactly aspose doing for mobile?
Thanks for your inquiry. I suspect that the difference in font size is actually the difference in fonts. Android has a limited set of fonts and when Aspose.Words cannot find some fonts upon rendering document to PDF it tries to substitute them. You can configure font substitution rules using FontSubstitutionSetings class.
If you need the exact rendering, then the same set of fonts must be used on different platforms. You can put the required fonts into a folder and use it as font source in FontSettings.

Java apache pdfbox adding an External Font in a Pdf file

I am looking for a way to embedd an external Font to use in on a PDF page (other fonts which are not available by default) in Java Apache PdfBox. Does someone know how to do it.
They have a way to use external fonts as described at
Ref - https://pdfbox.apache.org/1.8/cookbook/workingwithfonts.html :
...PDFBox will load Resources/PDFBox_External_Fonts.properties off of the classpath to map font names to TTF font files. The UNKNOWN_FONT property in that file will tell PDFBox which font to use when no mapping exists.
If you could share the specific issue/error you ran into, if you did, I can offer a better actionable-response.

Set Windows font on Linux in SVG with Apache Batik

I'm developing a simple web application that provides an URL for rendering an SVG image. I use Apache Batik library for SVG rendering and I have a problem with setting font-family. I would like the resulting SVG to look identical (as it can) when viewed from any browser (mainly Chrome, FF and Edge) and any OS (mainly Linux and Windows). The problem is, that as I see, Batik uses some internal logic that loads fonts from the local system, and this prevents cross-platform behaviour.
To be more precise: my webapp runs on Ubuntu Linux, and I set font-family:'DejaVu Sans' to my graphics. This works perfectly when the client uses Linux, but on Windows, obviously this font does not exist. I tried to set font-family according to the User-Agent header in the request, but when I set font-family:'Verdana' for Windows clients, Batik tries to load the 'Verdana' font, and it obviously fails.
I tried to set the attribute like this: font-family:'DejaVu Sans',Verdana but Batik can not handle comma-separated list of valid font families. Is there any way to resolve this besides installing the Verdana font on the server?
Finally I achieved it by including the font in my app and register it with GraphicsEnvironment.getLocalGraphicsEnvironment().registerFont(font);. This way Batik properly keeps the 'Verdana' string in the font-family attribute. Thanks for mike-pomax-kamermans for the helpful advice!

Java - read font characters from ttf file

For android app I'm working right now i have to test that font file supplies all needed characters. Hoverwer I can have a lot ttf files, so i wanted to check every.
But heres my problem: How to read font chars in UTF-8 and iterate through them?
Any help would be appreciated.
You should try using the sfntly library
This has been used in Android Font Creator project
I was able to print glyph ids and names using the test program on a desktop app. Should work on Android as well.
Ok, the way it worked for me was that a friend of mine prepared svg file with font and we could generate data with javascript from that file.

Displaying embedded fonts with PDFBox and Swing

I am using PDFBox to display PDF files inside a JInternalFrame. When opening PDF I get lots of warnings like this:
Changing font on <m> from <Tahoma Negrita> to the default font
I am aware that the fonts being reported are not part of the standard set of 14 fonts. So I decided to check if those fonts are embedded on the PDF file (thinking that there shouldn't be a problem loading embedded fonts, right?).
So I open the file on different readers and check properties/fonts. I am in doubt whether this section reports fonts required by the document or fonts actually embedded in the document.
The information that I get is as follows:
BAAAA+Tahoma-Bold (embedded Subset), type:TrueType, Encoding:
CAAAA+Tahoma (Embedded Subset), type:TrueType, Encoding:
Confused about this, I researched on how to embed fonts from OpenOffice and found that the PDF/A-1a option should be checked. So I made another PDF using this option (in case this was not used when making the original PDF file), yet I got the same results.
I would like your guidance understanding how this works. I would like to be able to open PDF files just as PDF readers do. I also read about the PDFBox_External_Fonts.properties but I am guessing this file shouldn't be modified since I am dealing with embedded fonts.
Thanks.
pdfbox is not able to parse embedded subsets of TrueType fonts.
As far as I understand it, embedded TrueType subsets are missing some metadata for the font file that pdfbox needs.
The bug is known but not easy to solve. Right now I can only advise to use embedded Type 1 Fonts if possible, pdfbox can deal with them.
You can also try to set the path to your complete font files in your pdfbox.jar under org/apache/pdfbox/resources/PDFBox_External_Fonts.properties, so if pdfbox cannot parse the subset, at least it can find a full path to the original font file. Maybe that works, but I have not tested this.
Good Luck!

Categories

Resources