Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register font is corrupting .TTF file

Tags:

java

truetype

On my system I need to register two external font .TTF files:

HamletOrNot.ttf (74 KB)
MorrisRoman-Black.ttf (67 KB)

Before creating the Font() objects, I record with the following commands:

/* Set full path of font */
String path = filesPath + fileName;

/* Read file */
Resource resource = applicationContext.getResource( path );
File fontFile = resource.getFile();

/* Load font */
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, fontFile ));

Then I create the objects (I create more than one Font () because the formatting is different):

Font font = new Font( "HamletOrNot" , Font.TRUETYPE_FONT , 10 );
Font font = new Font( "HamletOrNot" , Font.TRUETYPE_FONT , 12 );
Font font = new Font( "MorrisRoman-Black" , Font.TRUETYPE_FONT , 20 );
Font font = new Font( "MorrisRoman-Black" , Font.TRUETYPE_FONT , 22 );

Ok, system works normally. The problem is that after running, the .TTF files change size (to 83 KB and 80 KB), thus:

HamletOrNot.ttf (83 KB)
MorrisRoman-Black.ttf (80 KB)

Then when I go to run the system for the second time, the following error occurs:

...
Caused by: java.awt.FontFormatException: bad table, tag=1280594760
    at sun.font.TrueTypeFont.init(TrueTypeFont.java:513)
    at sun.font.TrueTypeFont.<init>(TrueTypeFont.java:162)
    at sun.font.FontManager.createFont2D(FontManager.java:2474)
    at java.awt.Font.<init>(Font.java:570)
    at java.awt.Font.createFont(Font.java:980)
    at br.com.linu.vectortown.base.screen.font.GameFontFactory.postConstruct(GameFontFactory.java:43)
    at br.com.linu.vectortown.base.screen.font.GameFontContainer.postConstruct(GameFontContainer.java:27)
    at br.com.linu.vectortown.client.looping.util.AutomaticInjector.postConstruct(AutomaticInjector.java:47)
    ... 99 more

If I replace the .TTF files with changed size (83 KB and 80 KB) by the original (with 74 KB and 67 KB), the program works normally.

What am I doing wrong? I thought I'd have to close open files with applicationContext.getResource(), but do not know how to do this. I do not know how to solve it.

Note: I get the applicationContext of the Spring. Original TTF files (with correct sizes) are in the resources folder of the project, while TTF bad files are in the target resource folder (the build is done with Maven).

Help me...

EDIT:

I'm suspicious that maven is corrupting the files. Why? I'm using Eclipse with the Maven plugin. Whenever I do a deploy or press F5 to refresh the project, .TTF files from target folder become corrupted.

Has anyone seen the maven corrupt resource files?

thanks

like image 341
lgapontes Avatar asked Aug 20 '14 11:08

lgapontes


1 Answers

Found it! The maven was corrupting .TTF files when doing the deploy code. To fix, you need to add the extensions that will not be filtered through nonFilteredFileExtension command in pom.xml (maven-resources-plugin).

<plugin>
  <artifactId>maven-resources-plugin</artifactId>
  <version>2.5</version>
  <configuration>
    <encoding>UTF-8</encoding>
    <nonFilteredFileExtensions>
      <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
    </nonFilteredFileExtensions>
  </configuration>
</plugin>

Thank you! xD, ... was lucky!

like image 185
lgapontes Avatar answered Oct 16 '22 08:10

lgapontes